// JavaScript Calendar for Liberty
// Make sure to also put calendar.inc in the file you are calling this from
// @author Jason Gabriele, Travwell
//Updated by Lucas Bennett, SwiftTrip 08-18-2008
//Updated by Lucas Bennett, SwiftTrip 01-30-2009 -- Fix to the "Double March Bug"

 <!-- Lukes ADDED STUFF -->
				  var isMultiDestinationFlightSet = new Array();
				  isMultiDestinationFlightSet[0] = 0;
				  isMultiDestinationFlightSet[1] = 0;
				  isMultiDestinationFlightSet[2] = 0;
				  var departDateMonth;
				  var departDateYear;
				  var departDateDay;
				  var returnDateMonth;
				  var returnDateYear;
				  var returnDateDay;
				  var calOption = 0;
				  var isDepartSet = 0;
				  var isReturnSet = 0;
				  var multiDestinationMonth=new Array(4);
				  var multiDestinationDate=new Array(4);
				  var multiDestinationYear=new Array(4);
				  var multiDestFlightNum = 0;
				  var isMultiDestinationFlight = 0;
				  
				  function setFlightNumber(theflight)
				  	{
					multiDestFlightNum = theflight;
					isMultiDestinationFlight = 1;
					//validateLastFlight(theflight);
					//alert("this is flight number" + multiDestFlightNum);
						
					}
				  
				  function setMultiDestination(flightNumber, theday, themonth, theyear)
				  	{
					multiDestinationMonth[flightNumber] = themonth;
					multiDestinationDate[flightNumber] = theday;
					multiDestinationYear[flightNumber] = theyear;
					//alert("this is flight number " + flightNumber + "on" + multiDestinationDate[flightNumber] + "," + multiDestinationMonth[flightNumber]	+ ","	+ multiDestinationYear[flightNumber] );			
						
					}
				  
				  function getDepartYMD(theday, themonth, theyear)
				  	{
						departDateDay = theday;
				  		departDateMonth = themonth;
						departDateYear = theyear;
						isDepartSet = 1;
						//alert(departDateDay);
						//alert(departDateYear);
				  		//alert(departDateMonth);
				   	}
				 function getReturnYMD(theday, themonth, theyear)
				  	{
						returnDateDay = theday;
				  		returnDateMonth = themonth;
						returnDateYear = theyear;
						isReturnSet = 1;
						//alert(departDateDay);
						//alert(departDateYear);
				  		//alert(departDateMonth);
				   	}
				  function setCalOption(flag)
					{
							calOption = flag;
							//alert(calOption);
					}
					
				  function setMultiDestinationFlight(flightNumber)
				  	{

						isMultiDestinationFlightSet[flightNumber-1] = 1;

					}
					
				  function validateLastFlight(flightNumber)
					{
						//alert(flightNumber);
						if(flightNumber == 1)
							{
							}
						else{
							if(isMultiDestinationFlightSet[flightNumber-2] == 0)
								{
									var previous = flightNumber - 1;
								alert("You must fill out appropriate information for Flight Number " + previous + " before submitting the following information.");
							return false;
							//cant use this because its two different functions
								
								}
								
							
							}
												
					//alert("the last flight is being validated");	
					}
				  
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

var defaultDay = new Date().getDate();
var defaultMonth = new Date().getMonth()+1;
var defaultYear = new Date().getFullYear();

var selectedDay = defaultDay;
var selectedMonth = defaultMonth;
var selectedYear = defaultYear;

//Iframe's src location
var iFrameSource = '';
//field open
//@var object
var inAction = false;
//array to hold objects
var compareArray = null;
//second month is present
//set to true if second month is present
var secondMonth = true;
//is konqueror?
//workaround for khtml browsers
var khtml = false;
var netscape = false;
var firefox = false;
if(window.navigator.product) {
	if(window.navigator.product.toLowerCase().indexOf("khtml")!=-1) khtml = true;
}
//check for safari and netscape
if(window.navigator.vendor) {
	if(window.navigator.vendor.toLowerCase().indexOf("apple")!=-1) {
		khtml = true;
	} else if(window.navigator.vendor == "Netscape") { 
		netscape = true;
	} else if(window.navigator.vendor == "Firefox") {
		firefox = true;
	}
}
//for firefox bug
var alreadyShown = false;
//is QA Server
var qaServer = false;
if(window.location.href.indexOf("qa.")!=-1) qaServer = true;
//number of days to skip forward by default
//only applicable to the compareFields function
//and only when day fields are already filled in
var daySkip = 0;

