// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var calender_element = "";

function check_blur_on_return_date(element, no_return_flight) {
  if (document.getElementById(element).value=='') document.getElementById(element).value = no_return_flight;
  remove_focus(element);
}

function check_focus_on_return_date(element, title, no_return_flight) {
  if (document.getElementById(element).value==no_return_flight) document.getElementById(element).value = '';
  show_calendar(element, title);
}

function remove_focus(element) {
  document.getElementById(element).className = 'not_selected';
}

function set_adults_and_children(controller, adults, children) {
  set_element_value(controller + '_request_adults', adults);
  set_element_value(controller + '_request_children', children);
  if (controller == 'hotel') {
    set_element_value('hotel_request_rooms', Math.round((adults + children)/2))
  }
}

function set_date(date) {
  set_element_value(calendar_element, date);
  switch (calendar_element) {
    case 'flight_request_departure_date':
    remove_focus('flight_request_departure_date');
    set_focus('flight_request_returning_date');
    break;
    case 'car_request_pickup_date':
    remove_focus('car_request_pickup_date');
    set_focus('car_request_dropoff_date');
    break;
    case 'hotel_request_checkin_date':
    remove_focus('hotel_request_checkin_date');
    set_focus('hotel_request_checkout_date');
    break;
  }
}

function set_element_value(element, value) {
  document.getElementById(element).value = value;
  set_focus(element);
}

function set_focus(element) {
  document.getElementById(element).className = 'selected';
  document.getElementById(element).focus();
}

function set_location(element, location) {
  set_element_value(element, location.replace("~", "'"))
}

function show_adults_and_children(element) {
  set_focus(element);
  new Ajax.Updater('west', '/west/show_adults_and_children/?element=' + element, {asynchronous:true, evalScripts:true}); return false; 
}

function show_calendar(element, title) {
  set_focus(element);
  calendar_element = element;
  if (this.value=='DD/MM/YYYY') this.value = '';
  if (document.getElementById('calendar_title') == null) {
    new Ajax.Updater('west', '/west/show_calendar/?title=' + title, {asynchronous:true, evalScripts:true}); return false; 
  } else {
    document.getElementById('calendar_title').innerHTML = title;
  }
}

function show_form(controller) {
  window.location.href = '/' + controller + '/show_form'; 
}

function show_tag_cloud(element) {
  set_focus(element);
  switch (element) {
    case 'flight_request_departure_location':
    element = '?&element=departure'; break;
    case 'flight_request_going_to_location':
    element = '?&element=going_to'; break;
    case 'car_request_pickup_location':
    element = '?&element=pickup'; break;
    case 'car_request_dropoff_location':
    element = '?&element=dropoff'; break;
    case 'car_request_pickup_location':
    element = '?&element=pickup'; break;
    case 'hotel_request_destination_location':
    element = '?&element=destination'; break;
  }
  new Ajax.Updater('west', '/west/show_tag_cloud/' + element, {asynchronous:true, evalScripts:true}); return false; 
}