﻿function InsertGuestImages(){
	var ImagesStr="";
	for(var i=0; i<32; i++){
		ImagesStr+="<img src=\"images/Face/"+ i +".gif\" onclick=\"InsertImg("+ i +")\" >";
	}
	$("GuestImages").innerHTML=ImagesStr;
}
function InsertImg(id){
	$("GuestContent").focus();
	event.srcElement.document.selection.createRange().text += "[:"+ id +"]";
}

function GetContentTextNum(o){
	if(o.value.trim()==""){
		$("ContentTextNum").innerHTML="";
		return false;
	}
	var TextNum=o.value.trim().replace(/\[:[0-9]{1,2}\]/g, "").length;
	$("ContentTextNum").innerHTML="当前内容<span style=\"color:red;\">"+ TextNum +"</span>个字符";
	if(TextNum>500){
		alert("内容长度为4～500个字符\n\n您输入已经超过了限制。");
	}
}

function CheckGuest(o){
	if($("UserLoginBox").style.display!="none"){
		o.UserName.value=o.UserName.value.trim();
		o.PassWord.value=o.PassWord.value.trim();
		if(o.UserName.value==""){
			alert("用户名不能为空");
			o.UserName.focus()
			return false;
		}
		if(o.PassWord.value==""){
			alert("密码不能为空");
			o.PassWord.focus()
			return false;
		}
	}
	
	if(o.CheckCode.value==""){
		alert("验证码不能为空");
		o.CheckCode.focus()
		return false;
	}
	if(! /^[0-9]{4}$/.test( o.CheckCode.value ) ){
		alert("验证码输入有误");
		o.CheckCode.value="";
		o.CheckCode.focus();
		return false;
	}
	var IsRightCode=GetAjax(null, null, "ajax.asp","action=CheckCode&CheckCodeStr="+ escape(o.CheckCode.value))
	if(IsRightCode!="True"){
		alert("验证码输入错误");
		o.CheckCode.className=(o.CheckCode.className.indexOf("ErrorInput")!=-1)?o.CheckCode.className:o.CheckCode.className + " ErrorInput";
		return false;
	}
	o.GuestTitle.value=o.GuestTitle.value.trim();
	if(o.GuestTitle.value==""&&o.TopicId.value==""){
		alert("主题不能为空");
		o.GuestTitle.focus()
		return false;
	}
	
	o.GuestContent.value=o.GuestContent.value.trim();
	if(o.GuestContent.value==""){
		alert("内容不能为空");
		o.GuestContent.focus()
		return false;
	}
	var re = /\[:[0-9]{1,2}\]/g;
	var arr,j=0;
    while ((arr = re.exec(o.GuestContent.value)) != null){
		j+=1;
	}
	if(j>5){
		alert("抱歉，最多只能插入五个图标");
		return false;
	}
	
	var Contenttext=o.GuestContent.value.replace(/\[:[0-9]{1,2}\]/g, "");
	if(Contenttext.length<4||Contenttext.length>500){
		alert("内容长度为4～500个字符");
		o.GuestContent.focus()
		return false;
	}
	Contenttext=o.GuestContent.value.replace(/\[:([0-9]{1,2})\]/g, "<img src=\"images/Face/$1.gif\" align=\"absmiddle\" />")

	if($("UserLoginBox").style.display!="none"){//用户登录
		var LoginData="action=UserLogin&"
		LoginData+="UserName="+ escape(o.UserName.value)
		LoginData+="&PassWord="+ escape(o.PassWord.value)
		LoginData+="&CheckCode="+ escape(o.CheckCode.value)
		var IsLogin=GetAjax($("Msg"), "<img src='images/loading.gif' width='50' height='30' align='absmiddle'>正在进行登录中，请稍后……", "ajax.asp", LoginData)
		if(IsLogin!="True"){
			var ReType=IsLogin.substr(0,1);
			var ReText=IsLogin.substr(1);
			switch(ReType){
				case "1":
					if(o.UserName.className.indexOf("ErrorInput")==-1)o.UserName.className+=" ErrorInput";
					alert(ReText);
					$("Msg").innerHTML=ReText;
					break;
				
				case "2":
					if(o.PassWord.className.indexOf("ErrorInput")==-1)o.PassWord.className+=" ErrorInput";
					alert(ReText);
					$("Msg").innerHTML=ReText;
					break;
			
				case "3":
					if(o.CheckCode.className.indexOf("ErrorInput")==-1)o.CheckCode.className+=" ErrorInput";
					alert(ReText);
					$("Msg").innerHTML=ReText;
					break;
				case "4":
					alert(ReText);
					$("Msg").innerHTML=ReText;
					break;
			}
			return false;
		}		
	}

	var MsgData="action=SendMessage&"
	MsgData+="Content="+ escape(Contenttext)
	MsgData+="&CheckCode="+ escape(o.CheckCode.value)
	MsgData+="&Title="+ escape(o.GuestTitle.value)
	MsgData+="&TopicId="+ escape(o.TopicId.value)
	//MsgData+="&Redirect="+ escape(o.Redirect.value)
	
	MsgData+="&MsgType=2";
	var TrunMsgStr=GetAjax($("Msg"), "<img src='images/loading.gif' width='50' height='30' align='absmiddle'>正在发送您的留言，请稍后……", "ajax.asp", MsgData)

	if(TrunMsgStr=="False"){
		$("Msg").innerHTML="发送失败，您还没有登录，请登录后再操作";
		alert("发送失败\n\n您还没有登录，请登录后再操作");
		if($("UserLoginBox").style.display=="none")$("UserLoginBox").style.display="block"
		o.UserName.focus();
		return false;
	}
	if(TrunMsgStr!="True"){
		$("Msg").innerHTML=TrunMsgStr;
		alert(TrunMsgStr);
		return false;
	}

	if(TrunMsgStr=="True"){
		if(o.TopicId.value==""){
			$("Msg").innerHTML="您的主题已经成功发送，谢谢您的支持";
			alert("您的主题已经成功发送，谢谢您的支持");
		}else{
			$("Msg").innerHTML="您的回复已经成功发送，谢谢您的支持";
			alert("您的回复已经成功发送，谢谢您的支持");
		}
		
		if($("UserLoginBox").style.display!="none")$("UserLoginBox").style.display="none";
		o.CheckCode.value="";
		$("checkcodeImg").click();
		o.GuestTitle.value="";
		o.GuestContent.value="";
		window.location.reload();

/*

			<div class="GuestMsg">
				<div class="GuestMsgTop">
					<span class="GuestMsgTitle">仙女仙女我爱你就像老鼠爱大米</span>
					<span class="GuestMsgInfo">2007-4-5 12:34:56</span>
				</div>
				<div class="GuestMsgContent">
重复留言,特别是发布其他连接广告等信息,我们会做处理,可能会删除,可能会隐藏。包括QQ空间连接地址可能也会被处理掉。本留言本只是方便大家找寻片源或对本站提出建设性意见。				
				</div>
			</div>




		var notepaperDiv=document.createElement("DIV");
		$("notepaperBox").appendChild(notepaperDiv);
		notepaperDiv.className="notepaper";
		var D=new Date();
		var Now=D.getFullYear() +"-"+ D.getMonth() +"-"+ D.getDate() +" "+ D.getHours() +":"+ D.getMinutes() +":"+ D.getSeconds();
		var HTMLStr="<h1><span disabled >"+ GetCookieValue("qxnUserName") +"  <em>"+ Now +"</em></span><img src=\"images/img/b-close.gif\" width=\"12\" height=\"12\" /><img src=\"images/img/b-big.gif\" width=\"12\" height=\"12\"/></h1>"
		HTMLStr+="<h2>"+ Contenttext +"</h2>";
		notepaperDiv.innerHTML=HTMLStr;
		*/
	}
	return false;
}