function showCalendar(inputBox) {
		
	if(showCalendar.arguments.length == 0) return false;
	
	if(!alreadyShown && firefox) {
		document.getElementById("cal_frame").style.display = "block";
		document.getElementById("cal_frame").style.display = "none";
		alreadyShown = true;
	}
	
	checkFormat(inputBox); //replace dashes
	
	if(showCalendar.arguments.length > 2) {
		compareArray = new Array(showCalendar.arguments.length-1);
		for(var i=1;i<showCalendar.arguments.length;i++) {
			compareArray[i-1] = showCalendar.arguments[i];
		}
	}
	
	if(inputBox.value == "" || !inputBox.value.indexOf("/") || inputBox.value == "mm/dd/yyyy" || inputBox.value == "MM/DD/YYYY") {
		year = defaultYear;
		month = defaultMonth;
		day = defaultDay;
	} 
	else 
	{
		var dateParts = inputBox.value.split("/");
		if(dateParts.length == 3) {
			month = new Number(dateParts[0]);
			day = new Number(dateParts[1]);
			year = new Number(dateParts[2]);
		} else {
			year = defaultYear;
			month = defaultMonth;
			day = defaultDay;
		}
	}
	
	
	
	//This is where you set the month for return date from the depature month on a roundtrip flight
			if(calOption == 2)
			{
				if(isDepartSet == 1)
				{
			 	month = departDateMonth;
			 	year = departDateYear;
				}
			}
			
	//This is where you set the month and year for a multidestination flight for the 2nd, 3rd or 4th flight as the default month on the calendar
		if(isMultiDestinationFlight == 1)
			{
			//alert("this is a multidestinationflight");	
				if(multiDestFlightNum == 4)
					{
						month = multiDestinationMonth[3];
						year = multiDestinationYear[3];
					}
				else if(multiDestFlightNum == 3)
					{
						month = multiDestinationMonth[2];
						year = multiDestinationYear[2];

					}
				else if(multiDestFlightNum == 2)
					{
						month = multiDestinationMonth[1];
						year = multiDestinationYear[1];
					}
				
			}
	
	
	//set year dropdown
	var drop = window.frames["cal_frame"].document.forms["calendarForm"].yearDrop.options;
	
	for(var x = 0; x < drop.length; x++) {
		if(drop[x].value == year) {
			window.frames["cal_frame"].document.forms["calendarForm"].yearDrop.selectedIndex = x;
			break;
		}
	}
	//set month dropdown
	window.frames["cal_frame"].document.forms["calendarForm"].monthDrop.selectedIndex = month-1;

	if(inAction) {
		hideCalendar();
	}
	inAction = inputBox;
			
			
	calendar(month,day,year,1);

	if(secondMonth) {
		if(month == 12) {
			calendar(1,1,parseInt(year)+1,2);
		} else {
			calendar(parseInt(month)+1,1,year,2);
		}
	}
	
	if(khtml || netscape) {
		document.getElementById("cal_frame").style.visibility = "visible";
	} else {
		document.getElementById("cal_frame").style.display = "block";
	}
	
	positionWindow(inputBox);
}

//pass true if you want to save
function hideCalendar(save) {
	if(!inAction) return;
	if(save) {
		inAction.value = getSelectedMonth() + "/" + getSelectedDay() + "/" + getSelectedYear();
	}
	inAction = null;

	if(khtml || netscape) {
		document.getElementById("cal_frame").style.visibility = "hidden";
	} else {
		document.getElementById("cal_frame").style.display = "none";
	}
	
	if(compareArray) {
		compareFields();
	}
}

//returns true if calendar is shown
function isCalendarShown() {
	return inAction;
}

