﻿///<reference path='jquery-1.3.2.js' />
///<reference path='Validation.js' />

$(document).ready(function() {
    createLoginDialog();
    $("#loginLink").click(function() {
        openLoginDialog();
    });
    bindLogoutLinkClickEvent();

    createRegisterDialog();
    $("#registerLink").click(function() {
        $("#registerErrSummary").hide();
        $("#registerDialog").dialog("open");
    });
});

/*****************
* Login, Logout *
*****************/
function openLoginDialog() {
    if (!$("#loginDialog").dialog("isOpen")) {
        $("#errSummary").hide();
        $("#loginDialog").dialog("open");
    }
}
function bindLogoutLinkClickEvent() {
    $("#logoutLink").click(function() {
        $(this).unbind("click").text("正在退出...");
        logout();
    });
}

function logout() {
    $.ajax({
        url: "Services/DNTUserService.aspx?act=logout",
        data: {},
        type: "GET",
        success: function(result) {
            if (result == "1") {
                showMessage("退出成功", "现在您已安全退出:)", function() { location.href = 'Default.aspx'; }, function() { location.href = 'Default.aspx' }, true);
            } else {
                bindLogoutLinkClickEvent();
                alert(result);
            }
        },
        cache: false
    });

}

function createLoginDialog() {
    $("#loginDialog").dialog({ title: "登陆", modal: true, resizable: false, autoOpen: false, bgiframe: true,
        close: function() { clearForm("loginDialog"); },
        buttons:
        {
            '我要注册': function() {
                $(this).dialog('close');
                openRegisterDialog();
            },
            '登陆': function() {
                if (!validateLoginForm()) {
                    return;
                }

                $("#loginInfoMsg").show().text("正在登陆，请稍候...");

                $.ajax({
                    url: "Services/DNTUserService.aspx?act=login",
                    data: { userName: $("#txt_userName").val(), password: $("#txt_pwd").val() },
                    type: "POST",
                    success: function(result) {
                        var opResultDto = eval("(" + result + ")");
                        if (opResultDto.Status == 1) {
                            $("#loginDialog").dialog("close");
                            if (onLogined) {
                                onLogined();
                            }
                        } else {
                            showErrorSummary("#errSummary", opResultDto.Message);
                        }
                        $("#loginInfoMsg").hide();
                    },
                    error: function(xhr) {
                        alert(xhr.responseText);
                        $("#loginInfoMsg").hide();
                    }
                });

            }
        }
    });
}

// Act as an event handler, which will be triggered after login.
var onLogined = function() {
    showMessage("登陆成功", "登陆成功，正在转入用户中心...", "", "", false);
    location.href = "UserCenter.aspx";
}

function validateLoginForm() {
    var isValid = true;
    var userNameErrTipEle = $("#txt_userName").next("span");
    var pwdErrTipEle = $("#txt_pwd").next("span");
    userNameErrTipEle.hide();
    pwdErrTipEle.hide();
    $("#errSummary").hide();

    var errText = "";

    if (!ValidateRequired("#txt_userName")) {
        isValid = false;
        userNameErrTipEle.show();
        errText += "用户名不能为空; ";
    }
    if (!ValidateRequired("#txt_pwd")) {
        isValid = false;
        pwdErrTipEle.show();
        errText += "密码不能为空; ";
    }

    if (!isValid) {
        showErrorSummary("#errSummary", errText);
    }

    return isValid;
}

/*******************
* Register ********
******************/
function createRegisterDialog() {
    $("#registerDialog").dialog({
        title: "注册",
        modal: true,
        resizable: false,
        autoOpen: false,
        close: function() { clearForm("registerDialog"); },
        buttons: {
            '我已经有帐号了': function() {
                $(this).dialog("close");
                openLoginDialog();
            },
            '注册': function() {
                if (!validateRegisterForm()) {
                    return;
                }

                $("#registerInfoMsg").show().text("正在处理中，请稍候...");

                $.ajax({
                    url: "Services/DNTUserService.aspx?act=register",
                    type: "POST",
                    data: { userName: $("#n_userName").val(), password: $("#n_pwd1").val(), tel: $("#n_tel").val(), email: $("#n_email").val(), qq: $("#n_qq").val(), msn: $("#n_msn").val() },
                    success: function(result) {
                        var opResult = eval("(" + result + ")");
                        if (opResult.Status == "1") {
                            //ok
                            onRegisterSuccess();
                        } else {
                            showErrorSummary("#registerErrSummary", opResult.Message);
                        }
                        $("#registerInfoMsg").hide();
                    },
                    error: function(xhr) {
                        document.write(xhr.responseText);
                        $("#registerInfoMsg").hide();
                    }
                });
            }
        }
    });
}

var onRegisterSuccess = function() {
    $("#registerDialog").dialog("close");
    showMessage("注册成功", "恭喜，您已注册成功!<br/>快点击确定进入用户中心吧:)", function() { location.href = 'UserCenter.aspx'; }, "", false);
};

function openRegisterDialog() {
    if (!$("#registerDialog").dialog("isOpen")) {
        $("#registerErrSummary").hide();
        $("#registerDialog").dialog("open");
    }
}

function validateRegisterForm() {
    var isValid = true;
    var errMsg = "";

    if (!ValidateRequired("#n_userName")) {
        isValid = false;
        errMsg += "用户名不能为空; ";
    }
    if (!ValidateRequired("#n_pwd1", "value", 6)) {
        isValid = false;
        errMsg += "密码不能为空且至少要有6个字符; ";
    } else if (!ValidateEquation("#n_pwd1", "#n_pwd2")) {
        isValid = false;
        errMsg += "两次输入的密码不一致; ";
    }
    if (!ValidateNumber("#n_tel")) {
        isValid = false;
        errMsg += "联系电话不能为空，且必须为数字; ";
    }
    // ValidateEmail allow empty input by default
    if (!ValidateEmail("#n_email")) {
        isValid = false;
        errMsg += "E-mail格式不正确; ";
    }
    // ValidateNumber allow empty input by default
    if (!ValidateNumber("#n_qq")) {
        isValid = false;
        errMsg += "QQ必须为数字; ";
    }
    if (!ValidateEmail("#n_msn")) {
        isValid = false;
        errMsg += "MSN格式不正确;";
    }

    if (!isValid) {
        showErrorSummary("#registerErrSummary", errMsg);
    }

    return isValid;
}

// clear the values of all form elements which are children of '#parentId'
function clearForm(parentId) {
    var formElements = $("#" + parentId).find("input[type='text'],input[type='password']");
    formElements.val('');
}