// SET-UP ITEMS

// PICK-UP SECTION
var pickup_location_visible_ids = {'San Juan del Sur': ['rental_prop_Specific_Location_SJ', 'rental_prop_Area_of_SJdS_'],
                        'Managua (airport)': ['rental_prop_Specific_Location_MA'],
                        'Granada': ['rental_prop_Specific_Location_GA']};
var pickup_location_ids = ['rental_prop_Specific_Location_SJ', 'rental_prop_Specific_Location_MA', 'rental_prop_Specific_Location_GA', 'rental_prop_Area_of_SJdS_'];
var pickup_location_time_offsets = {'In Town': 0,
'Maderas cutoff (Mango Rosa - Los Miradores)': 20,
'Maderas/Marsella area': 30,
'Nacascolo area': 15,
'Coco beach area': 45,
'Remanso area': 20,
'Yankee area': 45,
'Escamequita area': 35,
'Hostel San Angel': -5,
'Hotel Bohemian Paradise': -10,
'Hotel Casa Capricho': -10,
'Hotel Casa San Francisco': -5,
'Hotel Casa San Martin': -5,
'Hotel Casa Vivaldi': -5,
'Hotel Cocibolca': -5,
'Hotel El Club': -10,
'Hotel El Maltese': -10,
'Hotel Granada': -5,
'Hotel Kekoldi': -5,
'Hotel La Casona de los Estrada': -10,
'Hotel Le Pérgola': -5,
'Hotel Nuestra Casa': -10,
'Hotel Patio del Malinche': -5, 
'Hotel Posada de Don Alfredo': -10,
'Camino Real': -5,
'Best Western Las Mercedes': -5,
'Hilton Princess': -30,
'Intercontinental': -30,
'Holiday Inn Select': -30,
'La Pyrámide': -30,
'Los Balcones': -30,
'Metrocentro': -30,
'UCA': -30,
'Villa Fontana': -30};
                                  
var northbound_routes = {'Granada to Managua (airport)': 1,
                         'San Jorge (Rivas + Ometepe) to Granada': 1,
                         'San Jorge (Rivas + Ometepe) to Managua (airport)': 1};
var pickup_times = {'San Juan del Sur': ['3:00 am', '3:30 am','4:00 am','4:30 am','5:00 am','5:30 am','6:00 am','8:45 am','12:30 pm'],
                    'Managua (airport)': ['10:00 am', '1:15 pm', '4:45 pm', '8:15 pm', '9:45 pm'],
                    'Granada': ['2:00 pm', '5:30 pm'],
                    'Granada North': ['1:00 am', '2:00 pm', '4:00 pm'],
                    'San Jorge (Rivas + Ometepe)' : ['11:15 am', '2:30 pm', '6:00 pm'],
                    'San Jorge (Rivas + Ometepe) North' : ['1:00 am', '3:30 pm', '7:00 pm'],
                    'Peñas Blancas (Costa Rico border)': ['8:00 am', '10:00 am', '12:00 pm', '2:00 pm', '4:30 pm']};

// DESTINATION SECTION
var destination_location_visible_ids = {'San Juan del Sur': ['rental_prop_Area_of_SJDS', 'rental_prop_Specific_Destination_SJ'],
                                        'Managua (airport)': ['rental_prop_Specific_Destination_MA'],
                                        'Granada': ['rental_prop_Specific_Destination_GA']};
var destination_location_ids = ['rental_prop_Area_of_SJDS', 'rental_prop_Specific_Destination_SJ', 'rental_prop_Specific_Destination_MA', 'rental_prop_Specific_Destination_GA'];

var suv_only_items = ['Playa Gigante', 'Hacienda Iguana', 'Rancho Santana', 'Popoyo'];
var suv_warning_msg = "This specific pickup location or destination is accessible only by SUV for the price shown.";
// END OF SET-UP ITEMS

function planyo_get_param (name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp (regexS);
  var results = regex.exec (window.location.href);
  if (results == null)
    return null;
  else
    return decodeURIComponent(results[1]).replace(/\+/g, ' ');
}

function time_str_to_minutes(s) {
  var hour_sep_pos = s.indexOf(':');
  if (hour_sep_pos > 0) {
    var time_int = parseInt(s.substring(0, hour_sep_pos)) * 60 + parseInt(s.substring(hour_sep_pos + 1, hour_sep_pos + 3));
    if (time_int >= 12*60)
      time_int -= 12*60;
    if (s.substring(hour_sep_pos+3).indexOf('pm') != -1) {
      time_int += 12*60;
    }
    return time_int;
  }
  return null;
}

