// JavaScript Document

function ir_a_cat(id,situacion,pack){
	if (situacion==1){
		document.getElementById("indexform").action="subcategoria.php";
	}
	if(situacion==2){
		if(pack!=0){
			document.getElementById("indexform").action="pack.php";
		}else{
			document.getElementById("indexform").action="productos.php";
		}
		document.getElementById("pack").value=pack;
	}
	if (situacion==3){
		document.getElementById("indexform").action="pregunta.php";
	}
	if (situacion==4){
		document.getElementById("indexform").action="productos.php";	
	}
	document.getElementById("campo").value=id;
	document.getElementById("indexform").submit();
}

function comprar(unidad,campo){
	if(document.getElementById("patas_c")){
		
		if(document.getElementById("patas_c").checked) {
			document.getElementById("patas").value=true;
		}
	}
	document.getElementById("campo").value=campo;
	document.getElementById("pack").value=0;
	document.getElementById("producto").value=campo;
	document.getElementById("cant").value=1;
	document.getElementById("indexform").action="redireccion.php";
	document.getElementById("indexform").submit();
}
	
function comprar_p(unidad,pack){
	document.getElementById("indexform").action="redireccion.php";
	document.getElementById("campo").value=pack;
	document.getElementById("producto").value=pack;
	document.getElementById("cant").value=1;
	document.getElementById("indexform").submit();
}	
	
function ver(articulo,nombre){
	document.getElementById("indexform").action="incrementar.php";
	document.getElementById("promocion").value=document.getElementById("cod_prom").value;
	document.getElementById("prueba").value=document.getElementById(nombre).value;
	document.getElementById("prueba2").value=articulo;
	document.getElementById("indexform").submit();
}

function prod2(){
	document.getElementById("indexform").action="productos.php?#v";
	document.getElementById("pack").value=0;
	document.getElementById("campo").value=document.getElementById("medidas").value;
	document.getElementById("indexform").submit();
}

function prod3() {
	document.getElementById("indexform").action="packs.php?#v";
	document.getElementById("campo").value=document.getElementById("medidas").value;
	document.getElementById("indexform").submit();
}

function ir_pedidos(){
	document.cont_pedido.submit();
}

function codigoprom(){
	document.getElementById("indexform").action="carro.php";
	document.getElementById("promocion").value=document.getElementById("cod_prom").value;
	document.getElementById("indexform").submit();
}

/*function recargar(uni,total){
	document.getElementById('td_total_compra').innerHTML= '<p>'+uni+'<br />'+total+'&nbsp;&euro;<br /></p>';
}*/

function validar_datos(){
	var nom=document.cambiar.user.value;
	var cont=document.cambiar.pass.value;
	var cont2=document.cambiar.pass2.value;
	var a ='<p class="form_texto">';
	var b = '</p>';
	var mensaje="";
	var error=(-1);
	
	if (cont==cont2) {
		if((nom=="") || (nom==" ") || (cont=="") || (cont==" ")) {
			error=0;
		}
	}else{
		error=1;
	}
	
	switch (error) {
		case 0:
			mensaje=document.getElementById('mensaje').innerHTML=a+'Introduzca sus datos en las casillas del formulario.'+b;
		break;
		case 1:
			mensaje=document.getElementById('mensaje').innerHTML=a+'Las claves no coinciden, por favor vuelva a introducirlas.'+b;
		break;
	}
	
	if (error!=(-1)) {
		document.getElementById('div_resultado').style.display='block';
		document.getElementById('div_formulario').style.display='none';
		return;		
	}
	document.getElementById("cambiar").action="datos_acceso.php";
	document.cambiar.submit();
	
}

function validar_datosper(){
	var nom=document.datos_per.nombre.value;
	var ape=document.datos_per.apellido1.value;
	var ape=document.datos_per.apellido2.value;
	var mail=document.datos_per.mail.value;
	var dir=document.datos_per.direccion.value;
	var ciudad=document.datos_per.local.value;
	var postal=document.datos_per.cod.value;
	var digitos=document.datos_per.cod.value.length;
	var prov=document.datos_per.prov.value;
	var telf=document.datos_per.telf.value;
	var a ='<p class="form_texto">';
	var c ='<span class="e">Error</span>';
	var b = '</p>';
	var mensaje="";
	var error=(-1);
		
		if((nom=="") || (nom==" ") || (ape=="") || (ape==" ") || (mail=="") || (mail==" ") || (dir=="")||
	(dir==" ") || (telf=="") || (telf==" ") || (ciudad="") || (ciudad==" ") || (postal=="") || (postal==" ") || (prov=="") || (prov==" ")) {
			error=0;
		}else if (!validar_email(mail)) {
			error=1;
		}else if (!validar_telefono(telf)) {
			error=2;
		}else if(digitos!=5){
			error=3;
		}
	
	
	switch (error) {
		case 0:
		mensaje=document.getElementById('mensaje').innerHTML=a+c+'Verifique que ha introducido los datos requeridos en las casillas del formulario.'+b;
		break;
		case 1:
		mensaje=document.getElementById('mensaje').innerHTML=a+c+'Correo incorrecto.<br>Introduzca una direcci&oacute;n de correo v&aacute;lida.'+b;
		break;
		case 2:
		mensaje=document.getElementById('mensaje').innerHTML=a+c+'Tel&eacute;fono Incorrecto.<br>Introduzca un n&uacute;mero v&aacute;lido.'+b;
		break;
		case 3:
		mensaje=document.getElementById('mensaje').innerHTML=a+c+'C&oacute;digo postal incorrecto.'+b;
		break;
	}
	
	if (error!=(-1)) {
		document.getElementById('div_resultado').style.display='block';
		document.getElementById('div_formulario').style.display='none';
		return;		
	}	
	document.datos_per.submit();
}


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

function validar_telefono(telf) {
	
	if (telf!='') {
		if (/^[0-9]{2,3}-? ?[0-9]{6,7}$/.test(telf)){
			return (true);
		} else {
		 return (false);
		}
	}
	return (true);
}

/* Oculta la capa del error. */

function vaciar_capas_aux() {
	document.getElementById('div_resultado').style.display= 'none';
	document.getElementById('div_formulario').style.display= 'block';
}

function info() {
	document.getElementById('capa1').style.display='block';
	document.getElementById('capa2').style.display='none';
}

function cerrar_info() {
	document.getElementById('capa1').style.display='none';
	document.getElementById('capa2').style.display='block';
}

function esconder(id,accion){
	if (accion == 'esconder')
		document.getElementById(id).style.visibility = "hidden";
	else
		document.getElementById(id).style.visibility = "visible";
}