function returnDate(month,day,year) {
	today = new Date();
	tempDate = new Date(year,month-1,day);
	if((tempDate.getTime() - today.getTime()) > 31536000000) {
		alert("Sorry, you cannot book dates more than 1 year in advance.");
		return false;
	}
	
	inAction.value = month + "/" + day + "/" + year;
	hideCalendar();
	//alert(multiDestFlightNum);
	setMultiDestinationFlight(multiDestFlightNum);
}

function calendar(month,day,year,calNum) {


	if(!month) month = defaultMonth;
	//if(!day) day = defaultDay;  THIS LINE WAS A BUG
	day = parseInt(1);
	//01-30-09: Fix to the bug in the line above-- I left the line commented out for debugging purposes in the future
	//The problem was that if the current day's date is greater then the amount of days in the next month, and the user
	//selects a date for departure in the next month, when the page goes to load next month as its starting month, it 
	//would keep today's date as its default date. However, in February's case, and the 30 day months, Javascript would
	//relealize "Hey, it thinks the date is February 30, that date doesnt exist! So I'm going to make the date March 1st
	//instead" and thus it would display two months with the title March, even though the rest of the calendar was fine.
	//And therefore I just automatically set the date to the first everymonth regardless. This same fix was done before on
	//09-05-08 for browsing months in the calendar. So it is possible it could be needed in some other instance elsewhere.
	if(!year) year = defaultYear;
	if(!calNum) calNum = 1;

	//if(!checkValid(month,day,year))return false;
	
	
	var selected = new Date(year,month-1,day);
	//alert(months[selected.getMonth()]+ selected.getDate());

	if(calNum == 1) {
		//set selected
		selectedDay = day;
		selectedMonth = month;
		selectedYear = year;
		
	}
	
	//start table

	var tdCount = 0;
	var html = "<table class='calendar'><tr>";
	html += "<th colspan='7'>" + months[selected.getMonth()] + "</th>";
	html += "</tr><tr>";
	    html += "<td><font size=\"1\">Su</font></td>";
        html += "<td><font size=\"1\">Mo</font></td>";
        html += "<td><font size=\"1\">Tu</font></td>";
        html += "<td><font size=\"1\">We</font></td>";
        html += "<td><font size=\"1\">Th</font></td>";
        html += "<td><font size=\"1\">Fr</font></td>";
        html += "<td><font size=\"1\">Sa</font></td>";
        html += "</tr><tr>";
	
	//get start day
	var firstDay = new Date(year,month-1,1).getDay();
	for(var x=0; x<firstDay; x++) {
		html += "<td>&nbsp;</td>";
		tdCount++;
	}
	
	today = new Date();
	
	
	//make calendar
	for(var x = 1; x<=31; x++) {
		if(tdCount % 7 == 0) html += "</tr><tr>";
		
		if(checkValid(month,x,year)) 
		{
				var tempDay = new Date(year,month-1,x);
				returnCode = "" + month + "," + x + "," + year;
				html += "<td";
			if(calOption == 1)
			{

				if(isReturnSet == 1)
				{
					
					//Check to see if it is a departure or arrival
					if((tempDay.getTime() - today.getTime()) <= 0 ) 
						{
						if( x ==  today.getDate() ) 
							{
								html += "><a href='javascript:void parent.returnDate(" + returnCode + ");javascript:void parent.getDepartYMD("+ x + "," + month +","+ year +");'>" + tempDay.getDate() + "</a></td>";
							}

							else
							{
							html += " class='past'>" + tempDay.getDate() + "</td>";
							}
						} 
					//else
						//{
						//	if(month < departDateMonth && year <= departDateYear)
						//	{
						//	html += " class='past'>" + tempDay.getDate() + "</td>";
						//	}
						//else if(month == departDateMonth && x < departDateDay && year <= departDateYear)
						//	{
						//	html += " class='past'>" + tempDay.getDate() + "</td>";
						//	}
						//else if(year < departDateYear)
						//	{
						//	html += " class='past'>" + tempDay.getDate() + "</td>";
						//	}
						
						
						//If Statements to block out future dates in round trip flights if return date has been selected//
						else if(year > returnDateYear)
							{
							//This if is to block all the dates of the future year if a return date has been selected
							html += " class='past'>" + tempDay.getDate() + "</td>";
							}
						else if(month > returnDateMonth)
							{
							//This if is to block all the future months of the current year if a return date has been selected
							html += " class='past'>" + tempDay.getDate() + "</td>";
							}
						else if(month == returnDateMonth && x > returnDateDay)
							{
							//This if is to block all the future months of the current year if a return date has been selected
							html += " class='past'>" + tempDay.getDate() + "</td>";
							}
						//End of if statements to blouck out future dates if return date has been selected for round trip	
							
						else
							{
			html += "><a href='javascript:void parent.returnDate(" + returnCode + ");javascript:void parent.getDepartYMD("+ x + "," + month +","+ year +");'>" + tempDay.getDate() + "</a></td>";
							}
						//}
				}
				else
				{
					 
						if((tempDay.getTime() - today.getTime() ) <= 0 ) 
						{
							<!-- this statement checks if its todays date of this month, and leaves it open, else, it greys out all dates from previous months of todays date-->
							if( x ==  today.getDate() && month == today.getMonth()+1 ) 
								{
								html += "><a href='javascript:void parent.returnDate(" + returnCode + ");javascript:void parent.getDepartYMD("+ x + "," + month +","+ year +");'>" + tempDay.getDate() + "</a></td>";
								}
							else
							{
							html += " class='past'>" + tempDay.getDate() + "</td>";
							}
						} 
						else
						{
				html += "><a href='javascript:void parent.returnDate(" + returnCode + ");javascript:void parent.getDepartYMD("+ x + "," + month +","+ year +");'>" + tempDay.getDate() + "</a></td>";	
						}
				}
			}
			else if(calOption == 2)
			{

				if(isDepartSet == 1)
				{
					
					//Check to see if it is a departure or arrival
					if((tempDay.getTime() - today.getTime()) <= 0 ) 
						{
						if( x ==  today.getDate() )
							{
								if(x == departDateDay)
								{
								html += "><a href='javascript:void parent.returnDate(" + returnCode + ");javascript:void parent.getDepartYMD("+ x + "," + month +","+ year +");'>" + tempDay.getDate() + "</a></td>";
								}
								else
								{
								html += " class='past'>" + tempDay.getDate() + "</td>";
								}
							}
							else
							{
							html += " class='past'>" + tempDay.getDate() + "</td>";
							}
						} 
					else
						{
							if(month < departDateMonth && year <= departDateYear)
							{
							html += " class='past'>" + tempDay.getDate() + "</td>";
							}
						else if(month == departDateMonth && x < departDateDay && year <= departDateYear)
							{
							html += " class='past'>" + tempDay.getDate() + "</td>";
							}
						else if(year < departDateYear)
							{
							html += " class='past'>" + tempDay.getDate() + "</td>";
							}
						else
							{
			html += "><a href='javascript:void parent.returnDate(" + returnCode + ");javascript:void parent.getReturnYMD("+ x + "," + month +","+ year +");'>" + tempDay.getDate() + "</a></td>";
							}
						}
				}
				else
				{
						if((tempDay.getTime() - today.getTime()) <= 0 ) 
						{
							if( x ==  today.getDate() && month == today.getMonth()+1) 
								{
								html += "><a href='javascript:void parent.returnDate(" + returnCode + ");javascript:void parent.getDepartYMD("+ x + "," + month +","+ year +");'>" + tempDay.getDate() + "</a></td>";
								}
							else
							{
							html += " class='past'>" + tempDay.getDate() + "</td>";
							}
						} 
						else
						{
				html += "><a href='javascript:void parent.returnDate(" + returnCode + ");javascript:void parent.getReturnYMD("+ x + "," + month +","+ year +");'>" + tempDay.getDate() + "</a></td>";	
						}
				}
			}
			else if(isMultiDestinationFlight == 1)
			{
				

					if((tempDay.getTime() - today.getTime()) <= 0 ) 
						{
							if( x ==  today.getDate() && month == today.getMonth()+1) 
								{
									
									if( (multiDestinationDate[multiDestFlightNum-1] < x && multiDestinationMonth[multiDestFlightNum-1] > month) || (multiDestinationYear[multiDestFlightNum+1] > year) ||(multiDestinationYear[multiDestFlightNum-1] > year))
									{
										html += " class='past'>" + tempDay.getDate() + "</td>";
									}
									
									else//This else prints todays date on the calander only
									{
										if(x == multiDestinationDate[multiDestFlightNum-1])
									//this if leaves today open if for some odd reason someone wants a multidestination flight today
											{
											html += "><a href='javascript:void parent.returnDate(" + returnCode + ");javascript:void parent.setMultiDestination("+ multiDestFlightNum + "," + x + "," + month +","+ year +");'>" + tempDay.getDate() + "</a></td>";
											}
										else if(multiDestFlightNum == 1)
										//this if leaves today open for the first flight only
											{
												html += "><a href='javascript:void parent.returnDate(" + returnCode + ");javascript:void parent.setMultiDestination("+ multiDestFlightNum + "," + x + "," + month +","+ year +");'>" + tempDay.getDate() + "</a></td>";
											}
										else//if this if is reached, today should not be available as a flight, because it is passed
											{
											html += " class='past'>" + tempDay.getDate() + "</td>";
											}
												//html += "><a href='javascript:void parent.returnDate(" + returnCode + ");javascript:void parent.setMultiDestination("+ multiDestFlightNum + "," + x + "," + month +","+ year +");'>" + tempDay.getDate() + "0</a></td>";
										}
								

								}
							else
							{
							html += " class='past'>" + tempDay.getDate() + "</td>";
							}
						} 
					else
						{
							if(month < multiDestinationMonth[multiDestFlightNum-1] && year <= multiDestinationYear[multiDestFlightNum-1])
							{
							html += " class='past'>" + tempDay.getDate() + "</td>";
							}
						else if(month == multiDestinationMonth[multiDestFlightNum-1] && x < multiDestinationDate[multiDestFlightNum-1] && year <= multiDestinationYear[multiDestFlightNum-1])
							{
							html += " class='past'>" + tempDay.getDate() + "</td>";
							}
						else if(year < multiDestinationYear[multiDestFlightNum-1])
							{
							html += " class='past'>" + tempDay.getDate() + "</td>";
							}
						else if(month == multiDestinationMonth[multiDestFlightNum+1] && x > multiDestinationDate[multiDestFlightNum+1] && year >= multiDestinationYear[multiDestFlightNum+1])
							{
							//this should appear only if you have selected atleast two dates for multidest, and go back and change a previous	
							html += " class='past'>" + tempDay.getDate() + "</td>";
							}
							
						else if(year > multiDestinationYear[multiDestFlightNum+1] && month > multiDestinationMonth[multiDestFlightNum+1] )
							{
							html += " class='past'>" + tempDay.getDate() + "</td>";
							}
							else if(year == multiDestinationYear[multiDestFlightNum+1] && month > multiDestinationMonth[multiDestFlightNum+1] )
							{
							html += " class='past'>" + tempDay.getDate() + "</td>";
							}
							
							
							else if(year > multiDestinationYear[multiDestFlightNum+1] && month < multiDestinationMonth[multiDestFlightNum+1] )
							{
							//This if is to block out months that are less then the month selected date in a future year
							html += " class='past'>" + tempDay.getDate() + "</td>";
							}
							else if(year > multiDestinationYear[multiDestFlightNum+1] && month == multiDestinationMonth[multiDestFlightNum+1] &&  x <= multiDestinationDate[multiDestFlightNum+1])
							{
							//This if is to block all the dates of the same month selected in the future year
							html += " class='past'>" + tempDay.getDate() + "</td>";
							}
						
						else
							{
			html += "><a href='javascript:void parent.returnDate(" + returnCode + ");javascript:void parent.setMultiDestination("+ multiDestFlightNum + "," + x + "," + month +","+ year +");'>" + tempDay.getDate() + "</a></td>";	
							}
						}
				
				
				
				
			}
			else//just a back up incase something doesnt work with the round trip or multidestination page
			{
				
				if((tempDay.getTime() - today.getTime()) <= 0 ) 
					{
						
							if( x ==  today.getDate() && month == today.getMonth()+1 ) 
								{
								html += "><a href='javascript:void parent.returnDate(" + returnCode + ");javascript:void parent.getDepartYMD("+ x + "," + month +","+ year +");'>" + tempDay.getDate() + "</a></td>";
								}
							else
							{
							html += " class='past'>" + tempDay.getDate() + "</td>";
							}

					}
				else
					{
					html += "><a href='javascript:void parent.returnDate(" + returnCode + ");javascript:void parent.setMultiDestination("+ multiDestFlightNum + "," + x + "," + month +","+ year +");'>" + tempDay.getDate() + "</a></td>";		
					}
			}
		} 
		else 
		{
			break;
		}
		
		
		
		
		
		
		tdCount++;
	}

	//finish with blank cells
	if((tdCount%7)>0) {
		var left =  7 - tdCount%7;
		for(var x=0;x<left;x++) {
			html += "<td>&nbsp;</td>";
		}
	}
	
	html += "</tr></table>";
	
	window.frames["cal_frame"].document.getElementById("calendar"+calNum).innerHTML = html;
	
	return true;
}

