﻿var langShowWelcomInfo="欢迎您的回访, <em>%m</em> !";
var langShowRememberInfo="显示/修改信息";
var langRevComments="回复 <em>%m</em> 的评论:";
var langCancleRev="取消回复";
var langPostComments="Leave a reply";

//输出UBB
function DiyexportUbbFrame() {
	if(!objActive){objActive="txaArticle"};
	document.write("<p id=\"UbbFrame\" style=\"display:none;\"></p>");
	document.write("<p>");
	document.write("<a alt=\"\" onmousedown=\"InsertText(objActive,ReplaceText(objActive,'[URL]','[/URL]'),true);\" style=\"padding:2px;cursor:pointer;\">[URL]</a>  ");
	document.write("<a alt=\"\" onmousedown=\"InsertText(objActive,ReplaceText(objActive,'[URL=http://]','[/URL]'),true);\" style=\"padding:2px;cursor:pointer;\">[URL2]</a>  ");
	document.write("<a alt=\"\" onmousedown=\"InsertText(objActive,ReplaceText(objActive,'[EMAIL]','[/EMAIL]'),true);\" style=\"padding:2px;cursor:pointer;\">[EMAIL]</a>  ");
	document.write("<a alt=\"\" onmousedown=\"InsertText(objActive,ReplaceText(objActive,'[EMAIL=@]','[/EMAIL]'),true);\" style=\"padding:2px;cursor:pointer;\">[EMAIL2]</a>  ");
	document.write("<a alt=\"\" onmousedown=\"InsertText(objActive,ReplaceText(objActive,'[B]','[/B]'),true);\" style=\"padding:2px;cursor:pointer;\">[B]</a>  ");
	document.write("<a alt=\"\" onmousedown=\"InsertText(objActive,ReplaceText(objActive,'[I]','[/I]'),true);\" style=\"padding:2px;cursor:pointer;\">[I]</a>  ");
	document.write("<a alt=\"\" onmousedown=\"InsertText(objActive,ReplaceText(objActive,'[U]','[/U]'),true);\" style=\"padding:2px;cursor:pointer;\">[U]</a>  ");
	document.write("<a alt=\"\" onmousedown=\"InsertText(objActive,ReplaceText(objActive,'[S]','[/S]'),true);\" style=\"padding:2px;cursor:pointer;\">[S]</a>  ");
	document.write("<a alt=\"\" onmousedown=\"InsertText(objActive,ReplaceText(objActive,'[QUOTE]','[/QUOTE]'),true);\" style=\"padding:2px;cursor:pointer;\">[QUOTE]</a>  ");
	document.write("<u><a style=\"cursor:pointer;text-align:right;\" onclick=\"InsertUbbFace();if(document.getElementById('UbbFrame').style.display=='none'){document.getElementById('UbbFrame').style.display='block';}else{document.getElementById('UbbFrame').style.display='none'};this.style.display='none'\">"+str06+"</a></u> ");
	document.write("</p>");
}

//插入表情图片HTML代码
function DiyinsertUbbFace() {

	if(!document.getElementById("UbbFrame").innerHTML && strFaceName){

		var aryFileName="";
		var strFileName="";
		var strFaceHtml="";

		aryFileName = strFaceName.split("|");

		for (var i=0;i<aryFileName.length;i++)
		{
			strFileName = aryFileName[i];
			strFaceHtml=strFaceHtml + "<img src=\""+str00+"image/face/"+strFileName+".gif\" title=\""+strFileName+"\" alt=\""+strFileName+"\" onclick=\"InsertText(objActive,'[F]'+this.alt+'[/F]',false);\" style=\"padding:2px;cursor:pointer;\">";
		}
		document.getElementById("UbbFrame").innerHTML=strFaceHtml;
	}

}

//设置cookie
function DiysetCookie(sName, sValue,iExpireDays) {
	if (iExpireDays){
		var dExpire = new Date();
		dExpire.setTime(dExpire.getTime()+parseInt(iExpireDays*24*60*60*1000));
		document.cookie = sName + "=" + escape(sValue) + "; expires=" + dExpire.toGMTString() + "; path=/";
	}
	else{
		document.cookie = sName + "=" + escape(sValue);
	}
}


