function HotelvalidateCities() {
	var f = getHotelEntryForm();
	var checkstr = "";
	//Verificar ciudad
	if (f.td_A_City.value=="") {
		checkstr += "Introduce una ciudad destino\n";
	} else {
		f.td_A_City.value = Hotelcity(f.td_A_City.value);
	}
	return checkstr;
}

function HotelvalidatePassengers() {
	var f = getHotelEntryForm();
	var checkstr = "";
	var numAdults = f.td_select_adt?f.td_select_adt.selectedIndex:0;
	var numChilds = f.td_select_chd?f.td_select_chd.selectedIndex:0;
	var numOlds = 0;
   	var numTotal = 0 + numAdults + numChilds + numOlds;

	if (numTotal == 0) {
		checkstr += "Tienes que seleccionar las personas que van a viajar.\n";
	} else if (numTotal > 9) {
		checkstr += "Solo puede reservar un máximo de 9 plazas.\n";
	}
	return checkstr;
}

function HotelvalidateActualDate() {
	var f = getHotelEntryForm();
	var myDate = new Date();
	var checkstr = "";
	if (Number(f.td_D_Month[f.td_D_Month.selectedIndex].value.substring(4)) == myDate.getMonth()+1 && Number(f.td_D_Day[f.td_D_Day.selectedIndex].value) < myDate.getDate()) {
		checkstr = "No puede hacer busquedas en dias anteriores a hoy";
	}
	return checkstr;
}

function Hotelcity(v) {
	i=v.indexOf("(");
	j=v.lastIndexOf(")");
	if (j-i==4) return v.substring(i+1,j);
	return v;
}

