﻿// JavaScript Document
/**
*删除指定的评论信息
*
*/
var path = location.pathname;
var pathArr = getPathByUrl(path);


if (typeof(pathArr) == "undefined" || pathArr == "")
{
	
	alert("parameter error.");
	window.close();
}
if (pathArr.length <3)
{
	
	alert("parameter error.");
	window.close();
}

var comment_path = pathArr[1]+"/";
var comment_id   = pathArr[2];
var comment_pagenum = pathArr[3];

/*********************
若管理员是登陆状态，则
**********************/
//判断管理员是否登陆
function isLogin()
{
	if (getCookie('mmosite_commentadmin') =="" || getCookie('mmosite_commentadmin') == null)
		return false;	
	else
		return true;
}


function setDelMode()
{
	if (!isLogin())
		return false;
	
	if(document.getElementById('admin_div') != null) document.getElementById('admin_div').style.display='none';
	var prefix = "commentinfo_";
	var items = [];
	items = document.getElementsByClassName('commentinfo');
	if (items.length <= 0)
		return false;
	for (var i=0; i<items.length; i++) {
		if ( items[i] && items[i].id && (items[i].id.indexOf(prefix)==0) ) {
			var itemId = items[i].id.substr(prefix.length);
			$('itemId_'+itemId).innerHTML = '<input type="checkbox" name="idList[]" id="idList" value="'+itemId+'" />';
		}
	}
	$('delbuttonDiv').innerHTML = '<input type="button" name="button2" value="Check All" onClick="checkAll(\'idList[]\')" />&nbsp;&nbsp; <input type="button" name="button1" value="Reserv" onClick="checkFAll(\'idList[]\')" />&nbsp;&nbsp; <input type="button" name="del" value="Delete Comment" onClick="checkMoreDelInfoForm();"/>&nbsp;&nbsp; [<a href="javascript:exit();">Login Out</a>]';
	$('delbuttonDivBottom').innerHTML = '<input type="button" name="button2" value="Check All" onClick="checkAll(\'idList[]\')" />&nbsp;&nbsp; <input type="button" name="button1" value="Reserv" onClick="checkFAll(\'idList[]\')" />&nbsp;&nbsp; <input type="button" name="del" value="Delete Comment" onClick="checkMoreDelInfoForm();"/>&nbsp;&nbsp; [<a href="javascript:exit();">Login Out</a>]';	
}

function setNormalMode()
{
	if(document.getElementById('admin_div') != null) document.getElementById('admin_div').style.display='none';
	var prefix = "commentinfo_";
	var items = [];
	
	$('delbuttonDiv').innerHTML = "";
	
	items = document.getElementsByClassName('commentinfo');
	if (items.length <= 0)
		return;
	for (var i=0; i<items.length; i++)
	{
		if ( items[i] && items[i].id && (items[i].id.indexOf(prefix)==0) )
		{
			var itemId = items[i].id.substr(prefix.length);
			$('itemId_'+itemId).innerHTML = "";
		}
	}
	
	return;
}


/**
*多选选择删除信息验证是否有选择复选框
*
*/
function checkMoreDelInfoForm()
{
	if (!isLogin())	
	{
		setNormalMode();
		$('admin_div').style.display = "";
		return false;
	}
	
	
	var idList = getItemList2("idList[]");
	if (idList == "" )
	{
		alert('Please select delete item.');
		return false;
	}
	checkedDelInfo(idList);//删
}

/**
*验证提交评论表单
*参数：无
*返回：提示信息
*/
function checkpubForm()
{
	if ($F('nickname') == "")
	{
	  alert("please input your nickname.");
	  $('nickname').focus();
	  return false;
	}
	
	if ($F('content') == "" )
	{
	  alert("please input comment.");
	  $('content').focus();
	  return false;
	}
	if ($F('content').length>2000)
	{
		alert("content too long.");
		$('content').focus();
		return false;
	}


	$('Work').value = "AddInfo";
	$('id').value = comment_id;
	//alert(comment_id);
	$('pagenum').value = comment_pagenum;
	$('path').value = comment_path;

	//alert($('comment_title').value);
	//$('title').value = $('comment_title').value;
	return true;
}

