﻿var account =
{
    init: function()
    {
        if ($('signin-form') != null)
        {
            $('signin-form').onsubmit = function()
            {
                account.signIn();

                return false;
            }

            if ($('profile-delete-form') != null)
            {
                $('profile-delete-form').onsubmit = function()
                {
                    account.close('/');

                    return false;
                }
            }

            if ($('dialog-sign-in-form') != null)
            {
                $('dialog-sign-in-form').onsubmit = function()
                {
                    account.dialogSignIn();

                    return false;
                }
            }


        }
    },

    dialogSignIn: function(pid, ptype)
    {
        $('dialog-sign-in-box').addClassName('status');
        $('dialog-sign-in-box').innerHTML = 'Please wait while we sign you in ...';

        _form = $('dialog-sign-in-form');

        new Ajax.Request('/account/signin',
        {
            method: 'post',
            parameters:
            {
                emailAddress: _form['dialog-email'].value,
                password: _form['dialog-password'].value,
                isDialog: true
            },

            onSuccess: function(transport)
            {
                if (transport.responseText.include('id="exception"'))
                {
                    $('dialog-error-sign-in').addClassName('error');
                    $('dialog-error-sign-in').innerHTML = transport.responseText;

                    $('dialog-sign-in-box').className = '';
                    $('dialog-sign-in-box').innerHTML = '<button id="dialog-sign-in-button" type="submit" tabindex="1002">sign in</button> or <a href="/pages/view/sign-up-now">Sign Up</a>';
                }
                else
                {
                    messenger.quickUpdate('Account', 'You have successfuly signed in to your account, please enjoy.');

                    new PeriodicalExecuter(
                            function(executer)
                            {
                                menu.addToBasket(pid, ptype);
                                account.dialogUpdateProfile();
                                executer.stop();
                            }, 2);
                }
            },

            onFailure: function()
            {
                $('dialog-error-sign-in').addClassName('error');
                $('dialog-error-sign-in').innerHTML = 'An error occurred while attemping to sign up';

                $('dialog-sign-in-box').className = '';
                $('dialog-sign-in-box').innerHTML = '<button id="dialog-sign-in-button" type="submit" tabindex="1002">sign in</button> or <a href="/pages/view/sign-up-now">Sign Up</a>';
            },

            onException: function(requester, exception)
            {
                alert(exception.message);
            }
        });
    },

    dialogUpdateProfile: function()
    {
        new Ajax.Request('/account/showprofile',
        {
            method: 'get',

            onSuccess: function(transport)
            {
                if (transport.responseText.include('id="exception"'))
                {
                    new PeriodicalExecuter(
                            function(executer)
                    {
                                window.location.reload();
                                executer.stop();
                            }, 2);
                }
                else
                    $('signin-form').innerHTML = transport.responseText;
            }
        });
    },

    signIn: function()
    {
        $('sign-in-box').addClassName('status');
        $('sign-in-box').innerHTML = 'Please wait while we sign you in ...';

        _form = $('signin-form');

        new Ajax.Request('/account/signin',
        {
            method: 'post',
            parameters:
            {
                emailAddress: _form['email'].value,
                password: _form['password'].value
            },

            onSuccess: function(transport)
            {
                if (transport.responseText.include('id="exception"'))
                {
                    $('error-sign-in').addClassName('error');
                    $('error-sign-in').innerHTML = transport.responseText;

                    $('sign-in-box').className = '';
                    $('sign-in-box').innerHTML = '<button id="sign-in-button" type="submit" tabindex="3">sign in</button>';
                }
                else
                {
                    $('signin-form').innerHTML = transport.responseText;

                    if (typeof (menu) != "undefined")
                    {
                        menu.updateBasket();
                    }

                    messenger.quick('Account', 'You have successfuly signed in to your account, please enjoy.');
                }
            },

            onFailure: function()
            {
                $('error-sign-in').addClassName('error');
                $('error-sign-in').innerHTML = 'An error occurred while attemping to sign up';

                $('sign-in-box').className = '';
                $('sign-in-box').innerHTML = '<button id="sign-up-button" type="submit" tabindex="9">sign up</button>';
            },

            onException: function(requester, exception)
            {
                alert(exception.message);
            }
        });
    },

    signOut: function()
    {
        new Ajax.Request('/account/signout',
        {
            method: 'post',

            onSuccess: function(transport)
            {
                $('signin-form').innerHTML = transport.responseText;

                if (typeof (menu) != "undefined")
                {
                    menu.clearBasket();
                }

                messenger.quick('Account', 'You have successfuly signed out of your account');
            },

            onFailure: function()
            {
                alert('An error occurred while attemping to sign out');
            },

            onException: function(requester, exception)
            {
                alert(exception.message);
            }
        });
    },

    signInDialog: function(pid, ptype)
    {
        new Ajax.Request('/parts/dialogSignInForm',
        {
            method: 'get',
            parameters:
            {
                id: pid,
                type: ptype
            },

            onSuccess: function(transport)
            {
                messenger.update('Sign In', transport.responseText);
            },

            onFailure: function()
            {
                messenger.update('Sign In', 'An error occurred while attemping to sign up');
            },

            onException: function(requester, exception)
            {
                alert(exception.message);
            }
        });
    },

    close: function(url)
    {
        if (confirm("Are you sure you would like to close your account?"))
        {
            new Ajax.Request('/account/close',
            {
                method: 'post',

                onSuccess: function(transport)
                {
                    messenger.open('Account', 'You have successfuly closed your account, you will now be taken to the home page.');

                    window.location = url;
                },

                onFailure: function()
                {
                    alert('An error occurred while attemping to sign out');
                },

                onException: function(requester, exception)
                {
                    alert(exception.message);
                }
            });
        }
    }
}

window.onload =  account.init;