function doHotelSearch(tipo) {
	var f = getHotelEntryForm();

	var checkstr = "";
	checkstr += HotelvalidateCities();

	checkstr += HotelvalidatePassengers();

	checkstr += HotelvalidateActualDate();

	var myDate = new Date();
	if (Number(f.td_D_Month.value.substring(4)) == (myDate.getMonth() + 1) && Number(f.td_D_Day.value) <= (myDate.getDate() + 2)) {
		alert("Recuerda que sólo podrás hacer reservas con, al menos, 2 días antes del día de la entrada al hotel");
		return;
	}

	if(checkstr!=""){
		alert(checkstr);
		return;
	}

	if ( tipo == 5 ) //Viajar.com
	{
		f.submit();
	}
	else if ( tipo == 1 ) //eDreams
	{
		window.document.frm_Search.td_.value = f.td_A_City.value;
		window.document.frm_Search.td_arrivalDay.value = f.td_D_Day.value;
		tempmes = f.td_D_Month.value.substring(4,6) - 1;
		window.document.frm_Search.td_arrivalMonth.value = tempmes;

		window.document.frm_Search.td_departureDay.value = f.td_R_Day.value;
		tempmes = f.td_R_Month.value.substring(4,6) - 1;
		window.document.frm_Search.td_departureMonth.value = tempmes;

		window.document.frm_Search.td_numberOfRooms.value = f.td_dobleroom.value;
		window.document.frm_Search.td_numberOfAdults.value = f.td_select_adt.value;
		window.document.frm_Search.td_numberOfChilds.value = f.td_select_chd.value;
		window.document.frm_Search.submit();
	}
	else if ( tipo == 2 ) //Atrapalo
	{
	    window.document.busqueda.td_id_destino3.value = f.td_A_City.value;
	    window.document.busqueda.td_entrada_trigger.value = f.td_D_Day.value + "/" + f.td_D_Month.value.substring(4,6) + "/" + f.td_D_Month.value.substring(0,4);
	    inicio = (f.td_D_Day.value * 1) + (f.td_D_Month.value.substring(4,6) * 30) + (f.td_D_Month.value.substring(0,4) * 365);
	    fin = (f.td_R_Day.value * 1) + (f.td_R_Month.value.substring(4,6) * 30) + (f.td_R_Month.value.substring(0,4) * 365);
	    window.document.busqueda.td_num_noches.value = fin - inicio;
	    window.document.busqueda.td_adultos.value = f.td_select_adt.value * f.td_dobleroom.value;
	    window.document.busqueda.td_num_habitaciones.value = f.td_dobleroom.value;
	    window.document.busqueda.td_num_adultos_1.value = f.td_select_adt.value;
	    window.document.busqueda.td_num_adultos_2.value = f.td_select_adt.value;
	    window.document.busqueda.td_num_adultos_3.value = f.td_select_adt.value;
	    window.document.busqueda.td_num_ninos_1.value = f.td_select_chd.value;
	    window.document.busqueda.td_num_ninos_2.value = f.td_select_chd.value;
	    window.document.busqueda.td_num_ninos_3.value = f.td_select_chd.value;
	    window.document.busqueda.td_dia_entrada.value = f.td_D_Day.value;
	    window.document.busqueda.td_mes_entrada.value = f.td_D_Month.value.substring(4,6);
	    window.document.busqueda.td_anyo_entrada.value = f.td_D_Month.value.substring(0,4);
	    window.document.busqueda.td_fecha_entrada.value = f.td_D_Day.value + "/" + f.td_D_Month.value.substring(4,6) + "/" + f.td_D_Month.value.substring(0,4);
		window.document.busqueda.submit();
	}
	else if ( tipo == 4 ) // Hotels.com
	{
		window.document.bookingForm.url.value += '?validateCity=true';
		window.document.bookingForm.url.value += '&validateDates=true';
		window.document.bookingForm.url.value += '&cid=70995';
		window.document.bookingForm.url.value += '&pageName=hotSearch';
		window.document.bookingForm.url.value += '&passThrough=true';
		window.document.bookingForm.url.value += '&submitted=true';
		window.document.bookingForm.url.value += '&mode=2';
		window.document.bookingForm.url.value += '&showPopup=true';
		window.document.bookingForm.url.value += '&currencyCode=EUR';
		window.document.bookingForm.url.value += '&locale=es';
		window.document.bookingForm.url.value += '&city=' + f.td_A_City.value;;
		window.document.bookingForm.url.value += '&arrivalDateString=' + f.td_D_Day.value + "/" + f.td_D_Month.value.substring(4,6) + "/" + f.td_D_Month.value.substring(0,4);
		window.document.bookingForm.url.value += '&departureDateString=' + f.td_R_Day.value + "/" + f.td_R_Month.value.substring(4,6) + "/" + f.td_R_Month.value.substring(0,4);
		window.document.bookingForm.url.value += '&numberOfRooms=' + f.td_dobleroom.value;
		window.document.bookingForm.url.value += '&dateStringFormat=dd/MM/yy';
		adultos = '';
        ninos = '';
		for (var i = 0; i < f.td_dobleroom.value; i++) {
			if (i > 0) {
            	adultos += ':';
            	ninos += ',';
            }
			adultos += f.td_select_adt.value;
            ninos += f.td_select_chd.value;
			window.document.bookingForm.url.value += '&room-' + i + '-adult-total=' + f.td_select_adt.value;
			window.document.bookingForm.url.value += '&room-' + i + '-child-total=' + f.td_select_chd.value;

			for (var j = 0; j < f.td_select_chd.value; j++) {
				window.document.bookingForm.url.value += '&room-' + i + '-child-' + j + '-total=11';
            }
        }
		window.document.bookingForm.url.value += '&guestInfo=' + adultos + '-' + ninos;;
		window.document.bookingForm.submit();
	}
	else if ( tipo == 3 ) //LastMinute
	{
		window.document.hotels_search.url.value += '?skin=eses.lastminute.com';
		window.document.hotels_search.url.value += '&locale=es_ES';
		window.document.hotels_search.url.value += '&CATID=4';
		window.document.hotels_search.url.value += '&returnURL=http://www.es.lastminute.com/site/viajes/hoteles/homepage_es_ES.html';
		window.document.hotels_search.url.value += '&errorURL=http://www.es.lastminute.com/site/viajes/hoteles/error.html';
		window.document.hotels_search.url.value += '&preserveName-skin=eses.lastminute.com';
		window.document.hotels_search.url.value += '&preserveName-CATEGORY=hotels';
		window.document.hotels_search.url.value += '&preserveName-requestURL=advanced';
		window.document.hotels_search.url.value += '&searchType=criteria';
		window.document.hotels_search.url.value += '&lmnLocation=' + f.td_A_City.value;
		window.document.hotels_search.url.value += '&lmnAnyStar=checked';
		window.document.hotels_search.url.value += '&lmnResultsOnMap=false';
	    inicio = (f.td_D_Day.value * 1) + (f.td_D_Month.value.substring(4,6) * 30) + (f.td_D_Month.value.substring(0,4) * 365);
	    fin = (f.td_R_Day.value * 1) + (f.td_R_Month.value.substring(4,6) * 30) + (f.td_R_Month.value.substring(0,4) * 365);
		window.document.hotels_search.url.value += '&lmnLengthOfStay=' + (fin-inicio);
		window.document.hotels_search.url.value += '&lmnCheckInDay=' + f.td_D_Day.value;

        if (f.td_D_Month.value.substring(4,5) == '0')
        	t1 = f.td_D_Month.value.substring(5,6);
		else
        	t1 = f.td_D_Month.value.substring(4,6);
		window.document.hotels_search.url.value += '&lmnCheckInMonth=' + t1;
		window.document.hotels_search.url.value += '&lmnCheckOutDay=' + f.td_R_Day.value;
        if (f.td_R_Month.value.substring(4,5) == '0')
        	t2 = f.td_R_Month.value.substring(5,6);
		else
        	t2 = f.td_R_Month.value.substring(4,6);
		window.document.hotels_search.url.value += '&lmnCheckOutMonth=' + t2;
		window.document.hotels_search.url.value += '&lmnRooms=' + f.td_dobleroom.value;

		for (var i = 1; i <= f.td_dobleroom.value; i++) {
			window.document.hotels_search.url.value += '&lmnAdultsRoom' + i + '=' + f.td_select_adt.value;
			window.document.hotels_search.url.value += '&lmnChildrenRoom' + i + '=' + f.td_select_chd.value;

			for (j = 1; j <= f.td_select_chd.value; j++) {
				window.document.hotels_search.url.value += '&lmnRoom' + i + 'ChildAge' + j + '=11';
            }

            temp = parseInt(f.td_select_chd.value,10) + 1;
			for (jj = temp; jj <= 4; jj++) {
				window.document.hotels_search.url.value += '&lmnRoom' + i + 'ChildAge' + jj + '=-1';
            }
        }

		window.document.hotels_search.submit();
	}
}

