/////////////////////////////////////////////////////
// funções que pegam largura e altura do navegador //
/////////////////////////////////////////////////////
function getViewportHeight() {
    if (window.innerHeight!=window.undefined) return window.innerHeight;
    if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
    if (document.body) return document.body.clientHeight; 
    return window.undefined; 
}
function getViewportWidth() {
    if (window.innerWidth!=window.undefined) return window.innerWidth; 
    if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
    if (document.body) return document.body.clientWidth; 
    return window.undefined; 
}
/**
 * Função genérica para pegar objeto pelo id
 */
function byId(id) {
    var el = document.all ? document.all[id] : document.getElementById(id);
    return el;
}
/**
 * Modo Genérico de criar um objeto XMLHttpRequest.
 * Copiado do w3schools.
 */
function GetXmlHttpObject() {
    var xmlHttp=null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e){
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

function ModalDialog(ajaxSrc, w, h) {
    var modalFundo = document.createElement('div');
    modalFundo.className = "modal_fundo";
    modalFundo.style.width = getViewportWidth()+"px";
    modalFundo.style.height = getViewportHeight()+"px";
    modalFundo.setAttribute("id", "modalBG");
    document.body.appendChild(modalFundo);
    
    var modalWindow = document.createElement('div');
    modalWindow.className = "modal_window";
    modalWindow.setAttribute("id", "modalDialog");
    modalWindow.style.width = w+"px";
    modalWindow.style.height = h+"px";
    
   // modalWindow.style.top = ((getViewportHeight()-h)/2)+"px";
    //modalWindow.style.left = ((getViewportWidth()-w)/2)+"px";
	
	modalWindow.style.top = "210px";
    modalWindow.style.left = "119px";
    //modalWindow.innerHTML = "<span class=\"modal_link_fechar_container\"><a href=\"javascript:CloseModalDialog()\" class=\"modal_link_fechar\">Fechar X</a></span>";
    
    var modalContent = document.createElement('div');
    modalContent.className = "modal_content";
    modalContent.style.width = "100%";
    modalContent.style.height = "100%";
    modalContent.style.overflow = "hidden";
    
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp) {
        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState==4){
                modalContent.innerHTML = xmlHttp.responseText;
            }
        }
    }
    xmlHttp.open("GET",ajaxSrc,true);
    xmlHttp.send(null);
    
    document.body.appendChild(modalWindow);
    modalWindow.appendChild(modalContent);
}

function makePOSTRequest(http_request, url, parametersObj) {
    if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
    }

    var arrPar = new Array();
    for (prop in parametersObj) {
        arrPar.push(prop+"="+escape(parametersObj[prop]));
    }
    var parameters = arrPar.join("&");
    
    http_request.open('POST', url, true);
    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http_request.setRequestHeader("Content-length", parameters.length);
    http_request.setRequestHeader("Connection", "close");
    http_request.send(parameters);
}


function CloseModalDialog() {
    var modal = byId("modalDialog");
    var modal_bg = byId("modalBG");
    
    document.body.removeChild(modal);
    document.body.removeChild(modal_bg);
}

function Desabilita(pai, nome_tag, desabilita){
    if (desabilita == null) desabilita = true;

    var tags = pai.getElementsByTagName(nome_tag);
    for (i=0; i<tags.length; i++){
        tags[i].disabled = desabilita;
    }
}

function Mostra(el) {
    if (el.style.display == "none") {
        el.style.display = "";
    }
}

function Esconde(el) {
    el.style.display = "none"
}

/**
 * Pega o elemento pelo id e mostra se estiver escondido e esconde se estiver mostrando.
 */
function MostraEsconde(id) {
    var el = byId(id);
    
    if (el.style.display == "none") {
        el.style.display = "";
    } else {
        el.style.display = "none";
    }
}

