﻿// JScript File
var page = thisPage();

// misc pop ups
function newWin(url,title){
    myWin=window.open(url, "", "toolbar=no,width=460,height=500,left=25,top=100,directories=no,status=no,scrollbars=yes,resizable,menubar=no");    
}
// open Taxes and Fees Page
function OpenFees(){
  newwin=window.open("/taxes_and_fees.html","FeeSummary", "toolbar=no,width=400,height=400,left=25,top=100,directories=no,status=no,scrollbars=yes,resizable,menubar=no");
}
// Search Form Validation
function validateSearchForm() {
  searchForm = document.getElementsByTagName('form')[0];
  searchType = getSearchType();
  departField1 = document.getElementById('ctl00_ContentPlaceHolder1_Depart1');
  arriveField1 = document.getElementById('ctl00_ContentPlaceHolder1_Arrive1');
  startDateField1 = document.getElementById('ctl00_ContentPlaceHolder1_StartDate1');
  endDateField1 = document.getElementById('ctl00_ContentPlaceHolder1_EndDate1');
  blnValid = true;
  if (searchType == "R") {  // Do roundtrip field validation
    if (departField1.value.length < 3) {
      // should have at least 3 characters
      error = document.getElementById('fromError');
      error.style.display = 'inline';
      blnValid = false;
    } else {
      error = document.getElementById('fromError');
      error.style.display = 'none';
    }
    if (arriveField1.value.length < 3) {
      // should have at least 3 characters
      error = document.getElementById('toError');
      error.style.display = 'inline';
      blnValid = false;
    } else {
      error = document.getElementById('toError');
      error.style.display = 'none';
    }
    dateDiff = Date.parse(endDateField1.value) - Date.parse(startDateField1.value);
    
    if (dateDiff < 0) {
      error = document.getElementById('returnError');
      error.style.display = 'inline';
      blnValid = false;
    } else {
      error = document.getElementById('returnError');
      error.style.display = 'none';
    }
  }
  return blnValid;
}


// Set pagetime out values
if (page == 'flightResults.aspx') {
  setTimeout("timeoutSearchResults()",720000); // timeout in 4 minutes = 240000 720000 in 12 min
}
if (page == 'Paymentinfo.aspx') {
  setTimeout("timeoutPaymentPage()",720000); // timeout in 4 minutes = 240000
}

function FlightResultsRefresh() { // Show Search Results Timed-out
  blackOutScreen(true);
  var WaitMsg = document.getElementById('WaitMsg');
  centerThisDiv(WaitMsg, 380,150);
  WaitMsg.style.top = '200px';
  WaitMsg.style.display = 'block';
  scrolldelay = setInterval('keepAtTop()',100);
  var ResultsDiv = document.getElementById('SearchTable');
  ResultsDiv.style.display = 'none';
}

function timeoutSearchResults() { // Show Search Results Timed-out
  blackOutScreen(true);
  var tipBox = document.getElementById('tipBox');
  centerThisDiv(tipBox, 380,150);
  tipBox.style.top = '200px';
  tipBox.style.display = 'block';
  scrolldelay = setInterval('keepAtTop()',100);
}

function timeoutPaymentPage() { // Show Search Results Timed-out
  blackOutScreen(true);
  var tipBox = document.getElementById('tipBox');
  centerThisDiv(tipBox, 380,150);
  tipBox.style.top = '200px';
  tipBox.style.display = 'block';
  scrolldelay = setInterval('keepAtTop()',100);
}

function keepAtTop() {
  window.scroll(0,0);
}

function popLowestFareTips() {  // Show Low Fare Tips
  blackOutScreen(true);
  var tipBox = document.getElementById('tipBox');
  centerThisDiv(tipBox, 380,150);
  tipBox.style.display = 'block';
  return false;
}
  