function getDaysMonth(month, year){
	var arrDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
	if(isLeapYear(year) && month==1) return 29;
	else return arrDays[month];
}


function getMes(m) {
	var arrMonthNames = ["Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"];
	return arrMonthNames[m];
}

function HotelcompleteDays2(comboType){
	var f = getHotelEntryForm();

	var size = eval("f.td_" + comboType + "_Day.length;");
	for (var i=size; i >= 0;i--) {
		eval("f.td_" + comboType + "_Day.options[" + i +"] = null;");
	}

	var month = eval("f.td_" + comboType + "_Month[f.td_" + comboType + "_Month.selectedIndex].value.substring(4) - 1;");
	var year = eval("f.td_" + comboType + "_Month[f.td_" + comboType + "_Month.selectedIndex].value.substring(0,4);");

	var numDays = getDaysMonth(month, year);

	//rellenar dias
	for (var i=1;i< numDays+1;i++) {
		var newDay = new Option(i,(i<10?"0"+i:i));
		eval("f.td_" + comboType + "_Day[f.td_" + comboType + "_Day.length] = newDay;");
	}
}

function HotelcompleteMonths2() {
	var myDate = new Date();

	var f = getHotelEntryForm();

	for (var i=0; i < 10;i++) {
		f.td_D_Month.options[0] = null;
		f.td_R_Month.options[0] = null;
	}

	for (var i=0;i<10;i++) {
		var monthIndex = myDate.getMonth()+i <= 11?myDate.getMonth()+i:myDate.getMonth()+i-12;
		var strMonth = getMes(monthIndex);
		var year = (myDate.getMonth() > monthIndex?myDate.getFullYear()+1:myDate.getFullYear());
		var newMonthD = new Option(strMonth + " " + year,year+""+((monthIndex+1)<10?"0"+(monthIndex+1):(monthIndex+1)));
		var newMonthR = new Option(strMonth + " " + year,year+""+((monthIndex+1)<10?"0"+(monthIndex+1):(monthIndex+1)));
		f.td_D_Month[f.td_D_Month.length] = newMonthD;
		f.td_R_Month[f.td_R_Month.length] = newMonthR;
	}
}
function HotelinitDate(initDays) {
	var myDate = new Date();
	var f = getHotelEntryForm();
	HotelcompleteMonths2();
	HotelcompleteDays2("D");
	HotelcompleteDays2("R");

	var initDMonth = myDate.getMonth();
	var initRMonth = myDate.getMonth();
	var initDDay = myDate.getDate() + initDays;
	//Si pasamos del maximo de dias del mes actual
	if (initDDay > f.td_D_Day.options.length) {
		initDDay = initDDay - f.td_D_Day.options.length;
		initDMonth = initDMonth + 1;
		initRMonth = initRMonth + 1;
	}

	var daysToNextSunday;
	var dayWeek = myDate.getDay()+initDays;
	if (dayWeek > 7) dayWeek = dayWeek - 7;
	daysToNextSunday = 7 - dayWeek;
	if (daysToNextSunday == 0) daysToNextSunday = 7;

	var initRDay = initDDay + daysToNextSunday;
	if (initRDay > f.td_R_Day.options.length) {
		initRDay = initRDay - f.td_R_Day.options.length;
		initRMonth = initRMonth + 1;
	}
	//Le sumamos uno a los initMonths, ya que el 0 corresponde a enero
	HotelsetDateCalendar(initDDay,initDMonth+1,"D",false);
	HotelsetDateCalendar(initRDay,initRMonth+1,"R",false);
	HotelchangeDateField2("D");
	HotelchangeDateField2("R");
}


