/**********************************************************************************************************************************
Modulo: popup
Empresa, autor e data..: Focustech, Gileno Oliveira em 21/11/2006
Descricao..: Funcao para abrir popups usando DIV/IFRAME
*********************************************************************************************************************************/
var arrPopups = new Array();
var popupTabIndexes = new Array();
var popupTabbableTags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME", "SELECT");	

function popupWindow(strId, strUrl, intW, intH, strIdOpener) {
    wIntH = intH - 20;
    var popupParametros = 'popupID='+strId;
	var strConcat;
	if (strUrl.indexOf("?") == -1) {
		strConcat = '?'
	} else {
		strConcat = '&';
	}
	strHtml = '<iframe style="width:100%; height:20px; margin: 0px;" frameborder="0" scrolling="no" src="../common/popup_barra.inc.php?'+popupParametros+'"></iframe>';
    strHtml += '<iframe name="" style="width:100%; height:'+wIntH+'px; margin: 0px;" frameborder="0" scrolling="no" src="../'+strUrl+strConcat+popupParametros+'"></iframe>';
    var objDiv = document.getElementById(strId);
    if (objDiv == null) {
        var objDiv = document.createElement("div");
        objDiv.id = strId;
        document.body.appendChild(objDiv);
        objDiv.className = 'popupWindow';
        objDiv.style.border = "solid 1px #0A8CA6";
        objDiv.style.backgroundColor = "#82AAFF";
        objDiv.style.width = intW+"px";
        objDiv.style.height = intH+"px";
		objDiv.style.top = popupCalculaPontoCentralH(intH)+'px';
        objDiv.style.left = popupCalculaPontoCentralW(intW)+'px';
        arrPopups[arrPopups.length] = new popupNo(strId, strIdOpener);
    } else {
        objDiv.style.display = '';
    }
    objDiv.innerHTML = strHtml;
}//function
function popupMask(strId) {
    var objDiv = document.getElementById("popupMask"+strId);
    if (objDiv == null) {
        var objDiv = document.createElement("div");
        objDiv.id = "popupMask"+strId;
        objDiv.className = 'popupMask'        
        document.body.appendChild(objDiv);
        objDiv.style.width = popupGetViewPortW()+"px";
        objDiv.style.height = popupGetViewPortH()+"px";
    } else
        objDiv.style.display = '';
}
function popupShow(strId, strUrl, intW, intH, strIdOpener) {
    popupDisableTabIndexes();
    popupMask(strId);
    popupWindow(strId, strUrl, intW, intH, strIdOpener);
}
function popupRemove(strId) {
    popupRestoreTabIndexes();
//    document.getElementById("popupMask").innerHTML = '';
//    alert("popupMask"+strId);
    document.getElementById("popupMask"+strId).style.display = 'none';
//    document.getElementById(strId).innerHTML = '';
	var myWindow = document.getElementById(strId);
    myWindow.style.display = 'none';
    myWindow.innerHTML = '';
	myWindow.parentNode.removeChild(myWindow);
}//function
function popupGetViewPortH() {
    var intHeight = 0;
    if( typeof( window.innerHeight ) == 'number' ) {
        //Non-IE
        intHeight = window.innerHeight;
    } else if( document.documentElement && document.documentElement.clientHeight ) {
        intHeight = document.documentElement.clientHeight;
    } else if( document.body && document.body.clientHeight ) {
        //IE 4 compatible
        intHeight = document.body.clientHeight;
    }
    return intHeight;
}//function
function popupGetViewPortW() {
    var intWidth = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        intWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth ) {
        intWidth = document.documentElement.clientWidth;
    } else if( document.body && document.body.clientWidth ) {
        //IE 4 compatible
        intWidth = document.body.clientWidth;
    }
    return intWidth;
}//function    
function popupCalculaPontoCentralW(intTam) {
    return Math.round((popupGetViewPortW()/2)-(intTam/2));
}
function popupCalculaPontoCentralH(intTam) {
    return Math.round((popupGetViewPortH()/2)-(intTam/2));
}
function popupDisableTabIndexes() {
	var i = 0;
	for (var j = 0; j < popupTabbableTags.length; j++) {
		var tagElements = document.getElementsByTagName(popupTabbableTags[j]);
		for (var k = 0 ; k < tagElements.length; k++) {
			popupTabIndexes[i] = tagElements[k].tabIndex;
			tagElements[k].tabIndex="-1";
			i++;
		}
	}
}

// For IE. Restore tab-indexes.
function popupRestoreTabIndexes() {
	var i = 0;
	for (var j = 0; j < popupTabbableTags.length; j++) {
		var tagElements = document.getElementsByTagName(popupTabbableTags[j]);
		for (var k = 0 ; k < tagElements.length; k++) {
			tagElements[k].tabIndex = popupTabIndexes[i];
			tagElements[k].tabEnabled = true;
			i++;
		}
	}
}
function popupNo(strId, strIdOpener) {
    this.strId = strId;
    this.strIdOpener = strIdOpener;
    
    //*** METODOS ***
    this.find = popupFindObj;
    return this;
}
function popupFindObj(strId) {
    for (i=0; i<arrPopups.length; i++) {
        if (arrPopups[i].strId == strId) {
            return arrPopups[i];
            break;
        }
    }
    return null;
}
function popupGetFrameOpener(strId) {
    objPopup = new popupNo;
    objPopup = objPopup.find(strId);
    booEncontrado = false;
    if (objPopup != null) {
        for (i=0; i<window.frames.length;i++) {
            window.frames[i].frameBorder='1';
            if (window.frames[i].name == objPopup.strIdOpener) {
                booEncontrado = true;
                break;
            }
        }
    }
    if (booEncontrado == true) {
        return window.frames[i];    
    } else {
		return null;
	}
}
