/**
*
* This is for 91 comment system (http://pz.91.com/)
*
* @package comment
* @author JJC <min43@163.com,jjc3901537@gmail.com>
* @version $Id: get_comment.js,v 1.0 2008.6.17 11:31:05 JJC Exp $
* @copyright (c) 2007 TM WEBSOFT INC
* @license http://opensource.org/licenses/gpl-license.JavaScript GNU Public License
*
*/
var doc = document;

/*
try {
	try {
		if (IsIE && netscape.security.PrivilegeManager.enablePrivilege) {
			netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
		}
	} catch (e) {
		alert(e);
	}
} catch(e) {
	alert(e);
}
*/
// function: get param from url
function getParameter(url, paramName)
{
	var re = new RegExp("(^|\\?|&)"+ paramName+"=([^&]*)(\s|&|$)", "i");

	if (re.test(url))
		return unescape(RegExp.$2);
	else
		return "";
}

// function: get comment path from url
function getPathByUrl(path)
{
	if(path.length == 0) return;
	re = new RegExp("/html/([0-9]{4}/[0-9]{2}/[0-9]{2})/([0-9]{1,})/index\.htm*", "gi");
	re.compile("/html/([0-9]{4}/[0-9]{1,2}/[0-9]{2})/([0-9]{1,})/index\.htm*", "gi");
	arr = re.exec(path);
	re = null;
	if (arr == null || arr == "") return;
	return arr;
}

// function: get comment info
function getcommentinfo_default()
{
	if(userList.length <= 0 || conList.length <= 0) return;

	var listnum = parseInt(comment_js_shownum);
	if(listnum == NaN || listnum < 1)
		listnum = userList.length;
	else
		listnum = (listnum > userList.length) ? userList.length : listnum;

	var str = "";
	var imgurl = "http://images.bbgsite.com/www/pop/";
	for(i=0; i<listnum; i++)
	{
		//str += "<table cellspacing=0 cellpadding=0 border=0 class=comm_js><tr><td class=comm_js_title>     "+userList[i]+"  said          Time: "+timeList[i]+"  </td></tr><tr><td class=comm_js_content><div>  "+conList[i]+"</div><br></td></tr></table>";
	    str +="<div class=\"rev_item\"><div class=\"rev_item_con\"><div class=\"rev_item_con_bg\"><p class=\"rev_item_con_con\">"+conList[i]+"<\/p><p style='border-top:1px dashed #999999;color:#666666;margin-top:10px;padding:5px 10px;text-align:right;'><strong># "+fl[i]+"</strong> <a title=\"Digg\" href=\"#notop\" onClick=\"DoDigg('"+infoidList[i]+"');\"><img alt=\"digg\" title=\"digg\" src=\"http://images.mmosite.com/comment/digg.gif\" align=\"absmiddle\" border=\"0\"></a> <span id=\"diggnum_"+infoidList[i]+"\">"+diggList[i]+"</span>&nbsp;&nbsp;<a title=\"Bury\" href=\"#notop\" onClick=\"DoBury('"+infoidList[i]+"');\"><img alt=\"bury\" title=\"bury\" src=\"http://images.mmosite.com/comment/bury.gif\" align=\"absmiddle\" border=\"0\"></a> <span id=\"burynum_"+infoidList[i]+"\">"+buryList[i]+"</span></p><\/div><\/div><div class=\"rev_user\"><span class=\"orange\">"+userList[i]+"<\/span>   at "+timeList[i]+"  <\/div><\/div>";

	}
	if(commentsContainer != null) commentsContainer.innerHTML = str;
}

// function: show comment info
function showComments()
{
	if(typeof(userList) == "undefined")
	{
		setTimeout("showComments(userList,conList,timeList,infoidList,diggList,buryList,fl)", 200);
	}
	else
	{
		if(typeof(getcommentinfo) == "function")
		{
			//getcommentinfo();
			getcommentinfo_default(userList,conList,timeList,infoidList,diggList,buryList,fl);
		}
		else
		{
			getcommentinfo_default(userList,conList,timeList,infoidList,diggList,buryList,fl);
		}
	}
}

// function: do digg
function DoDigg(id)
{
	var url = "http://comment.bbgsite.com/ajax/getajaxinfo.php";
	var pars = "UseScript=1&Work=digg&id="+id+"&rid="+comment_id+"&path="+comment_path+"&template="+comment_template+"&page="+comment_page;
    
    myAjax(url,pars);
}
// function: do bury
function DoBury(id)
{
	var url = "http://comment.bbgsite.com/ajax/getajaxinfo.php";
	var pars = "UseScript=1&Work=bury&id="+id+"&rid="+comment_id+"&path="+comment_path+"&template="+comment_template+"&page="+comment_page;

	myAjax(url,pars);
}
//function: created ajax by cwq
function myAjax(url,pars)
{
    xmlHttp=false;
	if (window.XMLHttpRequest) 		
	{ 	
		 xmlHttp = new XMLHttpRequest();
	} 
	else if (window.ActiveXObject) 
	{ 		
		try 
		{
			 xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) {}
		}
	}		

	if (!xmlHttp) 
	{
		alert('sorryplease update your ie!');
		return false;
	}		
    
	var newUrl=url+"?"+pars;
		
	xmlHttp.open('GET', newUrl, true);
	xmlHttp.onreadystatechange =function()
	{		
		if (xmlHttp.readyState == 4 && xmlHttp.status==200)
		{}
	}
	xmlHttp.send(null);
	
}

