var dDT = new Date();
var wlt = dDT.getTime();
var wdt = '1800';


function handleAirWinOnload(evt) {
  
	defPopEvts();
  
     if (document.all['strAirItin_CitySearchCopy'] == null) {
	   airTripSelectionDefault();
	 } 
  
}


function gCFD(sDate, sOff) {
var dDate = new Date(sDate);
 if(isNaN(dDate)) {
 dDate = new Date();
 }
var sOff = String(sOff);
var aO = sOff.split("/");
var iMO = parseInt(aO[0]);
var iDO = parseInt(aO[1]);
var iYO = parseInt(aO[2]);
var dNew = new Date(dDate);
dNew.setDate(dNew.getDate() + iDO);
dNew.setMonth(dNew.getMonth() + iMO);

if (dNew.getYear() < 100)
{
dNew.setYear(dNew.getYear() + 2000 + iYO);
}
else
{
dNew.setYear(dNew.getFullYear() + iYO);
}

var sFD = String(dNew.getMonth() + 1) + "/" + String(dNew.getDate()) + "/" + String(dNew.getFullYear());
return(sFD);
}

var gIntMonth = 0;
var gIntDate = 0;
var gIntYear = 0;
var gSourceField = null;
var gSourceFieldValue = "";

function updateDependentDate(oBox2,sDate1,sOffset) {
var d1 = new Date(sDate1);
var d2 = new Date(oBox2.value);
 if(isNaN(d1)) {
 return;
 }
var sNew = gCFD(sDate1, sOffset);
 if(isNaN(d2)) {
 oBox2.value = sNew;
 return;
 }
 if(d1 >= d2) {
 oBox2.value = sNew;
 return;
 }
}

function cleanDigits(strDigits) {
var strResult = "0";
 for(var i = 0; i < strDigits.length; i++){
 var strC = strDigits.charAt(i);
  if(isDigit(strC)) {
  strResult += "" + strC;
  }
 }
var iResult = parseInt(strResult,10);
return(iResult);
}

function isDigit(ch) {
return(ch >= '0' && ch <= '9');
}

function GetDaysInMonth(intMonth, intYear) {
var arrDaysInMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var intCount = arrDaysInMonth[intMonth-1];
 if(intMonth == 2) {
  if((intYear%4) == 0 ) {	// post-2000 leap year - ignore 2100 etc.
  intCount++;
  }
 }
return intCount;
}

function IsValidDate(intDate, intMonth, intYear) {
 if( (intYear > 2050) || (intYear < 1900) ) {return false;}
 if( (intMonth > 12) || (intMonth < 1) ) {return false;}
 if(intDate < 1) {return false;}
var intMaxDate = GetDaysInMonth(intMonth, intYear);
 if(intDate > intMaxDate) {return false;}
return true;
}

function GetInputDate(strDate){
var lenDateString = strDate.length;
if(lenDateString == 0) {return(false);}

arrDate = strDate.split("/");
if(arrDate.length < 3) {return(false);}

var intMonth = cleanDigits(arrDate[0]);
var intDate = cleanDigits(arrDate[1]);
var intYear = cleanDigits(arrDate[2]);
if (intYear.length < 4){
	intYear = parseint("20" + string(intYear))
	}  


if(IsValidDate(intDate, intMonth, intYear)) {
 gIntMonth = intMonth;
 gIntDate = intDate;
 gIntYear = intYear;
}else{
 var dToday = new Date();
 gIntMonth = dToday.getMonth() + 1;
 gIntDate = dToday.getDate();
 gIntYear = dToday.getFullYear();
 }
return(true);
}

