//**********************************************************************
// This script is used to manipulate and validate the search elements
// found on the mini promo search widget, which may or may not incorporate
// a book it button.
//**********************************************************************

var pmini_gatewayList = null;
var pmini_destList = null;
var pmini_hotelList = null;
var pmini_defaultClientType = "AXCD";
var pmini_defaultTripType = "ALL";
var pmini_defaultMaxPax = 7;
var pmini_defaultMinChildAge = 0;
var pmini_defaultMaxChildAge = 21;
var pmini_defaultFlightClass = "C";
var pmini_noGatewaySelected = "- Select a Departure City -";
var pmini_noDestinationSelected = "- Select a Destination City -";


function PromoRestrictions(tripType, gateways, destinations, hotels, 
		restrictHotels, minStay, maxStay, minPax, maxPax, minAdult, maxAdult, 
		minChild, maxChild, minChildAge, maxChildAge, travelDates, 
		restrictDates, flightClass, restrictFlightClass, selectedDest, 
		selectedHotel) {
	 if (tripType != undefined && tripType != null && tripType != "") {
		 this.tripType = tripType;
	 }
	 else {
		 this.tripType = pmini_defaultTripType;
	 }
	
	 if (gateways != undefined && gateways != null && gateways != "") {
		 this.gateways = gateways;
	 }
	 else {
		 this.gateways = null;
	 }
	 
	 if (destinations != undefined && destinations != null && destinations != "") {
		 this.destinations = destinations;
	 }
	 else {
		 this.destinations = null;
	 }
	 
	 if (hotels != undefined && hotels != null && hotels != "") {
		 this.hotels = hotels;
	 }
	 else {
		 this.hotels = null;
	 }
	 
	 if (restrictHotels != undefined && restrictHotels != null && restrictHotels != "") {
		 this.restrictHotels = (restrictHotels == "true" ? new Boolean(true) : new Boolean(false));		 
	 }
	 else {
		 this.restrictHotels = new Boolean(false);
	 }
	 
	 if (minStay != undefined && minStay != null && minStay != "") {
		 this.minStay = minStay;
	 }
	 else {
		 this.minStay = null;
	 }
	 
	 if (maxStay != undefined && maxStay != null && maxStay != "") {
		 this.maxStay = maxStay;
	 }
	 else {
		 this.maxStay = null;
	 }
	 
	 if (minPax != undefined && minPax != null && minPax != "") {
		 this.minPax = minPax;
	 }
	 else {
		 this.minPax = null;
	 }
	 
	 if (maxPax != undefined && maxPax != null && maxPax != "") {
		 this.maxPax = maxPax;
	 }
	 else {
		 this.maxPax = pmini_defaultMaxPax;
	 }
	 
	 if (minAdult != undefined && minAdult != null && minAdult != "") {
		 this.minAdult = minAdult;
	 }
	 else {
		 this.minAdult = null;
	 }
	 
	 if (maxAdult != undefined && maxAdult != null && maxAdult != "") {
		 this.maxAdult = maxAdult;
	 }
	 else {
		 this.maxAdult = null;
	 }
	 
	 if (minChild != undefined && minChild != null && minChild != "") {
		 this.minChild = minChild;
	 }
	 else {
		 this.minChild = null;
	 }
	 
	 if (maxChild != undefined && maxChild != null && maxChild != "") {
		 this.maxChild = maxChild;
	 }
	 else {
		 this.maxChild = null;
	 }
	 
	 if (minChildAge != undefined && minChildAge != null && minChildAge != "") {
		 this.minChildAge = minChildAge;
	 }
	 else {
		 this.minChildAge = pmini_defaultMinChildAge;
	 }
	
	 if (maxChildAge != undefined && maxChildAge != null && maxChildAge != "") {
		 this.maxChildAge = maxChildAge;
	 }
	 else {
		 this.maxChildAge = pmini_defaultMaxChildAge;
	 }	
		
	 if (travelDates != undefined && travelDates != null && travelDates != "") {
		 this.travelDates = travelDates;
	 }
	 else {
		 this.travelDates = null;
	 }
	 
	 if (restrictDates != undefined && restrictDates != null && restrictDates != "") {
		 this.restrictDates = (restrictDates == "true" ? new Boolean(true) : new Boolean(false));		 
	 }
	 else {
		 this.restrictDates = new Boolean(false);
	 }
	 
	 if (flightClass != undefined && flightClass != null && flightClass != "") {
		 this.flightClass = flightClass;
	 }
	 else {
		 this.flightClass = pmini_defaultFlightClass;
	 }
	 
	 if (restrictFlightClass != undefined && restrictFlightClass != null && restrictFlightClass != "") {
		 this.restrictFlightClass = (restrictFlightClass == "true" ? new Boolean(true) : new Boolean(false));		 
	 }
	 else {
		 this.restrictFlightClass = new Boolean(false);
	 }
	 
	 if (selectedDest != undefined && selectedDest != null && selectedDest != "") {
		 this.selectedDest = selectedDest;
	 }
	 else {
		 this.selectedDest = null;
	 }
	 
	 if (selectedHotel != undefined && selectedHotel != null && selectedHotel != "") {
		 this.selectedHotel = selectedHotel;
	 }
	 else {
		 this.selectedHotel = null;
	 }
}



