var expertXmlhttpRequest;
var xml_http;
String.prototype.trim = function()
{return this.replace(/(^\s*)|(\s*$)/g, "");}
	
	//取得utf8字符的字节数
function Utf8StringByteLength(s) {
        var totalLength = 0;
        var i;
        var charCode;
        for (i = 0; i < s.length; i++) {
          charCode = s.charCodeAt(i);
          if (charCode < 0x007f) {
            totalLength = totalLength + 1;
          } else if ((0x0080 <= charCode) && (charCode <= 0x07ff)) {
            totalLength += 2;
          } else if ((0x0800 <= charCode) && (charCode <= 0xffff)) {
            totalLength += 3;
          }
        }
        return totalLength;
      } 
      
 function showApplybox(){
 	var obj=document.getElementById("sort-expert");
 	var expert_name=document.getElementById("expert_name"); 
 	obj.style.display="block"; 
 	expert_name.focus();
 }
 
 function applyExpert()
 {
    xml_http = createXMLHttpRequestByObject(xml_http);
    var category_name = document.getElementById("category_name").value;
    
    var url = "/themeExpertApply.do?Action=forApply&category_name=" + encodeURIComponent(category_name);
    xml_http.open("GET", url, true);
    
    xml_http.onreadystatechange = handleApplyExpert;
    xml_http.send(null);
 }
 
 function handleApplyExpert()
 {
     if (xml_http.readyState == 4) {
        if (xml_http.status == 200) {
            var result = xml_http.responseXML.getElementsByTagName("state")[0].firstChild.data;
            if(result == "canApply")
            {
                showApplybox();
            }else if(result == "hasApplied")
            {
            	alert("您已经申请过该类别的专家，请耐心等待");
            }else if(result == "hasBeen")
            {
                alert("您已经是该类别的专家");
            }
        }
     }
 }
 
 function hiddenApplybox(){
 	var obj=document.getElementById("sort-expert");
 	obj.style.display="none";  
 	var expert_name=document.getElementById("expert_name"); 
 	var expert_title=document.getElementById("expert_title");
 	var expert_msn=document.getElementById("MSN");
 	var expert_qq=document.getElementById("QQ");
 	var apply_reason=document.getElementById("apply_reason");
 	expert_name.value=expert_title.value=expert_msn.value=expert_qq.value=apply_reason.value="";
}
function checkexpertapply(categoryname){
        
         var expert_name=document.getElementById("expert_name"); 
 	     var expert_title=document.getElementById("expert_title");
 	     var expert_msn=document.getElementById("MSN");
 	     var expert_qq=document.getElementById("QQ");
 	     var apply_reason=document.getElementById("apply_reason");
 	     
 	     expert_nameValue=expert_name.value.trim();
 	     expert_titleValue=expert_title.value.trim();
 	     expert_msnValue=expert_msn.value.trim();
 	     expert_qqValue=expert_qq.value.trim();
 	     apply_reasonValue=apply_reason.value.trim();
 	     
 	     if(expert_nameValue==""){
 	        alert("专家姓名不能为空!");
 	        return false;
 	     }
 	     if(expert_titleValue==""){
 	      	alert("专家头衔不能为空!");
 	        return false;
 	     }
 	     if(expert_msnValue==""&&expert_qqValue==""){
 	      	alert("专家MSN或QQ必填一项!");
 	        return false;
 	     }
 	     if(apply_reasonValue==""){
 	      	alert("申请专家理由不能为空!");
 	        return false;
 	     }
		if(Utf8StringByteLength(apply_reasonValue)>45000){
			alert("申请理由过长！");
			return false;
		}
		var qq_pattern=/^[0-9]*[1-9][0-9]*$/;
        if(expert_qqValue!="" && !expert_qqValue.match(qq_pattern))
        {
          alert("QQ格式不正确,请检查!");
          return false;
	    }
		var pattern=/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
        if(expert_msnValue!=""&&!expert_msnValue.match(pattern)){
          alert("MSN格式不正确,请检查!");
          return false;
        }
		 hiddenApplybox();
		  expertXmlhttpRequest=createXMLHttpRequestByObject(expertXmlhttpRequest);
		  var para="Action=apply&category_name="+categoryname+"&expert_name="+expert_nameValue+"&expert_title="+expert_titleValue+"&msn="+expert_msnValue+"&qq="+expert_qqValue+"&apply_reason="+apply_reasonValue;
		  var url = "/themeExpertApply.do";
		  expertXmlhttpRequest.open("POST", url, true);
		  expertXmlhttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		  expertXmlhttpRequest.onreadystatechange = callback_expertapply;
		  expertXmlhttpRequest.send(para);
 	}
 	function callback_expertapply(){
 		if (expertXmlhttpRequest.readyState == 4) {
        if (expertXmlhttpRequest.status == 200) {
        	var state = expertXmlhttpRequest.responseXML.getElementsByTagName("state")[0].firstChild.data;
	        	if(state=="notheme")
	        	{
	        		alert("分类主题不存在！");
	        		return false;
	        	}
	        	if(state=="hasapplied")
	        	{
	        	    var expert = expertXmlhttpRequest.responseXML.getElementsByTagName("expert_name")[0].firstChild.data;
	        		alert(expert+"已经申请过该类别的专家，请耐心等待!");
	        		return false;
	        	}
	        	if(state=="isexpert")
	        	{
	        	    var expert = expertXmlhttpRequest.responseXML.getElementsByTagName("expert_name")[0].firstChild.data;
	        		alert(expert+"已经是本类专家，请申报其他专家！");
	        		return false;
	        	}
	        	if(state=="emptyaddress")
	        	{
	        		alert("至少填写一种联系方式！");
	        		return false;
	        	}
	        	if(state=="emptyreason"){
	        		alert("申请理由不能为空！");
	        		return false;
	        	}
	        	if(state=="login")
	        	{
	        		var date=new Date();
	        		date.setTime(date.getTime()+24*3600*1000);
	        		document.cookie='nextURL='+document.location.href+';expire='+date.toGMTString()+';path=/;domain=.hudong.com;';
	        		location="http://www.hudong.com/user/login/userLogin.jsp";
	        		return false;
	        	}
	        	if(state=="fail")
	        	{
	        		alert("申请专家失败!");
	        		//location.reload();
	        		return false;
	        	}
	        	if(state=="success")
	        	{
	        	    alert("申请专家成功!");
	        		//location.reload();
	        		return false;
	        	}
        	}
        }
 	}