	
	// Se agregan los elementos necesarios para el manejo de popups
	addEvent(window,'load',addDisplayItems);
	
	// Para aplicar el estilo al formulario
	addEvent(window,'load',aplicaEstiloFormulario);
	
	//Se agregan los eventos para establecer el alto de la tabla principal
	addEvent(window,'load',setMainHeight);
	addEvent(window,'resize',setMainHeight);
	
	/**
		Agrega los elementos necesarios para el manejo de popups internos	
	**/
	function addDisplayItems()
	{
		var body = document.getElementById("body");
		
		if (body == null)
			return;
		
		var frameDatos = document.getElementById("frameDatos");
		
		if (frameDatos == null)
		{
			frameDatos = document.createElement("iframe");
			frameDatos.setAttribute("id","frameDatos");
			frameDatos.className = "popupInterno";
			frameDatos.setAttribute("width","0");
			frameDatos.setAttribute("height","0");
			frameDatos.setAttribute("frameBorder","0");
			frameDatos.setAttribute("src","");
			body.insertBefore(frameDatos,body.firstChild);
		}
		
		var divWait = document.getElementById("divWait");
		
		if (divWait == null)
		{
			divWait = document.createElement("div");
			divWait.setAttribute("id","divWait");
			divWait.className = "divWait";
			body.insertBefore(divWait,body.firstChild);
		}
	}

	/**
		Oculta los selectores ("<select>") de todas las formas y todos los
		frames de un documento
	**/
	function hideSelects(doc)
	{
		for(var i = 0; i < doc.forms.length; i++)
		{
			var nElems = doc.forms[i].elements.length;
			for (var j = 0; j < nElems; j++)
			{
				if (doc.forms[i].elements[j].tagName.toLowerCase() == "select")
					doc.forms[i].elements[j].style.visibility = "hidden";
			}
		}
		
		for (var i = 0; i < doc.frames.length; i++)
		{
			hideSelects(doc.frames[i].document);
		}
	}
		
	/**
		Muestra los selectores ("<select>") de todas las formas y todos los
		frames de un documento
	**/
	function showSelects(doc)
	{
		for(var i = 0; i < doc.forms.length; i++)
		{
			var nElems = doc.forms[i].elements.length;
			for (var j = 0; j < nElems; j++)
			{
				if (doc.forms[i].elements[j].tagName.toLowerCase() == "select")
					doc.forms[i].elements[j].style.visibility = "visible";
			}
		}
		
		for (var i = 0; i < doc.frames.length; i++)
		{
			showSelects(doc.frames[i].document);
		}
	}
	
	/**
		Establece si mostrar o no el scroll en el objeto dado
		con los valores permitidos: visible, hidden, scroll o auto para objetos
		y auto, yes o no para frames o ventanas
	**/
	function setShowScroll(obj,val)
	{
		if (obj.tagName.toLowerString() == 'frame')
		{
			if (val != 'auto' &&
				val != 'yes' &&
				val != 'no')
				return false;
			
			frame.scrolling = val;
		}
		else
		{
			if (val != 'visible' &&
				val != 'hidden' &&
				val != 'scroll' &&
				val != 'auto')
				return false;
			
			obj.style.overflow = val;
		}
	}

	/**
		 Asocia las funciones de cambio de estilo a todos los elementos
		input de un formulario
	**/
	function aplicaEstiloFormulario()
	{
		var inputCheck = document.getElementsByTagName("input");
		
		for (var inputIndex=0;inputIndex<inputCheck.length;inputIndex++)
		{
			if (inputCheck[inputIndex].type == "checkbox" ||
				inputCheck[inputIndex].type == "radio")
			{
				inputCheck[inputIndex].className = "checkRadio";
			}
			
			if ((inputCheck[inputIndex].type == "button" ||
				inputCheck[inputIndex].type == "submit" ||
				inputCheck[inputIndex].type == "reset") &&
				inputCheck[inputIndex].getAttribute("setStyle") != "false")
			{
				inputCheck[inputIndex].className = "botonNormal";
				inputCheck[inputIndex].onmouseover = activaBoton;
				inputCheck[inputIndex].onmouseout  = desactivaBoton;
			}
		}
	}
	
	/**
		Modifica el estilo de un botón de un formaulario que tenga la
		función asociada al evento onmouseover
	**/
	function activaBoton(event) 
	{
		this.className = "botonActivo";
	}

	/**
		Modifica el estilo de un botón de un formaulario que tenga la
		función asociada al evento onmouseout
	**/
	function desactivaBoton(event)
	{
		this.className = "botonNormal";
	}
     
	/**
		Agrega un evento a un objeto donde evType puede ser, por ejemplo
		"load", "submit", "click", las cuales se completarán en el caso de
		IE o NS como sea necesario. Es útil cuando no se quiere sobreescribir
		el/los eventos existentes para el objeto.
	**/
	function addEvent(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;
		}
	}
	
	/**
		Elimina un evento a un objeto donde evType puede ser, por ejemplo
		"load", "submit", "click", las cuales se completarán en el caso de
		IE o NS como sea necesario.
	**/
	function removeEvent(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;	
		}
	}

	/**
		Regresa el ancho de la ventana actual. Este método solo es válido
		una vez que ha terminado de cargar el tag "body".
	**/
	function getWindowWidth()
	{
		if (document.body.offsetWidth)
			return document.body.offsetWidth;
		else if (window.innerWidth)
			return window.innerWidth;
		
		return 0;
	}

	/**
		Regresa el largo de la ventana actual. Este método solo es válido
		una vez que ha terminado de cargar el tag "body".
	**/
	function getWindowHeight()
	{
		if (document.body.offsetHeight)
			return document.body.offsetHeight;
		else if (window.innerHeight)
			return window.innerHeight;
		
		return 0;
	}
	
	/**
		Oculta o muestra los elementos que corresponden al tag dado.
	**/
	function hideShowElementsFromTag(doc,tagName,show)
	{
		for(var i = 0; i < doc.forms.length; i++)
		{
			var nElems = doc.forms[i].elements.length;
			for (var j = 0; j < nElems; j++)
			{
				if (doc.forms[i].elements[j].tagName.toLowerCase() == tagName)
					if (show)
						doc.forms[i].elements[j].style.visibility = "visible";
					else
						doc.forms[i].elements[j].style.visibility = "hidden";
			}
		}
		
		for (var i = 0; i < doc.frames.length; i++)
		{
			hideShowElementsFromTag(doc.frames[i].document,tagName,show);
		}
	}
	
	/**
		Establece el alto del contenido de la forma para llenar verticalmente
		cuando el contenido de la forma no llena la ventana
	**/
	function setMainHeight()
	{
		try
		{
			var hasAspDiv = hideEventValidationDiv();
			var hasStateDiv = hideViewStateDiv();

			var windowHeight = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
		
			var mainTable = document.getElementById("mainTable");
			
			if (mainTable == null)
				return;
				
			if (document.getElementById("tdPivote") == null)
			{
				var row = mainTable.insertRow(mainTable.rows.length);
				var td = row.insertCell(0);
				td.id = "tdPivote";
				td.appendChild(document.createTextNode(" "));
				td.style.fontSize = "1px";
				td.style.height = "0px";
			}
			
			var bannerHeight = 0;
			if (document.getElementById("banner") != null)
			{
				bannerHeight = document.getElementById("banner").offsetHeight + 14; //Los 14 son por el espacio que habrá entre el banner y el marco de la forma
			}
			
			var tdPivote = document.getElementById("tdPivote");
			
			tdPivote.style.height = "0px";
			var topBottomMarginsHeight = 20;
			if (mainTable.offsetHeight < (windowHeight - topBottomMarginsHeight))
			{
				if (hasAspDiv)
					tdPivote.style.height = windowHeight - mainTable.offsetHeight - bannerHeight - topBottomMarginsHeight + 4 + "px";
				else
					tdPivote.style.height = windowHeight - mainTable.offsetHeight - bannerHeight - topBottomMarginsHeight + 4 + "px";
			}
			
			try
			{
				setCustomMainHeight();
			}
			catch (e) {}
		}
		catch (ed) {}
	}
	
	/**
		En ASP.NET oculta el tag __EVENTVALIDATION pues aunque el componente no es visible
		utiliza espacio al ser dibujado por el navegador	
	**/
	function hideEventValidationDiv()
	{
		var hidden = document.getElementById("__EVENTVALIDATION");
		
		if (hidden != null)
		{
			hidden.parentNode.style.display = "none";
			return true;
		}
		
		return false;
	}

	function hideViewStateDiv()
	{
		var hidden = document.getElementById("__VIEWSTATE");
		
		if (hidden != null)
		{
			hidden.parentNode.style.display = "none";
			return true;
		}
		
		return false;
	}
	
	/**
		DEPRECATED
	**/
	function showPopup(url)
	{
		var scroll = getScrollXY();
		var divWait = document.getElementById("divWait");
		var frameDatos = document.getElementById("frameDatos");
		
		frameDatos.style.width = "80%";
		
		divWait.style.display = "block";
		
		divWait.style.width = screen.width;
		divWait.style.height = screen.height;    
		divWait.style.left=0;
		divWait.style.top=scroll[1];
		
		divWait.style.zIndex= 999;
		
		if (screen.width == 800 && screen.height == 600)
			frameDatos.style.top = scroll[1] + 0;
		else
			frameDatos.style.top = scroll[1] + 74;

		frameDatos.style.left = 90;
		frameDatos.style.zIndex= 1000;

		frameDatos.style.display = "block";
		window.frames["frameDatos"].window.navigate(url);

		document.body.parentNode.style.overflow = "hidden";
	}
	
	/**
		DEPRECATED
	**/
	function showPopupFixed(url,width,height)
	{
		var scroll = getScrollXY();
		var divWait = document.getElementById("divWait");
		var frameDatos = document.getElementById("frameDatos");
		
		if (width)
			frameDatos.style.width = width;
		else
			frameDatos.style.width = "80%";
		
		divWait.style.display = "block";
		
		divWait.style.width= screen.width;
		divWait.style.height= screen.height;
		divWait.style.left=0;
		divWait.style.top=scroll[1];
		
		divWait.style.zIndex= 999;
		
//		if (screen.width == 800 && screen.height == 600)
//			frameDatos.style.top = scroll[1] + 0;
//		else
//			frameDatos.style.top = scroll[1] + 74;

		var viewportWidth = (window.innerWidth) ? window.innerWidth : document.body.offsetWidth;
		var frameWidthPixels = 0;
		
		if (width)
			frameWidthPixels = width;
		else
			frameWidthPixels = frameDatos.style.width.replace("%","")*viewportWidth/100;

		if (height)
			frameDatos.style.height = height;
		else
			frameDatos.style.height = 300;
			
		//Se centra verticalmente
		var visibleHeight = top.frames["mainFrame"] != null ?
							visibleHeight = top.frames["mainFrame"].document.body.clientHeight :
							visibleHeight = document.body.clientHeight;
		var topPosition = visibleHeight > height ? ((visibleHeight - height) / 2) : 0;
		frameDatos.style.top = scroll[1] + topPosition;

		frameDatos.style.left = (viewportWidth/2) - (frameWidthPixels/2);
		frameDatos.style.zIndex= 1000;
		
		frameDatos.style.display = "block";
		window.frames["frameDatos"].window.navigate(url);

		document.body.parentNode.style.overflow = "hidden";
	}
	
	/**
		Función que debe utilizarse para abrir popups internos. El establecimiento de tamaño y
		posición se efectua al cargarse el contenido del popup.
		
		Donde:
			root - Si el popup abrirá en el documento actual pasar "this", si el popup abrirá en el frame padre pasar "parent"
			url - Url que se mostrará dentro del popup. La forma debe heredar de IberoPuebla.Comunes.Portal.Base.PrivatePopup o PublicPopup
			width - Ancho que tendrá el popup, si no se especifica se utilizará el 80% del ancho disponible
			height - Alto que tendrá el popup, si no se especifica se utilizará el 
	**/
	function showPopupUsing(root,url,width,height)
	{
		showHideWaitDiv(root,true);
		
		var vw = root.document.getElementById("vw");
		if (width && vw == null)
		{
			vw = root.document.createElement("input");
			vw.setAttribute("type","hidden");
			vw.setAttribute("id","vw");
			vw.setAttribute("value",width);
			
			var forma = root.document.getElementById("forma");
			if (forma == null)
				forma = root.document.getElementById(root.document.getElementById("formId").value);
			forma.appendChild(vw);
		}
		else if (width)
			vw.value = width;
		else if (vw != null)
			vw.value = "";
			
		var vh = root.document.getElementById("vh");
		
		if (height && vh == null)
		{	
			vh = root.document.createElement("input");
			vh.setAttribute("type","hidden");
			vh.setAttribute("id","vh");
			vh.setAttribute("value",height);
			
			var forma = root.document.getElementById("forma");
			if (forma == null)
				forma = root.document.getElementById(root.document.getElementById("formId").value);
			forma.appendChild(vh);
		}
		else if (height)
			vh.value = height;
		else if (vh != null)
			vh.value = "";
		
		root.document.getElementById("frameDatos").style.zIndex = 1000;
		root.document.getElementById("frameDatos").style.display = "block";
		
		if (root.window)
			root.window.frames["frameDatos"].window.navigate(url);
		else
			window.frames["frameDatos"].navigate(url);
		
		//Se oculta el scroll del padre
		root.document.getElementById("body").parentNode.style.overflow = "hidden";
	}
	
	/**
		Estable el tamaño y posición del contenedor de un popup. Esta función es llamada
		automáticamente por una página que herede de IberoPuebla.Comunes.Portal.Base.PrivatePopup o PublicPopup 
	**/
	function resizeContainer()
	{
		if (parent == null) //Si no existe un parent significa que la forma se está ejecutando en el top
			return;
			
		var parentScroll = parent.getScrollXY();
		var container = parent.document.getElementById("frameDatos");
		var body = parent.document.getElementById("body");
		var mainTable = document.getElementById("mainTable");
		var vw = parent.document.getElementById("vw");
		var vh = parent.document.getElementById("vh");
		var width = (vw != null && vw.value != "") ? (vw.value) : null;
		var height = (vh != null && vh.value != "") ? (vh.value) : null;
		
		//Se oculta el contenedor
		container.style.visibility = "hidden";
		
		if (container == null)
		{
			alert("Error: El contenedor de un popup debe tener el id=\"frameDatos\"");
			return;
		}
		
		//Se establece el ancho del iframe contenedor
		if (width)
			container.style.width = width;
		else
			container.style.width = "80%";

		var availableWidth = (parent.window.innerWidth) ? parent.window.innerWidth : Math.max(body.offsetWidth,body.parentNode.offsetWidth);
		var bestWidth = 0;
		
		if (width)
			bestWidth = width;
		else
			bestWidth = container.style.width.replace("%","")*availableWidth/100;

		//Se establece la mejor posición horizontal
		container.style.left = (availableWidth/2) - (bestWidth/2);
		
		var pnl = document.getElementById("pnlCentro");
		var availableHeight = body.parentNode.clientHeight;
		var internalVerticalOffset = 88;
		
		//Se establece el alto del iframe contenedor
		var bestHeight = pnl == null ? 300 : pnl.scrollHeight + internalVerticalOffset;
		bestHeight = pnl != null && (pnl.scrollHeight + internalVerticalOffset) < 300 && availableHeight >= 300 ? 300 : bestHeight;
		
		//Se establece el alto del panel interno (contenido del popup)
		if (pnl != null)
		{
			if (height)
				pnl.style.height = height - internalVerticalOffset;
			else if (availableHeight >= bestHeight)
				pnl.style.height = pnl.scrollHeight;
			else
				pnl.style.height = availableHeight - internalVerticalOffset;
				
			pnl.style.overflow = "auto";
		}
		
		if (height)
			container.style.height = height;
		else if (availableHeight >= bestHeight)
			container.style.height = bestHeight;
		else
			container.style.height = availableHeight;
		
		if (parseInt(container.style.height.replace("px","")) < mainTable.offsetHeight)
			container.style.height = mainTable.offsetHeight + 3;
		
		//Se valida que el tamaño de la tabla corresponda con el tamaño del frame
		if ((mainTable.offsetHeight - container.offsetHeight) < -3 && pnl != null)
			pnl.style.height = parseInt(pnl.style.height.replace("px","")) + (container.offsetHeight-mainTable.offsetHeight) -3;
		
		//Se establece la mejor posición vertical
		container.style.top = parentScroll[1] + (availableHeight > container.clientHeight ? ((availableHeight - container.clientHeight) / 2) : 0);
		
		//Se muestra el contenedor
		container.style.visibility = "visible";
	}
	
	/**
		Cierra el popup interno del documento padre. Es llamado generalmente desde
		el botón de cerrar dentro del popup.
	**/
	function closePopup()
	{
		showHideWaitDiv(parent,false);
		var frameDatos = parent.document.getElementById("frameDatos");
		frameDatos.style.display = "none";
		frameDatos.src = "";
		parent.document.body.parentNode.style.overflow = "auto";
	}
	
	/**
		Muestra/oculta la capa de espera
	**/
	function showHideWaitDiv(root,show)
	{
		var divWait = root.document.getElementById("divWait");
		var body = root.document.getElementById("body");
		
		if (show && body)
		{
			divWait.style.zIndex= 999;
			//divWait.style.width = screen.width; //Se eliminó esta parte porque empezó a mover la pantalla hacia la izquierda
			divWait.style.height = body.scrollHeight;
			divWait.style.left = 0;
			divWait.style.top = 0;
			divWait.style.display = "block";
		}
		else
			divWait.style.display = "none";
	}
	
	/**
		Obtiene un arreglo con el scroll en x y el scroll en y del documento
	**/
	function getScrollXY() 
	{
		var scrOfX = 0, scrOfY = 0;
		
		if(typeof(window.pageYOffset) == 'number')
		{
			//Netscape compliant
			scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		}
		else if(document.body && (document.body.scrollLeft || document.body.scrollTop))
		{
			//DOM compliant
			scrOfY = document.body.scrollTop;
			scrOfX = document.body.scrollLeft;
		}
		else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
		{
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
		}
	
		return [scrOfX,scrOfY];
	}
	
	/**
		Oculta o muestra el elemento con el nombre dado, haciendo cambios en
		la clase del objeto que lo llama y estableciendo el tooltip.
	**/
	function showHide(elemento,obj,tooltipText)
	{
		var tbl = document.getElementById(elemento);
		
		if (tbl.style.display == 'block' || tbl.style.display == '')
		{
			tbl.style.display = 'none';
			obj.className = 'expander';
			
			if (tooltipText != null && tooltipText.length > 0)
				obj.title = 'Mostrar ' + tooltipText;
		}
		else
		{
			tbl.style.display = 'block';
			obj.className = 'colapsar';
			
			if (tooltipText != null && tooltipText.length > 0)
				obj.title = 'Ocultar ' + tooltipText;
		}
	}
	
	/**
		
	**/
	function showHideContenido(initValue,currentValue)
	{
		var contenidoA = document.getElementById("trContenidoA");
		var contenidoB = document.getElementById("trContenidoB");
		
		if (initValue == currentValue)
		{
			if (contenidoA != null)
				contenidoA.style.visibility = "visible";
				
			if (contenidoB != null)
				contenidoB.style.visibility = "visible";
		}
		else
		{
			if (contenidoA != null)
				contenidoA.style.visibility = "hidden";
				
			if (contenidoB != null)
				contenidoB.style.visibility = "hidden";
		}
	}
	
	/**
		Establece correctamente el ancho de los selectores de año y mes para el componente
		WebDateChooser.
		Debe ser llamado desde WebDateChooser.ClientSideEvents.BeforeDropDown para mostrarse
		correctamente.
	**/
	function resizeDateChooserDropDowns(dateChooserName)
	{
		var selectorMes = document.getElementById(dateChooserName + "_DrpPnl_Calendar1_504");
		var selectorAnio = document.getElementById(dateChooserName + "_DrpPnl_Calendar1_506");
		
		selectorMes.style.width = "90px";
		selectorAnio.style.width = "55px";
	}
	
	/**
		Obtiene la posición en x de un objeto posicionado relativamente
	**/
	function getAbsoluteLeft(obj)
	{
		var left = obj.offsetLeft;
		var skipOffset = false;
		while(obj.parentNode != null && obj.parentNode.offsetLeft != null)
		{
			skipOffset = 	(obj.parentNode.nodeName.toUpperCase() == "TR") ||
							(obj.parentNode.nodeName.toUpperCase() == "FORM") ||
							(obj.parentNode.nodeName.toUpperCase() == "TBODY");
		
			if (!skipOffset)
				left += obj.parentNode.offsetLeft;
			
			obj = obj.parentNode;
		}
		
		return left;
	}
	
	/**
		Obtiene la posición en y de un objeto posicionado relativamente
	**/
	function getAbsoluteTop(obj)
	{
		var top = obj.offsetTop;
		var skipOffset = false;
		while(obj.parentNode != null && obj.parentNode.offsetTop != null)
		{
			skipOffset =	(obj.parentNode.nodeName.toUpperCase() == "TR") || 
							(obj.parentNode.nodeName.toUpperCase() == "FORM") ||
							(obj.parentNode.nodeName.toUpperCase() == "TBODY");
			
			if (!skipOffset)
				top += obj.parentNode.offsetTop;
				
			obj = obj.parentNode;
		}
		
		return top;
	}

