function getSaleCountry()
{
  var saleCountry = $('#saleCountry').val();
  if (saleCountry == undefined) saleCountry = 'us';
  return saleCountry;
}

function addPortionTrip(index, roundtrip, isAmtrakReq) {  
  var output = new Array();
  output.push("<div class='trip-search-field'><div class='trip' id='trip'>Trip "+(index+1)+" <br /> &nbsp;</div>");
  output.push("<div class='from'> From:<br> <input id='from"+index+"' name='from"+index+"'  autocomplete='off' ");
  if(index == 0 ) {
    output.push("onBlur='populateRoundTrip();' ");
  } 
  output.push(" class='from-field city-input' value='' type='text'></div>");
  output.push("<div class='to'>To:<br> <input id='to"+index+"' name='to"+index+"' autocomplete='off'   ");
  if(index == 0 ) {
    output.push("onBlur='populateRoundTrip();' ");
  } 
  output.push(" class='to-field city-input' value='' type='text'></div>");
  output.push("<div class='departure'>Departure Date:<br> <input name='deptDate"+index+"' id='deptDate"+index+"'  class='departure-field' value='' type='text'></div>");   
  output.push("<div class='trip-time'>Time:<br> <span class='field-float-left'> <select name='time"+index+"' id='time"+index+"' class='time-field'> <option value='morning' >12am to 12pm</option> <option value='afternoon'>12pm to 6pm</option> <option value='evening'>6pm to 12am</option> ")
  if(isAmtrakReq == 0) {
    output.push("<option value='anytime'  selected='selected'>Anytime</option> ");
  }
  output.push("</select> </span> </div>");
  output.push("</div>");
  output.push("<div class='clear'></div> <div class='trip-table'><!--clear--> </div> <!--trip-table-->");
  
  return output.join("");
}


function addRoundTrip(index, isAmtrakReq) {
  var output = new Array();
  output.push("<div class='trip-search-field'><div class='trip' id='trip'> Trip "+(index+1)+" <br /> </div>"); 
  output.push("<div class='from'> From: <br> <input name='fromCity"+index+"' class='from-field' value='' type='text'/> <input name='from"+index+"' type='hidden'> </div>");
  output.push("<div class='to'>To:<br> <input name='toCity"+index+"' class='to-field' value='' type='text'/> <input name='to"+index+"' type='hidden'> </div>");
  output.push("<div class='departure'>Return Date:<br> <input name='deptDate"+index+"' id='deptDate"+index+"'  class='departure-field' value='' type='text'></div>");  
  output.push("<div class='trip-time'>Time:<br> <span class='field-float-left'> <select name='time"+index+"' id='time"+index+"' class='time-field'> <option value='morning' >12am to 12pm</option> <option value='afternoon'>12pm to 6pm</option> <option value='evening'>6pm to 12am</option>  ");
  if(isAmtrakReq == 0) {
    output.push("<option value='anytime'  selected='selected'>Anytime</option> ");
  }
  output.push("</select> </span> </div>");
  output.push("</div>");
  output.push("<div class='clear'></div> <div class='trip-table'><!--clear--> </div> <!--trip-table-->");
  $(function()
  {
    $('#deptDate'+index).datePicker().val(new Date().asString()).trigger('change');
    if(isAmtrakReq == 0) {
      $('#deptDate'+index).dpSetEndDate(new Date().addDays(365).asString());
    } else {
      $('#deptDate'+index).dpSetEndDate(new Date().addDays(335).asString());
    }
    
  });
  return output.join("");
}

//For How To Book Customized function
function populateHowToRoundTrip() {
  var objForm =  document.form1;
  indexVal=0;
  for(i=0; i<objForm.roundtrip.length; i++) {
    if(objForm.roundtrip[i].checked == true) {     
      indexVal = objForm.roundtrip[i].value;
      break;
    }
  }
  if(indexVal == 1) {      
    // get trip 1 details  
    if(eval('document.form1.from1') == undefined) { 
      howToRoundTripCheck();
    } 
    document.form1.from1.value = document.form1.to0.value;
    document.form1.to1.value = document.form1.from0.value;
    document.form1.fromCity1.value = document.form1.to0.value;
    document.form1.toCity1.value = document.form1.from0.value;    
  } 
  
  amtrakReq=0;
  if(document.form1 != undefined && document.form1.isAmtrakRequest != undefined) {
    amtrakReq=document.form1.isAmtrakRequest.value;
  }
  if(amtrakReq == 1) {
    if((document.form1.to0.value !=undefined && document.form1.from0.value != undefined)){
      var contents = document.getElementById("trip-search-field-cars");
      contents.innerHTML = "";
      var output = new Array();
      fromCity = document.form1.from0.value.toLowerCase();
      toCity = document.form1.to0.value.toLowerCase();
      if((fromCity.indexOf('sanford') >= 0 && toCity.indexOf('lorton') >= 0) || 
        (toCity.indexOf('sanford') >= 0 && fromCity.indexOf('lorton') >= 0))  {  
          output.push("<p>");
          output.push("This route allows travelers to ride with their cars. Let us know if you would like to take your car.");
          output.push("</p>");
          
          output.push("<div class='car-count'># of Cars:<br/>");
          output.push("<span class='field-float-left'>");  
          output.push("<select class='bike-field' name='nCars' id='nCars'>");          
          output.push("<option value='1' selected='selected'>1</option>");            
          output.push("<option value='1'>2</option>");
          output.push("<option value='2'>3</option>");
          output.push("</select>");
          output.push("</span>");
          output.push("</div>");
          
          output.push("<div class='car-type'>Type of car #1:<br/>");
          output.push("<span class='field-float-left'>");
          output.push("<select class='car-type-field' id='carType0' name='carType0'>");
          output.push("<option value='REGVEH' selected='selected'>Regular</option>");
          output.push("<option value='MOTOVWH'>Motor Cycle</option>");          
          output.push("<option value='OVSVEH'>Oversize Vehicle</option>");
          output.push("</select>");
          output.push("</span>");
          output.push("</div>");
          output.push("<div class='clear'></div>");   
        }
      contents.innerHTML  = contents.innerHTML +output.join("");
    }
  }
  
}


function getTotalPax() {
  var objForm =  document.form1;
  
  adult = parseInt(objForm.nA.value);
  if(adult == NaN) {
    adult=0;
  }
  youth = parseInt(objForm.nY.value);
  if(youth == NaN) {
    youth=0;
  }
  child = parseInt(objForm.nC.value);
  if(child == NaN) {
    child=0;
  }
  senior = parseInt(objForm.nS.value);
  if(senior == NaN) {
    senior=0;
  }
  nPax = (adult+youth+child+senior);
  
  return nPax;
}

function populateRoundTrip() {
  var objForm =  document.form1;
  indexVal=0;
  for(i=0; i<objForm.roundtrip.length; i++) {
    if(objForm.roundtrip[i].checked == true) {     
      indexVal = objForm.roundtrip[i].value;
      break;
    }
  }
  if(indexVal == 1) {      
    // get trip 1 details
    document.form1.fromCity1.value = document.form1.to0.value;
    document.form1.toCity1.value = document.form1.from0.value;
    document.form1.from1.value = document.form1.to0.value;
    document.form1.to1.value = document.form1.from0.value;
    document.form1.fromCity1.disabled=true;
    document.form1.toCity1.disabled=true;
  }
  amtrakReq=0;
  if(document.form1 != undefined && document.form1.isAmtrakRequest != undefined) {
    amtrakReq=document.form1.isAmtrakRequest.value;
  }
  if(amtrakReq == 1) {
    populateCarField();
  }
}


