var iataFrom, iataTo, frm;

window.onload = function () {
    
	iataFrom = document.getElementById("iataFrom");
    iataTo = document.getElementById("iataTo");
	onewaytrip = document.getElementById("onewaytrip");
	frm = document.getElementById("frmsearch");
	
    if (onewaytrip.checked) {
        setVis('returndate', 'none');
    }
}


$(document).ready(function () {
    // get the input field
    $.get('/xmlfeeds/vibe-airports.xml', function parseXML(xml) {
        var matchingAirports = [];
        $(xml).find('airport').each(function () {
            matchingAirports[matchingAirports.length] = {
                'value': $(this).text()
            };
        });

        var deptAirport = $('input#iataFrom');
		deptAirport.autocomplete(matchingAirports, {
			delay: 10,
			minChars: 1,
			matchContains: "word",
			formatItem: function (data) {
				return $(data).val();
			}
		});

        var destAirport = $('input#iataTo');
		destAirport.autocomplete(matchingAirports, {
			delay: 10,
			minChars: 1,
			matchContains: "word",
			formatItem: function (data) {
				return $(data).val();
			}
		});
    });
});


function validateSearch() {

    
	var numAdults = document.getElementById("numAdults");
	var numChildren = document.getElementById("numChildren");
	var numInfants = document.getElementById("numInfants");
	var totalpax = (parseInt(numAdults[numAdults.selectedIndex].value) + parseInt(numChildren[numChildren.selectedIndex].value));
	var validDates = datePickerController.getSelectedDate("outboundMonthNumbers") && datePickerController.getSelectedDate("returnMonthNumbers");
	
	if (onewaytrip.checked) {
		validDates = datePickerController.getSelectedDate("outboundMonthNumbers");
	}	
	
	if (iataFrom.value == "") {
        alert("Please specify departure city or airport");
        iataFrom.focus();
        return false
    }

    if (iataTo.value == "") {
        alert("Please specify destination city or airport");
        iataTo.focus();
        return false
    }	
	
	if (!validDates) {
        alert("Please enter valid travel dates");
        return false
    }

    if (totalpax > 9) {
        alert("The maximum number of Adult and Child seats allowed in a single online booking is 9.\n\nPlease call 08718 551 551 to make this reservation.");
        return false
    }

    if (numInfants[numInfants.selectedIndex].value > numAdults[numAdults.selectedIndex].value) {
        alert("Airline regulations stipulate that you must travel with one adult per infant.\n\nIf you are travelling with " + numInfants[numInfants.selectedIndex].value + " infants, then you must take " + numInfants[numInfants.selectedIndex].value + " adults.");
        numAdults.focus();
        return false
    }
	
	oneWay();
	
	iataFrom.removeAttribute("autocomplete");
	iataTo.removeAttribute("autocomplete");

    frm.submit();

    return true
}
