/*
Fecha de creación: 11-07-2007
	Autor: Aaron Padron 
	Última modificación:
	Modificado por: 
	Probado por:
	Principales funcionalidades: 
		* 	Mostrar alerts en una capa y desabilita el fondo del browser.  Tambien se puede ejecutar una funcion al cerrar la capa del alert, asi se usa:
			alertaErrores.alert(Texto_que_aparecera_en_la_capa, nombre_del_campo_del_foco_al_cerrar_capa, funcion_a_ejecutar_al_cerrar_capa(opcional), "argumento1||agumento2||argumento3||..."(opcionales));
		*	Tanto la funcion como los argumentos son opcionales y los agumentos pueden ser desde 1 hasta n.  Puede tener funcion sin argumentos.
		
		
	Por otra parte hay que incluir en la pagina donde se quiera usar el siguente código:	

		<link href="/hesperia_core/css/alertsEstilos.css" rel="stylesheet" type="text/css">
		<script type="text/javascript" src="/hesperia_core/js/alertsFunciones.js"></script>
		<script language="javascript">
			alertaErrores.init(373, 162, 244, 263);  //funcion que inicializa la capa asi -> alertaErrores.init(anchoCapa, altoCapa, posicionIzquierda, posicionSuperior, estiloCapa, estiloBoton);
		</script>
		
	Ej:
	1) alertaErrores.alert("Debex insertar el nombre.", "nombre", alerta);
		y en el <script...>
			function alerta(){
				alert("asdasdasd");
			}
			
	2) alertaErrores.alert("Debex insertar el nombre.", "nombre", alerta, "mensaje");
		y en el <script...>
			function alerta(argumentos){
				alert(argumentos);
			}
			
	3) alertaErrores.alert("Debex insertar el nombre.", "nombre", alerta, "mensaje_1||mensaje_2||mensaje_3");
		y en el <script...>
			function alerta(argumentos){
				alert(argumentos[0]+" - "+argumentos[3]+" - "+argumentos[2]);
			}
*/

var __campoFoco = "";
var __anularCamposProblema = __esExplorer6oMenorVersion();
var __fncExec = "";
var __argumentosFuncionAlert = "";
var __idiomaBotonesCapa = 34;

function __esExplorer6oMenorVersion(){
	var browser = navigator.appName
	if (browser == "Microsoft Internet Explorer"){
		browser = "explorer"
		var b_version=navigator.appVersion
		
		var version=b_version.split(";");
		version = String(version[1]);
		version = version.split(" ");
		version = String(version[2]);
		version = parseFloat(version);
	}
	else
		version = "";
	
	if (browser == "explorer" && version < 7)
		return 1;
	else
		return 0;
}	

//***********************************************************************************//
//***********************************************************************************//
function ___f_clientWidth() {
	return ___f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}

function ___f_clientHeight() {
	return ___f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}

function ___f_scrollLeft() {
	return ___f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}