/**
*管理员登陆验证
*
*
*/
function adminLoginInfo()
{
	if($F('adminuser') == "")
	{
		alert("please input administrator.");
		$("adminuser").focus();
		return false;
	}
	
	if($F("adminpwd") == "")
	{
		alert("please input password.");
		$('adminpwd').focus();
		return false;
	}
	
	if($('divLogining') != null) $('divLogining').style.display = "";

	checkedDelInfo($('delid').value, $("adminuser").value, $('adminpwd').value);
}


function delCommentInfo(id)
{
	
	if (isLogin())		
	{
		checkedDelInfo(id);
	}else //打开登陆框
	{
		$('delid').value = id;
		$('admin_div').style.display = "";
	}
}

/**
*利用ajax删除指定评论信息
*参数：删除评论的ID 
*返回：提示信息
*/
function checkedDelInfo(delid, adminuser, adminpwd)
{
	var url = "/ajax/getajaxinfo.php";
	var pars = "Work=delComment&adminuser="+adminuser+"&adminpwd="+adminpwd+"&id="+delid+"&rid="+comment_id+"&path="+comment_path+"&template="+$('template').value+"&page="+comment_pagenum;
	var myAjax = new Ajax.Request(url, {method: "post", parameters:pars, onComplete: responseDelInfo});
}

function responseDelInfo(request)
{
	var arr = request.responseText.split("*");
	
	if($('divLogining') != null) $('divLogining').style.display = "none";
	
	switch(arr[0])
	{
		case "0":
			alert("No comments exits now, or Delete error. Please try again.");
			history.go(0);
			document.execCommand("refresh");
			document.location.href = location.href
			document.location.reload();
		  break;
		case "2":
			alert("Please input Administrator and password to login.");
			$('admin_div').style.display = "";
		  break;
		case "3":
			alert("Parameter error.");
		  break;
		case "4":
			alert("Administrator or password error.");
			$('admin_div').style.display = "";
		  break;
		case "5":
			alert("Already login.");
			$('admin_div').style.display = "none";
		  break;
		case "6":
			alert("You need re-login.");
			$('admin_div').style.display = "";
		  break;
		case "1":
			$('admin_div').style.display = "none";
			setDelMode();
			
			if (typeof(arr[1])=="undefined" || arr[1] == "")
			{
				alert("Login successfull.");
				break;
			}
			if (arr[1].indexOf(",") == -1)// 氐一ID
			{
				$('commentinfo_'+arr[1]).style.display = "none";
				break;
			}
			var arr1 = arr[1].split(",");
			if (arr1.length <=0)
				break;
			for(i=0;i<arr1.length;i++)
			{
				$('commentinfo_'+arr1[i]).style.display = "none";
			}
		  break;
		default:
			$('admin_div').style.display = "none";
			alert("An error happend.");
		  break;
	}
	arr = null;
	arr1 = null;
}


/**
*顺
*
*/
function exit()
{
	// 删Cookie
	delCookie('mmosite_commentadmin', '/');
	if($('adminpwd') != null) $('adminpwd').value = "";
	$('admin_div').style.display = "none";
	setNormalMode();
	//refreshWindow();
}


/**
*全选
*
*/
function checkAll(objName)
{
	//var cInput = document.all.tags('INPUT');
	var cInput = document.getElementsByTagName('INPUT');
	for(var i = 0; i < cInput.length; i++)
	{
		if( (cInput[i].type == 'checkbox') && (cInput[i].name == objName) )
		{
			cInput[i].checked = true;
		}
	}
}
/**
*全选
*
*/
function checkFAll(objName)
{
	//var cInput = document.all.tags('INPUT');
	var cInput = document.getElementsByTagName('INPUT');
	for(var i = 0; i < cInput.length; i++)
	{
		if( (cInput[i].type == 'checkbox') && (cInput[i].name == objName) )
		{
			cInput[i].checked = !cInput[i].checked;
		}
	}
}

/**
*根据url取得所需参数
*参数：path
*返回：数组
*
*/
function getPathByUrl(path)
{
	
	if (path == "")
		return ;
	re = new RegExp("/html/([0-9]{4}/[0-9]{2}/[0-9]{2})/([0-9]{1,})/([0-9]{1,5}).htm*","gi");
	re.compile("/html/([0-9]{4}/[0-9]{1,2}/[0-9]{2})/([0-9]{1,})/([0-9]{1,5}).shtml*","gi");
	arr = re.exec(path);
	re = null;
	if (arr == null || arr == "")
		return ;

	return arr;

}

