var tamanoAncho=2;
var tamanoAlto=2;
var TamanoFuente=1;
var EstadoCapas="Off";

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();


/* <![CDATA[ */
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function openwindowGaleria(id){
	window.open('/popup.php?id='+id,'Imagen'+id,'resizable=yes,menubar=no,toolbar=no,scrollbars=yes,width=468,height=300,top=50,left=50');
}

function openwindowNoticia(id){
	window.open('/popup.php?id='+id,'Imagen'+id,'resizable=yes,menubar=no,toolbar=no,scrollbars=yes,width=468,height=300,top=50,left=50');
}

function openwindowProducto(id){
	window.open('/tienda/popup.php?id='+id,'Imagen'+id,'resizable=yes,menubar=no,toolbar=no,scrollbars=yes,width=468,height=300,top=50,left=50');
}


/*-----------------------------------------------------------------------------------------*/
			
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-zñA-Z\d_-]+(\.[a-zñA-Z\d_-]+)*$/;
	var patDomainIP = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	var patDomain = /^[a-zñA-Z\d_-]+(\.[a-zñA-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 validarEmail(valor) {
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
    return (true)
  } else {
    alert("La dirección de Email es incorrecta.");
    return (false);
  }
}

function restrictLetraNum(idElementoPasarFoco){//funcion para onKeypress
	//if (event.keyCode == 13) document.getElementById(idElementoPasarFoco).focus();
	//Sólo numerico
	//if (event.keyCode < 48) event.returnValue = false;
	//if (event.keyCode > 57 && event.keyCode < 65) event.returnValue = false;
	//if (event.keyCode > 90 && event.keyCode < 97) event.returnValue = false;
	//if (event.keyCode > 122) event.returnValue = false;
}

function validarEntero(valor){
	if(valor!=0){
		valor = parseInt(valor);
		if (isNaN(valor)) {
			return "";
		}
	}
	return valor;
}

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 dateToIngles(strFecha){
	//Esta funcion cambia el formato dd/mm/yyyy por yyyy/mm/dd
	//Es util para comparar fechas como cadenas (para ver cual es mayor o menor)
	var arrayFecha = strFecha.split("/");
	var dia =  arrayFecha[0];
	var mes =  arrayFecha[1];
	var anio =  arrayFecha[2];
	return anio + "/" + mes + "/" + dia;
}
/* ]]> */

function MostrarSubmenu(IdMenu) {
	document.getElementById('SubMenu'+IdMenu).style.display = 'block';
	document.getElementById(IdMenu).style.backgroundColor = '#e8e8e8';
	document.getElementById(IdMenu).style.color ='#002b6c';
}

function OcultarSubmenu(IdMenu) {
	document.getElementById('SubMenu'+IdMenu).style.display = 'none';
	document.getElementById(IdMenu).style.backgroundColor = '#3e0000';
	document.getElementById(IdMenu).style.backgroundColor = '#001D3E';
	document.getElementById(IdMenu).style.color ='#fff';	
}

function AumentarFuente() {
	if (TamanoFuente<1.2) {
		TamanoFuente=TamanoFuente+0.1;
		document.getElementById('ColumnaCentral').style.fontSize=TamanoFuente+"em";
	}
}

function ReducirFuente() {
	if (TamanoFuente>1) {
		TamanoFuente=TamanoFuente-0.1;
		document.getElementById('ColumnaCentral').style.fontSize=TamanoFuente+"em";
	}
}

function charactermax(id,idreturn){
	var longit = document.getElementById(id).value.length;
	if(!longit) {longit = '0';}
	document.getElementById(idreturn).innerHTML = longit;

	if (longit>2500) {
		document.getElementById('Contador').style.color= '#990000';
		document.getElementById('Contador').style.fontWeight= 'bold';
	}else{
		if (longit>2200 && longit<2500) {
			document.getElementById('Contador').style.color= '#ff6000';
		}else{
			document.getElementById('Contador').style.color= '#999999';
		}
		document.getElementById('Contador').style.fontWeight= 'normal';
	}
}


function CorregirFormulario(elemento,Variable) {
	if (document.getElementById(elemento).value==Variable) {
		document.getElementById(elemento).value="";
	}
}


/* FUNCIONES PARA POPUPS POR CAPAS */
function VerCreciente(NombreCapa, ancho, alto) {
	if (EstadoCapas=="Off") {
		document.getElementById(NombreCapa).style.display = "block";
		redibujarCreciente(NombreCapa,ancho, alto);	
		tamanoAncho=20;
		tamanoAlto=11;	
	}
}

function redibujarCreciente(NombreCapa2, ancho, alto) {
	if (tamanoAncho<ancho || tamanoAlto<alto)  {
		if (tamanoAncho<ancho)  {
			tamanoAncho=tamanoAncho+20;
			document.getElementById(NombreCapa2).style.width = tamanoAncho+"px";
			document.getElementById(NombreCapa2).style.marginLeft = ((385-(tamanoAncho/2))+10)+"px";		
		}
		if (tamanoAlto<alto) {
			tamanoAlto=tamanoAlto+10;
			document.getElementById(NombreCapa2).style.height = tamanoAlto+"px";
			document.getElementById(NombreCapa2).style.marginTop = (200-(tamanoAlto/2))+"px";		
		}
		setTimeout("redibujarCreciente('"+NombreCapa2+"',"+ancho+", "+alto+")",5);
	}else{
		document.getElementById('Contenido'+NombreCapa2).style.display='block';
		EstadoCapas="On";
	}
}

function OcultarMenguante(NombreCapa) {
	if (EstadoCapas=="On") {
		document.getElementById('Contenido'+NombreCapa).style.display='none';
		redibujarMenguante(NombreCapa);
	}
}

function redibujarMenguante(NombreCapa2) {
	if (tamanoAncho>21 && tamanoAlto>11)  {
		tamanoAncho=tamanoAncho-20;
		document.getElementById(NombreCapa2).style.width = tamanoAncho+"px";
		document.getElementById(NombreCapa2).style.marginLeft = ((385-(tamanoAncho/2))+10)+"px";		
		tamanoAlto=tamanoAlto-10;
		document.getElementById(NombreCapa2).style.height = tamanoAlto+"px";
		document.getElementById(NombreCapa2).style.marginTop = (200-(tamanoAlto/2))+"px";		
		setTimeout("redibujarMenguante('"+NombreCapa2+"')",5);
	}else {
		tamanoAncho=2;
		tamanoAlto=2;
		document.getElementById(NombreCapa2).style.display = "none";
		EstadoCapas="Off";
	}
}



/* FUNCION CARRUSELL EMPRESAS */

	
function CarrusellProductos (NombreElementoAFundir) {
	//alert("Total: " + NumElementos+"\nContador: "+contador)
	if (contador<NumElementos-1) {
		contador++;
	}else{
		contador=0;
	}
	setTimeout("AlphaOut(100,"+contador+",'"+NombreElementoAFundir+"')",100);
}

function AlphaOut(NivelTransparencia,contador,NombreElementoAFundir) {
	imgobj=document.getElementById(NombreElementoAFundir);
	if (NivelTransparencia>0) {
		if (BrowserDetect.browser=="Firefox") {
			imgobj.style.MozOpacity=NivelTransparencia/100;
		} else {
			if (BrowserDetect.browser=="Explorer") {
				imgobj.style.filter = "alpha(opacity=" + NivelTransparencia + ")";
			} else {
				imgobj.style.opacity = NivelTransparencia/100; 
			}
		}
		NivelTransparencia-=10;
		setTimeout("AlphaOut("+NivelTransparencia+","+contador+",'"+NombreElementoAFundir+"')",100);
	}else{
		if (BrowserDetect.browser=="Firefox") {
			imgobj.style.MozOpacity=0;
		} else {
			if (BrowserDetect.browser=="Explorer") {
				imgobj.style.filter = "alpha(opacity=0)";
			} else {
				imgobj.style.opacity = 0; 
			}
		}
		document.getElementById('FotoProductos').src = '/img/'+Fotos[contador];		
		document.getElementById('EnlaceProductos').href = Enlaces[contador];
		document.getElementById('EnlaceProductosFoto').href = Enlaces[contador];
		document.getElementById('EnlaceProductos').innerHTML = Nombres[contador];
		document.getElementById('PrecioProductos').href = Enlaces[contador];			
		document.getElementById('PrecioProductos').innerHTML = Precios[contador];
		if (BrowserDetect.browser=="Firefox") {
			imgobj.style.MozOpacity=0;
		} else {
			if (BrowserDetect.browser=="Explorer") {
				imgobj.style.filter = "alpha(opacity=0)";
			} else {
				imgobj.style.opacity = 0; 
			}
		}		
		setTimeout("AlphaIn(0,'"+NombreElementoAFundir+"')",100);	
	}
}

function AlphaIn(NivelTransparencia,NombreElementoAFundir) {
	imgobj=document.getElementById(NombreElementoAFundir);
	if (NivelTransparencia<=90) {
		if (BrowserDetect.browser=="Firefox") {
			imgobj.style.MozOpacity=NivelTransparencia/100;
		} else {
			if (BrowserDetect.browser=="Explorer") {
				imgobj.style.filter = "alpha(opacity=" + NivelTransparencia + ")";
			} else {
				imgobj.style.opacity = NivelTransparencia/100; 
			}
		}
		NivelTransparencia+=10;
		setTimeout("AlphaIn("+NivelTransparencia+",'"+NombreElementoAFundir+"')",100);
	}else{
		if (BrowserDetect.browser=="Firefox") {
			imgobj.style.MozOpacity=1;
		} else {
			if (BrowserDetect.browser=="Explorer") {
				imgobj.style.filter = "alpha(opacity=100)";
			} else {
				imgobj.style.opacity = 1; 
			}
		}	
		setTimeout("CarrusellProductos('"+NombreElementoAFundir+"')",VelocidadCarrusel);	
	}
}

	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-61)+'px';
	   document.getElementById('CajaEnMapa').style.left= (x+10)+'px';  
	}
	function MostrarInformacion(LineaTexto) {
		document.getElementById('CajaEnMapa').style.display = 'block';
		document.getElementById('TextoCajaEnMapa').innerHTML  = Textos[LineaTexto];
	}
	function OcultarInformacion() {
		document.getElementById('CajaEnMapa').style.display = 'none';
	}	
	
	function MostrarContenidos(IdCaja) {
		if (document.getElementById("Contenidos"+IdCaja).style.display == 'block') {
			document.getElementById("Contenidos"+IdCaja).style.display = 'none';
		} else {
			var mylist = document.getElementById("ListadoDeCajasContenidos")
			var listitems= mylist.getElementsByTagName('div')
			for (i=0; i<listitems.length; i++) {
				listitems[i].style.display="none";
			}		
			document.getElementById("Contenidos"+IdCaja).style.display = 'block';
		}
	}
	
	function MeOculto(IdAOcultar) {
		document.getElementById(IdAOcultar).style.display = 'none';
	}
	
	function MostrarContenidosEXPO(IdCaja) {
		if (document.getElementById("Contenidos"+IdCaja).style.display == 'block') {
			document.getElementById("Contenidos"+IdCaja).style.display = 'none';
		} else {
			var mylist = document.getElementById("ListadoDeCajasContenidos")
			var listitems= mylist.getElementsByTagName('div')
			for (i=0; i<listitems.length; i++) {
				listitems[i].style.display="none";
			}		
			document.getElementById("Contenidos"+IdCaja).style.display = 'block';
		}
	}
	
	function MostrarInformacionEXPO(LineaTexto) {
		document.getElementById('CajaEnMapa').style.display = 'block';
		document.getElementById('TextoCajaEnMapa').innerHTML  = '<strong>' + document.getElementById(LineaTexto).alt + '</strong><br /> Pulsa para ver contenidos';
	}
