﻿/// <reference path="jquery-1.3.2-vsdoc.js" />
/// <reference path="jquery.validate.js" />
/// <reference path="forms.js" />

//http://malsup.com/jquery/form/#api
//http://docs.jquery.com/Plugins/Validation/validate#options
$(document).ready(function() {
    //    $("#SendConfirmationEmail").click(function() {
    //        $.ajax({
    //            url: (urlPrefix + "services/UserService.asmx/SendConfirmation"),
    //            data: "email=" + $("#signInEmail").val()
    //        });
    //    });


    $("#Master_ForgotPasswordButton").click(function(event) {
        event.preventDefault();
        var emailVal = $('#Master_LoginEMail').val();
        if (emailVal.length == 0) {
            alert("Please enter the e-mail address on your account.");
            return;
        }
        $("#img_LoadForgotPwdMaster").show();
        $.wsAjax({
            url: (urlPrefix + "services/UserService.asmx/RequestPasswordReset"),
            data: function() {
                return {
                    email: $('#Master_LoginEMail').val()
                }
            },
            success: function(result) {
                $("#img_LoadForgotPwdMaster").hide();
                if (!result || result == "") {
                    alert("An email is on its way that includes everything you need to easily change your password.  Thanks!");
                }
                else {
                    alert("Uh oh, there was a problem.  " + result);
                }
            }
        });
    });
    $('#Master_LoginPwd, #Master_LoginEMail').keypress(function(e) {
        OnEnter(e, '#button_masterLogId');
    });
});
function SignUpFromMaster() {
    $.goTo(urlPrefix + "signup.aspx?e=" + $("#Master_SignUpMail").val());
}
function LogInFromMaster() {
    //We don't use jquery Validate plugin in here because having nested forms causes layout and other issues.
    //We need to use regular validation.    
    var IsValid = ValidateMasterLogIn();
    if (IsValid) {
        $("#img_LoadLoginMaster").show();
        $.wsAjax({
            url: (loginFormAction),
            data: function() {
                return {
                    email: $("#Master_LoginEMail").val(),
                    password: $("#Master_LoginPwd").val(),
                    rememberMe: $("#rememberme").is(':checked'),
                    visitorLogId: globalVisitorId
                }
            },
            success: function(result) {
                $("#img_LoadLoginMaster").hide();
                if (result == "reactivate") {
                    $.goTo("/myaccount/reactivate.aspx");
                    return;
                }
                if (result == "success") {
                    if ($("#redirectUrl").length != 0)
                        $.goTo($("#redirectUrl").val());
                    else
                        window.location.reload(true);
                }
                else {
                    if (result == "NotConfirmed") {
                        $.goTo("/confirm-resend.aspx?e=" + $("#Master_LoginEMail").val());
                    }
                    else
                        alert(result);
                }
            }
        });
    }
}

function ValidateMasterLogIn() {
    var bReturn=true;
    if ($("#Master_LoginEMail").val() == "" || $("#Master_LoginPwd").val()=="")
        bReturn = false
    return bReturn;
}