function updateCalendar() {
	month = getSelectedMonth();
	//alert("month = " + month);
	day = getSelectedDay();
	year = getSelectedYear();
	calendar(month,day,year,1);
	if(secondMonth) 
	{
		if(month == 12) 
		{
			calendar(1,1,parseInt(year)+1,2);
		} 
		else {
			calendar(parseInt(month)+1,1,year,2);
		}
	}
	positionWindow(inAction);
}

function checkValid(month,day,year) {
	tryDate = new Date(year,month-1,day);
	//alert(month); only produced one february
	if(tryDate.getFullYear() == year && 
	   tryDate.getDate() == day &&
	   tryDate.getMonth() == month - 1) {
		//alert("true = " + month + "/" + day + "/" + year);
		return true;
	} else {
		//alert("false = " + month + "/" + day + "/" + year);
		return false;
	}
}

//checks a string and returns a date object
function extractDate(dateString) {
	if(dateString == "" || !dateString.indexOf("/") || dateString.toLowerCase() == "mm/dd/yyyy") {
		return false;
	} else {
		var dateParts = dateString.split("/");
		if(!dateParts.length == 3) {
			return false;
		} else {
			month = new Number(dateParts[0]);
			day = new Number(dateParts[1]);
			year = new Number(dateParts[2]);
		}
		
		if(checkValid(month,day,year)) {
			return new Date(year,month-1,day);
		} else {
			return false;
		}
	}
}

