/* $Id: funciones.js 136 2007-07-02 10:16:50Z svn $ */
/* $Date: 2007-07-02 11:16:50 +0100 (Mon, 02 Jul 2007) $ */
/* $Rev: 136 $ */
function tellerror(msg, url, linenumber){
alert('Error message= '+msg+'\nURL= '+url+'\nLine Number= '+linenumber)
return true
}

/* Pone un valor a una variable y envia el formulario */
/* Parametros : formulario, campo a asignar el valor, valor a asignar, pagina a donde enviar los datos. */

function enviarFormulario(formulario, campo, valor, destino) {
	document.forms[formulario].action = destino;
	document.forms[formulario].elements[campo].value = valor;
	document.forms[formulario].submit();

}

/* Asigna un valor a una variable */
/* Parametros: formulario, campo a asignar el valor, valor a asignar */
function asignaValor(formulario, campo, valor){
	document.forms[formulario].elements[campo].value = valor;
}

 function validarEmail(valor) {
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
   return (true)
  } else {
   return (false);
  }
 }

 /*
function validarEmail(emailStr) {
alert("Longitud");
   if (emailStr.length == 0){
     return false;
   }
   var emailPat=/^(.+)@(.+)$/;
   var specialChars="()<>@,;:\".[]";
   var validChars="[^s" + specialChars + "]";
   var quotedUser="(\"[^\"]*\")";
   var ipDomainPat=/^(d{1,3})[.](d{1,3})[.](d{1,3})[.](d{1,3})$/;
   var atom=validChars + '+';
   var word="(" + atom + "|" + quotedUser + ")";
   var userPat=new RegExp("^" + word + "(." + word + ")*$");
   var domainPat=new RegExp("^" + atom + "(." + atom + ")*$");
   var matchArray=emailStr.match(emailPat);

alert("matchArray");
   if (matchArray == null){
      return false;
   }

   var user=matchArray[1];
   var domain=matchArray[2];

alert("user.match");
alert(user + ' -> ' + userPat)
   if (user.match(userPat) == null){
      return false;
   }

   var IPArray = domain.match(ipDomainPat);
alert("IP");
   if (IPArray != null) {
      for (var i = 1; i <= 4; i++) {
         if (IPArray[i] > 255){
         	return false;
         }
   		}

   return true;
	 }

   var domainArray=domain.match(domainPat);
alert("domainArray");
   if (domainArray == null){
      return false;
   }

   var atomPat=new RegExp(atom,"g");
   var domArr=domain.match(atomPat);
   var len=domArr.length;

alert("domArr");
   if (domArr[domArr.length-1].length < 2 || domArr[domArr.length-1].length > 3){
      return false;
   }

alert("len");
   if (len < 2){
      return false;
   }

   return true;
}
*/

/* Funcion que cambia la clase css de un elemento */
/* Parametros: idRef */
var identificadorActual = '';
var identificadorNuevo = '';
var claseNormal = '';
var claseMarcada = '';
var claseIntermedia = '';




function establecerValores(idNuevo, claseN, claseM, claseI){
	identificadorNuevo = idNuevo;
	claseNormal = claseN;
	claseMarcada = claseM;
	if(identificadorNuevo != ''){
		 document.getElementById(identificadorNuevo).className = 'destacadoVerde';
	}
	
}

function cambiarClaseCss(){
		if(identificadorActual != ''){
			document.getElementById(identificadorActual).className = claseNormal;
		}
		if(identificadorNuevo != ''){
			document.getElementById(identificadorNuevo).className = claseMarcada;
		}
		identificadorActual = identificadorNuevo;
}
/* Funciones para AJAX */
var procesando = false;
var hex=255; // Initial color value.
var bajar=true;
var idCapa=null;
var familiaFoto = null;
var recargarFotoFamilia = true;

