var dtCh= "-";
var minYear=2010;
var maxYear=2100;

function isInteger(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 stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to 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){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    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 isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+2)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
//	var strYear=dtStr.substring(0,pos1)
//	var strMonth=dtStr.substring(pos1+1,pos2)
//	var strDay=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(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("El formato de la fecha ha de ser : dd-mm-yyyy \nCon digitos y separado por -")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Introduzca un formato correcto del MES con 2 digitos")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Introduzca un formato correcto del DÍA con 2 digitos")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Introduzca el AÑO en 4 digitos entre "+minYear+" y "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("El formato de la fecha ha de ser : dd-mm-yyyy \nCon digitos y separado por -")
		return false
	}
	return true
}

function esfecha(input){
	var dt=input
	if (dt.value!="" && dt.value!="00-00-0000") {
		if (isDate(dt.value)==false){
			dt.focus()
			return false
		}
	}
    return true
}

function longitud(input, nombre, minima, maxima) {
	if (input.value.length<minima) {
		if (minima > 1) {
			alert("Debes introducir un valor correcto en el campo " + nombre + ",\nque debe tener un mínimo de " + minima + " caracteres.");
		} else {
			alert("El campo " + nombre + " no puede estar vacío.");
		}
		input.focus();
		return(false);
	}
	if (input.value.length>maxima) {
		diferencia=input.value.length-maxima;
		alert("Debes introducir un valor correcto en el campo " + nombre + ".\nDebe tener un máximo de " + maxima + " caracteres.\nSobran " + diferencia + ".");
		input.focus();
		return(false);
	}
	return(true);
}

function escero(input, nombre) {
	if (input.value=='0') {
		alert("Debes seleccionar un valor correcto en el campo " + nombre + ".");
		input.focus();
		return(false);
	}
	return(true);
}

function trim(input){
	var string=input.value;
	expresion=/^\s*(\w*)/gi;
	string = string.replace(expresion,"$1");
	expresion=/(\w*)\s*$/gi;
	string = string.replace(expresion,"$1");
	input.value=string;
}

function netejar(input) {
	trim(input);
	string = input.value;
	caractersDolents=/[<>]/g;
	string = string.replace(/\"/g,"\'\'");
	string = string.replace(caractersDolents,"");
	input.value = string;
}

function validar0() {
		netejar(formReservas0.entrada);
		netejar(formReservas0.noches);
		if ( (esfecha(formReservas0.entrada)==false)
			|| (longitud(formReservas0.entrada,"Fecha entrada",10,10)==false)
			|| (longitud(formReservas0.noches,"Nº Noches",1,3)==false)
			) {
				return(false);
		}
		if ((formReservas0.noches.value == 0) || (formReservas0.noches.value == "") || (formReservas0.noches.value > 365)) {
			alert("ERROR DE VALIDACIÓN");
			formReservas0.noches.focus();
			return(false);
		}
		if (fecha_compara(formReservas0.entrada.value,formReservas0.hoyfecha.value)) {
			alert("ERROR DE VALIDACIÓN\nLA FECHA NO PUEDE SER INFERIOR AL DIA DE HOY");
			formReservas0.entrada.focus();
			return(false);
		}
		formReservas0.submit();
}

function verificarNum(n){
	permitidos=/[^0-9.]/;
	if(permitidos.test(n.value)){
		n.value="";
		n.focus();
	}
}

function fecha_compara(l_fecha1, l_fecha2) {
	//l_fecha1 op l_fecha2 ret: -1 = menor 0=igual 1=mayor 
	suma=new Date(l_fecha1.substring(6,10),l_fecha1.substring(3,5)-1,l_fecha1.substring(0,2));
	var l_anio=suma.getYear();
	var l_mes =suma.getMonth()+1;
	var l_dia =suma.getDate();
	
	suma2=new Date(l_fecha2.substring(6,10),l_fecha2.substring(3,5)-1,l_fecha2.substring(0,2));
	var l_anio2=suma2.getYear();
	var l_mes2 =suma2.getMonth()+1;
	var l_dia2 =suma2.getDate();
	
	if(l_anio < l_anio2)return(-1);
	else if(l_anio > l_anio2)return(0);
	else if(l_mes < l_mes2)return(-1);
	else if(l_mes > l_mes2)return(0);
	else if(l_dia < l_dia2)return(-1);
	else if(l_dia > l_dia2)return(0);
	else return(0);
}
