function event_instances_display_proper() {
  // Hardcoded, pull from objects
  startVal = start_slider.cur_time;
  endVal = end_slider.cur_time;

	var numFound = 0;
	$('instance_data').innerHTML = "";
	for (var i = 0; i < showTimes.length; i++) {
		var time = showTimes[i];
		if (time >= startVal && time <= endVal) {
			if (instanceId != instanceIds[i]) 
				$('instance_data').innerHTML += '<div><a href="' + urls[i] + '&instanceStart=' + startVal/interval + '&instanceEnd=' + endVal/interval + '">' + texts[i] + '</a></div>';
			else
				$('instance_data').innerHTML += '<div>' + texts[i] + '</div>';
			numFound++;
		}
	}
	
	if (numFound == 0) {
		$('instance_data').innerHTML += 'Could not find any instances<br/>';
	}
}

function event_instances_cancel_instance(event_instance_id) {
  var is_checked = $('confirm_instance_cancel_check').value;
  if(is_checked == 0) {
    alert('Please Confirm Event Cancellation');
    return;
  }

  $('confirm_instance_cancel_button').disable();
  $('confirm_instance_goback_button').disable();
  
  var reason = $('cancel_reason').value;

	new Ajax.Request('ajax.php',
	{
		parameters: { 
      _action: 'event_instance_cancel', 
      event_instance_id: event_instance_id, 
      reason: reason 
    },
		onSuccess: function(response) {
      var json_response = response.responseText.evalJSON();
      
      //$('event_status').innerHTML = "<div class=\"status_warning\" style=\"margin-bottom:15px\">Event has been canceled</div>";
      $('cancel_instance_option').hide();
			event_instances_close_cancel();
		},
		onFailure: function(){
			alert('Couldn\'t cancel instance');
      event_instances_close_cancel();
		}
	});
}

function event_instances_confirm_instance_cancel() {
  var is_checked = $('confirm_instance_cancel_check').value;
  
  var new_val = (is_checked == 1) ? 0 : 1;
  $('confirm_instance_cancel_check').value = new_val;
  
  if(is_checked == 1) {
    $('confirm_instance_cancel_button').disable();
  }
  else {
    $('confirm_instance_cancel_button').enable();
  }
}

function event_instances_close_cancel() {
  popup_hide("cancel_event");
}

////////////////////////////////////////////////////////////////////////////////
// Send Note JS
function event_instances_event_note_send(event_instance_id) {
  var message = $F('event_note_message');
  var param = Form.serialize('send_event_message_form');
  param += "&_action=event_message_send";
  param += "&message="+message;
  param += "&event_instance_id="+event_instance_id;

  // Disable Elements
  event_instances_event_note_busy();

  new Ajax.Request('ajax.php',
	{
		parameters: param,
		onSuccess: function(response) {
      var json_response = response.responseText.evalJSON();
      if(json_response.ajax_status == 1) {
        event_instances_event_note_free();
        event_instances_event_note_close();
      }
      else {
        alert(json_response.error_msg);
        event_instances_event_note_free();
      }
		},
		onFailure: function(){
      alert('There is an error with your submission');
      event_instances_event_note_free();
		}
	});
}

function event_instances_event_note_busy() {
  // Disable Elements
  spjs_disable('event_email_guests_submit_button');
  spjs_disable('event_email_guests_cancel_button');
  spjs_disable('event_note_message');
  popup_busy('event_email_guests');
}

function event_instances_event_note_free() {
  // Enable Elements
  spjs_enable('event_email_guests_submit_button');
  spjs_enable('event_email_guests_cancel_button');
  spjs_enable('event_note_message');
  popup_free('event_email_guests');
}

function event_instances_event_note_close() {
  spjs_set_value('event_note_message', '');
  popup_hide('event_email_guests');
}

////////////////////////////////////////////////////////////////////////////////


