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;
}


var dtCh= "/";
var minYear=1900;
var maxYear=2100;

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 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 ser un número entero.")
			return false;
			}
    }
    // All characters are numbers.
    return true;
}

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 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 esCoordenada(coorStr,campo){

	var posPuntoComa=coorStr.indexOf(";")
	var posGrado1=coorStr.indexOf("º")
	var posGrado2=coorStr.indexOf("º",posGrado1+1)
	var posMinuto1=coorStr.indexOf("'")
	var posMinuto2=coorStr.indexOf("'",posMinuto1+1)
	
	if (posPuntoComa != 7){
		alert("El formato de las " + campo + " debe ser : ggºmm'L;ggºmm'L")
		return false
		}

	if ((posGrado1 != 2)||(posGrado2 !=10)){
		alert("El formato de las " + campo + " debe ser : ggºmm'L;ggºmm'L")
		return false
		}

	if ((posMinuto1 != 5)||(posMinuto2 !=13)){
		alert("El formato de las " + campo + " debe ser : ggºmm'L;ggºmm'L")
		return false
		}

	if (coorStr.length != 15){
		alert("El formato de las " + campo + " debe ser : ggºmm'L;ggºmm'L")
		return false
		}
	var strGrados1=coorStr.substring(0,2);
	if (!esEnteroI(strGrados1)){
		alert("Introduzca un valor entero en los grados de la primera coordenada de las " + campo)
		return false
		}
	var strMinutos1=coorStr.substring(3,5);
	if (!esEnteroI(strMinutos1)){
		alert("Introduzca un valor entero en los minutos de la primera Coordenada de las " + campo)
		return false
		}
	var strLatitud1=coorStr.substring(6,7);
	if ((strLatitud1 != "N") && (strLatitud1 != "S")){
		alert("Introduzca una letra valida (N, S) en la primera Coordenada de las " + campo)
		return false
		}
	var strGrados2=coorStr.substring(8,10);
	if (!esEnteroI(strGrados2)){
		alert("Introduzca un valor entero en los grados de la segunda coordenada de las " + campo)
		return false
		}
	var strMinutos2=coorStr.substring(11,13);
	if (!esEnteroI(strMinutos2)){
		alert("Introduzca un valor entero en los minutos de la segunda Coordenada de las " + campo)
		return false
		}
	var strLatitud2=coorStr.substring(14,15);
	if ((strLatitud2 != "E") && (strLatitud2 != "W")){
		alert("Introduzca una letra valida (E, W) en la segunda Coordenada de las " + campo)
		return false
		}

return true

}
