/* Modifications to this file need to be implemented in the same way
   in the sprocket_search_panel.js file. */

/* Return an object with current search panel values, for transfer to another search panel */
function buildParameters() {
  function normalizeDuration(v) {
    return (v == 'Long Stay') ? 29 :
      (v == '2 to 4') ? 2 :
      (v == '10 or 11') ? 10 :
      (v == '21 to 28') ? 21 :
      (+v || 7);
  }
  function childAges(values) {
    var numChildren = +$F('NoChildren'), numInfants = +$F('NoInfants'), ages = [], i = 0;
    for (; i < numChildren; i++) {
      ages.push(values['childAge' + (i + 1)] = $F('child' + i));
    }
    while (numInfants--) {
      ages.push(values['childAge' + (++i)] = 0);
    }
    return ages;
  }
  function normalizeRating(v) {
    return (v == 'TOR_1') ? '2' :
      (v == 'TOR_2') ? '3' :
      (v == 'TOR_3') ? '4' :
      'Any';
  }
  function normalizeBudget() {
    function pounds(string) { return +string.replace(/[^0-9]/g, '') }
    var select = $('holiday_budget');
    var range = select.options[select.selectedIndex].text.split(' - ');
    return (range.length == 1) ? pounds(range[0]) :
      Math.ceil( ( pounds(range[0]) + pounds(range[1]) ) / 2 );
  }
  var values = {};
  values.departureDate = Dates.iso($F('calendar_day') + '/' + $F('calendar_month_year'));
  if ($F('SearchDuration') != 'Any') {
    values.duration = normalizeDuration($F('SearchDuration'));
  }
  values.dateCrossSell = +$F('departureDateExactly');
  values.departureAirport = $F('SearchAirport');
  values.numAdults = $F('NoAdults');
  values.numChildren = +$F('NoChildren') + +$F('NoInfants');
  var ages = childAges(values);
  if ($('familyroom').checked) {
    values.numRooms = 1;
    values.room1Adults = values.numAdults;
    values.room1Children = ages.join('|');
  }
  var destination = $F('SearchLocation'); // 3-letter-code (3LC) or a combo: 3LC,6LC
  var resort = $F('SearchResort'); // 6LC
  if (resort != 'Any') {
    values.location = resort;
  } else if (destination != 'Any') {
    values.location = destination.substring(destination.indexOf(',') + 1);
  }
  var unit = $F('SearchAccommodation');
  if (unit && unit != 'Any') {
    values.unit = unit;
  }
  var brochure = $F('SearchBrochure');
  if (brochure && brochure != 'Any') {
    values.brochure = brochure;
  }
  values.priority = $F('priority');
  values.boardBasis = $F('board_basis');
  values.rating = normalizeRating($F('tour_operator_rating'));
  if ($F('holiday_budget') != 'ANY') {
    values.budget = normalizeBudget();
  }
  return values;
}