function getNextMonthDate() {
  
  var myDate=new Date();
  myDate.setMonth(myDate.getMonth()+1);
  
  month = parseInt(myDate.getMonth())+1;
  day = myDate.getDate();
  year = myDate.getFullYear();
  mm = month>9?month:"0"+month;
  dd = (day>9?day:"0"+day);
  newDate = mm +"/"+ dd +"/"+year;
  
  return newDate;
}


function updateRoundTripDate() {
  var objForm =  document.form1;
  indexVal=0;
  for(i=0; i<objForm.roundtrip.length; i++) {
    if(objForm.roundtrip[i].checked == true) {     
      indexVal = objForm.roundtrip[i].value;
      break;
    }
  }
  if(indexVal == 1) {      
    // get trip 1 details
    deptDate = document.form1.deptDate0.value;
    if(deptDate != '') {
      str1 = deptDate.split("/");
      
      month = parseInt(str1[0],10)-1;
      day = parseInt(str1[1],10);
      year = str1[2];
      
      var myDate=new Date();
      // alert(' my date 1 : ' + myDate);
      myDate.setFullYear(year,month, day);
      // alert(' my date : ' + myDate);
      myDate.setDate(myDate.getDate()+5);
      
      //     alert(' after 5 , my date : ' + myDate);
      month = parseInt(myDate.getMonth())+1;
      day = myDate.getDate();
      year = myDate.getFullYear();
      mm = month>9?month:"0"+month;
      dd = (day>9?day:"0"+day);
      newDate = mm +"/"+ dd +"/"+year;
      
      document.form1.deptDate1.value = newDate;
      $(function()
      {
        $('#deptDate1').datePicker().val(newDate).trigger('change');
        $('#deptDate1').dpSetStartDate(deptDate);
        $('#deptDate1').dpSetEndDate(new Date().addDays(365).asString());
      });
    }
    
  }
  
}

function checkNoOfPax() {
  var objForm =  document.form1;
  
  adult = parseInt(objForm.nA.value);
  if(adult == NaN) {
    adult=0;
  }
  youth = parseInt(objForm.nY.value);
  if(youth == NaN) {
    youth=0;
  }
  child = parseInt(objForm.nC.value);
  if(child == NaN) {
    child=0;
  }
  senior = parseInt(objForm.nS.value);
  if(senior == NaN) {
    senior=0;
  }
  nPax = (adult+youth+child+senior);
  
  return true;
}

function populateHomeYouthAge() {
  var objForm =  document.form1;
  noYouth = parseInt(objForm.nY.value);
  
  if(checkNoOfPax()) {
    var contents = document.getElementById("youthAge");
    contents.innerHTML = ""; 
    var output = new Array();
    // get the ages value
    for(i=0; i<noYouth; i++) {  
      output.push(" <input name='youthAge"+i+"' value='20'  type='hidden'/> ");
    }
    
    if(noYouth > 0) {
      contents.innerHTML  = contents.innerHTML +output.join("");
    }
  }
}



function populateYouthAge(youthAges, headerText, youthPaxAge) {
  var objForm =  document.form1;
  
  noYouth = parseInt(objForm.nY.value);
  
  if(checkNoOfPax()) {
    var contents = document.getElementById("youthAge");
    contents.innerHTML = "";
    
    var output = new Array();
    
    
    output.push(" <p class='youth-note'>"+headerText + "</p> ");
    output.push("<div class='trip-passenger-field '>");    
    // get the ages value
    var tmp = new Array();
    tmp = youthAges.split("-");
    
    // alert(' tmp ' + tmp);
    for(i=0; i<noYouth; i++) {    
      output.push(" <div class='deals-form-field'> "+youthPaxAge +" #"+(i+1)+" <br/> <input name='youthAge"+i+"' size='2' class='tiny' ");
      if(tmp.length > 0 || tmp[i].length > 0) { 
        if(tmp[i] != undefined) {
          output.push(" value='"+tmp[i]+"' ");
        }
      }
      output.push("type='text'>  </div>");
    }
    output.push(" </div>");
    
    if(noYouth > 0) {
      contents.innerHTML  = contents.innerHTML +output.join("");
    }
  }
}


function populateHiddenYouthAge(youthAges, formName) {
  var objForm = document.forms[formName]; 
  
  noYouth = parseInt(objForm.nY.value);
  
  
  var contents = document.getElementById("youthAges");
  contents.innerHTML = "";
  
  var output = new Array();
  
  // get the ages value
  var tmp = new Array();
  tmp = youthAges.split("-");
  for(i=0; i<noYouth; i++) {    
    output.push(" <input name='youthAge"+i+"' ");
    if(tmp.length > 0 || tmp[i].length > 0) { 
      if(tmp[i] != undefined) {
        output.push(" value='"+tmp[i]+"' ");
      }
    }
    output.push("type='hidden'>");
  }
  
  if(noYouth > 0) {
    contents.innerHTML  = contents.innerHTML +output.join("");
  }
}

//Customized function for How To Book Page (CMS)

function howToPopulateYouthAge(youthAges, headerText, youthPaxAge) {
  var objForm =  document.form1;
  
  noYouth = parseInt(objForm.nY.value);
  
  if(checkNoOfPax()) {
    var contents = document.getElementById("youthAge");
    contents.innerHTML = "";
    
    var output = new Array();
    
    output.push("<p class='youth-note'> " + headerText + " </p>" );
    output.push("<div id='trip-passenger-field' class='trip-passenger-field'>");
    
    
    // get the ages value
    var tmp = new Array();
    tmp = youthAges.split("-");
    
    // alert(' tmp ' + tmp);
    for(i=0; i<noYouth; i++) {    
      output.push(" <div class='deals-form-field'> "+youthPaxAge +" #"+(i+1)+" <br/> <input name='youthAge"+i+"' class='tiny' maxlength='2' ");
      if(tmp.length > 0 || tmp[i].length > 0) { 
        if(tmp[i] != undefined) {
          output.push(" value='"+tmp[i]+"' ");
        }
      }
      output.push("type='text'>  </div>");
    }
    output.push(" </div>");
    
    if(noYouth > 0) {
      contents.innerHTML  = contents.innerHTML +output.join("");
    }
  }
}


function validateRTDate() {
  for(j=0; j<document.form1.roundtrip.length; j++) {
    if(document.form1.roundtrip[j].checked == true) {
      rVal = j;   
    }
  }
  if(rVal == 1) {
    if(eval('document.form1.deptDate1') != undefined) {
      $('#deptDate0').bind('dpClosed', 
        function(e, selectedDates)    {
          var d = selectedDates[0];
          if (d) {
            d = new Date(d);
            $('#deptDate1').dpSetStartDate(d.addDays(0).asString());
          }
        }
      );
    }
  }
  
  if(rVal == 1 && eval('document.form1.from1') == undefined) { 
    roundTripCheck();
  }
  
}

