// JavaScript Document

//Inicia el temporizador para cambiar el consejo cada 5 segundos
function Cambio()
{
	setTimeout("CambiarConsejo()",5000);
}

//Selecciona el consejo y lo muestra
function MostrarConsejo()
{
	//Indice para comprobar que quedan consejos sin mostrar
	var i;
	
	//Obtener el nuevo consejo
	valor=Math.round((Math.random())*(aconsejos_mostrados.length-1));
	while (aconsejos_mostrados[valor]==1)  //Se obtiene un nuevo valor si ese consejo ya se había mostrado
		valor=Math.round((Math.random())*(aconsejos_mostrados.length-1));

	//El consejo se marca como mostrado
	aconsejos_mostrados[valor]=1;

	//Mostrar el consejo
	if (numConsejos > 0)
		document.getElementById("miConsejo").innerHTML = aconsejos[valor].toUpperCase();
	
	//Comprobar si se han mostrado todos los consejos, en ese caso habilitarlos para ser mostrados todos de nuevo
	i=0;
	while ((i< aconsejos_mostrados.length)&&(aconsejos_mostrados[i]==1))
		i++;

	//Si ya se han mostrado todos los consejos se ponen a 0 (como no mostrados)
	if (i==aconsejos_mostrados.length)
		for (var j=0; j < aconsejos_mostrados.length; j++) aconsejos_mostrados[j]=0;
}

//Cambia el consejo que se va mostrando
function CambiarConsejo()
{
	//Mostrar consejo
	MostrarConsejo();
	
	//Volver a lanzar el temporizador
	Cambio();
}

//Muestra los datos del consejo (la explicación del consejo y los productos aconsejados)
function VerConsejo()
{
	location.href = path + "Consejos/VerConsejo.asp?cod_consejo="+aconsejos_cod[valor];
}



//Según la opción se va a la página correspondiente
function Menu(opcion, subseccion)
{
	switch (opcion)
	{
		case "1":
			document.location.href = path + "inicio.asp";
			break;
		case "2":
			document.location.href = path + "Consejos/ListadoConsejos.asp";
			break;
		case "3":
			document.location.href = path + "catalogo/categoria.asp";
			break;
		case "4":
			document.location.href = path + "marcas/Marcas.asp";
			break;
		case "5":
			document.location.href = path + "ofertas/ofertas.asp";
			break;
		case "6":
			document.location.href = path + "Pedido/carrito.asp";
			break;
		case "7":
			document.location.href = path + "Clientes/registro1.asp";
			break;
		case "8":
			document.location.href = "mailto:info@farmadescuento.com?subject=Iinformacion";
			break;
		case "9":
			document.location.href = path + "info/presentacion.asp";
			break;
		case "10":
			document.location.href = path + "catalogo/categoria.asp?subcategoria=199";
			break;
		case "10":
			document.location.href = path + "catalogo/categoria.asp?subcategoria=199";
			break;
		case "11":
			document.location.href = path + "info/presentacion.asp#" + subseccion;
			break;
		case "avan":
			document.location.href = path + "busquedas/buscarAmpl.asp";
			break;
		case "lupa":
			Buscar();
			break;
		default:
	}
}

//Comprueba que no sean (espacios) todos los carácteres de la búsqueda
function ConBlancos(valortexto)
{
	var cont=0;
	for (var i=0;i < valortexto.length;i++)
	{
		if (valortexto.substr(i,1)==" ") cont+=1;
	}
	if (cont==valortexto.length) return (true) //Todos los carácteres son blancos
	else return (false)
}

//Comprueba si hay palabras a buscar, en cuyo caso realiza la búsqueda
function Buscar()
{
	if ((document.buscar.buscar.value=="")||(ConBlancos(document.buscar.buscar.value)))
	{
		alert("Debe introducir alguna palabra");
		document.buscar.buscar.focus();
		return false;
	}
	else
	{
		document.buscar.action = path + "busquedas/buscar2.asp";
		document.buscar.submit();
	}
}

