rightnow = new Date();

function month(i,name) {
	var thisyear = rightnow.getFullYear();
	var nextyear = thisyear + 1;
	var a =	"";
    (i<rightnow.getMonth()) ? a+=nextyear : a+=thisyear;
	(i<9) ? a += "0"+(i+1) : a +=(i+1);
	var b = name+ ", ";
	(i<rightnow.getMonth()) ? b+=nextyear : b+=thisyear;
	this.name = b;
	this.value = a;
}

var months = new Array(new month(0,"January"), new month(1,"February"),new month(2,"March"),new month(3,"April"),new month(4,"May"), new month(5,"June"), new month(6,"July"), new month(7,"August"), new month(8,"September"), new month(9,"October"), new month(10,"November"), new month(11,"December"));
function init(){
	for(i=0;i<12;i++) {
		wrappedIndex = (rightnow.getMonth() + i) % 12;
		document.carSearchFormBean.pickMonth[i] = new Option (months[wrappedIndex].name,months[wrappedIndex].value);
		document.carSearchFormBean.dropMonth[i] = new Option (months[wrappedIndex].name,months[wrappedIndex].value);
	}
	
    for(d=0;d<31;d++) {
    	document.carSearchFormBean.dropDate[d] = new Option(d+1,((d<10)?"0"+(d+1):d+1));
    	document.carSearchFormBean.pickDate[d] = new Option(d+1,((d<10)?"0"+(d+1):d+1));
    }

    if(document.carSearchFormBean.pickUpDateForm.value==""){
		sevendays = new Date(rightnow.getTime()+(604800000));
		document.carSearchFormBean.pickDate.selectedIndex = sevendays.getDate()-1;
		then = new Date(sevendays.getTime()+(604800000))
		document.carSearchFormBean.dropDate.selectedIndex = then.getDate()-1;
		var datum = monthLengths[sevendays.getMonth()] -7
		if(rightnow.getDate() > datum ){document.carSearchFormBean.pickMonth.selectedIndex = document.carSearchFormBean.pickMonth.selectedIndex+1;}
		if(sevendays.getDate() > datum || rightnow.getMonth() < sevendays.getMonth() ){document.carSearchFormBean.dropMonth.selectedIndex = document.carSearchFormBean.dropMonth.selectedIndex+1;}
	}else{
		var pickup=document.carSearchFormBean.pickUpDateForm.value;
		var drop=document.carSearchFormBean.dropOffDateForm.value;
		var pmonth=parseInt(pickup.slice(4,6),10);
		var pday=parseInt(pickup.slice(6,8),10);
		var ptime=parseInt(pickup.slice(8,10),10);
		document.carSearchFormBean.pickMonth.selectedIndex=pmonth-1;
		document.carSearchFormBean.pickDate.selectedIndex=pday-1;
		document.carSearchFormBean.pickHour.selectedIndex=ptime;
		var dmonth=parseInt(drop.slice(4,6),10);
		var dday=parseInt(drop.slice(6,8),10);
		var dtime=parseInt(drop.slice(8,10),10);
		document.carSearchFormBean.dropMonth.selectedIndex=dmonth-1;
		document.carSearchFormBean.dropDate.selectedIndex=dday-1;
		document.carSearchFormBean.dropHour.selectedIndex=dtime;
	}

	//changeDate(document.carSearchFormBean.pickMonth, document.carSearchFormBean.pickDate);
	//changeDate(document.carSearchFormBean.dropMonth, document.carSearchFormBean.dropDate);
}
function setPickLoc() {
	document.carSearchFormBean.destination1.value = document.carSearchFormBean.searchString.value;
}
function setDropLoc() {
	document.carSearchFormBean.destination2.value = document.carSearchFormBean.searchStringDrop.value;
}
function submitCheck() {
    if (document.carSearchFormBean.age.selectedIndex!=0){
    	var day = parseInt(document.carSearchFormBean.pickDate.options[document.carSearchFormBean.pickDate.selectedIndex].value,10);
    	var monthVal = new String(document.carSearchFormBean.pickMonth.options[document.carSearchFormBean.pickMonth.selectedIndex].value);
    	var month = parseInt(monthVal.slice(4),10);
    	var year = parseInt(monthVal.slice(0,4),10);
    	var pickupDate = new Date(year,month-1,day,12,0,0,0);
    	day = parseInt(document.carSearchFormBean.dropDate.options[document.carSearchFormBean.dropDate.selectedIndex].value,10);
    	monthVal = new String(document.carSearchFormBean.dropMonth.options[document.carSearchFormBean.dropMonth.selectedIndex].value);
    	month = parseInt(monthVal.slice(4),10);
    	year = parseInt(monthVal.slice(0,4),10);

		var dropoffDate = new Date(year,month-1,day,12,0,0,0);
    	if (pickupDate>dropoffDate || pickupDate<rightnow) {
	    	if (pickupDate>dropoffDate) {
	    		alert("Please check your drop-off date");
	    		document.getElementById("returnDate").style.color="#FF3300";
	    	}
	    	if (pickupDate<rightnow) {
	    		alert("Please check your pick-up date");
	    		document.getElementById("outboundDate").style.color="#FF3300";
	    	}
    		return false;
    	} else {
			document.carSearchFormBean.pickUpDateForm.value=""+pickupDate.getFullYear()+
			(((pickupDate.getMonth()+1)<10)?"0"+(pickupDate.getMonth()+1):(pickupDate.getMonth()+1))+
				((pickupDate.getDate()<10)?"0"+pickupDate.getDate():pickupDate.getDate())+
				((pickupDate.getHours()<10)?"0"+pickupDate.getHours():pickupDate.getHours())
				+((pickupDate.getMinutes()<10)?"0"+pickupDate.getMinutes():pickupDate.getMinutes());
			document.carSearchFormBean.dropOffDateForm.value=""+dropoffDate.getFullYear()+
			(((dropoffDate.getMonth()+1)<10)?"0"+(dropoffDate.getMonth()+1):(dropoffDate.getMonth()+1))+
				((dropoffDate.getDate()<10)?"0"+dropoffDate.getDate():dropoffDate.getDate())+
				((dropoffDate.getHours()<10)?"0"+dropoffDate.getHours():dropoffDate.getHours())+
				((dropoffDate.getMinutes()<10)?"0"+dropoffDate.getMinutes():dropoffDate.getMinutes());

			if (document.carSearchFormBean.destination2.value=="") {
				document.carSearchFormBean.destination2.value = document.carSearchFormBean.destination1.value;
			}
			document.carSearchFormBean.destination1.value=escape(document.carSearchFormBean.destination1.value);
			document.carSearchFormBean.destination2.value=escape(document.carSearchFormBean.destination2.value);
			return true;
        }
    }
}