//保存信息
function DiysaveRememberInfo() {

	var strName=document.getElementById("inpName").value;
	var strEmail=document.getElementById("inpEmail").value;
	var strHomePage=document.getElementById("inpHomePage").value;
	var bolRemember=document.getElementById("chkRemember").checked;


	DiysetCookie("inpName",strName,365);
	DiysetCookie("inpEmail",strEmail,365);
	DiysetCookie("inpHomePage",strHomePage,365);
	DiysetCookie("chkRemember",bolRemember,365);

}

//载入
function DiyloadRememberInfo() {
	var strName=GetCookie("inpName");
	var strEmail=GetCookie("inpEmail");
	var strHomePage=GetCookie("inpHomePage");
	var bolRemember=GetCookie("chkRemember");

	if(bolRemember=="true"){

		if(strName){document.getElementById("inpName").value=strName;};
		if(strEmail){document.getElementById("inpEmail").value=strEmail;};
		if(strHomePage){document.getElementById("inpHomePage").value=strHomePage;};
		if(bolRemember){document.getElementById("chkRemember").checked=bolRemember;};

	}

	if(GetCookie("username")){
		strName=unescape(GetCookie("username"))
		document.getElementById("inpName").value=strName;
}

	//new feature
	if(bolRemember=="true"){
		if ($("#frmSumbit>p:first")[0].className!=="welcome-back"){
			$("#frmSumbit").prepend("<p class=\"welcome-back\"> "+langShowWelcomInfo.replace("%m",strName)+" <a href=\"javascript:void(0)\" onclick=\"DiyshowRememberInfo()\">"+langShowRememberInfo+"</a></p>");
		}
		//hide input
		$("#inpName").parent("p").hide();
		$("#inpEmail").parent("p").hide();
		$("#inpHomePage").parent("p").hide();
	}
}




//show Info
function DiyshowRememberInfo(){
	//show input
	$("#frmSumbit>p:eq(0)").remove();
	$("#inpName").parent("p").show();
	
	//check if reverting
	if ($("#divCommentPost>div.post-top>p:first")[0].className!=="cancle-revert"){
		$("#inpEmail").parent("p").show();
		$("#inpHomePage").parent("p").show();
	}
}

// 回复评论框移动
//Revert Comment
function DiyrevertComment(intRevID) {
	var f=document.getElementById(objActive).form;

	if (f.action.search("act=CommentRev")>0){
		f.action=f.action.replace(/act=CommentRev&id=[0-9]+/g,"act=cmt");
	}

	f.action=f.action.replace("cmd.asp?act=cmt","cmd.asp?act=CommentRev&id="+intRevID);

	//hide input
	var $obj=$("#divCommentPost");
	var $objT=$("#comment"+intRevID);
	var sName=$objT.children("cite").html();
	
	$obj.find("#inpEmail").parent("p").hide();
	$obj.find("#inpHomePage").parent("p").hide();
	$objT.children("div.reply").children("p.cancle").hide();

	//Move Form
	$obj.insertAfter($objT);
	$("#divAjaxComment").insertBefore("#comment"+intRevID);
	
	//Cancle Button
	$obj.children("div.post-top").children("p.cancle-revert").remove();
	$obj.children("div.post-top").children("p").children("a").html(langRevComments.replace("%m",sName));
	$obj.children("div.post-top").prepend("<p class=\"cancle-revert\">[<a href=\"javascript:void(0)\" onclick=\"cancelRevert("+intRevID+")\">"+langCancleRev+"</a>]</p>");

	//Form Effect
	var sFloat,iMargin;
	sFloat=$objT.css("float");
	iMargin="0"; //不知为何有bug...写死之....

	$obj.css("float",sFloat).css("margin-top",-iMargin).hide();
	$obj.fadeIn("slow");

	return true;

}

//取消回复
function cancelRevert(intRevID) {

	var f=document.getElementById(objActive).form;
	if (f.action.search("act=CommentRev")>0){
		f.action=f.action.replace(/act=CommentRev&id=[0-9]+/g,"act=cmt");
	}
	//show reply
	var $objT=$("#comment"+intRevID);
	//show reply
	$objT.children("div.reply").children("p.cancle").show();
;

	//Form Origin
	$("#divCommentPost").css("float","none").css("margin","0");

	//Move Form
	$("#divAjaxComment").appendTo("div.list:first");
	$("#divCommentPost").appendTo("div.list:first");
	//$("div.pagebar",$("#pageContent")).insertBefore("#divAjaxComment");

	//Set val
	$("#divCommentPost>div.post-top>p.cancle-revert").remove();
	$("#divCommentPost>div.post-top>p>a").html(langPostComments);
	
	//show input, check if info is hidden
	if ($("#frmSumbit>p:first")[0].className!=="welcome-back"){
		$("#inpEmail").parent("p").show();
		$("#inpHomePage").parent("p").show();

	}
}




