initPage() ;

/********************************************************/
/*		共用函数										*/
/********************************************************/
function Trim(x)
{
	x = x.replace (/\s/ig,'');
	return x;
}

function isIE(){ //ie? 
if (window.navigator.userAgent.toLowerCase().indexOf("msie")>=1) 
     return true; 
else 
     return false; 
} 

if(!isIE())
{ 
	//firefox innerText define
     HTMLElement.prototype.__defineGetter__("innerText", 
     function()
     {
         var anyString = "";
         var childS = this.childNodes;
         for(var i=0; i<childS.length; i++)
          {
             if(childS[i].nodeType==1)
                 //anyString += childS[i].tagName=="BR" ? "\n" : childS[i].innerText;
                 anyString += childS[i].innerText;
             else if(childS[i].nodeType==3)
                 anyString += childS[i].nodeValue;
         }
         return anyString;
     } 
     ); 
     
     HTMLElement.prototype.__defineSetter__("innerText", 
     function(sText)
     {
         this.textContent=sText; 
     } 
     ); 
}

function getJsData(url , divId)
{
	if(!GetObj(divId))
	{
         var DivObj = document.createElement("div");
         DivObj.id = divId;
         if(document.forms["commentform"])
         {
        	 document.forms["commentform"].appendChild(DivObj);
         }
         else if(document.forms["clickComp"])
         {
        	 document.forms["clickComp"].appendChild(DivObj);
         }
         else if(document.forms["selectComp"])
         {
        	 document.forms["selectComp"].appendChild(DivObj);
         }
         else if(document.forms["topCompDiv"])
         {
        	 document.forms["topCompDiv"].appendChild(DivObj);
         }
	}
	var Snode = document.createElement("script");
	Snode.setAttribute("type", "text/javascript");
	Snode.setAttribute("language", "javascript");
	Snode.setAttribute("src", url);
	GetObj(divId).innerHTML = "";
	GetObj(divId).appendChild(Snode);
}

function GetObj(objName)
{
	if(document.getElementById)
	{
         return eval('document.getElementById("' + objName + '")');
	}
	else
	{
         return eval('document.all.' + objName);
	}
}

//cookie的相关函数
 function getCookieVal(offset)
{ 
	var endstr = document.cookie.indexOf (";", offset); 
	if(endstr == -1)
		endstr = document.cookie.length; 
    return document.cookie.substring(offset, endstr); 
 } 

function getCookie(name)
{
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen)
    {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return getCookieVal (j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0)
            break;
    }
    return null;
 }
 
function setCookie (name, value)
{
    var exp = new Date();
    exp.setTime (exp.getTime()+60000000);
    document.cookie = name + "=" + value + "; expires=" + exp.toGMTString();
}

function check()
{
	var hasCookies = getCookie("FounderAnswerComment");
	if(hasCookies !=null)
	{
		hasCookies = hasCookies.split('%40');
		var isAnonymous =  document.createElement("input");
		isAnonymous.type = "hidden";
		isAnonymous.id = isAnonymous.name = "isAnonymous";
		isAnonymous.value = 0 ;
		
		var hasCookie =  document.createElement("input");
		hasCookie.type = "hidden";
		hasCookie.id = hasCookie.name = "hasCookie";
		hasCookie.value = 1 ;

		var commentLoginName =  document.createElement("input");
		commentLoginName.type = "hidden";
		commentLoginName.id = commentLoginName.name = "commentLoginName";
		commentLoginName.value =unescape(hasCookies[0]) ;

		document.getElementById("uid").value=hasCookies[1];

		document.forms["commentform"].appendChild(hasCookie);
		document.forms["commentform"].appendChild(isAnonymous);
		document.forms["commentform"].appendChild(commentLoginName);
	}
	else
	{
		var isAnonymous = document.getElementById("commentIsAnonymous") ;
		if(isAnonymous && isAnonymous.checked)
		{
			document.getElementById("isAnonymous").value = 1 ;
		}
		else
		{
			document.getElementById("isAnonymous").value = 0 ;
			var userName = document.getElementById("commentLoginName").value ;
			var password = document.getElementById("commentPassword").value ;
			if(userName == null || Trim(userName) == "")
			{
				alert("会员登录名不能为空！") ;
				return false;
			}
			if(password == null || Trim(password) == "")
			{
				alert("密码不能为空！") ;
				return false;
			}
		}
	}
	var commentContent  = document.getElementById("commentContent").value ;
	if(commentContent == null || Trim(commentContent) == "")
	{
		alert("评论内容不能为空！") ;
		return false ;
	}
	if(Trim(commentContent).length>1000)
	{
		alert("评论内容不能超过1000个字！") ;
		return false ;
	}
	return true ;
}

