function doBigflightSearch(codigo,epi) {
	var type_list = new Array (1,2,3,4,5,6,7,9,15,16,17,19,20,21); // ,10,13    18,
	//var type_list = new Array (1,7,9,20); // ,10,13
	var epi_list = new Array (epi,codigo,codigo,codigo,codigo,epi,codigo,codigo,codigo,epi,epi,epi); 
	var f = getAirEntryForm();	
	
	for (var i=0; i<type_list.length; i++) {
		name = "providers_id_" + type_list[i];
		extraname = "providers_id_dat_" + type_list[i];		
		eval("j=f." + name + ".checked");
		func = document.getElementById("providers_id_eval_" + type_list[i]).value;
		if (j) {
			if (func != "" && func != undefined){
		  		eval(func);
			}else{				
	//			document.getElementById(extraname).click();
				doFlightSearch(type_list[i],epi_list[i]);		
			}
		}

	}
}


function mostrarBotonActualizar(){
 	document.getElementById('doFlightsButton').style.display = 'none';
	document.getElementById('refreshButton').style.display = ''; 	
}

function submitRefreshForm(){
	if (document.getElementById('td_A_City').value != ""){
		document.getElementById('post_name').value = document.getElementById('td_A_City').value;
		document.getElementById('refreshForm').submit();	
	}else{
	 	alert("Introduce un nombre de hotel");
	}

}

function validateCities() {
        var f = getAirEntryForm();
        var checkstr = "";
        //Verificar ciudad
        if (f.td_A_City.value=="") {
                checkstr += "Introduce una ciudad destino\n";
        } else {
                f.td_A_City.value = city(f.td_A_City.value);
        }
        return checkstr;
}

