﻿       
       
        //去除空格
         function ltrim(id)
        {
            var s = document.getElementById(id).value;
            document.getElementById(id).value = s.replace( /\s*$/, "");
        }
        
        //检测用户名
        function checkuser()
        {
        
          if($("#txtusername").val() == "")
            {
                $("#userinfo").html("<img src='/zengsong/images/2.gif' align='absbottom' />&nbsp;不允许为空！");
                setStyle("userinfo");
                return false;
            }
            var $txtuser =  /^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$/;
            if($txtuser.test($("#txtusername").val()))
            {
           
            if(strLength($("#txtusername").val()) <4 || strLength($("#txtusername").val())>16)
            {
                $("#userinfo").html("<img src='/zengsong/images/2.gif' align='absbottom' />&nbsp;用户名长度请控制在4-16位！");
                setStyle("userinfo");
                return false;
            }
             }
             else
             {
                $("#userinfo").html("<img src='/zengsong/images/2.gif' align='absbottom' />&nbsp;用户名只允许数字、字母、_或中文！");
                setStyle("userinfo");
                return false;

             }
            
            checkUserResult($("#txtusername").val());
        
        }
        //返回检测用户名是否存在的状态
        
        //验证用户名 
 function checkUserResult(name)
    {
   // alert(name);
        $.ajax({
            url:"/ajax.aspx?q=checkuser&username="+escape(name)+"&time="+new Date().toString(),
            type:'GET',   
            beforeSend: function() {
                 $("#userinfo").html("<font color='#ff5500'>&nbsp;检测中，请稍后...</font>");
            },    
            success:function(){
                var $info=arguments[0];//成功则返回是否可用，-1:会员名存在，1：会员名可用
                if($info==1)//可用
                {
                    $("#userinfo").html("<img src='/zengsong/images/3.gif' align='absbottom' />&nbsp;会员名可用！");
                    clearErr("userinfo");
                    
                }
                else
                {
                    $("#userinfo").html("<img src='/zengsong/images/2.gif' align='absbottom' />&nbsp;<font color=red>会员名已存在，请重新选择。</font>");
                    setStyle("userinfo");
                    $("#txtusername").focus();
                }
            }
        });
    }
        
        //check the password
        function checkpwd()
        {
            var pwd = $("#txtpwd").val();
            if(pwd.length>10 || pwd.length<6)
            {
                $("#pwdinfo").html("<img src='/zengsong/images/2.gif' align='absbottom' />&nbsp;密码在6到10位之间！");
                setStyle("pwdinfo");
            }
            else
            {
                $("#pwdinfo").html("<img src='/zengsong/images/3.gif' align='absbottom' />&nbsp;密码输入正确。");
                clearErr("pwdinfo");
            }
        }
        
        //check the repassword 
        function checkpwdagain()
        {
            var pwd = $("#txtpwd").val();
            var pwdagain = $("#txtpwdagain").val();
            if(pwd!=pwdagain || pwd=="" || pwdagain =="")
            {
                $("#pwdagaininfo").html("<img src='/zengsong/images/2.gif' align='absbottom' />&nbsp;密码不匹配！");
                setStyle("pwdagaininfo");
            }
            else
            {
                 if(pwdagain.length>10 || pwdagain.length<6)
                {
                    $("#pwdagaininfo").html("<img src='/zengsong/images/2.gif' align='absbottom' />&nbsp;密码在6到10位之间！");
                    setStyle("pwdagaininfo");
                }
                else
                {
                    $("#pwdagaininfo").html("<img src='/zengsong/images/3.gif' align='absbottom' />&nbsp;密码输入正确，请牢记！");
                    clearErr("pwdagaininfo");
                }
            }
        }
        
        //check the email
        function checkemail()
        {
             if($("#txtemail").val() == "")
            {
                $("#emailinfo").html("<img src='/zengsong/images/2.gif' align='absbottom' />&nbsp;请填写您的Email地址！");
                setStyle("emailinfo");
                return false;
            }
             var $myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
            if(!$myreg.test($("#txtemail").val()))
            {
                 $("#emailinfo").html("<img src='/zengsong/images/2.gif' align='absbottom' />&nbsp;请正确填写您的Email地址！");
                setStyle("emailinfo");
                return false;
            }
             checkEmailResult($("#txtemail").val());
        }
      
        
                //验证邮箱存在性
 function checkEmailResult(email)
    {
   // alert(name);
        $.ajax({
            url:"/ajax.aspx?q=checkemail&email="+escape(email)+"&time="+new Date().toString(),
            type:'GET',   
            beforeSend: function() {
                 $("#emailinfo").html("<font color='#ff5500'>&nbsp;检测中，请稍后...</font>");
            },    
            success:function(){
                var $info=arguments[0];//成功则返回是否可用，-1:邮箱存在，1：邮箱可用
                if($info==1)//可用
                {
                    $("#emailinfo").html("<img src='/zengsong/images/3.gif' align='absbottom' />&nbsp;邮箱可用！");
                    clearErr("emailinfo");
                }
                else
                {
                    $("#emailinfo").html("<img src='/zengsong/images/2.gif' align='absbottom' />&nbsp;<font color=red>邮箱已存在，请重新选择。</font>");
                    setStyle("emailinfo");
                    $("#txtusername").focus();
                }
            }
        });
    }
        
        
        
        //check the mobile
        function checkmobile()
        {          
             var mobile = $("#txtMobile").val();
             
             if(mobile=="")
             {
                $("#MobileInfo").html("<img src='/zengsong/images/2.gif' align='absbottom' />&nbsp;手机号码不能为空！");
                setStyle("MobileInfo");
                return false;
             }
             if(!(/^13\d{9}$|^15\d{9}$|^0\d{11}$|^0\d{10}$|^02\d{9}$|^02\d{8}$|^010\d{8}$|^18\d{9}$/gi).test(mobile))
             {

                    $("#MobileInfo").html("<img src='/zengsong/images/2.gif' align='absbottom' />&nbsp;请输入正确的手机号码！");
                    setStyle("MobileInfo");
                    return false;
              }
            else
            {
                    //checkMobileResult(mobile);
                     $("#MobileInfo").html("<img src='/zengsong/images/3.gif' align='absbottom' />&nbsp;该手机可以使用！");
                    clearErr("MobileInfo");
            }
        }
        
 //验证手机存在性
 function checkMobileResult(mobile)
    {
   // alert(name);
        $.ajax({
            url:"/ajax.aspx?q=checkmobile&mobile="+escape(mobile)+"&time="+new Date().toString(),
            type:'GET',   
            beforeSend: function() {
                 $("#MobileInfo").html("<font color='#ff5500'>&nbsp;检测中，请稍后...</font>");
            },    
            success:function(){
                var $info=arguments[0];//成功则返回是否可用，-1:邮箱存在，1：邮箱可用
                if($info==1)//可用
                {
                    $("#MobileInfo").html("<img src='/zengsong/images/3.gif' align='absbottom' />&nbsp;该手机可以使用！");
                    clearErr("MobileInfo");
                }
                else
                {
                    $("#MobileInfo").html("<img src='/zengsong/images/2.gif' align='absbottom' />&nbsp;<font color=red>此手机已被占用，请勿重复申领。</font>");
                    setStyle("MobileInfo");
                    $("#txtMobile").focus();
                }
            }
        });
    }
        
       //验证姓名
        function chkRealname()
        {
            var real = $("#txtrealname").val();
            if(real == "")
            {
                $("#RealInfo").html("<img src='/zengsong/images/2.gif' align='absbottom' />&nbsp;不允许为空！");
                setStyle("RealInfo");
            }
            else
            {
                $("#RealInfo").html("<img src='/zengsong/images/3.gif' align='absbottom' />&nbsp;输入正确！");
                clearErr("RealInfo");
            }
            
        }
        
        //验证邮编
        function chkZip()
        {
//            var $zip = $("#txtzip").val();
//            if($zip == "")
//            {
//                $("#ZipInfo").html("<img src='/zengsong/images/2.gif' align='absbottom' />&nbsp;不允许为空！");
//                setStyle("ZipInfo");
//            }
//            else 
//            if($zip!="")
//            {
//                if($zip.length != 6)
//                {
//                    $("#ZipInfo").html("<img src='/zengsong/images/2.gif' align='absbottom' />&nbsp;输入不正确！");
//                    setStyle("ZipInfo");
//                }
//                else
//                {
//                    $("#ZipInfo").html("<img src='/zengsong/images/3.gif' align='absbottom' />&nbsp;输入正确！");
//                    clearErr("ZipInfo");
//                }
//            }
        }
       
    //验证省份及详细地址
    function chkprovince()
    {
        if($("select[@name=province] option[@selected]").text()=="--请选择省份--")
        {
            $("#sp_pcinfo").html("<font color=red>请选择省份！</font>");
            return false;
        }
        else
        {
              $("#sp_pcinfo").html("");
        }

    }
    //验证地址
    function chkAdd()
    {
    
        if($("select[@name=province] option[@selected]").text()=="--请选择省份--")
        {
            $("#sp_pcinfo").html("<font color=red>请选择省份！</font>");
            return false;
        }
        else
        {
              $("#sp_pcinfo").html("");
        }
   
        if($("#txtAddress").val()=="")
        {
            $("#AddressInfo").html("<img src='/zengsong/images/2.gif' align='absbottom' />&nbsp;不允许为空！");
             setStyle("AddressInfo");
            return false;
        }
        else
        {
           $("#AddressInfo").html("<img src='/zengsong/images/3.gif' align='absbottom' />&nbsp;输入正确！");
           clearErr("AddressInfo");   
        }
    }
        
