/////////////////////////////////////////////////////
// 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";
                }
            }
        }
    }
}

var espaco = 20
function PosicionaModal() {
    var modalContent = document.getElementById("modalContent");
    var modalWindow = document.getElementById("modalDialog");
    var link_fechar = document.getElementById("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 = "-24px";
    link_fechar.style.right = "-2px";
}

function ajustaModal() {
    var modalWindow = document.getElementById("modalDialog");
    var modalFundo = document.getElementById("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";
}

var modal_exists = false;

function ModalDialog(ajaxSrc, assync) {
    modal_exists = true;

    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 = '<div><a id="modal_link_fechar" href="javascript:CloseModalDialog()"><img src="/img/modal_fechar.gif" alt="Fechar" /></a></div>'+xmlHttp.responseText;
            }
        }
    }
    
    xmlHttp.open("GET", ajaxSrc, assync);
    //xmlHttp.setRequestHeader("Content-Type", "text/html; charset=ISO-8859-1");
    if (xmlHttp.overrideMimeType) {
        xmlHttp.overrideMimeType("text/html; charset=ISO-8859-1");
    }
    xmlHttp.setRequestHeader("Content-type", "text/html; charset=iso-8859-1");
    xmlHttp.send(null);
    
    document.body.appendChild(modalWindow);
    modalWindow.appendChild(modalContent);
    
    hideSelectBoxes();
    
    adEvent(window, "resize", ajustaModal);
}

function CloseModalDialog() {
    modal_exists = false;
    
    var modal = document.getElementById("modalDialog");
    var modal_bg = document.getElementById("modalBG");
    
    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);
}

/**
 * Modo crossbrowser 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 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;
    }
}