function ___f_scrollTop() {
	return ___f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function ___f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
//***********************************************************************************//
//***********************************************************************************//

function __llenarCapaMsnError(texto){
	var Tam = __tamVentana();
	document.getElementById("__capaFondoMsnError").style.height = Tam[1]+"px";
	document.getElementById("__capaFondoMsnError").style.width = Tam[0]+"px";
	
	if (document.getElementById("__capaMsnGuardarDatos")){
		if (document.getElementById("__capaMsnGuardarDatos").style.visibility == "visible"){
			document.getElementById("__capaMsnGuardarDatos").style.visibility = "hidden";
		}
	}
	
	__repararCamposProblema(0);
	
	document.getElementById("__capaContenidoMsnError").innerHTML=texto;
	
	if (document.getElementById("__capaContenidoMsnError").offsetHeight > ___altoCapaAlertaErrores){
		var es = document.getElementById("__capaContenidoMsnError").offsetHeight+70;
		document.getElementById("__capaMsnError").style.height = es+"px";
	} else {
		document.getElementById("__capaMsnError").style.height = ___altoCapaAlertaErrores+"px";
		
	}
	
	if (document.getElementById("__capaFondoMsnError")){
		document.getElementById("__capaFondoMsnError").style.visibility = "visible";
	}
	
	__centrarCapaAlert("__capaMsnError");
	document.getElementById("__capaMsnError").style.visibility = "visible";
}

function __llenarCapaMsnErrorConfirm(texto){
	var Tam = __tamVentana();
	document.getElementById("__capaFondoMsnError").style.height = Tam[1]+"px";
	document.getElementById("__capaFondoMsnError").style.width = Tam[0]+"px";
	
	if (document.getElementById("__capaMsnGuardarDatos")){
		if (document.getElementById("__capaMsnGuardarDatos").style.visibility == "visible"){
			document.getElementById("__capaMsnGuardarDatos").style.visibility = "hidden";
		}
	}
	
	__repararCamposProblema(0);
	
	document.getElementById("__capaContenidoMsnErrorConfirm").innerHTML=texto;
	
	if (document.getElementById("__capaContenidoMsnErrorConfirm").offsetHeight > ___altoCapaAlertaErrores){
		var es = document.getElementById("__capaContenidoMsnErrorConfirm").offsetHeight+70;
		document.getElementById("__capaMsnErrorConfirm").style.height = es+"px";
	} else {
		document.getElementById("__capaMsnErrorConfirm").style.height = ___altoCapaAlertaErrores+"px";
		
	}
	
	if (document.getElementById("__capaFondoMsnError")){
		document.getElementById("__capaFondoMsnError").style.visibility = "visible";
	}
	
	__centrarCapaAlert("__capaMsnErrorConfirm");
	document.getElementById("__capaMsnErrorConfirm").style.visibility = "visible";
}

function __centrarCapaAlert(id_capa){
	var tamWin = [___f_clientWidth(), ___f_clientHeight()];
	
	var anchoCapa = String(document.getElementById(id_capa).style.width);
	anchoCapa = anchoCapa.replace(/px/g, '');

	var altoCapa = String(document.getElementById(id_capa).style.height);
	altoCapa = altoCapa.replace(/px/g, '');
	
	var ancho = Number(((tamWin[0]/2) - (anchoCapa/2)));
	var alto = Number(((tamWin[1]/2) - (altoCapa/2))+___f_scrollTop());
	ancho = Math.round(ancho);
	alto = Math.round(alto);
	
	tamWin = [ancho, alto];
	
	__moverCapa(id_capa, tamWin)
	
	return true;
}

function __cerrarCapaMsnError(nombre){
	document.getElementById(nombre).style.visibility="hidden";
	if (document.getElementById('__capaFondoMsnError')){
		document.getElementById('__capaFondoMsnError').style.visibility = "hidden";
	}
	__repararCamposProblema(1);
	if (__campoFoco){
		if (document.getElementById(__campoFoco))
			document.getElementById(__campoFoco).focus();
	}
	
	if (__fncExec) {
		if (__argumentosFuncionAlert){
			__argumentosFuncionAlert = __argumentosFuncionAlert.split("||");		
			__fncExec.apply(this, new Array(__argumentosFuncionAlert));
		}
		else
			__fncExec.apply();
	}
}

function __soloCerrarCapaMsnError(nombre){
	document.getElementById(nombre).style.visibility="hidden";
	if (document.getElementById('__capaFondoMsnError')){
		document.getElementById('__capaFondoMsnError').style.visibility = "hidden";
	}
	__repararCamposProblema(1);
}

function __repararCamposProblema(tipo){
	if (__anularCamposProblema){
		var i = 0;

		while (document.getElementsByTagName("select")[i]) {
			if (document.getElementsByTagName("select")[i].title != "oculto")
			{
				if (tipo == 0)
					document.getElementsByTagName("select")[i].style.visibility = "hidden";
				else if (tipo == 1)
					document.getElementsByTagName("select")[i].style.visibility = "visible";
			}
			i++;
		}
	}
}

function __moverCapa(id_capa, punto){
	
	document.getElementById(id_capa).style.left = punto[0]+"px";
	document.getElementById(id_capa).style.top = punto[1]+"px";	
	
	return true;
}
	
function __tamVentana(){
	var Tamanyo = [0, 0];
	
	if( window.innerHeight && window.scrollMaxY ) { // Firefox 
		pageWidth = window.innerWidth + window.scrollMaxX;
		pageHeight = window.innerHeight + window.scrollMaxY;
	}
	else if(document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
	
		pageWidth = document.body.scrollWidth;
		pageHeight = document.body.scrollHeight;
	}
	else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		pageWidth = document.body.offsetWidth + document.body.offsetLeft;
		pageHeight = document.body.offsetHeight + document.body.offsetTop;
	}
	
	Tamanyo = [ 
		pageWidth, 
		pageHeight 
	];
	
	return Tamanyo;
}

