﻿
ALL = function () {

    var main = function () {

        var cmp = null;


        var init = function () {

            initAccount();
            initContact();
            initMarketReport();
            initFAQ();
            initHome();
            initLogin();
            initRegister();
            initInfopack();

        };


        var initAccount = function () {

            if ($('.accountForm').length === 0) { return; }


            $('.accountForm INPUT').keypress(function (e) {

                if (e.which === 13) {

                    e.preventDefault();

                    updateAccount();

                }

            });
            $('.accountForm .updateDetails A').click(function (e) {

                e.preventDefault();

                updateAccount();

            });

        };


        var initContact = function () {

            /* Setup contact form */

            if ($('.contactForm').length === 0) { return; }


            $('.contactForm INPUT').keypress(function (e) {

                if (e.which === 13) {

                    e.preventDefault();

                    contactUs();

                }

            });
            $('.contactForm .submit A').click(function (e) {

                e.preventDefault();

                contactUs();

            });

            if (cmp.contactType === 'info') {

                $('#ddl_type').val('Information Pack');

            }

        };

        var initMarketReport = function () {

            /* Setup contact form */

            if ($('.marketReportForm').length === 0) { return; }


            $('.marketReportForm INPUT').keypress(function (e) {

                if (e.which === 13) {

                    e.preventDefault();

                    marketReport();

                }

            });
            $('.marketReportForm .submit A').click(function (e) {

                e.preventDefault();

                marketReport();

            });

        };

        var initFAQ = function () {

            $('.faq .questionGroup').toggle(function () {

                $(this).find('.question').show('fast');

                $(this).css('background-image', 'url(i/faqLev1_ON.gif)');

            }, function () {

                $(this).find('.question').hide('fast');

                $(this).css('background-image', 'url(i/faqLev1_OFF.gif)');

            });

            $('.faq .question').toggle(function () {

                $(this).find('P').show('fast');

                $(this).css('background-image', 'url(i/faqLev2_ON.gif)');

            }, function () {

                $(this).find('P').hide('fast');

                $(this).css('background-image', 'url(i/faqLev2_OFF.gif)');

            });

        };


        var initHome = function () {

            $('#homepageQuotes').cycle({ timeout: 10000 });

        };


        var initLogin = function () {


            $('#login INPUT').keypress(function (e) {

                if (e.which === 13) {

                    e.preventDefault();

                    login();

                }

            });
            $('#login .go A').click(function (e) {

                e.preventDefault();

                login();

            });

            $('#account .logout A').click(function (e) {

                e.preventDefault();

                logout();

            });

        };


        var initRegister = function () {

            /* Setup contact form */

            if ($('.registerForm').length === 0) { return; }


            $('.registerForm INPUT').keypress(function (e) {

                if (e.which === 13) {

                    e.preventDefault();

                    register();

                }

            });
            $('.registerForm .completeRegistration A').click(function (e) {

                e.preventDefault();

                register();

            });

        };

        var initInfopack = function () {

            /* Setup contact form */

            if ($('.infopackForm').length === 0) { return; }


            $('.infopackForm INPUT').keypress(function (e) {

                if (e.which === 13) {

                    e.preventDefault();

                    infopack();

                }

            });
            $('.infopackForm .submit A').click(function (e) {

                e.preventDefault();

                infopack();

            });

        };


        var contactUs = function () {

            var name = $('#txt_name').val();
            var telephone = $('#txt_telephone').val();
            var email = $('#txt_email').val();
            var type = $('#ddl_type OPTION:selected').val();
            var comments = $('#txt_comments').val();

            var errors = '';

            if (name === '') { errors += '* Please enter your name.<br />'; }
            if (telephone === '') { errors += '* Please enter your telephone number.<br />'; }

            if (errors.length > 0) { $('.contactForm .status').html(errors); return; }


            $('.contactForm .status').html('');


            TVI.ajax({

                data: '{ \"d\": {' +
                '\"name\": \"' + name + '\",' +
                '\"telephone\": \"' + telephone + '\",' +
                '\"email\": \"' + email + '\",' +
                '\"type\": \"' + type + '\",' +
                '\"comments\": \"' + comments + '\"' +
                '} }',

                url: 'Handlers/ALL.aspx/contactUs',

                success: function () {

                    $('.contactForm .status').html('Thank you, your message has been sent.');

                    $('#txt_name').val('');
                    $('#txt_telephone').val('');
                    $('#txt_email').val('');
                    $('#txt_comments').val('');

                }

            });

        };

        var marketReport = function () {

            var name = $('#txt_name').val();
            var telephone = $('#txt_telephone').val();
            var email = $('#txt_email').val();

            var errors = '';

            if (name === '') { errors += '* Please enter your name.<br />'; }
            if (telephone === '') { errors += '* Please enter your telephone number.<br />'; }

            if (errors.length > 0) { $('.marketReportForm .status').html(errors); return; }


            $('.marketReportForm .status').html('');


            TVI.ajax({

                data: '{ \"d\": {' +
                '\"name\": \"' + name + '\",' +
                '\"telephone\": \"' + telephone + '\",' +
                '\"email\": \"' + email + '\"' +
                '} }',

                url: 'Handlers/ALL.aspx/marketReport',

                success: function () {

                    $('.marketReportForm .status').html('Thank you, your details have been sent.');

                    $('#txt_name').val('');
                    $('#txt_telephone').val('');
                    $('#txt_email').val('');

                }

            });

        };



        var login = function () {

            var email = $('#txt_loginEmail').val();
            var password = $('#txt_loginPassword').val();

            if (email === '' || password === '') { return; }


            TVI.ajax({

                data: '{ \"d\": {' +
                '\"email\": \"' + email + '\",' +
                '\"password\": \"' + password + '\"' +
                '} }',

                url: 'Handlers/ALL.aspx/login',

                success: function (e) {

                    window.location = 'account.aspx';

                },

                failure: function () {

                    $('#login .TVI-form').hide();
                    $('#login .error').show();

                    setTimeout(function () {

                        $('#login .error').hide();
                        $('#login .TVI-form').show();

                    }, 2000);

                }

            });

        };


        var logout = function () {

            TVI.ajax({

                url: 'Handlers/ALL.aspx/logout',

                success: function (e) {

                    window.location = '/';

                }

            });

        };


        var register = function () {

            var firstname = $('#txt_firstname').val();
            var lastname = $('#txt_lastname').val();
            var telephone = $('#txt_telephone').val();
            var mobile = $('#txt_mobile').val();
            var address = $('#txt_address').val();
            var email = $('#txt_email').val();
            var emailConfirm = $('#txt_emailConfirm').val();
            var password = $('#txt_password').val();
            var passwordConfirm = $('#txt_passwordConfirm').val();
            var experienceShares = $('#cb_experienceShares:checked').val();
            var experienceCommodities = $('#cb_experienceCommodities:checked').val();
            var experienceAlternative = $('#cb_experienceAlternative:checked').val();
            var investmentLevel = $('#ddl_investmentLevel OPTION:selected').val();
            var averageInvestment = $('#ddl_averageInvestment OPTION:selected').val();


            var errors = '';

            if (firstname === '') { errors += '* Please enter your first name.<br />'; }
            if (lastname === '') { errors += '* Please enter your last name.<br />'; }
            if (telephone === '') { errors += '* Please enter your telephone number.<br />'; }
            if (email !== '') {
                if (email !== emailConfirm) { errors += '* The email addresses don\'t match.<br />'; }
            }
            else { errors += '* Please enter your email address.<br />'; }
            if (password !== '') {
                if (password !== passwordConfirm) { errors += '* The passwords don\'t match.<br />'; }
            }
            else { errors += '* Please enter your password.<br />'; }
            if (!experienceShares && !experienceCommodities && !experienceAlternative) { errors += '* Please select your trading experience.<br />'; }
            if (investmentLevel === 'Select amount...') { errors += '* Please select your level of investment.<br />'; }
            if (averageInvestment === 'Select duration...') { errors += '* Please select your average investment.<br />'; }

            if (errors.length > 0) { $('.registerForm .status').html(errors); return; }


            var experience = '';

            if (experienceShares) { experience += 'Shares, '; }
            if (experienceCommodities) { experience += 'Commodities, '; }
            if (experienceAlternative) { experience += 'Alternative, '; }

            if (experience.length > 0) { experience = experience.substring(0, experience.length - 2); }


            $('.registerForm .status').html('');


            TVI.ajax({

                data: '{ \"d\": {' +
                '\"firstname\": \"' + firstname + '\",' +
                '\"lastname\": \"' + lastname + '\",' +
                '\"telephone\": \"' + telephone + '\",' +
                '\"mobile\": \"' + mobile + '\",' +
                '\"address\": \"' + address + '\",' +
                '\"email\": \"' + email + '\",' +
                '\"password\": \"' + password + '\",' +
                '\"experience\": \"' + experience + '\",' +
                '\"investmentLevel\": \"' + investmentLevel + '\",' +
                '\"averageInvestment\": \"' + averageInvestment + '\"' +
                '} }',

                url: 'Handlers/ALL.aspx/register',

                success: function () {

                    $('.registerForm').hide();
                    $('#thankyou').show();

                },

                failure: function (d) {

                    $('.registerForm .status').html(d.error);

                }

            });

        };

        var infopack = function () {

            var firstname = $('#txt_firstname').val();
            var lastname = $('#txt_lastname').val();
            var telephone = $('#txt_telephone').val();
            var address = $('#txt_address').val();
            var email = $('#txt_email').val();
            var experienceShares = $('#cb_experienceShares:checked').val();
            var experienceCommodities = $('#cb_experienceCommodities:checked').val();
            var experienceAlternative = $('#cb_experienceAlternative:checked').val();
            var investmentLevel = $('#ddl_investmentLevel OPTION:selected').val();
            var averageInvestment = $('#ddl_averageInvestment OPTION:selected').val();


            var errors = '';

            if (firstname === '') { errors += '* Please enter your first name.<br />'; }
            if (lastname === '') { errors += '* Please enter your last name.<br />'; }
            if (telephone === '') { errors += '* Please enter your telephone number.<br />'; }
            if (address == '') { errors += '* Please enter your address.<br />'; }

            if (errors.length > 0) { $('.infopackForm .status').html(errors); return; }


            var experience = '';

            if (experienceShares) { experience += 'Shares, '; }
            if (experienceCommodities) { experience += 'Commodities, '; }
            if (experienceAlternative) { experience += 'Alternative, '; }

            if (experience.length > 0) { experience = experience.substring(0, experience.length - 2); }


            $('.infopackForm .status').html('');


            TVI.ajax({

                data: '{ \"d\": {' +
                '\"firstname\": \"' + firstname + '\",' +
                '\"lastname\": \"' + lastname + '\",' +
                '\"telephone\": \"' + telephone + '\",' +
                '\"address\": \"' + address + '\",' +
                '\"email\": \"' + email + '\",' +
                '\"experience\": \"' + experience + '\",' +
                '\"investmentLevel\": \"' + investmentLevel + '\",' +
                '\"averageInvestment\": \"' + averageInvestment + '\"' +
                '} }',

                url: 'Handlers/ALL.aspx/infopack',

                success: function () {

                    $('.infopackForm').hide();
                    $('#thankyou').show();

                },

                failure: function (d) {

                    $('.infopackForm .status').html(d.error);

                }

            });

        };


        var updateAccount = function () {

            var firstname = $('#txt_firstname').val();
            var lastname = $('#txt_lastname').val();
            var telephone = $('#txt_telephone').val();
            var mobile = $('#txt_mobile').val();
            var address = $('#txt_address').val();


            var errors = '';

            if (firstname === '') { errors += '* Please enter your first name.<br />'; }
            if (lastname === '') { errors += '* Please enter your last name.<br />'; }
            if (telephone === '') { errors += '* Please enter your telephone number.<br />'; }

            if (errors.length > 0) { $('.accountForm .status').html(errors); return; }


            $('.accountForm .status').html('');


            TVI.ajax({

                data: '{ \"d\": {' +
                '\"firstname\": \"' + firstname + '\",' +
                '\"lastname\": \"' + lastname + '\",' +
                '\"telephone\": \"' + telephone + '\",' +
                '\"mobile\": \"' + mobile + '\",' +
                '\"address\": \"' + address + '\"' +
                '} }',

                url: 'Handlers/ALL.aspx/updateAccount',

                success: function () {

                    $('.accountForm .status').html('Your details have been updated.<br />');

                },

                failure: function (d) {

                    $('.accountForm .status').html(d.error);

                }

            });

        };


        return {

            clientId: 0,
            contactType: null,

            init: function () {

                cmp = this;

                $(init);

            }

        }

    };

    var m = new main();
    m.init();

    return m;

} ();