var monthLengths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

function isLeapYear(year) {
	return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}

function changeDate(objMonth, objDate) {
	maxDays = monthLengths[(rightnow.getMonth() + objMonth.selectedIndex) % 12];
	if ( (objMonth.selectedIndex == 1) && (isLeapYear(objMonth.value.substring(0, 4))) ) maxDays="29";

	var oldSelect = objDate.selectedIndex;
	if (oldSelect > maxDays-1) oldSelect = maxDays-1;
	
	objDate.length = 0;
	for (d=0;d<maxDays;d++) objDate[d] = new Option(d+1,((d<10)?"0"+(d+1):d+1));

	objDate.selectedIndex = oldSelect;
}

function validateAge() {
	var valid = (document.carSearchFormBean.age.selectedIndex != 0);
	if (valid == false) {
		alert("You must enter a driver age");
		document.getElementById("driverAge").style.color="#FF3300";
	}
	return valid;
}

function validatePickup() {
	var valid = (document.carSearchFormBean.destination1.value != "");
	if (valid == false) {
		alert("You must enter a pick up destination");
		document.getElementById("pickupDest").style.color="#FF3300";
	}
	return valid;
}

function validateForm() {
	document.getElementById("driverAge").style.color="#000000";
	document.getElementById("pickupDest").style.color="#000000";
	document.getElementById("returnDate").style.color="#000000";
    document.getElementById("outboundDate").style.color="#000000";
	return ( validateAge() ) && ( validatePickup() ) && ( submitCheck() );
}

