function CPProvincia(CP, provincia)
{
	if (CP.substr(0,2) != formatMesODia(provincia))
		return false;
	return true;
}
function tamanoCorrecto(campo, tamano, exacto)
{
	//exacto==1 -> lg de campo tiene que ser igual a tamano
	//exacto==0 -> lg de campo tiene que ser inferior o igual a tamano
	if (exacto && campo.length == tamano)
		return true;
	if (!exacto && campo.length <= tamano)
		return true;
	return false;
}

function esNIB(nib1, nib2, nib3, nib4)
{
	if (nib1=="" || nib2=="" || nib3=="" || nib4=="")
		return false;
	if (isNaN(nib1)) return false;
	if (isNaN(nib2)) return false;
	if (isNaN(nib3)) return false;
	if (isNaN(nib4)) return false;
	if (!tamanoCorrecto(nib1,  4, 1)) return false;
	if (!tamanoCorrecto(nib2,  4, 1)) return false;
	if (!tamanoCorrecto(nib3,  2, 1)) return false;
	if (!tamanoCorrecto(nib4, 10, 1)) return false;
	return true;
}

function tieneCifra(campo)
{
	for (i=0; i<campo.length; i++)
		if (!isNaN(campo.charAt(i)))
			return true;
	return false;
}

function numInvalido(tel)
{
	for (i=0; i<tel.length; i++)
		if (i==0)
			tmp=tel.charAt(i);
		else
			if (tmp!=tel.charAt(i))
				return false;
			else return true;
	return true;
}

function esTelEspanol(tel)
{
	if (!tamanoCorrecto(tel, 9, 1))
		return false;
	if (isNaN(tel))
		return false;
	if (tel.charAt(0) != '9' && tel.charAt(0) != '8' && tel.charAt(0) != '7' && tel.charAt(0) != '6')
		return false;
	return true;
}

function isEmail(email) {
//Modificación basada en el ejemplo de Bit Bucket Heaven
//Updated to be used with MAC-OS
var boolSalida = true;
    var invalidChars = " ~\'^\`\"*+=\\|][(){}$&!#%/:,;";
    // Checkea valor nulo "null"
    if (email == "")boolSalida=false;
    // Checkea caracteres inválidos definidos en "invalidChars"
    for (var i=0; i<invalidChars.length; i++) {
        var badChar = invalidChars.charAt(i);
        if (email.indexOf(badChar,0) > -1)boolSalida=false;
    }
    var lengthOfEmail = email.length;
    if ((email.charAt(lengthOfEmail - 1) == ".") || (email.charAt(lengthOfEmail - 2) == "."))boolSalida=false;
    var Pos = email.indexOf("@",1);
    if (email.charAt(Pos + 1) == ".")boolSalida=false;
    while ((Pos < lengthOfEmail) && ( Pos != -1)) {
        Pos = email.indexOf(".",Pos);
        if (email.charAt(Pos + 1) == ".")boolSalida=false;
        if (Pos != -1)Pos++;
    }
    // Por lo menos debe haber una @
    var atPos = email.indexOf("@",1);
    if (atPos == -1)boolSalida=false;
    // Pero solo una @
    if (email.indexOf("@",atPos+1) != -1)boolSalida=false;
    // Tambien checkea para al menos un punto "." despues de la @
    var periodPos = email.indexOf(".",atPos);
    if (periodPos == -1)boolSalida=false;
    if (periodPos+3 > email.length)boolSalida=false;
    return boolSalida;
}
function compruebaFecha(form)
{
	d = new Date();
	d1 = new Date();
	control=true;
	if (document.forms[0].mesHijo.value=="" && document.forms[0].mesHijo.value=="" && document.forms[0].mesHijo.value=="")
		return true;
	if (document.forms[0].mesHijo.value=="" || document.forms[0].mesHijo.value=="" || document.forms[0].mesHijo.value=="")
		control=false;
	d.setMonth(document.forms[0].mesHijo.value-1);
	d.setDate(document.forms[0].diaHijo.value);
	d.setYear(document.forms[0].anioHijo.value);
	diferencia=d1-d;
	if (diferencia<0)
		control=false;
	if ((eval(document.forms[0].mesHijo.value)==2 || eval(document.forms[0].mesHijo.value)==4 || eval(document.forms[0].mesHijo.value)==6 || eval(document.forms[0].mesHijo.value)==9 || eval(document.forms[0].mesHijo.value)==11) && eval(document.forms[0].diaHijo.value)==31)
		control=false;
	if (eval(document.forms[0].mesHijo.value)==2 && eval(document.forms[0].diaHijo.value)==30)
		control=false;
	if (eval(document.forms[0].mesHijo.value)==2 && eval(document.forms[0].diaHijo.value)==29 && ((eval(document.forms[0].anioHijo.value)%4) || (!(eval(document.forms[0].anioHijo.value)%4) && !(eval(document.forms[0].anioHijo.value)%400))))
		control=false;
	if (control)
	{
		document.forms[0].submit();
		return true;
	}
	alert("La fecha no es válida.");
	return false;
}