function __guardandoDatos(texto){
	var txt_guardando;
	if (__idiomaBotonesCapa == 29){
		txt_guardando = "Saving data...";
	} else {
		txt_guardando = "Guardando datos...";
	}
	
	if (!texto)
		texto = txt_guardando;
		
	__repararCamposProblema(0);
	
	var Tam = __tamVentana();
	document.getElementById("__capaFondoMsnError").style.height = Tam[1]+"px";
	document.getElementById("__capaFondoMsnError").style.width = Tam[0]+"px";
	
	
	document.getElementById("__capaContenidoMsnGuardarDatos").innerHTML=texto;
	
	if (document.getElementById("__capaFondoMsnError")){
		document.getElementById("__capaFondoMsnError").style.visibility = "visible";
	}
			
	__centrarCapaAlert("__capaMsnGuardarDatos");
//	alert(document.getElementById("__capaMsnGuardarDatos").style.top)
	document.getElementById("__capaMsnGuardarDatos").style.visibility = "visible";
}

window.onresize = function() { 
	var Tam = __tamVentana(); 
	document.getElementById("__capaFondoMsnError").style.height = Tam[1]+"px";
	document.getElementById("__capaFondoMsnError").style.width = Tam[0]+"px";
}; 

/*
window.onload = function() { 
	
	var Tam = __tamVentana();
	document.getElementById("__capaFondoMsnError").style.height = Tam[1]+"px";
	document.getElementById("__capaFondoMsnError").style.width = Tam[0]+"px";
	
};
*/
var __tamanoVentana = __tamVentana();

var ___anchoCapaAlertaErrores, ___altoCapaAlertaErrores;

alertaErrores = {};

