﻿
App = function() {

    /* Private */

    /* Properties */

    var cmp = {};





    /* Defaults */

    TVI.debug = true;





    /* Methods */

    var init = function() {

        /* Constructor */
        $('#topNav li a').each(function() {

            if (window.location.toString().indexOf($(this).attr('href')) > 0) {
                $(this).addClass('selected');
            }
            if (window.location.toString().indexOf('enquiries.aspx') > 0 && $(this).attr('href') == 'offices.aspx') {
               $(this).addClass('selected');
            }

        });


    };





    /* 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-image', $(this).data('background').replace("-password", ""));
                        }
                    });
                    $(this).blur(function () {
                        if ($(this).val() === '') {
                            $(this).css('background-image', $(this).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;


} ();

function pageLoad() {
    App.setFocusBlur($('.searchBox input'));
    setForm('.headerRight');
}

function resetInputs() {
    $('input[type=text]').css("background-color", "#f1f6ff");
    $('input[type=password]').css("background-color", "#f1f6ff");
}

function clear_checkboxes(myID) {
    var frm = document.forms[0];
    for (i = 0; i < frm.length; i++) {
        if (frm.elements[i].id != myID) {
            frm.elements[i].checked = false;
            $('input').next().removeClass('selected');
        }
    }
}


function setForm(formname) {

    //clear events
    $(formname).unbind();

    $(formname).live('keypress', function (e) {


        //check return key is pressed
        if (returnKeyPressed(e)) {
            console.log("return key");
            //check input is not textarea
            if (e.target.type != 'textarea') {

                //get postback arguments from button
                var postback = $(this).find("a:first").attr("href");
                if (postback != undefined) {

                    postback = postback.replace("javascript:__doPostBack('", "");
                    postback = postback.replace("','')", "");

                    //call postback
                    __doPostBack(postback, '');

                }

            }

        }

    });

}

function returnKeyPressed(e) {

    //check if event has been passed in, otherwise set it to the window event
    if (!e) e = event;

    //check return key has been pressed
    if ((e.which == 13) || (e.keyCode == 13)) {

        //check input is not textarea
        if (e.target.type != 'textarea') {

            //stop postback
            e.returnValue = false;
            if (!$.browser.mozilla) {
                event.returnValue = false;
            }

        }

        return true;

    } else {
        return false;
    }

}