function closeTipBox() {
  blackOutScreen(false);
  var tipBox = document.getElementById('tipBox');
  tipBox.style.display = 'none';
  if (typeof( scrolldelay ) != 'undefined' ) {
    clearInterval(scrolldelay);
  }
  
  return false;
}
function blackOutScreen(blnBlack)
  {
    var blackOut = document.getElementById('blackOut');
    if (blackOut == null) {
      blackOut = document.createElement('div');
      blackOut.setAttribute('id', 'blackOut');
      blackOut.style.display = 'none';
      blackOut.style.position = 'absolute';
      blackOut.style.left = '0px';
      blackOut.style.top = '0px';
      blackOut.style.width = '100%';
      blackOut.style.height = '100%';
      blackOut.style.overflow = 'hidden';
      blackOut.style.opacity = 0.8;
      blackOut.style.MozOpacity = 0.8;
      blackOut.style.filter='alpha(opacity=80)';
      blackOut.style.zIndex = 10;
      blackOut.style.backgroundColor = '#FFFFFF';
      document.getElementsByTagName("body")[0].appendChild(blackOut);
      blackOut = document.getElementById('blackOut');
    }
    if (blnBlack) {
      blackOut.style.display = 'block';
    } else {
      blackOut.style.display = 'none';
    }
  }
  
  function refreshPage() {
    window.location.reload( false );
  }

  function centerThisDiv(el, width, height)
  {
    var w = 640;
    var h = 480;  // if we can't get a doc size we assume it's small
    if (document.body.clientWidth) {
      w = document.body.clientWidth;
      h = document.body.clientHeight;
    }
    else if(window.innerWidth) {
      w = window.innerWidth;
      h = window.innerHeight;
    }

    var leftPos = (w-width)/2;
    var topPos = (h-height)/2;
    el.style.top = topPos + 'px';
    el.style.left = leftPos + 'px';
  }

  function thisPage() {
    var sPath = window.location.pathname;
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
    return sPage;
  }

