﻿function hideSelectElements(pstrIdContainer) {
    if (!document.all) { return; }
    hideElements(pstrIdContainer, "SELECT");
}

function showSelectElements(pstrIdContainer) {
    if (!document.all) { return; }
    showElements(pstrIdContainer, "SELECT");
}

function hideElements(pstrIdContainer, pstrTagElements) {
    handleElementsVisibility(pstrIdContainer, pstrTagElements, "hidden");
}

function showElements(pstrIdContainer, pstrTagElements) {
    handleElementsVisibility(pstrIdContainer, pstrTagElements, "visible");
}

function disableBackSpace(pobjEvent)
{
    try {
        if (pobjEvent.keyCode == 8) 
        {
            if (document.activeElement.readOnly == true) 
            {
                return false;
            }
            if (document.activeElement.tagName.toUpperCase() == "INPUT") 
            {
                if (document.activeElement.type.toUpperCase() == "TEXT" ||
                    document.activeElement.type.toUpperCase() == "PASSWORD") 
                {
                    return true;
                } 
            } 
            else if (document.activeElement.tagName.toUpperCase() == "TEXTAREA") 
            {
                return true;
            }   
            
            return false;             
        }
        else
        {
            return true;
        }
    }
    catch (e) {
    }
    
}

function handleElementsVisibility(pstrIdContainer, pstrTagElements, pstrVisibility) {
    var objContainer = document.getElementById(pstrIdContainer);
    if (!objContainer) { return; }
    var arrElements = objContainer.getElementsByTagName(pstrTagElements);
    if (arrElements.length == 0) { return; }
    for (var i = 0; i < arrElements.length; i++) {
        arrElements[i].style.visibility = pstrVisibility;
    }
}

function getSelectedValue(pstrIdContainer) {
    var objContainer = document.getElementById(pstrIdContainer);
    if (!objContainer) { return 0; }
    var arrElements = objContainer.getElementsByTagName("INPUT");
    if (arrElements.length == 0) { return 0; }
    for (var i = 0; i < arrElements.length; i++) {
        if (arrElements[i].type.toUpperCase() == "RADIO") {
            if (arrElements[i].checked) {
                return arrElements[i].value;
            }
        }
    }
    return 0;
}

function confirmUpdate() {
    return confirm(_strMessageConfirmUpdate);
}

var _ctlPositioner = null;

function loadScrollPosition() {
    if (!_ctlPositioner) {
        var arrInputs = document.getElementsByTagName("INPUT");
        for (var i = 0; i < arrInputs.length; i++) {
            if (arrInputs[i].type.toUpperCase() == "HIDDEN" &&
                arrInputs[i].name.indexOf("hdnPosicaoTela") >= 0) {
                _ctlPositioner = arrInputs[i];
                break;
            }
        }
    }
    
    if (!_ctlPositioner || _ctlPositioner.value.length == 0) { return; }
    
    var divForm = document.getElementById("divConteudo").firstChild;
    
    try {
        divForm.scrollTop = _ctlPositioner.value;
    } catch (e) { }
}

function storeScrollPosition(pobjDiv) {
    try {
        if (!_ctlPositioner) { return; }
        _ctlPositioner.value = pobjDiv.firstChild.scrollTop.toString();
    } catch (e) { }
}

function AbrirNovaJanela(pstrUrl,pstrNome,pstrLargura,pstrAltura)
{
    window.open(pstrUrl, pstrNome, 'width=' + (pstrLargura - 20) + ',height=' + pstrAltura + ',resizable=no');  
    return false;
}

function GetHttpXmlRequest() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    }
    
    if (window.ActiveXObject) {
        try {
            return new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                return new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                return false;
            }
        }
    }
}

function ExecuteCallBack(p_blnPost, p_strURL, p_strParams, p_objSuccessFunction, p_objProcessingFunction, p_objErrorFunction) {

    if (p_strParams == null || p_strParams.length == 0) {
        p_strParams = "0";
    }

    var objWS = GetHttpXmlRequest();
    objWS.open((p_blnPost ? "POST" : "GET"), p_strURL, true);
    objWS.setRequestHeader("Pragma", "no-cache");
    
    if (p_blnPost) {
        objWS.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        objWS.setRequestHeader("Content-Length", p_strParams.toString().length);
    } else {
        objWS.setRequestHeader("Content-Type", "text/xml"); 
    }
    
    objWS.onreadystatechange = function() {
        if (p_objProcessingFunction) {
            p_objProcessingFunction(objWS.readyState);
        }

        if (objWS.readyState == 4) {
            if (objWS.status == 200) {
                if (p_objSuccessFunction) {
                    p_objSuccessFunction(objWS.responseXML);
                }
            } else {
                if (p_objErrorFunction) {
                    p_objErrorFunction(objWS.status);
                }
            }
        }
    };

    objWS.send(p_strParams.toString());
}

function RecuperarTextoNode(p_objNode) {
    if (window.ActiveXObject) {
        return p_objNode.text;
    } else {
        return p_objNode.textContent;
    }
}