﻿ function deshabilita(form)
{
	form.cod_pais.disabled=true;
}

function habilita(form)
{
	form.cod_pais.disabled=false;
}

function Validar(form)
{
var checkOK = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ" + "abcdefghijklmnñopqrstuvwxyzáéíóú " + "ªº.";
  
   
 if (form.apellido1.value == "")
  { alert("Por favor introduzca su primer apellido"); form.apellido1.focus(); return false; }

  if (form.apellido1.value.length > 25)
  { alert("Su primer apellido es demasiado largo"); form.apellido1.focus(); return false; }
  
  var checkStr = form.apellido1.value;
  var allValid = true; 
  for (i = 0; i < checkStr.length; i++) {
    ch = checkStr.charAt(i); 
    for (j = 0; j < checkOK.length; j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length) { 
      allValid = false; 
      break; 
    }
  }
  if (!allValid) { 
    alert("Escriba sólo letras en el campo \"Primer Apellido\"."); 
    form.apellido1.focus(); 
    return (false); 
  }

   if (form.apellido2.value == "")
  { alert("Por favor introduzca su segundo apellido"); form.apellido2.focus(); return false; }

  if (form.apellido2.value.length > 25)
  { alert("Su segundo apellido es demasiado largo"); form.apellido2.focus(); return false; }
 
  var checkStr = form.apellido2.value;
  var allValid = true; 
  for (i = 0; i < checkStr.length; i++) {
    ch = checkStr.charAt(i); 
    for (j = 0; j < checkOK.length; j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length) { 
      allValid = false; 
      break; 
    }
  }
  if (!allValid) { 
    alert("Escriba sólo letras en el campo \"Segundo Apellido\"."); 
    form.apellido2.focus(); 
    return (false); 
}

  if (form.nombre.value == "")
  { alert("Por favor introduzca su nombre"); form.nombre.focus(); return false; }

  if (form.nombre.value.length > 25)
  { alert("Su nombre es demasiado largo"); form.nombre.focus(); return false; }
  
  
  var checkStr = form.nombre.value;
  var allValid = true; 
  for (i = 0; i < checkStr.length; i++) {
    ch = checkStr.charAt(i); 
    for (j = 0; j < checkOK.length; j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length) { 
      allValid = false; 
      break; 
    }
  }
  
  if (!allValid) { 
    alert("Escriba sólo letras en el campo \"Nombre\"."); 
    form.nombre.focus(); 
    return (false); 
  }

  /*
  if(form.edad)
  {
	  var checkOK = "0123456789";
	  var checkStr = form.edad.value;
	  var allValid = true; 
	  for (i = 0; i < checkStr.length; i++) {
		ch = checkStr.charAt(i); 
		for (j = 0; j < checkOK.length; j++)
		  if (ch == checkOK.charAt(j))
			break;
		if (j == checkOK.length) { 
		  allValid = false; 
		  break; 
		}
	  }
	  if (!allValid) { 
		alert("Escriba sólo dígitos en el campo edad."); 
		form.edad.focus(); 
		return (false); 
	  } 
  }//if(form.edad)
*/
/*
  if(form.profesion)
  {
	   if (form.profesion.value.length > 50)
	  { alert("El valor del campo profesión es demasiado largo."); form.profesion.focus(); return false; }
	 
	  var checkOK = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ" + "abcdefghijklmnñopqrstuvwxyzáéíóú " + "ªº."; 
	  var checkStr = form.profesion.value;
	  var allValid = true; 
	  for (i = 0; i < checkStr.length; i++) {
		ch = checkStr.charAt(i); 
		for (j = 0; j < checkOK.length; j++)
		  if (ch == checkOK.charAt(j))
			break;
		if (j == checkOK.length) { 
		  allValid = false; 
		  break; 
		}
	  }
	  if (!allValid) { 
		alert("Escriba sólo letras en el campo Profesión."); 
		form.profesion.focus(); 
		return (false); 
	  } 
  }//if
*/
if (form.nacionalidad[0].checked)
{
	
	if (form.numero_nif.value.length != 8)
	{ alert("Su dni debe tener 8 dígitos"); form.numero_nif.focus(); return false; }
}
if (form.nacionalidad[1].checked)
{
	
	if (form.numero_nif.value.length != 8)
	{ alert("Su dni debe tener 8 caracteres"); form.numero_nif.focus(); return false; }
}

if (form.nacionalidad[0].checked)
  {
  
  var checkOK = "0123456789";
  var checkStr = form.numero_nif.value;
  var allValid = true; 
  for (i = 0; i < checkStr.length; i++) {
    ch = checkStr.charAt(i); 
    for (j = 0; j < checkOK.length; j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length) { 
      allValid = false; 
      break; 
    }
  }
  if (!allValid) { 
    alert("Escriba sólo dígitos en el campo \"DNI\"."); 
    form.numero_nif.focus(); 
    return (false); 
  } 
  var cadena="TRWAGMYFPDXBNJZSQVHLCKET";
  var posicion = form.numero_nif.value % 23;
  var letra = cadena.substring(posicion,posicion+1);
  var letraNif = form.letra_nif.value.toUpperCase();
  
  if (letraNif !=letra)
  {
	alert("La letra no se corresponde con el \"DNI\" introducido."); 
    form.numero_nif.focus(); 
    return (false); 
  }

}
if(form.nacionalidad[1].checked)
{
	var cadena="TRWAGMYFPDXBNJZSQVHLCKET";
	var numero= form.numero_nif.value;
	var primerDigito = numero.substring(0,1);
	var restoDigito = numero.substring(1);
	
	if(isNaN(primerDigito))
		var posicion = restoDigito % 23;
	else
		var posicion = numero % 23;

	var letra = cadena.substring(posicion,posicion+1);
	var letraNif = form.letra_nif.value.toUpperCase();
	  
	if (letraNif !=letra)
	{
		alert("La letra no se corresponde con el \"DNI\" introducido."); 
		form.numero_nif.focus(); 
		return (false); 
	 }
}

  
  
  if (form.domicilio.value.length > 50)
  { alert("El domicilio es demasiado largo"); form.numero_nif.focus(); return false; }
  
  var checkOK = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ" + "abcdefghijklmnñopqrstuvwxyzáéíóú " + "0123456789" + ".ªº:/;-,";
  var checkStr = form.domicilio.value;
  var allValid = true; 
  for (i = 0; i < checkStr.length; i++) {
    ch = checkStr.charAt(i); 
    for (j = 0; j < checkOK.length; j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length) { 
      allValid = false; 
      break; 
    }
  }
  if (!allValid) { 
    alert("Escriba sólo letras, dígitos o ( . ª º : / ; - , ) en el campo \"Domicilio\"."); 
    form.domicilio.focus(); 
    return (false); 
  }

if (form.municipio.value.length > 30)
  { alert("El nombre del municipio es demasiado largo"); form.muncipio.focus(); return false; }


  var checkOK = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ" + "abcdefghijklmnñopqrstuvwxyzáéíóú " + "ªº.";
  var checkStr = form.municipio.value;
  var allValid = true; 
  for (i = 0; i < checkStr.length; i++) {
    ch = checkStr.charAt(i); 
    for (j = 0; j < checkOK.length; j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length) { 
      allValid = false; 
      break; 
    }
  }
  if (!allValid) { 
    alert("Escriba sólo letras en el campo \"Municipio\"."); 
    form.municipio.focus(); 
    return (false); 
  }

if (form.provincia.value.length > 30)
  { alert("El nombre de la provincia es demasiado largo"); form.provincia.focus(); return false; }
  

  var checkStr = form.provincia.value;
  var allValid = true; 
  for (i = 0; i < checkStr.length; i++) {
    ch = checkStr.charAt(i); 
    for (j = 0; j < checkOK.length; j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length) { 
      allValid = false; 
      break; 
    }
  }
  if (!allValid) { 
    alert("Escriba sólo letras en el campo \"Provincia\"."); 
    form.provincia.focus(); 
    return (false); 
  }

  if (form.cod_postal.value.length > 0)
{ 
	if (form.cod_postal.value.length != 5)
  { alert("El código postal debe contener 5 dígitos."); form.cod_postal.focus(); return false; }
  
  var checkOK = "0123456789";
  var checkStr = form.cod_postal.value;
  var allValid = true; 
  for (i = 0; i < checkStr.length; i++) {
    ch = checkStr.charAt(i); 
    for (j = 0; j < checkOK.length; j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length) { 
      allValid = false; 
      break; 
    }
  }
    if (!allValid) { 
    alert("Escriba sólo dígitos en el campo Cógido Postal."); 
    form.cod_postal.focus(); 
    return (false); 
  }
}

if (form.telefono.value.length != 9)
  { alert("Escriba 9 dígitos en el campo teléfono"); form.telefono.focus(); return false; }
  var checkOK = "0123456789";
  var checkStr = form.telefono.value;
  var allValid = true; 
  for (i = 0; i < checkStr.length; i++) {
    ch = checkStr.charAt(i); 
    for (j = 0; j < checkOK.length; j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length) { 
      allValid = false; 
      break; 
    }
  }
    if (!allValid) { 
    alert("Escriba sólo dígitos en el campo \"Teléfono\"."); 
    form.telefono.focus(); 
    return (false); 
  }

if (form.movil.value.length != 0)
{
	if (form.movil.value.length != 9)
	{ alert("Escriba 9 dígitos en el campo móvil"); form.movil.focus(); return false; }
 
  var checkStr = form.movil.value;
  var allValid = true; 
  for (i = 0; i < checkStr.length; i++) {
    ch = checkStr.charAt(i); 
    for (j = 0; j < checkOK.length; j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length) { 
      allValid = false; 
      break; 
    }
  }
    if (!allValid) { 
    alert("Escriba sólo dígitos en el campo Móvil."); 
    form.movil.focus(); 
    return (false); 
  }
}
if(form.email.value == "" )
  {	
	alert("Por favor ingrese una dirección de e-mail"); form.email.focus(); return false;
  }

  if( form.email.value.indexOf('@',0) == -1)
  {	
	alert("Por favor ingrese una dirección de e-mail válida"); form.email.focus(); return false;
  }
  
  else
  {
	var es_valido=isEmailAddress(form.email);
	if(!es_valido)
	{
		alert("Por favor ingrese una dirección de e-mail válida"); form.email.focus(); return false;
	}
  }
 
  if(form.fecha)
  {
		var a=form.fecha.value;
		if( a.length!=0)
		{
			if (!(a.length==10 && !(a.split("/")[0]*1>31) && !(a.split("/")[1]*1>12) && !(a.split("/")[2]*1<2000)))
			{
				alert ("La fecha de los hechos debe tener el formato dd/mm/aaaa. Inténtelo de nuevo"); form.fecha.focus(); return false;
			}
			else
			{
				arrayfecha=a.split("/");
				if(!(arrayfecha.lenght==3))
				{
					if(isNaN(arrayfecha[0])||isNaN(arrayfecha[1])||isNaN(arrayfecha[2]))
					{
						alert ("La fecha de los hechos debe tener el formato dd/mm/aaaa. Inténtelo de nuevo"); form.fecha.focus(); return false;
					}
					else
					{
						fecha=""+arrayfecha[0]+"/"+arrayfecha[1]+"/"+arrayfecha[2];
						if(!(a==fecha)){
							alert ("La fecha de los hechos debe tener el formato dd/mm/aaaa. Inténtelo de nuevo"); form.fecha.focus(); return false;
						}
					}
				}
			}
		}
  }// if(form.fecha)

  if(form.hora)
  {
	var a=form.hora.value;
	if( a.length!=0)
	{
		if (!(a.length==5 && !(a.split(":")[0]*1>23) && !(a.split(":")[1]*1>59)))
		{
			alert ("La hora de los hechos debe tener el formato HH:MM. Inténtelo de nuevo"); form.hora.focus(); return false;
		}
		else
		{
			arrayhora=a.split(":");
			if(!(arrayhora.lenght==2))
			{
				if(isNaN(arrayhora[0])||isNaN(arrayhora[1]))
				{
					alert ("La hora de los hechos debe tener el formato HH:MM. Inténtelo de nuevo"); form.hora.focus(); return false;
				}
				else
				{
					hora=""+arrayhora[0]+":"+arrayhora[1];
					if(!(a==hora)){
						alert ("La hora de los hechos debe tener el formato HH:MM. Inténtelo de nuevo"); form.hora.focus(); return false;
					}
				}
			}
		}
	}
  }//if(form.fecha)

  if(form.linea)
  {

	 if (form.linea.value.length > 3)
	  { alert("Demasiados carácteres para la línea"); form.linea.focus(); return false; }
	  
	  var checkOK = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ" + "abcdefghijklmnñopqrstuvwxyzáéíóú " + "0123456789" + ".ªº";
	  var checkStr = form.linea.value;
	  var allValid = true; 
	  for (i = 0; i < checkStr.length; i++) {
		ch = checkStr.charAt(i); 
		for (j = 0; j < checkOK.length; j++)
		  if (ch == checkOK.charAt(j))
			break;
		if (j == checkOK.length) { 
		  allValid = false; 
		  break; 
		}
	  }
	  if (!allValid) { 
		alert("Escriba sólo letras o dígitos en el campo Línea"); 
		form.linea.focus(); 
		return (false); 
	  }
  }//if(form.linea)

  if(form.coche)
  {

		if (form.coche.value.length != 0)
		{
		if (form.coche.value.length != 4)
		  { alert("El coche debe contener 4 cifras."); form.coche.focus(); return false; }
		  
		  var checkOK = "0123456789";
		  var checkStr = form.coche.value;
		  var allValid = true; 
		  for (i = 0; i < checkStr.length; i++) {
			ch = checkStr.charAt(i); 
			for (j = 0; j < checkOK.length; j++)
			  if (ch == checkOK.charAt(j))
				break;
			if (j == checkOK.length) { 
			  allValid = false; 
			  break; 
			}
		  }
		  if (!allValid) { 
			alert("Escriba sólo dígitos en el campo coche"); 
			form.coche.focus(); 
			return (false); 
		  }
		}
  }//if(form.coche)


  if(form.lugar)
  {

		if (form.lugar.value.length > 30)
		  { alert("El nombre del lugar es demasiado largo"); form.lugar.focus(); return false; }


		  var checkOK = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ" + "abcdefghijklmnñopqrstuvwxyzáéíóú " + "ªº.";
		  var checkStr = form.municipio.value;
		  var allValid = true; 
		  for (i = 0; i < checkStr.length; i++) {
			ch = checkStr.charAt(i); 
			for (j = 0; j < checkOK.length; j++)
			  if (ch == checkOK.charAt(j))
				break;
			if (j == checkOK.length) { 
			  allValid = false; 
			  break; 
			}
		  }
		  if (!allValid) { 
			alert("Escriba sólo letras en el campo \"Municipio\"."); 
			form.lugar.focus(); 
			return (false); 
		  }
  }//if(form.lugar)

  if(form.hacia)
  {

		if (form.hacia.value.length > 30)
		  { alert("El nombre del lugar es demasiado largo"); form.hacia.focus(); return false; }
		  

		  var checkStr = form.hacia.value;
		  var allValid = true; 
		  for (i = 0; i < checkStr.length; i++) {
			ch = checkStr.charAt(i); 
			for (j = 0; j < checkOK.length; j++)
			  if (ch == checkOK.charAt(j))
				break;
			if (j == checkOK.length) { 
			  allValid = false; 
			  break; 
			}
		  }
		  if (!allValid) { 
			alert("Escriba sólo letras en el campo \"Dirección a\"."); 
			form.hacia.focus(); 
			return (false); 
		  }
  }//if(form.hacia)

  if(form.relato)
  {
	  if (form.relato.value.length < 20)
	  { alert("El texto de la reclamación debe tener al menos 20 caracteres."); form.relato.focus(); return false; }
	  if (form.relato.value.length >  2500)
	  { alert("El contenido de la reclamación es demasiado extenso. Introduzca un máximo de 1000 caracteres."); form.relato.focus(); return false; }
  }
  else
  if(form.comentario)
  {
	  if (form.comentario.value.length < 20)
	  { alert("El texto del comentario debe tener al menos 20 caracteres."); form.comentario.focus(); return false; }
	  if (form.comentario.value.length >  2500)
	  { alert("El contenido del comentario es demasiado extenso. Introduzca un máximo de 1000 caracteres."); form.comentario.focus(); return false; }
  }

  return true;
}


function isEmailAddress(theElement)
{
var s = theElement.value;
//var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
var filter=/^[\-a-zA-Z0-9_]+(\.[\-a-zA-Z0-9_]+)*@([\-a-zA-Z0-9_]+\.)+[a-zA-Z]{2,3}$/;
if (s.length == 0 ) return true;
if (filter.test(s))
	return true;
else
	return false;
}
