// JavaScript Document
var xmlHttp2;
function createXMLHttp2Request()
{
    if(window.ActiveXObject)
    { 
        xmlHttp2 = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    else if(window.XMLHttpRequest)
    { 
        xmlHttp2 = new XMLHttpRequest(); 
    } 
}

function doSave()
{
	if(cmtForm.hidename.checked != true)
	{
        if(cmtForm.commentcontent_name.value=="" || cmtForm.commentcontent_name.value.length>30)
		{
            alert("用户名不能为空！或您输入的用户名太长");
            return false;
        }
    }
	else
	{
        cmtForm.username.value="Guest";
    }
    if(cmtForm.commentcontent_content.value=="")
	{
        alert("请填写内容！");
        return false;
    }
    cmtForm.commentcontent_content.value = cmtForm.commentcontent_content.value.replace(/<\/?[^>]+>/g,'');
    cmtForm.commentcontent_content.value = cmtForm.commentcontent_content.value.replace(/[ | ]*\r\n/g,'[p]');
    cmtForm.commentcontent_content.value =cmtForm.commentcontent_content.value.replace(/\n[\s| | ]*\r/g,'\n');
    
	/*Update A Start*/
	var username;
	if(document.getElementById("hidename").checked)
		username = "游客";
	else
		username = cmtForm.commentcontent_name.value;
	/*Update A End*/
	
	var daynow = new Date();
	var daystr = daynow.getFullYear() + "-" + (daynow.getMonth()+1) + "-" + daynow.getDate() + " " + daynow.getHours() + ":" + daynow.getMinutes() + ":" + daynow.getSeconds();
	var inerstr = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">";
	inerstr = inerstr + "<tr><td><span style=\"font-family:\'宋体\'; line-height:150%; color:#CC3300;\">"+username+" "+daystr+"<\/span><\/td><\/tr>";
	inerstr = inerstr + "<tr><td style=\"font-family:\'宋体\'; line-height:150%; border-bottom:1px #DEE1FE dashed; color:#cc3300;\">"+cmtForm.commentcontent_content.value+"<\/td><\/tr>";
	inerstr = inerstr + "</table>";
	
	
	var allstr = document.getElementById("showcmtarea").innerHTML;
	document.getElementById("showcmtarea").innerHTML = inerstr + allstr;
  
  /*Update by Charles @ Oct 29, 2009, commit user's commet by Ajax Method*/
  /*id will be get by the form action property*/
  var actionstr = cmtForm.action;
  var id = actionstr.substring(actionstr.lastIndexOf("content_id=") + 11);
  var comments = cmtForm.commentcontent_content.value;
	var domain = cmtForm.domain_name.value;
	
	postComment(id, username, comments, domain);
}

/*Ajax commit start*/
function postComment(id, username, comments, domain)
{
	createXMLHttp2Request();
    try
    {
    	var posturl;
    	var urlref= document.location.toString();
    	if(urlref.indexOf("http://www.aqsc.cn") != -1)
    	{
    		posturl = "http://www.aqsc.cn/cms/Commentcontent.do?param=saveComment&content_id="+id + "&r=" + Math.random();
    	}
    	else
    	{
    		posturl = "http://www.cworksafety.com/cms/Commentcontent.do?param=saveComment&content_id="+id + "&r=" + Math.random();
    	}
			var sendstr = "&commentcontent_name=" + username + "&comment_content=&commentcontent_content="+ comments +"&domain_name=" + domain;
			sendstr = encodeURI(sendstr);
			xmlHttp2.open("post", posturl, true);
			xmlHttp2.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			xmlHttp2.onreadystatechange = handleResult;
			xmlHttp2.send(sendstr);
    }
    catch(exception)
    {
    	var box = document.getElementById('showcmtarea');
			box.innerHTML = "提交评论出现异常";
			xmlHttp2.abort();
		}
}

function handleResult()
{
	if(xmlHttp2.readyState==4)
	{
		if(xmlHttp2.status==200)
		{
			cmtForm.commentcontent_content.value = "";
			document.getElementById('showplc_top').innerHTML = parseInt(document.getElementById('showplc_top').innerHTML) + 1;
		}
	}
}
/*Ajax commit finish*/

function reply_clearfield()
{
    if(cmtForm.commentcontent_content.value=="请填写评论")
    cmtForm.commentcontent_content.value="";
}

function hidenamea()
{
	if(document.getElementById("hidename").checked)
	{
		document.getElementById("commentcontent_name").disabled = true;
		document.getElementById("commentcontent_name").style.background = '#EEEEEE';
	}
	else
	{
		document.getElementById("commentcontent_name").disabled = false;
		document.getElementById("commentcontent_name").style.background = '#FFFFFF';
	}
}

function viewAllCommentsByID(id, obj)
{
	obj.target="_blank";
	obj.href="http://cms.aqsc.cn/cms/Commentcontent.do?param=queryAllResults&domain=www.aqsc.cn&content_id="+id;
}