function roundTripCheck() {
  
  for(j=0; j<document.form1.roundtrip.length; j++) {
    if(document.form1.roundtrip[j].checked == true) {
      rVal = j;   
    }
  }
  // check whether the current request is amtrak
  amtrakReq=0;
  if(document.form1.isAmtrakRequest != undefined) {
    amtrakReq = document.form1.isAmtrakRequest.value;
  }
  maxDeptDateDaysLimit = 365;
  if(amtrakReq != 0) {
    maxDeptDateDaysLimit = 335;
  }
  
  
  var anotherTrip = document.getElementById("add-another-trip");
  if(rVal  == 0) {   
    fromCity = document.form1.from0.value;
    toCity = document.form1.to0.value;
    deptdate = document.form1.deptDate0.value;
    time = document.form1.time0.value;
    var contents = document.getElementById("trip-frame");
    contents.innerHTML = "";
    document.getElementById("trip-frame").innerHTML = addPortionTrip(0,0, amtrakReq);
    document.form1.from0.value=fromCity;
    document.form1.to0.value=toCity;
    document.form1.deptDate0.value=deptdate;
    document.form1.time0.value=time;
    document.form1.rows.value=1;
    if(anotherTrip != undefined) {
      anotherTrip.innerHTML="";
    }
    $('#deptDate0').datePicker().val($('#deptDate0').val()).trigger('change');
    $('#deptDate0').dpSetEndDate(new Date().addDays(maxDeptDateDaysLimit).asString());  
    document.form1.deptDate0.value=deptdate;
  }
  
  // if rVal is 1, populate the From1 and To1 in second trip
  if(rVal  == 1) {    
    
    var output = new Array();
    
    // get trip 1 details
    fromCity = document.form1.from0.value;
    toCity = document.form1.to0.value;
    deptdate = document.form1.deptDate0.value;
    time = document.form1.time0.value;;
    
    var contents = document.getElementById("trip-frame");
    // clear the trips
    contents.innerHTML="";
    
    // add trip 1
    contents.innerHTML  = contents.innerHTML + addPortionTrip(0,1, amtrakReq);
    // add trip 2 - round trip
    contents.innerHTML  = contents.innerHTML + addRoundTrip(1, amtrakReq);
    
    // assign trip 1 values
    document.form1.from0.value=fromCity;
    document.form1.to0.value=toCity;
    document.form1.deptDate0.value=deptdate;
    document.form1.time0.value=time;
    
    
    // assign trip 2 values
    document.form1.from1.value=toCity;
    document.form1.fromCity1.value=toCity;
    document.form1.to1.value=fromCity;
    document.form1.toCity1.value=fromCity;
    document.form1.deptDate1.value=deptdate;
    
    document.form1.fromCity1.disabled=true;
    document.form1.toCity1.disabled=true;
    if(anotherTrip != undefined) {
      anotherTrip.innerHTML="";
    }
    document.form1.rows.value=2;
    $('#deptDate0').datePicker().val($('#deptDate0').val()).trigger('change');
    $('#deptDate1').datePicker().val($('#deptDate1').val()).trigger('change');
    $('#deptDate0').dpSetEndDate(new Date().addDays(maxDeptDateDaysLimit).asString());
    $('#deptDate1').dpSetEndDate(new Date().addDays(maxDeptDateDaysLimit).asString());
    
    if(eval('document.form1.deptDate1') != undefined) {
      $('#deptDate0').bind('dpClosed', 
        function(e, selectedDates)    {
          var d = selectedDates[0];
          if (d) {
            d = new Date(d);
            $('#deptDate1').dpSetStartDate(d.addDays(0).asString());
          }
        }
      );
    }
    
    updateRoundTripDate();
    
    $('#deptDate0').attr({ 
      onChange: "updateRoundTripDate();"
      });
    
  }
  
  if(rVal  == 2) {
    
    rows = document.form1.rows.value;
    
    if(rows == undefined) {
      rows = 6;
    }
    if(rows < 6) {
      rows = 6;
    }
    fromCityArray = new Array(rows);
    toCityArray  = new Array(rows);
    deptDateArray  = new Array(rows);
    deptTimeArray  = new Array(rows);
    for(i=0; i<rows; i++){
      if(eval('document.form1.from'+i) != undefined) {
        from = eval('document.form1.from'+i+'.value');
        //  alert(' from ' + from);
        if(from != undefined) {
          fromCityArray[i] = from;
        }
      } else {
        fromCityArray[i] = '';
      }
      
      if(eval('document.form1.to'+i) != undefined) {
        to = eval('document.form1.to'+i+'.value');
        //  alert(' to ' + to);
        if(to != undefined) {
          toCityArray[i] = to;
        }
      } else {
        toCityArray[i] = '';
      }
      
      if(eval('document.form1.deptDate'+i) != undefined) {
        ddate = eval('document.form1.deptDate'+i+'.value');
        //  alert(' ddate ' + ddate);
        if(ddate != undefined) {
          deptDateArray[i] = ddate;
        }
      } else {
        deptDateArray[i]=''
      }
      
      
      if(eval('document.form1.time'+i) != undefined) {
        dtime = eval('document.form1.time'+i+'.value');
        //  alert(' dtime ' + dtime);
        if(dtime != undefined) {
          deptTimeArray[i] = dtime;
        } 
      } else {
        deptTimeArray[i] = '';
      }
      
    }
    
    //   fromCity = document.form1.from0.value;
    //   toCity = document.form1.to0.value;
    //   deptdate = document.form1.deptDate0.value;
    //   time = document.form1.time0.value;
    
    var contents = document.getElementById("trip-frame");
    contents.innerHTML = "";
    document.getElementById("trip-frame").innerHTML = addPortionTrip(0,2,amtrakReq);
    for(i=1; i<rows; i++){
      contents.innerHTML  = contents.innerHTML + addPortionTrip(i,2, amtrakReq);
    }
    
    for(i=0; i<rows; i++){
      $('#from'+i).val(fromCityArray[i]);
      $('#to'+i).val(toCityArray[i]);
      
      if(deptDateArray[i] != '') {
        $('#deptDate'+i).datePicker().val(deptDateArray[i]).trigger('change');
      } else {
        $('#deptDate'+i).datePicker().val($('#deptDate'+(i-1)).val()).trigger('change');
      }

      if(deptTimeArray[i] != '') {
        $('#time'+i).val(deptTimeArray[i]);
      }
      //eval('document.form1.to'+i+'.value='+toCityArray[i]);
      //eval('document.form1.deptDate'+i+'.value='+deptDateArray[i]);
      //eval('document.form1.time'+i+'.value='+deptTimeArray[i]);
      
      $('#deptDate'+i).dpSetEndDate(new Date().addDays(365).asString());
    }
    
    var output = new Array();
    output.push("<a href='javascript:addAnotherTrip();'><img src='/redesign/images/img-plus.jpg' class='float-left' alt='Add Another Trip'/></a>");
    output.push("<div><a href='javascript:addAnotherTrip();'>Add another trip</a></div>");
    
    
    anotherTrip.innerHTML = output.join("");
    document.form1.rows.value=rows;
    // for(i=0; i<rows; i++){
    //   $('#deptDate'+i).datePicker().val(getNextMonthDate()).trigger('change');
    //   $('#deptDate'+i).dpSetEndDate(new Date().addDays(365).asString());
    //}
  }
  
  reinitialize_cities();  //NK
  return false;
}


function addAnotherTrip() {
  // get the current number of portions
  // index= parseInt(document.form1.rows.value);
  rows =1;
  for(i=0; i<12; i++) {
    //alert(' date ' + $('#deptDate'+i).val());
    if($('#deptDate'+i).val() != undefined) {
      rows++;
    }
  }
  index=rows-1;
  
  if(index == 12) {
    alert('Maximum number of portions allowed is 12.');
  } else {
    //var contents = document.getElementById("trip-frame");
    // contents.innerHTML  = contents.innerHTML + addPortionTrip(parseInt(index));
    //alert(' contents ' + contents);
    // document.getElementById("trip-frame").innerHTML += addPortionTrip(parseInt(index));
    $('#trip-frame').append(addPortionTrip(parseInt(index),2, 0));
    document.form1.rows.value=(index+1);
    $('#deptDate'+index).datePicker().val(getNextMonthDate()).trigger('change');
    $('#deptDate'+index).dpSetEndDate(new Date().addDays(365).asString());
    
    reinitialize_cities();  //NK
  }
  
}



