// JavaScript Document
function validateMe(){

if (Trim(document.contact.FirstName.value)==""){
	alert("First name must be provided.")
	document.contact.FirstName.value="";
	document.contact.FirstName.focus();
	return false;
}
else {
	document.contact.FirstName.value = Trim(document.contact.FirstName.value);
}

if (Trim(document.contact.LastName.value)==""){
	alert("Last name must be provided.")
	document.contact.LastName.value="";
	document.contact.LastName.focus();
	return false;
}
else {
	document.contact.LastName.value = Trim(document.contact.LastName.value);
}

if (Trim(document.contact.Phone.value)==""){
	alert("Telephone number must be provided.")
	document.contact.Phone.value = "";
	document.contact.Phone.focus();
	return false;
}
else {
	document.contact.Phone.value = Trim(document.contact.Phone.value);
}

// email
if ((document.contact.email.value==null)||(document.contact.email.value=="")){
		alert("Email address must be provided.")
		document.contact.email.focus()
		return false;
	}
if (checkMe(document.contact.email.value)==false){
		document.contact.email.focus()
		return false;
	}
	

if (document.contact.Passengers.value ==""){
	alert("Number of passengers must be provided.")
	document.contact.Passengers.focus();
	return false;
}

if (!isNaN(document.contact.Passengers.value)){
	var passNum = parseInt(document.contact.Passengers.value);
		if (passNum <= 0){
			alert("Number of passengers must be greater than 0.")
			document.contact.Passengers.focus();
			return false;
		}
	}
else {
	alert("Number of passengers must be a number.")
	document.contact.Passengers.focus();
	return false;
}

// round trip one way
if (document.contact.Legs.options[document.contact.Legs.selectedIndex].value ==""){
	alert("Round Trip, One Way or Multi Leg must be selected.")
	document.contact.Legs.focus();
	return false;
}

if (Trim(document.contact.DepartureCity.value)==""){
	alert("Departure City must be provided.")
	document.contact.DepartureCity.value="";
	document.contact.DepartureCity.focus();
	return false;
}
else {
	document.contact.DepartureCity.value = Trim(document.contact.DepartureCity.value);
}

if (Trim(document.contact.DepartureState.value)==""){
	alert("Departure State must be provided.")
	document.contact.DepartureState.value="";
	document.contact.DepartureState.focus();
	return false;
}
else {
	document.contact.DepartureState.value = Trim(document.contact.DepartureState.value);
}

if (Trim(document.contact.DestinationCity.value)==""){
	alert("Destination City must be provided.")
	document.contact.DestinationCity.value = "";
	document.contact.DestinationCity.focus();
	return false;
}
else {
	document.contact.DestinationCity.value = Trim(document.contact.DestinationCity.value);
}

if (Trim(document.contact.DestinationState.value)==""){
	alert("Destination State must be provided.")
	document.contact.DestinationState.value="";
	document.contact.DestinationState.focus();
	return false;
}
else {
	document.contact.DestinationState.value = Trim(document.contact.DestinationState.value);
}

if (document.contact.DeptDate.value ==""){
	alert("Departure Date must be provided.")
	document.contact.DeptDate.focus();
	return false;
}
else {
var check = dateCheckCurrent(document.contact.DeptDate.value);
	if (check == false){
		document.contact.DeptDate.focus();
		return false;
	}
}
//DepartureTime
if (document.contact.DepartureTime.options[document.contact.DepartureTime.selectedIndex].value ==""){
	alert("Pick a departure Time.")
	document.contact.DepartureTime.focus();
	return false;
}

// if a round trip has been selected
/*if (document.contact.Legs.options[document.contact.Legs.selectedIndex].value =="Round Trip"){

	if (document.contact.RetDate.value ==""){
		alert("Return Date must be provided.")
		document.contact.RetDate.focus();
		return false;
	}
	// Return Time
	if (document.contact.ReturnTime.options[document.contact.ReturnTime.selectedIndex].value ==""){
		alert("Pick a return Time.")
		document.contact.ReturnTime.focus();
		return false;
	}
}
*/

/*if (document.contact.Legs.options[document.contact.Legs.selectedIndex].value =="Multi Leg Trip") {
		var check = dateCheckCurrent(document.contact.DeptDate.value);
			if (check == false){
				document.contact.DeptDate.focus();
				return false;
			}
	}
*/

	

//}


// aircraft select
if (document.contact.AircraftPreference.options[document.contact.AircraftPreference.selectedIndex].value ==""){
	alert("Aircraft preference must be selected.")
	document.contact.AircraftPreference.focus();
	return false;
}


// Charter Frequency
if (document.contact.CharterFrequency.options[document.contact.CharterFrequency.selectedIndex].value ==""){
	alert("Select a charter fight frequency.")
	document.contact.CharterFrequency.focus();
	return false;
}

if (document.contact.Legs.options[document.contact.Legs.selectedIndex].value =="Round Trip"){
	if (document.contact.RetDate.value ==""){
		alert("Return Date must be provided.")
		document.contact.RetDate.focus();
		return false;
	}
	// Return Time
	if (document.contact.ReturnTime.options[document.contact.ReturnTime.selectedIndex].value ==""){
		alert("Pick a return Time.")
		document.contact.ReturnTime.focus();
		return false;
	}
	var OK = checkTravelDates();
	if(OK==false){
		alert("Please check your return date.");
		document.contact.RetDate.focus();
		return false;
	}
}

if (document.contact.Legs.options[document.contact.Legs.selectedIndex].value =="Multi Leg Trip"){
var retValue = true;
//alert(document.contact.Legs.options[document.contact.Legs.selectedIndex].value);
	if(document.getElementById("leg1").style.display == "block"){
	//alert("In 1");
	//alert("depdate1");
		//var OK = checkTravelDatesAdditional_1('deptdate','deptdate1');
		var OK = checkTravelDatesAdditional_1(1);
		if(OK==false){
			alert("Please check departure dates.");
			document.contact.DeptDate1.focus();
			retValue = false;
		}
	}
	if(document.getElementById("leg2").style.display == "block"){
	//alert("In 2");
		var OK = checkTravelDatesAdditional_1(2);
		//var OK = checkTravelDatesAdditional_1('deptdate1','deptdate2');
		if(OK==false){
			alert("Please check departure dates.");
			document.contact.DeptDate2.focus();
			retValue = false;
		}
	}
	if(document.getElementById("leg3").style.display == "block"){
	//alert("In 3");
		//var OK = checkTravelDatesAdditional_1('deptdate2','deptdate3');
		var OK = checkTravelDatesAdditional_1(3);
		if(OK==false){
			alert("Please check departure dates.");
			document.contact.DeptDate3.focus();
			retValue = false;
		}
	}
	if(document.getElementById("leg4").style.display == "block"){
	//alert("In 4");
		//var OK = checkTravelDatesAdditional_1('deptdate3','deptdate4');
		var OK = checkTravelDatesAdditional_1(4);
		if(OK==false){
			alert("Please check departure dates.");
			document.contact.DeptDate4.focus();
			retValue = false;
		}
	}
	return retValue;
}

/*if ((document.contact.comments==null)||(document.contact.comments.value=="")){
alert("Please submit a question or comment.")
document.contact.comments.focus();
return false;
} */
}

