var gCheckRandFlag = 0;//-1表示错误，1表示正确    
var gSubmitFlag = true; //防止重复提交
var  delconten=0;//留言文本框是否删除过，"1"删除过则不再执行删除操作
String.prototype.Trim = function()
	{
	    return this.replace(/(^\s*)|(\s*$)/g, "");
	}
function showRand(){
	 var span_commentrand=document.getElementById("comment_rand");
	  var ubbConid=document.getElementById("ubbContent");
	  if(ubbConid)
	  { 
	    if(delconten==0)
	    {
	    ubbConid.innerHTML="";
	     delconten=1;
	    }
	  }
	 if (span_commentrand == null)
		return;
	if(span_commentrand.innerHTML!="") return;
		span_commentrand.innerHTML = "<span>验证码：</span><input type='text' id='seccode' size='4' maxlength='4' class='mar-r10' value='' ><img class='yzm_img mar-r10 v_middle' width='80' height='25' id='imgsec' border='0' src='/Captcha.jpg'><a href='javascript:showsec(\"imgsec\",\"default\");'>看不清，换一个</span>";  
}    
						  
function check()  
{
	if (!gSubmitFlag){
	  return false;
	}
	var span_error = document.getElementById("err_info");
	var textArea = document.getElementById("ubbContent");
	var content = textArea.value.Trim();
	var userEmail = document.getElementById("user_email");
	var userPassword = document.getElementById("user_password");
	var anyoneDiscuss = document.getElementById("anyoneDiscuss");
	
	var commentRand = document.getElementById("seccode");
	span_error.innerHTML = "";	
	if (commentRand == null){
		if(span_error.innerHTML!="") return false;
		document.getElementById("err_info").innerHTML = "验证码不能为空！";
		//var html_info = document.getElementById("popup_leaveword_err").innerHTML;
		//span_error.innerHTML = html_info;
		showRand();		
		return false;
	}
	var commentRandValue = commentRand.value.Trim();
			if(anyoneDiscuss==null||!anyoneDiscuss.checked)
		{
			
			span_error.innerHTML = "";
			var userEmailValue = "";
			if (userEmail != null){
				userEmailValue = userEmail.value.Trim();
				if(userEmailValue == ""){
					if(span_error.innerHTML!="") return false;
					document.getElementById("err_info").innerHTML = "用户E-mail不能为空！";
					//var html_info = document.getElementById("popup_leaveword_err").innerHTML;
					//span_error.innerHTML = html_info;
					userEmail.focus();
					return false;
				}
				var pattern = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
				flag = pattern.test(userEmailValue);
				if(!flag && userEmailValue.length>0){
					if(span_error.innerHTML!="") return false;
					document.getElementById("err_info").innerHTML = "请输入正确的Email地址！";
					//var html_info = document.getElementById("popup_leaveword_err").innerHTML;
					//span_error.innerHTML = html_info;			
					userEmail.focus();
					return false;
				}
			}
			
			
			
			span_error.innerHTML = "";
			var userPasswordValue = "";
			if (userPassword != null){
				userPasswordValue = userPassword.value.Trim();
				if(userPasswordValue == ""){
					if(span_error.innerHTML!="") return false;
					document.getElementById("err_info").innerHTML = "用户密码不能为空！";
					//var html_info = document.getElementById("popup_leaveword_err").innerHTML;
					//span_error.innerHTML = html_info;				
					userPassword.focus();
					return false;
				}
			}
			
			
			span_error.innerHTML = "";
			if ( commentRandValue == "")
			{ 
				if(span_error.innerHTML!="") return false;
				document.getElementById("err_info").innerHTML = "验证码不能为空！";
				//var html_info = document.getElementById("popup_leaveword_err").innerHTML;
				//span_error.innerHTML = html_info;			
				commentRand.focus();
				return false;
			}
			
			
		}

	
	
	
	
	
	span_error.innerHTML = "";
	if(content.Trim() == "")
	{
		if(span_error.innerHTML!="") return false;
		document.getElementById("err_info").innerHTML = "内容不能为空！";
		//var html_info = document.getElementById("popup_leaveword_err").innerHTML;
		//span_error.innerHTML = html_info;			
		textArea.focus();
		return false;
	}
	
	span_error.innerHTML = "";
	if ( content.length > 300)
	{ 
		if(span_error.innerHTML!="") return false;
		document.getElementById("err_info").innerHTML = "内容不能超过300字！";
		//var html_info = document.getElementById("popup_leaveword_err").innerHTML;
		//span_error.innerHTML = html_info;			
		textArea.focus();
		return false;
	}
  	
   	span_error.innerHTML = "";
	if (commentRandValue.length != 4 || hasDBC(commentRandValue))
	{ 
		if(span_error.innerHTML!="") return false;
		document.getElementById("err_info").innerHTML = "验证码不准确，请重新输入！";
		//var html_info = document.getElementById("popup_leaveword_err").innerHTML;
		//span_error.innerHTML = html_info;		
		commentRand.focus();
		var span_commentrand=document.getElementById("comment_rand");
		span_commentrand.innerHTML = "";
		showRand();
		return false;
	}
	checkRand();
	return false;
}

	//回调，验证码是否准确
