﻿// JavaScript Document
/*
Content: Common components;
Remark: login;
Version: v0.1.1;
Author: guanxin.com JF.Z;
Date: 2011-02-11
*/

var email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+/;
var erroremail = "出错啦~   用户名不能为空";
var errorpwd = "出错啦~    密码位数应为6-16位字符";
var pwdnull = "出错啦~     密码不能为空";
var flag = true;
var a = b = 1;
var TRady = null;
$.fn.chkCss = function() {
    //初始化隐藏错误
    $(".erroes").hide()
    //设置关闭按钮
    $(".erroes dd").click(function() {
        $(".erroes").hide()
    })
    //初始化登录按钮
    $(".LoginDo").attr("disabled", true);
    //初始化防刷新
    TRady = setTimeout(function(){    	
	    if ($("input[name='userName']").val().length != 0) {
	        $(this).riglts(this, "errorTt", "none", "5px 0px", "#000");
	        a = 0;
	    }
	    if ($("input[name='userPwd']").val().length != 0 && $("input[name='userPwd']").val().length > 5) {
	        $(this).riglts(this, "errorPw", "none", "5px 0px", "#000");
	        b = 0;
	    }
	    $(".mcLeft").append("<span style=\"display:none;\">"+ a +"/"+b+"</span>");
	    //验证输入是否正确
	     $(this).Disabled();	     
    	clearTimeout(TRady);
    },150);
    //验证信息
    $("input[name='userName'],input[name='userPwd']").each(function() {
        $(this).focusin(function() {
            if (this.id == "txtUserName") {
                //var byteLength = ($(this).val()).replace(/[^\x00-\xff]/g, "aa").length;
                //if (byteLength < 4 && byteLength > 0)
                    //$(".username_title").css({ "top": $(this).offset().top + 20, "left": $(this).offset().left + 50 })
            } else {
                if ($(this).val().length < 6)
                    $(".userpwd_title").css({ "top": $(this).offset().top + 20, "left": $(this).offset().left + 50 })
            }
            $(this).css({ "border": "1px solid #006EE9" })
            $(this).css({ "color": "#000" })
        })
        $(this).focusout(function() {
            $(".username_title").css({ "top": "-99999px", "left": "-99999px" })
            $(".userpwd_title").css({ "top": "-99999px", "left": "-99999px" })
            if (this.id == "txtUserName") {
                //$(".username_title").css({ "top": "-99999px", "left": "-99999px" })
                if ($(this).attr("value") != "") {

                    $(this).riglts(this, "errorTt", "none", "5px 0px", "#000");
                    a = 0;

                } else {
                    $(this).riglts(this, "errorTt", "none", "5px 23px", "#000");
                    a = 1;
                }
            } else {
                //$(".userpwd_title").css({ "top": "-99999px", "left": "-99999px" })
                if ($(this).attr("value") != "" && $(this).attr("value").length > 5) {
                    $(this).riglts(this, "errorPw", "none", "5px 0px", "#000");
                    b = 0;
                } else {
                    $(this).riglts(this, "errorPw", "none", "5px 23px", "#000");
                    b = 1;
                }
            }
            $(this).css({ "border": "1px" })
            $(this).css({ "color": "#000" })
            $(this).Disabled()
        })
        $(this).keyup(function() {
            if (this.id == "txtUserName") {
                //$(".username_title").css({ "top": "-99999px", "left": "-99999px" })
                if ($(this).attr("value") != "") {
                    $(this).riglts(this, "errorTt", "none", "5px 0px", "#000");
                    $(".username_title").css({ "top": "-99999px", "left": "-99999px" })
                    a = 0;
                } else {
                    $(this).riglts(this, "errorTt", "none", "5px 23px", "#000");
                    a = 1;
                }
            } else {
                //$(".userpwd_title").css({ "top": "-99999px", "left": "-99999px" })
                if ($(this).attr("value") != "" && $(this).attr("value").length > 5) {
                    $(this).riglts(this, "errorPw", "none", "5px 0px", "#000");
                    $(".userpwd_title").css({ "top": "-99999px", "left": "-99999px" })
                    b = 0;
                } else {
                    $(this).riglts(this, "errorPw", "none", "5px 23px", "#000");
                    b = 1;
                }
            }
            $(this).css({ "border": "1px" })
            $(this).css({ "color": "#000" })
            $(this).Disabled();
        })
    })
    //选择cilckbox
    $(":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");
        },
            function() {
                $(this).prev()[0].checked = false;
                $(this).removeClass("chked").removeClass("mOver");
            })
            .prev()
            .hide();
};
$.fn.error = function(obj, Class, Border, Style, Color, Error) {
    $(obj).css({ "border": Border })
    $(obj).css({ "color": Color })
    $("." + Class).css({ "background-position": Style })
    $(".erroes").show(200)
    $(".erroes dl").html(Error)
}
$.fn.riglts = function(obj, Class, Border, Style, Color, Error) {
    $(obj).css({ "border": Border })
    $(obj).css({ "color": Color })
    $("." + Class).css({ "background-position": Style })
    $(".erroes").hide(200)
    $(".erroes dl").html(Error)
}
$.fn.Disabled = function() {
    if ((a + b) == "0") {
        $(".LoginDo").attr("disabled", false);
        $(".LoginDo").css({ "color": "#706B6B" })
    } else {
        $(".LoginDo").attr("disabled", true);
    }
}
function reg() {
    location.href = "http://passport.guanxin.com/User/RegUser";
}
function checklogin() {
    var userName = $('#txtUserName').val();
    var pwd = $('#txtUserPwd').val();
    var flag = true;
    if (userName == "") {
        $("#errorInfo").attr("innerHTML", '出错啦~~    用户名不能为空！');
        $(".erroes").show();
        return false;
    }
    //    else {       
    //        
    //    }
    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;
        }
    }
    $.ajax({
        type: 'Post', //URL方式为POST
        url: '/Ajax/CheckNameEmail.ashx', //这里是指向登录验证的页面
        data: 'parms=' + escape(userName), //把要验证的参数传过去
        dataType: 'json', //数据类型为JSON格式的验证
        async: false,
        //在发送数据之前要运行的函数 
        beforeSend: function() {
            //$('#confirm').html('登录中.........');
        },
        success: function(data) {
            //sta就是那个输出到客户端的标示
            if (data.JosnModel[0].Success == "0") {
                $("#errorInfo").attr("innerHTML", '出错啦~~   您输入的用户名或邮箱不存在！');
                $(".erroes").show();
                flag = false;
            }
        }
    });
    if (!flag) {
        return flag;
    }
    $.ajax({
        type: 'Post', //URL方式为POST
        url: '/Ajax/CheckLogin.ashx', //这里是指向登录验证的页面
        data: 'uname=' + escape(userName) + '&pwd=' + escape(pwd), //把要验证的参数传过去
        dataType: 'json', //数据类型为JSON格式的验证
        async: false,
        //在发送数据之前要运行的函数 
        beforeSend: function() {
            //$('#confirm').html('登录中.........');
        },
        success: function(data) {
            //Success就是那个输出到客户端的标示			
            if (data.Success == 0) {
                $("#errorInfo").attr("innerHTML", data.Description);
                $(".erroes").show();
                flag = false;
            }
        }
    });
    if (!flag) {
        return flag;
    }
}