$(function() {
	$("#promosearch").attr("action", "https:" + "//" + location.host + "/aev/initEngine.action");
		
	//Configure tracking
	if ($("#trackOrgination").val() == null || $("#trackOrgination").val() == "null" || $("#trackOrgination").val() == "") {
		$("#trackOrgination").val(document.referrer);
	}
	
	if ($("#search_URL").val() == null || $("#search_URL").val() == "null" || $("#search_URL").val() == "") {
		$("#search_URL").val(document.URL);
	}	
	
	//Load gateways, destinations, and hotels
	pmini_gatewayList = new GatewayCity();
	pmini_destList = new Destination();
	pmini_hotelList = new Hotel();
			
	//Populate default widget values
	loadDefaults();
		
	//Initialize Type Ahead, if applicable
	if (pmini_restrictions.gateways == null) {
		initDeptAirportSuggest(1, $("#gatewayDisplay")[0], $("#gateway")[0], $("#ssGatewaySuggestDiv")[0], $("#suggestFrame")[0]);
	}
	if (pmini_restrictions.destinations == null) {
		initDestAirportSuggest(1, $("#destCity1Display")[0], $("#destCity1")[0], $("#ssDest1SuggestDiv")[0], $("#suggestFrame")[0]);
	}
	
	
	
	//Type Ahead Events
	$("#gatewayDisplay").focus(function(event){checkDeptDefaultText(1);});
	$("#gatewayDisplay").keyup(function(event){suggestDeptAirports(1);});
	$("#gatewayDisplay").keydown(function(event){checkDeptAirportScroll(event, 1);});
	$("#gatewayDisplay").blur(function(event){checkDeptAirportSelection(1);});
	$("#deptFullList").click(function(event){toggleDeptAirportList(1, this);});
	
	$("#destCity1Display").focus(function(event){checkDestDefaultText(1);});
	$("#destCity1Display").keyup(function(event){suggestDestAirports(1);});
	$("#destCity1Display").keydown(function(event){checkDestAirportScroll(event, 1);});
	$("#destCity1Display").blur(function(event){checkDestAirportSelection(1);});
	$("#destFullList").click(function(event){toggleDestAirportList(1, this);});
	
		
	
	$("#destCity1Display, #destCity1").change(
        function(event){        	
        	clearError("destCity1Error");
        	
        	if (pmini_restrictions.restrictHotels == false) {
        		if ($("#specificHotelsOpt").is(":checked")) {
            		$("#allHotelsOpt").attr("checked", true);
            		$("#hotelListDiv").hide();
            		$("#hotelList0").empty();
            	}
        	}
        	else {
        		//If hotels are restricted, 'specific' is always checked 
        		//and the hotel select box is always visible so just
        		//trigger reloading of hotel list
        		$("#specificHotelsOpt").click();
        	}
    });
	
	
		
	$("#tripType").change(
        function(event){        	
            $("#flightOptionsRow").toggle();
    });
	
	
	
	$("#startDate").blur(
        function(event){
        	var errorMsg = getDateInputErrors($(this)[0], true);
        	if (errorMsg == null) {
        		clearError("startDateError");
        	}
        	else {
        		displayError(errorMsg, "startDateError");
        	}
    });
	
	
	
	$("#startDate").change(
        function(event){        	
        	if (! $("#startDateError").is(":visible")) {
        		var departDate = parseDateMMDDYYYY($(this).val());
        		var returnDate = getDateFromSeed(departDate, 7);
        		$("#depDate1").val(dateToMMDDYYYYString(returnDate));
        	}
    });
	
	
	
	$("#depDate1").blur(
        function(event){
        	var errorMsg = getDateInputErrors($(this)[0], true);
        	if (errorMsg == null) {
        		clearError("depDate1Error");
        	}
        	else {
        		displayError(errorMsg, "depDate1Error");
        	}        	
    });
	
	
	
	$("#deptCalendar").click(
        function(event){
        	getDepartCalendars($("#calendarDiv")[0], $("#calendarFrame")[0], $("#startDate")[0], $("#depDate1")[0], $("#clientType").val(), pmini_restrictions.travelDates);       
    });
	
	
	
	$("#destCalendar").click(
        function(event){
        	getReturnCalendars($("#calendarDiv")[0], $("#calendarFrame")[0], $("#startDate")[0], $("#depDate1")[0], $("#depDate1")[0], $("#clientType").val(), pmini_restrictions.travelDates);       	       
    });
	
	
	
	$("#allHotelsOpt").click(
        function(event){
        	clearError("hotelList0Error");
        	$("#hotelListDiv").hide();
    		$("#hotelList0").empty();
    });
	
	
	
	$("#specificHotelsOpt").click(
        function(event){
        	if (pmini_restrictions.restrictHotels == false) {
	        	if ($("#destCity1").val().length == 0 || $("#destCity1").val() == pmini_noDestinationSelected) {
	        		$("#allHotelsOpt").attr("checked", true);
	        		displayError("Please select a destination city to see hotels", "destCity1Error");
	        	}
	        	else {
	        		if (pmini_hotelList.loadBookitHotels($("#hotelList0")[0], $("#destCity1").val(), pmini_restrictions.hotels)) {
	        			$("#hotelListDiv").show();
	        		}
	        		else{        			
	            		$("#allHotelsOpt").attr("checked", true);
	        		}        		
	        	}
        	}
        	else {
        		//If hotels are restricted, 'specific' is always checked 
        		//and the hotel select box is always visible so just 
        		//clear the box if no destination has been selected,
        		//or reload it based on the new destination
        		if ($("#destCity1").val().length == 0 || $("#destCity1").val() == pmini_noDestinationSelected) {
        			$("#hotelList0").empty();
        		}
        		else {
        			pmini_hotelList.loadBookitHotels($("#hotelList0")[0], $("#destCity1").val(), pmini_restrictions.hotels);
        		}
        	}
    });
	
	
	
	$("#roomCount").change(
        function(event){
        	switch ($(this).val()) {                
            	case "1":
            		$("#r2Pax").hide();            		
            		updatePassengers($("#room2Adults"), "1");
            		updatePassengers($("#room2Child"), "0");            		
                    toggleChildAges(2, "0");
                    break;
                    
                case "2":                    
                	if (parseInt($("#room1Adults").val()) + parseInt($("#room1Child").val()) == pmini_restrictions.maxPax) {                    	
                    	$(this).val("1");
                		displayError("Total number of passengers cannot exceed the maximum of " + pmini_restrictions.maxPax, "paxCountError");                    	
                    }
                    else {
                    	clearError("paxCountError");
                    	$("#r2Pax").show();
                    }
                    break;            
            }
    });
    
    
    
    $("#room1Adults").change(
        function(event){            
        	var paxMsg = validatePassengers(true);
        	if (paxMsg == null) {
        		clearError("paxCountError");
        		updatePassengers($("#room1Adults"), $(this).val());
        	}        	
        	else {        		
        		updatePassengers($("#room1Adults"), $(this).data("oldVal"));        		
        		displayError(paxMsg, "paxCountError");
        	}        	
    });
    
        
   
    $("#room1Child").change(
        function(event){            
        	var paxMsg = validatePassengers(true);
        	if (paxMsg == null) {        		
        		clearError("paxCountError");
        		updatePassengers($("#room1Child"), $(this).val());
        		toggleChildAges(1, $(this).val());
        	}
        	else {
        		updatePassengers($("#room1Child"), $(this).data("oldVal"));        		
        		displayError(paxMsg, "paxCountError");
        	}
    });
    
    
    
    $("#room2Adults").change(
        function(event){            
        	var paxMsg = validatePassengers(true);
        	if (paxMsg == null) {
        		clearError("paxCountError");
        		updatePassengers($("#room2Adults"), $(this).val());
        	}
        	else {        		
        		updatePassengers($("#room2Adults"), $(this).data("oldVal"));        		
        		displayError(paxMsg, "paxCountError");
        	}
    });
    
    
       
    $("#room2Child").change(
        function(event){            
        	var paxMsg = validatePassengers(true);
        	if (paxMsg == null) {
        		clearError("paxCountError");
        		updatePassengers($("#room2Child"), $(this).val());
        		toggleChildAges(2, $(this).val());
        	}
        	else {
        		updatePassengers($("#room2Child"), $(this).data("oldVal"));        		
        		displayError(paxMsg, "paxCountError");
        	}
    });
    	
	
	
	$("#searchBtn").click(
        function(event){
        	if (validateFormInput()) {
        		$("#promosearch").submit();
        	}        	
    });
})



