﻿var ua = "";
function getUa(obj) {
    ua = obj;
}
function changecode(obj) {
    var s = 'Page/Code.aspx?a=' + Math.random();
    $("#img_code").attr("src", s);
}
$(function() {
    $("#userName").blur(function() { check_Name(); });
    $("#passWord").blur(function() { check_passWord(); });
    $("#ConpassWord").blur(function() { check_ConpassWord(); });
    $("#Email").blur(function() { check_Email(); });
    $("#myCode").blur(function() { check_Code(); });
    
    $("#userNameMsg").html("");
    $("#passWordMsg").html("");
    $("#ConpassWordMsg").html("");
    $("#EmailMsg").html("");
    $("#CodeMsg").html("");
})

function check_Name() {
        var userName = $.trim($("#userName").val()) == "" ? null : $("#userName").val();
        $("#userNameMsg").html("");
        if (userName == null) {
            $("#userNameMsg").html("请填写帐号名。");
            return false;
        }
        else if (userName.length < 3 || userName.length > 20) 
        {
            $("#userNameMsg").html("请正确输入帐号名。");
            return false;
        }
        else        
        {
            var data = FyWeb3_0_0.reg.checkUserName(userName).value;
            if (data == "username_no_exists") {
                $("#userNameMsg").html();
            } else if (data == "username_exists") {
                $("#userNameMsg").html("此帐号名已存在。");
                return false;
            }
        }
        return true;
}
function check_passWord() {
    var passWord = $.trim($("#passWord").val()) == "" ? null : $("#passWord").val();
    $("#passWordMsg").html("");
    if (passWord == null) {
        $("#passWordMsg").html("请设置登录密码。");
        return false;
    }
    else if(passWord.length < 6 || passWord.length > 50) {
        $("#passWordMsg").html("请正确设置登录密码。");
        return false;
    }
    return true;
}
function check_ConpassWord() {
    var passWord = $.trim($("#passWord").val()) == "" ? null : $("#passWord").val();
    var ConpassWord = $.trim($("#ConpassWord").val()) == "" ? null : $("#ConpassWord").val();
    $("#ConpassWordMsg").html("");
    if (ConpassWord == null) {
        $("#ConpassWordMsg").html("确认密码长度为6-16位。");
        return false;
    }
    else if(passWord != ConpassWord) {
        $("#ConpassWordMsg").html("和登录密码不吻合。");
        return false;
    }
    return true;
}
function check_Email() {
        var Email = $.trim($("#Email").val()) == "" ? null : $("#Email").val();
        $("#EmailMsg").html("");
        if (Email == null) {
            $("#EmailMsg").html("请输入有效的电子邮箱。");
            return false;
        }
        else if (!isEMail(Email)) 
        {
            $("#EmailMsg").html("请输入有效的电子邮箱。");
            return false;
        }
        else        
        {
            var data = FyWeb3_0_0.reg.checkEmail(Email).value;
            if (data == "email_no_exists") {
                $("#EmailMsg").html("");
            } else if (data == "email_exists") {
                $("#EmailMsg").html("此邮箱已被使用。");
                return false;
            }
        }
        return true;
}

function check_Code() {
    var myCode = $.trim($("#myCode").val()) == "" ? null : $("#myCode").val();
    $("#CodeMsg").html("");
    if (myCode == null) {
        $("#CodeMsg").html("请输入验证码。");
        return false;
    }
    return true;
}

function BtnRegClick() {
    var flag = true;
    if (!check_Name()) {
        flag = false;
    }
    if (!check_passWord()) {
        flag = false;
    }
    if (!check_ConpassWord()) {
        flag = false;
    }
    if (!check_Email()) {
        flag = false;
    }
    if (!check_Code()) {
        flag = false;
    }
    if (!$("#checkboxID").attr("checked")) {
        flag = false;
        $("#checkboxIDMsg").html("请先勾选！");
    }
    if (flag) {
        var userName = $.trim($("#userName").val()) == "" ? null : $("#userName").val();
        var passWord = $.trim($("#passWord").val()) == "" ? null : $("#passWord").val();
        var Email = $.trim($("#Email").val()) == "" ? null : $("#Email").val();
        var myCode = $.trim($("#myCode").val()) == "" ? null : $("#myCode").val();
        var data = FyWeb3_0_0.reg.userReg(userName, passWord, Email, myCode, ua).value;
        if (data != null) {
            if (data.IsSuccess) {
                if (data.ExecMessage == "0") {
                    setTimeout("jumpPage();", 100);
                } else {
                    alert("注册失败！");
                    return false;
                }
            }
            else {
                alert(data.ExecMessage)
            }
        }
        else { alert("注册失败！"); }
    } else {
        return false;   
    }
}

function jumpPage() {
    alert("注册成功！");
    window.location = "http://fy.ledu.com";
}
//验证是否符合E-MAIL格式
function isEMail(EMail) 
{
   return checkMatch(EMail, /^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/gi);
}
function checkMatch(str, reg)
{
    var match = str.match(new RegExp(reg));
    return match == null ? false : true;
}