function createOption()
{
	var obj  = document.body.innerHTML ;
	var comments = obj.substring(obj.indexOf("<!--enpproperty"),obj.indexOf("/enpproperty--")) ;
	var nodeID_value = comments.substring(comments.indexOf("<nodeid>")+8,comments.indexOf("</nodeid>")) ;
	var paperID_value = comments.substring(comments.indexOf("<siteid>")+8,comments.indexOf("</siteid>")) ;
	var nsID_value = comments.substring(comments.indexOf("<articleid>")+11,comments.indexOf("</articleid>")) ;
	var articleTitle_value = comments.substring(comments.indexOf("<title>")+7,comments.indexOf("</title>")) ;
	var puburl_value = window.location.href;


	//添加paperID隐藏域
	var paperID =  document.createElement("input");
	paperID.type = "hidden";
	paperID.id = paperID.name = "paperID";
	paperID.value = paperID_value ;

	//添加nodeId隐藏域
	var nodeID =  document.createElement("input");
	nodeID.type = "hidden";
	nodeID.id = nodeID.name = "nodeID";
	nodeID.value = nodeID_value ;

	//添加articleTitle隐藏域
	var articleTitle =  document.createElement("input");
	articleTitle.type = "hidden";
	articleTitle.id = articleTitle.name = "articleTitle";
	articleTitle.value = articleTitle_value ;

	//添加nsID隐藏域
	var nsID =  document.createElement("input");
	nsID.type = "hidden";
	nsID.id = nsID.name = "nsID";
	nsID.value = nsID_value ;

	//添加稿件url隐藏域
	var puburl =  document.createElement("input");
	puburl.type = "hidden";
	puburl.id = puburl.name = "puburl";
	puburl.value = puburl_value ;

	//添加isPk隐藏域
	var isPk =  document.createElement("input");
	isPk.type = "hidden";
	isPk.id = isPk.name = "isPk";
	isPk.value = 0 ;
	var commentform = document.forms["commentform"] ;
	var clickCommentform = document.forms["clickCommentform"] ;
	var selectCommentform = document.forms["selectCommentform"] ;
	if(commentform)
	{
		var isAnonymous =  document.createElement("input");
		isAnonymous.type = "hidden";
		isAnonymous.id = isAnonymous.name = "isAnonymous";
		isAnonymous.value = 0 ;

		commentform.appendChild(isPk);
		commentform.appendChild(paperID);
		commentform.appendChild(nodeID);
		commentform.appendChild(articleTitle);
		commentform.appendChild(nsID);
		commentform.appendChild(puburl);
		commentform.appendChild(isAnonymous);
	}
	else if(clickCommentform)
	{
		clickCommentform.appendChild(isPk);
		clickCommentform.appendChild(paperID);
		clickCommentform.appendChild(nodeID);
		clickCommentform.appendChild(articleTitle);
		clickCommentform.appendChild(nsID);
		clickCommentform.appendChild(puburl);
	}
	else if(selectCommentform)
	{
		selectCommentform.appendChild(isPk);
		selectCommentform.appendChild(paperID);
		selectCommentform.appendChild(nodeID);
		selectCommentform.appendChild(articleTitle);
		selectCommentform.appendChild(nsID);
		selectCommentform.appendChild(puburl);
	}
}

