// JavaScript Document

	function comprobarSiBisisesto(anio){
		if ( ( anio % 100 != 0) && ((anio % 4 == 0) || (anio % 400 == 0))) {
			return true;
		} else {
			return false;
		}
	}
	
	//12.04.11 carmen -- Convierte la fecha de dd-mm-aaaa o dd.mm.aa a dd/mm/aaaa
	function formatoFechaCorrecto(textboxname) {
		var fechastr;
		var txt;
		//ECD - 11/05/2011 - lo cogemos mediante JQuery
		//txt = document.getElementById(textboxname);
		txt = $("#" + textboxname);
		if (txt!=null){
	
			//fechastr = txt.value;
			fechastr = txt.val();
			
			//ECD - 20/04/2011 - si no tiene nada, salimos
			if (fechastr=='') return true;
			
			while (fechastr.indexOf('-') >= 0) {
				fechastr = fechastr.replace("-","/");
			}
			while (fechastr.indexOf('.') >= 0) {
				fechastr = fechastr.replace(".","/");
			}
			
			/* buscarmos los dos divisores de la fecha, sino existe alguno de los dos, la fecha esta mal escrita */
			pos1 = fechastr.indexOf("/");
			pos2 = fechastr.lastIndexOf("/");
			
			if (pos1 > 0 && pos2 > pos1) {
				var dia = fechastr.substring(0,pos1);
				var mes = fechastr.substring(pos1+1,pos2);
				var anio = fechastr.substring(pos2+1);
				
				if (anio.length < 4) {
					if (anio.length == 2) {
						anio = "20" + anio;
					} else if (anio.length == 3) {
						anio = "2" + anio;
					} else if (anio.length == 1) {
						anio = "201" + anio;
					}
				}
				if (mes.length < 2) {
					mes = "0" + mes;
				}
				if (dia.length < 2) {
					dia = "0" + dia;
				}
		
				fechastr = dia + "/" + mes + "/" + anio;
			}
			
			if (esFechaValida(fechastr)==true) {
				//document.getElementById(textboxname).value = fechastr;
				$("#" + textboxname).val(fechastr);
			} else { 
				//document.getElementById(textboxname).value = '';
				$("#" + textboxname).val('');
			}
		}
	}
	
	function numDiasMes(mnth, yr) {
		rem = yr % 4;
		if(rem == 0) {
			leap = 1;
		} else {
			leap = 0;
		}
		noDays = 0;
		if ((mnth == 1) || (mnth == 3) || (mnth == 5) ||
			(mnth == 7) || (mnth == 8) || (mnth == 10) ||
			(mnth == 12)) {
			noDays = 31;
		} else if (mnth == 2) {
			noDays = 28 + leap;
		} else {
			noDays = 30;
		}
		return noDays;
	}//numDiasMes()
	
	//ECD - 04/05/2011 La siguiente función, devuelve el número de día a partir de la fecha
	//correspondiente.
	function numDiaFechaStr(fechaStr) {
		var fechaDt=GetDateStr(fechaStr);
		return numDiaFecha(fechaDt);
	}//numDiaFechaStr()

	function numDiaFecha(fecha) {
		//var fechaDt=fecha;
		//var fechaIniAno=new Date(fechaDt.getFullYear()-1,11,31);
		var auxDiaAno;
		//auxDiaAno = DateDiff.inDays(fechaIniAno, fechaDt);
		auxDiaAno=fecha.getDOY();
		return auxDiaAno;
	}//numDiaFecha()
	
	
	/* 08.04.11 */	
	/*ECD - cambio la función porque falla en IE6.*/
	/*function compare_dates(fecha_ini, fecha_fin) {
		var xMonth=fecha_ini.substring(3, 5);
		var xDay=fecha_ini.substring(0, 2);
		var xYear=fecha_ini.substring(6,10);
		var yMonth=fecha_fin.substring(3, 5);
		var yDay=fecha_fin.substring(0, 2);
		var yYear=fecha_fin.substring(6,10);
		
		if (xYear> yYear) {
			return(true);
		} else {
			if (xYear == yYear) { 
				if (xMonth> yMonth) {
					return(true);
				} else { 
					if (xMonth == yMonth) {
						if (xDay> yDay) return(true);
						else return(false);
					} else return(false);
				}
			} else return(false);
		}
	}*/
	function compare_dates(fecha_ini, fecha_fin) {
	//devuelve true si las fechas no son correctas.
	//ECD - 12/09/2011 - devuelve false si la fecha es menor o igual que la fecha actual. (antes solo menor)
		var dFecha_ini;
		var dfecha_fin;
		
		dFecha_ini=GetDateStr(fecha_ini);
		dFecha_fin=GetDateStr(fecha_fin);
		
		if (dFecha_fin<=dFecha_ini)
		{
			return true;
		} else return false;
		
	}
	
	
	function comparaFechas(fex) {	
	//devuelve false si la fecha es menor que la fecha actual.
	//devuelve true si la fecha es correcta. 
		if (fex.length==0) return true; 
		
		//ECD - 12/09/2011 --> var fex_actual = new Date();
		var fex_actual=today();
		
		var dFecha_ini;
		dFecha_ini=GetDateStr(fex);

		if (dFecha_ini<fex_actual)
		{
			return false;
		} else return true;
		
		/*
		var tam = fex.length;
		var dia = fex.substring(0,2);
		var mes = fex.substring(3,5);
		var anio = fex.substring(tam-4);
		
		var anio_actual = fex_actual.getFullYear();
		var mes_actual = fex_actual.getMonth() + 1;
		var dia_actual = fex_actual.getDate();
		if (dia_actual<10)
			dia_actual="0"+dia_actual;
		if (mes_actual<10)
			mes_actual="0"+mes_actual;
				
		if (anio <= anio_actual) {
			//alert('Es el mismo año');
			if (mes <= mes_actual) {
				//alert('Es el mismo mes o menor');
				if (mes <= mes_actual) {
					if (mes < mes_actual) {
						//alert('No puede introducir una fecha menor al dia actual.');
						return (false);
					} else { //si es el mismo mes
						if (dia < dia_actual) {
							//alert('No puede introducir una fecha menor al dia actual.');
							return (false);
						} 
					}
				} 
			} 
		} 
		return (true);	*/
		
		
	}
	
	// comprueba números **************************************
	function IsNum(str) {
		// Return immediately if an invalid value was passed in
		if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
			return false;
		var isValid = true;
		// convert to a string for performing string comparisons.
		str += "";	
		// Loop through length of string and test for any numeric characters
		for (i = 0; i < str.length; i++) {
			// Numeric must be between "0"-"9"
			if (!((str.charAt(i) >= "0") && (str.charAt(i) <= "9"))) {
					isValid = false;
					break;
			}	
		} // END for   
		return isValid;
	}  // end IsNum
	//*********************************************************

	function Date_AddWeek(fecha){
		//la fecha tiene el siguiente formato dd/mm/aaaa
		var myDate =GetDateStr(fecha);
		myDate.setDate(myDate.getDate() + 7);
		
		var aux="";
		aux=DateToStr(myDate);
		return aux;
	}
	
	function Date_AddDays(fecha,numDias){
		//la fecha tiene el siguiente formato dd/mm/aaaa
		var myDate =GetDateStr(fecha);
		myDate.setDate(myDate.getDate() + numDias);
		
		var aux="";
		aux=DateToStr(myDate);
		return aux;
	}
	
	function GetDateStr(fecha) {
		var myDate = new Date(fecha.substring(3, 5) + '/' + fecha.substring(0, 2) + '/' +fecha.substring(6, 10));
		return myDate;
	}
	
	function DateToStr(myDate) {
		var aux="";
		aux=Right('0' + myDate.getDate(),2) + '/' + Right('0' + (parseInt(myDate.getMonth())+1),2) + '/' + myDate.getFullYear();
		return aux;
	}
	

	function Left(str, n){
		if (n <= 0)
			return "";
		else if (n > String(str).length)
			return str;
		else
			return String(str).substring(0,n);
	}
	
	function Right(str, n){
		if (n <= 0)
		   return "";
		else if (n > String(str).length)
		   return str;
		else {
		   var iLen = String(str).length;
		   return String(str).substring(iLen, iLen - n);
		}
	}
	
	var DateDiff = {
	 
		inDays: function(d1, d2) {
			var t2 = d2.getTime();
			var t1 = d1.getTime();
	 
			return parseInt((t2-t1)/(24*3600*1000));
		},
	 
		inWeeks: function(d1, d2) {
			var t2 = d2.getTime();
			var t1 = d1.getTime();
	 
			return parseInt((t2-t1)/(24*3600*1000*7));
		},
	 
		inMonths: function(d1, d2) {
			var d1Y = d1.getFullYear();
			var d2Y = d2.getFullYear();
			var d1M = d1.getMonth();
			var d2M = d2.getMonth();
	 
			return (d2M+12*d2Y)-(d1M+12*d1Y);
		},
	 
		inYears: function(d1, d2) {
			return d2.getFullYear()-d1.getFullYear();
		}
	}
	
	//19.04.11 carmen -- para conocer si la fecha de hoy es superior al valor que le pasemos
	function compararFechas(fecha) {
		var f = new Date();
		var dia = f.getDate();
		var mes = f.getMonth() + 1 ;
		var anyo = f.getFullYear();
		if (mes < 10) {
			mes = "0" + mes;
		}
		var xMonth = mes;
		var xDay = dia;
		var xYear = anyo;
		
		var yMonth = fecha.substring(3, 5);
		var yDay = fecha.substring(0, 2);
		var yYear = fecha.substring(6,10);
		
		if (xYear> yYear) {
			return(true);
		} else {
			if (xYear == yYear) { 
				if (xMonth> yMonth) {
					return(true);
				} else { 
					if (xMonth == yMonth) {
						if (xDay> yDay) return(true);
						else return(false);
					} else return(false);
				}
			} else return(false);
		}
	}
	
	//function FFin_SumarSemana(txtinicio, txtfin){
		function FFin_SumarDiasReserva(txtinicio, txtfin){
		var inicio = document.getElementById(txtinicio).value;
		var fin = document.getElementById(txtfin).value;
		var duracion;
		
		//Comprobamos si tenemos el número de días reserva que haya seleccionado el solicitante.
		if (diasReserva==null)
		{
			diasReserva=7;
		}
		
		if (diasReserva<=0)
		{
			diasReserva=7;
		}
		
		
		if (inicio != '') {		
			if (fin == '') {
				fin = Date_AddDays(inicio,diasReserva);
				document.getElementById(txtfin).value = fin;
			} else {
				if (GetDateStr(fin)<GetDateStr(inicio)){
					fin = Date_AddDays(inicio,diasReserva);
					document.getElementById(txtfin).value = fin;
				}
			}
			duracion=DateDiff.inDays(GetDateStr(inicio), GetDateStr(fin));
		} 
	}
	
	//ECD - Devuelve el número de día de una fecha.
	Date.prototype.getDOY = function() {
		var onejan = new Date(this.getFullYear(),0,1);
		return Math.ceil((this - onejan) / 86400000);
	}
	
	//ECD  - 220/06/2011 - Realiza los reemplazos necesarios para que se muestre correctamente
	//un texto html en un alert.
	function htmlToStringAlert(html){
		html=html.replace("<br>","\n");
		html=html.replace("<BR>","\n");
		html=html.replace("<br />","\n");
		html=html.replace("<BR />","\n");
		html=html.replace("&nbsp;"," ");
		return html;
	}
	
	//ECD - 12/09/2011 - Nos devuelve la fecha actual
	function today(){
		var currentTime = new Date();
		var fechaStr=DateToStr(currentTime);
		return GetDateStr(fechaStr);
	}
