function isemail (s)
{
        // Writen by david, we can delete the before code
        if (s.length > 100)
        {
             window.alert("email地址长度不能超过100位!");
                return false;
        }
				s = s.toLowerCase()
				var regu = "^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*))@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2}|net|NET|com|COM|gov|GOV|mil|MIL|org|ORG|edu|EDU|int|INT|name|NAME)$";
				var re = new RegExp(regu);
         if (s.search(re) != -1) {
               return true;
         } else {
               window.alert ("请输入有效合法的E-mail地址 ！");
               return false;
         }
}
function ChangeOption(ObjName, DesName)
{
  //GET OBJECT VALUE, ID OF DESTINATION OBJECT
  ObjID    = GetObjID(ObjName);
  DesObjID = GetObjID(DesName);
  if ( ObjID != -1 && DesObjID != -1 )
  {
    CatValue = document.Form1.elements[ObjID].value.substring(0, 2);
    if ( CatValue == "0" )
      document.Form1.elements[DesObjID].length=0;
    else
    { //PARSING
      document.Form1.elements[DesObjID].length=0;
      var tt = document.Form1.AreaCode.value;
      var SubCategory = tt.split(";");
      //GENERATE OPTIONS
      j=0;
      for (var i=0; i< SubCategory.length; i++ )
      { pp = SubCategory[i].split(",");
        if ( pp[0].substring(0,2) == CatValue )
        {  if ( pp[0].substring(0,2) == CatValue)
           {  document.Form1.elements[DesObjID].options[j]= new Option( pp[1], pp[0]);
              j++;
           }
        }//if
      }//for
    }//if
  }//if
  //Add by Eliza ,if HK ,then Nation code to 825  2003-1-27 


}

function GetObjID(ObjName)

{ for (var ObjID=0; ObjID < document.Form1.elements.length; ObjID++)

    if ( document.Form1.elements[ObjID].name == ObjName )

    {  return(ObjID);

       break;

    }

  return(-1);

}

function ToReset(){

	document.Form1.company.value = "";
	document.Form1.passwordhp.value = "";
	document.Form1.nickname.value = "";
	document.Form1.loginId.value = "";
	document.Form1.password.value = ""
	document.Form1.password_ag.value = "";
	document.Form1.email.value = "";

}	

function ToSave(num)

{
	CheckOK(num);
}


function getProvince(){
	
     var se =document.getElementById("LocCat");  
     var option=se.getElementsByTagName("option");  
     var str = "" ;  
     
     for(var i=0;i<option.length;++i)  
     {  
       if(option[i].selected)  
       {   
          document.getElementById("province").value = option[i].text;   
       }  
     }  
}
function getCity(){
	
     var se =document.getElementById("Location");  
     var option=se.getElementsByTagName("option");  
     var str = "" ;  
     for(var i=0;i<option.length;++i)  
     {  
       if(option[i].selected)  
       {   
           document.getElementById("city").value =  option[i].text;   
       }  
     }  
}