//Set defaults for widget elements after page has loaded to prevent
//previously entered data from being displayed, and thus ensure that
//all JS functions will work properly.  Also, store passenger data
//for use with error handling later.
function loadDefaults() {
	//TRIP TYPE
	if (pmini_restrictions.tripType == "ALL" || pmini_restrictions.tripType == "FLHL" || pmini_restrictions.tripType == "VC") {
		$("#tripType").append($("<option></option>").attr("value", "FLHL").text("Flight & Hotel Packages"));
	}
	if (pmini_restrictions.tripType == "ALL" || pmini_restrictions.tripType == "HL" || pmini_restrictions.tripType == "LO") {
		$("#tripType").append($("<option></option>").attr("value", "HL").text("Hotel Packages"));
	}
	
			
	//GATEWAY
	if (pmini_restrictions.gateways == null) {		
		$("#gateway").val("");	
	}
	else {
		pmini_gatewayList.loadBookitGways($("#gateway")[0], pmini_restrictions.gateways);
	}
	
	
	//DESTINATION
	if (pmini_restrictions.destinations == null) {
		if (pmini_restrictions.selectedDest != null) {
			$("#destCity1").val(pmini_restrictions.selectedDest);
		}
		else {
			$("#destCity1").val("");
		}
	}
	else {
		if (pmini_destList.loadBookitDests($("#destCity1")[0], pmini_restrictions.destinations)) {
			if (pmini_restrictions.selectedDest != null) {
				$("#destCity1").val(pmini_restrictions.selectedDest);
			}
		}
	}
	
	
	//DATES
	loadDateDefaults();
	
	
	//FLIGHT CLASS
	if (pmini_restrictions.tripType == "HL" || pmini_restrictions.tripType == "LO") {
		$("#flightOptionsRow").hide();
	}
	else {
		if (pmini_restrictions.restrictFlightClass == true) {
			if (pmini_restrictions.flightClass == "C") {
				$("#flightClassB").attr("disabled", "disabled");				
				$("#flightClassF").attr("disabled", "disabled");					
			}
			else if (pmini_restrictions.flightClass == "F") {
				$("#flightClassC").attr("disabled", "disabled");
			}
		}
		
		var classField = "#flightClass" + pmini_restrictions.flightClass;        
		$(classField).attr("checked", true);
	}
	
	
	//HOTELS
	if (pmini_restrictions.restrictHotels == false) {
		if ($("#destCity1").val().length > 0 && pmini_hotelList.loadBookitHotels($("#hotelList0")[0], $("#destCity1").val(), pmini_restrictions.hotels)) {
			$("#specificHotelsOpt").attr("checked", true);		
			if (pmini_restrictions.selectedHotel != null) {
				$("#hotelList0").val(pmini_restrictions.selectedHotel);
				$("#hotelList0 option:selected").focus();
			}
			$("#hotelListDiv").show();
		}
		else {		
			$("#allHotelsOpt").attr("checked", true);
		}
	}
	else {
		$("#allHotelsOpt").attr("disabled", "disabled");		
		$("#specificHotelsOpt").attr("checked", true);
		$("#hotelListDiv").show();
		
		if ($("#destCity1").val().length > 0 && pmini_hotelList.loadBookitHotels($("#hotelList0")[0], $("#destCity1").val(), pmini_restrictions.hotels)) {			
			if (pmini_restrictions.selectedHotel != null) {
				$("#hotelList0").val(pmini_restrictions.selectedHotel);
				$("#hotelList0 option:selected").focus();
			}
		}		
	}
	
	
	
	//PASSENGERS	
	$("#roomCount").val("1");
	
	if (pmini_restrictions.minAdult != null) {
		updatePassengers($("#room1Adults"), pmini_restrictions.minAdult);
	}
	else {
		updatePassengers($("#room1Adults"), "2");
	}
	
	if (pmini_restrictions.minChild != null) {
		updatePassengers($("#room1Child"), pmini_restrictions.minChild);
	}
	else {
		updatePassengers($("#room1Child"), "0");
	}	
	
	updatePassengers($("#room2Adults"), "1");
	updatePassengers($("#room2Child"), "0");
	populateChildAges();
}



