function isNumeric(cadena) {
	for (i=0; i<cadena.length; i++) {
		if (cadena.charAt(i) < '0' || cadena.charAt(i) > '9') {
			return(0);
		}
	}
	return(1);
}
function isAlphabetic(cadena) {
	for (i=0; i<cadena.length; i++) {
		if (!(((cadena.charAt(i) >= 'a') && (cadena.charAt(i) <= 'z')) ||
			  ((cadena.charAt(i) >= 'A') && (cadena.charAt(i) <= 'Z')) )) {
		 	return(0);
		}
	}
	return(1);
}
function isAlphanumeric(cadena) {
	for (i=0; i<cadena.length; i++) {
		if (!(((cadena.charAt(i) >= 'a') && (cadena.charAt(i) <= 'z')) ||
			  ((cadena.charAt(i) >= 'A') && (cadena.charAt(i) <= 'Z')) ||
			  ((cadena.charAt(i) >= '0') && (cadena.charAt(i) <= '9')))) {
		 	return(0);
		}
	}
	return(1);
}
function emailValidator(dir_email) {
	var pos_arroba, ult_pos_arroba, nombre_dir, dominio_dir;
	var pos_punto, ult_pos_punto, antes_punto, tras_punto;

	if (dir_email.length == 0)
		return (0);	// si direccion email vacia, no se comprueba

	pos_arroba = dir_email.indexOf("@");
	ult_pos_arroba = dir_email.lastIndexOf("@");
	nombre_dir = dir_email.substr(0, pos_arroba);
	dominio_dir = dir_email.substr(pos_arroba+1, dir_email.length);
	if (pos_arroba != ult_pos_arroba)
		return (1);	// error en la direccion email - hay varias @
	if (pos_arroba < 1)
		return (1);	// error en la direccion email - no hay @
	if (nombre_dir.length == 0)
		return (1);	// error en la direccion email - no hay nada antes de @
	if (dominio_dir.length == 0)
		return (1);	// error en la direccion email - no hay nada tras @

	pos_punto = dominio_dir.indexOf(".");
	ult_pos_punto = dominio_dir.lastIndexOf(".");
	antes_punto = dominio_dir.substr(0, pos_punto);
	tras_punto = dominio_dir.substr(pos_punto+1, dominio_dir.length);
	if (pos_punto < 1)
		return (1);	// error en la direccion email - no hay . en el dominio
	if (antes_punto.length == 0)
		return (1);	// error en la direccion email - no hay nada antes . en el dominio
	if (tras_punto.length == 0)
		return (1);	// error en la direccion email - no hay nada tras . en el dominio

	return(0);	// direccion email correcta
}
function openWin(winFile, wTitle, wSize, hSize) {
	if (wTitle == "") wTitle = "Rugby Soluciones";
	windowOpen = window.open(winFile, wTitle, "width="+wSize+",height="+hSize+",resizable=no,scrollbars=yes,status=no,toolbar=no,location=no,menubar=no,directories=no,hotkeys=no,dependent=yes");
	windowOpen.moveTo(150,150);
}
function loadOpener(url) {
	window.opener.document.location.href = url;
}
function testNif(nif, doctype) {
	var ERROR00 = "OK";
	var ERROR01 = "<b>identificador</b> INCORRECTO (m&aacute;ximo 12 caracteres)";
	var ERROR02 = "<b>DNI / NIF</b> INCORRECTO (comprueba que el n&uacute;mero y la letra est&aacute;n bien escritos)";
	var ERROR03 = "<b>CIF</b> INCORRECTO (comprueba que el n&uacute;mero y la letra est&aacute;n bien escritos)";
	var ERROR04 = "<b>Tarjeta de residencia</b> INCORRECTA (comprueba que el n&uacute;mero y la letra est&aacute;n bien escritos)";
	var ERROR05 = "<b>identificador</b> INCORRECTO (m&iacute;nimo 4 caracteres)";
	var ERROR06 = "<b>identificador</b> NO corresponde con el tipo de documento";

	if (nif.value.length > 12)
		return ERROR01;

	if (nif.value.length < 4) {
		if (doctype.options[doctype.selectedIndex].value == 0)
    		return ERROR02;
		else if (doctype.options[doctype.selectedIndex].value == 1)
    		return ERROR03;
		else if (doctype.options[doctype.selectedIndex].value == 2)
    		return ERROR04;
		else
    		return ERROR05;
   	}
   	nif.value = ignoreSpaces(nif.value);

	if ((doctype.options[doctype.selectedIndex].value == 0) || 
	    (doctype.options[doctype.selectedIndex].value == 1) || 
	    (doctype.options[doctype.selectedIndex].value == 2)) {
		if (nif.value.length > 16) {
    		if (doctype.options[doctype.selectedIndex].value == 0)
				return ERROR02;
    		else if (doctype.options[doctype.selectedIndex].value == 1)
				return ERROR03;
    		else if (doctype.options[doctype.selectedIndex].value == 2)
				return ERROR04;
		}
		if (validaNif(nif.value) != 0) {
    		if (doctype.options[doctype.selectedIndex].value == 0)
				return ERROR02;
    		else if (doctype.options[doctype.selectedIndex].value == 1)
				return ERROR03;
    		else if (doctype.options[doctype.selectedIndex].value == 2)
				return ERROR04;
		}
	}
	
	if (validaDocumentoYTipo(nif.value, doctype.value) == 0)
		return ERROR06;

	return ERROR00;
}  
function validaDocumentoYTipo(cadenaNif, tipo) {
	var valoresNIF = new Array ("0","1","2","3","4","5","6","7","8","9");
	var valoresCIF = new Array ("A","B","C","D","E","F","G","H","K","L","M","N","P","Q","S");	
	var valoresNIE = "X";	
	var tipoNIF = 0;
	var tipoCIF = 1;
	var tipoNIE = 2;
	var tipoPasaporte = 3;
	while (cadenaNif.length < 9) cadenaNif = "0" + cadenaNif;	
	primerCaracter = cadenaNif.substring(0, 1).toUpperCase();
	
	if (tipo == tipoNIF) {
		for (i=0; i<valoresNIF.length; i++) { 
			if (valoresNIF[i] == primerCaracter) return true; 	
		}
	}	
	if (tipo == tipoCIF) {
		for (i=0; i<valoresCIF.length; i++) { 
			if (valoresCIF[i] == primerCaracter) return true; 	
		}
	}
	if (tipo == tipoNIE)
		if (valoresNIE == primerCaracter) return true;
	if (tipo == tipoPasaporte) return true;

	return false;
}
function validaNif(cadenaNif) {
	var valoresNif = new Array ("0","1","2","3","4","5","6","7","8","9","K","L","X");
	var valoresCif = new Array ("A","B","C","D","E","F","G","H","K","L","M","N","P","Q","S");
	var controlNif = new Array ("T","R","W","A","G","M","Y","F","P","D","X","B","N","J","Z","S","Q","V","H","L","C","K","E");
	var controlCifLetras = new Array ("J","A","B","C","D","E","F","G","H","I");
	while (cadenaNif.length < 9) cadenaNif = "0" + cadenaNif;	
	caracterControl = cadenaNif.substring(cadenaNif.length-1, cadenaNif.length).toUpperCase();
	//TIPO DE NIF	
	primerCaracter = cadenaNif.substring(0, 1).toUpperCase();
	var tipo = "desconocido";	
	
	for (i=0; i<valoresNif.length; i++) {
		if (valoresNif[i] == primerCaracter) tipo = "NIF";
	}	
	if (tipo == "desconocido") {
		for (i=0; i<valoresCif.length; i++) {
			if (valoresCif[i] == primerCaracter) tipo="CIF";
		}
	}
	if (tipo == "desconocido") return (5);	//El primer caracter del nif no es válido.
	if (tipo == "NIF") {	
		var provincia = "";
		if ((primerCaracter == "K") || (primerCaracter == "L")){		
			provincia = cadenaNif.substring(1, 3);
			if (!isNumeric(provincia)) return 2;	//Codigo de provincia no válido
			numero = cadenaNif.substring(3, 8);			
		} else if (primerCaracter == "X"){	
			numero = cadenaNif.substring(1, cadenaNif.length-1);		
		} else {		
			numero = cadenaNif.substring(0, cadenaNif.length-1);		
		}
		if (!isNumeric(numero)) return 3;	// Parte numérica del Nif no válida	
		caracterControlCalculado = controlNif [((parseFloat(numero)%23))];																		  //sumarle 1, eso lo hacemos al indexar el 
																		  //array ya que los indices empiezan en 0				
		if (caracterControl != caracterControlCalculado) return 4;   //Carácter Control incorrecto
	} else if (tipo == "CIF") {
		centralPart = cadenaNif.substring(1, (cadenaNif.length)-1);
		sumaA = 0;
		for (ind=0; ind<centralPart.length; ind++) {
			if ((ind+1)%2 == 0) {sumaA = sumaA + parseInt(centralPart.substring(ind, ind+1), 10);}
		}
		sumaB = 0;
		for (ind2=0; ind2<centralPart.length; ind2++) {
			if ((ind2+1)%2 > 0) {
				digito = parseInt(centralPart.substring(ind2, ind2+1), 10);
				digitox = digito * 2;
				digitox = new String(digitox);
				if (digitox.length == 1) {digitox = "0" + digitox;}
				sumaparcial = parseInt(digitox.substring(0, 1), 10) + parseInt(digitox.substring(1, 2), 10);
				sumaB = sumaB + sumaparcial;
			}
		}
		sumaC = sumaA + sumaB;
		sumaCString = new String(sumaC);
		if (sumaCString.length == 1) {sumaCString = "0" + sumaCString;}
		digitoUnidadC = sumaCString.substring(1, 2);
		valorControl = 10 - (parseInt(digitoUnidadC, 10));
		if (valorControl == 10) {valorControl = 0}
		cadenaNifControl = cadenaNif.substring((cadenaNif.length)-1, cadenaNif.length);
		if (isNumeric(cadenaNifControl)) {
			if (!(parseInt(cadenaNifControl, 10) == valorControl)) {return 5;}
		} else {
			if (!(cadenaNifControl.toUpperCase() == controlCifLetras[valorControl])) {return 6;}
		}
	} 
	return 0;  //todo bien
}
function checkString(field, string) {
	if (field.value.indexOf(string) > -1)
		return true;
	else
		return false;
}
function ignoreSpaces(string) {
	var temp = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i=0; i<splitstring.length; i++)
	temp += splitstring[i];
	return temp;
}