/* ========================================================================== */
/*                                 Modify Search                              */
/* ========================================================================== */
function toggleDropOffLocation()
{
  var sameDropOff = document.CarSearchForm.SameDropOffLoc.value;
  if(sameDropOff == 'false')
  {
    document.CarSearchForm.SameDropOffLoc.value = 'true'
  }
  else
  {
    document.CarSearchForm.SameDropOffLoc.value = 'false'
  }
  
  displayHideDifferentDropOffLocation();
}

function displayHideDifferentDropOffLocation()
{
  //  Get the current value
  var sameDropOff = document.CarSearchForm.SameDropOffLoc.value;
  if( sameDropOff == null  || sameDropOff == 'true' || sameDropOff == '' )
  {
    document.getElementById("dropOffLocationHrefShow").style.display = '';
    document.getElementById("dropOffLocationHrefHide").style.display = 'none';
    document.getElementById("dropOffLocation").style.display = 'none';
    
    if(document.CarSearchForm.CarDropOffLocationStage1 != null)
    {
      document.CarSearchForm.CarDropOffLocationStage1.value='';
    }
    if(document.CarSearchForm.CarDropOffLocationStage2 != null)
    {
      document.CarSearchForm.CarDropOffLocationStage2.value='';
    }
    if(document.CarSearchForm.CarDropOffLocationStage3 != null)
    {
      document.CarSearchForm.CarDropOffLocationStage3.value='';
    }
    if(document.CarSearchForm.CarDropOffLocationStage4 != null)
    {
      document.CarSearchForm.CarDropOffLocationStage4.value='';
    }
  }
  else
  {
    document.getElementById("dropOffLocationHrefShow").style.display = 'none';
    document.getElementById("dropOffLocationHrefHide").style.display = '';
    document.getElementById("dropOffLocation").style.display = 'block';
    
    correctDescendantLabelSize(document.getElementById('dropOffLocation'));
  }
  
  correctFormNo();
}


function setPickUpAddress(chosenPosition)
{
  if(chosenPosition != '' && chosenPosition != 'undefined' || chosenPosition == 0)
  {
    document.CarSearchForm.pickUpAddress.value = pu_Criteria[chosenPosition] ;	
  }
  return false;
}

function setPickUpDMAddress(chosenPosition) {
  if ((chosenPosition != '') && (chosenPosition != 'undefined') || (chosenPosition == 0)) {
    document.CarSearchForm.pickUpDMAddress.value = pu_CriteriaDM[chosenPosition];
  }
  return false;
}

function setDropOffDMAddress(chosenPosition) {
  if ((chosenPosition != '') && (chosenPosition != 'undefined') || (chosenPosition == 0)) {
    document.CarSearchForm.dropOffDMAddress.value = do_CriteriaDM[chosenPosition];
  }
  return false;
}


function setPickUpCountry(chosenPosition)
{
  if(chosenPosition != '' && chosenPosition != 'undefined' || chosenPosition == 0)
  {
    document.CarSearchForm.pickUpCountry.value = pu_Countries[chosenPosition] ;
    //alert('Setting :' + document.CarSearchForm.pickUpCountry.value);	
    // Set the state code if the country is Canada, clear it otherwise
    if(  pu_Countries[chosenPosition] = 'CA' )
    {
      document.CarSearchForm.pickUpState.value = pu_States[chosenPosition] ;
    }
    else
    {
      document.CarSearchForm.pickUpState.value = '' ;
    }
  }
  
  return false;
}

function setDropOffAddress(chosenPosition)
{
  if(chosenPosition != '' && chosenPosition != 'undefined' || chosenPosition == 0)
  {
    document.CarSearchForm.dropOffAddress.value = do_Criteria[chosenPosition] ;	
  }
  return false;
}

function setDropOffCountry(chosenPosition)
{
  if(chosenPosition != '' && chosenPosition != 'undefined' || chosenPosition == 0)
  {
    document.CarSearchForm.dropOffCountry.value = do_Countries[chosenPosition] ;
    //alert('Setting :' + document.CarSearchForm.dropOffCountry.value);	
   
    // Set the state code if the country is Canada, clear it otherwise
    if(  do_Countries[chosenPosition] = 'CA' )
    {
       document.CarSearchForm.dropOffState.value = do_States[chosenPosition] ;
    }
    else
    {
      document.CarSearchForm.dropOffState.value = '' ;
    }
  }
  
  return false;
}