function loadDateDefaults() {
    var today = new Date();
    var departDate;
    var returnDate;    
    
    if (pmini_restrictions.travelDates == null) {        
        //Departure date should default to 21 days in the future
        departDate = getDateFromSeed(today, 21);
		        
        //Return date should default to 7 days after departure date
        returnDate = getDateFromSeed(departDate, 7);		
    }
    else {    	
    	var earliestBookableDate = getDateFromSeed(today, 4);
    	var earliestPromoDate = null;
        var dateRangeIndex = null;
      
        for (var i=0; i<pmini_restrictions.travelDates.length; i++) {
            if (earliestBookableDate <= pmini_restrictions.travelDates[i].beginDate) {
            	if (earliestPromoDate == null || earliestPromoDate > pmini_restrictions.travelDates[i].beginDate) {
                    earliestPromoDate = pmini_restrictions.travelDates[i].beginDate;
                    dateRangeIndex = i;
                }
            }
            else {
            	if (earliestBookableDate < pmini_restrictions.travelDates[i].endDate) {
            		if (earliestPromoDate == null || earliestPromoDate > earliestBookableDate) {
                        earliestPromoDate = earliestBookableDate;
                        dateRangeIndex = i;
                    }
            	}
            }
        }
        
        if (earliestPromoDate == null) {
        	earliestPromoDate = earliestBookableDate;
        }
        
                
        //Departure date should default to first valid promo date
        departDate = getDateFromSeed(earliestPromoDate, 0);		
        
        //Return date should default to the minimum stay value (or 7 days
        //if a min stay has not been specified) after the departure date
        //or the last day of the selected promo date range, whichever is
        //earlier        
        if (pmini_restrictions.minStay != null) {
        	returnDate = getDateFromSeed(departDate, parseInt(pmini_restrictions.minStay));
        }
        else {
        	returnDate = getDateFromSeed(departDate, 7);
        }        
        if (dateRangeIndex != null && returnDate > pmini_restrictions.travelDates[dateRangeIndex].endDate) {
            returnDate.setTime(pmini_restrictions.travelDates[dateRangeIndex].endDate.getTime());
        }
    }
    
    $("#startDate").val(dateToMMDDYYYYString(departDate));
    $("#depDate1").val(dateToMMDDYYYYString(returnDate));
}



