﻿// customer login
function Login() {
    // validate the customer input
    var flag = $("#frmLogin").validate().form();

    if (flag) {
        // name
        var name = $("#TxtName").val();
        // password
        var password = $("#TxtPassword").val();
        // validate code
        var code = $("#TxtCode").val();

        var url = "Data_Login.aspx?action=Login&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() {
                    //请求出错处理
                }
            });
        }
        
    }

}

// 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","ValidateCode.aspx?date=" + new Date());

}

function GetPassword() {
    // validate the customer input
    var flag = $("#frmForgetPassword").validate().form();

    if (flag) {
        // customer name
        var name = $("#TxtName").val();
        // question answer
        var answer = $("#TxtAnswer").val();
        // question id
        var question = $("#TxtQuestionID").val();

        var url = "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 = "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
    });
}