function checkTravelDates() {
	
	var dateDep_array = document.contact.DeptDate.value.split("/");
	var dateRet_array = document.contact.RetDate.value.split("/");

	var monthDep = dateDep_array[0];
	var dayDep = dateDep_array[1];
	var yearDep = dateDep_array[2];

	var monthRet = dateRet_array[0];
	var dayRet = dateRet_array[1];
	var yearRet = dateRet_array[2];

	var dateDep = yearDep.toString()+monthDep.toString()+dayDep.toString();
	var dateRet = yearRet.toString()+monthRet.toString()+dayRet.toString();
	
	//alert(parseInt(dateRet) + " > " + parseInt(dateDep))
	
	if (parseInt(dateRet) >= parseInt(dateDep)){
		return true;
		}
	else {
		return false;
		}
}
function checkTravelDatesLegs(date1,date2) {
	
	var dateDep_array = date1.split("/");
	var dateRet_array = date2.split("/");

	var monthDep = dateDep_array[0];
	var dayDep = dateDep_array[1];
	var yearDep = dateDep_array[2];

	var monthRet = dateRet_array[0];
	var dayRet = dateRet_array[1];
	var yearRet = dateRet_array[2];

	var dateDep = yearDep.toString()+monthDep.toString()+dayDep.toString();
	var dateRet = yearRet.toString()+monthRet.toString()+dayRet.toString();
	
	//alert(parseInt(dateRet) + " > " + parseInt(dateDep))
	
	if (parseInt(dateRet) >= parseInt(dateDep)){
		return true;
		}
	else {
		return false;
		}
}

