/**
 * Javascript for plan forms (add or edit)
 *
 */

/**
 *  send the when field back to the server to make sure it is valid
 */   
function plan_form_when_string_validate(event)
{ 
  var when_string = $('#plan_when_string').val();
  
  // Don't bother looking anything up if event was triggered by an "operational" key
  if(event && jQuery.inArray(event.keyCode, operational_keycodes) !== -1) { return true; }

  // Clear previously stored start and stop values
	$('#plan_when_start').val(null);
	$('#plan_when_stop').val(null);

  track_event('Add plan', 'When detect', when_string);

  var params = {};
  params['plan[when_string]'] = when_string;

  // Update when start and stop fields with data from server
	var onSuccess = function(data)
  {
    if(data.when_string_original != $('#plan_when_string').val()) { return; } // canary
    
    if(data.when_start && data.when_stop)
    {
      $('#plan_when_start').val(data.when_start);
      $('#plan_when_stop').val(data.when_stop);
			$('#plan_when_string').addClass('input_validated');
    }
    else
    {
      $('#plan_when_string').addClass('input_invalidated');
    }
  }

	$.get(routes['plan_when_string_validate'], params, onSuccess);
}

/**
 * Stores place information upon where string suggestion selection
 *
 */
function plan_form_where_string_autosuggest_select_callback()
{
	if($(this).attr('place_name'))
	{
	  $('#plan_place_id').val($(this).attr('place_id'));
  	$('#plan_place_name').val($(this).attr('place_name'));
  	$('#plan_place_latitude').val($(this).attr('place_latitude'));
  	$('#plan_place_longitude').val($(this).attr('place_longitude'));
  	$('#plan_place_street_address').val($(this).attr('place_street_address'));
  	$('#plan_place_city').val($(this).attr('place_city'));
  	$('#plan_place_state').val($(this).attr('place_state'));
  	$('#plan_place_country').val($(this).attr('place_country'));
  	$('#plan_place_accuracy').val($(this).attr('place_accuracy'));
		$('#plan_where_string').val($(this).attr('place_name'));
	}
}

/**
 * Clears stored place information upon where string value change
 *
 */
function plan_form_where_string_change_callback()
{
  $('#plan_place_id').val(''); 
  $('#plan_place_name').val(''); 
  $('#plan_place_latitude').val(''); 
  $('#plan_place_longitude').val(''); 
  $('#plan_place_street_address').val(''); 
  $('#plan_place_city').val(''); 
  $('#plan_place_state').val('');  
  $('#plan_place_country').val('');  
  $('#plan_place_accuracy').val('');

	if(trim($('#plan_where_string').val()))
	{
		$('#plan_where_string').addClass('input_validated');
	}
}

/**
 * Toggles the display of an optional field in the plan add form
 *
 * @param string field Field name
 */
function plan_form_toggle_field(field)
{
  if($('#plan_form_include_links_divider').css('display') == 'none')
  {
    $('#plan_form_include_links').hide();
		$('#plan_description_exterior').css('marginBottom', '15px');
  }

  switch(field)
  {
    case 'description':
      $('#plan_form_description_row').show();
      $('#plan_form_description_link').hide();
      $('#plan_form_include_links_divider').hide();
      $('#plan_form #plan_description').focus();
      break;
      
    case 'link':
      $('#plan_form_link_row').show();
      $('#plan_form_link_link').hide();
      $('#plan_form_include_links_divider').hide();
      $('#plan_form #plan_external_url').focus();
      break;
  }
}