function CheckOK(num)
{
 getProvince();
 getCity();
 

  if(num=1){
		
	if (document.Form1.passwordhp.value == "")
		{
			window.alert("惠普密码不能为空!");
			document.Form1.passwordhp.focus();
			return ;
		}
  }
    if (document.Form1.loginId.value == "")
  {
    window.alert("用户名不能为空!");
    document.Form1.loginId.focus();
    return ;
  }

  var Char = document.Form1.loginId.value.charAt(0);
  if(!isalpha(Char)){
	window.alert("用户名首位必须是字母！");
    document.Form1.loginId.focus();
	document.Form1.loginId.value='';
    return ;
  }

  if(!check_userid(document.Form1.loginId.value)){
	window.alert("用户名只能是英文字母、数字、下划线和点组成！");
	document.Form1.loginId.value='';
    document.Form1.loginId.focus();
    return ;
  }
    if (document.Form1.loginId.value.length < 3)
  {
    window.alert("用户名长度必须大于3位!");
	document.Form1.loginId.value='';
    document.Form1.loginId.focus();
    return ;
  }

    if (document.Form1.password.value == "")
  {
    window.alert("密码不能为空!");
    document.Form1.password.focus();
    return ;
  }

    if (document.Form1.password.value.length <6)
  {
    window.alert("密码长度必须大于5位!");
	document.Form1.password.value='';
    document.Form1.password.focus();
    return ;
  }

  if(!check_userid(document.Form1.password.value)){
	window.alert("密码只能是英文字母、数字、下划线和点组成！");
	document.Form1.password.value='';
    document.Form1.password.focus();
    return ;
  }

      if (document.Form1.password_ag.value == "")
  {
    window.alert("确认密码不能为空!");
    document.Form1.password_ag.focus();
    return ;
  }
    if (document.Form1.email.value == "")
  {
    window.alert("邮箱不能为空!");
    document.Form1.email.focus();
    return ;
  }
    
    var pwd=document.Form1.password.value;
  	var pwd_ag=document.Form1.password_ag.value;
  	
  
   if(pwd!=pwd_ag)
	{	
		window.alert("密码不一至请重新输入密码！");
		document.Form1.password.value=''
		document.Form1.password_ag.value=''
		document.Form1.password.focus();
    	return ;
    }
	//email检验
    if(!isemail(document.Form1.email.value))
    {
	   document.Form1.email.value='';
	   document.Form1.email.focus();
       return ;
    }
	

	var url = './userSerAction.do?';
	var pars ='method=drmAdduser&userid='+document.Form1.loginId.value+'&hpUserid='+document.Form1.company.value+'&hpPwd='+document.Form1.passwordhp.value+'&newPassword='+document.Form1.password.value+'&cardid='+document.Form1.company.value;


	new Ajax.Request(url,{method:'post',parameters:pars,onComplete :checkUser});

}

//ajax call_back function
function checkUser(transport){	
	var status=0;
	status=parseInt(transport.responseText);
	if(status==3){
		document.Form1.passwordhp.focus();
		alert("密码错误");
	}
	if(status==0){
		document.Form1.submit();

	}
	if(status==1){
		alert("用户名已经被注册了!");
		document.getElementById("loginUserid").style.display = "inline";
	}
	if(status==2){
		alert("连接数据库失败，请与管理员联系!");
	}
}

function chick_img(num){
	var div_zlxg = $('div_zlxg');
	var div_wdzl = $('div_wdzl');

	if(num==1){
		div_wdzl.style.display ="inline";
		div_zlxg.style.display ="none";
	}else{
		div_wdzl.style.display ="none";
		div_zlxg.style.display ="inline";
	}
}

function ToSubmit(){
	var email = $('email');
    if(email.value.indexOf("'")>=0){
		alert("用户名输入有误!");
		email.focus();
		return 
	}
	if(email.value==""){
		window.alert("用户名不能为空!");
		email.focus();
		return 
	}
	document.FormEmail.submit();
}
function ToResetEmail()
{
	var email = $('email');
	email.value='';
}

//校验英文字母、数字、下划线和点组成
function check_userid(s){
    for (var i=0; i<s.length; i++)
    {
        var Char = s.charAt(i);
		if(!isalpha(Char)&&!isnumeric(Char)&&Char!="_"&&Char!=".")
			return false;
    }
    return true;
}

//1、校验输入是不是字母

function isalpha(s)
{
    for (var i=0; i<s.length; i++)
    {
        var Char = s.charAt(i);
        if ((Char < "a" || Char > "z") && (Char < "A" || Char > "Z"))
            return false;
    }
    return true;
}


//2、数字校验

function isnumeric(p)
{
 if (p == "")
  return false;
 var l = p.length;
 var count=0;
 for(var i=0; i<l; i++)
 {
  var digit = p.charAt(i);
  if(digit == "." )
 {
    ++count;
    if(count>1) return false;
   }
  else if(digit < "0" || digit > "9")
  return false;
 }
 return true;
}

function goTo(){
	document.location.href='./index.jsp'
}
function loginId_in(){
	document.getElementById('loginId').style.borderColor='#99E300';
	document.getElementById("loginUserid").style.display = "none";

}
function loginId_out(){
	var id = $('loginId');
	var err1 = $('errspan1');
	var err2 = $('errspan2');
	var err3= $('errspan3');

	var Char = id.value.charAt(0);
	if(!isalpha(Char)){
	    err1.className = "user";
		err2.className = "zi";
		err3.className = "zi";
		id.style.borderColor='red';
		//id.focus();
		//id.value='';
		return ;
	}

  if(!check_userid(id.value)){
	    err1.className = "zi";
		err2.className = "user";
		err3.className = "zi";
		id.style.borderColor='red';
	 // id.value='';
     // id.focus();
      return ;
  }

  if(id.value.length < 3){
	    err1.className = "zi";
		err2.className = "zi";
		err3.className = "user";
		id.style.borderColor='red';
	 // id.value='';
     // id.focus();
      return ;
  }

  	var url = './userSerAction.do?';
	var pars = 'method=checkUserid&userid='+document.Form1.loginId.value;


	new Ajax.Request(url,{method:'post',parameters:pars,onComplete :checkUser_out});

	    err1.className = "zi";
		err2.className = "zi";
		err3.className = "zi";
  	    id.style.borderColor='#A3BFA8';
}