function Reply(TopicId){
	$("GuestWriteBox").style.top="-300px";
	$("GuestWriteBox").style.left=parseInt((Browser.Width() - 365)/2)+"px";
	$("GuestWriteBox").style.border="#2B98FF 5px solid"
	window.scrollTo(0,0);
	$("TopicId").value=TopicId;
	$("TopicTitle").style.display="block";
	$("TopicTitle").innerText="回复主题";
	createBgDiv("");
}

function CancelReply(){
	$("GuestWriteBox").style.top="";
	$("GuestWriteBox").style.left="";
	$("GuestWriteBox").style.border=""
	$("TopicId").value="";
	$("TopicTitle").style.display="none";
	$("TopicTitle").innerText="";
	DropBgDiv();
}

function createBgDiv(){
	var	id = "backgroundDiv";
	offHeight = window.screen.height;
	var div = document.createElement("DIV");
	div.id = id;
	div.style.left = 0;
	div.style.top = 0;
	div.style.width=document.body.scrollWidth;
	div.style.height = document.body.scrollHeight;
	div.style.position = "absolute";
	div.style.display = "";
	div.style.zIndex = "999";
	div.style.backgroundColor="#2A4A6B";
	document.body.appendChild(div);
	fadeBg(10, id);
	return div;
}
function DropBgDiv(){
	if($("backgroundDiv")==null)return;
	document.body.removeChild($("backgroundDiv"));
}

function fadeBg(index, str){
	var obj = $(str);
	obj.style.filter = "alpha(Opacity=" + index + ")";
	if (index < 50){
		window.setTimeout("fadeBg(" + (index+5) + ", '" + str + "')", 10);
	}
}





window.onload=InsertGuestImages;
