var CGIPATH='/ibi_apps/WFServlet';

function cerrar() {
	var ventana = window.self;
	ventana.opener = window.self;
	ventana.close();
}

function CopyUser(e) {
	e.WAMUSER.value=e.IBIC_user.value;
	submita();
}

function submita() {
	if (formValidat()) {
		chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
		pass = "";

	   for(x=0;x<99;x++)
	   {
		  i = Math.floor(Math.random() * 62);
		  pass += chars.charAt(i);
	   }
		document.form1.WAMUSER.value=document.form1.IBIC_user.value;
		document.form1.target = 'LABEL'+pass+'new';
		window.open('about:blank','LABEL'+pass+'new',"width="+(window.screen.availWidth)+",height="+(window.screen.availHeight)+",toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes")
		document.form1.WAMUSER.value=document.form1.IBIC_user.value;
		document.form1.submit()
	}
}
		
function tamanyoVentana() {
	var Tamanyo = [0, 0];
	if (typeof window.innerWidth != 'undefined'){
		Tamanyo = [window.innerWidth,window.innerHeight];
	}else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0){
		Tamanyo = [document.documentElement.clientWidth,document.documentElement.clientHeight];
	}else{
		Tamanyo = [document.getElementsByTagName('body')[0].clientWidth, document.getElementsByTagName('body')[0].clientHeight];
	}
	return Tamanyo;
}

function poselements() {
	window.moveTo(0,0)
	window.resizeTo(window.screen.availWidth,window.screen.availHeight);
}

function formValidat(){
	var frmValid = false;
	if (document.form1.IBIC_user.value.length > 0 && document.form1.IBIC_pass.value.length > 0){ frmValid = true; }
	if (!frmValid) {window.alert("Debe entrar un nombre de usuario y una clave");}
	return frmValid;
}

function iSubmitEnter(oEvento, oFormulario){ 
	 var iAscii; 

	 if (oEvento.keyCode) 
		 iAscii = oEvento.keyCode; 
	 else if (oEvento.which) 
		 iAscii = oEvento.which; 
	 else 
		 return false; 

	 if (iAscii == 13) validarusr(); 

	 return true; 
} 

function mostrarInfoWeb(nombreCapa){
	n = document.getElementsByTagName('iframe');
	for (var i=0; i<n.length; i++) {
		n[i].style.display= 'none';
	}
	if (nombreCapa=="" || nombreCapa==null  || nombreCapa==undefined) {
		$('logos').style.display = 'inline';
		$('logos_2').style.display = 'inline';
		$('login_bkg').style.display = 'inline';
		$('login_form').style.display = 'inline';
	}
	else {
		$('logos').style.display = 'none';
		$('logos_2').style.display = 'none';
	}
	switch(nombreCapa) {
		case "infoLG":
			break;
		case "ASPGRP": mostrarIframe("ASPGRP");
			break;
		case "IPORIUM": mostrarIframe("IPORIUM");
			break;
		case "INECOWEB": mostrarIframe("INECOWEB");
			break;
		case "TYDA": mostrarIframe("TYDA");
			break;
		case "":
			break;
	}
}

function mostrarIframe(nombreIframe) {
	$(nombreIframe).style.display = 'inline';
	$(nombreIframe).focus();
}

function mostrarCapa(idCapa){ $(idCapa).style.display="inline"; }
function ocultarCapa(idCapa){ $(idCapa).style.display="none"; }

function shownoDisp(w) {
	mPos = $('logos2').cumulativeOffset();
	mPosL = mPos[0];
	mPosT = mPos[1];
	$('nodisp').visibilityV();
	$('nodisp').style.left = (w == 1) ? mPosL + 50 + 'px' : mPosL + 600 + 'px';
	$('nodisp').style.top = mPosT - 85 + 'px';	
}

function hidenoDisp() {
	$('nodisp').visibilityH();
}

