// JavaScript Document

prm.add_pageLoaded(setupPage);
prm.add_pageLoaded(setFocus);

function setFocus() {

    if (typeof(tb) != "undefined" && tb != null && tb.focus) {
        
        tb.focus();
    }
}

function setupPage() {

    $('a.fieldHelp').cluetip({
        'sticky': true
        , 'closePosition': 'title'
        , 'cluetipClass': 'jtip'
    });

    $('.autoFieldHelp').cluetip({
        'mouseOutClose': true
        , 'activation': 'focus'
        , 'cluetipClass': 'jtip'
    });

    $('.autoFieldClickHelp').cluetip({
        'positionBy': 'auto'
        , 'sticky': true
        , 'closePosition': 'title'
        , 'activation': 'click'
        , 'cluetipClass': 'jtip'
    });

    $('a.fieldHelpNoLink').cluetip({
        'sticky': true
        , 'mouseOutClose': true
        , 'closePosition': 'title'
        , 'splitTitle': '|'
        , 'cluetipClass': 'jtip'
    });

    $("div.fieldReadOnlyArea").bind("click", function(e) {
        this.style.display = 'none';
        var $parent = $(this).parent();
        $parent.find('div.fieldEditArea').each(function(i) {
            this.style.display = 'inline';
        });
        $parent.find('input[@type=text], textarea').each(function(i) {
            if (i == 0) this.select();
        });
    });

//    $("input, textarea, select").focus(function() {
//        $('div.label').removeClass("curFocus");
//        $(this).parents('div.label').addClass("curFocus");
//    });

//    $("input, textarea, select").blur(function() {
//        // remove the focus rectangle
//        $(this).parents('div.label').removeClass("curFocus");
//    });

//    $("input[@type=radio]").click(function() {
//        $('div.label').removeClass("curFocus");
//        $('input[@type=radio]').parent('div.label').addClass("curFocus");
//    });

//    $("input[@type=checkbox]").click(function() {
//        $('div.label').removeClass("curFocus");
//        $(this).parents('div.label').addClass("curFocus");
//    });

    setFocus();
}

function autoTab(element, nextElement) {
    if (element.value.length == element.maxLength && nextElement != null) {
        document.getElementById(nextElement).focus();
        document.getElementById(nextElement).select();
    }
}

function numbersOnly(e) {
    var unicode = e.charCode ? e.charCode : e.keyCode
    if (unicode != 8) { //if the key isn't the backspace key (which we should allow)
        if (unicode < 48 || unicode > 57) //if not a number
            return false //disable key press
    }
}

// Shows a cluetip when a specific value is selected in a drop down.
// The cluetip is shown by triggering the click event on a hidden input element.
function select_onchange(dropDownID, placeHolderID, matchingValue) {

    var elem = document.getElementById(dropDownID);
    var ph = document.getElementById(placeHolderID);
    if (ph && elem) {
        var dropdownIndex = elem.selectedIndex;
        var dropdownValue = elem[dropdownIndex].value
        if (dropdownValue == matchingValue)
            ph.click();
    }

    return true;
}

function changeValidationGroup(button, validation, id) 
{
    try {

        var btn = document.getElementById(button);
        btn.onclick = WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(id, "", true, validation, "", false, false));
        btn.click();
    }
    catch (err) 
    {
        //alert(err.description);
    }
}

function ShowPanel(panel) 
{
    var pnl = document.getElementById(panel);
    pnl.style.display = "block";
}

function revealModal(divID) 
{
    window.onscroll = function() { document.getElementById(divID).style.top = document.body.scrollTop; };
    document.getElementById(divID).style.display = "block";
    document.getElementById(divID).style.top = document.body.scrollTop;
}

function hideModal(divID) 
{
    document.getElementById(divID).style.display = "none";
}