function compruebaFechaNacimiento(form)
{
	d = new Date();
	d1 = new Date();
	control=true;
	if (document.forms[0].mesHijo.value=="" || document.forms[0].mesHijo.value=="" || document.forms[0].mesHijo.value=="")
		control=false;
	d.setMonth(document.forms[0].mesHijo.value-1);
	d.setDate(document.forms[0].diaHijo.value);
	d.setYear(document.forms[0].anioHijo.value);
	diferencia=d1-d;
	diferencia=diferencia/24/60/60/1000;
	if (diferencia<eval("-275"))
		control=false;
	if ((eval(document.forms[0].mesHijo.value)==2 || eval(document.forms[0].mesHijo.value)==4 || eval(document.forms[0].mesHijo.value)==6 || eval(document.forms[0].mesHijo.value)==9 || eval(document.forms[0].mesHijo.value)==11) && eval(document.forms[0].diaHijo.value)==31)
		control=false;
	if (eval(document.forms[0].mesHijo.value)==2 && eval(document.forms[0].diaHijo.value)==30)
		control=false;
	if (eval(document.forms[0].mesHijo.value)==2 && eval(document.forms[0].diaHijo.value)==29 && ((eval(document.forms[0].anioHijo.value)%4) || (!(eval(document.forms[0].anioHijo.value)%4) && !(eval(document.forms[0].anioHijo.value)%400))))
		control=false;
	if (control)
	{
		return true;
	}
	return false;
}

function formatMesODia(i)
{
	if (i<10)
		return "0"+i+"";
	else
		return ""+i+"";
}

function contieneSoloLetras(str)
{
	for (i=0; i<str.length; i++)
		if (str.charCodeAt(i)<65 && str.charCodeAt(i)>90 || str.charCodeAt(i)<97 || str.charCodeAt(i)>122)
			return false;
	return true;
}

function contieneLetra(str)
{
	for (i=0; i<str.length; i++)
		if ((str.charCodeAt(i)>=65 && str.charCodeAt(i)<=90) || (str.charCodeAt(i)>=97 && str.charCodeAt(i)<=122))
			return true;
	return false;
}

function esComplONumValido(str)
{
	for (i=0; i<str.length; i++)
		if (!((str.charCodeAt(i)>=65 && str.charCodeAt(i)<=90) || (str.charCodeAt(i)>=97 && str.charCodeAt(i)<=122) || str.charCodeAt(i)==44 || str.charCodeAt(i)==32 || str.charCodeAt(i)==45 || (str.charCodeAt(i)>=48 && str.charCodeAt(i)<=57)))
			return false
	return true;
}

function getMonthName(i)
{
	switch(i)
	{
	 case  1: return "enero";
	 case  2: return "febrero";
	 case  3: return "marzo";
	 case  4: return "abril";
	 case  5: return "mayo";
	 case  6: return "junio";
	 case  7: return "julio";
	 case  8: return "agosto";
	 case  9: return "septiembre";
	 case 10: return "octubre";
	 case 11: return "noviembre";
	 case 12: return "diciembre";
	 defalut: return "";
	}
}
