var maxPassengers = new Array();
maxPassengers[4009] = 2;

var formname = "qForm";

function daysInMonth(year, month){
    month = parseInt(month) - 1;
    return 32 - new Date(year, month, 32).getDate();
}

function changeSecondDate(){
    var fromDay = document.getElementById(formname).fromDay.value;
    var fromMonth = document.getElementById(formname).fromMonth.value;
    var fromMonthArray = fromMonth.split("-");
    var fromMonthMonth = fromMonthArray[1];
    var fromMonthYear = fromMonthArray[0];
    var daysinmonth = daysInMonth(fromMonthYear, fromMonthMonth);
    if ((parseInt(fromDay) + 6) > daysinmonth) {
        var newDay = (parseInt(fromDay) + 6) - daysinmonth;
        var newMonth = parseInt(fromMonthMonth) + 1;
        var newYear = parseInt(fromMonthYear);
        if (newMonth > 12) {
            newMonth = 1;
            newYear = parseInt(fromMonthYear) + 1;
        }
        var newMonthDate = newYear + "-" + newMonth;
        var toDaySelect = document.getElementById(formname).toDay;
        for (var i = 0; i < toDaySelect.options.length; i++) {
            if (toDaySelect.options[i].value == newDay) {
                toDaySelect.selectedIndex = i;
            }
        }
        var toMonthSelect = document.getElementById(formname).toMonth;
        for (var i = 0; i < toMonthSelect.options.length; i++) {
            if (toMonthSelect.options[i].value == newMonthDate) {
                toMonthSelect.selectedIndex = i;
            }
        }
    }
    else {
    
        var toDaySelect = document.getElementById(formname).toDay;
        for (var i = 0; i < toDaySelect.options.length; i++) {
            if (toDaySelect.options[i].value == (parseInt(fromDay) + 6)) {
                toDaySelect.selectedIndex = i;
            }
        }
        var toMonthSelect = document.getElementById(formname).toMonth;
        for (var i = 0; i < toMonthSelect.options.length; i++) {
            if (toMonthSelect.options[i].value == fromMonth) {
                toMonthSelect.selectedIndex = i;
            }
        }
    }
}