// function: do digg call back
function responseDiggInfo(responseText)
{
	var arr = 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)	// return single id
			{
				doc.getElementById('diggnum_'+arr[1]).innerHTML = parseInt(doc.getElementById('diggnum_'+arr[1]).innerHTML)+1;
			}
		  break;
		default:
			alert("Sorry, an error occurred, please contact the administrators!");
		  break;
	}
	arr = null;
}


// function: do bury call back
function responseBuryInfo(responseText)
{
	var arr = 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
			{
				doc.getElementById('burynum_'+arr[1]).innerHTML = parseInt(doc.getElementById('burynum_'+arr[1]).innerHTML)+1;
			}
			arr1 = null;
		  break;
		default:
			alert("Sorry, an error occurred, please contact the administrators!");
		  break;
	}
	arr = null;
}

function ReloadCommentInfo()
{
	userlist = [];
	var head = doc.getElementsByTagName("head")[0] || doc.documentElement;
	var srcObj = doc.createElement('script');
	try{
		if(comment_js_charset) srcObj.setAttribute('charset', comment_js_charset);
		srcObj.setAttribute('src', comment_js_src + "&time="+new Date().getTime());
		head.appendChild(srcObj);
	}catch(e){
		try{
			if(comment_js_charset) srcObj.src = comment_js_charset;
			srcObj.src = comment_js_src + "&time="+new Date().getTime();
			head.appendChild(srcObj);
			head.removeChild(srcObj);
		}
		catch(e){
		}
	}

	setTimeout("showComments(userList,conList,timeList,infoidList,diggList,buryList,fl)", 2000);
}

// vars for comment
var commentsContainer = doc.getElementById('commentinfoDiv');
var getCommentJsSrc = doc.getElementById('getCommentJs').src;	// eg. src = http://pz.91.com/get_comment.js?comment_url=http://pz.91.com/html/2007/39/25/144/index.htm&comment_num=5
var comment_js_refurl = getParameter(getCommentJsSrc, "comment_url");
var comment_js_shownum = getParameter(getCommentJsSrc, "comment_num");
var comment_js_charset = getParameter(getCommentJsSrc, "comment_charset") || '';
var temp_charset = comment_js_charset ? " charset=\"" + comment_js_charset + "\"" : "";
var comment_js_getnum = comment_js_shownum;
var comment_js_src = "http://comment.bbgsite.com/port/getcomment_info.php?comment=" + comment_js_refurl + "&num=" + comment_js_getnum;
var comment_js_sript = "<scr"+"ipt language=\"javascript\" src=\"" + comment_js_src + "&time="+new Date().getTime() + "\"" + temp_charset + "></scr"+"ipt>";

// vars for digg
var pathArr = getPathByUrl(comment_js_refurl);
var comment_path = pathArr[1]+"/";
var comment_id   = pathArr[2];
var comment_page = 20;
var comment_template = 1;

// Init comment info
doc.write(comment_js_sript);
ReloadCommentInfo();

function set(value){
	var nickname=document.getElementById('name').value;
	var n_checkbox = document.getElementById('n_checkbox').checked;

	if(value=="name" && n_checkbox == true){
	setCookies("comment_nickname",nickname);
	}else if (value=="check" && n_checkbox == true){
		if(!nickname){
			alert("Please input your nickname, before your check 'Remember me'");
	    }else{
			setCookies("comment_nickname",nickname);
	    }
	}else if (value=="name" && n_checkbox.checked == false){
		if(nickname == "" ){setCookies("comment_nickname",'');}
	}
}


//设置cookie
function setCookies(name, value){
	expires=new Date();
	expires.setTime(expires.getTime()+(1000*86400*365));
	document.cookie=name+"="+escape(value)+"; expires="+expires.toGMTString()+"; path=/";
}

//获取cookie
function getCookies(name){
	var start=document.cookie.indexOf(name);
	var end=document.cookie.indexOf(";",start);
	return start==-1 ? null : unescape(document.cookie.substring(start+name.length+1,(end>start ? end : document.cookie.length)));
}

//删除Cookie
function delCookie(name)
{
	var expires = new Date();
	expires.setTime (expires.getTime() - 1000);
	var cval = getCookies (name);
	document.cookie = name + "=" + cval + "; expires="+ expires.toGMTString()+"; path=/";
}