//表单验证
     function checkbutton()
        {
            var infoofuser = $("#userinfo").html();
            var infoofpwd = $("#pwdinfo").html();
            var infoofpwdagain = $("#pwdagaininfo").html();
            var infoofemail = $("#emailinfo").html();
            var txtadd = $("#AddressInfo").html();
            var txtmol = $("#MobileInfo").html();
            var txtreal = $("#txtrealname").val();
            var txtzip = $("#ZipInfo").html();
          //  alert(infoofuser);
            //真实姓名
            if(infoofuser.indexOf("会员名可用") == -1)
            {
                alert("您的用户名输入错误，请根据提示正确填写用户名！");
                $("#txtusername").focus();
                $("#usernull").show();
                return false;
            }
            //密码
            else if(infoofpwd.indexOf("密码输入正确") == -1)
            {
                alert("您的密码输入错误，请根据提示正确填写密码！");
                $("#txtpwd").focus();
                return false;
            }
            //确认密码
            else if(infoofpwdagain.indexOf("密码输入正确") == -1)
            {
                alert("您的二次密码输入错误，请根据提示正确填写！");
                $("#txtpwdagain").focus();
                return false;
            }
            //邮箱
            else if(infoofemail.indexOf("邮箱可用") == -1)
            {
                alert("您的邮箱输入错误，请根据提示正确填写！");
                $("#txtemail").focus();
                return false;
            }
            
           else if(txtreal == "")
            {
                alert("真实姓名不能为空！");
                $("#txtrealname").focus();
                return false;
            }
            //获取省市
             else if($("select[@name=province] option[@selected]").text()=="--请选择省份--")
            {
                alert("请选择省份！");
                $("#ddl_province").focus();
                return false;
            }
             //地址
            else if(txtadd.indexOf("输入正确") == -1)
            {
                alert("请详细填写收货地址！");
                $("#txtAddress").focus();
                return false;
            }
            //手机
            else if(txtmol.indexOf("该手机可以使用") == -1)
            {
                alert("您的手机输入错误，请根据提示正确填写！");
                $("#txtMobile").focus();
                return false;
            }
//            //邮编
//            else if(txtzip.indexOf("输入正确") == -1||txtzip.indexOf("所在地邮编")>-1)
//            {
//                alert("请正确输入邮编！");
//                $("#txtzip").focus();
//                return false;
//            }
           else
            {
            //alert("ok");
            $("#ImageButton2").hide();
            $("#loginstatus").show();
                return true;
            }
        }
        
   
    function setStyle(divinfo)
    {
         $("#"+divinfo).css ("color","red");
    }
    
    function clearErr(divinfo)
    {
        $("#"+divinfo).css ("color","green");
    }
    //区分中英文
    function strLength(str)
    {            
        return str.replace(/[^\x00-\xFF]/g,'**').length;
    }