// JavaScript Document
function GetDays(monthIndex, intType, dayIndex) {
	var optionNew;
	var now=new Date();
	var currMonth = now.getMonth();
	var year=now.getYear();
	//Clear Select Box and Add New Days
	if (intType=='F') {
		var objDay =document.APSForm.DayF;
		var objMonth=document.APSForm.MonthF;
	}
	else {
		objDay =document.APSForm.DayT;
		objMonth=document.APSForm.MonthT;
	}
	for (i=objDay.options.length; i>=0; i--){
			objDay.options[i]=null;
	}
	var month = objMonth.options[monthIndex].value;
	if (currMonth > month) {
		year++;
	}
	var days = getDaysInMonth(month,year)
	for (j=1; j<=days; j++) {
		optionNew=new Option(j,j);
		objDay.options[j-1]=optionNew;
	}
	objDay.options[dayIndex].selected=true;
}

function getDaysInMonth(month,year)  {
	var days;
	if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)  days=31;
	else if (month==4 || month==6 || month==9 || month==11) days=30;
	else if (month==2)  {
		if (isLeapYear(year)) { days=29; }
		else { days=28; }
	}
	return (days);
}


function isLeapYear (Year) {
	if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) {
		return (true);
	} else { return (false); }
}

function ResetValues() {
			GetAddr(document.APSForm.routeF.selectedIndex,1); 
			GetAddr(document.APSForm.routeT.selectedIndex,2);
			GetDays(document.APSForm.MonthF.selectedIndex, 'F', document.APSForm.DayF.selectedIndex);
			GetDays(document.APSForm.MonthT.selectedIndex, 'T', document.APSForm.DayT.selectedIndex);
}

function TestValues() {
	bPassed=true;
	var now=new Date();
	var currMonth = now.getMonth()+1;
	var currDay=now.getDate();
	var currYear=now.getYear();
	var Today = new Date(currYear, now.getMonth(), currDay, now.getHours(), now.getMinutes(), 0);
	if (document.APSForm.rteOption[0].checked || document.APSForm.rteOption[1].checked) {
		var MonthF=document.APSForm.MonthF.options[document.APSForm.MonthF.selectedIndex].value;
		var DayF=document.APSForm.DayF.options[document.APSForm.DayF.selectedIndex].value;
		var HourF=document.APSForm.HourF.options[document.APSForm.HourF.selectedIndex].value;
		var MinF=document.APSForm.MinuteF.options[document.APSForm.MinuteF.selectedIndex].value;
		//Convert to numeric with the ++ and then add 11 to get 12 hour clock
		if (document.APSForm.AmpmF.options[document.APSForm.AmpmF.selectedIndex].value=="PM") {
			HourF++;
			HourF+=11;
		}
		if (MonthF<currMonth) {
			var DateF=new Date(currYear+1, MonthF-1, DayF, HourF, MinF, 0);
		}
		else {
			DateF=new Date(currYear, MonthF-1, DayF, HourF, MinF, 0);
		}
		if (DateF<=Today) {
			alert ("Route to Airport:  Date/Time selection cannot be in the past.");
			bPassed=false;
		}
	}
	if (bPassed) {
		if (document.APSForm.rteOption[0].checked || document.APSForm.rteOption[2].checked) {
			var MonthT=document.APSForm.MonthT.options[document.APSForm.MonthT.selectedIndex].value;
			var DayT=document.APSForm.DayT.options[document.APSForm.DayT.selectedIndex].value;
			var HourT=document.APSForm.HourT.options[document.APSForm.HourT.selectedIndex].value;
			var MinT=document.APSForm.MinuteT.options[document.APSForm.MinuteT.selectedIndex].value;
			//Convert to numeric with the ++ and then add 11 to get 12 hour clock
			if (document.APSForm.AmpmT.options[document.APSForm.AmpmT.selectedIndex].value=="PM") {
				HourT++;
				HourT+=11;
			}
			if (MonthT<currMonth) {
				var DateT=new Date(currYear+1, MonthT-1, DayT, HourT, MinT, 0);
			}
			else {
				DateT=new Date(currYear, MonthT-1, DayT, HourT, MinT, 0);
			}
			if (DateT<=Today) {
				alert ("Route from Airport:  Date/Time selection cannot be in the past.");
				bPassed=false;
			}
		}
	}
	
	
	// Check to make sure the return date is after the departure date
	/*
	if(bPassed) 
	{
		
		if(document.APSForm.rteOption[0].checked == true)
		{
			
			if($('#MonthF').val() > $('#MonthT').val()){ // Departure month is greater than arrival month
				alert('Departure date must be earlier than arrival date.');
				bPassed = false;
			}else if($('#MonthF').val() == $('#MonthT').val()){ // same month :: validate day
				
				if(parseFloat($('#DayF').val()) > parseFloat($('#DayT').val())){ // departure day is greater than arrival day
					alert('Departure date must be earlier than arrival date.');
					bPassed = false;
				}else if($('#DayF').val() == $('#DayT').val()){ // dates are equal
				
					alert('Arrival date cannot be the same as departure date.');
					bPassed = false;
					
				
					if($('#AmpmF').val() == $('#AmpmT').val()){
					
						if(parseInt($('#HourF').val()) >= parseInt($('#HourT').val())){ 
						// arrival hour must be later than departure
							
							alert('Departure time must be earlier than arrival time.');
							bPassed = false;
						}
					}
				}
			
			}
		}
	}
	
	/*if (bPassed) {
		if (document.APSForm.rteOption[0].checked) {
			if (document.APSForm.AirportT.options[document.APSForm.AirportT.selectedIndex].text
				=="Mall of America") {
				var MonthT=document.APSForm.MonthT.options[document.APSForm.MonthT.selectedIndex].value;
				var DayT=document.APSForm.DayT.options[document.APSForm.DayT.selectedIndex].value;
				var MonthF=document.APSForm.MonthF.options[document.APSForm.MonthF.selectedIndex].value;
				var DayF=document.APSForm.DayF.options[document.APSForm.DayF.selectedIndex].value;
				if (MonthT!=MonthF) {
					alert ("Same day required for Mall of America route.");
					bPassed=false;
				}
				if (DayT!=DayF) {
					alert ("Same day required for Mall of America route.");
					bPassed=false;
				}
			}
		}
	}*/
	if (bPassed) {
		var bEnterInfo=true;
		if (document.APSForm.rteOption[0].checked || document.APSForm.rteOption[1].checked) {
			if (document.APSForm.AirlineF.options[document.APSForm.AirlineF.selectedIndex].text=="") {
				bPassed=!confirm("Adding airline information will be of great assistance to our driver. Click on okay to return to the form and enter the information or cancel to continue.");
				bEnterInfo=bPassed;
			}
		}	
		if (bEnterInfo && document.APSForm.rteOption[0].checked || document.APSForm.rteOption[2].checked) {
			if (document.APSForm.AirlineT.options[document.APSForm.AirlineT.selectedIndex].text=="") {
				bPassed=!confirm("Adding airline information will be of great assistance to our driver. Click on okay to return to the form and enter the information or cancel to continue.");
			}
		}
	}
	
	
	
	
	if(bPassed){
		// Open Terms & Conditions window
		$('#modal').modal({
			opacity:80,
			overlayCss: {backgroundColor:"#000"}
		});
		return false;
	}else{
		return false;
	}
	
	
}