function HotelCheck(){
	var f=document.forms['HOTEL_ENTRY_FORM'];
		var dYearMonthValue = f.td_D_Month.options[f.td_D_Month.selectedIndex].value;
		var dDayValue = f.td_D_Day.options[f.td_D_Day.selectedIndex].value;
		var dTimeIndex = f.td_D_Time.selectedIndex;

		var departureDate = new Date(dYearMonthValue.substring(0,4), parseInt(dYearMonthValue.substring(4,6))-1, dDayValue, dTimeIndex-1, 0, 0);
	var today=new Date();
	today.setTime(today.getTime()+(1000*60*60*24*3));
	today.setHours(7);
	alert(departureDate);
	alert(today);
	if(departureDate < today)
	{
		if(!confirm("Recuerda que sólo podrás hacer reservas con, al menos, 3 días de antelación al día de viaje"))
			return false;
	}
	return true;
}

function getHotelEntryForm() {

	var f;
	if (navigator.userAgent.indexOf("MSIE") >= 0) {
		for (var i=0;i < document.forms.length;i++) {
			if (document.forms[i].name == "HOTEL_ENTRY_FORM") {
				f=document.forms[i];
			}
		}
	} else {
		f=document.forms["HOTEL_ENTRY_FORM"];
	}

	return f;
}

