﻿//var going=0;

// customer login
function Login() {
    // validate the customer input
    var flag = $("#frmLogin").validate().form();
    
    if (flag)
     {
    
//        if(going == 1)
//            return;
//        
//        goint =1;
        
        // name
        var name = $("#TxtName").val();
        // password
        var password = $("#TxtPassword").val();
        // validate code
        var code = $("#TxtCode").val();

        //fix for login name
        var url = "/epoint/Data_Login.aspx?action=Login&name=" + escape(name) + "&password=" + password + "&code=" + code;

        var aCookie = document.cookie;
        
     
        // check the user error login times, if more than 5 show pop window
        if (aCookie != "" && aCookie.indexOf("errorTimes=5")>=0) {
            var windowWidth = document.documentElement.clientWidth;
            var windowHeight = document.documentElement.clientHeight;

            var popupHeight = $("#popUpHelp").height();
            var popupWidth = $("#popUpHelp").width();
            //centering
            $("#popUpHelp").css({
                "position": "absolute",
                "top": document.documentElement.scrollTop + windowHeight / 2 - popupHeight / 2 - 100,
                "left": windowWidth / 2 - popupWidth / 2 - 100
            });
            $("#backgroundPopup").css({
                "opacity": "0.3"
            });
            $("#backgroundPopup").fadeIn("slow");
            $("#popUpHelp").fadeIn("slow");
            $("#backgroundPopup").css({
                "height": document.documentElement.clientHeight
            });
        }
        else {
            $.ajax({
                type: "get",
                url: url,
                dataType: "text",
                data: { t: new Date() },
                beforeSend: function(XMLHttpRequest) {
                    $("#loadingImage").show();
                },
                success: function(data, textStatus) {
                    var arrMessage = data.split('*');
                  // alert("arrMessage[0]:"+arrMessage[0]+"arrMessage[1]:"+arrMessage[1]);
                    // login success
                    if (arrMessage[0] == arrMessage[1]) {
                        location.href = arrMessage[2];
                    }
                   //login 失败 
                    else {
                   //alert(arrMessage[1]); 
                        $("#divError").html(arrMessage[1]);
                     //出错后，重新刷新验证码 
                      createCode(); 
                        
//                        if (typeof(going)=='undefined'){
//                             var going=0;
//                        }
//                        going=0;    

                        return;
                    }
                },
                complete: function(XMLHttpRequest, textStatus) {
                    $("#loadingImage").hide();
                    
//                    if (typeof(going)=='undefined'){
//                         var going=0;
//                    }
//                    going=0;    
                },
                error: function() {
                    //请求出错处理
//                    if (typeof(going)=='undefined'){
//                         var going=0;
//                    }
//                    going=0;    
                }
            });
        }
        
    }

}

// close the pop window
function CloseDiv() {
    $("#popUpHelp").fadeOut("slow");
    $("#backgroundPopup").fadeOut("slow");
}

// go to forget password page
function ForgetPassword() {
    location.href = "ForgetPassword.aspx";
}

// go to register page
function GoRegister() {
    location.href = "CustomerRegister.aspx";
}
// create the validate code
function createCode() {

    //$("#divCodeImage").html('<img src="ValidateCode.aspx?date=' + new Date() + '" alt="" />');
    $("#divCodeImage").attr("src","/epoint/ValidateCode.aspx?date=" + new Date());

}

function GetPassword() {
    // validate the customer input
    var flag = $("#frmForgetPassword").validate().form();
    if (flag) {
        var name = $("#TxtName").val();
//        var answer = $("#TxtAnswer").val();
//        var question = $("#TxtQuestionID").val();
        var url = "/epoint/Data_Login.aspx?action=GetPassword&name=" + name;// + "&answer=" + answer + "&question=" + question;
        $("#loadingImage").show();
        $.get(url, { t: new Date() }, function call(data) {
            var arrMessage = data.split('*');
            if (arrMessage[0] == arrMessage[1]) {
                var windowWidth = document.documentElement.clientWidth;
                var windowHeight = document.documentElement.clientHeight;
                var popupHeight = $("#popUpHelp").height();
                var popupWidth = $("#popUpHelp").width();
                //centering
                $("#popUpHelp").css({
                    "position": "absolute",
                    "top": document.documentElement.scrollTop + windowHeight / 2 - popupHeight / 2 - 150,
                    "left": windowWidth / 2 - popupWidth / 2 - 150
                });
                $("#backgroundPopup").css({
                    "opacity": "0.3"
                });
                $("#backgroundPopup").fadeIn("slow");
                $("#popUpHelp").fadeIn("slow");
                $("#backgroundPopup").css({
                    "height": document.documentElement.clientHeight
                });
            }
            else {
                $("#divError").html(arrMessage[0]);
            }
            $("#loadingImage").hide();
        });
    }
}

// customer login
function ActiveLogin() {
    // validate the customer input
    var flag = $("#frmActiveLogin").validate().form();

    if (flag) {
        var name = $("#TxtName").val();
        var password = $("#TxtPassword").val();
        var code = $("#TxtCode").val();

        var url = "/epoint/Data_Login.aspx?action=ActiveLogin&name=" + name + "&password=" + password + "&code=" + code;
        var aCookie = document.cookie;
                
        // check the user error login times, if more than 5 show pop window
        if (aCookie != "" && aCookie.indexOf("errorTimes=5") >= 0) {
            var windowWidth = document.documentElement.clientWidth;
            var windowHeight = document.documentElement.clientHeight;

            var popupHeight = $("#popUpHelp").height();
            var popupWidth = $("#popUpHelp").width();
            //centering
            $("#popUpHelp").css({
                "position": "absolute",
                "top": document.documentElement.scrollTop + windowHeight / 2 - popupHeight / 2 - 100,
                "left": windowWidth / 2 - popupWidth / 2 - 100
            });
            $("#backgroundPopup").css({
                "opacity": "0.3"
            });
            $("#backgroundPopup").fadeIn("slow");
            $("#popUpHelp").fadeIn("slow");
            $("#backgroundPopup").css({
                "height": document.documentElement.clientHeight
            });
        }
        else {
            $.ajax({
                type: "get",
                url: url,
                dataType: "text",
                data: { t: new Date() },
                beforeSend: function(XMLHttpRequest) {
                    $("#loadingImage").show();
                },
                success: function(data, textStatus) {
                    var arrMessage = data.split('*');
                    if (arrMessage[0] == arrMessage[1]) {
                        location.href = arrMessage[2];
                    }
                    else {
                        $("#divError").html(arrMessage[1]);
                        return;
                    }
                },
                complete: function(XMLHttpRequest, textStatus) {
                    $("#loadingImage").hide();
                },
                error: function() {
                    //请求出错处理
                }
            });
        }

    }

}

// remove the default text in the textbox
function DeleteValue() {
    var txtValue = $("#TxtName").val();
    if (txtValue == "会员卡号/电子邮件") {
        $("#TxtName").val("");
    }

}

function HelpClick() {
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;

    var popupHeight = $("#popUpHelp").height();
    var popupWidth = $("#popUpHelp").width();
    //centering
    $("#popUpHelp").css({
        "position": "absolute",
        "top": document.documentElement.scrollTop + windowHeight / 2 - popupHeight / 2 - 100,
        "left": windowWidth / 2 - popupWidth / 2 - 100
    });
    $("#backgroundPopup").css({
        "opacity": "0.3"
    });
    $("#backgroundPopup").fadeIn("slow");
    $("#popUpHelp").fadeIn("slow");
    $("#backgroundPopup").css({
        "height": document.documentElement.clientHeight
    });
}
