﻿function changePassword() {
        var usernameElement = $("#textBoxUsername").val();
        usernameElement = $.trim(usernameElement);

        if (usernameElement) {
            var hostName = location.hostname;
            var url = "/Handlers/Account/ChangePassword.ashx";
            $("ul.validation-summary-errors > li").remove();
            $("form").ajaxSubmit({ url: url, type: "POST", dataType: "json", success: changePasswordSuccess });
        }
        else {
            $("#textBoxUsername").css({ "border": "1px solid red" });
            $("#textBoxUsername").blur(function () {
                $(this).css({ "border": "1px solid #B5B8C8" });
            });
            $("#textBoxUsername").nextAll("label.error").show();
            $("#textBoxUsername").addClass("error");
                
            $("#textBoxUsername").after(
                $("<label />").addClass("error").text("THIS FIELD IS REQUIRED.")
            );
            $("#throbber").hide();
        }

        $("#textBoxUsername").keypress(function () {
            $(this).next("label").remove();
        });
}

function changePasswordSuccess(data) {

    $("#throbber").hide();

    if (data.ErrorMessage) {
        $("ul.validation-summary-errors").append("<li>" + data.ErrorMessage + "</li>");
    }
    else {
        if (data.UserServices > 1) {

            var hostName = location.hostname;
            var url = "/Account/ChangePassword.aspx";

            hookColorBoxWithContent(url, 500, 300);
        }
        else {
            if (data.PasswordChanged == "1") {

                var hostName = location.hostname;
                var url = "/Account/ChangePasswordSuccess.aspx";

                hookColorBoxWithContent(url, 470, 540);
            }
        }
    }
}

function displayPasswordError(message) {
    $.fn.colorbox.close();
    $("ul.validation-summary-errors").append("<li>" + message + "</li>"); 
}

function displayPasswordSuccess() {
    var hostName = location.hostname;
    var url = "/Account/ChangePasswordSuccess.aspx";
    
    $("#cboxIframe").attr("src", url);
}