function ShowCalendar(launchButton,dateInputField,datePrevField,strEarliestDate,strLatestDate,strEarliestOffset,strLatestOffset){
var calDocumentAll=document.all.CalFrame;
var oCalFrame = window.frames.CalFrame;

//alert("test 1");
if(oCalFrame.gblnCalendarLoaded == null || oCalFrame.gblnCalendarLoaded == false){
 alert("Calendar:" + oCalFrame.gblnCalendarLoaded + "\r\nUnable to load popup calendar.\r\nPlease reload the page.");
return;
}

var strStartRange = strEarliestDate;
var strEndRange = strLatestDate;

if(datePrevField != null) {
 if(String(dateInputField.value) != "") {
 var sDate = String(datePrevField.value);
  if(strEarliestOffset != "") {
  strStartRange = gCFD(sDate, strEarliestOffset);
    }
  if(strLatestOffset != "") {
  strEndRange = gCFD(sDate, strLatestOffset);
  }
 }
}
oCalFrame.SetDateRange(new Date(strStartRange),new Date(strEndRange));

if(dateInputField == gSourceField && calDocumentAll.style.display == "block"){
 if(gSourceFieldValue != dateInputField.value && GetInputDate(dateInputField.value)){
 oCalFrame.SetInputDate(gIntDate, gIntMonth, gIntYear);
 oCalFrame.SetDate(gIntDate, gIntMonth, gIntYear);
 gSourceFieldValue = dateInputField.value;
 }
 else
 calDocumentAll.style.display = "none";
}else{
 if(GetInputDate(dateInputField.value)){
 dateInputField.value = String(gIntMonth) + "/" + String(gIntDate) + "/" + String(gIntYear);
 oCalFrame.SetInputDate(gIntDate, gIntMonth, gIntYear);
 oCalFrame.SetDate(gIntDate, gIntMonth, gIntYear);
 } else if(null != datePrevField && GetInputDate(datePrevField.value)){
 oCalFrame.SetInputDate(gIntDate, gIntMonth, gIntYear);
 oCalFrame.SetDate(gIntDate, gIntMonth, gIntYear);
 }else{
 var dDate = new Date(strEarliestDate);
 oCalFrame.SetInputDate(-1, -1, -1);
 oCalFrame.SetDate(dDate.getDate(), dDate.getMonth()+1, dDate.getFullYear());
}

var intCalLeft = 0;
var intCalTop = 0;

 for(var p = launchButton; p && p.tagName!='BODY'; p = p.offsetParent){
 intCalLeft += p.offsetLeft;
 intCalTop += p.offsetTop;
 }

var intLaunchButtonHeight = launchButton.offsetHeight;
var intDocHeight = calDocumentAll.style.pixelHeight;
var intScrollTop = document.body.scrollTop;
calDocumentAll.style.left = intCalLeft;

 if( (intCalTop - intDocHeight >= intScrollTop) && (intCalTop + intLaunchButtonHeight + intDocHeight > document.body.clientHeight + intScrollTop) )
 calDocumentAll.style.top = intCalTop - intDocHeight;
 else
 calDocumentAll.style.top = intCalTop+intLaunchButtonHeight;
  if(calDocumentAll.style.display == "none")
  calDocumentAll.style.display = "block";
  gSourceField = dateInputField;
  gSourceFieldValue = dateInputField.value;
 }
}

function SetDate(intDate, intMonth, intYear){
gSourceField.focus();
gSourceField.value = (intMonth + "/" + intDate + "/" + String(intYear));
gSourceFieldValue = gSourceField.value;
}

var lsUseDHTML = 'Y';
var lsAirTripType = 'RT';
var lsASO = 'N';