function fadetext(){
	if(procesando && document.getElementById("procesandoPeticion") != null && document.getElementById("procesandoPeticion") != undefined){ 
		if(bajar) { //If color is not black yet
			hex-=6; // increase color darkness
			document.getElementById("procesandoPeticion").style.color="rgb("+hex+","+hex+","+hex+")";
			if(hex<0){
				bajar=false;
			}
		}else{
			hex+=11; // increase color darkness
			document.getElementById("procesandoPeticion").style.color="rgb("+hex+","+hex+","+hex+")";
			if(hex>255){
				bajar=true;
			}
		}
		setTimeout("fadetext()",20);
	}
}

function fadetext2(){
	if(procesando){ 
		if(bajar) { //If color is not black yet
			hex-=6; // increase color darkness
			document.getElementById(identificadorNuevo).style.color="rgb("+hex+","+hex+","+hex+")";
			if(hex<0){
				bajar=false;
			}
		}else{
			hex+=11; // increase color darkness
			document.getElementById(identificadorNuevo).style.color="rgb("+hex+","+hex+","+hex+")";
			if(hex>255){
				bajar=true;
			}
		}
		setTimeout("fadetext2()",20);
	} /* else {
	  
	  if(identificadorActual!=""){
			document.getElementById(identificadorActual).style.value = "";
		}
		cambiarClaseCss();	
		
	}*/
}

function stateChanged(){ 
	if (xmlHttp.readyState==4){ 
		document.getElementById(idCapa).innerHTML=xmlHttp.responseText;
		idCapa = null;
		procesando = false;
		bajar=true;
		hex=255;
		cambiarClaseCss();	
	}
}

