﻿// Default text in search box (from ToolTip) + change searchbox content on focus and blur
//jQuery(document).ready(function()
//{
//    jQuery(".mainsearchbox > .searchbox").focus(function(srcc)
//    {
//        if (jQuery(this).val() == jQuery(this)[0].title)
//        {
//            //jQuery(this).removeClass("defaultTextActive");
//            jQuery(this).val("");
//        }
//    });
//    
//    jQuery(".mainsearchbox > .searchbox").blur(function()
//    {
//        if (jQuery(this).val() == "")
//        {
//            //jQuery(this).addClass("defaultTextActive");
//            jQuery(this).val(jQuery(this)[0].title);
//        }
//    });
//    
//    jQuery(".mainsearchbox > .searchbox").blur();        
//});
// ---------------------------------------------------

// Create print-sized new window
jQuery(document).ready(function () {
    jQuery(".printlink").click(function () {
        window.open(this.href, "print", "status=1,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,width=650,height=600");
        return false;
    });
    jQuery(".printlink").keypress(function () {
        if (event.keyCode == 13) onclick(); else return true;
    })
});
// ---------------------------------------------------

//Make links with attributes rel="external" open in new window (target="blank" - alternative for XHTML 1.0 strict compliance)
jQuery(document).ready(function () {
    jQuery('a[rel|=external]').attr('target', '_blank');
});

// ---------------------------------------------------

jQuery(document).ready(function () {
    jQuery('.yeardropdownlist').each(function () {
        initSelect(jQuery(this).attr('id'));
    });
});

function SetFocusToOrderLine(containerId)
{
    jQuery('#' + containerId + ' > input').focus();
} 

function initSelect(selectId) {

    var theSelect = jQuery('#' + selectId);

    theSelect.changed = false;

    theSelect.focus(function () { selectFocussed(theSelect) });

    theSelect.keyup(function (e) {

        var theEvent;
        var keyCodeTab = "9";
        var keyCodeEnter = "13";
        var keyCodeEsc = "27";
        if (e) {
            theEvent = e;
        }
        else {
            theEvent = event;
        }

        //        Tabs wont create postbacks anymore
        if (theEvent.keyCode == keyCodeEnter && theSelect.val() != theSelect.initValue) {
            this.changed = true;
            __doPostBack(selectId, '');
        }
        else if (theEvent.keyCode == keyCodeEsc) {
            this.value = this.initValue;
        }
        else {
            this.changed = false;
        }

        return true;
    });

    theSelect.click(function () {
        this.changed = true;

        if (theSelect.val() != theSelect.initValue) {
            __doPostBack(selectId, '');
        }
    });

    theSelect.change(function () {
        this.changed = true;

        if (theSelect.val() != theSelect.initValue) {
            __doPostBack(selectId, '');
        }
    });

    return true;
}

function selectFocussed(element) {
    element.initValue = element.val();
    return true;
}

// -------------------------------------
function initRedirSelect(selectId) {
    var theSelect = jQuery('#' + selectId);

    theSelect.changed = false;

    theSelect.focus(function () { selectFocussed(theSelect) });

    theSelect.change(function () {
        this.changed = true;

        if (theSelect.val() != theSelect.initValue) {
            document.location = jQuery(theSelect).val();
        }
    });

    theSelect.keyup(function (e) {

        var theEvent;
        var keyCodeTab = "9";
        var keyCodeEnter = "13";
        var keyCodeEsc = "27";
        if (e) {
            theEvent = e;
        }
        else {
            theEvent = event;
        }

        //        Tabs wont create postbacks anymore
        if (theEvent.keyCode == keyCodeEnter && theSelect.val() != theSelect.initValue) {
            this.changed = true;
            document.location = jQuery(theSelect).val();
        }
        else if (theEvent.keyCode == keyCodeEsc) {
            this.value = this.initValue;
        }
        else {
            this.changed = false;
        }

        return true;
    });

    theSelect.click(function () {
        this.changed = true;

        if (theSelect.val() != theSelect.initValue) {
            document.location = jQuery(theSelect).val();
        }
    });

    return true;
}

function selectRedirChanged(theElement) {
    var theSelect;

    if (theElement && theElement.value) {
        theSelect = theElement;
    }
    else {
        theSelect = this;
    }

    if (!theSelect.changed) {
        return false;
    }

    document.location = jQuery(theSelect).val();

    return true;
}

function selectRedirClicked() {
    this.changed = true;
}

function selectRedirFocussed(element) {
    element.initValue = element.val();
    return true;
}

function selectRedirKeyed(e) {
    var theEvent;
    var keyCodeTab = "9";
    var keyCodeEnter = "13";
    var keyCodeEsc = "27";

    if (e) {
        theEvent = e;
    }
    else {
        theEvent = event;
    }

    if (theEvent.keyCode == keyCodeEnter && theSelect.val() != theSelect.initValue) {
        this.changed = true;
        selectRedirChanged(this);
    }
    else if (theEvent.keyCode == keyCodeEsc) {
        this.value = this.initValue;
    }
    else {
        this.changed = false;
    }

    return true;
}
// ---------------------------------------------------