function HotelchangeDateField2(dateType){
	var f = getHotelEntryForm();
	eval("f.td_" + dateType + "_Date.value = f.td_" + dateType + "_Day.value + '/' + f.td_" + dateType + "_Month.value.substring(4) + '/' + f.td_" + dateType + "_Month.value.substring(0,4);");
}

function isLeapYear (year) {
    var leapYear = false;
    if ((year % 400) == 0) {
        leapYear = true;
    } else if ((year % 100) == 0) {
        leapYear = false;
    } else if ((year % 4) == 0) {
        leapYear = true;
    } else {
        leapYear =false;
    }
    return leapYear;
}


function getDaysMonth(month, year){
	var arrDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
	if(isLeapYear(year) && month==1) return 29;
	else return arrDays[month];
}


function HotelcompleteDays2(comboType){
	var f = getHotelEntryForm();

	var size = eval("f.td_" + comboType + "_Day.length;");
	for (var i=size; i >= 0;i--) {
		eval("f.td_" + comboType + "_Day.options[" + i +"] = null;");
	}

	var month = eval("f.td_" + comboType + "_Month[f.td_" + comboType + "_Month.selectedIndex].value.substring(4) - 1;");
	var year = eval("f.td_" + comboType + "_Month[f.td_" + comboType + "_Month.selectedIndex].value.substring(0,4);");

	var numDays = getDaysMonth(month, year);

	//rellenar dias
	for (var i=1;i < numDays+1;i++) {
		var newDay = new Option(i,(i<10?"0"+i:i));
		eval("f.td_" + comboType + "_Day[f.td_" + comboType + "_Day.length] = newDay;");
	}
}

function HotelsetDateCalendar(day, month, typeDate, blnChangeOtherDate){
	var f = getHotelEntryForm();
	month = Number(month);
	day = Number(day);
	cmbDay = eval("f.td_"+typeDate+"_Day");
	cmbMonth = eval("f.td_"+typeDate+"_Month");
	cmbDate = eval("f.td_"+typeDate+"_Date");
	year = Number(cmbMonth.value.substring(0,4));

	for (var i=0;i < cmbMonth.options.length;i++){
		if (Number(cmbMonth.options[i].value.substring(4)) == month) {
			cmbMonth.selectedIndex = i;
		}
	}

	//Si cambia el numero de dias que tiene el mes rellenamos de nuevo el combo
	if (getDaysMonth(month-1, year) != cmbDay.options.length) {
		HotelcompleteDays2(typeDate);
	}
	if (day-1 >= cmbDay.options.length) {
		cmbDay.selectedIndex = cmbDay.options.length-1;
	} else {
		cmbDay.selectedIndex = day-1;
	}
	HotelchangeDateField2(typeDate);

	var dDate = f.td_D_Month.value.substring(0,4) + f.td_D_Month.value.substring(4) + f.td_D_Day.value;
	var rDate = f.td_R_Month.value.substring(0,4) + f.td_R_Month.value.substring(4) + f.td_R_Day.value;

	if (blnChangeOtherDate){
		if ("D" == typeDate && dDate > rDate) {
			if (f.td_D_Day.selectedIndex == f.td_D_Day.options.length - 1) {
				month = month + 1;
				day = 1;
			} else {
				day = day + 1;
			}
			setDateCalendar(day,month,"R",false);
		} else if ("R" == typeDate && rDate < dDate) {
			setDateCalendar(day,month,"D",false);
		}
	}
}
function HotelsetCmbDate(typeDate, blnChangeOtherDate){
	var f = getHotelEntryForm();

	cmbDay = eval("f.td_"+typeDate+"_Day");
	cmbMonth = eval("f.td_"+typeDate+"_Month");
	cmbDate = eval("f.td_"+typeDate+"_Date");

	var day = Number(cmbDay.value);
	var month = Number(cmbMonth.value.substring(4));

	setDateCalendar(day,month,typeDate,blnChangeOtherDate);
}