function validamail(theform)
{
 if(theform.nombre.value == '') { alert('Debe introducir su nombre y apellidos'); theform.nombre.focus(); return false; }
 var filter=/^[A-Za-z][A-Za-z0-9_.ñÑ-]*@[A-Za-z0-9_-]+\.[A-Za-z0-9_.-]+[A-za-z]$/;
 if(filter.test(theform.email.value) == false) { alert('Debe introducir email válido'); theform.email.select(); return false; }
 if(theform.coment.value == '') { alert('Debe introducir su consulta'); theform.coment.focus(); return false; }   
}

function validanews(theform)
{
 var filter=/^[A-Za-z][A-Za-z0-9_.ñÑ-]*@[A-Za-z0-9_-]+\.[A-Za-z0-9_.-]+[A-za-z]$/;
 if(filter.test(theform.mail.value) == false) { alert('Debe introducir email válido'); theform.mail.select(); return false; }
}

function validabusca(theform)
{
 if(theform.texto.value == '') { alert('Debe introducir texto a buscar'); theform.texto.focus(); return false; } 
}

function validauser(theform)
{  
 var filter=/^[A-Za-z][A-Za-z0-9_.ñÑ-]*@[A-Za-z0-9_-]+\.[A-Za-z0-9_.-]+[A-za-z]$/;
 if(filter.test(theform.mail.value) == false) { alert('Debe introducir email válido'); theform.mail.select(); return false; }
 if(theform.password.value.length < 4) { alert('El password debe ser de, almenos, 4 carácteres'); theform.password.focus(); return false; }     
 if(theform.password.value != theform.passconf.value) { alert('Los dos passwords deben ser idénticos'); theform.password.focus(); return false; }
 if(theform.nombre.value == '') { alert('Debe introducir su nombre'); theform.nombre.focus(); return false; }
 if(theform.apellidos.value == '') { alert('Debe introducir sus apellidos'); theform.apellidos.focus(); return false; }
 if(theform.nif.value == '') { alert('Debe introducir su NIF/NIE/CIF'); theform.nif.focus(); return false; }
 if(theform.direccion.value == '') { alert('Debe introducir su dirección'); theform.direccion.focus(); return false; }  
 if(theform.poblacion.value == '') { alert('Debe introducir su población'); theform.poblacion.focus(); return false; }
 if(theform.provincia.value == 0 && theform.pais.value == 57) { alert('Debe seleccionar su provincia'); theform.provincia.focus(); return false; }
 if(theform.cpostal.value == '') { alert('Debe introducir su código postal'); theform.cpostal.focus(); return false; }
 if(theform.telefono1.value == '') { alert('Debe introducir un número de contacto'); theform.telefono1.focus(); return false; }
 if(theform.ccontrato.checked != true) { alert('Debe aceptar las condiciones del contrato.'); theform.ccontrato.focus(); return false; }
}

function validauser2(theform)
{  
 var filter=/^[A-Za-z][A-Za-z0-9_.ñÑ-]*@[A-Za-z0-9_-]+\.[A-Za-z0-9_.-]+[A-za-z]$/;
 if(filter.test(theform.mail.value) == false) { alert('Debe introducir email válido'); theform.mail.select(); return false; }
 if(theform.password.value.length < 4 && theform.password.value != '') { alert('El password debe ser de, almenos, 4 carácteres'); theform.password.focus(); return false; }     
 if(theform.password.value != theform.passconf.value) { alert('Los dos passwords deben ser idénticos'); theform.password.focus(); return false; }
 if(theform.nombre.value == '') { alert('Debe introducir su nombre'); theform.nombre.focus(); return false; }
 if(theform.apellidos.value == '') { alert('Debe introducir sus apellidos'); theform.apellidos.focus(); return false; }
 if(theform.nif.value == '') { alert('Debe introducir su NIF/NIE/CIF'); theform.nif.focus(); return false; }
 if(theform.direccion.value == '') { alert('Debe introducir su dirección'); theform.direccion.focus(); return false; }  
 if(theform.poblacion.value == '') { alert('Debe introducir su población'); theform.poblacion.focus(); return false; }
 if(theform.provincia.value == 0 && theform.pais.value == 57) { alert('Debe seleccionar su provincia'); theform.provincia.focus(); return false; }
 if(theform.cpostal.value == '') { alert('Debe introducir su código postal'); theform.cpostal.focus(); return false; }
 if(theform.telefono1.value == '') { alert('Debe introducir un número de contacto'); theform.telefono1.focus(); return false; }
}

function abven(pag,w,h,t,l,s)
{
 ventana=window.open(pag,'Ampliar','scrollbars='+ s +',toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no,top='+ t + ',left='+ l + ',width='+ w + ',height=' + h);
}