function getSelectedYear() {
	var drop = window.frames["cal_frame"].document.forms["calendarForm"].yearDrop;
	return selectedYear = drop.options[drop.selectedIndex].value;
}
function getSelectedDay() {
	return selectedDay;
}
function getSelectedMonth() {
	var drop = window.frames["cal_frame"].document.forms["calendarForm"].monthDrop;
	return selectedMonth = drop.options[drop.selectedIndex].value;
}

//setup the fields with todays date.
// @param objects 1 or more fields to populate
function setupFields(fields) {
	if(setupFields.arguments.length == 0) return false;

	//setup first field if necessary
	if(setupFields.arguments[0].value == "" || setupFields.arguments[0].value.toLowerCase() == "mm/dd/yyyy") {
    	var tempDay = defaultDay + 14;
    	var tempMonth = defaultMonth;
    	var tempYear = defaultYear;
	    futureDate = new Date(tempYear,tempMonth-1,tempDay);
	    setupFields.arguments[0].value = "" + (futureDate.getMonth()+1) + "/" + futureDate.getDate() + "/" + futureDate.getFullYear();
	} else { //extract current date
	    var tempDate;
	    if(!(tempDate = extractDate(setupFields.arguments[0].value))) return false;
	    var tempDay = tempDate.getDate();
    	var tempMonth = tempDate.getMonth()+1;
    	var tempYear = tempDate.getFullYear();
	}
	
	//setup rest of the fields
	if(setupFields.arguments.length > 1) {
		for(var i=1;i<setupFields.arguments.length;i++) {
		    if(setupFields.arguments[i].value == "" || setupFields.arguments[i].value.toLowerCase() == "mm/dd/yyyy") {
			    var futureEndDate = new Date(tempYear,tempMonth-1,tempDay+3);
			    setupFields.arguments[i].value = "" + (futureEndDate.getMonth()+1) + "/" + futureEndDate.getDate() + "/" + futureEndDate.getFullYear();
			    tempDay+=3;
			} else {
			    var tempDate;
	            if(!(tempDate = extractDate(setupFields.arguments[i].value))) return false;
        	    tempDay = tempDate.getDate();
            	tempMonth = tempDate.getMonth()+1;
            	tempYear = tempDate.getFullYear();
			}
		}
	}
}