function checkTravelDatesAdditional_1(numLeg) {
//alert(numLeg);
 var checkDate;
var legNum = numLeg;
	if (legNum == 1){
	//dateCheckCurrent(document.getElementById('DeptDate').value)
	var check = dateCheckCurrent(document.getElementById('DeptDate1').value);
	if (check == false){
		document.getElementById('DeptDate1').focus();
		return false;
	}
	if((checkDate = checkTravelDatesLegs(document.getElementById('DeptDate').value,document.getElementById('DeptDate1').value))==false){
		alert("Second leg date can not be earlier than departure date")
		document.getElementById('DeptDate1').focus();
		return false;
	}
	//Dept1 = document.getElementById('DeptDate').value;
	//Dept2 = document.getElementById('DeptDate1').value;
	}
	if (legNum == 2){
	var check = dateCheckCurrent(document.getElementById('DeptDate2').value);
	if (check == false){
		document.getElementById('DeptDate2').focus();
		return false;
	}
	if((checkDate = checkTravelDatesLegs(document.getElementById('DeptDate1').value,document.getElementById('DeptDate2').value))==false){
		alert("Third leg date can not be earlier than second leg departure date")
		document.getElementById('DeptDate2').focus();
		return false;
	}
	//Dept1 = document.getElementById('DeptDate1').value;
	//Dept2 = document.getElementById('DeptDate2').value;
	}
	if (legNum == 3){
	var check = dateCheckCurrent(document.getElementById('DeptDate3').value);
	if (check == false){
		document.getElementById('DeptDate3').focus();
		return false;
	}
	if((checkDate = checkTravelDatesLegs(document.getElementById('DeptDate2').value,document.getElementById('DeptDate3').value))==false){
		alert("Fourth leg date can not be earlier than third leg departure date")
		document.getElementById('DeptDate3').focus();
		return false;
	}
	//Dept1 = document.getElementById('DeptDate2').value;
	//Dept2 = document.getElementById('DeptDate3').value;
	}
	if (legNum == 4){
	var check = dateCheckCurrent(document.getElementById('DeptDate4').value);
	if (check == false){
		document.getElementById('DeptDate4').focus();
		return false;
	}
	if((checkDate = checkTravelDatesLegs(document.getElementById('DeptDate3').value,document.getElementById('DeptDate4').value))==false){
		alert("Fifth leg date can not be earlier than fourth leg departure date")
		document.getElementById('DeptDate4').focus();
		return false;
	}
	//Dept1 = document.getElementById('DeptDate3').value;
	//Dept2 = document.getElementById('DeptDate4').value;
	}
	/*alert(Dept1);
	alert(Dept2);
	var dateDep_array = Dept1.split("/");
	var dateRet_array = Dept2.split("/");

	var monthDep = dateDep_array[0];
	var dayDep = dateDep_array[1];
	var yearDep = dateDep_array[2];

	var monthRet = dateRet_array[0];
	var dayRet = dateRet_array[1];
	var yearRet = dateRet_array[2];

	var dateDep = yearDep.toString()+monthDep.toString()+dayDep.toString();
	var dateRet = yearRet.toString()+monthRet.toString()+dayRet.toString();
	
	alert(parseInt(dateRet) + " > " + parseInt(dateDep))
	
	if (parseInt(dateRet) >= parseInt(dateDep)){
		return true;
		}
	else {
		return false;
		}*/
		//return true;
}

