// JavaScript Document
/*
Content: Common components;
Remark: reg;
Version: v0.1;
Author: guanxin.com JF.Z;
Date: 2010-07-2
*/
var email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+/;
var a = b = c = d = e = f = 1;
var checkName = /^[A-Za-z0-9_\u4e00-\u9fa5]+$/;
$.fn.yztext = function() {
    //关闭 错误提示
    $(".erroes dd").click(function() {
        $(".erroes").hide(200);
    })
    $(":text").each(function(i) {
    })
    //获取焦点
	.focusin(function() {
	    //初始化
	    //$(".error").css({"background-position":"0px 25px"})
	    //$(".error dd").hide();
	    $(this).css({ "border": "1px solid #006EE9" })
	    $(this).css({ "color": "#000" })
	    //提示语言
	    if (this.id == "txtUserEmail") {
	        $(this).error(this, ".userEmail", ".userEmail .tishi", "#006EE9", "0 100px", "#000")
	        //$(".userEmail .tishi").show();
	    }
	    if (this.id == "txtUserName") {
	        $(this).error(this, ".userName", ".userName .tishi", "#006EE9", "0 100px", "#000")
	        //$(".userName .tishi").show();
	    }
	    if (this.id == "txtCheckCode") {
	        $(this).error(this, ".userYz", ".userYz .tishi", "#006EE9", "0 100px", "#000")
	        //$(".userYz .tishi").show();
	    }
	})
    //失去焦点
	.focusout(function() {
	    $(this).css({ "border": "1px solid #c6c6c6" })
	    //$(".error dd").hide();

	    //验证
	    if (this.id == "txtUserEmail") {
	        if ($(this).val() != null && $(this).val() !== "") {
	            if (email.test($(this).val())) {
	                $.getJSON('/Ajax/CheckEmail.ashx?userEmail=' + escape($(this).val()), function(result) {
	                    if (result.JosnModel[0].Success == "1") {
	                        a = 0;
	                        $(this).right(".userEmail");
	                    }
	                    else {
	                        $(this).error("", ".userEmail", ".userEmail .spanError2", "red", "0 -25px", "red")
	                        a = 1;
	                    }
	                });

	            } else {
	                $(this).error(this, ".userEmail", ".userEmail .spanError", "red", "0 -25px", "red")
	                a = 1
	            }
	        } else {
	            $(this).error("", ".userEmail", ".userEmail .nulls", "none", "0 100px", "#000")
	            a = 1
	        }
	    }
	    if (this.id == "txtUserName") {		
	        if ($(this).val() != null && $(this).val() !== "") {
		    var byteLength = ($(this).val()).replace(/[^\x00-\xff]/g, "aa").length;
	            if (byteLength >=4 && byteLength <16) {			
	                $.getJSON('/Ajax/CheckUserName.ashx?userName=' + escape($(this).val()), function(result) {
	                    if (result.JosnModel[0].Success == "1") {
	                        b = 0;
	                        $(this).right(".userName");
	                    }
	                    else {
	                        $(this).error("", ".userName", ".userName .spanError2", "red", "0 -25px", "red")
	                        b = 1;
	                    }
	                    if ($('#txtUserName').val().length > 16) {
	                        $(this).error("#txtUserName", ".userName", ".userName .spanError3", "red", "0 -25px", "red");
	                        b = 1;
	                    }
	                    if (!checkName.test($('#txtUserName').val())) {
	                        $(this).error("#txtUserName", ".userName", ".userName .spanError4", "red", "0 -25px", "red");
	                        b = 1;
	                    }
	                });
	            } else {			
	                $(this).error(this, ".userName", ".userName .spanError", "red", "0 -25px", "red")
	                b = 1;
	            }
	        } else {
	            $(this).error("", ".userName", ".userName .nulls", "none", "0 100px", "#000")
	            b = 1;
	        }

	    }
	    if (this.id == "txtCheckCode") {
	        if ($(this).val() != null && $(this).val() !== "") {
	            if ($(this).val().length == 4) {
	                $.getJSON('/Ajax/CheckCode.ashx?checkcode=' + escape($(this).val()), function(result) {
	                    if (result.JosnModel[0].Success == "0") {
	                        c = 1;
	                        $(this).error("", ".userYz", ".userYz .spanError", "red", "0 -25px", "red");
	                    } else {
	                        c = 0;
	                        $(this).right(".userYz");
	                        $(".erroes").hide();
	                    }
	                });

	            } else {
	                $(this).error(this, ".userYz", ".userYz .spanError", "red", "0 -25px", "red")
	                c = 1
	            }
	        } else {
	            $(this).error("", ".userYz", ".userYz .nulls", "none", "0 100px", "#000")
	            c = 1
	        }

	    }
	    $(this).inputs()

	});
}
$.fn.yzpassword = function() {
    $(":password").each(function(i) {
    })
	.focusin(function() {
	    $(this).css({ "border": "1px solid #006EE9" })
	    $(this).css({ "color": "#000" })
	    //$(".error").css({"background-position":"0px 25px"})
	    //$(".error dd").hide();

	    if (this.id == "txtUserPass") {
	        $(this).error(this, ".userPwd1", ".userPwd1 .tishi", "#006EE9", "0 100px", "#000")
	        //$(".userPwd1 .tishi").show();
	    }
	    if (this.id == "txtConfirmPwd") {
	        $(this).error(this, ".userPwd2", ".userPwd2 .tishi", "#006EE9", "0 100px", "#000")
	        //$(".userPwd2 .tishi").show();
	    }
	})
	.focusout(function() {
	    $(this).inputs()
	    $(this).css({ "border": "1px solid #c6c6c6" })
	    //$(".error dd").hide();
	    if (this.id == "txtUserPass") {
	        if ($(this).val() != "") {
	            if ($(this).val().length < 6 || $(this).val().length > 16) {
	                $(this).error(this, ".userPwd1", ".userPwd1 .spanError", "red", "0 -25px", "red")
	                d = 1
	            } else if ($(this).val() != $("#txtConfirmPwd").val() && $("#txtConfirmPwd").val() != "") {
	                $(this).error("#txtConfirmPwd", ".userPwd2", ".userPwd2 .spanError", "red", "0 -25px", "red")
	                $(this).right(".userPwd1", "#txtUserPass")
	                d = 0
	            } else if ($(this).val() == $("#txtConfirmPwd").val() && $("#txtConfirmPwd").val() != "") {
	                $(this).right(".userPwd2", "#txtConfirmPwd")
	                $(this).right(".userPwd1", "#txtUserPass")
	                d = 0
	            } else {
	                $(this).right(".userPwd1")
	                d = 0
	            }
	        } else {
	            $(this).error("", ".userPwd1", ".userPwd1 .nulls", "none", "0 100px", "#000")
	        }
	    }

	    if (this.id == "txtConfirmPwd") {
	        if ($(this).val() != "") {
	            if ($(this).val().length < 6 || $(this).val().length > 16) {
	                $(this).error(this, ".userPwd2", ".userPwd2 .spanError", "red", "0 -25px", "red")
	                e = 1
	            } else if ($(this).val() != $("#txtUserPass").val()) {
	                $(this).error(this, ".userPwd2", ".userPwd2 .spanError", "red", "0 -25px", "red")
	                e = 1
	            } else {
	                $(this).right(".userPwd2")
	                e = 0;
	            }
	        } else {
	            $(this).error("", ".userPwd2", ".userPwd2 .nulls", "none", "0 100px", "#000")
	        }
	    }
	    $(this).inputs();
	});
}
$.fn.error = function(obj, Class, Class1, Color, Str, Colors) {
    $(Class).css({ "background-position": Str })
    $(obj).css({ "border": "1px solid " + Color })
    //$(obj).css({ "background-color": "#FEECEC"})
    $(Class + " dd").hide();
    $(Class1).show();
    $(obj).css({ "color": Colors })
}
$.fn.right = function(obj, id) {
    $(obj).css({ "background-position": "0px 0px" })
    $(id).css({ "border": "1px solid #c6c6c6" })
    $(id).css({ "color": "#000" })
    $(obj + " dd").hide();

}
function validate() {
    var email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+/;
    var userEmail = $('#txtUserEmail').val();
    var userName = $('#txtUserName').val();
    var checkName = /^[A-Za-z0-9_\u4e00-\u9fa5]+$/;
    var pwd = $('#txtUserPass').val();
    var confirmpwd = $('#txtConfirmPwd').val();
    var code = $('#txtCheckCode').val();
    var flag = true;
    if (userEmail == "") {
        $("#errorInfo").attr("innerHTML", '邮箱不能为空！');
        $(".erroes").show();
        return false;
    }
    else {
        if (!email.test(userEmail)) {
            $("#errorInfo").attr("innerHTML", '邮箱格式有误！');
            $(".erroes").show(); //邮箱格式有误
            return false;
        } else {
            $.ajax({
                type: "Get",
                url: "/Ajax/CheckEmail.ashx",
                data: "userEmail=" + escape(userEmail),
                dataType: "json",
                cache: false,
                async: false,
                beforeSend: function(XMLHttpRequest) {
                    $("#showResult").text("正在查询");
                },
                success: function(msg) {
                    if (msg.JosnModel[0].Success == "0") {
                        $("#errorInfo").attr("innerHTML", '此邮箱已被占用！');
                        $(".erroes").show();
                        flag = false;
                    }
                },
                complete: function(XMLHttpRequest, textStatus) {
                    //隐藏正在查询图片
                },
                error: function() {
                    //错误处理
                }
            });
        }
    }
    if (userName == "") {
        $("#errorInfo").attr("innerHTML", '用户名不能为空！');
        $(".erroes").show();
        return false;
    }
    else {
        $.ajax({
            type: "Get",
            url: "/Ajax/CheckUserName.ashx",
            data: "userName=" + escape(userName),
            dataType: "json",
            cache: false,
            async: false,
            beforeSend: function(XMLHttpRequest) {
                $("#showResult").text("正在查询");
            },
            success: function(msg) {
                if (msg.JosnModel[0].Success == "0") {
                    $("#errorInfo").attr("innerHTML", '此用户名已被占用！');
                    $(".erroes").show();
                    flag = false;
                }
            },
            complete: function(XMLHttpRequest, textStatus) {
                //隐藏正在查询图片
            },
            error: function() {
                //错误处理
            }
        });
    }
    var byteLength = (userName).replace(/[^\x00-\xff]/g, "aa").length;   
    if (byteLength < 4 || byteLength > 16) {
        $("#errorInfo").attr("innerHTML", '请将用户名控制在4-16个字符内！');
        $(".erroes").show();
        return false;
    }
    if (!checkName.test(userName)) {
        $("#errorInfo").attr("innerHTML", '用户名含特殊字符，请重新输入！');
        $(".erroes").show();
        return false;
    }
    if (pwd == "") {
        $("#errorInfo").attr("innerHTML", '请输入密码！');
        $(".erroes").show();
        return false;
    }
    else {
        if (pwd.length < 6 || pwd.length > 16) {
            $("#errorInfo").attr("innerHTML", '请将密码控制在6-16个字符内！');
            $(".erroes").show();
            return false;
        }
    }
    if (confirmpwd == "") {
        $("#errorInfo").attr("innerHTML", '请输入确认密码！');
        $(".erroes").show();
        return false;
    }
    else {
        if (confirmpwd != pwd) {
            $("#errorInfo").attr("innerHTML", '两次输入密码不一致！');
            $(".erroes").show();
            return false;
        }
    }
    if (code == "") {
        $("#errorInfo").attr("innerHTML", '请输入验证码！');
        $(".erroes").show();
        return false;
    }
    else {
        $.ajax({
            type: "Get",
            url: "/Ajax/CheckCode.ashx",
            data: "checkcode=" + escape(code),
            dataType: "json",
            cache: false,
            async: false,
            beforeSend: function(XMLHttpRequest) {
                $("#showResult").text("正在查询");
            },
            success: function(msg) {
                if (msg.JosnModel[0].Success == "0") {
                    $("#errorInfo").attr("innerHTML", '验证码有误,请重新输入！');
                    $(".erroes").show();
                    flag = false;
                }
            },
            complete: function(XMLHttpRequest, textStatus) {
                //隐藏正在查询图片
            },
            error: function() {
                //错误处理
            }
        });
    }
    if (!flag) {
        return flag;
    }
}

jQuery.fn.cssCheckBox = function() {
    $(":input[type=checkbox] + label").each(function() {
        if ($(this).prev()[0].checked)
        { $(this).addClass("chked"); }
    })
        .hover(
            function() { $(this).addClass("mOver"); },
            function() { $(this).removeClass("mOver"); }
            )
        .toggle(function()  /*不能 click，不然checked无法回到unchecked*/
        {

            $(this).prev()[0].checked = true;
            $(this).addClass("chked");
            if ($("#chcIsAgree").attr("checked")) {
                f = 0
            }
            $(this).inputs()
        },
            function() {
                $(this).prev()[0].checked = false;
                $(this).removeClass("chked").removeClass("mOver");
                if (!$("#chcIsAgree").attr("checked")) {
                    f = 1
                }
                $(this).inputs()
            })
            .prev()
            .hide();
}
jQuery.fn.inputs = function() {
    if ((f) == 0) {
        $(".regbutton").attr("disabled", false);
        $(".regbutton").mouseover(function() {
            $(this).addClass("hover_reg");
        })
        $(".regbutton").mouseout(function() {
            $(this).removeClass("hover_reg");
        })
    } else {
        $(".regbutton").attr("disabled", true);
    }
}
	