function changeRandFlag(){
  if (xmlHttp.readyState == 4){
    if (xmlHttp.status == 200){
      var flag = xmlHttp.responseXML.getElementsByTagName("flag")[0].firstChild.data;
      if(flag=="true"){
        	gCheckRandFlag = 1;
        	document.message_form.target="_self";
        	var nexturl=document.getElementById("nexturl");
        	if(!nexturl&&nexturl.length())
        	{
        	nexturl.value=location.href;
        	}
        	//alert(nexturl.value);
            //document.message_form.action="/createLeaveWord.do";
            document.message_form.submit();
            gSubmitFlag = false;
            return;
      }else {
      	gCheckRandFlag = -1;
      	  //var span_error=document.getElementById("error_list");
	  	  //span_error.innerHTML = "";
		  //if(span_error.innerHTML!="") return false;
		  document.getElementById("err_info").innerHTML = "验证码不准确，请重新输入！";
		  //var html_info = document.getElementById("popup_leaveword_err").innerHTML;
		  //span_error.innerHTML = html_info;			  
		  document.getElementById("seccode").focus();
		  var span_commentrand = document.getElementById("comment_rand");
	      span_commentrand.innerHTML ="";
	      showRand();
		  gCheckRandFlag = 0;	  		
      }
    }
  }
}

function DBC2SBC(str)
{
 var result = '';
 for (i=0 ; i<str.length; i++)
 {
  code = str.charCodeAt(i);//获取当前字符的unicode编码
  if (code >= 65281 && code <= 65373)//在这个unicode编码范围中的是所有的英文字母已经各种字符
  {
   result += String.fromCharCode(str.charCodeAt(i) - 65248);//把全角字符的unicode编码转换为对应半角字符的unicode码
  }else if (code == 12288)//空格
  {
   result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32);
  }else
  {
   result += str.charAt(i);
  }
 }
 return result;
}

function hasDBC(str)
{
 for (i=0 ; i<str.length; i++)
 {
  code = str.charCodeAt(i);//获取当前字符的unicode编码
  if (code >= 256)//在这个unicode编码范围中的是所有的英文字母已经各种字符
  {
   return true;
  }
 }
 return false;
}

var testhttp;
function checkRand(){
	createXMLHttpRequest();
	//testhttp=createXmlHttpRequest_each();
	var url = "/ajaxCheckRand.do?randType=default&rand="+document.getElementById("seccode").value.Trim();
	xmlHttp.open("POST", url, true);
	xmlHttp.onreadystatechange = changeRandFlag;
	xmlHttp.send(null);	
}

function deletemessage(){
	
}