//check several fields to make sure they operate in order
function compareFields() {
	if(compareArray==null || compareArray.length < 2) return false;
	checkFormat(compareArray[0]); //replace dashes
	
	var tempDate;
	var previousDate;
	
	for(var i=1;i<compareArray.length;i++) {
		if(compareArray[i].value == "" && (previousDate = extractDate(compareArray[i-1].value))) {
//			startYear = previousDate.getFullYear();
//	   		startDay = previousDate.getDate();
//	   		startMonth = previousDate.getMonth()+1;
//	   		alert("Your departure date has been selected after your return. Please rewise the return date");
//			compareArray[i].value = "";
		} else {
			checkFormat(compareArray[i]); //replace dashes
			if((tempDate = extractDate(compareArray[i].value)) && (previousDate = extractDate(compareArray[i-1].value))) {
		   		startYear = previousDate.getFullYear();
		   		startDay = previousDate.getDate();
		   		startMonth = previousDate.getMonth()+1;
		   		
				endYear = tempDate.getFullYear();
		   		endDay = tempDate.getDate();
		   		endMonth = tempDate.getMonth()+1;
		   		
		   		if(checkValid(startMonth,startDay,startYear) && checkValid(endMonth,endDay,endYear)) {
					if((startMonth >= endMonth && startDay > endDay && startYear >= endYear) || 
					   (startMonth > endMonth && startYear >= endYear) ||
					    startYear > endYear) { //end day is before start day
						alert("Return date can not be before departure date");
						compareArray[i].value = "";
					}
				} else {
					break;
				}
			} else {
				break;
			}
		}
	}
}