//ajax call_back function
function checkUser_out(transport){	
	var status=0;
	//alert(transport.responseText);
	status=parseInt(transport.responseText);
	if(status==0){
		//document.Form1.submit();

	}
	if(status==1){
		//document.Form1.loginId.value="";
		//document.Form1.loginId.focus();
		document.getElementById("loginUserid").style.display = "inline";
	}
	if(status==2){
		alert("连接数据库失败，请与管理员联系!");
	}
}

function hp_pwd(){
	var pwd = $('passwordhp');
	if(pwd.value.length==0){
		pwd.style.borderColor='red';
		return;
	}else{
		pwd.style.borderColor='#A3BFA8';
	}
}

function user_pwd(){
	var pwd = $('password');

	if(!check_userid(pwd.value)){
		pwd.style.borderColor='red';
      return ;
	}
	if(pwd.value.length==0||pwd.value.length<6){
		pwd.style.borderColor='red';
		return;
	}else{
		pwd.style.borderColor='#A3BFA8';
	}
}

function user_pwd_ag(){
	var pwd = $('password_ag');
	if(pwd.value.length==0||pwd.value.length<6){
		pwd.style.borderColor='red';
		return;
	}else{
		pwd.style.borderColor='#A3BFA8';
	}
}

function user_email(){
	var email = $('email');
	if(email.value==''){
		email.style.borderColor='red';
	}else{
		if(!isemail(email.value)){
			email.style.borderColor='red';
			return;
		}else{
			email.style.borderColor='#A3BFA8';
		}
	}
}
function update_ok()
{

	getProvince();
	getCity();
 
	var pwdOld = $('passwordOld');
	if(pwdOld.value==""){
		alert("旧密码不能为空!")
		return;
	}


	var email = $('email');
	var pwd= $('password');
  	var pwd_ag=$('password_ag');
	var province=$('province');
	var nick = $('nickname');



  if(!check_userid(pwd.value)){
	window.alert("密码只能是英文字母、数字、下划线和点组成！");
	pwd.value='';
    pwd.focus();
    return ;
  }

	if(pwd.value!=''){
	  if(pwd.value!=pwd_ag.value)
		{	
			window.alert("密码不一至请重新输入密码！");
			pwd.value=''
			pwd_ag.value=''
			pwd.focus();
    		return ;
		}
	 if (document.Form1.password.value.length <6)
		{
			 window.alert("密码长度必须大于5位!");
			 document.Form1.password.value='';
			 document.Form1.password.focus();
			 return ;
		}
    }

	if(email.value!=''){
		if(!isemail(email.value)){
			email.focus();
			return;
		}
	}

	if(email.value==''&&pwd.value==''&&province.value==''&&nick.value==''){
		window.alert("请输入你要修改的资料");		
		return;
	}
	if(pwd.value!=''){		
		var url = './userSerAction.do?';
		var pars ='method=drmUpdateuser&userid='+document.Form1.loginId.value+'&passwordOld='+document.Form1.passwordOld.value+'&newPassword='+document.Form1.password.value+'&cardid='+document.Form1.hpuser.value;
		new Ajax.Request(url,{method:'post',parameters:pars,onComplete :updateDRM});
	}else{
		document.Form1.submit();
	}
}

//ajax call_back function
function updateDRM(transport){	
	var status=0;
	//	alert(transport.responseText)
	status=parseInt(transport.responseText);

	if(status==3){
		alert("密码错误");
		document.Form1.passwordOld.focus();
	}
	if(status==0){
		document.Form1.submit();

	}
	if(status==1){
		alert("连接数据库失败，请与管理员联系!");
		
	}
	if(status==2){
		alert("连接数据库失败，请与管理员联系!");
	}
}
	

