// Similar to uninvite
function event_invite_delete_invite(invite_id, notification) {
  var confirmation = confirm("Are you sure you want delete your invitation?");
  if(!confirmation)
    return;
  
  if(notification == null)
    notification = false;

  $('invite_options_'+invite_id).innerHTML = "<img style=\"vertical-align:middle\" src=\"layout/progress_yellow.gif\"> Your invite is being deleted...";

  new Ajax.Request('ajax.php', 
	{
		parameters: {"ajax": 'ajax_invite_uninvite.php', "event_invite_id":invite_id,"notification":notification},
		onSuccess: function(response) {
      if(notification) {
        $('invite_notification_'+invite_id).hide();

        $('invite_notification_'+invite_id+'_completed').innerHTML = response.responseText;
        $('invite_notification_'+invite_id+'_completed').show();
      }
      else {
        // Show Updated Invite
        $('event_invite').innerHTML = response.responseText;
      }
		},
		onFailure: function(){
			alert('Couldn\'t process your reponse to the invite.  Please try again later or contact us about the problem');
		}
	});
}

function event_invite_show_invite_response_textbox(id) {
  $('invite_response_message_display_'+id).hide(); 
  $('invite_response_message_input_'+id).show();
}

function event_invite_update_guestlist_view(invite_id, newstatus) {
 
}

function event_invite_show_rsvp(invite_id, invite_hash, source) {

  popup_show('event_rsvp');

  $('popupdiv_event_rsvp_content').hide();
  $('popupdiv_event_rsvp_loading').show();

  new Ajax.Request('ajax.php', 
	{
		parameters: {
      "_action": 'invite_load', 
      "invite_id":invite_id,
      "invite_hash":invite_hash,
      "source":source
    },
		onSuccess: function(response) {
      var json_response = response.responseText.evalJSON();
      
      if(json_response.ajax_status == 1) {
        $('popupdiv_event_rsvp_content_main').innerHTML = json_response.popup_html;
        
        $('popupdiv_event_rsvp_content').show();
        $('popupdiv_event_rsvp_loading').hide();
      }
      else {
        
      }
		},
		onFailure: function(){
			alert('Couldn\'t load your invite.  Please try again later or contact us.');
		}
	});
}

function event_invite_rsvp_popup() {
  var invite_id = $('popup_rsvp_invite_id').value;
  var invite_hash = $('popup_rsvp_invite_hash').value;
  var source = $('popup_rsvp_source').value;
  var rsvp = $RF('event_rsvp_form', 'rsvp');
  var response_text = $('rsvp_response').value;
  
  popup_busy('event_rsvp');
 
  new Ajax.Request('ajax.php', 
	{
		parameters: { 
      _action: 'invite_respond', 
      invite_id:invite_id, 
      invite_hash:invite_hash, 
      answer: rsvp, 
      response_text: response_text,  
      source: source
    },
		onSuccess: function(response) {
      var json_response = response.responseText.evalJSON();
      if(json_response.ajax_status == 1) {  
        // Common
        popup_hide('event_rsvp');
        popup_free('event_rsvp');
        
        if(source == 'notification') {
          $('event_invite_blurb_'+invite_id).innerHTML = json_response.html;
        }
        else if(source == 'view') {
          $('event_invite_blurb_'+invite_id).innerHTML = json_response.html;
        }
        else {
          alert(source + ' unknown rsvp source');
        }
      }
      else {
        alert(json_response.error_msg);
        popup_free('event_rsvp');
      }
		},
		onFailure: function(){
			alert('Couldn\'t process your reponse to the invite.  Please try again later or contact us about the problem');
      popup_free('event_rsvp');
		}
	}); 

}

function event_invite_join_event(event_id, event_instance_id) 
{
	spjs_hide('event_invite_blurb_0');
  spjs_show('event_invite_busy');
  
	new Ajax.Request('ajax.php', 
	{
		parameters: { 
      _action: 'invite_join_event', 
      event_instance_id: event_instance_id, 
      event_id: event_id,
      return_type: 'event_invite_blurb'
    },
		onSuccess: function(response) {
      var json_response = response.responseText.evalJSON();
      
      if(json_response.ajax_status == 1) {
        spjs_hide('event_invite_busy');
        spjs_append_html('event_invite_blurbs', json_response.html);
			}
			else {
			  alert(json_response.error_msg);
        spjs_hide('event_invite_busy');
        spjs_show('event_invite_blurb_0');
			}
		},
		onFailure: function(){
			alert('Couldn\'t get invitation for this event.  Please try again later or contact us about the problem');
		}
	});
}