/* Search Form Specific Functions */
    function HideLayer(layer){
		document.getElementById(layer).style.display='none';
	}
	
	function ShowLayer(layer){
		document.getElementById(layer).style.display='block';
	}
	
	function LoadFlights() {
		ShowLayer("ReturnDiv");
	    ShowLayer("ReturnTimeDiv");
	    document.getElementById('<%= FlightType.ClientID %>_0').checked=true;
	}
	
	// Handles Switch between one-way, round-trip, and multi-city
	function ShowSearch(el)
	{
	  searchType = getSearchType();
	  if (searchType == 'R') {
	    showElement('ReturnDiv');
	    showElement('ReturnTimeDiv');
	  }
	  if (searchType == 'O') {
	    hideElement('ReturnDiv');
	    hideElement('ReturnTimeDiv');
	  }
	  if (searchType == 'M') {
	    window.location='http://www.youtrvl.com/flights/flightsearch-multicity.aspx';
	  }
	}
	
	function ShowSearchMulti(el)
	{
	  searchType = getSearchType();
	  if (searchType == 'R') {
	    window.location='http://www.youtrvl.com/default.aspx';
	    //showElement('ReturnDiv');
	    //showElement('ReturnTimeDiv');
	  }
	  if (searchType == 'O') {
	    window.location='http://www.youtrvl.com/default.aspx?st=O';
	    //hideElement('ReturnDiv');
	    //hideElement('ReturnTimeDiv');
	  }
	  if (searchType == 'M') {
	    window.location='http://www.youtrvl.com/flights/flightsearch-multicity.aspx';
	  }
	}
	
	function getSearchType() {
	  el = document.getElementById('ctl00_ContentPlaceHolder1_FlightType_0');
	  if (el.checked == true) {
	    return 'R';
	  }
	  el = document.getElementById('ctl00_ContentPlaceHolder1_FlightType_1');
	  if (el.checked == true) {
	    return 'O';
	  }
	  el = document.getElementById('ctl00_ContentPlaceHolder1_FlightType_2');
	  if (el.checked == true) {
	    return 'M';
	  } else {
	    return '';
	  }
	}
	
	function hideElement(id) {
	  div = document.getElementById(id);
	  div.style.display = 'none';
	}
		
	function showElement(id) {
	  div = document.getElementById(id);
	  div.style.display = 'block';
	}
	
	
  var aCal;
  var dateFormat = 'MM/dd/yyyy';
  function openFromCalendar()
  {
    try {
      aCal.hidePopup();
    } catch(err) {
    
    }
	  now = new Date();
    aCal = new CalendarPopup("fromCal");
    aCal.offsetX = 0;
	  aCal.offsetY = 26;
	  aCal.setCssPrefix("fCal");
	  aCal.addDisabledBeforeDates(null, formatDate(now,dateFormat));
    aCal.select(document.getElementById("ctl00_ContentPlaceHolder1_StartDate1"),'ctl00_ContentPlaceHolder1_StartDate1',dateFormat);
  }

  function openToCalendar()
  {
    try {
      aCal.hidePopup();
    } catch(err) {
    
    }
    var now = new Date();
    var toDateEl = document.getElementById("ctl00_ContentPlaceHolder1_EndDate1");
    var fromDateEl = document.getElementById("ctl00_ContentPlaceHolder1_StartDate1");
    aCal = new CalendarPopup("toCal");
	  aCal.setCssPrefix("fCal");
	  startDate = fromDateEl.value;
	  if ((startDate != "") && (isDate(startDate,dateFormat)))
	  {
	    aCal.addDisabledBeforeDates(null, startDate);
	    toDateEl.value = startDate;
	  } else {
	    aCal.addDisabledBeforeDates(null, formatDate(now,dateFormat));
	  }
	  aCal.offsetX = 0;
	  aCal.offsetY = 26;
    aCal.select(toDateEl,'ctl00_ContentPlaceHolder1_EndDate1',dateFormat);
  }
 
 function openToCalendar2()
  {
    try {
      aCal.hidePopup();
    } catch(err) {
    
    }
    var now = new Date();
    var toDateEl = document.getElementById("ctl00_ContentPlaceHolder1_StartDate2");
    var fromDateEl = document.getElementById("ctl00_ContentPlaceHolder1_StartDate1");
    aCal = new CalendarPopup("toCal");
	  aCal.setCssPrefix("fCal");
	  startDate = fromDateEl.value;
	  if ((startDate != "") && (isDate(startDate,dateFormat)))
	  {
	    aCal.addDisabledBeforeDates(null, startDate);
	    toDateEl.value = startDate;
	  } else {
	    aCal.addDisabledBeforeDates(null, formatDate(now,dateFormat));
	  }
	  aCal.offsetX = 0;
	  aCal.offsetY = 26;
    aCal.select(toDateEl,'ctl00_ContentPlaceHolder1_StartDate2',dateFormat);
  }
  
  function openToCalendar3()
  {
    try {
      aCal.hidePopup();
    } catch(err) {
    
    }
    var now = new Date();
    var toDateEl = document.getElementById("ctl00_ContentPlaceHolder1_StartDate3");
    var fromDateEl = document.getElementById("ctl00_ContentPlaceHolder1_StartDate2");
    aCal = new CalendarPopup("toCal");
	  aCal.setCssPrefix("fCal");
	  startDate = fromDateEl.value;
	  if ((startDate != "") && (isDate(startDate,dateFormat)))
	  {
	    aCal.addDisabledBeforeDates(null, startDate);
	    toDateEl.value = startDate;
	  } else {
	    aCal.addDisabledBeforeDates(null, formatDate(now,dateFormat));
	  }
	  aCal.offsetX = 0;
	  aCal.offsetY = 26;
    aCal.select(toDateEl,'ctl00_ContentPlaceHolder1_StartDate3',dateFormat);
  }
