var ua = navigator.userAgent.toLowerCase();
function detect(text) {
   stringposition = ua.indexOf(text) + 1;
   data = text;
   return stringposition;
}

function ir(Ruta) {
	parent.top.location = Ruta;
}		
			
/* ONBLUR DESDE JAVASCRIPT DE TODOS LOS FORMULARIOS. */			

function MarcadoresDeFormularios() {

	var inputes=document.getElementsByTagName('input');
	
	var textareas=document.getElementsByTagName('textarea');
	
	var selectes=document.getElementsByTagName('select');
	
	
	
	var numElementosInput = inputes.length;
	
	var numElementosTextareas = textareas.length;
	
	var numElementosSelect = selectes.length;
	
	
	for (contador=0; contador<numElementosInput; contador++) {
		if (inputes[contador].type=='radio' || inputes[contador].type=='checkbox') {
			inputes[contador].style.backgroundImage= "none";
			inputes[contador].style.border="0px"
		}
		
		if (inputes[contador].className!='Boton' && inputes[contador].type!='radio' && inputes[contador].type!='checkbox') {
			inputes[contador].onfocus= function() { this.style.border= '1px solid #00a7ed';}
			inputes[contador].onblur= function() { this.style.border= '1px solid #c0c0c0';}
		}
	}
	for (contador=0; contador<numElementosTextareas; contador++) {
		textareas[contador].onfocus= function() { this.style.border= '1px solid #00a7ed';}
		textareas[contador].onblur= function() { this.style.border= '1px solid #c0c0c0';}
	}		
	for (contador=0; contador<numElementosSelect; contador++) {
		selectes[contador].onfocus= function() { this.style.border= '1px solid #00a7ed';}
		selectes[contador].onblur= function() { this.style.border= '1px solid #c0c0c0';}
	}			
}



function position(event){
   var x = event.clientX+document.body.scrollLeft;
   var y = event.clientY+document.body.scrollTop+document.documentElement.scrollTop;
   document.getElementById('CajaEnMapa').style.top= (y-75)+'px';
   document.getElementById('CajaEnMapa').style.left= (x-110)+'px';  
}
function MostrarInformacion(IDPadre) {
	document.getElementById('CajaEnMapa').style.display = 'block';
	document.getElementById('TextoCajaEnMapa').innerHTML  = document.getElementById(IDPadre).alt;
}
function OcultarInformacion() {
	document.getElementById('CajaEnMapa').style.display = 'none';
}



function AccionMenu(IdPadre) {
	var NumMenus=4;
	for (i=1;i<=NumMenus; i++) {
		document.getElementById('SubMenu'+i).style.display='none';
		if ('Menu'+i==Estado) {
			document.getElementById('Menu'+i).className='ConPuntero';
		}else{
			document.getElementById('Menu'+i).className='';
		}
	}
	if (document.getElementById('Sub'+IdPadre).style.display!='block') {
		document.getElementById('Sub'+IdPadre).style.display='block';
		if (IdPadre==Estado) {
			document.getElementById(IdPadre).className='Activo ConPuntero';
		}else{
			document.getElementById(IdPadre).className='Activo';		
		}
	}else{
		document.getElementById('Sub'+IdPadre).style.display='none';
		if (IdPadre==Estado) {
			document.getElementById(IdPadre).className='ConPuntero';
		}else{
			document.getElementById(IdPadre).className='';		
		}
	}
}
/*-----------------------------------------------------------------------------------------*/
			
function CheckEmailAddress(value){
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(value)){
		return true;
	}else{
		return false;
	}
}

function VerificarMail(strMail) {
//La siguiente funcion da error si se introduce algo.usuario@dominio.es
	var patMail = /^(.+)@(.+)$/;
	var patUser = /^[a-zA-Z\d_-]+(\.[a-zA-Z\d_-]+)*$/;
	var patDomainIP = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	var patDomain = /^[a-zA-Z\d_-]+(\.[a-zA-Z\d_-]+)+$/;
	var matchArray = strMail.match(patMail);
	if(matchArray == null){return false;}
	var user = new String(matchArray[1]);
	var domain = new String(matchArray[2]); 
	if(user.match(patUser) == null){return false;}
	var IPArray = domain.match(patDomainIP);
	if(IPArray != null){ 
		 for (var i=1;i<=4;i++) {
		  if(IPArray[i]>255){return false;}
		 }
		 return true; 
	}
	var domainArray = domain.match(patDomain);
	if(domainArray == null) {return false;}
	if(domainArray[domainArray.length - 1].length < 3 || domainArray[ domainArray.length - 1].length > 5) { return false;}
	return true;
}