function populateChildAges() {	
	var minAge = parseInt(pmini_restrictions.minChildAge);
	var maxAge = parseInt(pmini_restrictions.maxChildAge);
	
	for (var i=1; i<7; i++) {		
		for (var a=minAge; a<=maxAge; a++) {			
			$("#room1ChildAge" + i).append($("<option></option>").attr("value", a).text(a));				
		}		
    }
	for (var i=1; i<6; i++) {                  
		for (var a=minAge; a<=maxAge; a++) {
			$("#room2ChildAge" + i).append($("<option></option>").attr("value", a).text(a));					
		}		
    }
}



function clearErrors() {
	$(".widgetError").empty();
	$(".widgetErrorRow").hide();
}



function clearError(errorDivID) {	
	$("#" + errorDivID).closest(".widgetError").empty();
	$("#" + errorDivID).hide();
}



function displayError(message, errorDivID) {
	$("#" + errorDivID).find(".widgetError").html(message);
	$("#" + errorDivID).show();
}



function updatePassengers(field, newVal) {	
	$(field).val(newVal);
	$(field).data("oldVal", newVal);
}



function toggleChildAges(roomNum, numChildren) {       
	var divName = "#room" + roomNum + "ChildAges";
	
	if (numChildren != "0") {    	
    	$(divName).show();
    	
    	switch (numChildren) {
            case "1":
                enableChildAges(roomNum, 1, 1);
                disableChildAges(roomNum, 2, 6)
                break;
                
            case "2":
                enableChildAges(roomNum, 1, 2);
                disableChildAges(roomNum, 3, 6)
                break;
            
            case "3":
                enableChildAges(roomNum, 1, 3);
                disableChildAges(roomNum, 4, 6)
                break;
            
            case "4":
                enableChildAges(roomNum, 1, 4);
                disableChildAges(roomNum, 5, 6)
                break;
            
            case "5":
                enableChildAges(roomNum, 1, 5);
                disableChildAges(roomNum, 6, 6)
                break;
            
            case "6":
                enableChildAges(roomNum, 1, 6);                
                break;          
        }
    }
    else {
    	$(divName).hide();
    	disableChildAges(roomNum, 1, 6);
    }
}