function validatePassengers() {
        var f = getAirEntryForm();
        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 validateActualDate() {
        var f = getAirEntryForm();
        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 city(v) {
        i=v.indexOf("(");
        j=v.lastIndexOf(")");
        if (j-i==4) return v.substring(i+1,j);
        return v;
}


function doFlightSearch(tipo,place,hotelName,hotelUrl) {
        
        var f = getAirEntryForm();

        var checkstr = "";
        checkstr += validateCities();

        checkstr += validatePassengers();

        checkstr += validateActualDate();

        if(checkstr!=""){
                alert(checkstr);
                return;
        }

        if (place != '' && place != undefined){
		 	f.td_A_City.value = place;
		}

        if ( tipo == 8 ) //Viajar.com
        {
                f.submit();
        }
        else if ( tipo == 6 ) // Hotelopia
        {
                f.action = "http://ad.zanox.com/ppc/?2589978C1884044351T&ULP=[[http://www.hotelopia.es/home/homepage.jsp]]";
                f.submit();             
        }
        else if ( tipo == 3 ) //eDreams
        {
				window.document.frm_Search.td_city.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 == 1 ) //Atrapalo
        {
				if (hotelUrl != '' && hotelUrl != undefined){
					window.open(unescape(hotelUrl));
				}else{ 
		            window.document.busqueda.td_id_destino3.value = f.td_A_City.value;
		            window.document.busqueda.td_anyo_entrada.value = f.td_D_Month.value.substring(0,4);
		            window.document.busqueda.td_mes_entrada.value = f.td_D_Month.value.substring(4,6);
		            window.document.busqueda.td_dia_entrada.value = f.td_D_Day.value;
		            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);
		            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_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_adultos_4.value = f.td_select_adt.value;
		            window.document.busqueda.td_num_adultos_5.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_num_ninos_4.value = f.td_select_chd.value;
		            window.document.busqueda.td_num_ninos_5.value = f.td_select_chd.value;
		            window.document.busqueda.td_edad_1_1.value = "4";
		            window.document.busqueda.td_edad_1_2.value = "4";
		            window.document.busqueda.td_edad_1_3.value = "4";
		            window.document.busqueda.td_edad_2_1.value = "4";
		            window.document.busqueda.td_edad_2_2.value = "4";
		            window.document.busqueda.td_edad_2_3.value = "4";
		            window.document.busqueda.td_edad_3_1.value = "4";
		            window.document.busqueda.td_edad_3_2.value = "4";
		            window.document.busqueda.td_edad_3_3.value = "4";
		            window.document.busqueda.td_edad_4_1.value = "4";
		            window.document.busqueda.td_edad_4_2.value = "4";
		            window.document.busqueda.td_edad_4_3.value = "4";
		            window.document.busqueda.td_edad_5_1.value = "4";
		            window.document.busqueda.td_edad_5_2.value = "4";
		            window.document.busqueda.td_edad_5_3.value = "4";
	                window.document.busqueda.submit();
	        }
        }
        else if ( tipo == 7 ) // Hotels.com
        {
				if (hotelUrl != '' && hotelUrl != undefined){
					window.open(unescape(hotelUrl));
				}else{   
	                window.document.bookingForm.url.value += '?showhp=true';
	                window.document.bookingForm.url.value += '&dateSeparator=/';
	                window.document.bookingForm.url.value += '&typeOfPage=homepage';
	                window.document.bookingForm.url.value += '&nonjs=1';
	                window.document.bookingForm.url.value += '&destination=' + f.td_A_City.value;
	                window.document.bookingForm.url.value += '&arrivalDate=' + 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 += '&departureDate=' + 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 += '&adultsPerRoom=' + f.td_select_adt.value;
	                window.document.bookingForm.url.value += '&childrenPerRoom=' + f.td_select_chd.value;
	                window.document.bookingForm.url.value += '&childAgesPerRoom=1';
	                window.document.bookingForm.submit();
				}
        }
        else if ( tipo == 9 ) // Booking.com
        {
                
                if (hotelUrl != '' && hotelUrl != undefined){
			 		 hotelUrl += '?checkin_monthday='+f.td_D_Day.value+'&checkin_year_month='+f.td_D_Month.value.substring(0,4)+'-'+f.td_D_Month.value.substring(4,6)+'&checkout_monthday='+f.td_R_Day.value+'&checkout_year_month='+f.td_R_Month.value.substring(0,4)+'-'+f.td_R_Month.value.substring(4,6)+'&do_availability_check=1&order=price&rows=&lang=es&aid=303736';
					 window.open(unescape(hotelUrl));
				}else{
					 window.open('http://www.booking.com/index.html?aid=303736','Booking','');
				}
        }
        else if ( tipo == 2 ) // Barcelo
        {
				if (hotelUrl != '' && hotelUrl != undefined){
					window.open(unescape(hotelUrl));
				}else{ 
	                window.document.barceloForm.url.value = 'http://www.barceloviajes.com/sites/all/procesa_peticion_3os.jsp';
	                window.document.barceloForm.url.value += '?destinotxt=' + f.td_A_City.value;
	                window.document.barceloForm.url.value += '&fida=' + f.td_D_Day.value + "/" + f.td_D_Month.value.substring(4,6) + "/" + f.td_D_Month.value.substring(0,4);
	                window.document.barceloForm.url.value += '&fvuelta=' + f.td_R_Day.value + "/" + f.td_R_Month.value.substring(4,6) + "/" + f.td_R_Month.value.substring(0,4);
	                window.document.barceloForm.url.value += '&habitaciones=' + f.td_dobleroom.value;
	                window.document.barceloForm.url.value += '&adultos=' + f.td_select_adt.value;
	                window.document.barceloForm.url.value += '&nens=' + f.td_select_chd.value;
					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.barceloForm.url.value += '&dias=' + (fin - inicio);
	                window.document.barceloForm.url.value += '&tprod=HOT';
	                window.document.barceloForm.url.value += '&form=HOTEL_ENTRY_FORM';
	                window.document.barceloForm.submit();
	            }
        }          
        else if ( tipo == 5 ) // Logitravel
        {
				if (hotelUrl != '' && hotelUrl != undefined){
					window.open(unescape(hotelUrl));
				}else{ 
	                temp = "http://ad.zanox.com/ppc/?5786024C1607290479T&ULP=[[";
	                temp += "&nombreCiudad=" + f.td_A_City.value;
	                temp += "&fechaEntrada=" + 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);
	                num_noches = fin - inicio;
	                temp += "&noches=" + num_noches;
	                num_pax = parseInt(f.td_dobleroom.value) * (parseInt(f.td_select_adt.value) + parseInt(f.td_select_chd.value));
	                temp += "&numPax=" + num_pax.toString();
	                for (var i = 1; i <= (parseInt(f.td_dobleroom.value) * parseInt(f.td_select_adt.value)); i++) {
	                        temp += '&edad_' + i + '= 30';
	                }
	                for (var j = 1; j <= (parseInt(f.td_dobleroom.value) * parseInt(f.td_select_chd.value)); j++) {
	                        temp += '&edad_' + (j+i) + '= 10';
	                }
	                temp += "&numHab=" + f.td_dobleroom.value;
	                temp += "]]";
	                f.action = temp;
	                f.submit();
	            }
        }
        else if ( tipo == 15 ) // Hotelius
        {
				if (hotelUrl != '' && hotelUrl != undefined){
					window.open(unescape(hotelUrl));
				}else{       
					window.open('http://ad.zanox.com/ppc/?3505551C1064702312T&ULP=[[http://www.hotelius.com]]');
				}
        }        
        else if ( tipo == 16 ) // Accor
        {
                if (hotelUrl != '' && hotelUrl != undefined){
					window.open(unescape(hotelUrl));
				}else{                
					window.open('http://clk.tradedoubler.com/click?p=31822&a=1308183&url=http://www.accorhotels.com/es/home/index.shtml');
				}	
				
        }
        else if ( tipo == 17 ) // Destinia
        {
                if (hotelUrl != '' && hotelUrl != undefined){
					window.open(unescape(hotelUrl));
				}else{	
					window.open('http://clk.tradedoubler.com/click?p=17509&a=1308183&url=http://destinia.com/hotels/es');
				}
        }
        else if ( tipo == 18 ) // Hotelclub
        {
                if (hotelUrl != '' && hotelUrl != undefined){
					var hotelUrl = unescape(hotelUrl);
					var hotelId = hotelUrl.substring(hotelUrl.indexOf("ULP")+6, hotelUrl.indexOf("]]"));
					hotelUrl = 'http://www.hotelclub.net/directhotel.asp?id='+hotelId+'&inDay='+f.td_D_Day.value+'&inMonth='+f.td_D_Month.value.substring(4,6)+'&inYear='+f.td_D_Month.value.substring(0,4)+'&outDay='+f.td_R_Day.value+'&outMonth='+f.td_R_Month.value.substring(4,6)+'&outYear='+f.td_R_Month.value.substring(0,4);
					window.open('http://ad.zanox.com/ppc/?8847452C1276260155T&ULP=[['+hotelUrl+']]');
				}else{	
                	window.open('http://ad.zanox.com/ppc/?8847452C1276260155T&ULP=[[http://www.hotelclub.es/]]');
                }
        }
        else if ( tipo == 19 ) // Muchoviaje
        {
				if (hotelUrl != '' && hotelUrl != undefined){
					window.open(unescape(hotelUrl));
				}else{ 
	                window.open('http://ad.zanox.com/ppc/?5420116C1466754726T&ULP=[[http://hoteles.muchoviaje.com/]]','Muchoviaje','');
					//window.document.Form1.poblacion.value += f.td_A_City.value;
					//window.document.Form1.entradaD.value += 18;
					//window.document.Form1.entradaM.value += 4;
					//window.document.Form1.entradaY.value += 2008;
					//window.document.Form1.habitaciones.value += 1;
					//window.document.Form1.noches.value += 1;
	
	                //window.document.Form1.submit();
	            }
        }
        else if ( tipo == 20 ) // Venere
        {
                    hotelUrl = hotelUrl.replace(/0/,"29795");
				    hotelUrl += '&sd='+f.td_D_Day.value+'&sm='+f.td_D_Month.value.substring(4,6)+'&sy='+f.td_D_Month.value.substring(0,4)+'&ed='+f.td_R_Day.value+'&em='+f.td_R_Month.value.substring(4,6)+'&ey='+f.td_R_Month.value.substring(0,4)+'&pval='+f.td_select_adt.value+'&rval='+f.td_dobleroom.value;
				window.open('http://clk.tradedoubler.com/click?p=73621&a=1508215&url=' + hotelUrl);

        }        
        else if ( tipo == 21 ) // Bravofly
        {
                if (hotelUrl != '' && hotelUrl != undefined){
                    hotelUrl = hotelUrl.replace(/0/,"29795");
				    hotelUrl += '&sd='+f.td_D_Day.value+'&sm='+f.td_D_Month.value.substring(4,6)+'&sy='+f.td_D_Month.value.substring(0,4)+'&ed='+f.td_R_Day.value+'&em='+f.td_R_Month.value.substring(4,6)+'&ey='+f.td_R_Month.value.substring(0,4)+'&pval='+f.td_select_adt.value+'&rval='+f.td_dobleroom.value;
					window.open(unescape(hotelUrl));
				}else{	
					window.open('http://es.venere.com/?ref=29795');
				}
        } 
        else if ( tipo == 4 ) //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();
        }
        
		if (place != '' && place != undefined){
		 	 if (hotelName != ''){
			 	f.td_A_City.value = hotelName;
			 }
		}
}

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 completeDays2(comboType){
        var f = getAirEntryForm();

        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 completeMonths2() {
        var myDate = new Date();

        var f = getAirEntryForm();

        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 initDate(initDays) {
        var myDate = new Date();
        var f = getAirEntryForm();
        completeMonths2();
        completeDays2("D");
        completeDays2("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 + 1;
        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
        setDateCalendar(initDDay,initDMonth+1,"D",false);
        setDateCalendar(initRDay,initRMonth+1,"R",false);
        changeDateField2("D");
        changeDateField2("R");
}


function Check(){
        var f=document.forms['AIR_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 getAirEntryForm() {

        var f;
        if (navigator.userAgent.indexOf("MSIE") >= 0) {
                for (var i=0;i < document.forms.length;i++) {
                        if (document.forms[i].name == "AIR_ENTRY_FORM") {
                                f=document.forms[i];
                        }
                }
        } else {
                f=document.forms["AIR_ENTRY_FORM"];
        }

        return f;
}

function changeDateField2(dateType){
        var f = getAirEntryForm();
        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 completeDays2(comboType){
        var f = getAirEntryForm();

        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 setDateCalendar(day, month, typeDate, blnChangeOtherDate){
        var f = getAirEntryForm();
        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) {
                completeDays2(typeDate);
        }
        if (day-1 >= cmbDay.options.length) {
                cmbDay.selectedIndex = cmbDay.options.length-1;
        } else {
                cmbDay.selectedIndex = day-1;
        }
        changeDateField2(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 setCmbDate(typeDate, blnChangeOtherDate){
        var f = getAirEntryForm();

        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);
}