function initPage()
{
	createOption() ;
	if(document.forms["commentform"])
	{
		var isstop = 0 ;
		var nsID = document.getElementById("nsID").value ;
		var nodeID = document.getElementById("nodeID").value ;
		var paperID = document.getElementById("paperID").value ;
		var ajaxUrl = document.getElementById("ajaxUrl").value ;
		var initUrl = ajaxUrl+ "&actionType=initPage&nsID=" + nsID  + "&targetId=initElement";
		getJsData(initUrl , "initDiv") ;
		var topNum = document.getElementById("topNum").value ;
		if(topNum>0)
		{
			var commentTop10DivText = document.getElementById("newCommentTop10").innerHTML ;
			commentTop10DivText = encodeURIComponent(commentTop10DivText) ;
			var topUrl = ajaxUrl+ "&actionType=newCommentTop&nsID=" + nsID  + "&topNum=" + topNum + "&nodeID=" + nodeID+"&commentTop10DivText="+commentTop10DivText;
			getJsData(topUrl , "CommentTop10") ;
		}
		if(document.getElementById("commentIsAnonymous"))
		{
			var hasCookies = getCookie("FounderAnswerComment") ;
			//hasCookies = hasCookies.split('%40');
			if(hasCookies !=null)
			{
				var cookie = decodeURI(decodeURI (hasCookies));
				//alert(cookie);
				var temp = cookie.split('%40');
				//hasCookies = unescape(hasCookies) ;
				//var temp = hasCookies.split('%40');
				document.getElementById("commentLogin").innerHTML = "登录名： " + unescape(temp[0]) ;
				document.getElementById("uid").value = temp[1] ;
			}
			else
			{
				if(document.getElementById("commentIsAnonymous"))
				{
					document.getElementById("commentIsAnonymous").checked = true ;
					changeIsAnonymous() ;
				}
			}
		}
		
	}
	
}

function changeIsAnonymous()
{
	var isAnonymous =  document.getElementById("commentIsAnonymous") ;
	if(isAnonymous)
	{
		if(isAnonymous.checked)
		{
			document.getElementById("memberPassword").style.display = "none" ;
			document.getElementById("memberName").style.display = "block" ;
			document.getElementById("commentLoginName").style.display = "block" ;
			document.getElementById("memberName").innerHTML = "昵称：" ;
			document.getElementById("isAnonymous").value = 1 ;
		}
		else
		{
			document.getElementById("memberName").style.display = "none" ;
			document.getElementById("commentLoginName").style.display = "none" ;
			document.getElementById("commentPassword").style.display = "none" ;
			document.getElementById("memberPassword").style.display = "none" ;
			document.getElementById("memberName").innerHTML = "用户名：" ;
			document.getElementById("isAnonymous").value = 0 ;
		}
	}
}

function result()
{	
	var commentCount = document.getElementById("commentCount");	
	if (commentCount!=null)
	{								
		var count = commentCount.innerText ;
		if(count==0)
		{
			alert("该文章下还没有评论，欢迎您发表评论！") ;
		}
		else
		{
			var url = document.getElementById("commentResult").value ;
			var paperID = document.getElementById("paperID").value ;
			var nodeID = document.getElementById("nodeID").value ;
			var nsID = document.getElementById("nsID").value ;
		
			url = url + "&paperID=" + paperID +"&nodeID=" + nodeID +"&nsID=" + nsID +"&isPk=0" ;
			window.open(url) ;
		}
	}
	else
	{
			var url = document.getElementById("commentResult").value ;
			var paperID = document.getElementById("paperID").value ;
			var nodeID = document.getElementById("nodeID").value ;
			var nsID = document.getElementById("nsID").value ;
		
			url = url + "&paperID=" + paperID +"&nodeID=" + nodeID +"&nsID=" + nsID +"&isPk=0" ;
			window.open(url) ;	
	}
	
}

function gotoComment()
{
	document.getElementById("commentContent").focus();
}

/********************************************************/
/*		表单评论组件处理(普通评论、pk评论)				*/
/********************************************************/
function formComp_submit()
{
	var pass = check() ;
	if(pass)
	{
		document.forms["commentform"].submit() ;
	}
	else
		return false;
}

function openXXDiv(msgid)
{
	var url = document.getElementById("commentResult").value ;
	url = url.substring(0,url.indexOf("/commentResult.jsp")) ;
	url = url + "/commentContent.jsp?msgId=" + msgid ;
	window.open (url, "newwindow", "height=550, width=600, toolbar= no, menubar=no, scrollbars=no, resizable=no, location=no, status=no,top=100,left=300");
}