function multioverviewSubmit(formName, aafFormName,  strAction) {
  multiForm = document.forms[formName]; 
  vpFormName = document.forms[aafFormName]; 
  
  
  
  if(multiForm.fromFareType.length > 0) {
    for(i=0; i<multiForm.fromFareType.length; i++) {
      if(multiForm.fromFareType[i].checked == true) {
        fareType = multiForm.fromFareType[i].value;
        break;
      }
    }
  } else {
    fareType = multiForm.fromFareType.value;
  }
  
  
  // if it is virtual pass, submit the form to shopping cart
  // AAF_PREMIER_FARE = "4";
  if(fareType == 4) { 
    vpFormName.submit();
  } else {
    multiForm.action = '/' + getSaleCountry() + '/rail/point_to_point/multioverview.htm?strAction='+strAction+'&fromFareType='+fareType;      
    multiForm.submit();
  }
}

function portionSubmit(formName, strAction) {
  checkRTCompatibility(formName);
  portionForm = document.forms[formName];
  // get value of isFareCompatible
  result = portionForm.isFareCompatible.value;
  message = portionForm.rtMessage.value;
  editPortionId = portionForm.editPortionId.value;
  //  alert(' isfarect ' + result);
  //  alert("editPortionId : " + editPortionId);
  
  if(result == 'true') {
    portionForm.strAction.value=strAction;
    portionForm.action = '/' + getSaleCountry() + '/rail/point_to_point/portionresult.htm';
    portionForm.submit();
  } else {
    res = confirm(message);
    if(res == true){
      submitMandatoryRTFares(formName, editPortionId);
      //      modifyPortion(formName, editPortionId);
    } else {
      return false;
    }
  }
}


function submitMandatoryRTFares(formName, portionId) {
  // alert('edit button ' + portionId);
  var objForm = document.forms[formName];   
  objForm.action = '/' + getSaleCountry() + '/rail/point_to_point/portionresult.htm?editPortionId='+portionId+'&strAction=edit&mandatoryRtSelected=true';
  objForm.submit();
}




function earlierTrain(criteriaForm, presultForm, strAction) {
  // 0 - 6
  // 6 - 12
  // 12 - 18
  startTime =  getStartTimeHour(criteriaForm);
  //alert(' startTime ' + startTime);
  endTime = 0;
  if(startTime >= 0 && startTime < 11) {
    startTime = 0;
    endTime = 288;
  } else if(startTime >= 11 && startTime < 17) {
    startTime = 0;
    endTime = 288;
  } else if(startTime >= 17 && startTime < 24) {
    startTime = 288;
    endTime = 432;
  } 
  
  document.criteriaForm.minTime.value = startTime;
  document.criteriaForm.maxTime.value = endTime;
  
  var params='&'+ getCriteriaParams(criteriaForm, presultForm);
  //alert(' earlier params ' + params);
  //return false;
  
  
  
  portionForm = document.forms[presultForm]; 
  portionForm.action = '/' + getSaleCountry() + '/rail/point_to_point/portioncriteria.htm?'+'strAction='+strAction+params;      
  portionForm.submit();
  
}

function laterTrain(criteriaForm, presultForm, strAction) {
  // 0 - 6
  // 6 - 12
  // 12 - 18
  startTime =  getStartTimeHour(criteriaForm);
  endTime = 0;
  if(startTime >= 0 && startTime < 11) {
    startTime = 288;
    endTime = 432;
  } else if(startTime >= 11 && startTime < 17) {
    startTime = 432;
    endTime = 576;
  } else if(startTime >= 17 && startTime < 24) {
    startTime = 432;
    endTime = 576;
  } 
  
  
  
  document.criteriaForm.minTime.value = startTime;
  document.criteriaForm.maxTime.value = endTime;
  
  
  var params='&'+ getCriteriaParams(criteriaForm, presultForm);
  // alert(' later params ' + params);
  //return false;
  
  
  portionForm = document.forms[presultForm]; 
  portionForm.action = '/' + getSaleCountry() + '/rail/point_to_point/portioncriteria.htm?'+'strAction='+strAction+params;      
  portionForm.submit();
  
}


function getStartTimeHour(criteriaForm) {
  var slider_min  = parseInt(document.criteriaForm.minTime.value);
  //  alert(' slider min ' + slider_min );
  //  if(slider_min > 0) {
  //    slider_min += 34;
  //  }
  
  var startTime = 0;
  if(slider_min > 0) {
    startTime = Math.floor((slider_min/24));
    //    startTime -= 1;
  }
  stTime = startTime+"";
  
  if(stTime.indexOf(".")> 0) {
    stTime = stTime.substring(0, stTime.indexOf("."));
  }
  //  alert(' stTime ' + stTime );
  return stTime;
}



function getStartTimeMin(criteriaForm) {
  var slider_min  = parseInt(document.criteriaForm.minTime.value);
  var startTime = 0;
  if(slider_min > 0) {
    startTime = slider_min%24;
  }
  stTime = startTime * 2.5;
  return stTime;
}


function getEndTimeHour(criteriaForm) {
  var slider_max  =  parseInt(document.criteriaForm.maxTime.value);
  //  alert(' slider_max   ' + slider_max   );
  //  slider_max += 34;
  
  var endTime = Math.floor((slider_max/24));
  //  endTime -= 1;
  edTime = endTime+"";
  
  if(edTime.indexOf(".")> 0) {
    edTime = edTime.substring(0, edTime.indexOf("."));
  }
  //alert(' edTime   ' + edTime   );
  return edTime;
}

function getEndTimeMin(criteriaForm) {
  var slider_max  =  parseInt(document.criteriaForm.maxTime.value);
  //  alert(' slider_max   ' + slider_max   );
  //  slider_max += 34; 
  var endTime = slider_max%24;
  //  alert(' endTime ' + endTime);
  edTime = 0;
  
  edTime = endTime * 2.5;
  
  //  alert(' edTime min   ' + edTime   );
  return edTime;
}


function getCriteriaParams(criteriaForm, presultForm) {
  // need to consider all the components
  // whichever component is clicked
  // get all the values and pass to the server
  
  //for(i=0; i<document.criteriaForm.elements.length; i++)
  //{
  // alert("The field name is: " + document.criteriaForm.elements[i].name + " and it’s value is: " + document.criteriaForm.elements[i].value + ".<br />");
  //}
  objResultForm = document.forms[presultForm]; 
  objCritForm =  document.forms[criteriaForm]; 
  
  var portionId = objResultForm.portionId.value;
  var faresSelected = objResultForm.faresSelected.value;
  var resultId = objResultForm.resultId.value;
  var fareKey = document.portionHeaderForm.portionFareSelected.value;
  var connection0 = "";
  var connection1 = "";
  var connection2 = "";
  var fastest = "";
  var cheapest = "";
  var comfort = "";
  var firsttrainArriving = "";
  var lasttrainDeparting = "";
  
  if(objCritForm.zero.checked) {
    connection0 = objCritForm.zero.value;
  }
  
  if(objCritForm.one.checked) {
    connection1 = objCritForm.one.value;
  }
  
  if(objCritForm.two.checked) {
    connection2 = objCritForm.two.value;
  } 
  
  var startTime = objCritForm.minTime.value;
  var endTime = objCritForm.maxTime.value;
  
  if( objCritForm.fastest.checked){
    fastest = objCritForm.fastest.value;
  }
  
  
  if( objCritForm.cheapest.checked){
    cheapest = objCritForm.cheapest.value;
  }
  
  if( objCritForm.comfort.checked){
    comfort = objCritForm.comfort.value;
  }
  
  if( objCritForm.firsttrain.checked){
    firsttrainArriving = objCritForm.firsttrain.value;
  }
  
  if( objCritForm.lasttrain.checked){
    lasttrainDeparting = objCritForm.lasttrain.value;
  }
  
  stTimeHour = getStartTimeHour(criteriaForm);
  stTimeMin = getStartTimeMin(criteriaForm);
  
  edTimeHour =  getEndTimeHour(criteriaForm);
  edTimeMin =  getEndTimeMin(criteriaForm);
  
  var params = "zero="+ connection0 +"&one="+ connection1 +"&two="+ connection2 +"&minTimeHr="+ stTimeHour +"&maxTimeHr="+ edTimeHour +"&minTime="+ stTimeHour +"&maxTime="+ edTimeHour+"&minTimeMin="+ stTimeMin +"&maxTimeMin="+ edTimeMin +"&fastest="+ fastest +"&cheapest="+ cheapest +"&comfort="+ comfort+"&firsttrain="+firsttrainArriving+"&lasttrain="+lasttrainDeparting+"&fareKey="+fareKey+"&faresSelected="+faresSelected+"&resultId="+resultId+"&portionId="+portionId;
  
  // alert('params ' + params);
  return params;
}