function errorTelefono(telf)
{
	for(var i=0; i < telf.length; i++)	//si contiene algún caracter distinto de [0 a 9] es erróneo
	{
        var car=telf.substring(i, i+1);
        if(car<"0" || car>"9") return(true);
	}
	return(false);
}

function errorCodPostal(cp)
{
	if (cp.length != 5) return(true); 	//si la longitud no es 5 es erróneo
	for(var i=0; i < cp.length; i++)	//si contiene algún caracter distinto de 0 a 9 es erróneo
	{
        var car=cp.substring(i, i+1);
        if(car<"0" || car>"9") return(true);
	}
	return(false);
}

function errorEmail(e_mail)
{
	if (SinComillas(e_mail)==false) return(true);	// Si contiene comillas es erróneo
	if (e_mail.indexOf("@")==-1) return(true);		// Si no contiene el símbolo '@' es erróneo
	return(false);    
}

function SinComillas(c)
{
	for(var i=0; i < c.length; i++) //si contiene comillas es un código erróneo
	{
       	var car=c.substring(i, i+1);
       	if(car=="'" || car=="\"") return(false);
	}
	return(true);
}

function FmtFecha(fecha)
{
	var dd="", mm="", aa="", car, fechaAux="", control=0;

	for(var i=0; i<fecha.length; i++)
	{
		car=fecha.substring(i, i+1);
		if (car<"0" || car>"9") control++;
		else
		{
			switch(control)
			{
				case 0: dd=dd+car; break;
				case 1: mm=mm+car; break;
				case 2: aa=aa+car; break;
			}
		}      
	}                     
	if(dd.length==1) dd="0"+dd;	// Se pone el '0' por la izquierda
	if(mm.length==1) mm="0"+mm;
	if(aa.length==2) 
	{
		if(aa<50) aa="20"+aa; 
		else aa="19"+aa; 
	}
	fechaAux=dd+"-"+mm+"-"+aa;
	return(fechaAux);
}

function FmtHora(h)
{
	var hh="", mm="", result="";
	var ind=h.indexOf(":"); // Se buscan los dos puntos
	hh=h.substring(0, ind);
	if (hh.length==1) hh="0"+hh;
	mm=h.substring(ind+1, h.length);
	if (mm.length==1) mm="0"+mm;
	result=hh+":"+mm;
	return(result);
}

function chkFecha(fecha)
{
 var dd="", mm="", aa="", car, control=0;

 for(var i=0; i<fecha.length; i++)
 {
        car=fecha.substring(i, i+1);
        if (car<"0" || car>"9") control++;
        else
	{
                switch(control)
		{
                        case 0: 
                                dd=dd+car;	//dia
                                break;
                        case 1:           
                                mm=mm+car;	//mes
                                break;
                        case 2:           
                                aa=aa+car;	//año
                                break;
                }
        }      
 }                     
 if (dd.length==0 || mm.length==0 || aa.length==0 || dd.length>2 || mm.length>2 || aa.length==1 || aa.length==3 || aa.length>4 || control>2) return(false);
 if(dd<1 || dd>31) return(false);
 if(mm<1 || mm>12) return(false);
 if((mm=="4" || mm=="04" || mm=="6" || mm=="06" || mm=="9" || mm=="09" || mm=="11") && (dd>30)) return(false);

 if((aa%4)==0)
 {
 	//Año bisiesto
        if(mm==2 && dd>29) return(false);
 }
 else if(mm==2 && dd>28) return(false);

 return(true); // No hay error!
}

function Fecha()
{
 if (document.alta1.fecha.value == "") return(false);
 if (chkFecha(document.alta1.fecha.value)==false) return(false);
 document.alta1.fecha.value=FmtFecha(document.alta1.fecha.value);
 return(true);
}

function fmtPassword(pwd)
{
 var i=0, salir=false;
 var aux="";

 for (i=0; i<pwd.length; i++){
 	if (salir==false){
 		if (pwd.charAt(i)!=" "){
 			aux=aux+pwd.charAt(i);
 			salir=true;
 		}
 	}
 	else if (salir==true) aux=aux+pwd.charAt(i);
 }
 return(aux);
}









