// Copyright © 2001 by Apple Computer, Inc., All Rights Reserved.
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.

// This code was modified by Alejandro Tabares <webmaster@frikilandia.com> creating the function checkNif and translating the error messages to Spanish Language.

function checkEmail (strng) {
var error="";
if (strng == "") {
   error = "No introdujo una dirección de email.\n\n";
}

    var emailFilter=/^.+@.+\..{2,4}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Por favor introduzca un dirección válida de email.\n\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "La dirección que introdujo contiene caracteres no permitidos.\n\n";
       }
    }
return error;    
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng) {
var error = "";
if (strng == "") {
   error = "Debe introducir un número de TELEFONO.\n\n";
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "El teléfono que introdujo contiene caracteres no permitidos.";
  
    }
    if (!(stripped.length == 9)) {
	error = "Asegurese de que introdujo 9 dígitos en el teléfono.\n\n";
    } 
return error;
}


// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a password.\n";
}

    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if ((strng.length < 6) || (strng.length > 8)) {
       error = "The password is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
      error = "The password contains illegal characters.\n";
    } 
    else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
       error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
    }  
return error;    
}    


// username - 4-10 chars, uc, lc, and underscore only.

function checkUsername (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a username.\n";
}


    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < 4) || (strng.length > 10)) {
       error = "The username is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
    error = "The username contains illegal characters.\n";
    } 
return error;
}       


// non-empty textbox

function isEmpty(strng,campo) {
var error = "";
  if (strng.length == 0) {
     error = "El campo " + campo + " es obligatorio.\n\n"
  }
return error;	  
}

// was textbox altered

function isDifferent(strng) {
var error = ""; 
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\n";
  }
return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue,campo) {
var error = "";
   if (!(checkvalue)) {
       error = "Por favor seleccione una opcion del campo " + campo +".\n\n";
    }
return error;
}

// valid selector from dropdown list

function checkDropdown(choice,campo) {
var error = "";
    if (choice == 0) {
    error = "No elegiste una opción del campo "+  campo +".\n\n";
    }    
return error;
}

// Validate a NIF checing length, and the last character
function checkNif(nif){

	letras=["T","R","W","A","G","M","Y","F","P","D","X","B","N","J","Z","S","Q","V","H","L","C","K","E","F"];
	
	//dado un NIF te devuelve si es o no correcto
	var error = "";			//texto que se mostraría en caso de error

	dni=nif.substring(0,nif.length-1);	//del NIF nos quedamos con el DNI
	dni=parseInt(dni);					//al convertirlo a entero, le quitamos los ceros de la izquierda
	letra=nif.charAt(nif.length-1);	//la letra que nos han pasado
	letra = letra.toUpperCase();
	letraCorrecta = letras[ dni % 23];	//la letra que debería ser para ese DNI

	if (dni > 99999999){ 
		error = "El DNI tiene a lo sumo 8 cifras\n\n";
		return error;
	}
	
	var chars= /\D/;
	if(!letra.match(chars)){
		error = "El último carácter del NIF debe ser una letra\n\n";
		return error;
	} 
	
	if(letra != letraCorrecta) {
		error = "La letra introducida no se corresponde con este DNI\n\n";
		return error;
	}
return error;
}