function enableChildAges(roomNum, beginNr, endNr) {
    var fieldName = null;
    
    for (var i=beginNr; i<endNr+1; i++) {
        fieldName = "#room" + roomNum + "ChildAge" + i;            
        $(fieldName).show();
    }
}



function disableChildAges(roomNum, beginNr, endNr) {
    var fieldName = null;
    
    for (var i=beginNr; i<endNr+1; i++) {
        fieldName = "#room" + roomNum + "ChildAge" + i;  
        $(fieldName).val("?");
        $(fieldName).hide();
    }
}



function validatePassengers(maxOnlyFlag) {
	var totalAdults = parseInt($("#room1Adults").val());
	var totalChildren = parseInt($("#room1Child").val());
	
	if ($("#r2Pax").is(":visible")) {
		totalAdults += parseInt($("#room2Adults").val());
		totalChildren += parseInt($("#room2Child").val());
	}
		
	if (totalAdults + totalChildren > pmini_restrictions.maxPax) {
		return "Total number of passengers exceeds the maximum of " + pmini_restrictions.maxPax;
	}
	else if (pmini_restrictions.maxAdult != null && totalAdults > pmini_restrictions.maxAdult) {
		return "Total number of adults exceeds the maximum of " + pmini_restrictions.maxAdult;
	}
	else if (pmini_restrictions.maxChild != null && totalChildren > pmini_restrictions.maxChild) {
		return "Total number of children exceeds the maximum of " + pmini_restrictions.maxChild;
	}
	else if (! maxOnlyFlag) {
		if (pmini_restrictions.minPax != null && totalAdults + totalChildren < pmini_restrictions.minPax) {
			return "Total number of passengers does not meet the minimum of " + pmini_restrictions.minPax;
		}
		else if (pmini_restrictions.minAdult != null && totalAdults < pmini_restrictions.minAdult) {
			return "Total number of adults does not meet the minimum of " + pmini_restrictions.minAdult;
		}
		else if (pmini_restrictions.minChild != null && totalChildren < pmini_restrictions.minChild) {
			return "Total number of children does not meet the minimum of " + pmini_restrictions.minChild;
		}
	}
	else {
		return null;
	}
}



