﻿$().ready(function(){
    $("#changeImg").click(function(){
        var url = "/getvalidateimg.aspx?rnd="+Math.random();
        $("#txtCode").css("background","url(/getvalidateimg.aspx?rnd="+Math.random()+") no-repeat right top").val("");
        return false;
    });
    $("#changeImg").click();
    
    $("#cbSure").click(function(){
        var btn=$("#btnReg");
        if($(this).attr("checked"))
            btn.attr("disabled","");
        else
            btn.attr("disabled","disabled");
    });
});

function checkForm(){
    var btn=$("#btnReg");
    btn.attr("disabled","disabled");
    var ctl,msg;
    ctl=$("#txtName");
    msg=ctl.next();
    if(ctl.val()==""){
        msg.html("请填写用户名！");
        btn.attr("disabled","");
        return false;
    }
    if(ctl.val().trim().length<3 || ctl.val().trim().length>20)
    {
        msg.html("用户名应在3～20个字符之间(一个中文等于2个字符)");
        btn.attr("disabled","");
        return  false;
    }
    
    $.post("register.aspx",{f:'cu',un:ctl.val().trim()},function(e){
        if(e.error){
            msg.html(e.msg);
            btn.attr("disabled","");
        }else{
            msg.html("");
            ctl=$("#txtPassword");
            msg=ctl.next();
            if(ctl.val()=="" || (ctl.val().length<6 || ctl.val().length>16)){
                msg.html("请填写您的登录密码,并且密码长度应在6～16个字符之间");
                btn.attr("disabled","");
                return false;
            }else{
                msg.html("");
                ctl=$("#txtConfirm");
                msg=ctl.next();
                if($("#txtPassword").val()!=ctl.val())
                {
                    msg.html("两次输入的密码不一至，请重新输入");
                    btn.attr("disabled","");
                    return false;
                }else{
                    msg.html("");
                    ctl=$("#txtEmail");
                    msg=ctl.next();
                    if(ctl.val()=="")
                    {
                        msg.html("请输入您常用的电子邮箱地址");
                        btn.attr("disabled","");
                        return false;
                    }
                    d=ctl.val().match(/^[^\.]\S+@\S+\.\S+[^\.]$/); 
                    if(!d){
                        msg.html("请输入合法的Email地址");
                        btn.attr("disabled","");
                        return false;
                    }
                    msg.html("");
                    ctl=$("#txtCode");
                    msg=ctl.next();
                    if(ctl.val()=="")
                    {
                        msg.html("请输入验证码！");
                        btn.attr("disabled","");
                        return false;
                    }
                    msg.html("");
                    $("#form").submit();
                }
            }
        }
    },"json");
        
    return false;
}

function checkCode(){
    var ctl,msg;
    ctl=$("#txtCode");
    msg=ctl.next();
    if(ctl.val()=="")
    {
        msg.html("请输入验证码！");
        return false;
    }
    msg.html("");
    return true;
}

function checkEmail(obj){
    var ctl,msg;
    ctl=$("#txtEmail");
    msg=ctl.next();
    if(ctl.val()=="")
    {
        msg.html("请输入您常用的电子邮箱地址");
        return false;
    }
    d=ctl.val().match(/^[^\.]\S+@\S+\.\S+[^\.]$/); 
    if(!d){
        msg.html("请输入合法的Email地址");
        return false;
    }
    
    $.ajax({
        type:"post",
        url:"/checkmsg.aspx",
        data:"f=ce&n="+ctl.val(),
        beforeSend:function(){
            msg.html("正在检查您的邮箱地址...");
        },
        success:function(m){
            if(m==""){
                msg.html("");
            }
            else
            {
                msg.html(m);
                return false;
            }
        }
    });
    msg.html("");
    return true;
}
function checkComfirm()
{
    var ctl,msg;
    ctl=$("#txtConfirm");
    msg=ctl.next();
    if($("#txtPassword").val()!=ctl.val())
    {
        msg.html("两次输入的密码不一至，请重新输入");
        return false;
    }
    msg.html("");
    return true;
}

function checkUser(fun){
    var ctl,msg;
    ctl=$("#txtName");
    msg=ctl.next();
    if(ctl.val()==""){
        msg.html("请填写通行证用户名！");
        return false;
    }
    if(getLength(ctl.val())<3 || getLength(ctl.val())>20)
    {
        msg.html("通行证用户名应在3～20个字符之间(一个中文等于2个字符)");
        return  false;
    }
    
    $.ajax({
        type:"post",
        url:"/checkmsg.aspx",
        data:"f=cn&n="+ctl.val(),
        beforeSend:function(){
            msg.html("正在检查用户名...");
        },
        success:function(m){
            if(m==""){
                msg.html("");
            }
            else
            {
                msg.html(m);
                return false;
            }
        }
    });
    msg.html("");
    return true;
}

function checkPassword(){
    var ctl,msg;
    ctl=$("#txtPassword");
    msg=ctl.next();
    if(ctl.val()==""){
        msg.html("请填写您的登录密码");
        return false;
    }else if(ctl.val().length<6 || ctl.val().length>16){
        msg.html("密码长度应在6～16个字符之间");
        return false;
    }
    msg.html("");
    return true;
}