//验证信息
var strFormAction;
function DiyverifyMessage() {

	var strName=document.getElementById("inpName").value;
	var strEmail=document.getElementById("inpEmail").value;
	var strHomePage=document.getElementById("inpHomePage").value;
	var strArticle;
	

	if(document.getElementById("txaArticle").value){
		strArticle=document.getElementById("txaArticle").value;
	}
	else{
		strArticle=document.getElementById("txaArticle").innerText;
	}

	if(strName==""){
		alert(str01);
		return false;
	}
	else{
		re = new RegExp("^[.A-Za-z0-9\u4e00-\u9fa5]+$");
		if (!re.test(strName)){
			alert(str02);
			return false;
		}
	}

	if(strEmail==""){
		//alert(str01);
		//return false;
	}
	else{
		re = new RegExp("^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$");
		if (!re.test(strEmail)){
			alert(str02);
			return false;
		}
	}

	if(typeof(strArticle)=="undefined"){
		alert(str03);
		return false;
	}

	if(typeof(strArticle)=="string"){
		if(strArticle==""){
			alert(str03);
			return false;
		}
		if(strArticle.length>intMaxLen)
		{
			alert(str03);
			return false;
		}
	}

	document.getElementById("inpArticle").value=strArticle;
	document.getElementById("inpLocation").value=parent.window.location.href;
	if(document.getElementById("frmSumbit").action.search("act=cmt")>0){
		strFormAction=document.getElementById("frmSumbit").action;
	}

	//get origin comment id, if new comment return 0.
	var intRevID;
	if(document.getElementById("frmSumbit").action.search("act=CommentRev")>0){
		intRevID=document.getElementById("frmSumbit").action.match(/id=[0-9]+/i).join().replace("id=","");
	}else{
		intRevID=0;
	}


	var strSubmit=$("#frmSumbit :submit").val();
	$("#frmSumbit :submit").val("正在提交");
	$("#frmSumbit :submit").attr("disabled","disabled");
	$("#frmSumbit :submit").addClass("btnloading");
	//disable mng
	//var $objCmtMng=$("p.cmtbtn,p.cancle-revert",$("#cmt"));
	//$objCmtMng.hide();


	//ajax comment begin
	$.post(document.getElementById("frmSumbit").action,
		{
		"inpAjax":true,
		"inpID":$("#inpId").val(),
		"inpVerify":(document.getElementById("inpVerify")?$("#inpVerify").val():""),
		"inpEmail":strEmail,
		"inpName":strName,
		"inpArticle":strArticle,
		"inpHomePage":strHomePage
		},
		function(data){
			var s =data;
			var $objCmtContent;
			if((s.search("faultCode")>0)&&(s.search("faultString")>0))
			{
				alert(s.match("<string>.+?</string>")[0].replace("<string>","").replace("</string>",""))
			}
			else{
				$("#comment"+intRevID).remove();
				$(s).insertBefore("#divAjaxComment");
				$objCmtContent=$("#divAjaxComment").prev("ol.commentlist");
				$objCmtContent.hide();
				if(strFormAction){
					document.getElementById("frmSumbit").action=strFormAction;
				}
				var bolRemember=document.getElementById("chkRemember").checked;
				if(bolRemember==true){
				DiysaveRememberInfo();
				}
				else{
				DiysetCookie("chkRemember",bolRemember,365);
				}
			}
			$("#frmSumbit :submit").removeClass("btnloading");
			$("#frmSumbit :submit").attr("disabled","");
			$("#frmSumbit :submit").val(strSubmit);
			$("#txaArticle").val("");
			//$objCmtMng.show();用了悬停显示，于是乎这个不需要了...
			cancelRevert(); //默认的脚本有这样一个问题，没有取消选项还不说，纵使提交完，函数还有效...
			DiyloadRememberInfo();
			$objCmtContent.show();
			if(document.getElementById("inpVerify")){
				$("#inpVerify").val("");
				var objImageValid=$("img[@src^='"+str00+"function/c_validcode.asp?name=commentvalid']");
				objImageValid.attr("src",str00+"function/c_validcode.asp?name=commentvalid"+"&random="+Math.random());
			}
		}
	);



	return false;
	//ajax comment end

}