//
// VERSION 2.0. 09/04/2010.
//

function esCC(i_entidad,i_oficina,i_digito,i_cuenta){
	var wtotal,wcociente, wresto;
	if (i_entidad.length != 4){
	return false;
	}
	if (i_oficina.length != 4){
	return false;
	}
	if (i_digito.length != 2){
	return false;
	}
	if (i_cuenta.length != 10){
	return false;
	}
	wtotal = i_entidad.charAt(0) * 4;
	wtotal += i_entidad.charAt(1) * 8;
	wtotal += i_entidad.charAt(2) * 5;
	wtotal += i_entidad.charAt(3) * 10;
	wtotal += i_oficina.charAt(0) * 9;
	wtotal += i_oficina.charAt(1) * 7;
	wtotal += i_oficina.charAt(2) * 3;
	wtotal += i_oficina.charAt(3) * 6;
	// busco el resto de dividir wtotal entre 11
	wcociente = Math.floor(wtotal / 11);
	wresto = wtotal - (wcociente * 11);
	//
	wtotal = 11 - wresto;
	if (wtotal == 11){
	wtotal=0;
	}
	if (wtotal == 10){
	wtotal=1;
	}
	if (wtotal != i_digito.charAt(0)){
	return false;
	}
	//hemos validado la entidad y oficina
	//-----------------------------------
	wtotal = i_cuenta.charAt(0) * 1;
	wtotal += i_cuenta.charAt(1) * 2;
	wtotal += i_cuenta.charAt(2) * 4;
	wtotal += i_cuenta.charAt(3) * 8;
	wtotal += i_cuenta.charAt(4) * 5;
	wtotal += i_cuenta.charAt(5) * 10;
	wtotal += i_cuenta.charAt(6) * 9;
	wtotal += i_cuenta.charAt(7) * 7;
	wtotal += i_cuenta.charAt(8) * 3;
	wtotal += i_cuenta.charAt(9) * 6;
	
	// busco el resto de dividir wtotal entre 11
	wcociente = Math.floor(wtotal / 11);
	wresto = wtotal - (wcociente * 11);
	//
	wtotal = 11 - wresto;
	if (wtotal == 11){wtotal=0;}
	if (wtotal == 10){wtotal=1;}
	
	if (wtotal != i_digito.charAt(1)){
	//alert(wtotal+' y no '+i_digito.charAt(1));
	return false;
	}
	// hemos validado la cuenta corriente
	
	return true;
}

function esCifoNif(cif,campo){
	
	par = 0
	non = 0
	letras="ABCDEFGHKLMNPQS"
	let=cif.charAt(0)
	
	if (!isNaN(let))
	  {
	  nif=cif
	  return esNIF(nif,campo)
    }
	
	if (cif.length!=9)
	  {
	  alert('El campo ' + campo + ' debe tener 9 dígitos.')
	  return false;
	  }
	
	if (letras.indexOf(let.toUpperCase())==-1)
	  {
	  alert("El comienzo del campo " + campo + " no es válido")
	  return false;
	  }
	
	for (zz=2;zz<8;zz+=2)
	  {
	  par = par+parseInt(cif.charAt(zz))
	  }
	
	for (zz=1;zz<9;zz+=2)
	  {
	  nn = 2*parseInt(cif.charAt(zz))
	  if (nn > 9) nn = 1+(nn-10)
	  non = non+nn
	}
	
	parcial = par + non
	
	control = (10 - ( parcial % 10))
	
	if (control==10) control=0
	
	if (control!=cif.charAt(8))
	  {
	  alert("El campo " + campo + " no es válido")
	  return false;
	  }
	return true;
	
}

function esNIF(abc,campo){
	
	dni=abc.substring(0,abc.length-1)
	let=abc.charAt(abc.length-1)
	if (!isNaN(let))
	 {
	  alert('Falta la letra en ' + campo)
	  return false
	 }
	else
	 {
	  cadena="TRWAGMYFPDXBNJZSQVHLCKET"
	  posicion = dni % 23
	  letra = cadena.substring(posicion,posicion+1)
	  if (letra!=let.toUpperCase())
	   {
		alert(campo + " no válido")
		return false;
	   }
	 }
	return true;

}

function esObligatorio(obligatorio,campo) {

	if (obligatorio == "") {
	    alert("El campo " + campo + " es obligatorio.");
		
	   	return false;
		}
	return true;

}

function esCorreoI(cadena) {
	// ¿se soportan expresiones regulares?
	var soportado = false;
	if (window.RegExp) {
  		var cadenaTemp = "a";
    	var regularTemp = new RegExp(cadenaTemp);
	    if (regularTemp.test(cadenaTemp)) soportado = true;
		}

	if (!soportado)
   		return (cadena.indexOf(".") > 2) && (cadena.indexOf("@") > 0);
		
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	return (!r1.test(cadena) && r2.test(cadena));
}

function esCorreo(correo,campo) {

	if (!esCorreoI(correo)) {
	    alert("El campo " + campo + " es incorrecto.");
	   	return false;
		}
	return true;
}

function esEntero(s,campo){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) {
			alert("El campo " + campo + " debe contener dígitos exclusivamente.")
			return false;
			}
    }
    // All characters are numbers.
    return true;
}