//**********************************************************************
//Name			: validateTravelDates
//Description	: Validates the travel date fields.
//Output		: True if the travel date values are valid or false if
//				  errors are found.
//**********************************************************************
function validateTravelDates() {
	var departDate = null;
	var returnDate = null;
	var today = null;
	var firstSelectableDate = null;
	var lastSelectableDatePkg = null;
	var valid = true;
	var errorMsg = null;

	//Dates must be valid and in correct format
	errorMsg = getDateInputErrors($("#startDate")[0], false);
	if (errorMsg == null) {
		departDate = parseDateMMDDYYYY($("#startDate").val());		
	}
	else {
		valid = false;
		displayError(errorMsg, "startDateError");
	}
	
	errorMsg = getDateInputErrors($("#depDate1")[0], false);
	if (errorMsg == null) {
		returnDate = parseDateMMDDYYYY($("#depDate1").val());		
	}
	else {
		valid = false;
		displayError(errorMsg, "depDate1Error");		
	}
		
	if (departDate != null) {
		 today = new Date();    
		 firstSelectableDate = getDateFromSeed(today, 4);
		 lastSelectableDatePkg = getDateFromSeed(today, 331);
		 
		 //Earliest selectable date should be 4 days after current date
		 if (departDate < firstSelectableDate) {
			 valid = false;
			 displayError("Departure date must be at least 4 days in the future", "startDateError");
		 }
		 
		 //Air + hotel packages can only be made up to 331 days in advance    
		 if ($("#tripType").val() == "FLHL" && departDate > lastSelectableDatePkg) {
			 valid = false;
			 displayError("Flight &amp; Hotel Packages can only be booked up to 331 days in advance; to book a later date, please select the Hotel Packages option", "startDateError");			 	 
		 }
		
		 if (returnDate != null) {
			 //Dates cannot be the same
			 if (departDate.getTime() == returnDate.getTime()) {				 
				 valid = false;
				 displayError("Departure date and return date cannot be the same", "depDate1Error");
			 }			 
			 
			 //Departure date must be earlier than return date
			 if (departDate > returnDate) {				 
				 valid = false;
				 displayError("Return date cannot be earlier than departure date", "depDate1Error");
			 }
			 
			 //Verify that min/max stay requirements are met, if applicable
			 var numDays = (returnDate.getTime() - departDate.getTime()) / (1000*60*60*24);
			 if (pmini_restrictions.minStay != null && numDays < pmini_restrictions.minStay) {			
			 	valid = false;
			 	displayError("Travel dates do not meet minimum stay of " + pmini_restrictions.minStay + " days", "startDateError");				
			 }
			 if (pmini_restrictions.maxStay != null && numDays > pmini_restrictions.maxStay && $("#gcevent").val() == "N") {				
			 	valid = false;
			 	displayError("Travel dates exceed maximum stay of " + pmini_restrictions.maxStay + " days", "startDateError");				
			 }
						 
			 //If travel dates are restricted, ensure that travel dates fall
			 //within a valid promo date range
			 if (pmini_restrictions.restrictDates == true && pmini_restrictions.travelDates != null) {
			     var validDates = false;
			     
			     for (var v=0; v<pmini_restrictions.travelDates.length; v++) {        
			         if (departDate >= pmini_restrictions.travelDates[v].beginDate && returnDate <= pmini_restrictions.travelDates[v].endDate) {
			             validDates = true;
			             break;
			         }
			     }
			     
			     if (! validDates) {
			    	 valid = false;
			    	 displayError("Selected travel dates are not eligible for this promotion", "startDateError");			    	 	    	   
			     }
			 }
		 } //end end valid return date
	} //end valid departure date

	return valid;
}



function validateFormInput() {
	var errorFound = false;
	var errorMsg = null;
	
	clearErrors();
		
	if ($("#gateway").val().length == 0 || $("#gateway").val() == pmini_noGatewaySelected) {
		errorFound = true;
		displayError("Select a departure city", "gatewayError");
	}
	if ($("#destCity1").val().length == 0 || $("#destCity1").val() == pmini_noDestinationSelected) {		
		errorFound = true;
		displayError("Select a destination city", "destCity1Error");
	}
	if ($("#gateway").val().length > 0 && $("#destCity1").val().length > 0 &&
			$("#gateway").val() ==  $("#destCity1").val()) {
		errorFound = true;
		displayError("Departure and destination city cannot be the same", "destCity1Error");
	}
		
	if (! validateTravelDates()) {
		errorFound = true;
	}
	
	if ($("#destCity1").val().length > 0 && $("#destCity1").val() != pmini_noDestinationSelected && 
			$("#specificHotelsOpt").attr("checked") == true && $("#hotelList0 option:selected").length < 1) {
		errorFound = true;
		displayError("Select a hotel", "hotelList0Error");
	}
		
	errorMsg = validatePassengers(false);
	if (errorMsg != null) {		
		errorFound = true;
		displayError(errorMsg, "paxCountError");
	}
	
	for (var i=1; i<7; i++) {
		if ($("#room1ChildAge" + i).is(":visible") && $("#room1ChildAge" + i).val() == "?") {		
			errorFound = true;
			displayError("Select Room 1 child age(s)", "room1ChildAgesError");
			break;			
		}
    }
	for (var i=1; i<6; i++) {                  
		if ($("#room2ChildAge" + i).is(":visible") && $("#room2ChildAge" + i).val() == "?") {
			errorFound = true;
			displayError("Select Room 2 child age(s)", "room2ChildAgesError");
			break;
		}
    }
		
	if (errorFound) {		
		return false;
	}
	else {
		return true;
	}
}
