//document.domain = "127.0.0.1" ;

initPage() ;

/********************************************************/
/*		ajax获取组件信息代码							*/
/********************************************************/
/*建立 XMLHttpRequest 对象*/
function creatXMLHttpRequest()
{
	var req ;
	if (window.XMLHttpRequest)
	{
        req = new XMLHttpRequest();
    }
	else if (window.ActiveXObject)
	{
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
	return req ;
}

function jah(url,target,req)
{
    document.getElementById(target).innerHTML = '';
    if (window.XMLHttpRequest)
	{
        //req = new XMLHttpRequest();
        req.onreadystatechange = function() {jahDone(target,req);};
        req.open("GET", url, true);
        req.send(null);
    }
	else if (window.ActiveXObject)
	{
        //req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req)
		{
            req.onreadystatechange = function() {jahDone(target,req);};
            req.open("GET", url, true);
            req.send();
        }
    }
}

/*异步获取组件html代码函数*/
function jahDone(target,req)
{
    if (req.readyState == 4)
	{
        if (req.status == 200)
		{
            var results = req.responseText;
            document.getElementById(target).innerHTML = results;
        }
		else
		{
            document.getElementById(target).innerHTML="loading...";
        }
		req = null ;
    }
}

/*异步获取的组件代码中如果包含javascript代码，需要用这个函数来执行*/
var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
var bOpera = (navigator.userAgent.indexOf('Opera') != -1);
var bMoz = (navigator.appName == 'Netscape');
function execJS(node)
{
	var st = node.getElementsByTagName('SCRIPT');
	var strExec;
	for(var i=0;i<st.length; i++)
	{
		if (bSaf)
		{
			strExec = st[i].innerHTML;
		}
		else if (bOpera)
		{
			strExec = st[i].text;
		}
		else if (bMoz)
		{
			strExec = st[i].textContent;
		}
		else
		{
			strExec = st[i].text;
		}
		try
		{
			eval(strExec);
		}
		catch(e)
		{
			alert(e);
		}
    }
	node.style.display="";
 }

function getRequestBody(oForm)
{
	var aParams=new Array();
	for(var i=0;i<oForm.elements.length;i++)
	{
		var sParam= oForm.elements[i].name;
		sParam+="=";
		sParam+=oForm.elements[i].value;
		aParams.push(sParam);
	}
	return aParams.join("&");
}

/********************************************************/
/*		共用函数										*/
/********************************************************/
function Trim(x)
{
	x = x.replace (/\s/ig,'');
	return x;
}


function getJsData(url , divId)
{
	if(!GetObj(divId))
	{
         var DivObj = document.createElement("div");
         DivObj.id = divId;
         //document.body.appendChild(DivObj);
         if(document.getElementById("commentform"))
         {
         	commentform.appendChild(DivObj);
         }
         else if(document.getElementById("clickComp"))
         {
         	clickComp.appendChild(DivObj);
         }
         else if(document.getElementById("selectComp"))
         {
         	clickComp.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();
}
//删除cookie
function delCookie(name)
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null)
    {
        document.cookie= name + "=" + cval + ";expires=" + exp.toGMTString();
    }
}

function check()
{
	var hasCookies = getCookie("FounderAnswerComment");
	if(hasCookies !=null)
	{
		var cookieValue = decodeURI(hasCookies);
		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 =cookieValue ;

		commentform.appendChild(hasCookie);
		commentform.appendChild(isAnonymous);
		commentform.appendChild(commentLoginName);
	}
	else
	{
		var isAnonymous = document.getElementById("commentIsAnonymous") ;
		/*if(!isAnonymous.checked)
		{	*/
			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;
			}


			//alert("zzzzzzzzzz" +  window.frames["ssoFrame"] ) ;
			//var aaa = window.frames["ssoFrame"].document.body.innerHTML ;
			//alert("11111" + aaa) ;

			/**/
			//到sso系统中验证用户名和密码是否正确
			//var ssoUrl = document.getElementById("ssoUrl").value ;
			//ssoUrl = ssoUrl + "&username=" + userName + "&password=" + password ;
			/*
			var sendUrl="http://127.0.0.1:8080/nis/comment/web/proxysso.jsp?url="+encodeURIComponent(ssoUrl);

			var ssoReq = creatXMLHttpRequest() ;
			var loginType = "0" ; //sso登录结果，0表示登录失败，1表示登录成功
			if (window.XMLHttpRequest)
			{
				ssoReq.open("GET", sendUrl, false);
				ssoReq.send(null);
				loginType = ssoReq.responseText;
			}
			else if (window.ActiveXObject)
			{
				if (ssoReq)
				{
					ssoReq.open("GET", ssoUrl, false);
					ssoReq.send();
					loginType = ssoReq.responseText;
				}
			}
			loginType = loginType.substring(10,11) ;
			*/
			//while(loginType==-1)
			//{
			//}
			//if(loginType == 0)
			//{
			//	alert("会员登录名或密码错误！") ;
			//	return false;
			//}
			//else
			//{
			//	setCookie("loginSession" , userName) ;
			//}
		/**}
		else
		{
			document.getElementById("isAnonymous").value = 1 ;
		}
		**/
	}
	var commentContent  = document.getElementById("commentContent").value ;
	if(commentContent == null || Trim(commentContent) == "")
	{
		alert("评论内容不能为空！") ;
		return false ;
	}
	if(Trim(commentContent).length>1000)
	{
		alert("评论内容不能超过1000个字！") ;
		return false ;
	}
	var PK = document.getElementById("PK") ;
	var isCheck = 0 ;
	if(PK.style.display != "none")
	{
		var radio = document.all("radio") ;
		if(radio[0].checked)
		{
			document.getElementById("pkType").value = radio[0].value ;
			isCheck = 1 ;
		}
		else if(radio[1].checked)
		{
			document.getElementById("pkType").value  = radio[1].value ;
			isCheck = 1 ;
		}
		else if(radio[2].checked)
		{
			document.getElementById("pkType").value  = radio[2].value ;
			isCheck = 1 ;
		}
		if(isCheck==0)
		{
			alert("请选择支持哪一方！") ;
			return false ;
		}
	}



	return true ;
}

