// Conjunto de funciones para el calendario

var contenedor_calendario = "envoltorio-calendario"

// ----------------------------------
// Funcion verificarCalendario
// ----------------------------------

function verificarCalendario() {

	enviar_formulario = true;
	
	fecha_inicio_string = document.forms.paso_1.fecha_recogida.value;
	fecha_fin_string = document.forms.paso_1.fecha_devolucion.value;
	
	fecha_inicio = convertirEnFecha(fecha_inicio_string,0);
	fecha_fin = convertirEnFecha(fecha_fin_string,0);
	
	// Si no se han seleccionado recogida y devolución devolvemos un error.
	
	if (fecha_inicio_string == "" || fecha_fin_string == "") {
	
		alert(DEBE_INDICAR_FECHAS);
		
		enviar_formulario = false;
		
		return (false);
	
	}
	
	// Si recogida es anterior a devolución mostramos un error.
	
	if (fecha_inicio > fecha_fin) {
	
		alert(FECHA_DEVOLUCION_POSTERIOR);
		
		enviar_formulario = false;
		
		return false;
	
	}
	
	// Si recogida y devolución son el mismo día verificamos las horas.
	
	if (fecha_inicio_string == fecha_fin_string) {
	
		// Obtenemos las horas como enteros
	
		hora_inicio = document.forms.paso_1.hora_recogida.value;
		hora_fin = document.forms.paso_1.hora_devolucion.value;

		hora_inicio_int = parseFloat(hora_inicio.substr(0,2));
		hora_fin_int = parseFloat(hora_fin.substr(0,2));	
		
		if (hora_inicio_int >= hora_fin_int) {
		
			alert(HORA_DEVOLUCION_POSTERIOR);

			enviar_formulario = false;
			
			return (false);
		
		}
	
	}

	if(enviar_formulario) {
	
		document.forms.paso_1.submit();
	
	}

}

// ----------------------------------
// Funcion convertirEnFecha
// ----------------------------------

function convertirEnFecha(fecha,hora) {

	ano_temp = parseFloat(fecha.substr(6,4));

	mes_temp = parseFloat(fecha.substr(3,2)) - 1;

	dia_temp = parseFloat(fecha.substr(0,2));

	fecha_temp = new Date(ano_temp, mes_temp, dia_temp, hora, 0, 0);

	return fecha_temp;
		
}

function abrirCalendario(idioma,formulario,campo,bandera,ruta_referencia) {

	document.getElementById(contenedor_calendario).style.visibility = "visible";

	var fecha_inicio = '';
	var fecha_fin = '';
	var dia;
	var mes;
	var anio;

	switch (campo) {

	case "fecha_recogida":
		//alert(ruta_referencia+"modulos/gestion_reservas/libs/calendario.asp?campo="+campo+"&form="+formulario+"&idioma="+idioma+"&fecha_inicio="+fecha_inicio+"&fecha_aux="+fecha_inicio+"&ruta_referencia="+ruta_referencia, contenedor_calendario);
		ajax.AbrirHTTP(ruta_referencia+"modulos/gestion_reservas/libs/calendario.asp?campo="+campo+"&form="+formulario+"&idioma="+idioma+"&fecha_inicio="+fecha_inicio+"&fecha_aux="+fecha_inicio+"&ruta_referencia="+ruta_referencia, contenedor_calendario);
		break;

	case "fecha_devolucion":

		fecha_inicio = eval("document.forms." + formulario + ".fecha_recogida.value;");

		if (fecha_inicio == '') {

			if(ruta_referencia == "../") {
				alert(DEBE_INDICAR_FECHA_RECOGIDA);
			}
			else {
				alert(DEBE_INDICAR_FECHA_INICIO);
			}
		}
		else {

			dia = fecha_inicio.substr(0,2);

			//A no ser que lo indiquemos con bandera = 1, el mismo día de recogida no puede ser el de devolucion
			if(bandera == 0) {
				dia++;
			}

			//if (dia < 10) {
			//	dia = '0' + dia
			//}

			fecha_inicio = dia + fecha_inicio.substr(2,8);

			if (verificar_fecha (fecha_inicio)==false) {
				dia = '01';
				mes = parseFloat(fecha_inicio.substr(3,2)) + 1;
				anio = fecha_inicio.substr(6,4);

				if (mes == 13) {
					mes = '01';
					anio = parseFloat(anio) + 1;
					}
				fecha_inicio = dia + '/' + mes + '/' + anio;
			}

			//alert("../modulos/gestion_reservas/libs/calendario.asp?campo="+campo+"&form="+formulario+"&fecha_inicio="+fecha_inicio+"&fecha_aux="+fecha_inicio, contenedor_calendario)
			ajax.AbrirHTTP(ruta_referencia+"modulos/gestion_reservas/libs/calendario.asp?campo="+campo+"&form="+formulario+"&idioma="+idioma+"&fecha_inicio="+fecha_inicio+"&fecha_aux="+fecha_inicio+"&ruta_referencia="+ruta_referencia, contenedor_calendario);
			//alert("fin");

		}

		break;
	   }

}

function muestraIframe(nombre_iframe) {
	if(!window.attachEvent) return false;
	var iframes = document.getElementsByTagName("iframe");
	for( var i=0; i<iframes .length; i++ ){
		if (iframes[i].name == nombre_iframe) {
			iframes[i].style.display = "block";
		}
	}


}


function ocultaIframe(nombre_iframe) {
	if(!window.attachEvent) return false;
	var iframes = document.getElementsByTagName("iframe");
	for( var i=0; i<iframes .length; i++ ){
		if (iframes[i].name == nombre_iframe) {
			iframes[i].style.display = "none";
		}
	}
}

function cambiarMesCalendario(url) {
	ajax.AbrirHTTP(url, contenedor_calendario);
}

// Escribe en el formulario la fecha seleccionada en el calendario

function escribirFecha(fecha, formulario, campo){
	eval ("document.forms." + formulario + "." + campo + ".value='" + fecha + "'");
	ocultaIframe("bajo-calendario");
	document.getElementById(contenedor_calendario).style.visibility="hidden";
	
	verificarLimiteRecogida();
	calcularDias();
}



function verificar_fecha(field){

var checkstr = "0123456789";
var Fecha = field;
var Datevalue = "";
var DateTemp = "";
var seperator = "/";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var i;
   err = 0;
   DateValue = Fecha;
   /* Delete all chars except 0..9 */
   for (i = 0; i < DateValue.length; i++) {
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
	     DateTemp = DateTemp + DateValue.substr(i,1);
	  }
   }
   DateValue = DateTemp;
   /* Always change date to 8 digits - string*/
   /* if year is entered as 2-digit / always assume 20xx */
  // if (DateValue.length == 6) {
  //   DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
   if (DateValue.length != 8) {
      err = 19;
   }
   /* year is wrong if year = 0000 */
   year = DateValue.substr(4,4);
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = DateValue.substr(2,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = DateValue.substr(0,2);
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }

	if (DateTemp=='' || DateTemp=='undefined') {
		err=27;
	}
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
     // DateField.value = day + seperator + month + seperator + year;
	  return (true);
	  
   }
   /* Error-message if err != 0 */
   else {
     //  alert("Fecha incorrecta. Formato válido DD/MM/AAAA");
    //  DateField.select();
	 // DateField.focus();
	  return (false);
   }

}