function validarFloat(valor){
	if(valor!=0){
		valor = parseFloat(valor);
		if (isNaN(valor)) {
			return "";
		}
	}
	return valor;
}
function Redondear(x, places){
	return Math.round(x * Math.pow(10, places)) / Math.pow(10, places);
}
function str_replace(haystack, needle, replacement) {
	if(haystack.indexOf(needle) != -1) {
		var temp = haystack.split(needle);
		return temp.join(replacement);
	}
	return haystack;
}

function ConfirmarSalir(Ruta) {
	if (confirm("¿Está seguro de que desea cerrar la sesión?")){
		document.location.href = Ruta + "salir.php";
	}
}

function ConfirmarBaja(Ruta) {
	if (confirm("¿Está seguro de que desea darse de baja?")){
		document.location.href = Ruta + "baja.php";
	}
}

function charactermax(id, idreturn, idcontador, longtotal){
	var longit = document.getElementById(id).value.length;
	if(!longit) {longit = '0';}
	document.getElementById(idreturn).innerHTML = longit + '';

	if (longit>longtotal) {
		document.getElementById(idcontador).style.color= '#990000';
		//document.getElementById(idcontador).style.fontWeight= 'bold';
	}else{
		if (longit>longtotal && longit<longtotal) {
			document.getElementById(idcontador).style.color= '#ff6000';
		}else{
			document.getElementById(idcontador).style.color= '#999999';
		}
		//document.getElementById(idcontador).style.fontWeight= 'normal';
	}
}

/*---------------------------------------------------------------------*/

/* FUNCION QUE MUESTRA UN CUBREPAGINAS. */
/* ---- Abel Tena - 26 marzo 2009 ---- */

function mostrarFoto(NombreFoto,TextoFoto){
	if (detect('msie')){ 	
		var ScrollDePantalla =  window.document.documentElement.scrollTop;
		var TotalPantalla =  window.document.documentElement.scrollBottom;
	}else{
		var ScrollDePantalla =  window.pageYOffset;	
	}	
	document.getElementById('FotoPopUp').src=NombreFoto;
	document.getElementById('FotoPopUp').alt=TextoFoto;
	if (TextoFoto!="") {
		document.getElementById('TextoFotoPopUp').style.display = 'block';
	}
	document.getElementById('TextoFotoPopUp').innerHTML=TextoFoto;
	document.getElementById('Cubrepaginas').style.marginTop=(ScrollDePantalla)+"px";	
	document.getElementById('Cubrepaginas').style.display = 'block';
	
	if (detect('msie')){ 
		document.getElementById('CuerpoPrincipal').scroll = 'no';
		document.getElementById('Cubrepaginas').style.filter= "alpha(opacity=85)";
	}else{	
		document.getElementById('Cubrepaginas').style.opacity = '0.85';
		document.getElementById('CuerpoPrincipal').style.overflow = 'hidden';
	}	
	
	document.getElementById('PopUpFoto').style.display = 'block';	
	centradoEnPagina();
}

function ocultarCubrepaginasFoto(){
	document.getElementById('FotoPopUp').src="/img/Loader.gif";
	document.getElementById('FotoPopUp').alt="";
	document.getElementById('Cubrepaginas').style.display = 'none';
	document.getElementById('PopUpFoto').style.display = 'none';
	document.getElementById('TextoFotoPopUp').style.display = 'none';
	document.getElementById('EnlaceFoto').style.marginTop= "0px";
	
	if (detect('msie')){ 
		document.getElementById('CuerpoPrincipal').scroll = 'yes';	
	}else{	
		document.getElementById('CuerpoPrincipal').style.overflow = 'auto';
	}
	
}

function centradoEnPagina() {
	setTimeout("CalcularCentradodeFoto()",1400);
}

function CalcularCentradodeFoto() {
	var altoImagen = document.getElementById('FotoPopUp').clientHeight;
	if (detect('msie')){ 	
		var altoDePantalla = document.body.clientHeight;
		var altoDePantalla = document.documentElement.clientHeight;
		var ScrollDePantalla =  window.document.documentElement.scrollTop;
	}else{
		var altoDePantalla =  window.innerHeight;	
		var ScrollDePantalla =  window.pageYOffset;	
	}

	if (altoDePantalla-altoImagen>0) {
		var margenImagen = ((altoDePantalla-altoImagen)/2);
	}else{
		var margenImagen = 0;
	}
	var espaciadoSuperior=parseInt(ScrollDePantalla)+parseInt(margenImagen)-20;
	document.getElementById('PopUpFoto').style.marginTop= ""+espaciadoSuperior+"px";
}
