var gCheckRandFlag = 0;//-1表示错误，1表示正确    
var gSubmitFlag = true; //防止重复提交



function showRand(){
     
	 var span_commentrand=document.getElementById("comment_rand");
	 ranNum = Math.random();
	 if (span_commentrand == null)
		return;
	if(span_commentrand.innerHTML!="") return;
		span_commentrand.innerHTML = "<span>验证码：<input type='text' id='seccode' size='4' maxlength='4' style='height:15px' value='' ><img align='absmiddle' width='80' height='25' id='imgsec' border='0' src='/Captcha.jpg?random="+ranNum+"'></span><span style='margin:5px 0; display:block'>(输入上图中数字)<a href='javascript:showsec(\"imgsec\");'>看不清，换一个</a></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 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();


	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(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 > 2000)
	{ 
		if(span_error.innerHTML!="") return false;
		document.getElementById("err_info").innerHTML = "内容不能超过2000字！";
		//var html_info = document.getElementById("popup_leaveword_err").innerHTML;
		//span_error.innerHTML = html_info;			
		textArea.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 (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(){
//	alert("changeRandFlag");
  if (testhttp.readyState == 4){
    if (testhttp.status == 200){
      var flag = testhttp.responseXML.getElementsByTagName("flag")[0].firstChild.data;
          if(flag=="true"){
        	gCheckRandFlag = 1;
        	document.message_form.target="_self";
            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(){
//	alert("checkRand");
	//createXMLHttpRequest();
	testhttp=createXmlHttpRequest_each();
//	alert("xmlHttp");
	var commentRand = document.getElementById("seccode");
//	alert(commentRand);
	var url = "/ajaxCheckRand.do?rand="+commentRand.value.Trim();
//	alert(url);
	testhttp.open("POST", url, true);
	testhttp.onreadystatechange = changeRandFlag;	
	testhttp.send(null);	
}
function createXmlHttpRequest_each() {
	var xmlHttp;
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		if (window.XMLHttpRequest) {
			xmlHttp = new XMLHttpRequest();
		}
	}
	if (!xmlHttp) { // 异常，创建对象实例失败
		window.alert("\u4e0d\u80fd\u521b\u5efaXMLHttpRequest\u5bf9\u8c61\u5b9e\u4f8b.");
		return false;
	}
	return xmlHttp;
}

function deletemessage(){
	
}