
function EsVacio(cadena){
	return ((cadena == null) || (cadena.length == 0));
}

function SinSeleccion(cadena){
	return ((cadena == 0));
}

function Longitud2(cadena){
    if (cadena.length<6) {
		mensaje_error += "\nField ["+nombre+"] has less than 6 characters.\n";
		numero_errores++;	
	}
	return;
}

function iguales(cadena,cadena2){
    if (cadena!=cadena2) {
		mensaje_error += "\nField ["+nombre+"] you wrote two differents passwords.\n";
		numero_errores++;	
	}
	return;
}


function Longitud(cadena){
	return cadena.length;
}

function ComprobarVacio(formulario,campo,nombre) {
	if (eval("EsVacio(document.forms[formulario]."+campo+".value)")) {
		mensaje_error += "\nField "+nombre+" required.\n";
		numero_errores++;
	}
	return;
}

function ComprobarSeleccion(formulario,campo,nombre) {
	if (eval("SinSeleccion(document.forms[formulario]."+campo+".value)")) {
		mensaje_error += "\nSelect an option to the field ["+nombre+"].\n";
		numero_errores++;
	}
	return;
}

function ComprobarFecha(formulario,campo,nombre) {
  var fecha,error, i, total, pos, dia, mes, anno;
  
  fecha=eval("document.forms[formulario]."+campo+".value");
  
  error = 0;
  i     = 0;
  total = 0;
  
  //Cuenta los '/' de la fecha
  while (fecha.indexOf("/",i+1)>0) {
    total++;
    i = fecha.indexOf("/",i+1)
  }
  
  if (total!=2) error = 1;
  
  pos = fecha.indexOf("/",0);
  dia = fecha.substring(0,fecha.indexOf("/",0));
  mes = fecha.substring(pos+1,fecha.indexOf("/",pos+1));
  pos = fecha.indexOf("/",pos+1);
  anno = fecha.substring(pos+1,fecha.length);
  
  // Comprueba si son números
  if (isNaN(anno)) error = 1;
  if (isNaN(mes)) error = 1;
  if (isNaN(dia)) error = 1;
  // Comprueba el rango
  if (anno<1900 || anno>2500) error = 1;
  if (mes<1 || mes>12) error = 1;
  if (dia<1 || dia>31) error = 1;

  if ((mes == 4 || mes == 6 || mes == 9 || mes == 11) && dia==31) error = 1;
  
  // Años no bisiestos
  if ( mes == 2) {
    if (((anno % 4 == 0) && (anno % 100 != 0)) || (anno % 400 == 0)) {
      if (dia>29) error = 1;
    }
    else {
      if (dia>28) error = 1;
    }
  }
	if (error==1) {
		mensaje_error += "\nThe field ["+nombre+"] has a wrong date.\nWrite the date at this form: 25/12/1980.\n";
		numero_errores++;
	}
	return;
  //return error;
}

function ComprobarEmail(formulario,campo,nombre) {
	var email;
	
	email=eval("document.forms[formulario]."+campo+".value");
	
	if (email.indexOf('@')<1 || email.lastIndexOf('.')<3 || email.lastIndexOf('.')==(email.length-1)) {
		mensaje_error += "\nThe field ["+nombre+"] has a wrong email.\n";
		numero_errores++;
	}
	else {
		if((email.lastIndexOf('.') - email.indexOf('@'))<2) {
			mensaje_error += "\nThe field ["+nombre+"] has a wrong email.\n";
			numero_errores++;
		}
		return ;
	}
}