function checkLeg(leg,rd,rt){
var le = document.getElementById("Legs");
var lrd = document.getElementById("RetDate");
var lrdbtn = document.getElementById("RetDateBtn");
var lrt = document.getElementById("ReturnTime");

if (le.options[le.selectedIndex].value =="One Way"){
	lrd.value = "";
	lrt.value = "";
	lrd.disabled = true;
	lrt.disabled = true;
	lrdbtn.disabled = true;
	document.getElementById("retDateDiv").style.display="none";
	document.getElementById("leg1A").style.display="none";
	if(document.getElementById("leg1")){
		document.getElementById("Passengers1").value="";
		document.getElementById("DestinationCity1").value="";
		document.getElementById("DestinationState1").value="";
		document.getElementById("DeptDate1").value="";
		document.getElementById("DepartureTime1").value="";
		document.getElementById("leg1").style.display="none";
		
	}
	if(document.getElementById("leg2")){
		document.getElementById("Passengers2").value="";
		document.getElementById("DestinationCity2").value="";
		document.getElementById("DestinationState2").value="";
		document.getElementById("DeptDate2").value="";
		document.getElementById("DepartureTime2").value="";
		document.getElementById("leg2").style.display="none";
	}
	if(document.getElementById("leg3")){
		document.getElementById("Passengers3").value="";
		document.getElementById("DestinationCity3").value="";
		document.getElementById("DestinationState3").value="";
		document.getElementById("DeptDate3").value="";
		document.getElementById("DepartureTime3").value="";
		document.getElementById("leg3").style.display="none";
	}
	if(document.getElementById("leg4")){
		document.getElementById("Passengers4").value="";
		document.getElementById("DestinationCity4").value="";
		document.getElementById("DestinationState4").value="";
		document.getElementById("DeptDate4").value="";
		document.getElementById("DepartureTime4").value="";
		document.getElementById("leg4").style.display="none";
	}
}
if (le.options[le.selectedIndex].value =="Round Trip") {
	lrd.disabled = false;
	lrt.disabled = false;
	lrdbtn.disabled = false;
	document.getElementById("retDateDiv").style.display="block";
	dp_calRetDate  = new Epoch('epoch_popup','popup',document.getElementById('RetDate'));
	document.getElementById("leg1A").style.display="none";
	if(document.getElementById("leg1")){
		document.getElementById("Passengers1").value="";
		document.getElementById("DestinationCity1").value="";
		document.getElementById("DestinationState1").value="";
		document.getElementById("DeptDate1").value="";
		document.getElementById("DepartureTime1").value="";
		document.getElementById("leg1").style.display="none";
		
	}
	if(document.getElementById("leg2")){
		document.getElementById("Passengers2").value="";
		document.getElementById("DestinationCity2").value="";
		document.getElementById("DestinationState2").value="";
		document.getElementById("DeptDate2").value="";
		document.getElementById("DepartureTime2").value="";
		document.getElementById("leg2").style.display="none";
	}
	if(document.getElementById("leg3")){
		document.getElementById("Passengers3").value="";
		document.getElementById("DestinationCity3").value="";
		document.getElementById("DestinationState3").value="";
		document.getElementById("DeptDate3").value="";
		document.getElementById("DepartureTime3").value="";
		document.getElementById("leg3").style.display="none";
	}
	if(document.getElementById("leg4")){
		document.getElementById("Passengers4").value="";
		document.getElementById("DestinationCity4").value="";
		document.getElementById("DestinationState4").value="";
		document.getElementById("DeptDate4").value="";
		document.getElementById("DepartureTime4").value="";
		document.getElementById("leg4").style.display="none";
	}
}
if (le.options[le.selectedIndex].value =="Multi Leg Trip") {
	lrd.disabled = true;
	lrt.disabled = true;
	lrdbtn.disabled = true;
	document.getElementById("retDateDiv").style.display="none";
	document.getElementById("leg1A").style.display="block";
	if(document.getElementById("leg1")){
		document.getElementById("leg1").style.display="none";
	}
	if(document.getElementById("leg2")){
		document.getElementById("leg2").style.display="none";
	}
	if(document.getElementById("leg3")){
		document.getElementById("leg3").style.display="none";
	}
	if(document.getElementById("leg4")){
		document.getElementById("leg4").style.display="none";
	}
}
}

