/*
 * CODIGO POPUP WINDOW v1.3
 */

var src = "Popups/";
var massage;

function showPopWin(url, width, height, returnFunc, msm) {
	massage = msm;
	initPopUp();
	gPopupIsShown = true;
	disableTabIndexes();
	gPopupMask.style.display = "block";
	gPopupContainer.style.display = "block";
	// calcular dónde colocar la ventana en la pantalla
	centerPopWin(width, height);
	
	var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
	
	gPopupContainer.style.width = width + "px";
	gPopupContainer.style.height = (height+titleBarHeight) + "px";
	// necesidad de establecer la anchura del iframe a la barra de título, porque el ancho de la dropshadow
	gPopFrame.style.width = parseInt(document.getElementById("popupTitleBar").offsetWidth, 10) + "px";
	gPopFrame.style.height = (height) + "px";
	
	// cargar la nueva URL
	gPopFrame.src = url;
	gReturnFunc = returnFunc;

	// para IE
	if (gHideSelects == true) {
		hideSelectBoxes();
	}
	if(gHideFlashDivs == true){
		hideFlashDivs();
	}
    var oInp = document.getElementsByTagName('select');
    for(var i=0;i<oInp.length;i++)
            oInp[i].style.display="none";
	
	window.setTimeout("setPopTitle();", 600);
}

// CODIGO para el POPUP
var gPopupMask = null;
var gPopupContainer = null;
var gPopFrame = null;
var gReturnFunc;
var gPopupIsShown = false;
var gHideSelects = false;
var gHideFlashDivs = true;
var gTabIndexes = new Array();
// Pre-definidos lista de las etiquetas que deseas inhabilitar / habilitar en tabbing
var gTabbableTags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");	

// Si utiliza Mozilla o Firefox, el uso de ficha-clave trampa..
if (!document.all) {
	document.onkeypress = keyDownHandler;
}

function initPopUp() {
	// Agregar al HTML en la etiqueta Body, necesita tener BODY para no marcar ERROR
	theBody = document.getElementsByTagName('BODY')[0];
	popmask = document.createElement('div');
	popmask.id = 'popupMask';
	popcont = document.createElement('div');
	popcont.id = 'popupContainer';
	popcont.innerHTML = '' +
		'<div id="popupInner" style="border:navy 2px solid">' +
			'<div id="popupTitleBar" >' +
				'<div id="popupTitle" >&nbsp; Datateam Consulting<br/>'+ massage +'</div>' +
				'<div id="popupControls" >' +
					'<img src="'+src+'/close2.gif" onclick="hidePopWin(false);" />' +
				'</div>' +
			'</div>' +
			'<iframe src="'+src+'loading.gif" style="width:100%;height:100%;background-color:transparent;" scrolling="auto" frameborder="0" allowtransparency="true" id="popupFrame" name="popupFrame" width="100%" height="100%"></iframe>' +
		'</div>';
	theBody.appendChild(popmask);
	theBody.appendChild(popcont);
	
	gPopupMask = document.getElementById("popupMask");
	gPopupContainer = document.getElementById("popupContainer");
	gPopFrame = document.getElementById("popupFrame");	
	
	// comprobar si se trata de la versión 6 de IE o inferior. ocultar seleccione las casillas en caso afirmativo
	var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
	if (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1) {
		gHideSelects = true;
	}
	
	// Añadir a manipuladores onclick 'a' elementos de la clase submodal o submodal de ancho-altura
	var elms = document.getElementsByTagName('a');
	for (i = 0; i < elms.length; i++) {
		if (elms[i].className.indexOf("submodal") == 0) { 
			//var onclick = 'function (){showPopWin(\''+elms[i].href+'\','+width+', '+height+', null);return false;};';
			//elms[i].onclick = eval(onclick);
			elms[i].onclick = function(){
				// el valor default de ancho y alto
				var width = 400;
				var height = 200;
				// Analiza a cabo opcional anchura y la altura del className
				params = this.className.split('-');
				if (params.length == 3) {
					width = parseInt(params[1]);
					height = parseInt(params[2]);
				}
				showPopWin(this.href,width,height,null); return false;
			}
		}
	}
}
//addEvent(window, "load", initPopUp);