//check for dashes
//@param string field to check
function checkFormat(field) {
	if(field.value.indexOf("-")!=-1) {
		var replaceDashes = /-/g;
		field.value = field.value.replace(replaceDashes, "/");
	}
}

//Move calendar forward one month
function moveMonthForward() {
	moveCalendarMonth(1);
}
//Move calendar backward one month
function moveMonthBackward() {
	moveCalendarMonth(-1);
}
//Move calendar by increment
//@param int negative=back, positive=forward
function moveCalendarMonth(moveIncrement) {
	month = parseInt(getSelectedMonth());
	//day = parseInt(getSelectedDay()); THIS LINE IS A PROBLEM, The Next LINE IS THE FIX
	day = parseInt(1);//09-05-2008 - instead of getSelectedDay(), i used this to solve the problem where, for example the date is jan 31, and u click next month, it passes the current date to this function, and starts building the next month off of the current date, but feb oponly has 28 days, and the prebuilt javascript funtion automatically makes it march, thus making all the months with fewer then 31 days have the wrong title
	year = parseInt(getSelectedYear());
		
	//if current month is december then move forward one year
	var tempYearIndex = parseInt(window.frames["cal_frame"].document.forms["calendarForm"].yearDrop.selectedIndex);
	if((month + moveIncrement) > 12) {
		var moveToMonth = 1;
		var moveToYear = year + 1;
		var tempYearIndex = tempYearIndex + 1;
	} else if((month + moveIncrement) < 1) {
		var moveToMonth = 12;
		var moveToYear = year - 1;
		var tempYearIndex = tempYearIndex - 1;
	} else {
		var moveToMonth = month + moveIncrement;
		//alert(moveToMonth);
		var moveToYear = year;
	}
	
	//check if date is out of range of calendar
	var numOptions = window.frames["cal_frame"].document.forms["calendarForm"].yearDrop.options.length;
	var lowOption = parseInt(window.frames["cal_frame"].document.forms["calendarForm"].yearDrop.options[0].value);
	var highOption = parseInt(window.frames["cal_frame"].document.forms["calendarForm"].yearDrop.options[numOptions-1].value);
	if(moveToYear < lowOption || moveToYear > highOption) {
		alert("Sorry. That date is out of the range currently available for reservations.");
		return;
	}
	
	//check if day is out of range for month
	if(!checkValid(moveToMonth,day,moveToYear)) {
		
	}
	
	calendar(moveToMonth,day,moveToYear,1);
	if(secondMonth) 
	{
		if(moveToMonth+1 == 13) 
		{
			calendar(1,1,moveToYear+1,2);
		} 
		else {
			calendar(moveToMonth+1,1,moveToYear,2);
		}
	}
	
	//update select boxes first before calling calendar
	window.frames["cal_frame"].document.forms["calendarForm"].monthDrop.selectedIndex = moveToMonth - 1;
	window.frames["cal_frame"].document.forms["calendarForm"].yearDrop.selectedIndex = tempYearIndex;
	
	positionWindow(inAction);
}

