function isEmpty(s)
{ 
	return ((s == null) || (s.length == 0)) 
}

function ClearOut(field)
{
	if ((field.value == " Where to?") || (field.value == " Where from?"))
	{
		field.value = '';
	}
} 

function checkInput(f)
{
	var errorflag = 0;
	var errormsg;
	var Xerrorflag = 0;
	var Xerrormsg;
	
	errormsg = "Please enter the following information before submitting your form:\n\n";
	Xerrormsg = "The form you submitted contains the following errors:\n\n";
	
	if ((isEmpty(f.departCity.value)) || (f.departCity.value == " Where from?"))
	{
		errorflag = 1;
		errormsg += "-- Departure City (where you're leaving from)\n";
	}
	
	if ((isEmpty(f.returnCity.value)) || (f.returnCity.value == " Where to?"))
	{
		errorflag = 1;
		errormsg += "-- Arrival City (where you're going to)\n";
	}
	
	if (errorflag == 1)
	{
		errormsg += "\nThank you for using US1Travel.com.\n";
		alert(errormsg);
		return false;
	}
	
	if (!(isDate(f.depDay.selectedIndex+1, f.depMonth.selectedIndex+1,f.depYear.value)))
	{
		Xerrorflag = 1;
		Xerrormsg += "-- Your departure date is invalid\n";
	}
	
	if (daysElapsed(f.depDay.selectedIndex+1, f.depMonth.selectedIndex+1, f.depYear.value,f.retDay.selectedIndex+1, f.retMonth.selectedIndex+1, f.retYear.value) < 0)
	{
		Xerrorflag = 0;
		Xerrormsg += "-- Your departure date is after your return date (please switch the two)\n";
	}
	
	if (!(isDate(f.retDay.selectedIndex+1, f.retMonth.selectedIndex+1,f.retYear.value)))
	{
		Xerrorflag = 1;
		Xerrormsg += "-- Your return date is invalid\n";
	}
	
	if (Xerrorflag == 1)
	{
		Xerrormsg += "\nPlease correct these errors and resubmit the form.\nThank you for using US1Travel.com.\n";
		alert(Xerrormsg);
		return false;
	}
	
	// Construct query string from the inputted values and launch a new window
	var QueryString;
	QueryString =  "&direction=" + f.direction.options[f.direction.selectedIndex].value + "&";
	QueryString += "classService=" + f.classService.options[f.classService.selectedIndex].value + "&";
	QueryString += "departCity=" + f.departCity.value + "&";
	QueryString += "depMonth=" + f.depMonth.options[f.depMonth.selectedIndex].value + "&";
	QueryString += "depDay=" + f.depDay.options[f.depDay.selectedIndex].value + "&";
	QueryString += "depTime=" + f.depTime.options[f.depTime.selectedIndex].value + "&";
	QueryString += "returnCity=" + f.returnCity.value + "&";
	QueryString += "retMonth=" + f.retMonth.options[f.retMonth.selectedIndex].value + "&";
	QueryString += "retDay=" + f.retDay.options[f.retDay.selectedIndex].value + "&";
	QueryString += "retTime=" + f.retTime.options[f.retTime.selectedIndex].value + "&";
	QueryString += "ADT=" + f.ADT.options[f.ADT.selectedIndex].value + "&";
	QueryString += "CHD=" + f.CHD.options[f.CHD.selectedIndex].value + "&";
	QueryString += "INF=" + f.INF.options[f.INF.selectedIndex].value + "&";
	QueryString += "SCR=" + f.SCR.options[f.SCR.selectedIndex].value + "&";
	QueryString += "flightType=" + f.flightType.options[f.flightType.selectedIndex].value + "&";
	QueryString += "actionType=" + f.actionType.options[f.actionType.selectedIndex].value;
	
	var bookURL = "Https://ww3.dotres.com/meridia?posid=2BH4&page=requestAirMessage_air&action=airRequest&currency=USD&" + QueryString;
	var bookWin = window.open(bookURL, "Book", "status=yes,toolbar=yes,location=yes,resizable=yes,scrollbars=yes");
	bookWin.opener = self;
	bookWin.focus();
}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function isDate(day, month, year) {
// checks if date passed is valid
// will accept dates in following format:
// isDate(dd,mm,ccyy), or
// isDate(dd,mm) - which defaults to the current year, or
// isDate(dd) - which defaults to the current month and year.
// Note, if passed the month must be between 1 and 12, and the
// year in ccyy format.

    var today = new Date();
    year = ((!year) ? y2k(today.getYear()):year);
    month = ((!month) ? today.getMonth():month-1);
    if (!day) return false
    var test = new Date(year,month,day);
    if ( (y2k(test.getYear()) == year) &&
         (month == test.getMonth()) &&
         (day == test.getDate()) )
        return true;
    else
        return false
}

function daysElapsed(dd1,mm1,yy1,dd2,mm2,yy2) {
	date1 = new Date(yy1,mm1,dd1);
	date2 = new Date(yy2,mm2,dd2);
    var difference =
        Date.UTC(y2k(date2.getYear()),date2.getMonth(),date2.getDate(),0,0,0)
      - Date.UTC(y2k(date1.getYear()),date1.getMonth(),date1.getDate(),0,0,0);
    return difference/1000/60/60/24;
}