/********************************************************/
/*		点击评论组件处理								*/
/********************************************************/
if(document.forms["clickCommentform"])
{
	/*
	var clickAjaxUrl = document.getElementById("clickAjaxUrl").value ;

	var isPk = document.getElementById("isPk").value ;
	var paperID = document.getElementById("paperID").value  ;
	var nodeID = document.getElementById("nodeID").value  ;
	var articleTitle = document.getElementById("articleTitle").value  ;
	var nsID = document.getElementById("nsID").value  ;
	var puburl = document.getElementById("puburl").value  ;
	var compTypeId = document.getElementById("compTypeId").value  ;
	var compId = document.getElementById("compId").value  ;

    var url = clickAjaxUrl + "&actionType=clickData" + "&nsID=" + nsID + "&compTypeId=2"
		+ "&compId=" + compId + "&timestamp=" + new Date().getTime() ;
	getJsData(url , "clickComment") ;
	*/

}

function changeColor()
{
	var cv=new Array();
	var optionIds = document.getElementById("optionIds").value ;
	var optionList = optionIds.split(",") ;
	
	for(i=0;i<optionList.length ;i++)
	{
		cv[i]=Number(document.getElementById("clickCount_"+optionList[i]).innerHTML);
	}
	cv.sort(function(x,y){return parseInt(x)-parseInt(y);}); // 将N个表情的点击数由低到高排序
	var total_num = 0;
	for(i=0;i<optionList.length ;i++)
	{
		var voteon_count=String(document.getElementById("clickCount_"+optionList[i]).innerHTML/cv[cv.length-1]).substr(0,8); // 以最高的那个表情的点击数为1，算占用百分比，取8位
		var tmp=String(voteon_count*39); // 乘以39，得到柱状显示高度
		var v=tmp.split(".");
		if(document.getElementById("clickCount_"+optionList[i]).innerHTML==cv[cv.length-1])
		{
			document.getElementById("image_"+optionList[i]).style.height="39px"; // 最高者为39px
		}
		else
		{
			document.getElementById("image_"+optionList[i]).style.height=v[0]+"px";
		}

		total_num += parseInt(cv[i]); // 得到总数
	}
	document.getElementById("clickview").innerHTML = document.getElementById("clickResult").innerHTML;
}

function clickComp_submitCheck(optionId)
{
	var paperID = document.getElementById("paperID").value  ;
	var nodeID = document.getElementById("nodeID").value  ;
	var articleTitle = document.getElementById("articleTitle").value  ;
	var nsID = document.getElementById("nsID").value  ;
	var puburl = document.getElementById("puburl").value  ;
	var compTypeId = document.getElementById("compTypeId").value  ;
	var actionType = document.getElementById("actionType").value  ;
	var compId = document.getElementById("compId").value  ;
	var clickCommentform = document.forms["clickCommentform"] ;
	var url = clickCommentform.action + "?isPk=0&paperID=" + paperID + "&nodeID=" + nodeID + "&nsID=" + nsID + "&articleTitle="
		+ encodeURIComponent(articleTitle) + "&puburl=" + puburl + "&compTypeId=2" + "&actionType=" + actionType
		+ "&compId=" + compId + "&optionId=" + optionId ;
	getJsData(url , "clickCommentSubmit") ;
}

/********************************************************/
/*		选择评论组件处理								*/
/********************************************************/
function showStars(starNum,rate)
{
	try
	{
		pro = rate + '_';
		greyStars();
		colorStars(starNum);
	}
	catch(e)
	{
	}
}

function colorStars(starNum)
{
	try
	{
		for (var i=1; i <= starNum; i++)
		{
			var tmpStar = document.getElementById(pro + i);
			tmpStar.src = sArray[1].src;
		}
	}
	catch(e)
	{
	}
}
function greyStars()
{
	try
	{
		for (var i=1; i<11; i++)
		{
			var tmpStar = document.getElementById(pro + i);
			tmpStar.src = sArray[0].src;
			}
		}
	catch(e)
	{
	}
}
function setStars(starNum,rate)
{
	rate = rate ;
	try
	{
		var rating = document.getElementById(rate);
		rating.value = starNum;
		showStars(starNum , rate);
	}
	catch(e)
	{
	}
}
function resetStars(rate)
{
	try
	{
		var rating = document.getElementById(rate);
		if (rating.value != '')
	{
		setStars(rating.value,rate);
	}
	else
	{
		greyStars();
	}
	}
	catch(e)
	{
	}
}

