﻿String.prototype.trim=function(){
	return this.replace(/(^\s+)|\s+$/g, "");
}

String.prototype.format = function(){
	var tmpStr = this;
	var iLen = arguments.length;
	for(var i=0;i<iLen;i++){
		tmpStr = tmpStr.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i]);
	}
	return tmpStr;
}

Request = {
	QueryString : function(){
		var sValue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)","i"));
		return sValue ? sValue[1] : sValue;
	}
}

Cookie = {
	Set : function (){
		var name = arguments[0], value = escape(arguments[1]), days = 365, path = "/";
		if(arguments.length > 2) days = arguments[2];
		if(arguments.length > 2) path = arguments[3];
		with(new Date()){
			setDate(getDate()+days);
			days=toUTCString();
		}
		document.cookie = "{0}={1};expires={2};path={3}".format(name, value, days, path);
	},
	Get : function (){
		var returnValue=document.cookie.match(new RegExp("[\b\^;]?" + arguments[0] + "=([^;]*)(?=;|\b|$)","i"));
		return returnValue?unescape(returnValue[1]):returnValue;
	}
}

function ajax(_url, _data, _callback, _method){//by dafo
	var oXmlHttp;
		if (window.XMLHttpRequest)		// Gecko
			oXmlHttp = new XMLHttpRequest();
		else if (window.ActiveXObject)	// IE
			oXmlHttp = new ActiveXObject("MsXml2.XmlHttp") ;
	var bAsync = (typeof(_callback) == "function");
	_method = _method || 1;
	var method = _method == 1?"POST":"GET"; 
	oXmlHttp.open(method, _url, bAsync);
	if(_method == 1) oXmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	if (bAsync){	
		oXmlHttp.onreadystatechange = function() 
		{
			if (oXmlHttp.readyState == 4){
				if (oXmlHttp.status == 200)
					_callback( oXmlHttp.responseText ) ;
				else
					alert("XML Request Error: " + oXmlHttp.statusText + " (" + oXmlHttp.status + ")" ) ;
			}
		}
	}
	if(window.ActiveXObject && _method == 2) oXmlHttp.send();
	else oXmlHttp.send(_data);
}

function getObj(id){
	return document.getElementById(id);
}

function getTop(){
	ajax("/comment/commentget.aspx?{0}&rnd={1}".format(_params, Math.random()), null, refreshTop, 2);
}
function refreshTop(){
	getObj("gbooklist").innerHTML = arguments[0];
}

function appendData(reValue){
	if(Cookie.Get("__RememberMe") != "true"){
		getObj("__username").value = "";
		getObj("__email").value = "";
		getObj("__rm").checked = false;
	}
	getObj("__vcode").value = "";
	getObj("__vcode__").src = "/comment/commentvcode.aspx?rnd=" + Math.random();
	var errCode = reValue.match(/^errCode=(\d)/i)[1] | 0;
	var returnValue = reValue.match(/returnValue=(.+)$/i)[1];
	hideTip();
	if(errCode == 0){
		getObj("__content").value = "";
		getTop(); 
	}
	else alert(returnValue);
	getObj("__submit").disabled = false; 

}

function process(_name, _mail, _content, _vcode, _bRm){
	Cookie.Set("__RememberMe", _bRm);
	if(_bRm){
		Cookie.Set("__Name", _name);
		Cookie.Set("__Email", _mail);
	}
	var params = typeof(_params) == "undefined" ? "" : _params;
	var postData = "username={0}&email={1}&content={2}&vcode={3}&{4}".format(_name, _mail, _content, _vcode, params);
	ajax("/comment/commentserver.aspx", postData, appendData);
}