function toggleRefineSearch()
{
  var carRefineSearch = document.CarSearchForm.carRefineSearch.value;
  if(carRefineSearch == 'false')
  {
    document.CarSearchForm.carRefineSearch.value = 'true'
  }
  else
  {
    document.CarSearchForm.carRefineSearch.value = 'false'
  }
  
  displayHideRefineSearch();
}

function displayHideRefineSearch()
{
  //  Get the current value 
  var carRefineSearch = document.CarSearchForm.carRefineSearch.value;

  if( carRefineSearch == null  || carRefineSearch == 'false' || carRefineSearch == '' )
  {
    //  Hide Preferences
    document.getElementById("optionsRow").style.display = 'none';
    document.getElementById('refineSearchHrefHide').style.display = 'none';
    document.getElementById('refineSearchHrefShow').style.display = 'block';
    
    //  Clear all the refine search params
    document.CarSearchForm.CompanyCode.value='NP';
    document.CarSearchForm.Transmission.selectedIndex=0;
    document.CarSearchForm.rentalCost.value='';
    document.CarSearchForm.carType.value='all';
  }
  else
  {
    // Show Preferences
    document.getElementById("optionsRow").style.display = 'block';
    document.getElementById('refineSearchHrefHide').style.display = 'block';
    document.getElementById('refineSearchHrefShow').style.display = 'none';
    
    correctDescendantLabelSize(document.getElementById('optionsRow'));
  }
  
  correctFormNo();
}


function setLocationStage1()
{
  var chosenPickUpValue = null;
  var chosenDropOffValue = null;

  // Pick up
  if( document.CarSearchForm.CarSearchLocationStage4 != null && document.CarSearchForm.CarSearchLocationStage4.value != null && document.CarSearchForm.CarSearchLocationStage4.value != '' )
  {
    chosenPickUpValue = document.CarSearchForm.CarSearchLocationStage4.options[document.CarSearchForm.CarSearchLocationStage4.selectedIndex].text;
  }
  else if( document.CarSearchForm.CarSearchLocationStage3 != null && document.CarSearchForm.CarSearchLocationStage3.value != null &&  document.CarSearchForm.CarSearchLocationStage3.value != '' )
  {
    chosenPickUpValue = document.CarSearchForm.CarSearchLocationStage3.options[document.CarSearchForm.CarSearchLocationStage3.selectedIndex].text;
  }
  else if( document.CarSearchForm.CarSearchLocationStage2 != null &&document.CarSearchForm.CarSearchLocationStage2.value != null && document.CarSearchForm.CarSearchLocationStage2.value != '' )
  {
    chosenPickUpValue = document.CarSearchForm.CarSearchLocationStage2.options[document.CarSearchForm.CarSearchLocationStage2.selectedIndex].text;
  }
  else if( document.CarSearchForm.CarSearchLocationStage1 != null && document.CarSearchForm.CarSearchLocationStage1.value != null && document.CarSearchForm.CarSearchLocationStage1.value != '' )
  {
    chosenPickUpValue = document.CarSearchForm.CarSearchLocationStage1.value;
  }
  
  //  Drop off 
  if( document.CarSearchForm.CarDropOffLocationStage4 != null && document.CarSearchForm.CarDropOffLocationStage4.value != null && document.CarSearchForm.CarDropOffSLocationStage4.value != '' )
  {	
    chosenDropOffValue  = document.CarSearchForm.CarDropOffLocationStage4.options[document.CarSearchForm.CarDropOffSLocationStage4.selectedIndex].text;
  }
  else if( document.CarSearchForm.CarDropOffLocationStage3 != null && document.CarSearchForm.CarDropOffLocationStage3.value != null &&  document.CarSearchForm.CarDropOffLocationStage3.value != '' )
  {	
    chosenDropOffValue  = document.CarSearchForm.CarDropOffLocationStage3.options[document.CarSearchForm.CarDropOffLocationStage3.selectedIndex].text;
  }
  else if( document.CarSearchForm.CarDropOffLocationStage2 != null &&document.CarSearchForm.CarDropOffLocationStage2.value != null && document.CarSearchForm.CarDropOffLocationStage2.value != '' )
  {	
    chosenDropOffValue  = document.CarSearchForm.CarDropOffLocationStage2.options[document.CarSearchForm.CarDropOffLocationStage2.selectedIndex].text;
  }
  else if( document.CarSearchForm.CarDropOffLocationStage1 != null && document.CarSearchForm.CarDropOffLocationStage1.value != null && document.CarSearchForm.CarDropOffLocationStage1.value != '' )
  {
    chosenDropOffValue = document.CarSearchForm.CarDropOffLocationStage1.value;
  }

  if(chosenPickUpValue != null)
  {
    //  Set the search input field to display the value chosen if they have selected from a select box.
    document.CarSearchForm.CarSearchLocationStage1.value = chosenPickUpValue;
  }
  
  if( document.CarSearchForm.CarDropOffLocationStage1 != null && document.CarSearchForm.CarDropOffLocationStage1.value != '')
  {
    document.CarSearchForm.CarDropOffLocationStage1.value = chosenDropOffValue;
  }
}

