// funciones para validación de fechas
function Date_toSpanishString() {
	return ( this.getDate() + "/" + (this.getMonth() + 1) + "/" + this.getFullYear() )
}
Date.prototype.toSpanishString=Date_toSpanishString;

function Date_iniWithSpanishString(cadena) {
	var i=cadena.indexOf("/");
	var year = cadena.substr( cadena.lastIndexOf("/")+1 )
	if (year.length == 2)
		if (parseInt(year, 10) > 30 )
			 year = "19" + "" + year
		else
			 year = "20" + "" + year

	this.setFullYear( 
		parseInt( year, 10 ),
		parseInt( cadena.substring( i+1,cadena.lastIndexOf("/") ), 10 ) - 1,
		parseInt( cadena.substr(0,i), 10 )
	);
	
}
Date.prototype.iniWithSpanishString=Date_iniWithSpanishString;


function validStrDate(cadena) {
	var sAaaa, sMm, sDd, aaaa, mm, dd, i, bisiesto=false;

	i=cadena.indexOf("/");
	sAaaa=cadena.substr( cadena.lastIndexOf("/")+1 );
	sMm=cadena.substring(i+1,cadena.lastIndexOf("/"));
	sDd=cadena.substr(0,i);
	
	// validación longitud
	if (sAaaa.length == 2 )
		if (parseInt(sAaaa, 10) > 30 )
			 sAaaa = "19" + "" + sAaaa
		else
			 sAaaa = "20" + "" + sAaaa
			 
	if ( sAaaa.length != 4 || sMm.length != 2 || sDd.length != 2 ) return false;
	
	// validación numérica
	if ( isNaN(sAaaa) || isNaN(sMm) || isNaN(sDd) ) return false;
	aaaa=parseInt(eval(sAaaa), 10);
	mm=parseInt(eval(sMm), 10);
	dd=parseInt(eval(sDd), 10);
    //if ( isNaN(aaaa) || isNaN(mm) || isNaN(dd) ) return false;
	
	// comprobación año
	if ( aaaa < 1980 || aaaa > 2100 ) return false;
	
	// comprobación mes
	if ( mm < 1 || mm > 12 ) return false;
	
	// comprobacion dia
	if ( dd < 1 || dd > 31 ) return false;
	
	// comprobación dia de meses no febrero
	if ( ( mm == 4 || mm == 6 || mm == 9 || mm == 11 ) && dd > 30 ) return false;
	
	// comprobación año bisiesto
	if ( mm == 2 ) {
		if ( aaaa % 4 == 0 ) bisiesto=true;
		if ( aaaa % 100 == 0 ) bisiesto=false;
		if ( aaaa % 400 == 0 ) bisiesto=true;
		if ( bisiesto ) {
			if ( dd > 29 ) return false;
		} else {
			if ( dd > 28 ) return false;
		}
	}
	return true;
}

function validStrDateBetween(sFecha,sIni,sFin) {

	if ( !validStrDate(sFecha) ) 
		return false;
    //   return true

	var Fecha=new Date(), Ini=new Date(), Fin=new Date();
	Fecha.iniWithSpanishString(sFecha);
	Ini.iniWithSpanishString(sIni);
	if ( sFin != null ) 
		Fin.iniWithSpanishString(sFin);
	
	if ( Date.UTC( Fecha.getFullYear(), Fecha.getMonth(), Fecha.getDate()) < Date.UTC( Ini.getFullYear(), Ini.getMonth(), Ini.getDate()) 	|| 
			Date.UTC( Fecha.getFullYear(), Fecha.getMonth(), Fecha.getDate()) > Date.UTC( Fin.getFullYear(), Fin.getMonth(), Fin.getDate()) ) 
			return false;
	return true;
}