// on change whenever any component is clicked
// or changed

function submitCriteria(criteriaForm, presultForm) {
  
  objResultForm = document.forms[presultForm]; 
  objCritForm =  document.forms[criteriaForm]; 
  
  var portionId = objResultForm.portionId.value;
  var faresSelected = objResultForm.faresSelected.value;
  var resultId = objResultForm.resultId.value;
  
  // need to consider all the components
  // whichever component is clicked
  // get all the values and pass to the server
  var params = getCriteriaParams(criteriaForm, presultForm);
  // alert(' params ' + params);
  // get processFlag value
  prFlag = objCritForm.processFlag.value;
  // processFlag  - used for basically not to submit the multiple request continuously
  // if it is false, change to true and call ajax request
  
  // if it is true , dont call ajax request
  if(prFlag == 'true') {
    return false;
  }
  
  if(prFlag == 'false') {
    objCritForm.processFlag.value = 'true';
  }
  
  
  // return false;
  
  var action = '/' + getSaleCountry() + "/rail/point_to_point/portioncriteria.htm?"+params;
  
  var output = new Array();
  
  
  output.push(" <form name='PtpResults'  method='post'> ");
  output.push(" <input type='hidden' name='action' />");
  output.push(" <input type='hidden' name='portionId' value='"+ portionId + "' />");
  output.push(" <input type='hidden' name='resultId' value='"+ resultId + "' />");
  output.push(" <input type='hidden' name='faresSelected' value='"+faresSelected+"'/>");
  // output.push(" There was a problem with the network in getting the results. ");
  output.push(" </form> ");
  
  var resultTxt1 = "";
  
  var contents = document.getElementById("portioncontents");
  contents.innerHTML = "";
  //     alert('ptpResultData ' + ptpResultData);
  document.getElementById("portioncontents").innerHTML = "<div align='center'><img src='/redesign/images/fs_waiting.gif' alt='' border='0' align='absmiddle' /></div>";
  
  $.ajax({
    url: action,
      type: 'POST',
      cache: false,
      dataType: 'text/html',
      timeout: 4000,
      error: function(){
        
        var contents = document.getElementById("portioncontents");
        contents.innerHTML = "";
        //     alert('ptpResultData ' + ptpResultData);
        document.getElementById("portioncontents").innerHTML = output.join("");
        
      },
    success: function(resultTxt){
      var contents = document.getElementById("portioncontents");
      contents.innerHTML = "";
      // alert('resultTxt ' + resultTxt);
      document.getElementById("portioncontents").innerHTML = resultTxt;
      compareHideUnHide();
      // extract java script
      cssScript = getIndicatorCSSscript(resultTxt);
      // and eval
      eval(cssScript);
    }
  });
  objCritForm.processFlag.value = 'false';
  
  
}


function getIndicatorCSSscript(respText) {
  scriptIndex1 = respText.indexOf("<script>");
  scriptIndex2 = respText.indexOf("}");
  //alert(' scriptIndex1  ' + scriptIndex1 );
  // alert(' scriptIndex2  ' + scriptIndex2 );
  strScript = respText.substring(scriptIndex1+8, scriptIndex2+3);
  // alert(' strScript ' + strScript);
  return strScript;
}


function updatePrice(portionId, priceSelected) {
  
  // get the fare amount selected
  var strPrice =  priceSelected.value;
  strPrice = strPrice.substring(0, strPrice.indexOf('@@'));
  // get the portion index
  var index = portionId.substring(portionId.indexOf("_")+1, portionId.length);
  
  // get current selected fare selection key 
  var strFare =  priceSelected.value;
  //alert(' strFare ' + strFare);
  
  eval('document.portionHeaderForm.portionFareSelected.value="'+strFare+'"');
  
  fareIndex = (strFare.substring(strFare.indexOf('@@')+2, strFare.length));
  //alert(' fareIndex[0] ' + fareIndex[0]);
  // alert(' fareIndex[1] ' + fareIndex[1]);
  fareKeyType = strFare.substring(strFare.length-1, strFare.length);
  // alert(' fareKeyType ' + fareKeyType);
  
  var strVar = portionId + '_PRICE';
  // set the price to hidden variable, which can be used for calculating total
  if(document.portionHeaderForm.PORTION_PRICE.length > 0) {
    eval('document.portionHeaderForm.PORTION_PRICE['+index+'].value='+strPrice);
  } else {
    eval('document.portionHeaderForm.PORTION_PRICE.value='+strPrice);
  }
  
  
  // set selected portion fare to div
  document.getElementById(strVar+'_LABEL').innerHTML="";
  document.getElementById(strVar+'_LABEL').innerHTML=strPrice;
  updateTotalPrice();
}

function updateFareCSS(fareId) {
  
  // var radios = document.forms.PtpResults.elements.fareKey;
  // for(var i=0; i<radios.length; i++){
  //   fareValue =radios[i].value;
  $("input[type='radio']").parent().removeClass();
  $("input[type='radio']").parent().addClass('td-choice-options first');
  
  // }
  $("#" + fareId ).removeClass();
  $("#" + fareId ).addClass('td-choice-options first on');
  
  // change the css in compare classes also
  
}



function updateTotalPrice() {
  var total=0;
  // calculate total
  if(document.portionHeaderForm.PORTION_PRICE.length > 0) {
    for(i=0; i<document.portionHeaderForm.PORTION_PRICE.length; i++) {
      total += parseFloat(document.portionHeaderForm.PORTION_PRICE[i].value);
    }
  } else {
    total += parseFloat(document.portionHeaderForm.PORTION_PRICE.value);
  }
  // set total value
  var strAmount = total+"";
  if (strAmount.indexOf('.') == -1) {
    strAmount = total + '.00'; 
  }
  
  document.getElementById("PORTION_TOTAL_PRICE_LABEL").innerHTML="";
  document.getElementById("PORTION_TOTAL_PRICE_LABEL").innerHTML=strAmount;
  
}

function aafSubmit(formName) {
  var objForm = document.forms[formName];     
  objForm.submit();
}


function editPortion(formName, portionId) {
  // alert('edit button ' + portionId);
  var objForm = document.forms[formName];   
  
  var faresSelected = objForm.faresSelected.value;
  // need to consider all the components
  // whichever component is clicked
  objForm.method='post';
  objForm.action = '/' + getSaleCountry() + '/rail/point_to_point/portionresult.htm?portionId='+portionId+'&faresSelected='+faresSelected+'&strAction=edit';
  objForm.submit();
}

function modifyPortion(formName, portionId) {
  // alert('edit button ' + portionId);
  var objForm = document.forms[formName];   
  objForm.action = '/' + getSaleCountry() + '/rail/point_to_point/portionresult.htm?portionId='+portionId+'&strAction=edit';
  objForm.submit();
}


function addToCart(formName, btn) {
  actionName = '/' + getSaleCountry() + '/rail/point_to_point/add_to_cart.htm';
  var objForm = document.forms[formName];    
  objForm.action = actionName;    
  btn.disabled = true;
  objForm.submit(); 
}