function checkForm(){
	var name = getObj("__username");
	var mail = getObj("__email");
	var content = getObj("__content");
	var code = getObj("__vcode");
	var bRemember = getObj("__rm").checked;
	if(name.value.trim() == ""){
		alert("名字不允许为空");
		name.focus();
		return false;
	}
	if(!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(mail.value)){
		alert("Email格式不正确");
		mail.focus();
		return false;
	}
	if(content.value.length>300 || content.value.length==0){
		alert("评论不允许为空且必须在300字之内");
		content.focus();
		return false;
	}
	if(code.value.trim() == ""){
		alert("请填写验证码");
		code.focus();
		return false;
	}
	getObj("__submit").disabled = true;
	showTip();
	process(name.value, mail.value, content.value, code.value, bRemember);
	return false;
}

function showTip(){
	with(getObj("__tip").style){
		left = document.body.scrollLeft + (parseInt(document.body.clientWidth) - parseInt(width)) / 2 + "px";
		top = document.body.scrollTop + (parseInt(document.body.clientHeight) - parseInt(height)) / 2 + "px";
		display = "block";
	}
}

function hideTip(){
	getObj("__tip").style.display = "none";
}

function showHideForm(btn){
	with(document.getElementById('commentForm').style){display = {"none":"", "":"none"}[display];}
	with(btn){value = {"显示评论表单":"隐藏评论表单", "隐藏评论表单":"显示评论表单"}[value];}
}

function initData(){
 var __shtml =	"	<div class=\"endDeliver\">"	+"\n"+
	"	<form id=\"commentForm\" method=\"post\" action=\"\" onsubmit=\"return false\">"	+"\n"+
	"		<div class=\"title\">"	+"\n"+
	"			<h5>我来评两句<\/h5>"	+"\n"+
	"			<div class=\"login\">"	+"\n"+
	"				您的昵称：<input type=\"text\" id=\"__username\" value=\"{0}\" class=\"s2\" \/>&nbsp;&nbsp;"	+"\n"+
	"				邮件：<input type=\"text\" id=\"__email\" value=\"{1}\"  class=\"s2\"\/>"	+"\n"+
	"				<input type=\"checkbox\" id=\"__rm\" {2}\/>记住我"	+"\n"+
	"			<\/div>"	+"\n"+
	"		<\/div>"	+"\n"+
	""	+"\n"+
	"		<div class=\"content\">"	+"\n"+
	"			<div class=\"textArea\"><textarea id=\"__content\" rows=\"5\" cols=\"55\"><\/textarea><\/div>"	+"\n"+
	"			<div class=\"text\">"	+"\n"+
	"				<img id=\"__vcode__\" src=\"\/comment\/commentvcode.aspx\" alt=\"验证码\" title=\"验证码，可以通过单击右键刷新\" align=\"absbottom\" oncontextmenu=\"this.src=\'\/comment\/commentvcode.aspx?rnd=\'+Math.random();return false;\"\/>"	+"\n"+
	"				<input type=\"text\" id=\"__vcode\"  class=\"s1\"  \/><br \/><br \/>"	+"\n"+
	"				<input type=\"button\" id=\"__submit\" onclick=\"checkForm();\" value=\"提交评论\" \/>"	+"\n"+
	"			<\/div>"	+"\n"+
	"		<\/div>"	+"\n"+
	"		<div class=\"clear\"><\/div>"	+"\n"+
	"		<\/form>"	+"\n"+
	"	<\/div>"	+"\n"+
	"	<div id=\"__tip\" style=\"position:absolute;left:0;top:0;filter:alpha(opacity=70);opacity:0.7;background-color:green;border:1px solid #000;height:25px;width:200px;font:bold 11px Tahoma;line-height:25px;text-align:center;display:none\"><img src=\"\/images\/icon\/waiting.gif\" BORDER=0 alt=\"waiting..\" title=\"waiting..\">waiting...<\/div>";
	if(Cookie.Get("__RememberMe") == "true"){
		__shtml = __shtml.format(Cookie.Get("__Name"), Cookie.Get("__Email"), "checked=\"checked\" ");
	}
	else{
		__shtml = __shtml.format("", "", "");
	}
	getObj("formContainer").innerHTML = __shtml;
	getTop();
	//setInterval(getTop, 6000);
}
window.onload = function(){
	initData();
}