var dtCh= "/";
var minYear=1900;
var maxYear=2100;
function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
function daysInFebruary (year){
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}
function esEnteroI(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function esFecha(dtStr,campo){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strDay)
	day=parseInt(strMonth)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("El formato de la fecha " + campo + " debe ser : dd/mm/aaaa")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Introduzca un mes valido en " + campo)
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Introduzca un dia valido en " + campo)
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Introduzca un año de cuatro dígitos en " + campo + " entre "+minYear+" y "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || esEnteroI(stripCharsInBag(dtStr, dtCh))==false){
		alert("Por favor introduzca una fecha valida en " + campo)
		return false
	}
return true
}
function esHora(str,campo){
	var hora = str;
	if (hora=='') {return}
	if (hora.length!=5) {
		alert("El formato del campo " + campo + " debe ser HH:MM");
		return false
	}
	var i;
    for (i = 0; i < hora.length; i++){   
        var c = hora.charAt(i);
        if (((c < "0") || (c > "9")) && (c != ":")) {
			alert("El campo " + campo + " sólo puede contener números y los dos puntos.")
			return false;
			}
    }
	a=hora.charAt(0) //h1
	b=hora.charAt(1) //h2
	c=hora.charAt(2) //:
	d=hora.charAt(3) //m1
	e=hora.charAt(4) //m2
	if ((a==2 && b>3) || (a>2)) {alert("El valor que introdujo en la hora del campo " + campo + " no es correcto, introduzca dos números entre 00 y 23");return false}
	if (c!=':') {alert("Introduzca el caracter ':' para separar la hora de los minutos en el campo " + campo + ".");return false}
	if (d>5) {alert("El valor que introdujo en los minutos del campo " + campo + " no es correcto, introduzca dos números entre 00 y 59");return false}
	return true;
}

function mostrar_provincia(CodigoPostal) {

	// Si el código postal está vacío o su tamaño no es 5 o está fuera del rango...
	if (CodigoPostal=="" || CodigoPostal.length!=5 || CodigoPostal>52999) {
	
		Provincia="";
	
	} else {

		// Si el código postal no es un número...
		if (isNaN(CodigoPostal)) {
		
			Provincia="";
		
		} else {

			// Los dígitos a tener en cuenta para obtener la provincia son sólo los dos primeros
			Digitos=CodigoPostal.substr(0,2);

			if (Digitos=="01") Provincia="ALAVA";
			if (Digitos=="02") Provincia="ALBACETE";
			if (Digitos=="03") Provincia="ALICANTE";
			if (Digitos=="04") Provincia="ALMERÍA";
			if (Digitos=="05") Provincia="AVILA";
			if (Digitos=="06") Provincia="BADAJOZ";
			if (Digitos=="07") Provincia="ISLAS BALEARES";
			if (Digitos=="08") Provincia="BARCELONA";
			if (Digitos=="09") Provincia="BURGOS";
			if (Digitos=="10") Provincia="CACERES";
			if (Digitos=="11") Provincia="CADIZ";
			if (Digitos=="12") Provincia="CASTELLON";
			if (Digitos=="13") Provincia="CIUDAD REAL";
			if (Digitos=="14") Provincia="CORDOBA";
			if (Digitos=="15") Provincia="LA CORUNA";
			if (Digitos=="16") Provincia="CUENCA";
			if (Digitos=="17") Provincia="GERONA";
			if (Digitos=="18") Provincia="GRANADA";
			if (Digitos=="19") Provincia="GUADALAJARA";
			if (Digitos=="20") Provincia="GUIPUZCOA";
			if (Digitos=="21") Provincia="HUELVA";
			if (Digitos=="22") Provincia="HUESCA";
			if (Digitos=="23") Provincia="JAEN";
			if (Digitos=="24") Provincia="LEON";
			if (Digitos=="25") Provincia="LERIDA";
			if (Digitos=="26") Provincia="LA RIOJA";
			if (Digitos=="27") Provincia="LUGO";
			if (Digitos=="28") Provincia="MADRID";
			if (Digitos=="29") Provincia="MALAGA";
			if (Digitos=="30") Provincia="MURCIA";
			if (Digitos=="31") Provincia="NAVARRA";
			if (Digitos=="32") Provincia="ORENSE";
			if (Digitos=="33") Provincia="ASTURIAS";
			if (Digitos=="34") Provincia="PALENCIA";
			if (Digitos=="35") Provincia="LAS PALMAS";
			if (Digitos=="36") Provincia="PONTEVEDRA";
			if (Digitos=="37") Provincia="SALAMANCA";
			if (Digitos=="38") Provincia="S.C. DE TENERIFE";
			if (Digitos=="39") Provincia="CANTABRIA";
			if (Digitos=="40") Provincia="SEGOVIA";
			if (Digitos=="41") Provincia="SEVILLA";
			if (Digitos=="42") Provincia="SORIA";
			if (Digitos=="43") Provincia="TARRAGONA";
			if (Digitos=="44") Provincia="TERUEL";
			if (Digitos=="45") Provincia="TOLEDO";
			if (Digitos=="46") Provincia="VALENCIA";
			if (Digitos=="47") Provincia="VALLADOLID";
			if (Digitos=="48") Provincia="VIZCAYA";
			if (Digitos=="49") Provincia="ZAMORA";
			if (Digitos=="50") Provincia="ZARAGOZA";
			if (Digitos=="51") Provincia="CEUTA";
			if (Digitos=="52") Provincia="MELILLA";

		}

	}

	return Provincia;

}
