﻿function Combos(diaSalida, mesSalida, diaLlegada, mesLlegada, calendarSalida, calendarLlegada, fechaSalida, fechaLlegada) {
	this.dia = new Object();
	this.mes = new Object();
	this.calendar = new Object();
	this.fecha = new Object();
	this.hoy = new Object();
	
	this.fecha.salida = new Date();
	this.fecha.llegada = new Date();
	
	this.dia.salida = diaSalida;
	this.mes.salida = mesSalida;
	this.dia.llegada = diaLlegada;
	this.mes.llegada = mesLlegada
	
	this.calendar.salida = calendarSalida;
	this.calendar.llegada = calendarLlegada;
	
    if (IdIdiomaFront == 1) {
		this.dias = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
		this.meses = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	}
    else if (IdIdiomaFront == 3) {
		this.dias = new Array("Domenica", "Lunedi", "Martedi", "Mercoledi", "Jiovedi", "Venerdi", "Sabato");
		this.meses = new Array("Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre");
    }
    else if (IdIdiomaFront == 7) {
		this.dias = new Array("Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag");
		this.meses = new Array("Januar", "Februar", "Marz", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember");
    }
    else {
        this.dias = new Array("Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sabado");
        this.meses = new Array("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");				
    }
	// devuelve la fecha actual (sin hora)
	this.hoy = function() {
		var hoy = new Date();
		hoy.setSeconds(0);
		hoy.setMinutes(0);
		hoy.setHours(0);
		return(hoy);
	}
	
	// devuelve el año actual
	this.hoy.anio = function() {
		var hoy = new Date();
		return(hoy.getFullYear());
	}
	
	// devuelve el mes actual
	this.hoy.mes = function() {
		var hoy = new Date();
		return(hoy.getMonth() + 1);
	}
	
	// devuelve el día actual
	this.hoy.dia = function() {
		var hoy = new Date();
		return(hoy.getDate());
	}
	
	// devuelve la fecha máxima que se puede seleccionar (sin hora)
	this.max = function() {
		var max = new Date();
		max.setSeconds(0);
		max.setMinutes(0);
		max.setHours(0);
		max.setDate(1);
		max.setMonth(max.getMonth()+1);
		max.setDate(max.getDate()-1);
		max.setFullYear(max.getFullYear() + 1);
		return(max);
	}
	
	// devuelve el día seleccionado en el combo
	this.getSelectedDia = function(campo) {
		return this.dia[campo].options[this.dia[campo].selectedIndex].value;
	}
	
	// devuelve el mes seleccionado en el combo ('01'-'12')
	this.getSelectedMes = function(campo) {
		return this.mes[campo].options[this.mes[campo].selectedIndex].value.substr(0,2);
	}
	
	// devuelve el año seleccionado en el combo
	this.getSelectedAnio = function(campo) {
		return this.mes[campo].options[this.mes[campo].selectedIndex].value.substr(2);
	}	
	
	// devuelve la fecha seleccionada en formato Date
	this.getFecha = function (campo) {		
		return (this.fecha[campo]);
	}
	
	// devuelve la fecha seleccionada en formato dd/mm/aaaa
	this.getFechaForm = function (campo) {		
		return (this.getSelectedDia(campo) + "/" + this.getSelectedMes(campo) + "/" + this.getSelectedAnio(campo));
	}
	
	// devuelve el dia del mes de un combo dia (1-31)
	this.getCampoDia = function(campo,i) {
		return parseInt(this.dia[campo].options[i].value, 10);
	}
	
	// devuelve el mes de un combo mes (1-12)
	this.getCampoMes = function(campo,i) {
		return parseInt(this.mes[campo].options[i].value.substr(0,2),10);
	}
	
	// devuelve el año de un combo mes
	this.getCampoAnio = function(campo,i) {
		return parseInt(this.mes[campo].options[i].value.substr(2),10);
	}

	// devuelve el nº de días del mes
	this.longitudmesdia = function (mes, anio) {
		mes=mes*1;
		anio=anio*1;
	
		switch (mes) {
		   case 1:
		   case 3:
		   case 5:
		   case 7:
		   case 8:
		   case 10:
		   case 12:	   
				return  31;
			break;
	
		   case 4:
		   case 6:
		   case 9:
		   case 11:
				return 30;
			break;
			case 2 :
				if (((anio % 4) == 0) && ((anio % 100) != 0))
					return 29;
				else
					return 28;
			break;
		}
	}
	
	// actualiza la fecha seleccionada y rellena el campo fecha del calendario
	this.traspaso = function (campo) {
		
		this.fecha[campo].setDate(this.getSelectedDia(campo));
		this.fecha[campo].setMonth(this.getSelectedMes(campo) - 1);
		this.fecha[campo].setFullYear(this.getSelectedAnio(campo));
		
		this.calendar[campo].value = this.getFechaForm(campo);
	};

	// genera el combo de días a partir de la fecha indicada, hasta el final del mes
	this.actualizar_dias = function (campo, dia, mes, anio) {	
		longitud = this.longitudmesdia(mes,anio);
		
		this.dia[campo].length=0;
					
		Mi_Fecha = new Date(anio*1, mes*1-1, dia);
		z=0;
		
		for (i=dia; i <=longitud; i++) {
			j = Mi_Fecha.getDate();
			
			if (i < 10)
				j = "0" + j;									
			if (dia == j) 
				var option = new Option(j+" "+ this.dias[Mi_Fecha.getDay()], j, "selected");
			else
				var option = new Option(j+" "+ this.dias[Mi_Fecha.getDay()], j);
				
			this.dia[campo].options[z]=option;
			z++;
			
			Mi_Fecha.setDate(Mi_Fecha.getDate() + 1);
		}
		this.traspaso(campo);
	}
	
	// se lanza al cambiar el mes en el combo
	this.cambia_mes = function (campo) {
		var mes = this.mes[campo].options[this.mes[campo].selectedIndex].value.substr(0,2) * 1;
		var anio = this.mes[campo].options[this.mes[campo].selectedIndex].value.substr(2);
		
		if (mes > this.hoy.mes())
			dia = 1;
		else if ( (anio == this.hoy.anio()) && (mes == this.hoy.mes()) )
			dia = this.hoy.dia();
		else
			dia = 1;
		this.actualizar_dias(campo,dia,mes,anio);
		this.traspaso(campo);
	}
	
	// se lanza al cambiar el dia en el combo
	this.cambia_dia = function (campo) {
		this.traspaso(campo);
	}
	
	// pasa la fecha seleccioanda en el calendario a los combos
	this.calendar2combos = function(campo) {
		var fechaSeleccionada = new Date(this.fecha[campo]);
		
		// si la fecha seleccionada en el calendario está en los combos	, marcarla	
		for (j=0; j < this.mes[campo].options.length; j++) {					

			if ((this.getCampoAnio(campo,j)==fechaSeleccionada.getFullYear()) && (this.getCampoMes(campo,j)==fechaSeleccionada.getMonth()+1)) {
				this.mes[campo].options[j].selected = true;
				
				this.cambia_mes(campo);

				for (k=0; k < this.dia[campo].options.length; k++) {					
					
					if (this.getCampoDia(campo, k) == fechaSeleccionada.getDate()) {
						this.dia[campo].options[k].selected = true;
						this.fecha[campo] = fechaSeleccionada;
						this.traspaso(campo);
					}
				}						
				return;
			}
		}
	}
	
	// enviar el formulario
	this.enviar = function () {
		if (this.fecha.salida >= this.fecha.llegada) {
			alert('La fecha de llegada debe ser posterior a la de salida');
			return false;
		} else {
			return true;			
		}
	}
}