function GetXmlHttpObject(){
	var xmlHttp=null;
	try{
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	}catch (e){
	  // Internet Explorer
	  try{
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }catch (e){
	    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	}
	return xmlHttp;
}

/* Funcion para cambiar las fotos de la familia*/
var ultimaPos = 0;
var primera = true;
var primeraVez = true;
var arrayFotos = Array();


function cambiaFoto(){
		document.getElementById('fotoFamilia1').display=true;
		document.getElementById('fotoFamilia2').display=true;
		recargarFotoFamilia=false;
		if(primera==true){
//alert('1'+' length: '+arrayFotos.length+' ultimaPos: '+ultimaPos);			;
			if(ultimaPos+1 > arrayFotos.length){
				ultimaPos = 0
			}
//alert('1 UltimaPos'+ultimaPos);
			document.getElementById('fotoFamilia1').src = arrayFotos[ultimaPos]['url'];
			document.getElementById('fotoFamilia1').width = arrayFotos[ultimaPos]['medidas']['ancho'];
			document.getElementById('fotoFamilia1').height = arrayFotos[ultimaPos]['medidas']['alto'];
			ultimaPos++;
			primera=!primera;
		} else {
//alert('2'+' length: '+arrayFotos.length+' ultimaPos: '+ultimaPos);
			if(ultimaPos+1 > arrayFotos.length){
				ultimaPos = 0
			}
//alert('2 UltimaPos'+ultimaPos);
			document.getElementById('fotoFamilia2').src = arrayFotos[ultimaPos]['url'];
			document.getElementById('fotoFamilia2').width = arrayFotos[ultimaPos]['medidas']['ancho'];
			document.getElementById('fotoFamilia2').height = arrayFotos[ultimaPos]['medidas']['alto'];
			ultimaPos++;
			primera=!primera;
			primeraVez=false;
		}
		if(primeraVez){
//alert('3'+' length: '+arrayFotos.length+' ultimaPos: '+ultimaPos);
			if(ultimaPos+1 > arrayFotos.length){
				ultimaPos = 0
			}
//alert('3 UltimaPos'+ultimaPos);
			document.getElementById('fotoFamilia2').src = arrayFotos[ultimaPos]['url'];
			document.getElementById('fotoFamilia2').width = arrayFotos[ultimaPos]['medidas']['ancho'];
			document.getElementById('fotoFamilia2').height = arrayFotos[ultimaPos]['medidas']['alto'];
			ultimaPos++;
			primera=!primera;
			primeraVez=false;
		}
		
		setTimeout('cambiaFoto()', 2000);	
									
}	

/* Funcion para cambiar las noticias de la portada*/
var ultimaPosNoticia = 0;
var arrayNoticias = Array();
var fotomostrada = '' // Foto que se muestra

var flagTrasparenteNoticia = true;
var opacidadIE = 100;
var opacidadF = 1;
var flagPrimeraVez = true; 

/**
 * funcion que se usa para cambiar las noticias
 */
function cambiaNoticia(){
	
	if(!flagPrimeraVez){
		//quitar
		opacidadNoticias();
		document.getElementById('linkNoticia').style.visibility='hidden';
		document.getElementById('fotoNoticia').style.visibility='hidden';
		document.getElementById('entradillaNoticia').style.visibility='hidden';
		pausa(1000);
	}
	document.getElementById('linkNoticia').innerHTML = arrayNoticias[ultimaPosNoticia]['titular'];
	//document.getElementById('entradillaNoticia').innerHTML = arrayNoticias[ultimaPosNoticia]['entradilla'];
	document.getElementById('entradillaNoticia').innerHTML = arrayNoticias[ultimaPosNoticia]['cuerpo'];
	document.getElementById('linkNoticia').href = 'news.php?nid='+arrayNoticias[ultimaPosNoticia]['noticiaId'];
	document.getElementById('linkNoticiaCuerpo').href = 'news.php?nid='+arrayNoticias[ultimaPosNoticia]['noticiaId'];
	document.getElementById('entradillaNoticia').className = 'txt-noticiaPortada';
	
	if(arrayNoticias[ultimaPosNoticia]['foto'] != ''){
		document.getElementById('fotoNoticia').display = true;
		document.getElementById('fotoNoticia').src = arrayNoticias[ultimaPosNoticia]['foto'];
		fotomostrada = arrayNoticias[ultimaPosNoticia]['foto'];
		document.getElementById('fotoNoticia').width = arrayNoticias[ultimaPosNoticia]['medidas']['ancho'];
		document.getElementById('fotoNoticia').height = arrayNoticias[ultimaPosNoticia]['medidas']['alto'];
	}else{
		fotomostrada = '';
		document.getElementById('fotoNoticia').display = false;
	}
	
	ultimaPosNoticia++;

	if(ultimaPosNoticia >= arrayNoticias.length){
		ultimaPosNoticia = 0
	}
	if(!flagPrimeraVez){
		//poner
		opacidadNoticias();
		pausa(1000);
	}
	if(fotomostrada == ''){
		document.getElementById('fotoNoticia').style.visibility='hidden';
	}
	flagPrimeraVez = false;	
	setTimeout('cambiaNoticia()', 10000);	
								
}	


// Controla la opacidad de las noticias en la portada principal

function opacidadNoticias(){
	if(flagTrasparenteNoticia==true) { 
		//desaparece
		opacidadIE-=4; 
		opacidadF-=.15;
		//titular
		document.getElementById("opacidadNoticia1").style.filter="alpha(opacity="+opacidadIE+")";
		document.getElementById("opacidadNoticia1").style.mozOpacity=" "+opacidadF+" ";
		document.getElementById("opacidadNoticia1").style.opacity=" "+opacidadF+" ";
		//foto+entradilla
		document.getElementById("opacidadNoticia2").style.filter="alpha(opacity="+opacidadIE+")";
		document.getElementById("opacidadNoticia2").style.mozOpacity=" "+opacidadF+" ";
		document.getElementById("opacidadNoticia2").style.opacity=" "+opacidadF+" ";
		
		if(opacidadIE<0){
			flagTrasparenteNoticia=false;
			opacidadF=0;
			return;
		}
	} else {
		// aparece
		opacidadIE+=4;
		opacidadF+=.15;
		//titular
		document.getElementById("opacidadNoticia1").style.filter="alpha(opacity="+opacidadIE+")";
		document.getElementById("opacidadNoticia1").style.mozOpacity=" "+opacidadF+" ";
		document.getElementById("opacidadNoticia1").style.opacity=" "+opacidadF+" ";
		//foto+entradilla
		document.getElementById("opacidadNoticia2").style.filter="alpha(opacity="+opacidadIE+")";
		document.getElementById("opacidadNoticia2").style.mozOpacity=" "+opacidadF+" ";
		document.getElementById("opacidadNoticia2").style.opacity=" "+opacidadF+" ";
		
		
		document.getElementById('linkNoticia').style.visibility='visible';
		document.getElementById('fotoNoticia').style.visibility='visible';
		document.getElementById('entradillaNoticia').style.visibility='visible';
		if(fotomostrada == ''){
			document.getElementById('fotoNoticia').style.visibility='hidden';
		}
		if(opacidadIE>99){
			flagTrasparenteNoticia=true;
			opacidadF=1;
			return;
		}
	}
	setTimeout("opacidadNoticias()",40);
}



/* Funciones para cambiar las ofertas de la portada */

var ultimaPosOferta = 0;
var arrayOfertas = Array();
var fotomostradaOferta = ''; // Foto que se muestra
var textoFinExistencia = ''; // Texto para el fin de existencias

var flagTrasparenteOferta = true;
var opacidadOfertaIE = 100;
var opacidadOfertaF = 1;
var flagPrimeraVezOferta = true; 

/**
 * funcion que se usa para cambiar las noticias
 */
function cambiaOferta(){
	if(!flagPrimeraVezOferta){
		//quitar
		opacidadOfertas();
		document.getElementById('linkOferta').style.visibility='hidden';
		document.getElementById('fotoOferta').style.visibility='hidden';
		document.getElementById('entradillaOferta').style.visibility='hidden';
		pausa(1000);
	}
	document.getElementById('linkOferta').innerHTML = arrayOfertas[ultimaPosOferta]['titular'];
	document.getElementById('entradillaOferta').innerHTML = arrayOfertas[ultimaPosOferta]['descripcion'];
	document.getElementById('precioOferta').innerHTML = arrayOfertas[ultimaPosOferta]['pvp'];
	document.getElementById('existenciaOferta').innerHTML = textoFinExistencia;
	document.getElementById('linkOferta').href = 'oferta_ppal_detalle.php?tof=1&mof='+arrayOfertas[ultimaPosOferta]['ofertaId']+'&lof=-1';
	document.getElementById('linkOfertaCuerpo').href = 'oferta_ppal_detalle.php?tof=1&mof='+arrayOfertas[ultimaPosOferta]['ofertaId']+'&lof=-1';
	document.getElementById('entradillaOferta').className = 'txt-oferta';
	document.getElementById('precioOferta').className = 'txt-oferta';
	document.getElementById('existenciaOferta').className = 'txt-oferta';
	
	if(arrayOfertas[ultimaPosOferta]['foto'] != ''){
		document.getElementById('fotoOferta').display = true;
		document.getElementById('fotoOferta').src = arrayOfertas[ultimaPosOferta]['foto'];
		fotomostradaOferta = arrayOfertas[ultimaPosOferta]['foto'];
		document.getElementById('fotoOferta').width = arrayOfertas[ultimaPosOferta]['medidas']['ancho'];
		document.getElementById('fotoOferta').height = arrayOfertas[ultimaPosOferta]['medidas']['alto'];
		document.getElementById('fotoOferta').className = 'fotoPortadaOferta';
	}else{
		fotomostradaOferta = '';
		document.getElementById('fotoOferta').display = false;
		document.getElementById('fotoOferta').className = 'fotoPortadaOfertaHidden';
	}
	
	ultimaPosOferta++;

	if(ultimaPosOferta >= arrayOfertas.length){
		ultimaPosOferta = 0
	}
	if(!flagPrimeraVezOferta){
		//poner
		opacidadOfertas();
		pausa(1000);
	}
	if(fotomostradaOferta == ''){
		document.getElementById('fotoOferta').display = false;
		document.getElementById('fotoOferta').className = 'fotoPortadaOfertaHidden';
	}
	flagPrimeraVezOferta = false;	
	setTimeout('cambiaOferta()', 8000);	
								
}	


// Controla la opacidad de las ofertas en la portada principal

function opacidadOfertas(){
	if(flagTrasparenteOferta==true) { 
		//desaparece
		opacidadOfertaIE-=4; 
		opacidadOfertaF-=.15;
		//titular
		document.getElementById("opacidadOferta1").style.filter="alpha(opacity="+opacidadOfertaIE+")";
		document.getElementById("opacidadOferta1").style.mozOpacity=" "+opacidadOfertaF+" ";
		document.getElementById("opacidadOferta1").style.opacity=" "+opacidadOfertaF+" ";
		//foto+entradilla
		document.getElementById("opacidadOferta2").style.filter="alpha(opacity="+opacidadOfertaIE+")";
		document.getElementById("opacidadOferta2").style.mozOpacity=" "+opacidadOfertaF+" ";
		document.getElementById("opacidadOferta2").style.opacity=" "+opacidadOfertaF+" ";
		
		if(opacidadOfertaIE<0){
			flagTrasparenteOferta=false;
			opacidadOfertaF=0;
			return;
		}
	} else {
		// aparece
		opacidadOfertaIE+=4;
		opacidadOfertaF+=.15;
		//titular
		document.getElementById("opacidadOferta1").style.filter="alpha(opacity="+opacidadOfertaIE+")";
		document.getElementById("opacidadOferta1").style.mozOpacity=" "+opacidadOfertaF+" ";
		document.getElementById("opacidadOferta1").style.opacity=" "+opacidadOfertaF+" ";
		//foto+entradilla
		document.getElementById("opacidadOferta2").style.filter="alpha(opacity="+opacidadOfertaIE+")";
		document.getElementById("opacidadOferta2").style.mozOpacity=" "+opacidadOfertaF+" ";
		document.getElementById("opacidadOferta2").style.opacity=" "+opacidadOfertaF+" ";
		
		
		document.getElementById('linkOferta').style.visibility='visible';
		document.getElementById('fotoOferta').style.visibility='visible';
		document.getElementById('entradillaOferta').style.visibility='visible';
		if(fotomostrada == ''){
			//document.getElementById('fotoOferta').style.visibility='hidden';
			document.getElementById('fotoOferta').className = 'fotoPortadaOfertaHidden';
		}
		if(opacidadOfertaIE>99){
			flagTrasparenteOferta=true;
			opacidadOfertaF=1;
			return;
		}
	}
	setTimeout("opacidadOfertas()",40);
}



function pausa(tiempo){
	if(tiempo<=0){
		tiempo=1000;
	}
	var tiempoAux = new Date(new Date().getTime() + tiempo); 
	while (new Date() < tiempoAux) {
		
	}
}

/****************************************************************************/
/*************** FUNCIONES PARA LA CAPA DE CARGA DEL AJAX *******************/
/****************************************************************************/
var ie5 = (document.getElementById && document.all);
var ns6 = (document.getElementById && !document.all);


function SetUp(accion){

	if(ie5 || ns6) {
		if(accion == 'act'){
			//document.getElementById("load").style.visibility = "hidden";
			document.getElementById("load").style.width = '0px';
			document.getElementById("load").style.height = '0px'
			document.getElementById("load").className = "esconderCapaLoad";
		}else{
			//document.getElementById("load").style.visibility = "visible";
			document.getElementById("load").style.width = document.body.scrollWidth;
			document.getElementById("load").style.height = document.body.scrollHeight;
			document.getElementById("load").className = "mostrarCapaLoad";
		}
	}
}

/******************************************************************************/
/**************** FUNCIONES PARA EL ARBOL DE DIRECTORIOS **********************/
/******************************************************************************/
function gestCarpeta(carpeta){
	var obj = document.getElementById(carpeta);
	if(obj.className == '' ){
	//if(obj.style.visibility == 'visible'){
		obj.className = 'esconderCarpeta';
		//obj.style.visibility = 'hidden';
		//obj.display = 'none';
	}else{
		obj.className = '';
		//obj.style.visibility = 'visible';
		//obj.display = 'inline';
	}
}
