﻿function ShowControl(controlId) {
    SetControlDisplayStyle(controlId, "block");
}

function HideControl(controlId) {
    SetControlDisplayStyle(controlId, "none");
}

function SetControlDisplayStyle(controlId, displayStyle) {
    var control = document.getElementById(controlId);
    control.style.display = displayStyle;
}

function GetControlHeight(id) {

    var curHeight = document.getElementById(id).style.height;
    var pxidx = curHeight.indexOf("px");
    if (pxidx > 0)
        curHeight = curHeight.substring(0, pxidx);
    return curHeight;
}


function ShowControlSmooth(id, curHeight, reqHeight) {
    // to use this function your control should have style="overflow:hidden"
    if (curHeight < 0)
        curHeight = GetControlHeight(id);

    curHeight = curHeight + 20;

    if (curHeight > reqHeight) {
        curHeight = reqHeight;
    }

    document.getElementById(id).style.height = curHeight + 'px';

    if (curHeight < reqHeight) {
        setTimeout("ShowControlSmooth('" + id + "'," + curHeight + "," + reqHeight + ")", 16);
    }
}

function HideControlSmooth(id, curHeight) {
    // to use this function your control should have style="overflow:hidden"
    if (curHeight < 0)
        curHeight = GetControlHeight(id);

    curHeight = curHeight - 20;

    if (curHeight < 0) {
        curHeight = 0;
    }

    document.getElementById(id).style.height = curHeight + 'px';

    if (curHeight > 0) {
        setTimeout("HideControlSmooth('" + id + "'," + curHeight + ")", 16);
    }
}

function WireEvent(obj, evTp, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(evTp, fn, false);
        return true;
    } else if (obj.attachEvent) {
        var rslt = obj.attachEvent('on' + evTp, fn);
        return rslt;
    }
    else return false;
}

function UnwireEvent(obj, evTp, fn) {
    if (obj.removeEventListener) {
        obj.removeEventListener(evTp, fn, false);
        return true;
    } else if (obj.detachEvent) {
        var rslt = obj.detachEvent('on' + evTp, fn);
        return rslt;
    }
    else return false;
}

function ClearFileInputField(elementId) {
    document.getElementById(elementId).innerHTML = document.getElementById(elementId).innerHTML;
}

function EscapeTextForHTMLForControl(control) {
    if (control == null) return;
    control.value = EscapeTextForHTML(control.value);
}

function EscapeTextForHTML(text) {
    return text.toString().replace(/&/g, "&amp;").replace(/'/g, "\u0027").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}