alertaErrores.init = function(anchoCapa, altoCapa, posicionIzquierda, posicionSuperior, estiloCapa, estiloBoton){
	if (!estiloCapa)
		estiloCapa = "estiloCapaMsnError";

	if (!estiloBoton)
		estiloBoton = "botonCerrarCapaMsnError";

	if (!posicionIzquierda)
		posicionIzquierda = "0";

    if (!posicionSuperior) 
		posicionSuperior = "0";

	if (!anchoCapa)
		anchoCapa = 373;

//	alert("C"+ posicionSuperior);

	if (!altoCapa)
		altoCapa = 162;
		
	___anchoCapaAlertaErrores = anchoCapa;
	___altoCapaAlertaErrores = altoCapa;

/* CAPA ALERT */
	document.write("<div id=\"__capaMsnError\" style=\"position:absolute; left:"+posicionIzquierda+"px; top:"+posicionSuperior+"px; width:"+anchoCapa+"px; height:"+altoCapa+"px; z-index:1000000; visibility:hidden;\" class=\""+estiloCapa+"\">");
	document.write("	<table width=\"100%\" height=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
	document.write("		<tr>");
	document.write("			<td height=\"18\" align=\"right\" valign=\"top\">&nbsp;</td>");
	document.write("		</tr>");
	document.write("		<tr>");
	document.write("			<td align=\"center\" class=\"textoCapaMsnError\" valign=\"top\" width=\"100%\" height=\"70%\">");
	document.write("				<table cellpadding=\"0\" cellspacing=\"0\" width=\"98%\" height=\"100%\" align=\"center\" border=\"0\">");
	document.write("					<tr>");
	document.write("						<td width=\"100%\" height=\"100%\" valign=\"middle\" align=\"center\" class=\"textoCapaMsnError\"><div id=\"__capaContenidoMsnError\"></div></td>");
	document.write("					</tr>");
	document.write("				</table>");
	document.write("			</td>");
	document.write("		</tr>");
	document.write("		<tr>");
	document.write("			<td height=\"30\" align=\"center\" valign=\"top\"><span style=\"cursor:pointer;\" onClick=\"javascript:__cerrarCapaMsnError('__capaMsnError');\" class=\""+estiloBoton+"\"><span id=\"__textoBotonMsnError\"></span></span></td>");
	document.write("		</tr>");
	document.write("	</table>");
	document.write("</div>");
	
/* CAPA CONFIRM */
	document.write("<div id=\"__capaMsnErrorConfirm\" style=\"position:absolute; left:"+posicionIzquierda+"px; top:"+posicionSuperior+"px; width:"+anchoCapa+"px; height:"+altoCapa+"px; z-index:1000000; visibility:hidden;\" class=\""+estiloCapa+"\">");
	document.write("	<table width=\"100%\" height=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
	document.write("		<tr>");
	document.write("			<td height=\"18\" align=\"right\" valign=\"top\">&nbsp;</td>");
	document.write("		</tr>");
	document.write("		<tr>");
	document.write("			<td align=\"center\" class=\"textoCapaMsnError\" valign=\"top\" width=\"100%\" height=\"70%\">");
	document.write("				<table cellpadding=\"0\" cellspacing=\"0\" width=\"98%\" height=\"100%\" align=\"center\" border=\"0\">");
	document.write("					<tr>");
	document.write("						<td width=\"100%\" height=\"100%\" valign=\"middle\" align=\"center\" class=\"textoCapaMsnError\"><div id=\"__capaContenidoMsnErrorConfirm\"></div></td>");
	document.write("					</tr>");
	document.write("				</table>");
	document.write("			</td>");
	document.write("		</tr>");
	document.write("		<tr>");
	document.write("			<td height=\"30\" align=\"center\" valign=\"top\"><span style=\"cursor:pointer;\" onClick=\"javascript:__cerrarCapaMsnError('__capaMsnErrorConfirm');\" class=\""+estiloBoton+"\"><span id=\"__textoBotonMsnErrorConfirm\"></span></span>");
	document.write("				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"cursor:pointer;\" onClick=\"javascript:__soloCerrarCapaMsnError('__capaMsnErrorConfirm');\" class=\""+estiloBoton+"\"><span id=\"__textoBotonMsnErrorCancelar\"></span></span>");
	document.write("			</td>");
	document.write("		</tr>");
	document.write("	</table>");
	document.write("</div>");

/* CAPA GUARDANDO DATOS */
	document.write("<div id=\"__capaMsnGuardarDatos\" style=\"position:absolute; left:"+posicionIzquierda+"px; top:"+posicionSuperior+"px; width:200px; height:50px; z-index:1000000; visibility:hidden; border:3px solid;\" class=\""+estiloCapa+"\">");
	document.write("	<table width=\"100%\" height=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
	document.write("		<tr>");
	document.write("			<td align=\"center\" class=\"textoCapaMsnError\" valign=\"middle\" width=\"100%\" height=\"70%\">");
	document.write("				<table cellpadding=\"0\" cellspacing=\"0\" width=\"98%\" height=\"100%\" align=\"center\" border=\"0\">");
	document.write("					<tr>");
	document.write("						<td align=\"center\"><img src=\"/hesperia_core/img/loading.gif\"></td>");
	document.write("					</tr>");
	document.write("					<tr><td height=\"5\"></td></tr>");
	document.write("					<tr>");
	document.write("						<td align=\"center\" class=\"textoCapaMsnError\"><div id=\"__capaContenidoMsnGuardarDatos\"></div></td>");
	document.write("					</tr>");
	document.write("				</table>");
	document.write("			</td>");
	document.write("		</tr>");
	document.write("	</table>");
	document.write("</div>");
	
	var txt_aceptar, txt_cancelar;
	
	if (__idiomaBotonesCapa == 29){
	//	txt_aceptar = "&nbsp;&nbsp;&nbsp;OK&nbsp;&nbsp;&nbsp;";
		txt_aceptar = "&nbsp;&nbsp;&nbsp;OK&nbsp;&nbsp;&nbsp;";
		txt_cancelar = "Cancel";
	} else {
		txt_aceptar = "Aceptar";
		txt_cancelar = "Cancelar";
	}
	//*** Texto boton capa alert
	document.getElementById("__textoBotonMsnError").innerHTML = txt_aceptar;
	
	//*** Texto boton capa confirm
	document.getElementById("__textoBotonMsnErrorCancelar").innerHTML = txt_cancelar;
	document.getElementById("__textoBotonMsnErrorConfirm").innerHTML = txt_aceptar;
	
	var Tam = __tamVentana();
	document.getElementById("__capaFondoMsnError").style.height = Tam[1]+"px";
	document.getElementById("__capaFondoMsnError").style.width = Tam[0]+"px";
}

alertaErrores.idioma = function(id_idioma){
	__idiomaBotonesCapa = id_idioma;
	
	if (document.getElementById("__textoBotonMsnError") || document.getElementById("__textoBotonMsnErrorCancelar")){
		var txt_aceptar, txt_cancelar;
		
		if (__idiomaBotonesCapa == 29){
		//	txt_aceptar = "&nbsp;&nbsp;&nbsp;OK&nbsp;&nbsp;&nbsp;";
			txt_aceptar = "&nbsp;&nbsp;&nbsp;OK&nbsp;&nbsp;&nbsp;";
			txt_cancelar = "Cancel";
		} else {
			txt_aceptar = "Aceptar";
			txt_cancelar = "Cancelar";
		}
		
		//*** Texto boton capa alert
		document.getElementById("__textoBotonMsnError").innerHTML = txt_aceptar;
		
		//*** Texto boton capa confirm
		document.getElementById("__textoBotonMsnErrorCancelar").innerHTML = txt_cancelar;
		document.getElementById("__textoBotonMsnErrorConfirm").innerHTML = txt_aceptar;
	}
}

alertaErrores.alert = function(mensajeAlert, campoFocus, funcionEjecutar, argumentosFuncion){
	var Tam = __tamVentana();
	document.getElementById("__capaFondoMsnError").style.height = Tam[1]+"px";
	document.getElementById("__capaFondoMsnError").style.width = Tam[0]+"px";
	
	if (document.getElementById("__capaMsnError").style.top < 0)  
		document.getElementById("__capaMsnError").style.top = 236;
	
	if (mensajeAlert && document.getElementById("__capaMsnError").style.visibility == "hidden"){

		__campoFoco = "";
		
		if (campoFocus)
			__campoFoco = campoFocus;
			
		__llenarCapaMsnError(mensajeAlert);
	
		if (funcionEjecutar) {
			__fncExec = funcionEjecutar;
			
			if (argumentosFuncion)
				__argumentosFuncionAlert = argumentosFuncion;
			else
				__argumentosFuncionAlert = "";
		}
		else {
			__fncExec = "";
			__argumentosFuncionAlert = "";
		}
	}
}

alertaErrores.confirm = function(mensajeAlert, campoFocus, funcionEjecutar, argumentosFuncion){
	
	if (document.getElementById("__capaMsnErrorConfirm").style.top < 0)  
		document.getElementById("__capaMsnErrorConfirm").style.top = 236;
	
	if (mensajeAlert && document.getElementById("__capaMsnErrorConfirm").style.visibility == "hidden"){
	
		__campoFoco = "";
		
		if (campoFocus)
			__campoFoco = campoFocus;
			
		__llenarCapaMsnErrorConfirm(mensajeAlert);
	
		if (funcionEjecutar) {
			__fncExec = funcionEjecutar;
			
			if (argumentosFuncion)
				__argumentosFuncionAlert = argumentosFuncion;
			else
				__argumentosFuncionAlert = "";
		} else {
			__fncExec = "";
			__argumentosFuncionAlert = "";
		}
	}
}

alertaErrores.valueBotonOK = function (donde,txt_aceptar){
	if (!txt_aceptar)
		txt_aceptar = "";
	
	if (!donde)
		donde = "";
		
	if (donde == "confirm" || donde == "alert"){
		if (donde == "confirm"){
			donde = "__textoBotonMsnErrorConfirm";
		}
		else if (donde == "alert"){
			donde = "__textoBotonMsnError";
		}
		
		if (txt_aceptar != ""){
			document.getElementById(donde).innerHTML = txt_aceptar;
		}
		else{
			if (__idiomaBotonesCapa == 29){
				txt_aceptar = "&nbsp;&nbsp;&nbsp;OK&nbsp;&nbsp;&nbsp;";
			} else {
				txt_aceptar = "Aceptar";
			}
			
			document.getElementById(donde).innerHTML = txt_aceptar;
		}
	}
}

alertaErrores.guardando = function(mensajeAlert){
	if (document.getElementById("__capaMsnGuardarDatos").style.visibility == "hidden"){
		__guardandoDatos(mensajeAlert);
	}
}

alertaErrores.ocultarGuardando = function(){
	if (document.getElementById("__capaMsnGuardarDatos").style.visibility == "visible"){
		__repararCamposProblema(1);
		document.getElementById("__capaMsnGuardarDatos").style.visibility = "hidden"
		document.getElementById("__capaFondoMsnError").style.visibility = "hidden";
	}
}

//document.write("<div id=\"__capaFondoMsnError\" style=\"position:absolute; left: 0; top: 0; z-index:999999; visibility:hidden;\" class=\"estiloCapaFondoMsnError\"></div>");