function textCounter(obj, maxlimit) { 
	if (obj.length > maxlimit) 
		obj = obj.substring(0, maxlimit); 
	//else 
		document.pubForm.remLen.value = maxlimit - obj.length;
}



function doStr(src) {
	var quote = /\[f=(\d{1,3})\]/gi;
	var src = src.replace(quote, "<img src=http://images.mmosite.com/bbs/smile/$1.gif border=0>");
	document.write(src);
}




function fz(id)
{
	 $('content').value = "[quote="+id+"]";
	 $('defind').value = id;
}

function ChangeFace(face_id)
{
	var sex = parseInt($('sex').value);
	var name = (sex==1)?"1":"2";
	$("user_face").src ="http://images.mmosite.com/comment/face/" + name+ face_id+ ".gif";
	$('headimg').value = name+face_id;
	
}

//设置表情
function setFace(face_id)
{
	
	$('content').value += face_id;
}


//取checkBox值校远鸥
function getItemList2(objName)
{
    var ItemList = "";
	//var cInput = document.all.tags('INPUT');
	var cInput = document.getElementsByTagName('INPUT');
	for(var i = 0; i < cInput.length; i++)
	{
		if( (cInput[i].type == 'checkbox') && (cInput[i].checked == true) && (cInput[i].name == objName) )
		{
			if(ItemList == "") ItemList += cInput[i].value;
			else ItemList += "," + cInput[i].value;
		}
	}

    return ItemList;
}



/**
*利用ajax进行“顶”操作
*参数：删除评论的ID 
*返回：提示信息
*/
function DoDigg(id)
{
	var url = "/ajax/getajaxinfo.php";
	var pars = "Work=digg&id="+id+"&rid="+comment_id+"&path="+comment_path+"&template="+$('template').value+"&page="+$("page").value;
	var myAjax = new Ajax.Request(url, {method: "get", parameters:pars, onComplete: responseDiggInfo});
}

/**
*利用ajax进行“踩”操作
*参数：删除评论的ID 
*返回：提示信息
*/
function DoBury(id)
{
	var url = "/ajax/getajaxinfo.php";
	var pars = "Work=bury&id="+id+"&rid="+comment_id+"&path="+comment_path+"&template="+$('template').value+"&page="+$("page").value;
	var myAjax = new Ajax.Request(url, {method: "get", parameters:pars, onComplete: responseBuryInfo});
}

// Ajax异步回调函数，digg操作完成后
function responseDiggInfo(request)
{
	var arr = request.responseText.split("*");
	
	switch(arr[0])
	{
		case "-1":
			alert("Sorry, this comment doesn't support digg operation!");
		  break;
		case "0":
			window.location.reload();
		  break;
		case "2":
			alert("Sorry, you have digged this comment.");
		  break;
		case "3":
			alert("Parameter error, please try again.");
		  break;
		case "1":
			alert("OK, thank you for your participation.");
			if (arr[1].indexOf(",") == -1)// 氐一ID
			{
				$('diggnum_'+arr[1]).innerHTML = parseInt($('diggnum_'+arr[1]).innerHTML)+1;
			}
		  break;
		default:
			alert("Sorry, an error occurred, please contact the administrators!");
		  break;
	}
	arr = null;
}

// Ajax异步回调函数，bury操作完成后
function responseBuryInfo(request)
{
	var arr = request.responseText.split("*");
	
	switch(arr[0])
	{
		case "-1":
			alert("Sorry, this comment doesn't support digg operation!");
		  break;
		case "0":
			window.location.reload();
		  break;
		case "2":
			alert("Sorry, you have digged this comment.");
		  break;
		case "3":
			alert("Parameter error, please try again.");
		  break;
		case "1":
			alert("OK, thank you for your participation.");
			if (arr[1].indexOf(",") == -1)// 氐一ID
			{
				$('burynum_'+arr[1]).innerHTML = parseInt($('burynum_'+arr[1]).innerHTML)+1;
			}
			arr1 = null;
		  break;
		default:
			alert("Sorry, an error occurred, please contact the administrators!");
		  break;
	}
	arr = null;
}