function createOption()
{
	var comments ="";
	//var obj = document.body.childNodes;
	var obj = document.all ;
	for(var i = 0;i<obj.length;i++)
	{
		if(obj[i].nodeName=="#comment")
		{
			if(obj[i].nodeValue.indexOf("enpproperty")>-1)
			{
				comments = obj[i].nodeValue ;
				break ;
			}
		}
	}

	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 ;

	if(document.getElementById("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(document.getElementById("clickCommentform"))
	{
		clickCommentform.appendChild(isPk);
		clickCommentform.appendChild(paperID);
		clickCommentform.appendChild(nodeID);
		clickCommentform.appendChild(articleTitle);
		clickCommentform.appendChild(nsID);
		clickCommentform.appendChild(puburl);
	}
	else if(document.getElementById("selectCommentform"))
	{
		selectCommentform.appendChild(isPk);
		selectCommentform.appendChild(paperID);
		selectCommentform.appendChild(nodeID);
		selectCommentform.appendChild(articleTitle);
		selectCommentform.appendChild(nsID);
		selectCommentform.appendChild(puburl);
	}
}

function initPage()
{
	createOption() ;
	if(document.getElementById("commentform"))
	{
		var isstop = 0 ;
		var ispk = 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 topUrl = ajaxUrl+ "&actionType=newCommentTop&nsID=" + nsID  + "&topNum=" + topNum + "&nodeID=" + nodeID;
			getJsData(topUrl , "newCommentTop10") ;
		}
		
		var hasCookies = getCookie("FounderAnswerComment");

		if(hasCookies !=null)
		{
			var cookieValue = decodeURI(hasCookies);
			document.getElementById("commentLogin").innerHTML = "登录名： " + cookieValue ;
			

		}
		else
		{
			document.getElementById("commentIsAnonymous").checked = true ;
			//changeIsAnonymous() ;
		}
	}
	
}

function changeIsAnonymous()
{

//	var isAnonymous =  document.getElementById("commentIsAnonymous") ;
	/**if(isAnonymous.checked)
	{
		document.getElementById("memberPassword").style.display = "none" ;
		document.getElementById("memberName").innerHTML = "昵称：" ;
		document.getElementById("isAnonymous").value = 1 ;
	}
	else
	{**/
//		document.getElementById("memberPassword").style.display = "" ;
//		document.getElementById("memberName").innerHTML = "用户名：" ;
//		document.getElementById("isAnonymous").value = 0 ;
	/**}**/
}
function result()
{
	var url = document.getElementById("commentResult").value ;
	var paperID = document.getElementById("paperID").value ;
	var nodeID = document.getElementById("nodeID").value ;
	var nsID = document.getElementById("nsID").value ;
	var isPk = document.getElementById("isPk").value ;

	url = url + "&paperID=" + paperID +"&nodeID=" + nodeID +"&nsID=" + nsID +"&isPk=" + isPk ;
	window.open(url) ;
}


function gotoComment()
{
	document.getElementById("commentContent").focus();
}


/********************************************************/
/*		表单评论组件处理(普通评论、pk评论)				*/
/********************************************************/
function formComp_submit()
{
	var pass = check() ;
	//alert(pass) ;
	if(pass)
	{
		commentform.submit() ;
	}
	else
		return ;
}

/*
if(document.getElementById("formComp") && document.getElementById("formComp").style.display!="none")
{
	var ajaxUrl = document.getElementById("ajaxUrl").value ;
	var articleId = document.getElementById("nsID").value ;
	var isPK = document.getElementById("isPK").value ;

	var formInfo = ajaxUrl +"&actionType=messageInfo"+"&isPK=" + isPK +"&articleId=" + articleId ;
	var formReq = creatXMLHttpRequest() ;
	formReq.onreadystatechange = function() {setformInfo(formReq);};
    formReq.open("GET", formInfo, true);
    formReq.send(null);
}

function setformInfo(formReq)
{
	if (formReq.readyState == 4)
	{
		if (formReq.status == 200)
		{
			var results = formReq.responseText;
			if(Trim(results)!="")
			{
				var tempResult =  results.split("##founder##") ;
				//alert("tempResult[0] =" + tempResult[0]) ;
				document.getElementById("comment_view").innerHTML=tempResult[0] ;
				//alert("tempResult[1] =" + tempResult[1]) ;
				document.getElementById("comment_count").innerHTML=tempResult[1] ;
			}
		}
		req = null ;
	}
}

*/

/********************************************************/
/*		点击评论组件处理								*/
/********************************************************/
if(document.getElementById("clickComp"))
{
	var clickAjaxUrl = document.getElementById("clickAjaxUrl").value ;
	var nsID = document.getElementById("nsID").value ;
	var compId = document.getElementById("compId").value ;
	
	
	/*
	var optionIds = document.getElementById("optionIds").value ;
	var ids = optionIds.split(",") ;
	for(var i=0 ; i<ids.length ; i++)
	{
		var url = clickAjaxUrl + "&actionType=clickData" + "&nsID="+nsID+"&optionId=" +  ids[i] ;
		var target = "clickCount_" + ids[i] ;
		var req  = creatXMLHttpRequest() ;
		jah(url,target,req) ;
	}
	*/
	var url = clickAjaxUrl + "&actionType=clickData" + "&nsID="+nsID+"&compId=" +  compId ;
	getJsData(url , "clickComment") ;

}

function clickComp_submitCheck(optionId)
{
	/*
	document.getElementById("optionId").value =  optionId;
	//var oForm=document.forms[1];
	var sBody=getRequestBody(clickCommentform);

	var oXmlHttp=creatXMLHttpRequest();
	oXmlHttp.open("post",clickCommentform.action,true);
	oXmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	oXmlHttp.onreadystatechange=function()
	{
		if(oXmlHttp.readyState==4)
		{
			var ret = oXmlHttp.responseText;
			if(ret == "-1")
			{
				alert("请不要重复点击！") ;
			}
			else
			{
				var target = "clickCount_" + optionId ;
				document.getElementById(target).innerHTML = ret ;
			}


		}
		else
		{
		}
	}
	oXmlHttp.send(sBody);
	*/		
	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 actionType = document.getElementById("actionType").value  ;
	var compId = document.getElementById("compId").value  ;

	var url = clickCommentform.action + "?isPk=" + isPk + "&paperID=" + paperID + "&nodeID=" + nodeID + "&articleTitle=" 
		+ articleTitle + "&nsID=" + nsID + "&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) ;
	//document.getElementById("selectValue").value = selectValue ;

	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 actionType = document.getElementById("actionType").value  ;
	var compId = document.getElementById("compId").value  ;

	var url = selectCommentform.action + "?isPk=" + isPk + "&paperID=" + paperID + "&nodeID=" + nodeID + "&articleTitle=" 
		+ articleTitle + "&nsID=" + nsID + "&puburl=" + puburl + "&compTypeId=3" + "&actionType=" + actionType 
		+ "&compId=" + compId + "&selectValue=" + selectValue ;

	getJsData(url , "selectCommentSubmit") ;


	/*

	var sBody=getRequestBody(selectCommentform);
	var oXmlHttp=creatXMLHttpRequest();
	oXmlHttp.open("post",selectCommentform.action,true);
	oXmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	oXmlHttp.onreadystatechange=function()
	{
		if(oXmlHttp.readyState==4)
		{
			var results = oXmlHttp.responseText;
			if(Trim(results)!="")
			{
				if(results == "-1")
				{
					alert("您已经打过分！请不要重复打分！") ;
				}
				else
				{
					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) ;
					}
				}

			}

		}
		else
		{
		}
	}
	oXmlHttp.send(sBody);

	*/
}





if(document.getElementById("selectComp"))
{
	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  ;
	//var req = creatXMLHttpRequest() ;
	//req.onreadystatechange = function() {setSelectComp_count(req);};
    //req.open("GET", selectComp_count, true);
    //req.send(null);
	getJsData(selectComp_count , "selectData") ;
}


/********************************************************/
/*		排行组件处理								*/
/********************************************************/
if(document.getElementById("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 ;
	getJsData(topComp_ajaxUrl , "topComment") ;
}

