﻿/// <summary>
/// Restricts the input length of a textbox, providing an optional visual indicator of the number of characters remaining
/// </summary>
function limitTextboxLength(elem, length, counter, short) {
    if (typeof elem == "string") {
        elem = document.getElementById(elem);
    }
    if (typeof counter == "string") {
        counter = document.getElementById(counter);
    }
    if (counter != null) {
        counter.display = function () {
            var l = elem.value.length;
            if (short) {
                counter.innerHTML = (length - l);
            }
            else {
                counter.innerHTML = (length - l) + " characters remaining";
            }
            if (l <= length) {
                counter.style.color = "#000";
            }
            else {
                counter.style.color = "#F00";
            }
        }
    }
    if (elem != null) {
        elem.onkeypress = function (evt) {
            evt = (evt) ? evt : event;
            var c = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
            if (counter != null) {
                counter.display();
            }
            if (c == 8 || c == 16 || (c >= 35 && c <= 40) || c == 46) {
                return true;
            }
            return elem.value.length < length;
        }
        if (counter != null) {
            elem.onkeyup = function (evt) {
                if (elem.value.length > length) {
                    elem.value = elem.value.substr(0, length);
                }
                counter.display();
            }
            counter.display();
        }
    }
}

/// <summary>
/// Toggles display of one element based on the state of another, usually a dropdown box
/// </summary>
function showHideElemOnChange(sourceElem, targetElem, showVal, invert) {
    if (typeof sourceElem == "string") {
        sourceElem = document.getElementById(sourceElem);
    }
    if (typeof targetElem == "string") {
        targetElem = document.getElementById(targetElem);
    }
    if (typeof showVal == "string") {
        showVal = [showVal];
    }
    if (sourceElem != null && targetElem != null) {
        var oldOnChange = sourceElem.onchange;
        sourceElem.onchange = function () {
            if (oldOnChange != null) {
                oldOnChange();
            }
            var display = false;
            for (var i = 0; i < showVal.length; i++) {
                if (invert) {
                    if (sourceElem.value != showVal[i]) {
                        display = true;
                    }
                }
                else {
                    if (sourceElem.value == showVal[i]) {
                        display = true;
                    }
                }
            }
            if (display == true) {
                targetElem.style.display = "";
            }
            else {
                targetElem.style.display = "none";
            }
        };
        sourceElem.onchange();
    }
}
