/////////////////////////////////////////////////////
// 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; 
}

function getScrOfY() {
    var scrOfY = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        scrOfY = window.pageYOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        scrOfY = document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        scrOfY = document.documentElement.scrollTop;
    }
    return scrOfY;
}

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

//////////////////////////////////////////////////////////////////////////
// funções que escondem os selects se for Internet Explorer abaixo de 6 //
//////////////////////////////////////////////////////////////////////////
/* Hides all drop down form select boxes on the screen so they do not appear above the mask layer.
* IE has a problem with wanted select form tags to always be the topmost z-index or layer
*
* Thanks for the code Scott!
*/
function hideSelectBoxes() {
    var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
    if (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1) {
        for(var i = 0; i < document.forms.length; i++) {
            for(var e = 0; e < document.forms[i].length; e++){
                if(document.forms[i].elements[e].tagName == "SELECT") {
                    document.forms[i].elements[e].style.visibility="hidden";
                }
            }
        }
    }
}

/**
* Makes all drop down form select boxes on the screen visible so they do not reappear after the dialog is closed.
* IE has a problem with wanted select form tags to always be the topmost z-index or layer
*/
function displaySelectBoxes() {
    var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
    if (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1) {
        for(var i = 0; i < document.forms.length; i++) {
            for(var e = 0; e < document.forms[i].length; e++){
                if(document.forms[i].elements[e].tagName == "SELECT") {
                document.forms[i].elements[e].style.visibility="visible";
                }
            }
        }
    }
}

/**
 * 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;
}


var espaco = 20
function PosicionaModal() {
    var modalContent = byId("modalContent");
    var modalWindow = byId("modalDialog");
    var link_fechar = byId("modal_link_fechar");
    var off = 0;

    var new_left = ((getViewportWidth()-(modalContent.clientWidth))/2);
    if(new_left < espaco) new_left = espaco;
	

	off = getScrOfY();
	modalWindow.style.top = off + "px";

	
    modalWindow.style.top = off + "px";
    modalContent.style.top = "50px";
    modalContent.style.left = new_left+"px";
    link_fechar.style.top = "-15px";
}

function ajustaModal() {
    var modalWindow = byId("modalDialog");
    var modalFundo = byId("modalBG");
    
    modalFundo.style.width = document.body.clientWidth+"px";
    modalFundo.style.height = (getViewportHeight()>(document.body.clientHeight+5)?getViewportHeight() : (document.body.clientHeight+5)) +"px";
    
    modalWindow.style.width = getViewportWidth()+"px";
    modalWindow.style.height = getViewportHeight()+"px";
}

function ModalDialog(ajaxSrc, legenda) {
    document.body.style.overflow = "hidden";
    document.documentElement.style.overflow = "hidden"; 

    var modalFundo = document.createElement('div');
    modalFundo.className = "modal_fundo";
    modalFundo.setAttribute("id", "modalBG");
    modalFundo.style.top = "0";
    document.body.appendChild(modalFundo);
    modalFundo.style.width = document.body.clientWidth+"px";
    modalFundo.style.height = (getViewportHeight()>(document.body.clientHeight+5)?getViewportHeight() : (document.body.clientHeight+5)) +"px";
    
    var modalWindow = document.createElement('div');
    modalWindow.className = "modal_window";
    modalWindow.setAttribute("id", "modalDialog");
    modalWindow.style.top = 0;
    modalWindow.style.left = 0;
    modalWindow.style.height = getViewportHeight()+"px";
    modalWindow.style.width = getViewportWidth()+"px";
    modalWindow.style.overflow = "auto";
    
    //*** Descomentar para o efeito de Appear
    //modalWindow.style.visibility = "hidden";
    
    var modalContent = document.createElement('div');
    modalContent.className = "modal_content";
    modalContent.setAttribute("id", "modalContent");
    modalContent.style.top = "-100000px";
    
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp) {
        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState==4){
                modalContent.innerHTML = xmlHttp.responseText;
            }
        }
    }
    if (legenda) {
        xmlHttp.open("POST",ajaxSrc,true);
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttp.setRequestHeader("Content-length", legenda.length);
        xmlHttp.setRequestHeader("Connection", "close");
        xmlHttp.send(legenda);
    } else {
        xmlHttp.open("GET",ajaxSrc,true);
        xmlHttp.send(null);
    }
    
    document.body.appendChild(modalWindow);
    modalWindow.appendChild(modalContent);
    
    hideSelectBoxes();
    
    adEvent(window, "resize", ajustaModal);
}

function CloseModalDialog() {
    var modal = byId("modalDialog");
    var modal_bg = byId("modalBG");
    
    Effect.Fade("modalDialog");
    
    document.body.removeChild(modal);
    document.body.removeChild(modal_bg);
    document.body.style.height = "";
    document.body.style.overflow = "";
    document.documentElement.style.overflow = ""; 
    
    displaySelectBoxes();
    rmEvent(window, "resize", ajustaModal);
}

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 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";
    }
}

function adEvent(obj, evType, fn){
    if (obj.addEventListener){
        obj.addEventListener(evType, fn, false);
        return true;
    } else if (obj.attachEvent){
        var r = obj.attachEvent("on"+evType, fn);
        return r;
    } else {
        return false;
    }
}

function rmEvent(obj, evType, fn){
    if (obj.removeEventListener){
        obj.removeEventListener(evType, fn, false);
        return true;
    } else if (obj.detachEvent){
        var r = obj.detachEvent("on"+evType, fn);
        return r;
    } else {
        return false;
    }
}

/** Troca o marcador do menu Tacosola **/
function trocaMarcador (idMenu) {
    var marcador = document.getElementById(idMenu);
    if (marcador.style.display == 'none') {
        marcador.style.display = '';
    } else {
        marcador.style.display = 'none';
    }
}