function init(url) {
	if (url!='BLANK') {
		//temporal
		if (url=='LABEL' || url=='TYDA') {
			id = (url=='LABEL') ? 'nodisp_lab' : 'nodisp_tyd';
			$(id).setOpacity(0.95);
			dimen = $(id).getDimensions();
			w = ((dimen.width/2)*(-1)) + 'px';
			h = ((dimen.height/2)*(-1)) + 'px';
			$(id).setStyle({
				left:'50%',
				top:'50%',
				marginLeft:w,
				marginTop:h,
				visibility:'visible'
			});
			setTimeout("mostrarInfoWeb('')",15000);
			setTimeout("$('"+id+"').visibilityH()",15000);
		}
		else 
			mostrarIframe(url);	
	}
	else {
		$('logos').style.display = 'inline';
		$('logos_2').style.display = 'inline';
		$('login_bkg').style.display = 'inline';
		$('login_form').style.display = 'inline';
		document.form1.IBIC_user.focus();
	}
}

function validarusr() {
	var mURL = '/custom/php/validaUsr.php';
	var usuario = $('IBIC_user').value;
	var emp = $('IBIC_emp').value;
	var pass = $('IBIC_pass').value;
	new Ajax.Request(mURL,
		{
		method:'post',
		parameters: 'usuario='+usuario+'&emp='+emp+'&pass='+pass+'&gemp='+gemp,
		onSuccess: function(transport){
		var response = transport.responseText;
		response = response.strip();
		if (response=='session_ok') {
			submita();
			lastForm = '';
			toUpd = '<div align="center" class="txt1">Datos de usuario correctos<br/>Intranet lanzada en ventana nueva';
			toUpd = toUpd+'<br/><br/><input style="width:100px" type="button" value="Mostrar validaci&oacute;n"/ onclick="seeValForm();" class="ininput"></div>';
			$('login_form').update(toUpd);
		}
		else {
			alert("Error en los datos de usuario")
			$('IBIC_user').style.border = '1px solid #c00000';
			$('IBIC_pass').style.border = '1px solid #c00000';
			$('IBIC_emp').style.border = '1px solid #c00000';
			}
	},
		onFailure: function(){ alert('Error en la comprobación de usuario') }   
	});
}

function seeValForm() {
	var fecha=new Date();
	toUpd = '<div class="txt1">&nbsp;Validaci&oacute;n de usuario</div>';
	toUpd = toUpd + '<div class="txt2 m1" style="padding-left:5px">Usuario</div>';
	toUpd = toUpd + '<div style="padding-left:5px">';
	toUpd = toUpd + '<input type="text" class="txt2 w300" name="IBIC_user" id="IBIC_user" onchange="this.value=this.value.toUpperCase()"  onkeypress="iSubmitEnter(event, document.form1)"/>';
	toUpd = toUpd + '<input type="hidden" value="'+CGIPATH+'?IBIF_ex=arseg002&amp;RANDOM='+fecha.toGMTString()+'" name="WF_SIGNON_MESSAGE" />';
	toUpd = toUpd + '</div>';
	toUpd = toUpd + '<div class="txt2 m1" style="padding-left:5px">Clave</div>';
	toUpd = toUpd + '<div style="padding-left:5px"><input type="password" class="txt2 w300" id="IBIC_pass" name="IBIC_pass" value="" onkeypress="iSubmitEnter(event, document.form1)"/></div>';
	toUpd = toUpd + '<div class="txt2 m1" style="padding-left:5px">Identificador</div>';
	toUpd = toUpd + '<div style="padding-left:5px"><input type="text" class="txt2 w300" id="IBIC_emp" name="IBIC_emp" value=""  onkeypress="iSubmitEnter(event, document.form1)"/></div>';
	toUpd = toUpd + '<div style="text-align:right"><input type="button" onclick="validarusr()" name="Submit" value="Validar" class="ininput" /></div>';
	$('login_form').update(toUpd);
}