function onCalendarChoose(theElement)
{
  if(  theElement.id == 'PickupDateStrSpan')
  {
    //  Move focus to the pick up time field when the user has chosen a date
    document.CarSearchForm.PickupHour.focus();
    
    
    // If the dropoff date is on the same day or earlier than the pickup date,
    // set it to one day later than the pickup date
    var pickupDate = new Date(document.CarSearchForm.PickupYear.value, document.CarSearchForm.PickupMonth.value - 1, document.CarSearchForm.PickupDay.value);
    var dropoffDate = new Date(document.CarSearchForm.DropoffYear.value, document.CarSearchForm.DropoffMonth.value - 1, document.CarSearchForm.DropoffDay.value);
    
    var ONE_DAY_MILLIS = 24 * 60 * 60 * 1000;
    if(dropoffDate.getTime() - pickupDate.getTime() < ONE_DAY_MILLIS)
    {
      dropoffDate.setTime(pickupDate.getTime() + ONE_DAY_MILLIS);
      document.CarSearchForm.DropoffDateStr.value = strftime(date_format,dropoffDate);
      document.CarSearchForm.DropoffYear.value = dropoffDate.getFullYear();
      document.CarSearchForm.DropoffMonth.value = dropoffDate.getMonth() + 1;
      document.CarSearchForm.DropoffDay.value = dropoffDate.getDate();
      document.CarSearchForm.DropoffDate.value = dropoffDate.getFullYear() + "-" + zeroPad((dropoffDate.getMonth() + 1)) + "-" + zeroPad(dropoffDate.getDate());
    }
  }
  else
  {
    document.CarSearchForm.DropoffHour.focus();
  }
}

function zeroPad(intValue)
{
  var intStr = "" + intValue;
  if(intStr.length == 1)
    intStr = "0" + intStr;
  
  return intStr;
}


/* ========================================================================== */
/*                                 Search Results                             */
/* ========================================================================== */
function toggleCarDisplay( carID )
{
  currentValue = document.getElementById('cars_'+carID).style.display;
  
  if( currentValue == 'none' )
  {
    // Show Cars
    document.getElementById('cars_'+carID).style.display = '';
    document.getElementById('carLink_'+carID+'_show').style.display = 'none';
    document.getElementById('carLink_'+carID+'_hide').style.display = '';
  }
  else
  {
    //  Hide Cars
    document.getElementById('cars_'+carID).style.display = 'none';
    document.getElementById('carLink_'+carID+'_show').style.display = '';
    document.getElementById('carLink_'+carID+'_hide').style.display = 'none';
  }
}


function toggleModifySearch() {
 var searchDiv = document.getElementById('carSearchContainer');
 if (searchDiv.style.display != '') {
   searchDiv.style.display = '';
   document.getElementById('modifySearchLink_show').style.display = 'none';
   document.getElementById('modifySearchLink_hide').style.display = '';
   document.getElementById('searchSummary').style.display = 'none';
   correctLabelSize();
 }
 else {
   searchDiv.style.display = 'none';
   document.getElementById('modifySearchLink_show').style.display = '';
   document.getElementById('modifySearchLink_hide').style.display = 'none';
   document.getElementById('searchSummary').style.display = '';
 }
}