function compareHideUnHide() {
  var faqtable=new switchicon("icongroup", "div") //Limit scanning of switch contents to just "div" elements
    faqtable.setHeader('Close <img src="/redesign/images/img-arrow-collapse.jpg" alt="" />', 'View Fare & Accommodation Detail <img src="/redesign/images/img-arrow-expand.jpg"  alt="" />') //Set header HTML
    faqtable.collapsePrevious(false) //Allow more than 1 content to be open simultanously
    faqtable.setPersist(true, 0) //Enable persistence to remember last switch content states for 7 days
    faqtable.init()
    
  }


String.prototype.trim = function () {
  return this.replace(/^\s*/, "").replace(/\s*$/, "");
}



// on change whenever any component is clicked
// or changed

function checkRTCompatibility(presultForm) {
  
  objResultForm = document.forms[presultForm]; 
  
  
  var portionId = objResultForm.portionId.value;
  var faresSelected = objResultForm.faresSelected.value;
  var resultId = objResultForm.resultId.value;
  var fareKey = document.portionHeaderForm.portionFareSelected.value;
  // need to consider all the components
  // whichever component is clicked
  var params = "fareKey="+fareKey+"&faresSelected="+faresSelected+"&resultId="+resultId+"&portionId="+portionId;
  
  var action = '/' + getSaleCountry() + "/rail/point_to_point/rtcompatibility.htm?"+params;
  result=true;
  var resultTxt = "";
  contents="";
  $.ajax({
    url: action,
      type: 'GET',
      cache: false,
      async: false,
      dataType: 'text/html',
      error: function(){   
        result=false;
      },
    success: function(resultTxt){
      var contents = document.getElementById("rtcompatible");
      contents.innerHTML = "";
      // alert('resultTxt ' + resultTxt);
      document.getElementById("rtcompatible").innerHTML = resultTxt;
      
    }
  });
  
}


function updateShoppingCartTotal(formName, currency) {
  shopForm = document.forms[formName]; 
  
  amount=0;
  if(shopForm.itemFare != undefined) {
    
    if(shopForm.itemFare.length > 0) {
      for(i=0; i<shopForm.itemFare.length; i++) {
        amount += parseFloat(shopForm.itemFare[i].value);
      }
    } else {
      amount += parseFloat(shopForm.itemFare.value);
    }
    var strAmount = amount+"";
    if (strAmount.indexOf('.') == -1) {
      strAmount = amount + '.00'; 
    }
    document.getElementById("total").innerHTML = "";
    document.getElementById("total").innerHTML = currency +''+ strAmount;
  }
  
}


function upgradePortionFare(presultForm, portionId, fareKey) {
  
  objResultForm = document.forms[presultForm]; 
  var resultId = objResultForm.resultId.value;
  var faresSelected = objResultForm.faresSelected.value;
  // need to consider all the components
  // whichever component is clicked
  var params = "fareKey="+fareKey+"&faresSelected="+faresSelected+"&resultId="+resultId+"&portionId="+portionId;
  var action = '/' + getSaleCountry() + "/rail/point_to_point/fareupgrade.htm?"+params;
  objResultForm.action = action;
  objResultForm.submit();
  
}



function upgradeAllPortions(presultForm) {
  
  objResultForm = document.forms[presultForm]; 
  
  var action = '/' + getSaleCountry() + "/rail/point_to_point/fareupgrade.htm?strAction=upgradeall";
  objResultForm.action = action;
  objResultForm.submit();
  
}


function passSubmit(formName, strAction) {
  passForm = document.forms[formName];
  var portionId = passForm.portionId.value;
  var resultId = passForm.resultId.value;
  var faresSelected = passForm.faresSelected.value;
  var fromFare = passForm.fromFareType.value;
  
  var fareKey = '';
  if(passForm.fareKey != undefined) {
    
    if(passForm.fareKey.length > 0) {
      for(i=0; i<passForm.fareKey.length; i++) {
        if(passForm.fareKey[i].checked == true) {
          fareKey = passForm.fareKey[i].value;
          break;
        }
      }
    } else {
      fareKey = passForm.fareKey.value;
    }
  }
  
  
  var params = "strAction="+strAction+"&fareKey="+fareKey+"&faresSelected="+faresSelected+"&resultId="+resultId+"&portionId="+portionId+"&fromFareType="+fromFare;
  alert('params ' + params);
  // return false;
  passForm.action = '/' + getSaleCountry() + '/rail/point_to_point/passresult.htm?'+params;
  passForm.submit();
}


function modifyPass(formName, portionId) {
  // alert('edit button ' + portionId);
  var objForm = document.forms[formName];   
  objForm.action = '/' + getSaleCountry() + '/rail/point_to_point/passresult.htm?editPortionId='+portionId+'&strAction=edit';
  objForm.submit();
}



function editPass(formName, portionId) {
  // alert('edit button ' + portionId);
  var passForm = document.forms[formName];   
  var fromFare = passForm.fromFareType.value;
  
  var faresSelected = passForm.faresSelected.value;
  var fareKey = '';
  if(passForm.fareKey != undefined) {
    
    if(passForm.fareKey.length > 0) {
      for(i=0; i<passForm.fareKey.length; i++) {
        if(passForm.fareKey[i].checked == true) {
          fareKey = passForm.fareKey[i].value;
          break;
        }
      }
    } else {
      fareKey = passForm.fareKey.value;
    }
  }
  
  // need to consider all the components
  // whichever component is clicked
  passForm.method='post';
  passForm.action = '/' + getSaleCountry() + '/rail/point_to_point/passresult.htm?portionId='+portionId+"&fareKey="+fareKey+'&faresSelected='+faresSelected+"&fromFareType="+fromFare+'&strAction=edit';
  passForm.submit();
}



function minimiseAllPassFares(portionId) {
  var checkboxes = document.forms.PtpResults.elements.fareCheckBox;
  rlength = document.forms.PtpResults.fareKey.length; 
  for(var i=0; i<rlength; i++){
    $("#fareCheck"+i).css('display', 'none');  
  }
  
  // update total price  on top
  var index = portionId.substring(portionId.indexOf("_")+1, portionId.length);
  index = parseInt(index) + 1;;
  strPrice="0.00";
  var strVar = portionId + '_PRICE';
  // set the price to hidden variable, which can be used for calculating total
  if(document.passHeaderForm.PORTION_PRICE.length > 0) {
    eval('document.passHeaderForm.PORTION_PRICE['+index+'].value='+strPrice);
  } else {
    eval('document.passHeaderForm.PORTION_PRICE.value='+strPrice);
  }
  // set selected portion fare to div
  document.getElementById(strVar+'_LABEL').innerHTML="";
  document.getElementById(strVar+'_LABEL').innerHTML=strPrice;
  updatePassTotalPrice();
  
}

function updatePassPrice(portionId, priceSelected) {
  
  // get the fare amount selected
  var strPrice =  priceSelected.value;
  
  
  strPrice = strPrice.substring(0, strPrice.indexOf('@@'));
  // get the portion index
  var index = portionId.substring(portionId.indexOf("_")+1, portionId.length);
  index = parseInt(index) + 1;;
  // get current selected fare selection key 
  var strFare =  priceSelected.value;
  
  
  eval('document.passHeaderForm.portionFareSelected.value="'+strFare+'"');
  
  fareIndex = (strFare.substring(strFare.indexOf('@@')+2, strFare.length));
  //alert(' fareIndex[0] ' + fareIndex[0]);
  // alert(' fareIndex[1] ' + fareIndex[1]);
  fareKeyType = strFare.substring(strFare.length-1, strFare.length);
  // alert(' fareKeyType ' + fareKeyType);
  
  if(priceSelected.checked == false) {
    strPrice = "0";
  }
  
  var strVar = portionId + '_PRICE';
  // set the price to hidden variable, which can be used for calculating total
  if(document.passHeaderForm.PORTION_PRICE.length > 0) {
    eval('document.passHeaderForm.PORTION_PRICE['+index+'].value='+strPrice);
  } else {
    eval('document.passHeaderForm.PORTION_PRICE.value='+strPrice);
  }
  
  
  // set selected portion fare to div
  document.getElementById(strVar+'_LABEL').innerHTML="";
  document.getElementById(strVar+'_LABEL').innerHTML=strPrice;
  updatePassTotalPrice();
}