function time_minutes_to_str(t) {
  var hours = Math.floor(t / 60);
  var minutes = t % 60;
  var pm = false;
  if (hours >= 12) {
    if (hours > 12)
      hours -= 12;
    pm = true;
  }
  if (hours == 0)
    hours = 12;
  if (minutes == 0)
    minutes = '00';
  else if (minutes < 10)
    minutes = '0'+minutes;
  return hours+':'+minutes+' '+(pm ? 'pm' : 'am');
}

function in_arr(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    }
    return false;
}

function on_route_change() {
    var warning_msg = '';
    var vehicle_enabled_value = '';
    if (($('#rental_prop_Pickup_City_or_Area').val() == 'San Juan del Sur' && in_arr($('#rental_prop_Specific_Location_SJ').val(), suv_only_items)) ||
        ($('#rental_prop_Destination_City_or_Area').val() == 'San Juan del Sur' && in_arr($('#rental_prop_Specific_Destination_SJ').val(), suv_only_items))) {
        warning_msg = suv_warning_msg;
        $('#rental_prop_Type_of_Vehicle').val('SUV');
        //vehicle_enabled_value = 'disabled';
    }        
    if (document.getElementById('additional_info_element'))
        document.getElementById('additional_info_element').innerHTML = warning_msg;
    //if (document.getElementById('rental_prop_Type_of_Vehicle'))
    //    document.getElementById('rental_prop_Type_of_Vehicle').readonly = vehicle_enabled_value;
}

function update_pickup_trip_times() {
  var resource_id = planyo_get_param('resource_id');
  if (!resource_id)
    resource_id = document.resource_id;
  on_route_change();
  if (resource_id == 6152)
    return;

  var pickup_city;
  var destination_city;
  if (window.jQuery) {
    pickup_city = $('#rental_prop_Pickup_City_or_Area').val();
    destination_city = $('#rental_prop_Destination_City_or_Area').val();
  }
  else {
    pickup_city = document.getElementById('rental_prop_Pickup_City_or_Area').value;
    destination_city = document.getElementById('rental_prop_Destination_City_or_Area').value;
  }
  var is_northbound = northbound_routes[pickup_city+' to '+destination_city];

  var trip_time_names = new Array();
  var show_items = pickup_location_visible_ids[pickup_city];
  trip_time_names.push(pickup_city);
  for(pi in show_items) {
    if (window.jQuery) {
      if ($('#'+show_items[pi]).length && $('#'+show_items[pi]).val() != 'none' && $('#'+show_items[pi]).val()) {
        trip_time_names.push($('#'+show_items[pi]).val());
      }
    }
    else {
      if (planyo$(show_items[pi]) && planyo$(show_items[pi]).value != 'none' && planyo$(show_items[pi]).value) {
        trip_time_names.push(planyo$(show_items[pi]).value);
      }
    }
  }
  var offset = 0;
  var offset_found = false;
  for(off in pickup_location_time_offsets) {
    for(n in trip_time_names) {
      if (trip_time_names[n] == off) {
        offset = parseInt(pickup_location_time_offsets[off]);
        offset_found = true;
        break;
      }
    }
    if (offset_found)
      break;
  }

  if (window.jQuery) {
    $('#rental_prop_Pickup_Time > option').remove();
    $('#rental_prop_Pickup_Time').append("<option value='none' selected='selected'>---</option>");
  }
  else {
    var time_text = "<option value='none' selected='selected'>---</option>";
  }
  var pickup_time_arr;
  if (is_northbound)
    pickup_time_arr = pickup_times[pickup_city+' North'];
  else
    pickup_time_arr = pickup_times[pickup_city];
  for (t in pickup_time_arr) {
    if (!isNaN(t-0)) {
      var time_value = pickup_time_arr[t];
      var time_int = time_str_to_minutes(time_value);
      if (is_northbound)
        time_int += offset;
      else
        time_int -= offset;
      if (time_int < 0)
        time_int += 24*60;
      var time_str = time_minutes_to_str(time_int);
      if (window.jQuery)
        $('#rental_prop_Pickup_Time').append("<option value='"+time_str+"'>"+time_str+"</option>");
      else
        time_text += "<option value='"+time_str+"'>"+time_str+"</option>";
    }
  }
  if (!window.jQuery && planyo$('rental_prop_Pickup_Time'))
    planyo$('rental_prop_Pickup_Time').innerHTML = time_text;
}