function checkMe(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail Address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail Address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail Address")

		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail Address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail Address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail Address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail Address")
		    return false
		 }

 		 return true					
	}
	
function dateCheckCurrent(string){
//alert("in dateCheckCurrent " +string);
var date = new Date();
currentYear = date.getFullYear();
currentMonth = date.getMonth()+1;
currentDay = date.getDate();
//alert(currentMonth+"/"+currentDay+"/"+currentYear);

var date_array = string.split("/");

//alert("From Array: "+date_array[0]+"/"+date_array[1]+"/"+date_array[2]);

var dateString = string;
var month = parseInt(date_array[0]);
var div = "/";
var day = parseInt(date_array[1]);
var year = parseInt(date_array[2]);
var cmon = false;
var cday = false;
var cyear = false;
// check year
if (!isNaN(year)){
	year = parseInt(year);
//	alert(currentYear);
	if (year>=currentYear) {
	//	alert(year);
		cyear = true;
		}
	else {
		//alert(year);
		cyear = false;
	}
	}
else {
		//alert(year);
		cyear = false;
	}
// check month
if (!isNaN(month)){
	//month = parseInt(month)
	if ((month>0)&&(month<13)) {
		//alert(month);
		if (year == currentYear){
			if (month >= currentMonth){
				cmon = true;
				}
			else {
				cmon = false;
				}
			}
		else {
			cmon = true;
			}
			}
		else {
			cmon = false;
		}
		}
	else {
		//alert(month);
		cmon = false;
	}
// check day
if (!isNaN(day)){
	//day = parseInt(day)
	if ((day>0)&&(day<32)) {
		//alert(day);
		if (month == currentMonth){
			if (day >= currentDay){
				cday = true;
				}
			else {
				cday = false;
				}
			}
		else {
			cday = true;
			}
		}
	else {
		
		cday = false;
	}
	}
else {
		
		cday = false;
	}

if ((cmon)&&(cday)&&(cyear)){
	return true;
}
else {
	//alert("There is a problem with the date format, or the date is in the past.");
	return true;
} 
}

function Trim(TRIM_VALUE){
	
	if(TRIM_VALUE.length < 1){
		return "";
	}

	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);

	if(TRIM_VALUE==""){
		return "";
	}
	else{
		return TRIM_VALUE;
	}
} //End Function


function RTrim(VALUE){

	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	
	if(v_length < 0){
		return"";
	}
	var iTemp = v_length -1;

	while(iTemp > -1){
		if(VALUE.charAt(iTemp) == w_space){
		}
		else{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}

		iTemp = iTemp-1;

	} //End While

	return strTemp;

} //End Function


function LTrim(VALUE){

	var w_space = String.fromCharCode(32);

	if(v_length < 1){
		return"";
	}

	var v_length = VALUE.length;
	var strTemp = "";
	var iTemp = 0;

	while(iTemp < v_length){
		if(VALUE.charAt(iTemp) == w_space){
		}
		else{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}

		iTemp = iTemp + 1;

	} //End While
	return strTemp;

} //End Function