function event_instances_notification_close_event_update(event_id, event_type_id) {
  $('close_event_update_'+event_id).disable();

  new Ajax.Request('ajax.php',
	{
		parameters: {
      _action: 'history_view', 
      target_id: event_id, 
      target_type_id: event_type_id
    },
		onSuccess: function(response) {
      var json_response = response.responseText.evalJSON();
      $('updated_event_'+event_id).innerHTML = 'Update Noted';
      base_finish_notification();
		},
		onFailure: function(){
      alert('We experienced a problem closing your event update notification.  Please try again later.');
		}
	});
}


// ----------------------------------------------

function event_instances_show_select_popup() {
  popup_show('instance_select');
  event_instances_load_instances();
}

function event_instances_load_instances() {
  var old_loc_id = $('popup_instance_date_select_old_location_id').value;
  var loc_id = $('popup_instance_select_location_div_location').value;
  var loc_text = $('popup_instance_select_location_div_input').value;
  var event_id = $('popup_instance_date_select_event_id').value;
  var type = $('popup_instance_date_select_type').value;
  var year = $('popup_instance_date_select_year').value;
  var month = $('popup_instance_date_select_month').value;
  var day = $('popup_instance_date_select_day').value;
  
  $('event_instances_select_results').hide();
  $('event_instances_select_results_busy').show();
  $('event_instances_select_results').innerHTML = "";
  
  new Ajax.Request('ajax.php',
	{
		parameters: {_action: 'event_load_instances', event_id: event_id, old_location_id: old_loc_id, location_id :loc_id, location_text: loc_text, year: year, month: month, day: day, type: type},
		onSuccess: function(response) {
      var json_response = response.responseText.evalJSON();
      
      if(json_response.ajax_status == 1) {
        $('event_instances_select_results').innerHTML = json_response.html;
        
        if(json_response.update_venue == 1) {
          // Update Venue
          $('popup_instance_date_select_old_location_id').value = loc_id;
          
          // Refresh Calendar Highlighted Days (need to limit due to prototype bug)
          popup_instance_date_select.clear_day_nums();
          var len = json_response.calendar_dates.length;
          for (var i=0; i<len; i++) {
            var datestr = json_response.calendar_dates[i];
            
            // Use name from popup
            popup_instance_date_select.add_day_num_events(datestr);
            popup_instance_date_select.display_dates();
          }
        }
      }
      else {
        $('event_instances_select_results').innerHTML = json_response.html;
      }
      
      $('event_instances_select_results').show();
      $('event_instances_select_results_busy').hide();
		},
		onFailure: function(){
      alert('We experienced a problem viewing the event times.  Please try again later.');
		}
	});
}

function event_instances_load_instances_panel() {
  
  var old_loc_id = $('panel_othertimes_old_location_id').value;
  var loc_id = $('panel_othertimes_location_div_location').value;
  var loc_text = $('panel_othertimes_location_div_input').value;
  var event_id = $('panel_othertimes_event_id').value;
  var type = $('paenl_othertimes_type').value;
  var year = $('panel_othertimes_year').value;
  var month = $('panel_othertimes_month').value;
  var day = $('panel_othertimes_day').value;

  $('panel_othertimes_results').hide();
  $('panel_othertimes_busy').show();
  $('panel_othertimes_results').innerHTML = "";
  
  new Ajax.Request('ajax.php',
	{
		parameters: {
      _action: 'event_load_instances', 
      event_id: event_id, 
      old_location_id: old_loc_id, 
      location_id :loc_id, 
      location_text: loc_text, 
      year: year, 
      month: month, 
      day: day, 
      type: type
    },
		onSuccess: function(response) {
      var json_response = response.responseText.evalJSON();
      
      if(json_response.ajax_status == 1) {
        $('panel_othertimes_results').innerHTML = json_response.html;
        
        // Update Venue
        if(json_response.update_venue == 1)
          $('popup_instance_date_select_old_location_id').value = loc_id;
      }
      else {
        $('panel_othertimes_results').innerHTML = json_response.html;
      }
      
      $('panel_othertimes_results').show();
      $('panel_othertimes_busy').hide();
		},
		onFailure: function(){
      alert('We experienced a problem viewing the event times.  Please try again later.');
		}
	}); 
}
