var displayTextBoxes = false;

function FsData() {
  this.urls = new Array();
  this.urls["fsRequest"] = "/rail/point_to_point/results.htm";
  this.urls["fsLater"] = "/rail/point_to_point/later.htm";
  this.urls["fsVpRequest"] = "/rail/point_to_point/vp_trip_result.htm";
  this.urls["fsVpLater"] = "/rail/point_to_point/vp_trip_later.htm";
}
  
function getBrowserType()
{
  var browserName = navigator.appName.toLowerCase();
  var browserUserAgent = navigator.userAgent.toLowerCase();

  var browserType = "X";  // some unknown browser

  if (browserName.indexOf("microsoft") >= 0) {
    browserType = "I";
  }
  else if (browserName.indexOf("netscape") >= 0) {
    browserType = "N";
  }
  else if (browserUserAgent.indexOf("aol") >= 0) {
    browserType = "A";
  }
  else if (browserUserAgent.indexOf("opera") >= 0) {
    browserType = "O";
  }
  return browserType;
}

function getBrowserVersion()
{
  var browserVersion = navigator.appVersion;
  for (var v = 3; v <= 6; v++) {
    if (browserVersion.indexOf(v + ".") >= 0) return v;
  }
  return 0;
}

function doTextBoxes() {
  var browserType = getBrowserType();
  var ver = getBrowserVersion();
  return ((browserType == "N" && ver >= 5) || 
          (browserType == "I" && ver >= 4) ||
           browserType == "A");
}

function binSearch(toFind, array, low, high)
{
  var mid = Math.floor( (low+high)/2 );
  if (low > high) {
    return -1;
  }
  var matchResult = match(toFind, array[mid].text);
  if (low == high) {
    return (matchResult == 0 ) ? mid : -1;
  }
  else if (matchResult == 0) {
    return mid;
  }
  else if (matchResult < 0) {
    return binSearch(toFind, array, low, mid-1);
  }
  else {
    return binSearch(toFind, array, mid+1, high);
  }
}

function match(toFind, subject)
{
  var toFindLC = toFind.toLowerCase();
  var subjectLC = subject.toLowerCase();
  var toFindLCLen = toFindLC.length;
  if (toFindLCLen > 0 && toFindLC == subjectLC.substr(0, toFindLCLen)) {
    return 0;
  }
  else {
    return (toFindLC < subjectLC) ? -1 : +1;
  }
}

function selectFirstMatch(city, index, fromTo)
{
  var array = document.fspage[fromTo].options;
  for (var i = index; i > 0; i--) {
    if (match(city, array[i - 1].text) != 0) break;
  }	    
  return i;
}

function hOnKeyUp(fromTo)
{
  var selectBox = document.fspage[fromTo];
  var array = selectBox.options;
  var city = document.fspage["TXT_" + fromTo].value;

  var index = binSearch(city, array, 0, array.length-1);

  if ( index == -1 ) {
    selectBox.selectedIndex = -1;
  }
  else  {
    var newIndex = selectFirstMatch(city, index, fromTo);
    array[newIndex].selected = true;
  }
}

function hOnBlur(fromTo)
{
  var selectWidget = document.fspage[fromTo];
  var array = selectWidget.options;
  var index = selectWidget.selectedIndex;
  if (index >= 0) {
    document.fspage['TXT_' + fromTo].value = array[index].text;
  }
}

function request_text_box(fromTo, heading) {
  if (displayTextBoxes) {
    document.write(heading);
    var tabIndex = (fromTo == 'F') ? 1 : 2;
    document.write('<input autocomplete="off" type="text" ' +
      'name="TXT_' + fromTo + '" ' +
      'size="20" onKeyUp="hOnKeyUp(\'' + fromTo + '\')" ' +
      'onBlur="hOnBlur(\'' + fromTo + '\')" ' +
      'TABINDEX="' + tabIndex + '"><p>');
  }
}

displayTextBoxes = doTextBoxes();