//Positioning 
//Taken from http://www.quirksmode.org/about/copyright.html
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

//position the window according to the inputBox and its position in the window
//@param object the input box you are working on
function positionWindow(inputBox) {

	xPos = findPosX(inputBox);
	yPos = findPosY(inputBox) + 22;
	
	if (window.innerWidth) {
        windowWidth = window.innerWidth + window.pageXOffset - 20;
    } else {
        windowWidth = document.body.offsetWidth + document.body.scrollLeft - 20;
	}

	//modified code from quirksmode - resizes the iframe in case its too small for its contents - modified by jason
	if(document.getElementById('cal_frame').contentWindow) {
		var test1 = document.getElementById('cal_frame').contentWindow.document.body.scrollHeight;
		var test2 = document.getElementById('cal_frame').contentWindow.document.body.offsetHeight;
		if (test1 > test2) {// all but Explorer Mac
			document.getElementById('cal_frame').style.height = "" + document.getElementById('cal_frame').contentWindow.document.body.scrollHeight + "px";
			document.getElementById('cal_frame').style.width = "" + document.getElementById('cal_frame').contentWindow.document.body.scrollWidth + "px";
			if(windowWidth < (parseInt(document.getElementById('cal_frame').style.width)+xPos)) {
				var moveLeft = ((parseInt(document.getElementById('cal_frame').style.width)+xPos)-windowWidth);
				if(!isNaN(moveLeft) && (moveLeft>0)) {
					xPos -= moveLeft;
				}
			}
		} else { // Explorer Mac;
			document.getElementById('cal_frame').height = "" + document.getElementById('cal_frame').contentWindow.document.body.offsetHeight + "px";
			document.getElementById('cal_frame').width = "" + document.getElementById('cal_frame').contentWindow.document.body.offsetWidth + "px";
			if(windowWidth < (parseInt(document.getElementById('cal_frame').width)+xPos)) {
				var moveLeft = ((parseInt(document.getElementById('cal_frame').width)+xPos)-windowWidth);
				if(!isNaN(moveLeft) && (moveLeft>0)) {
					xPos -= moveLeft;
				}
			}
		}
	}

	document.getElementById("cal_frame").style.left = "" + xPos + "px";
	document.getElementById("cal_frame").style.top = "" + yPos + "px";
}

//Creates the iframe instance
function createCalenderHTML() {
	if(document.body.appendChild) {
		var spana = document.createElement("span");
		spana.innerHTML = "<iframe id='cal_frame' name='cal_frame' src='" + iFrameSource + "'></iframe>";
		document.body.appendChild(spana);
		if(khtml || netscape) {
			document.getElementById("cal_frame").style.visibility = "hidden";
		} else {
			document.getElementById("cal_frame").style.display = "none";
		}
	}
}
//Switch on and off the iframe (for external scripts) (mac only)
function calFrameOn() {
	if(khtml || netscape) {
		document.getElementById("cal_frame").style.visibility = "visible";
	}
}
function calFrameOff() {
	if(khtml || netscape) {
		document.getElementById("cal_frame").style.visibility = "hidden";
	}
}