function checkForm(form){

    var valid = true;
    var message = 'There were errors in your selection.\n Please check the following errors and re-submit.\n\n';
    
    
    if (!validateDropdown(form, 'fromLocation', '')) {
        valid = false;
        message += '\t- Please select a pick up location.\n';
    }
    
    if (!validateDropdown(form, 'toLocation', '')) {
        valid = false;
        message += '\t- Please select a drop off destination.\n';
    }
    
    if (!validateDropdown(form, 'toMonth', '')) {
        valid = false;
        message += '\t- Please select a month for pick up.\n';
    }
    
    if (!validateDropdown(form, 'toDay', '')) {
        valid = false;
        message += '\t- Please select a day for pickup.\n';
    }
    
    if (!validateDropdown(form, 'fromMonth', '')) {
        valid = false;
        message += '\t- Please select a drop off destination.\n';
    }
    
    if (!validateDropdown(form, 'fromDay', '')) {
        valid = false;
        message += '\t- Please select a drop off destination.\n';
    }
		
		var motorhome = getSelectedItemInDropDown(form, 'motorhome');
		var matches = motorhome.getAttribute('rel').match(/max=(\d+)/);
		var maxPassengers = matches[1];
		var totalPassengers = parseInt(getSelectedItemInDropDown(form, 'passengersAdult').value) + parseInt(getSelectedItemInDropDown(form, 'passengersChild').value) + parseInt(getSelectedItemInDropDown(form, 'passengersInfant').value);
		
		if (totalPassengers > maxPassengers) {
			valid = false;
			message += '\t- The maximum number of passengers for this vehicle is ' + maxPassengers + '.';
		}
    
    
    var travelUntil = null;
    var travelUntil = null;
    var travelUntil = null;
    var travelUntil = null;
    var rate = 100;
    
    
    
    if (valid) {
    
        //do the date stuff.. we need to have valid data here to do this..
        var toMonth = getSelectedItemInDropDown(form, 'toMonth');
        var toDay = getSelectedItemInDropDown(form, 'toDay');
        var fromMonth = getSelectedItemInDropDown(form, 'fromMonth');
        var fromDay = getSelectedItemInDropDown(form, 'fromDay');
        
        
        //javascript date stuff is evil.
        from = fromMonth.value + "-" + fromDay.value;
        fromBits = from.split("-");
        fromDate = new Date(fromBits[0], fromBits[1] - 1, fromBits[2]);
        
        to = toMonth.value + "-" + toDay.value;
        toBits = to.split("-");
        toDate = new Date(toBits[0], toBits[1] - 1, toBits[2]);
        
        if ((fromDate - (new Date())) <= 0) {
        
            valid = false;
            message += "\t- Bookings can only be made in the future.\n";
        }
        
        if (toDate < fromDate) {
            valid = false;
            message += "\t- Drop off date is after the pick up date.\n";
        }

				var May012009 = new Date(2009, 5 - 1, 1);
				var Sep302009 = new Date(2009, 9 - 1, 30);
				
				if (toDate > May012009 && toDate < Sep302009) {
	        //check that we are travelling longer than 5 days.
					if ((toDate - fromDate) < 345600000) {
						valid = false;
						message += "\t- The minimum hire period is 5 days.\n";
					}
				} else {
	        //check that we are travelling longer than 7 days.
					if ((toDate - fromDate) < 483840000) {
						valid = false;
						message += "\t- The minimum hire period is 7 days.\n";
					}
				}
				
				var Oct012009 = new Date(2009, 10 - 1, 1);

				if (fromDate < Oct012009 && motorhome.value == '12451') {
	        //check L4 is booked after Oct 01 2009
					valid = false;
					message += "\t- Base Jumper L4 is available from October 01, 2009.\n";
				}
    }
    
		/*
    if (valid && hasVoucherCode && rate < 100) {
        if (null != expiryDate) {
            var expireBits = expiryDate.split('-');
            expireDate = new Date(expireBits[0], expireBits[1] - 1, expireBits[2]);
            if ((expireDate - new Date()) <= 0) {
                valid = false;
                message += '\t- This discount code has expired.\n';
            }
        }
        
        if (hasVoucherCode && null != travelFrom) {
            var travelFromBits = travelFrom.split('-');
            travelFromDate = new Date(travelFromBits[0], travelFromBits[1] - 1, travelFromBits[2]);
            if ((travelFromDate - fromDate) > 0) {
                valid = false;
                message += '\t- Your travel must start after ' + travelFrom + '.\n';
            }
        }
        
        //if ( hasVoucherCode && null != travelUntil )	{
        //	var travelUntilBits = travelUntil.split('-');
        //	travelUntilDate = new Date(travelUntilBits[0], travelUntilBits[1] - 1, travelUntilBits[2] );
        //	if( (travelUntilDate - toDate ) <= 0 ) {
        //		valid = false;
        //		message += '\t- Your travel must end before ' + travelUntil +'.\n';					
        //	}			
        //}
    
    }*/
    
    
    if (!valid) {
        alert(message);
    }
    
    return valid;
}

function validateDropdown(form, dropdown, noSelectionValue){
    var selected = form.elements[dropdown].selectedIndex;
    return getSelectedItemInDropDown(form, dropdown).value != noSelectionValue;
}

function getSelectedItemInDropDown(form, dropdown){
    return form.elements[dropdown].options[form.elements[dropdown].selectedIndex];
}

// Second set of functions

function fromDateSelect(calendar, date){
    selectDropDownItemWithValue('qForm', 'fromMonth', calendar.date.getFullYear() + "-" + (calendar.date.getMonth() + 1));
    selectDropDownItemWithValue('qForm', 'fromDay', '' + (calendar.date.getDate()));
    if (calendar.dateClicked) {
        calendar.callCloseHandler();
    }
}

function toDateSelect(calendar, date){
    selectDropDownItemWithValue('qForm', 'toMonth', calendar.date.getFullYear() + "-" + (calendar.date.getMonth() + 1));
    selectDropDownItemWithValue('qForm', 'toDay', '' + (calendar.date.getDate()));
    if (calendar.dateClicked) {
        calendar.callCloseHandler();
    }
}
/*
var fromCalendar = Calendar.setup({
    "ifFormat": "%a %d %B %Y",
    "daFormat": "%a %d %B %Y",
    "ttFormat": "%a %d %B %Y",
    "button": "f-calendar-trigger-fromDate",
    "range": new Array('2008', '2010'),
    "onSelect": fromDateSelect
});
var toCalendar = Calendar.setup({
    "ifFormat": "%a %d %B %Y",
    "daFormat": "%a %d %B %Y",
    "ttFormat": "%a %d %B %Y",
    "button": "f-calendar-trigger-toDate",
    "range": new Array('2008', '2010'),
    "onSelect": toDateSelect
});
*/