﻿COB = function() {

    /* Private */

    /* Properties */

    var cmp = {};





    /* Methods */

    var init = function() {

        TVI.debug = true;

        TVI.Forms.handlerURL = '/handlers/';


        // IE6 (temp)
        if ($('BODY').hasClass('.browserIE6')) {

            $('HEAD').prepend('<link rel="stylesheet" href="css/ie6.css" type="text/css" media="screen"  />');

        }

        // POPUP

        // Open Quote popup
        $('A.sendYourQuote').click(function() {
            $('#quotePopup').fadeIn('fast');

            return false;
        });

        // Open Brochure popup
        $('A.sendBrochure').click(function() {
            $('#brochurePopup').fadeIn('fast');

            return false;
        });

        // Height for popup
        var totalHeight = $(document).height();
        $('.popupWrapper').css('height', totalHeight);

        // Close popup
        $('.popup .close').click(function() {
            $(this).parents('.popupWrapper').fadeOut('fast');

            return false;
        });

        $('a.logout').live('click', function() {
            $.ajax({
                type: "POST",
                url: '/handlers/app.aspx/logOut',
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(data) {
                    document.cookie = "tradeLogin" + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
                    location.reload(true);
                }

            });

            return false;

        });

        $('a.login').click(function() {
            var JSONData = "{'email': '" + $('#txt_loginEmail').val() + "', 'password': '" + hex_md5($('#txt_loginPassword').val()) + "'}";
            $.ajax({
                type: "POST",
                url: '/handlers/app.aspx/logIn',
                data: JSONData,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(data) {
                    result = JSON.parse(data.d);
                    if (result.success) {
                        location.reload(true);
                    }
                    else {
                        if (result.errorMessage != "Server Error") {
                            $('#loginError').html("<span class=\"error\">" + result.errorMessage + "</span>").show();

                        }
                        else {
                            $('#loginError').html("<span class=\"error\">There was an error logging you in, please try again later</span>").show();
                        }
                    }

                },
                failure: function(data) {
                    $('#loginError').html("<span class=\"error\">There was an error logging you in, please try again later</span>").show();
                }
            });
            return false;

        });

        $('a.forgottenPassword').click(function() {
            
            if ($('#txt_loginEmail').val() == "" || $('#txt_loginEmail').val() == "Email Address") {
                $('#loginError').html("<span class=\"error\">Enter your email address in the box above and click forgotten password again to send a reminder</span>").show();
                return false;
            }

            var JSONData = "{'email': '" + $('#txt_loginEmail').val() + "'}";

            $.ajax({
                type: "POST",
                url: '/handlers/app.aspx/passwordReminder',
                data: JSONData,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(data) {
                    result = JSON.parse(data.d);
                    if (result.success) {
                        $('#loginError').html("<span class=\"error\">Thank You, your reminder has been sent</span>").show();
                    }
                    else {
                        $('#loginError').html("<span class=\"error\">" + result.errorMessage + "</span>").show();
                    }

                },
                failure: function(data) {
                    $('#loginError').html("<span class=\"error\">There was an error, please try again later</span>").show();
                }
            });
            return false;

        });


        COB.setFocusBlur($('input'));

        cmp.brochureForm = new TVI.Form({

            ID: 'brochureForm',
            errorsEl: '.status',
            handler: sendBrochureForm

        });
        cmp.quoteForm = new TVI.Form({

            ID: 'quoteForm',
            errorsEl: '.status',
            handler: sendQuoteForm

        });

        cmp.newsletterForm = new TVI.Form({

            ID: 'newsletterForm',
            errorsEl: '.status',
            handler: newsletterSignup

        });
        TVI.addListener('quoteForm-fileUpload.onUpload', function(d) {

            $('#quoteForm-fileUpload input[type=text]').val(d.filename);

        });

    };


    var sendBrochureForm = function(valid) {
        if (valid !== true) {

            cmp.brochureForm.validate({

                success: function() {

                    cmp.brochureForm.submit({

                        url: '/handlers/app.aspx/sendContact',
                        data: {
                            email: 'brochurerequest@coblandsnurseries.com'
                        },
                        success: function() {

                            cmp.brochureForm.clear();

                            cmp.brochureForm.error('Thank you, your brochure will arrive within 5 working days<br /><br />');

                        }

                    });

                },
                failure: function(d) {

                    cmp.brochureForm.error(d.errors);

                }

            });

            return;

        }


    };

    var sendQuoteForm = function(valid) {
        if (valid !== true) {

            cmp.quoteForm.validate({

                success: function() {

                    cmp.quoteForm.submit({

                        url: '/handlers/app.aspx/sendContact',
                        success: function() {

                            cmp.quoteForm.clear();

                            cmp.quoteForm.error('Thank you, we endeavour to return your completed quote within 48 hours<br /><br />');

                        }

                    });

                },
                failure: function(d) {

                    cmp.quoteForm.error(d.errors);

                }

            });

            return;

        }


    };

    var newsletterSignup = function(valid) {
        if (valid !== true) {

            cmp.newsletterForm.validate({

                success: function() {

                    cmp.newsletterForm.submit({

                        url: '/handlers/app.aspx/newsletterSignup',
                        success: function() {

                            cmp.newsletterForm.clear();

                            cmp.newsletterForm.error('Thank you, your email address has been added.<br /><br />');

                        }

                    });

                },
                failure: function(d) {

                    cmp.newsletterForm.error(d.errors);

                }

            });

            return;

        }


    };

    /* Public */

    TVI.apply(cmp, {

        /* Properties */

        setFocusBlur: function(inputs) {

            // Function to set focus-blur on textboxes
            inputs.each(function() {

                // For password boxes remove the background on focus and restore it on blur if its empty
                if ($(this).attr('type') === 'password') {

                    $(this).focus(function() {
                        if ($(this).val() === '') {
                            $(this).data('background', $(this).css('background-image'));
                            $(this).css('background', '#DCDCD5 url(/i/textbox_bg.gif) repeat-x scroll 0 0');
                        }
                    });
                    $(this).blur(function() {
                        if ($(this).val() === '') {
                            $(this).parent().css('background-image', $(this).parent().data('background'));
                        }
                    });
                }
                else {
                    // For normal
                    $(this).data('original', $(this).val());

                    $(this).focus(function() {
                        if ($(this).val() === $(this).data('original')) {
                            $(this).val('');
                        }
                    });

                    $(this).blur(function() {
                        if ($(this).val() === '') {
                            $(this).val($(this).data('original'));
                        }
                    });
                }
            });

        }

    });


    TVI.ready(init);


    return cmp;


} ();

