﻿
    function GetRblValue()
    {
        var rvl = "";
        var rbl = $("#user input[name='rUserType']");
        for (k = 0; k < rbl.length; k++)
        {
            if (rbl.eq(k).attr("checked") == true)
            {
                rvl = rbl.eq(k).val();
                break;
            }
        }
        
        return rvl;
    }
    
    function SwUser()
    {
        $("#SItem").hide();
        $("#SItem").next().show();
        $("#dealer").prev().hide();
        $("#dealer").hide();
        $("#"+jshiRegType).val("User");
    }
    
    function SwDealer()
    {
        $("#SItem").hide();
        $("#SItem").next().show();
        $("#rUserType1").parent().parent().hide();
        $("#dealer").show();
        $("#dealer").prev().show();
        $("#dealer").prev().find("a").show(); 
        $("#"+jshiRegType).val("Dealer");
    }
    
    function SwEnterprise()
    {
        $("#SItem").hide();
        $("#SItem").next().show();
        $("#dealer").show();
        $("#dealer").prev().show();
        $("#dealer").prev().find("a").hide();
        $("#"+jsrblBussinessType).parent().parent().hide();
        $("#"+jshiRegType).val("Enterprise");
    }
    
    $("#regUser").click(function(){
        SwUser();
    });
    $("#regDealer").click(function(){
        SwDealer();
    });
    $("#rUserType1").click(function(){
        if ($(this).attr("checked") == true)
        {
            SwUser();
        }
    });
    $("#rUserType2").click(function(){
        if ($(this).attr("checked") == true)
        {
            SwEnterprise();
        }
    });
    
    
    //验证用户名
    $("#"+jstxtUserName)
    .focus(function(){ 
        $(this).next().attr("class","Tips").html("只能输入6-15位的数字或字母"); 
    })
    .blur(function(){
        $(this).val($(this).val().replace(/\s/g,'').replace("'","")); //Trim()
        if ($(this).val() == "")
        {
            $(this).next().attr("class","Warn").html("您还没有输入用户名");
        }
        else if ($(this).val().length < 6)
        {
            $(this).next().attr("class","Warn").html("用户名不能少于6个字符");
        }
        else if ($(this).val().length > 15)
        {
            $(this).next().attr("class","Warn").html("用户名不能超过15个字符");
        }
        else
        {
            var reg = new RegExp("^[A-Za-z0-9]+$");
            if (!reg.test($(this).val()))
            {
                $(this).next().attr("class","Warn").html("用户名格式不正确，只能输入字母和数字");
            }
            else
            {
                $(this).next().attr("class","").html(jsWorking+"正在检查您输入的用户名是否可用");
                
                $.ajax({
                   type: "POST",
                   url: "/Ajax/RegisterCheck.ashx",
                   data: "CType=UserName&UserName="+$(this).val(),
                   success: function(msg){
                     
                     if (msg == "1")
                     {
                        $("#"+jstxtUserName).next().attr("class","Warn").html($("#"+jstxtUserName).val()+"此用户名已被占用，请输入别的用户名");
                     }
                     else
                     {
                        $("#"+jstxtUserName).next().attr("class","True").html($("#"+jstxtUserName).val()+"此用户名可用");
                     }
                   }
                }); 
            }
        }
    });
    
    function ValiUserName()
    {
        var rvl = true;
        var UserName = $("#"+jstxtUserName);
        if (UserName.val() == "")
        {
            UserName.next().attr("class","Warn").html("您还没有输入用户名");
            alert("您还没有输入用户名");
            rvl = false;
        }
        else if (UserName.val().length < 6)
        {
            UserName.next().attr("class","Warn").html("用户名不能少于6个字符");
            alert("用户名不能少于6个字符");
            rvl = false;
        }
        else if (UserName.val().length > 15)
        {
            UserName.next().attr("class","Warn").html("用户名不能超过15个字符");
            alert("用户名不能超过15个字符");
            rvl = false;
        }
        else
        {
            var reg = new RegExp("^[A-Za-z0-9]+$");
            if (!reg.test(UserName.val()))
            {
                UserName.next().attr("class","Warn").html("用户名格式不正确，只能输入字母和数字");
                alert("用户名格式不正确，只能输入字母和数字");
                rvl = false;
            }
        }
        
        return rvl;
    }
    
    //验证密码
    $("#"+jstxtPWD)
    .focus(function(){ 
        $(this).next().attr("class","Tips").html("只能输入6-20位的数字或字母"); 
    })
    .blur(function(){
        $(this).val($(this).val().replace(/(^ )|( $)/g,""));
        if ($(this).val() == "")
        {
            $(this).next().attr("class","Warn").html("您还没有输入密码");
        }
        else if ($(this).val().length < 6)
        {
            $(this).next().attr("class","Warn").html("为了安全起见，密码不能少于6位");
        }
        else if ($("#"+jstxtPassword).val() != "" && $("#"+jstxtPassword).val() != $(this).val())
        {
            $(this).next().attr("class","Warn").html("两次输入的密码不一样，请重新输入");
            $("#"+jstxtPassword).next().attr("class","Warn").html("两次输入的密码不一样，请重新输入");
        }
        else
        {
            $(this).next().attr("class","True").html("已输入");
            
        }
    });
    
    $("#"+jstxtPassword)
    .focus(function(){ 
        $(this).next().attr("class","Tips").html("只能输入6-20位的数字或字母"); 
    })
    .blur(function(){
        $(this).val($(this).val().replace(/(^ )|( $)/g,""));
        if ($(this).val() == "")
        {
            $(this).next().attr("class","Warn").html("您还没有输入密码");
        }
        else if ($("#"+jstxtPassword).val() == $("#"+jstxtPWD).val())
        {
            $(this).next().attr("class","True").html("已输入");
            $("#"+jstxtPWD).focus().blur();
        }
        else
        {
            $(this).next().attr("class","Warn").html("两次输入不一样");
        }
    });
    
    function ValiPWD()
    {
        var rvl = true;
        var PWD = $("#"+jstxtPWD);
        var Password = $("#"+jstxtPassword);
        if (PWD.val() == "")
        {
            PWD.next().attr("class","Warn").html("您还没有输入密码");
            alert("您还没有输入密码");
            rvl = false;
        }
        else if (Password.val() == "")
        {
            Password.next().attr("class","Warn").html("您还没有输入密码");
            alert("您还没有输入密码");
            rvl = false;
        }
        return rvl;
    }
    
    
    //验证Email格式
    $("#"+jstxtRegEmail).focus(function(){
        $(this).next().attr("class","Tips").html("输入有效的E-mail"); 
    })
    .blur(function(){
        $(this).val($(this).val().replace(/(^ )|( $)/g,"").replace("'",""));
        if ($(this).val() != "")
        {
            var reg = new RegExp("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$");
            if (!reg.test($(this).val()))
            {
                $(this).next().attr("class","Warn").html("邮箱格式错误，请输入有效的邮箱,参考格式如：service@yindon.com");  
            }
            else
            {
                $(this).next().attr("class","").html(jsWorking+"正在检查Email是否可用");
                
                $.ajax({
                   type: "POST",
                   url: "/Ajax/RegisterCheck.ashx",
                   data: "CType=Email&Email="+$(this).val(),
                   success: function(msg){
                     
                     if (msg == "1")
                     {
                        $("#"+jstxtRegEmail).next().attr("class","Warn").html("此Email已被注册过，不能重复注册");
                     }
                     else
                     {
                        $("#"+jstxtRegEmail).next().attr("class","True").html("此Email可用");
                     }
                   }
                });
            }
        }
        else
        {
           $(this).next().attr("class","Warn").html("您还没有输入Email邮箱");  
        }
    });
    
    function ValiEmail()
    {
        var rvl = true;
        var Email = $("#"+jstxtRegEmail);
        var reg = new RegExp("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$");
        if (Email == "")
        {
            Email.next().attr("class","Warn").html("注册箱用来找回密码和验证用，您还没有填哦");
            alert("您还没有填写Email地址");
            rvl = false; 
        }
        else if (Email.next().attr("class") == "Warn")
        {
            alert(Email.next().html());
            rvl = false; 
        }
        else if (!reg.test(Email.val()))
        {
            Email.next().attr("class","Warn").html("Email地址格式不对,参考如：service@yindon.com ");
            alert("您填写的Email地址格式不对,请重新填写");
            rvl = false;
        }
        
        return rvl;
    }
    
    
    //验证地区
    $(function(){
        $.ajax({
            url:"/Ajax/GetArea.ashx?ParentID=0",
            success:function(html){
                if (html != "")
                {
                    $("#sProvince").html(html);
                }
            }
        });
        
        $("#sCity").hide();
        $("#sArea").hide();
        $("#"+jshiAreaID).val("");
        $("#"+jstxtValiCode).val("");
        $("#"+jsbtnSubmit).attr("disabled","disabled");
        $("#cb").attr("checked","");
        
        if ($("#"+jshiRegType).val() == "User")
        {
            SwUser();
        }
        else if ($("#"+jshiRegType).val() == "Dealer")
        {
            SwDealer();
        }
        else if ($("#"+jshiRegType).val() == "Enterprise")
        {
            SwEnterprise();
        }
    });
    
    $("#sProvince").change(function(){
        var pid = $(this).val();
        if (pid != "")
        {
            $.ajax({
                url:"/Ajax/GetArea.ashx?ParentID="+pid,
                success:function(html){
                    if (html != "")
                    {
                        $("#sCity").html(html).show();
                        $("#sArea").html("").hide();
                        
                        showarea();
                        $("#"+jshiAreaID).val("");
                        $("#labelArea").next().attr("class","Tips").html("地区要选到第三级");
                    }
                }
            });
        }
    });
    
    $("#sCity").change(function(){
        var cid = $(this).val();
        if (cid != "")
        {
            $.ajax({
                url:"/Ajax/GetArea.ashx?ParentID="+cid,
                success:function(html){
                    if (html != "")
                    {
                        $("#sArea").html(html).show();
                        
                        $("#"+jshiAreaID).val("");
                        showarea();
                        $("#labelArea").next().attr("class","Tips").html("地区要选到第三级");
                    }
                }
            });
        }
    });
    
    $("#sArea").change(function(){
        var aid = $(this).val();
        if (aid != "")
        {
            showarea();
            $("#"+jshiAreaID).val(aid);
            $("#labelArea").next().attr("class","True").html("地区已选");
        }
    });
    
    function showarea()
    {
        var spa = "";
        if ($("#sArea").val() != null && $("#sArea").val() != "")
        {
            spa = $("#sProvince>option:selected").get(0).text+">>"+$("#sCity>option:selected").get(0).text+">>"+$("#sArea>option:selected").get(0).text;
        }
        else if ($("#sCity").val() != null && $("#sCity").val() != "")
        {
            spa = $("#sProvince>option:selected").get(0).text+">>"+$("#sCity>option:selected").get(0).text;
        }
        else if ($("#sProvince").val() != null && $("#sProvince").val() != "")
        {
            spa = $("#sProvince>option:selected").get(0).text;
        }
        else
        {
            spa = "";
        }
        
        $("#labelArea").html(spa);
    }
    
    function ValiArea()
    {
        var rvl = true;
        var reg = new RegExp("^[0-9]*$");
        
        if ($("#"+jshiAreaID).val() == "")
        {
            $("#labelArea").next().attr("class","Warn").html("您还没有选择地区，要求选择到第三级");
            alert("您还没有选择地区，要求选择到第三级");
            rvl = false;
        }
        else if (!reg.test($("#"+jshiAreaID).val()))
        {
            $("#labelArea").next().attr("class","Warn").html("地区格式不对，请重新选择地区");
            alert("地区格式不对");
            rvl = false;
        }
        
        return rvl;
    }
    
    //验证公司名 公司营业执照
    $("#"+jstxtCompanyName)
    .focus(function(){
        $(this).next().attr("class","Tips").html("请输入公司名的全称，如：“深圳市******销售有限公司”");
    })
    .blur(function(){
        $(this).val($(this).val().replace(/\s/g,'').replace("'",""));
        if ($(this).val() == "")
        {
            $(this).next().attr("class","Warn").html("公司名为必填项，您还没有输入");
        }
        else
        {
            $(this).next().attr("class","").html(jsWorking+"正在检查公司名");
                
            $.ajax({
                type: "POST",
                url: "/Ajax/RegisterCheck.ashx",
                data: "CType=CompanyName&CompanyName="+$(this).val(),
                success: function(msg){
                    if (msg == "1")
                    {
                        $("#"+jstxtCompanyName).next().attr("class","Warn").html("此公司名已存在！");
                        showTips();
                        $("#tcontent").html("此公司名以前已注册过！<br />请用以前注册过的账号登录，如果忘记密码，请联系我们客服人员进行“认领”操作！");
                    }
                    else
                    {
                        $("#"+jstxtCompanyName).next().attr("class","True").html("已输入");
                    }
                }
            });
        }
    });
    
    $("#"+jstxtLicence)
    .focus(function(){
        $(this).next().attr("class","Tips").html("请输入公司营业执照号");
    })
    .blur(function(){
        $(this).val($(this).val().replace(/(^ )|( $)/g,'').replace("'",""));
        $(this).next().attr("class","").html("此项不会在前台显示，只做为企业验证凭据");
    });
    
    function ValiCompanyName()
    {
        var rvl = true;
        if ($("#"+jstxtCompanyName).val() == "")
        {
            $("#"+jstxtCompanyName).next().attr("class","Warn").html("公司名为必填项，您还没有输入");
            alert("公司名为必填项，您还没有输入");
            rvl = false;
        }
//        else if ($("#"+jstxtLicence).val() == "")
//        {
//            $("#"+jstxtLicence).next().attr("class","Warn").html("营业执照为必填项，您还没有输入");
//            alert("营业执照为必填项，您还没有输入");
//            rvl = false;
//        }
        
        return rvl;
    }
    
    /// ValiCode验证码
    $("#"+jstxtValiCode).focus(function(){
        $("#imgVC").show();
        $("#imgVC").next().attr("class","Tips").html("点击图片可以刷新验证码");
    })
    .blur(function(){
        $(this).val($(this).val().replace(/(^ )|( $)/g,'').replace("'",""));
        if ($(this).val() == "")
        {
            $("#imgVC").next().attr("class","Warn").html("您还没有输入验证码,点击图片可以刷新验证码");
        }
        else
        {
            $("#imgVC").next().attr("class","").html("点击图片可以刷新验证码");
        }
    });
    
    $("#imgVC").click(function(){
        $(this).attr("src","/ValiCode.aspx?id="+Math.random());
    });
    
    function ValiValiCode()
    {
        var rvl = true;
        var vc = $("#"+jstxtValiCode);
        if (vc.val() == "")
        {
            $("#imgVC").next().attr("class","Warn").html("您还没有输入验证码,点击图片可以刷新验证码");
            alert("您还没有输入验证码,点击图片可以刷新验证码");
            rvl = false;
        }
        return rvl;
    }
    
    ///注册协议
    $("#cb").click(function(){
        if ($(this).attr("checked") == true)
        {
            $("#voic").show();
            $("#"+jsbtnSubmit).attr("disabled","");
        }
        else
        {
            $("#voic").hide();
            $("#"+jsbtnSubmit).attr("disabled","disabled");
        }
    });
    
    
    $("#"+jsbtnSubmit).click(function(){
        $(this).attr("disabled","disabled");
        return ValiForm();
    });
    
    
    function ValiForm()
    {
        var ret = true;
        
        if (!ValiUserName())
        {
            $("#"+jsbtnSubmit).attr("disabled","");
            return false;
        }
        else if (!ValiPWD())
        {
            $("#"+jsbtnSubmit).attr("disabled","");
            return false;
        }
//        else if (!ValiEmail())
//        {
//            $("#"+jsbtnSubmit).attr("disabled","");
//            return false;
//        }
        else if (!ValiArea())
        {
            $("#"+jsbtnSubmit).attr("disabled","");
            return false;
        }
        else if (!ValiValiCode())
        {
            $("#"+jsbtnSubmit).attr("disabled","");
            return false;
        }
        else if ($("#"+jshiRegType).val() == "Dealer" || $("#"+jshiRegType).val() == "Enterprise")
        {
            if (!ValiCompanyName())
            {
                ret = false;
            }
            $("#"+jsbtnSubmit).attr("disabled","");
            return ret;
        }
        else
        {
            $("#"+jsbtnSubmit).attr("disabled","");
            $("#"+jsbtnSubmit).attr("style","visibility:hidden;");
            return true;
        }
    }