var gi = 0;
function centerPopWin(width, height) {
	if (gPopupIsShown == true) {
		if (width == null || isNaN(width)) {
			width = gPopupContainer.offsetWidth;
		}
		if (height == null) {
			height = gPopupContainer.offsetHeight;
		}
		
		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();
		
		//var theBody = document.documentElement;
		var theBody = document.getElementsByTagName("BODY")[0];
		theBody.style.overflow = "hidden";
		
		var scTop = parseInt(theBody.scrollTop,10);
		var scLeft = parseInt(theBody.scrollLeft,10);
		
		gPopupMask.style.top = scTop + "px";
		gPopupMask.style.left = scLeft + "px";
		
		gPopupMask.style.height = theBody.scrollHeight + "px";
		gPopupMask.style.width = theBody.scrollWidth + "px";
		
		//window.status = gPopupMask.style.top + " " + gPopupMask.style.left + " " + gi++;
		
		var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
		
		gPopupContainer.style.top = (scTop + ((fullHeight - (height+titleBarHeight)) / 2)) + "px";
		gPopupContainer.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";
		//alert(fullWidth + " " + width + " " + gPopupContainer.style.left);
	}
}
addEvent(window, "resize", centerPopWin);
//addEvent(window, "scroll", centerPopWin);
window.onscroll = centerPopWin;

/**
 * @argument callReturnFunc - bool - determines if we call the return function specified
 * @argument returnVal - anything - return value 
 */

function hidePopWin(callReturnFunc) {
	gPopupIsShown = false;
	var theBody = document.getElementsByTagName("BODY")[0];
	theBody.style.overflow = "";
	restoreTabIndexes();
	if (gPopupMask == null) {
		return;
	}
	gPopupMask.style.display = "none";
	gPopupContainer.style.display = "none";
	if (callReturnFunc == true && gReturnFunc != null) {
		gReturnFunc(window.frames["popupFrame"].returnVal);
	}
	gPopFrame.src = src + '/loading.html';
	// Mostrar los Objetos
	if (gHideSelects == true) {
		displaySelectBoxes();
	}
	
	if(gHideFlashDivs == true){
		showFlashDivs();
	}
    var oInp = document.getElementsByTagName('select');
    for(var i=0;i<oInp.length;i++)
            oInp[i].style.display="block";

}

/**
 * Sets the popup title based on the title of the html document it contains.
 * Uses a timeout to keep checking until the title is valid.
 */
function setPopTitle() {
	return;
	if (window.frames["popupFrame"].document.title == null) {
		window.setTimeout("setPopTitle();", 10);
	} else {
		document.getElementById("popupTitle").innerHTML = window.frames["popupFrame"].document.title;
	}
}

// Tab key trap. iff popup is shown and key was [TAB], suppress it.
// @ argumento e - evento - teclado hecho que causó esta función para ser llamada
function keyDownHandler(e) {
    if (gPopupIsShown && e.keyCode == 9)  return false;
}

// Para  IE.  Ir a través de etiquetas predefinidas y desactivar tabbing en ellos.
function disableTabIndexes() {
	if (document.all) {
		var i = 0;
		for (var j = 0; j < gTabbableTags.length; j++) {
			var tagElements = document.getElementsByTagName(gTabbableTags[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				gTabIndexes[i] = tagElements[k].tabIndex;
				tagElements[k].tabIndex="-1";
				i++;
			}
		}
	}
}

// para IE. regresar tab-indexes.
function restoreTabIndexes() {
	if (document.all) {
		var i = 0;
		for (var j = 0; j < gTabbableTags.length; j++) {
			var tagElements = document.getElementsByTagName(gTabbableTags[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				tagElements[k].tabIndex = gTabIndexes[i];
				tagElements[k].tabEnabled = true;
				i++;
			}
		}
	}
}


/**
* Oculta todas las forma de menú desplegable, seleccione las casillas en la pantalla por lo que no aparecen por encima de la máscara de capa.
*/
function hideSelectBoxes() {
	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";
			}
		}
	}
}


/**
* Hace que todos los menú desplegable, seleccione las casillas forma en la pantalla visible para que no vuelve a aparecer después de que el diálogo está cerrado.
*/
function displaySelectBoxes() {
	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";
			}
		}
	}
}

// Ocultar todos los DIV y Flash
function hideFlashDivs(){
	var divs = document.getElementsByTagName("DIV");
	for(i =0; i < divs.length; i++){
		if(divs[i].id == "flashMovie"){
			divs[i].style.visibility="hidden";
		}
	}
}

// Mostrar DIVS de FLASH
function showFlashDivs(){
	var divs = document.getElementsByTagName("DIV");
	for(i =0; i < divs.length; i++){
		if(divs[i].id == "flashMovie"){
			divs[i].style.visibility="visible";
		}
	}
}