﻿function Combos(idavuelta, 
				diaSalida, mesSalida, 
				diaLlegada, mesLlegada, 
				calendarSalida, calendarLlegada, 
				fechaSalida, fechaLlegada,
				adultos, adultosNR, menores, menoresNR, bebes, bebesNR) {
	
	this.dia = new Object();
	this.mes = new Object();
	this.calendar = new Object();
	this.fecha = new Object();
	this.hoy = new Object();
	this.pax = 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;
	
	this.pax.adultos = adultos; 
	this.pax.adultosNR = adultosNR; 
	this.pax.menores = menores; 
	this.pax.menoresNR = menoresNR; 
	this.pax.bebes = bebes; 
	this.pax.bebesNR = bebesNR; 
	
	this.dias = new Array("dom", "lun", "mar", "mié", "jue", "vie", "sab");
	this.meses = new Array("Ene", "Feb", "Mar", "Abr", "May", "Jun", "Jul", "Ago", "Sep", "Oct", "Nov", "Dic");
	
	this.idavuelta = idavuelta;
	
	// 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) {
		
		if (this.dia[campo].selectedIndex >= 0)
			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) {
		
		var selectedDay = this.getSelectedDia(campo);
		
		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 (selectedDay != undefined) {
				if (selectedDay == 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);
			}
			else {
				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);
		}
		//si habia un dia seleccionado, pero se sale de rango dentro del nuevo mes
		if ( (selectedDay != undefined) && (this.getSelectedDia(campo) != selectedDay) ) {	
			if (selectedDay < this.dia[campo].options[0].value)
				this.dia[campo].options[0].selected = true;
			else if (selectedDay > this.dia[campo].options[this.dia[campo].options.length-1].value)
				this.dia[campo].options[this.dia[campo].options.length-1].selected = true;
		}
		
		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())
			dia = this.hoy.dia();*/
		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;
			}
		}
	}
	this.checkIdaVuelta = function () {
		if (this.idavuelta[0].checked)
			document.getElementById('FilaLlegada').style.display = 'none';
		else if (this.idavuelta[1].checked)
			document.getElementById('FilaLlegada').style.display = '';
	}
	
	// enviar el formulario
	this.enviar = function () {
		var na = parseInt(0 + this.pax.adultos.value, 10) + parseInt(0 + this.pax.adultosNR.value, 10);
		var nm = parseInt(0 + this.pax.menores.value, 10) + parseInt(0 + this.pax.menoresNR.value, 10);
		var nb = parseInt(0 + this.pax.bebes.value, 10) + parseInt(0 + this.pax.bebesNR.value, 10);
		var nt = na + nm + nb;
		var origen = document.getElementById("txtOrigen");
		var destino = document.getElementById("txtDestino");
		
		if (origen.options[origen.selectedIndex].value == "") {
			alert('Seleccione un origen');
			return false;	
		}
		if (destino.options[destino.selectedIndex].value == "") {
			alert('Seleccione el destino');
			return false;	
		}
	
		if ( (this.idavuelta[1].checked) && (this.fecha.salida > this.fecha.llegada) ) {
			alert('La fecha de llegada no puede ser anterior a la de salida');
			return false;
		}
		if (nt < 1) {
			alert('Seleccione 1 pasajero como mínimo');
			return false;
		}
		if (nt > 8) {
			alert('Seleccione 8 pasajeros como máximo');
			return false;
		}
		if (nb > na) {
			alert('El número de pasajeros adultos ha de ser mayor o igual que el número de bebés');
			return false;
		}

		return true;
	}
}