var en1=false, en2=false;
function actdes(parte)
{
  var a;
  if(parte == 'env') a=1; else a=2;
  var f = document.presu              
  eval("f.nombre"+a+".disabled = en"+a+";");
  eval("f.apellidos"+a+".disabled = en"+a+";");
  eval("f.nif"+a+".disabled = en"+a+";");
  eval("f.direccion"+a+".disabled = en"+a+";");
  eval("f.poblacion"+a+".disabled = en"+a+";");
  eval("f.provincia"+a+".disabled = en"+a+";");
  eval("f.cpostal"+a+".disabled = en"+a+";");
  eval("f.pais"+a+".disabled = en"+a+";");
  eval("f.mail"+a+".disabled = en"+a+";");
  eval("f.telefono1"+a+".disabled = en"+a+";");
  eval("f.telefono2"+a+".disabled = en"+a+";");
  eval("f.fax"+a+".disabled = en"+a+";");   
  eval("if(en"+a+" == false) en"+a+" = true; else en"+a+" = false;");
  eval("if(en"+a+" == false) { f.nombre"+a+".value = oldnom; f.apellidos"+a+".value = oldape; f.nif"+a+".value = oldnif; f.direccion"+a+".value = olddir; f.poblacion"+a+".value = oldpob; f.provincia"+a+".value = oldprv;f.cpostal"+a+".value = oldcpo;f.pais"+a+".value = oldpai;f.mail"+a+".value = oldmai;f.telefono1"+a+".value = oldte1;f.telefono2"+a+".value = oldte2;f.fax"+a+".value = oldfax;}");
}

function recalcula()
{
	//las variables de los precios se cogen de la bbd en
	// /includes/costesEnvio.php que printa un javascript
	// inicializando las variables
  var f = document.presu;
  pack5 = Math.ceil(numlibs/5);
  
  if(f.pais1.value == 57) // España
  {
    if(f.formapago.value == 1) gastos = costesEspanaTarjeta*pack5; //tarjeta
	else gastos = costesEspanaContraReembolso*pack5;   //contrareembolso
  } 
  else if(f.pais1.value == 70) // Andorra
  {
    gastos = costesAndorraTarjeta*pack5;  
  }
  else if(f.pais1.value == 49) // Portugal
  {
	  gastos = costesPortugalTarjeta*pack5;  
  }
  else { //resto del mundo
	  gastos = 0;
	  for(i=0; i< numlibs; i++){
		  index = 0;
		  if(i>=costesEuropaTarjeta.length) index = costesEuropaTarjeta.length - 1;
		  else index = i;
		  
		  gastos += costesEuropaTarjeta[index];
	  }
  }
  document.getElementById('genv').innerHTML = ((Math.round(gastos*100)/100) + " &euro;").replace('.',',');
  document.getElementById('total').innerHTML = (Math.round((gastos + costetotal)*100)/100 + " &euro;").replace('.',',');
  f.costgastos.value = Math.round(gastos * 100)/100; 
  f.costefinal.value = Math.round((gastos + costetotal) * 100)/100;   
}
function cambiopais()
{
  var f = document.presu;
  if(f.pais1.value != 57) // No es España -> Solo se acepta tarjeta de crédito
  {
    alert("Para los envíos a este país solamente es permitido el pago con tarjeta de crédito. Revise el coste total del envío.");
    f.formapago.value = 1;    
  }
  recalcula();  
}
function validapresu(theform)
{
 if(theform.formapago.value=="") { alert('Seleccione forma de pago'); theform.formapago.focus(); return false;}
 if(theform.formapago.value==2 && theform.pais1.value != 57) { alert('ContraReembolso sólo válido para España'); theform.formapago.focus(); return false;} 
 var filter=/^[A-Za-z][A-Za-z0-9_.ñÑ-]*@[A-Za-z0-9_-]+\.[A-Za-z0-9_.-]+[A-za-z]$/;
 
 if(theform.nombre1.value == '') { alert('Debe introducir su nombre'); theform.nombre1.focus(); return false; }
 if(theform.apellidos1.value == '') { alert('Debe introducir sus apellidos'); theform.apellidos1.focus(); return false; }
 if(theform.nif1.value == '') { alert('Debe introducir su NIF/NIE/CIF'); theform.nif1.focus(); return false; }
 if(theform.direccion1.value == '') { alert('Debe introducir su dirección'); theform.direccion1.focus(); return false; }  
 if(theform.poblacion1.value == '') { alert('Debe introducir su población'); theform.poblacion1.focus(); return false; }
 if(theform.provincia1.value == 0 && theform.pais1.value == 57) { alert('Debe seleccionar su provincia'); theform.provincia1.focus(); return false; }
 if(theform.cpostal1.value == '') { alert('Debe introducir su código postal'); theform.cpostal1.focus(); return false; }
 if(filter.test(theform.mail1.value) == false) { alert('Debe introducir email válido'); theform.mail1.select(); return false; }
 if(theform.telefono11.value == '') { alert('Debe introducir un número de contacto'); theform.telefono11.focus(); return false; }
 
 if(theform.nombre2.value == '') { alert('Debe introducir su nombre'); theform.nombre2.focus(); return false; }
 if(theform.apellidos2.value == '') { alert('Debe introducir sus apellidos'); theform.apellidos2.focus(); return false; }
 if(theform.nif2.value == '') { alert('Debe introducir su NIF/NIE/CIF'); theform.nif2.focus(); return false; }
 if(theform.direccion2.value == '') { alert('Debe introducir su dirección'); theform.direccion2.focus(); return false; }  
 if(theform.poblacion2.value == '') { alert('Debe introducir su población'); theform.poblacion2.focus(); return false; }
 if(theform.provincia2.value == 0 && theform.pais2.value == 57) { alert('Debe seleccionar su provincia'); theform.provincia2.focus(); return false; }
 if(theform.cpostal2.value == '') { alert('Debe introducir su código postal'); theform.cpostal2.focus(); return false; }
 if(filter.test(theform.mail2.value) == false) { alert('Debe introducir email válido'); theform.mail2.select(); return false; }
 if(theform.telefono12.value == '') { alert('Debe introducir un número de contacto'); theform.telefono12.focus(); return false; }
}