function airTripSelectionDefault() {
 
 airTripSelection(lsAirTripType);
 
}
function airTripSelection(sTrip) {
  if (sTrip=="OW") {
	if (document.all['divAirItinOneWay'] != null) {
		document.all['divAirItinRoundTrip'].style.display='none';
		document.all['divAirItinMultiCity'].style.display='none';
		document.all['divAirItinOneWay'].style.display='block';
	}
	if (document.all['spanLinkOneWay'] != null) {
		document.all.spanLinkOneWay.innerHTML = '<b style="color:#FFFFFF">One-Way</b>';
		document.all.spanLinkRoundTrip.innerHTML = "<a href=\"JavaScript:airTripSelection('RT')\" class='airfare-link'>Round-Trip</a>";
		document.all.spanLinkMultiCity.innerHTML = "<a href=\"JavaScript:airTripSelection('MD')\" class='airfare-link'>Multi-Destination</a>";
	}
	if (document.all['AirTripType'] != null) {
		document.all.AirTripType(1).checked = true;
	}
	if (document.all['divAirItinNavOW'] != null) {
		document.all['divAirItinNavRT'].style.display='none';
		document.all['divAirItinNavMD'].style.display='none';
		document.all['divAirItinNavOW'].style.display='block';
	}
	if (document.all['divAirItinNyop'] != null) {
		document.all['divAirItinNyop'].style.display='none';
	}
	if (document.all['divAirItinTC1'] != null) {
		document.all['divAirItinTC2'].style.display='block';
		document.all['divAirItinTC1'].style.display='none';
	}
  } else if (sTrip=="MD") {
	if (document.all['divAirItinMultiCity'] != null) {
		document.all['divAirItinRoundTrip'].style.display='none';
		document.all['divAirItinOneWay'].style.display='none';
		document.all['divAirItinMultiCity'].style.display='block';
	}
	if (document.all['spanLinkOneWay'] != null) {
		document.all.spanLinkMultiCity.innerHTML = '<b style="color:#FFFFFF">Multi-Destination</b>';
		document.all.spanLinkOneWay.innerHTML = "<a href=\"JavaScript:airTripSelection('OW')\" class='airfare-link'>One-Way</a>";
		document.all.spanLinkRoundTrip.innerHTML = "<a href=\"JavaScript:airTripSelection('RT')\" class='airfare-link'>Round-Trip</a>";
			
	}
	if (document.all['AirTripType'] != null) {
		document.all.AirTripType(2).checked = true;
	}
	if (document.all['divAirItinNavMD'] != null) {
		document.all['divAirItinNavRT'].style.display='none';
		document.all['divAirItinNavOW'].style.display='none';
		document.all['divAirItinNavMD'].style.display='block';
	}
	if (document.all['divAirItinNyop'] != null) {
		document.all['divAirItinNyop'].style.display='none';
	}
	if (document.all['divAirItinTC1'] != null) {
		document.all['divAirItinTC2'].style.display='block';
		document.all['divAirItinTC1'].style.display='none';
	}
  } else {
	if (document.all['divAirItinRoundTrip'] != null) {
		document.all['divAirItinOneWay'].style.display='none';
		document.all['divAirItinMultiCity'].style.display='none';
		document.all['divAirItinRoundTrip'].style.display='block';
	}
	if (document.all['spanLinkOneWay'] != null) {
		document.all.spanLinkRoundTrip.innerHTML = '<b style="color:#FFFFFF">Round-Trip</b>';
		document.all.spanLinkOneWay.innerHTML = "<a href=\"JavaScript:airTripSelection('OW')\" class='airfare-link'>One-Way</a>";
		document.all.spanLinkMultiCity.innerHTML = "<a href=\"JavaScript:airTripSelection('MD')\" class='airfare-link'>Multi-Destination</a>";
	}
	if (document.all['AirTripType'] != null) {
		document.all.AirTripType(0).checked = true;
	}
	if (document.all['divAirItinNavRT'] != null) {
		document.all['divAirItinNavOW'].style.display='none';
		document.all['divAirItinNavMD'].style.display='none';
		document.all['divAirItinNavRT'].style.display='block';
	}
	if (document.all['divAirItinNyop'] != null) {
		document.all['divAirItinNyop'].style.display='block';
	}
	if (document.all['divAirItinTC1'] != null) {
		document.all['divAirItinTC1'].style.display='block';
		document.all['divAirItinTC2'].style.display='none';
	}
  }
}

function airASO(sTrip) { 
 if (document.all['divAso'+sTrip] != null) {
  document.all['divAslRT'].style.display='none';
  document.all['divAsoRT'].style.display='block';
  document.all['divAslOW'].style.display='none';
  document.all['divAsoOW'].style.display='block';
  document.all['divAslMD'].style.display='none';
  document.all['divAsoMD'].style.display='block'; 
  document.all['divAsoDtRT'].style.display='block';
  document.all['divAsoRtRT'].style.display='block';
  document.all['divAsoDtOW'].style.display='block'; 
  for (var i=1; i<=Number(5); i++) {
    document.all['divAsoDtMD'+i].style.display='block'; 
  }
  if (sTrip=="RT") { 
	document.airItinRT['Navigation/Required/Advanced_Search_Option'].value='Y';	
  }
  if (sTrip=="OW") { 
   document.airItinOW['Navigation/Required/Advanced_Search_Option'].value='Y';	
  }
  if (sTrip=="MD") { 
  	document.airItinMD['Navigation/Required/Advanced_Search_Option'].value='Y';	
  }
 }
}