function updatePassTotalPrice() {
  var total=0;
  // calculate total
  
  if(document.passHeaderForm.PORTION_PRICE.length > 0) {
    for(i=0; i<document.passHeaderForm.PORTION_PRICE.length; i++) {
      total += parseFloat(document.passHeaderForm.PORTION_PRICE[i].value);
    }
  } else {
    total += parseFloat(document.passHeaderForm.PORTION_PRICE.value);
  }
  // set total value
  var strAmount = total+"";
  if (strAmount.indexOf('.') == -1) {
    strAmount = total + '.00'; 
  }
  document.getElementById("PORTION_TOTAL_PRICE_LABEL").innerHTML="";
  document.getElementById("PORTION_TOTAL_PRICE_LABEL").innerHTML=strAmount;
  
}

function addPassToCart(formName) {
  actionName = '/' + getSaleCountry() + '/shopping/add_pass_selector_product.htm';
  var objForm = document.forms[formName];    
  objForm.action = actionName;
  return false;
}


function getPassHolderEarlierTrain(presultForm, strAction) {
  var passForm = document.forms[presultForm];  
  minTime = passForm.minTime.value;
  maxTime = passForm.maxTime.value;  
  resultId = passForm.resultId.value;  
  passForm.action = '/' + getSaleCountry() + '/rail/point_to_point/passholdercriteria.htm?'+'strAction='+strAction+'&minTime='+minTime+'&maxTime='+maxTime+'&resultId='+resultId;     
  passForm.submit();
}


function getPassReservationEarlierLaterTrain(presultForm, strAction) {
  var passForm = document.forms[presultForm];  
  minTime = passForm.minTime.value;
  maxTime = passForm.maxTime.value;  
  resultId = passForm.resultId.value;  
  portionId = passForm.portionId.value;  
  
  var fromFare = passForm.fromFareType.value;
  
  var faresSelected = passForm.faresSelected.value;
  var fareKey = '';
  if(passForm.fareKey != undefined) {
    
    if(passForm.fareKey.length > 0) {
      for(i=0; i<passForm.fareKey.length; i++) {
        if(passForm.fareKey[i].checked == true) {
          fareKey = passForm.fareKey[i].value;
          break;
        }
      }
    } else {
      fareKey = passForm.fareKey.value;
    }
  }
  
  passForm.action = '/' + getSaleCountry() + '/rail/point_to_point/passresult.htm?'+'strAction='+strAction+'&minTime='+minTime+'&maxTime='+maxTime+'&resultId='+resultId+'&portionId='+portionId+"&fareKey="+fareKey+'&faresSelected='+faresSelected+"&fromFareType="+fromFare;     
  passForm.submit();
  
}


//For the Home page
function roundTripCheckHome(rVal) {
  
  if(rVal.value == 0) {  
    fromCity = document.form1.from0.value;
    toCity = document.form1.to0.value;
    deptdate = document.form1.deptDate0.value;     
    document.form1.from0.value=fromCity;
    document.form1.to0.value=toCity;
    document.form1.deptDate0.value=deptdate;     
    document.form1.from1.value='';
    document.form1.to1.value='';
    document.form1.deptDate1.disabled =true;     
    
    $('#deptDate0').datePicker().val($('#deptDate0').val()).trigger('change');
    $('#deptDate1').addClass("dp-disabled"); 
    //$('#deptDate1').dpSetDisabled(true);
  }
  
  // if rVal is 1, populate the From1 and To1 in second trip
  if(rVal.value == 1) { 
    var output = new Array(); 
    // get trip 1 details
    fromCity = document.form1.from0.value;
    toCity = document.form1.to0.value;
    deptdate = document.form1.deptDate0.value;
    time = document.form1.time0.value;
    
    // assign trip 1 values
    document.form1.from0.value=fromCity;
    document.form1.to0.value=toCity;
    document.form1.deptDate0.value=deptdate;
    document.form1.time0.value=time;
    
    // assign trip 2 values
    document.form1.from1.value=toCity;   
    document.form1.to1.value=fromCity;   
    document.form1.deptDate1.disabled =false;
    document.form1.deptDate1.value=deptdate;
    
    $('#deptDate0').datePicker().val($('#deptDate0').val()).trigger('change');
    $('#deptDate1').dpSetDisabled(false);
    $('#deptDate1').datePicker().val($('#deptDate1').val()).trigger('change');
    
    updateRoundTripDate();
    
    $('#deptDate0').attr({ 
      onBlur: "updateRoundTripDate();"
      });
  } 
}

function getNextMonthDate() {  
  var myDate=new Date();
  myDate.setMonth(myDate.getMonth()+1);
  
  month = parseInt(myDate.getMonth())+1;
  day   = myDate.getDate();
  year  = myDate.getFullYear();
  mm    = month>9?month:"0"+month;
  dd    = (day>9?day:"0"+day);
  newDate = mm +"/"+ dd +"/"+year;
  
  return newDate;
}


function populateRoundTripHome() {
  var objForm =  document.form1;
  indexVal=0;
  for(i=0; i<objForm.roundtrip.length; i++) {
    
    if(objForm.roundtrip[i].checked == true) {     
      indexVal = objForm.roundtrip[i].value;    
      break;
    }
  }
  if(indexVal == 1) { 
    
    fromCity = document.form1.from0.value;
    toCity = document.form1.to0.value;
    
    // get trip 1 details
    document.form1.from1.value = toCity;  
    document.form1.to1.value   = fromCity;  
    
  }
  
  nA = document.form1.nA.value;
  if(nA.trim().length == 0) {
    document.form1.nA.value='0';
  }
  
  nC = document.form1.nC.value;
  if(nC.trim().length == 0) {
    document.form1.nC.value='0';
  }
  
  nY = document.form1.nY.value;
  if(nY.trim().length == 0) {
    document.form1.nY.value='0';
  }
  
  nS = document.form1.nS.value;
  if(nS.trim().length == 0) {
    document.form1.nS.value='0';
  }
}

$(document).ready(function() {
  var objForm =  document.form1;
  if (objForm && objForm.roundtrip) {
    indexVal=0;
    for(i=0; i<objForm.roundtrip.length; i++) {
      if(objForm.roundtrip[i].checked == true) {     
        indexVal = objForm.roundtrip[i].value;    
        break;
      }
    }
    if(indexVal == 0) { 
      if (objForm.from1) {
        objForm.from1.value='';
        objForm.to1.value='';
        objForm.deptDate1.disabled =true;    
        $('#deptDate1').addClass("dp-disabled"); 
      }
    }
    else if(indexVal == 1) { 
      if (objForm.from1) {
        objForm.from1.value=objForm.to0.value;
        objForm.to1.value=objForm.from0.value;
        objForm.deptDate1.disabled =false;    
        $('#deptDate1').removeClass("dp-disabled");  
      }
    }
  }
});

