// Conjunto de funciones para el calendario

var contenedor_calendario = "envoltorio-calendario";

// ----------------------------------
// Funcion verificarCalendario
// ----------------------------------

function verificarCalendario() {

	enviar_formulario = true;
	
	fecha_inicio = document.forms.paso_1.fecha_recogida.value;
	fecha_fin = document.forms.paso_1.fecha_devolucion.value;
	
	// Si no se han seleccionado recogida y devolución devolvemos un error.
	
	if (fecha_inicio == "" || fecha_fin == "") {
	
		alert("Debe indicar la fecha de recogida y la fecha de devolución del vehículo.");
		
		enviar_formulario = false;
	
	}
	
	// Si recogida y devolución son el mismo día verificamos las horas.
	
	if (fecha_inicio == fecha_fin) {
	
		// 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("La hora de devolución ha de ser posterior a la hora de recogida.");

			enviar_formulario = false;
		
		}
	
	}

	if(enviar_formulario) {
	
		document.forms.paso_1.submit();
	
	}

}

function probandoX(formulario,campo,bandera,ruta_referencia,posMOUSE) {

	

	if(checkBodyClass("interior")) {
	
//		alert(posMOUSE.x);
	
		//var posMOUSE=capturaMOUSE(event);

		document.getElementById(contenedor_calendario).style.left = posMOUSE.x + "px";
		document.getElementById(contenedor_calendario).style.top = posMOUSE.y + "px";

	}

}


function abrirCalendario(formulario,campo,bandera,ruta_referencia,coordenadas_click) {

	document.getElementById(contenedor_calendario).style.visibility = "visible";

	var fecha_inicio = '';
	var fecha_fin = '';
	var dia;
	var mes;
	var anio;
	
	idioma = eval("document.forms." + formulario + ".id_idioma.value;");
	idioma = idioma.toLowerCase();
	
	// Averiguamos si estamos en bienvenida o no para colocar el calendario en su ubicación correcta

	if(checkBodyClass("bienvenida")) {

		document.getElementById(contenedor_calendario).style.left = "-10px";
		document.getElementById(contenedor_calendario).style.top = "-30px";

	}

	if(checkBodyClass("interior")) {

		// No usamos las coordenadas del click porque necesitaríamos rehacer los cálculos
		
		// document.getElementById(contenedor_calendario).style.left = coordenadas_click.x + "px";
		// document.getElementById(contenedor_calendario).style.top = coordenadas_click.y + "px";

		switch (campo) {
	
		case "fecha_llegada":
			
			document.getElementById(contenedor_calendario).style.left = "180px";
		
		break;
		
		case "fecha_salida":

			document.getElementById(contenedor_calendario).style.left = "380px";		
		
		break;
		
		}
	
		document.getElementById(contenedor_calendario).style.top = "-95px";

	}
	
	switch (campo) {

	case "fecha_llegada":

		//ajax.AbrirHTTP(ruta_referencia+"modulos/gestion_reservas/libs/calendario.asp?campo="+campo+"&form="+formulario+"&fecha_inicio="+fecha_inicio+"&fecha_aux="+fecha_inicio+"&ruta_referencia="+ruta_referencia, contenedor_calendario);
		//ajax.AbrirHTTP("calendario_dinamico.html", contenedor_calendario);
		
		muestraIframe("bajo-calendario");
		
		ajax.AbrirHTTP(ruta_referencia+"stanquecms/scripts/calendario.asp?campo="+campo+"&form="+formulario+"&fecha_inicio="+fecha_inicio+"&fecha_aux="+fecha_inicio+"&ruta_referencia="+ruta_referencia+"&idioma="+idioma, contenedor_calendario);

		break;

	case "fecha_salida":

		fecha_inicio = eval("document.forms." + formulario + ".fecha_llegada.value;");
		
		if (fecha_inicio == '') {
		
			alert(FECHA_SALIDA_POSTERIOR_LLEGADA);

		}
		else {

			dia = fecha_inicio.substr(0,2).toLowerCase();

			//A no ser que lo indiquemos con bandera = 1, el mismo día de recogida no puede ser el de devolucion
			if(bandera == 1) {
				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;
			}
			
			muestraIframe("bajo-calendario");

			//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+"stanquecms/scripts/calendario.asp?campo="+campo+"&form="+formulario+"&fecha_inicio="+fecha_inicio+"&fecha_aux="+fecha_inicio+"&ruta_referencia="+ruta_referencia+"&idioma="+idioma, 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){

	// Creamos este switch dado que el nombre de los campos usados no corresponde con los campos que dictan
	// las especificaciones de Noray.

	switch (campo) {
		case "fecha_llegada":
			eval ("document.forms." + formulario + ".FechaLlegada.value='" + fecha + "'");
		break;
		case "fecha_salida":
			eval ("document.forms." + formulario + ".FechaSalida.value='" + fecha + "'");
		break;
	}
	
	eval ("document.forms." + formulario + "." + campo + ".value='" + fecha + "'");
	
	ocultaIframe("bajo-calendario");
	document.getElementById(contenedor_calendario).style.visibility="hidden";
}



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);
   }

}