function setSelectComp_count(req)
{
	if (req.readyState == 4)
	{
		if (req.status == 200)
		{
			var results = req.responseText;
			if(Trim(results)!="")
			{
				var stars =  results.split(",") ;
				for(var i=0 ; i<stars.length ; i++)
				{
					var rate = stars[i].substring(0,stars[i].indexOf("_")) ;
					var starNum = stars[i].substring(stars[i].indexOf("_")+1) ;
					showStars(starNum,rate) ;
				}
			}
		}
		req = null ;
	}
}

function  selectComp_submitCheck ()
{
	var strOptions = document.getElementById("strOptions").value ;
	var tempOptionId =  strOptions.split(",") ;
	var selectValue = '' ;

	for(var i=0 ; i<tempOptionId.length ; i++)
	{
		var divOption = document.getElementById("star"+i).value ;
		if(divOption == undefined)
		{
			alert("有选项的星级数未选，请重新选择！") ;
			return ;
		}
		else
		{
			selectValue = selectValue + tempOptionId[i] + "_" + divOption + "," ;
		}
	}
	selectValue = selectValue.substring(0,selectValue.length-1) ;

	var paperID = document.getElementById("paperID").value  ;
	var nodeID = document.getElementById("nodeID").value  ;
	var articleTitle = document.getElementById("articleTitle").value  ;
	var nsID = document.getElementById("nsID").value  ;
	var puburl = document.getElementById("puburl").value  ;
	var compTypeId = document.getElementById("compTypeId").value  ;
	var actionType = document.getElementById("actionType").value  ;
	var compId = document.getElementById("compId").value  ;

	var url = document.forms["selectCommentform"].action + "?isPk=0&paperID=" + paperID + "&nodeID=" + nodeID + "&articleTitle=" 
		+ encodeURIComponent(articleTitle) + "&nsID=" + nsID + "&puburl=" + puburl + "&compTypeId=3" + "&actionType=" + actionType 
		+ "&compId=" + compId + "&selectValue=" + selectValue ;
	getJsData(url , "selectCommentSubmit") ;
}

if(document.forms["selectCommentform"])
{
	var selectComp_greyStarUrl = document.getElementById("selectComp_greyStarUrl").value ;
	var selectComp_colorStarUrl = document.getElementById("selectComp_colorStarUrl").value ;

	var sArray = new Object;
	sArray[0] = new Image();
	sArray[0].src = selectComp_greyStarUrl ;
	sArray[1] = new Image();
	sArray[1].src = selectComp_colorStarUrl ;
	var pro ; var rate ;
	
	var nsID = document.getElementById("nsID").value ;
	var selectAjaxUrl = document.getElementById("selectAjaxUrl").value ;
	var selectComp_count = selectAjaxUrl + "&actionType=selectData" + "&nsID=" + nsID  ;
	getJsData(selectComp_count , "selectData") ;
}


/********************************************************/
/*		排行组件处理								*/
/********************************************************/
if(document.forms["topComp"])
{
	var topComp_type = document.getElementById("topComp_type").value ;
	var topComp_score = document.getElementById("topComp_score").value ;
	var topComp_fieldId = document.getElementById("topComp_fieldId").value ;
	var topComp_ajaxUrl = document.getElementById("topComp_ajaxUrl").value ;
	var topComp_num = document.getElementById("topComp_num").value ;
	topComp_ajaxUrl = topComp_ajaxUrl + "&actionType=topCompData" + "&topCompType=" 
				+ topComp_type + "&topCompScore=" + topComp_score + "&topCompFieldId=" + topComp_fieldId + "&topCompNum=" + topComp_num ;
	alert(topComp_ajaxUrl) ;
	getJsData(topComp_ajaxUrl , "topComment") ;
}