//Code to setup initialization of trip summary carousel.
$(document).ready(function() {
  var CAROUSEL_DISPLAY_SIZE = 3;           
  var $carouselItems = $('ul#ptp_carousel > li');
  var size = $carouselItems.size();
  if (size > CAROUSEL_DISPLAY_SIZE) { 
    var selectedItem = $('li#current-trip').get(0);
    var index = $carouselItems.index(selectedItem);
    var start = index - Math.floor(CAROUSEL_DISPLAY_SIZE/2);
    if (start < 0) start = 0;
    if (start > size - CAROUSEL_DISPLAY_SIZE) {
      start = size - CAROUSEL_DISPLAY_SIZE;
    }
    jQuery('#ptp_carousel').jcarousel({
      vertical: true,
        scroll: 1,
        visible: CAROUSEL_DISPLAY_SIZE,
        start: start + 1  //jcarousel uses 1-based indexes
      });
  }
});




// This function is being replicated to make the # of rows for Multi City to be 3 

function howToRoundTripCheck() {
  
  for(j=0; j<3; j++) {
    if(document.form1.roundtrip[j] != undefined && document.form1.roundtrip[j].checked == true  ) {
      rVal = j;   
    }
  }
  amtrakReq=0;
  if(document.form1.isAmtrakRequest != undefined) {
    amtrakReq=document.form1.isAmtrakRequest.value;
  }
  
  var anotherTrip = document.getElementById("add-another-trip");
  if(rVal  == 0) {   
    fromCity = document.form1.from0.value;
    toCity = document.form1.to0.value;
    deptdate = document.form1.deptDate0.value;
    time = document.form1.time0.value;
    var contents = document.getElementById("trip-frame");
    contents.innerHTML = "";
    document.getElementById("trip-frame").innerHTML = addPortionTrip(0,0, amtrakReq);
    document.form1.from0.value=fromCity;
    document.form1.to0.value=toCity;
    document.form1.deptDate0.value=deptdate;
    document.form1.time0.value=time;
    document.form1.rows.value=1;
    anotherTrip.innerHTML="";
    $('#deptDate0').datePicker().val($('#deptDate0').val()).trigger('change');
    $('#deptDate0').dpSetEndDate(new Date().addDays(365).asString()); 
  }
  
  // if rVal is 1, populate the From1 and To1 in second trip
  if(rVal  == 1) {    
    
    var output = new Array();
    
    // NEW
    output.push("<div class='trip-search-field-cars' id='trip-search-field-cars'></div>");
    
    // get trip 1 details
    fromCity = document.form1.from0.value;
    toCity = document.form1.to0.value;
    deptdate = document.form1.deptDate0.value;
    time = document.form1.time0.value;;
    
    var contents = document.getElementById("trip-frame");
    // clear the trips
    contents.innerHTML="";
    
    // add trip 1
    contents.innerHTML  = contents.innerHTML + addPortionTrip(0,1, amtrakReq);
    // add trip 2 - round trip
    contents.innerHTML  = contents.innerHTML + addRoundTrip(1, amtrakReq) + output.join("");
    
    // assign trip 1 values
    document.form1.from0.value=fromCity;
    document.form1.to0.value=toCity;
    document.form1.deptDate0.value=deptdate;
    document.form1.time0.value=time;
    
    
    // assign trip 2 values
    document.form1.from1.value=toCity;
    document.form1.fromCity1.value=toCity;
    document.form1.to1.value=fromCity;
    document.form1.toCity1.value=fromCity;
    document.form1.deptDate1.value=deptdate;     
    document.form1.fromCity1.disabled=true;
    document.form1.toCity1.disabled=true;  
    
    anotherTrip.innerHTML="";
    document.form1.rows.value=2;
    $('#deptDate0').datePicker().val($('#deptDate0').val()).trigger('change');
    $('#deptDate1').datePicker().val($('#deptDate1').val()).trigger('change');
    $('#deptDate0').dpSetEndDate(new Date().addDays(365).asString());
    $('#deptDate1').dpSetEndDate(new Date().addDays(365).asString());
    validateRTDate();
    updateRoundTripDate();
    
    $('#deptDate0').attr({ 
      onChange: "updateRoundTripDate();"
      });
    
  }
  
  if(rVal  == 2) {
    
    rows = document.form1.rows.value;
    
    if(rows == undefined) {
      rows = 3;
    }
    if(rows < 3) {
      rows = 3;
    }
    fromCityArray = new Array(rows);
    toCityArray  = new Array(rows);
    deptDateArray  = new Array(rows);
    deptTimeArray  = new Array(rows);
    for(i=0; i<rows; i++){
      if(eval('document.form1.from'+i) != undefined) {
        from = eval('document.form1.from'+i+'.value');
        //  alert(' from ' + from);
        if(from != undefined) {
          fromCityArray[i] = from;
        }
      } else {
        fromCityArray[i] = '';
      }
      
      if(eval('document.form1.to'+i) != undefined) {
        to = eval('document.form1.to'+i+'.value');
        //  alert(' to ' + to);
        if(to != undefined) {
          toCityArray[i] = to;
        }
      } else {
        toCityArray[i] = '';
      }
      
      if(eval('document.form1.deptDate'+i) != undefined) {
        ddate = eval('document.form1.deptDate'+i+'.value');
        //  alert(' ddate ' + ddate);
        if(ddate != undefined) {
          deptDateArray[i] = ddate;
        }
      } else {
        deptDateArray[i]='';
      }
      
      
      if(eval('document.form1.time'+i) != undefined) {
        dtime = eval('document.form1.time'+i+'.value');
        //  alert(' dtime ' + dtime);
        if(dtime != undefined) {
          deptTimeArray[i] = dtime;
        } 
      } else {
        deptTimeArray[i] = '';
      }
      
    }
    
    //   fromCity = document.form1.from0.value;
    //   toCity = document.form1.to0.value;
    //   deptdate = document.form1.deptDate0.value;
    //   time = document.form1.time0.value;
    
    var contents = document.getElementById("trip-frame");
    contents.innerHTML = "";
    document.getElementById("trip-frame").innerHTML = addPortionTrip(0,2, 0);
    for(i=1; i<rows; i++){
      contents.innerHTML  = contents.innerHTML + addPortionTrip(i,2, 0);
    }
    
    for(i=0; i<rows; i++){
      // eval('document.form1.from'+i+'.value='+fromCityArray[i]);
      $('#from'+i).val(fromCityArray[i]);
      $('#to'+i).val(toCityArray[i]);
      //if(deptDateArray[i] != '') {       
      //$('#deptDate'+i).datePicker().val(deptDateArray[i]).trigger('change');
      // } else {
	     if(deptDateArray[i] != '') {
	        $('#deptDate'+i).datePicker().val(deptDateArray[i]).trigger('change');
	      } else {
	        $('#deptDate'+i).datePicker().val($('#deptDate'+(i-1)).val()).trigger('change');
	      }
      // }
      if(deptTimeArray[i] != '') {
        $('#time'+i).val(deptTimeArray[i]);
      }
      
      //eval('document.form1.to'+i+'.value='+toCityArray[i]);
      //eval('document.form1.deptDate'+i+'.value='+deptDateArray[i]);
      //eval('document.form1.time'+i+'.value='+deptTimeArray[i]);
      
      $('#deptDate'+i).dpSetEndDate(new Date().addDays(365).asString());
    }
    
    var output = new Array();
    output.push("<a href='javascript:addAnotherTrip();'><img src='/redesign/images/img-plus.jpg' class='float-left' alt='Add Another Trip'/></a>");
    output.push("<div><a href='javascript:addAnotherTrip();'>Add another trip</a></div>");
    
    
    anotherTrip.innerHTML = output.join("");
    document.form1.rows.value=rows;
    // for(i=0; i<rows; i++){
    //   $('#deptDate'+i).datePicker().val(getNextMonthDate()).trigger('change');
    //   $('#deptDate'+i).dpSetEndDate(new Date().addDays(365).asString());
    //}
  }
  reinitialize_cities();  //NK  
  return false;
}