// New FS
function showPopup(pURL) {
  var cWidth= 420;
  var cHeight= 625;
  var cParams= "status=1,toolbar=1,scrollbars=1,location=0,menu=1,resizable=1";

  var width = (showPopup.arguments[1] > 0) ? showPopup.arguments[1] : cWidth;
  var height = (showPopup.arguments[2] > 0) ? showPopup.arguments[2] : cHeight;
  var params = (showPopup.arguments[3]) ? showPopup.arguments[3] : cParams;
  popupWindow = window.open(pURL,
                            "popupWindow",
                            "width=" + width + "," +
                            "height=" + height + "," +
                            params);
  if (popupWindow.focus) popupWindow.focus();
}
function fareDebug(countrySite, stateId, solutionIndex, klass, step) {
  showPopup("/fs/" + countrySite + "/FSServe?fn=fareDebug&stateId=" + 
            stateId + "&solutionIndex=" + solutionIndex +
     "&klass=" + klass + "&step=" + step);
}
function fareRules(countrySite, stateId, solutionIndex, klass, step) {
  showPopup("/fs/" + countrySite + "/FSServe?fn=fareRules&stateId=" + 
            stateId + "&solutionIndex=" + solutionIndex +
     "&klass=" + klass + "&step=" + step);
}
function showAccomodations(countrySite, stateId) {
  showPopup("/fs/" + countrySite + "/FSServe?fn=showAccomodations&stateId=" +
            stateId, 420, 400);
}
function book(widget, value) {
  document.PtpResults.fareSelect.value = value;
  //alert(document.PtpResults);
  //document.PtpResults.submit();
  return true;
}
function selectFare(widget, value) {
  document.PtpResults.fareSelect.value = value;
  document.PtpResults.submit();
  return false;
}
function selectFare1(form, value) {
  form.fareSelect.value = value;
  form.submit();
  return false;
}

function mask(f) {if(isNaN(parseInt(f.value))) f.value= "";}

function enableRT(f) {
    f.d1.disabled=false;
    f.m1.disabled=false;
    f.t1.disabled=false;
}
function disableRT(f) {
    f.d1.disabled=true;
    f.m1.disabled=true;
    f.t1.disabled=true;
}
function initializeRT(f) {
  var rtContr = f.r[0];
  if (rtContr == null) return; 
  if (f.r[0].checked) {
    enableRT(f);
  }
  else {
    disableRT(f);
  }
}

//var dots;
//var count=0;
function wait_page_on_load(mainDiv, dotsDiv, img, width, height, countrySite){
  //dots = dotsDiv;
  var key = getFunctionKey();
  var fsData = new FsData();
  var finurl = "/" + countrySite + fsData.urls[key] + window.location.search;
  window.location.replace(finurl);

  if(document.getElementById&&!(navigator.userAgent.indexOf("5.2")>-1)){
    mainDiv.style.backgroundImage = img;
    mainDiv.style.position = "relative";
    mainDiv.style.width = width;
    mainDiv.style.height = height;
    mainDiv.style.backgroundRepeat = "no-repeat";

//    if(!document.all) {
//      animate();
//    }
  }
}
//function animate(){
//  wait = "We are searching for your ticket(s) ";
//  
//  switch (count){
//    case 0:dots.firstChild.nodeValue = wait+"";count++;break;
//    case 1:dots.firstChild.nodeValue = wait+".";count++;break;
//    case 2:dots.firstChild.nodeValue = wait+"..";count++;break;
//    case 3:dots.firstChild.nodeValue = wait+"...";count++;break;
//    case 4:dots.firstChild.nodeValue = wait+"....";count++;break;
//    case 5:dots.firstChild.nodeValue = wait+".....";count++;break;
//    default:dots.firstChild.nodeValue = wait+"......";count=0;
//  }
//  setTimeout("animate()",500);
//}

// Second argument is an optional country code
// Only the cities that belongs to such country will be returned

function setupPTP(countrySite) {
/*
  var countryIndex = 1;
  var filterCountryCode = setupPTP.arguments[countryIndex];
  if ((typeof(document.form1)!="undefined")&&(typeof(document.form1.from0)!="undefined")){
  // setTimeout("initialize(document.form1.F0,document.form1.T0, 
  //  document.form1.d0, true, countrySite)",900);
    initialize(document.form1.from0,document.form1.to0, document.form1.deptDate0, true,
      countrySite, filterCountryCode);
//    initializeRT(document.form1);
   document.form1.from0.onfocus= function() {
     handleFocus(0);
   }
   document.form1.to0.onfocus= function() {
     handleFocus(1);
   }
  }
*/  
}


function showPtpPopunder(url) {
  ptpPopunder=window.open(url, "ptpPopunder", "status=yes,toolbar=yes,location=yes,menubar=yes,directories=yes,resizable=yes,scrollbars=yes,width=800,height=600");
  ptpPopunder.blur();
  window.focus();
}
  
function getFunctionKey() {
  var fnName = "fn";
  var query = window.location.search;
  var mainEls = query.split("&");
  var result = null;
  
  for( var index in mainEls) {
    var element = mainEls[index];
    if( element.indexOf("fn=") > -1) {
      //this is the one
      var subEls = element.split("=");
      if( subEls.length == 2) {
        result = subEls[1];
        break;
      }
    }
  }
  return ( result!=null )? result : "fsRequest";
}

///////  new wait page function

function g3_wait_page_on_load(countrySite)
{
  var key = getFunctionKey();
  var fsData = new FsData();
  var finurl = "/" + countrySite + fsData.urls[key] + window.location.search;
  window.location.replace(finurl);
}
