﻿var iDreamLogin = {
    PostFunction: null,
    visualstage: null,
    Timer: null,
    Begin: function() {
        $.ajax({
            type: "GET",
            url: "ajax/login/xt_login.asp",
            cache: false,
            success: function(msg) {
                //Test Unblock UI first:
                $.blockUI({ message: msg, css: { padding: 0, margin: 0, border: 0, height: "517px", width: "636px", top: ($(window).height() - 517) / 2 + 'px', left: ($(window).width() - 636) / 2 + 'px', cursor: 'default'} });
                //iDreamHold
                $('.blockOverlay').click($.unblockUI);
                $('#close_button').click($.unblockUI);
                if (!iDreamLogin.PostFunction)
                    $('#back_button').hide();
                //$('.blockOverlay').click();
                //$('#close_button').click();
                $('#frmLogin').submit(function() {
                    iDreamLogin.ShowMessage(); // Hide anything there
                    //Form has been submitted - what do we do?
                    if ($('#loginEmail').val().length == 0) {
                        iDreamLogin.ShowMessage("You forgot to enter your email address.");
                        $('#loginEmail').focus();
                    } else if ($('#loginPassword').val().length == 0) {
                        iDreamLogin.ShowMessage("You forgot to enter your password.");
                        $('#loginPassword').focus();
                    } else {
                        //Show a loading animation xD
                        iDreamLogin.ShowAnimated();

                        //Ajax Request
                        $.ajax({
                            type: "POST",
                            url: "ajax/login/xt_login.asp",
                            cache: false,
                            data: {
                                loginEmail: $('#loginEmail').val(),
                                loginPassword: $('#loginPassword').val()
                            },
                            success: function(msg) {
                                iDreamLogin.ShowAnimated(); // Clear Animation
                                switch (parseInt(msg)) {
                                    case -404:
                                        iDreamLogin.ShowMessage("Sorry I wasn't able to log you in, please try again.");
                                        iDreamLogin.ClearForm();
                                        break;
                                    case -403:
                                        iDreamLogin.ShowMessage("Sorry, it looks as if your account is suspended.");
                                        iDreamLogin.ClearForm(true);
                                        break;
                                    case -401:
                                        iDreamLogin.ShowMessage("Sorry I wasn't able to log you in, please try again.");
                                        iDreamLogin.ClearForm(true);
                                        break;
                                    default:
                                        lgUID = parseInt(msg);
                                        iDreamLogin.ClearForm();
                                        $(".register").load("ajax/login/xt_header.asp", { a: "0" }, function() {
                                            //Loaded Partial Header
                                            hook_top_actions();
                                        }); //force POST
                                        if (iDreamLogin.PostFunction) {
                                            iDreamLogin.PostFunction.call();
                                            iDreamLogin.PostFunction = null;
                                        } else {
                                            $.unblockUI();
                                        }
                                }
                            },
                            error: function() {
                                iDreamLogin.ShowAnimated(); // Clear Animation
                                iDreamLogin.ShowMessage("There was an error logging you in");
                            }
                        });
                    }
                    return false;
                });
                //Other functions needed
            },
            error: function() {
                $.blockUI({ message: "There was an error processing your request", timeout: 3000 });
                $('.blockOverlay').click($.unblockUI);
            }
        });
    },
    ClearForm: function(blJustPassword) {
        $('#loginPassword').val('');
        if (!blJustPassword) {
            $('#loginEmail').val('');
            $('#loginEmail').focus();
        } else {
            $('#loginPassword').focus();
        }
    },
    ShowMessage: function(msg) {
        if ($('#rtMessage').hasClass("Invisible"))
            $('#rtMessage').removeClass("Invisible");
        if (msg) {
            if ($('#rtMessage').html() == "")
                $('#rtMessage').fadeIn("def");
            $('#rtMessage').html(msg);
        } else {
            $('#rtMessage').hide();
            $('#rtMessage').html("");
        }
    },
    ShowAnimated: function() {
        if (iDreamLogin.Timer) {
            clearInterval(iDreamLogin.Timer);
            iDreamLogin.Timer = null;
            iDreamLogin.ShowMessage();
        } else {
            iDreamLogin.ShowMessage("Working");
            iDreamLogin.Timer = setInterval(function() {
                if ($('#rtMessage').html() == "...Working...") {
                    iDreamLogin.ShowMessage("Working");
                } else {
                    iDreamLogin.ShowMessage("." + $('#rtMessage').html() + ".");
                }
            }, 250);
        }
    }
}
