﻿
$(function () {
    $.ajaxSetup({ cache: false });

    $.unblockUI();

    $("#inner_header_left").click(function () {
        window.location = "/Main/Index";
    });

    $("#login_button").click(function () {
        //$.blockUI({ message: '<div><div class="journal_ajax_load_text">Logging in...</div><div class="journal_ajax_load_main"></div><div class="clear"></div></div>' });
        //$("#hidden_login_button").click();
        attemptLogin();
    });

    $("#login_username").keypress(function (e) {
        if (e.which == 13) {
            //$("#hidden_login_button").click();
            attemptLogin();
        }
    });

    $("#login_password").keypress(function (e) {
        if (e.which == 13) {
            //$("#hidden_login_button").click();
            attemptLogin();
        }
    });

    $("#forgot_password_link").click(function () {
        $("#password_reset_email").val('Enter email address here').css('color', '#666362');
        $("#send_new_password").dialog("open");
    });

    $("#send_new_password").dialog({
        autoOpen: false,
        height: 200,
        width: 350,
        modal: true,
        resizable: false,
        buttons: { Cancel: function () { $(this).dialog("close"); }, "Reset": function () { resetPassword(); } }
    });

    $("#contact_window").dialog({
        autoOpen: false,
        height: 400,
        width: 400,
        modal: true,
        resizable: false,
        buttons: { Cancel: function () { $(this).dialog("close"); }, "Send Message": function () { sendContactMessage(); } }
    });

    $("#contact_link").click(function () {
        $("#contact_email").val('');
        $("#contact_message").val('');
        $("#contact_window").dialog("open");
    });

    $("#password_reset_email").css('color', '#666362').val('Enter email address here');

    $("#password_reset_email").focus(function () {
        if ($(this).val() == 'Enter email address here') {
            $(this).val('');
            $(this).css('color', '#000000');
        }
        else
            $(this).css('color', '#000000');
    });

    $("#password_reset_email").blur(function () {
        $(this).filter(function () {
            return ($(this).val() == "");
        }).css('color', '#666362').val('Enter email address here');
    });

    $("#signup_button").click(registerUser);

    $("#header_left").click(function () {
        window.location = "/Main/Index";
    });

    $("#header_left_no_image").click(function () {
        window.location = "/Main/Index";
    });

    $("#facebook_area").click(function () {
        window.open("http://www.facebook.com/pages/MyFitnessJournal/188773494482847", "MyFitnessJournal", "options");
        return false;
    });

    $("#twitter_area").click(function () {
        window.open("http://twitter.com/#!/myfitjournal", "MyFitnessJournal", "options");
        return false;
    });

    $("#login_form").validate({
        errorPlacement: function (error, element) { },
        rules: {
            UserName: {
                required: true
            },
            Password: {
                required: true
            }
        },

        messages: {
            UserName: {
                required: "Invalid username"
            },
            Password: {
                required: "Invalid password"
            }
        }
    });
});

function attemptLogin() {
    if ($("#login_form").valid()) {

        $.blockUI({ message: '<div><div class="journal_ajax_load_text">Logging in...</div><div class="journal_ajax_load_main"></div><div class="clear"></div></div>' });
        var model = $("#login_form").serialize();

        $.ajax({
            type: "POST",
            url: "/Main/Login",
            data: model,
            success: function (data, status, xhr) {

                if (data.result == "true") {
                    window.location = "/Home/Index";
                }
                else {
                    $.unblockUI();
                    alert(data.message);
                }
            },
            error: function (xhr, status, error) {
            }
        });
    }
    else {
        alert($("#login_form").validate().errorList[0].message);
    }
}

function sendContactMessage() {

    if ($("#contact_email").val() == "") {
        alert("Please enter your email address.");
        return;
    }

    if ($("#contact_message").val() == "") {
        alert("Please enter your message");
        return;
    }

    $.post("/Main/SendContactMessage", { "email_address": $("#contact_email").val(), "message": $("#contact_message").val() }, function (data) {
        if (data.result == 'true') {
            alert("Your message has been sent from " + data.email + ".\n\nWe will attempt to provide a response in a timely matter.");
            $("#contact_window").dialog("close");
        }
        else {
            if (data.reason != "") {
                alert(data.reason);
            }
            else
                alert("Unable to send message at this time.");
        }
    });
}

function resetPassword() {

    if ($("#password_reset_email").val() == "Enter email address here")
        alert("Please enter your email address.");
    else {
        $.post("/Main/ResetPassword", { "email_address": $("#password_reset_email").val() }, function (data) {
            if (data.result == 'true')
                alert("Your new password has been sent to " + data.email + ".\n\nNote you can always change your password from the Change Password link on the home page after you login");
            else
                alert("Unable to reset your password at this time.\n\nPlease contact customer support for additional help.");

            $("#send_new_password").dialog("close");
        });
    }
}

function registerUser() {
    if ($("#register_user").valid()) {

        var model = $("#register_user").serialize();

        $("#signup_area").block({
            message: '<div><div class="journal_ajax_load_text">Loading...</div><div class="journal_ajax_load"></div></div>'
        });

        $.post("/Main/Signup", model, function (data) {
            $("#signup_area").unblock();

            if (data.result == "true")
                window.location = "/Home/Index";
            else {
                if (data.reason != "")
                    alert(data.reason);
                else
                    alert("Unable to register at this time.\n\nPlease contact customer support for additional help.");
            }
        });
    }
    else {
        alert($("#register_user").validate().errorList[0].message);
    }
}


