﻿function LanguageChange(dropDown, divPanelId) {
    // show other language div if required, otherwise hide it
    var isOther = dropDown.options[dropDown.selectedIndex].value == "Other";
    document.getElementById(divPanelId).style.display = isOther ? "block" : "none";
}

function CourtOrdersChange(dropDown, rowId) {
    // show court orders if required, otherwise hide it
    var hasCourtOrders = dropDown.options[dropDown.selectedIndex].value == "1";
    document.getElementById(rowId).style.display = hasCourtOrders ? "" : "none";
}

function AddressNotApplicable(checkbox, rowIds) {
    // show address if required, otherwise hide it
    var visiblity = checkbox.checked ? "none" : "";
    for (var i = 0; i < rowIds.length; i++) {
        document.getElementById(rowIds[i]).style.display = visiblity;
    }
}

function DateRangeChange(dropDown, rowIds) {
    // show custom date rows if required, otherwise hide it
    var isDateRange = dropDown.options[dropDown.selectedIndex].value == "dateRange";
    for (var i = 0; i < rowIds.length; i++) {
        document.getElementById(rowIds[i]).style.display = isDateRange ? "" : "none";
    }
}

/*This function is used for showing popups in the application.
I know - I am hard coding the behavior ID here because I need it to act on ajax load()
so there's no easy way to inject the behavior ID - this means *one popup per page allowed* which is OK for now.
*/
function showPopup() {
    var behavior = $find('childPopup');
    if (behavior) {
        behavior._popupBehavior.show();
        behavior._popupVisible = true;

        Sys.Application.remove_load(showPopup);
    }
}

function hidePopup() {
    var behavior = $find('childPopup');
    if (behavior) {
        behavior._popupBehavior.hide();
        behavior._popupVisible = false;
    }
}

// copyright 1999 Idocs, Inc. http://www.idocs.com
// Distribute this script freely but keep this notice in place
function numbersOnly(myfield, e, dec) {
    var key;
    var keychar;

    if (window.event)
        key = window.event.keyCode;
    else if (e)
        key = e.which;
    else
        return true;
    keychar = String.fromCharCode(key);

    // control keys
    if ((key == null) || (key == 0) || (key == 8) ||
    (key == 9) || (key == 13) || (key == 27))
        return true;

    // numbers
    else if ((("0123456789").indexOf(keychar) > -1))
        return true;

    // decimal point jump
    else if (dec && (keychar == ".")) {
        myfield.form.elements[dec].focus();
        return false;
    }
    else
        return false;
}

var suppressBackKeyCheck = false;
function disableBackSpaceKey(optionalInfoMessageIfPressed) {
    // Add it to the document
    document.onkeydown = document.onkeypress = function (evt) {
        if (suppressBackKeyCheck) {
            suppressBackKeyCheck = false;
            return true;
        }
        if (typeof evt == 'undefined')
            evt = window.event;
        if (evt) {
            var keyCode = evt.keyCode ? evt.keyCode : evt.charCode;
            if (keyCode == 8) {
                if (evt.preventDefault)
                    evt.preventDefault();

                if (optionalInfoMessageIfPressed)
                    alert(optionalInfoMessageIfPressed);

                return false;
            }
            else
                return true;
        }
        else
            return true;
    }
}

function enableBackSpaceKey() {
    document.onkeydown = document.onkeypress = function (evt) {
        return true;
    }
}

function enableBackSpaceKeyOnInputsAndTextAreas() {
    // Then remove it from the input and textarea elements
    var validUses = new Array();
    validUses = document.getElementsByTagName("INPUT");
    for (var i = 0; i < validUses.length; i++) {
        var object = document.getElementsByTagName("INPUT").item(i)
        object.onkeydown = object.onkeypress = function (evt) {
            suppressBackKeyCheck = true;
            return true;
        }
    }
    validUses = document.getElementsByTagName("TEXTAREA");
    for (var i = 0; i < validUses.length; i++) {
        var object = document.getElementsByTagName("TEXTAREA").item(i)
        object.onkeydown = object.onkeypress = function (evt) {
            suppressBackKeyCheck = true;
            return true;
        }
    }
}