function update_pickup_items(value) {
  var pickup_city = value;
  var show_items = pickup_location_visible_ids[pickup_city];
  update_pickup_trip_times(pickup_city);
  for(i in pickup_location_ids) {
    if (!isNaN(i-0)) {
      var pi_found = false;
      for(pi in show_items) {
        if (pickup_location_ids[i] == show_items[pi]) {
          pi_found = true;
          break;
        }
      }
      
      if (window.jQuery) {
        if(pi_found)
          $('#row_'+pickup_location_ids[i]).css('display','');
        else
          $('#row_'+pickup_location_ids[i]).css('display','none');
      }
      else if (planyo$('row_'+pickup_location_ids[i])){
        if(pi_found)
          planyo$('row_'+pickup_location_ids[i]).style.display='';
        else
          planyo$('row_'+pickup_location_ids[i]).style.display='none';
      }
    }
  }
}

function update_destination_items(value) {
  var destination_city = value;
  var show_items = destination_location_visible_ids[destination_city];
  for(i in destination_location_ids) {
    if (!isNaN(i-0)) {
      var pi_found = false;
      for(pi in show_items) {
        if (destination_location_ids[i] == show_items[pi]) {
          pi_found = true;
          break;
        }
      }
      if (window.jQuery) {
        if(pi_found)
          $('#row_'+destination_location_ids[i]).css('display','');
        else
          $('#row_'+destination_location_ids[i]).css('display','none');
      }
      else {
        if(pi_found)
          planyo$('row_'+destination_location_ids[i]).style.display='';
        else
          planyo$('row_'+destination_location_ids[i]).style.display='none';
      }
    }
  }

  if (window.jQuery) {
    var pickup_city = $('#rental_prop_Pickup_City_or_Area').val();
  }
  else {
    var pickup_city = planyo$('rental_prop_Pickup_City_or_Area').value;
  }
  if (pickup_times[pickup_city+' North']) {
    update_pickup_trip_times(pickup_city);
  }
  else {
    on_route_change();
  }
}

function on_planyo_form_loaded(mode) {
  if (mode=='reserve') {
    var resource_id = planyo_get_param('resource_id');
    if (!resource_id)
      resource_id = document.resource_id;
    update_pickup_items($('#rental_prop_Pickup_City_or_Area').val());
    $('#rental_prop_Pickup_City_or_Area').change(function() {
        update_pickup_items($(this).val());
      });
    for(l in pickup_location_ids) {
      $('#'+pickup_location_ids[l]).change(update_pickup_trip_times);
    }

    if (true /*resource_id != 6152*/)
      update_destination_items($('#rental_prop_Destination_City_or_Area').val())
    $('#rental_prop_Destination_City_or_Area').change(function() {
        update_destination_items($(this).val());
      });
    for(l in destination_location_ids) {
        $('#'+destination_location_ids[l]).change(function() {
            on_route_change();
        });
    }

    $('#start_date').val('');
    $('#rental_prop_Pickup_City_or_Area').val('Managua (airport)');
    $('#rental_prop_Specific_Location_MA').val('none');
    $('#rental_prop_Specific_Location_GA').val('none');
    $('#rental_prop_Specific_Location_SJ').val('none');
    $('#rental_prop_Area_of_SJdS_').val('none');
    $('#rental_prop_Other_Location').val('');
    $('#rental_prop_Destination_City_or_Area').val('San Juan del Sur');
    $('#rental_prop_Specific_Destination_MA').val('none');
    $('#rental_prop_Specific_Destination_GA').val('none');
    $('#rental_prop_Specific_Destination_SJ').val('none');
    $('#rental_prop_Area_of_SJDS').val('none');
    $('#row_rental_prop_Other_Destination').val('');
    $('#rental_prop_Flight_Number').val('');
    $('#rental_prop_Flight_Arrival_or_Departure_Time').val('');
    $('#rental_prop_Passenger_Names__if_different_from_above_').val('');
    $('#rental_prop_Traveling_with_one_or_more_surfboards').val('');
    $('#rental_prop_Traveling_with_extra_luggage').val('');
    $('#rental_prop_Baby_Seat').val('');
  }
}

function init_adelante_stuff() {
  if (window.jQuery)
    return;

  if (planyo$('rental_prop_Pickup_City_or_Area')) {
    update_pickup_items(planyo$('rental_prop_Pickup_City_or_Area').value);
    planyo$('rental_prop_Pickup_City_or_Area').addEvent('change', function() {
        update_pickup_items(planyo$(this).value);
      });
    for(l in pickup_location_ids) {
      if (!isNaN(l-0) && planyo$(pickup_location_ids[l])) {
        planyo$(pickup_location_ids[l]).addEvent('change', update_pickup_trip_times);
      }
    }
  }
  
  if (planyo$('rental_prop_Destination_City_or_Area')) {
    update_destination_items(planyo$('rental_prop_Destination_City_or_Area').value);
    planyo$('rental_prop_Destination_City_or_Area').addEvent('change', function() {
        update_destination_items(planyo$(this).value);
      });
  }
}

