
AWASA.events={bindAddEventForm:function(){if(typeof AWASA.events.event!="undefined"){AWASA.events.initEventFormDataForUpdate(AWASA.events.event);AWASA.events.event=undefined;}
$("#modify-event .date-picker").datepicker({showOn:'button',buttonImage:'theme/images/calendar.gif',buttonImageOnly:true,changeMonth:true,changeYear:true});$('input').addClass('ui-corner-all');$('textarea').addClass('ui-corner-all');$('a[name=button-add-event]').click(function(){var form_request={"class":"FormServer","which":"AddEvent"}
AWASA.core.JSONRequest(form_request,AWASA.init.urls.event,function(response,code){response.title="<h2>Add an Event</h2>";response.paging="";response.message_space="";AWASA.core.loadMainBlocks(response,AWASA.events.bindAddEventForm,'#events');});return false;});$('a[name=button-view-upcoming-events]').unbind('click').click(function(){AWASA.events.viewUpcomingEvents();return false;});$('a[name=button-view-participants]').click(function(){AWASA.events.getParticipantViewer();return false;});$('a[name=button-view-past-events]').click(function(){AWASA.events.viewPastEvents();return false;});$("button[name=event-form-choose-contact]").click(function(){var request={'class':'FormServer','which':'SmallContactSearch'}
AWASA.core.JSONRequest(request,AWASA.init.urls.search,function(response,code){AWASA.core.showOverlay($("button[name=event-form-choose-contact]").offset(),response.content,AWASA.events.bindSmallContactSearch);});return false;});$("button[name=event-form-choose-states]").click(function(){var request={'class':'FormServer','which':'ChooseStates'}
AWASA.core.JSONRequest(request,AWASA.init.urls.search,function(response,code){AWASA.core.showOverlay($("button[name=event-form-choose-states]").offset(),response.content,AWASA.events.bindChooseStates);});return false;});$("input[name=event-form-use-custom]").click(function(){($(this).is(':checked'))?$('div[name=event-form-add-custom]').slideDown():$('div[name=event-form-add-custom]').slideUp();});$('button[name=event-form-new-question-add]').click(function(){AWASA.events.addCustomQuestion();});$('button[name=event-form-store-event]').click(function(){AWASA.events.storeEvent();});},loadEventForm:function(event_id){var form_request={"class":"FormServer","which":"AddEvent"}
if(event_id>0){var event_request={"class":"EventController","action":"loadEventForUpdate","event_id":event_id}
AWASA.core.JSONRequest(event_request,AWASA.init.urls.event,function(response){AWASA.events.event=response.event;});}
AWASA.core.JSONRequest(form_request,AWASA.init.urls.event,function(response,code){response.title="<h2>Add an Event</h2>";response.paging="";response.message_space="";AWASA.core.loadMainBlocks(response,AWASA.events.bindAddEventForm,'#events');});return false;},initEventFormDataForUpdate:function(e){$('#events .tab-content-title').html("<h2>Edit Event</h2>");$("input[name=event-form-start-date]").val(e.event_startdate);$("input[name=event-form-end-date]").val(e.event_enddate);$("input[name=event-form-short-name]").val(e.event_nameshort);$("input[name=event-form-full-name]").val(e.event_namefull);$("input[name=event-form-location]").val(e.event_location);$("span.event-form-display-states").html(e.event_state);$("#modify-event").append("<input type='hidden' name='event-form-event-id' value='"+e.event_id+"'/>");$("textarea[name=event-form-event-description]").val(e.event_description);if(e.event_contact){$('#modify-event .display-contact-name').html(e.event_contact.last_name+", "+e.event_contact.first_name).append('<input type="hidden" name="contact_id" value="'+e.event_maincontactid+' "/>');}
$("input[name=event-form-event-nameurl]").val(e.event_nameurl);$("input[name=event-form-registration-starts]").val(e.registration_opens);$("input[name=event-form-registration-closes]").val(e.registration_closes);$("input[name=event-form-email-to]").val(e.event_formto);$("input[name=event-form-redirect]").val(e.redirect_to);$("textarea[name=event-form-reply-text]").val(e.event_reply);$.each(e.custom_questions,function(){$("input[name=event-form-use-custom]").attr('checked','true');$('div[name=event-form-add-custom]').slideDown();var question=$(this).get(0);$('textarea[name=event-form-new-question-text]').val(question.question_text);var checked=(question.required>0)?"true":"false";$("input[name=event-form-new-question-required]").attr("checked",checked);AWASA.events.addCustomQuestion();});$('button[name=event-form-store-event]').text("Update Event");},viewUpcomingEvents:function(){var request={"class":"EventController","action":"showUpcomingEvents","num_events":-1}
AWASA.core.JSONRequest(request,AWASA.init.urls.event,function(response,code){if(response.content==false){response.content="";}
AWASA.core.loadMainBlocks(response,AWASA.events.bindViewEvents,'#events');});},viewPastEvents:function(){AWASA.jr({"class":"EventController","action":"showPastEvents","num_events":-1},AWASA.init.urls.event,function(response,code){console.log(response);if(response.content==false){response.content="";}
AWASA.core.loadMainBlocks(response,AWASA.events.bindViewEvents,'#events');return false;});},bindViewUpcomingEvents:function(){AWASA.events.bindViewEvents();return false;},bindViewEvents:function(){$('button.view-events-delete-event').unbind('click').click(function(){var event_id=$(this).attr('name').split(":");event_id=event_id[1];if(confirm('Are you sure you wish to delete this event?')){AWASA.core.JSONRequest({'class':'EventController','action':'deleteEvent','deep_remove':true,'event_id':event_id},AWASA.init.urls.event,AWASA.events.viewUpcomingEvents);}
return false;});$('button.view-events-edit-event').click(function(){var event_id=$(this).attr('name').split(":");event_id=event_id[1];AWASA.events.loadEventForm(event_id);return false;});$('button.view-events-view-participants').click(function(){var event_id=$(this).attr('name').split(":");event_id=event_id[1];AWASA.events.getParticipantViewer(event_id);return false;});$("button.view-events-view-notes").click(function(){var event_id=$(this).attr('name').split(":");event_id=event_id[1];AWASA.core.JSONRequest({"class":"EventController","action":"showEventNotes","event_id":event_id},AWASA.init.urls.event,AWASA.events.showEventNotes);});},showEventNotes:function(response,code){AWASA.core.showOverlay({"left":0,"top":0},response.content,AWASA.events.bindEventNotes);},bindEventNotes:function(){$(".edit-event-notes-notes").blur(function(){var event_id=$(this).attr('name').split(":");AWASA.core.JSONRequest({"class":"EventController","action":"storeEventNotes","event_id":event_id[1],"notes":$(this).val()},AWASA.init.urls.event,function(){});});},getParticipantViewer:function(event_id){if(event_id<1){event_id=-1;}
var request={"class":"EventController","action":"getParticipantViewer","event_id":event_id}
AWASA.core.JSONRequest(request,AWASA.init.urls.event,function(response,code){$('#events .tab-content-title').html("<h2>"+response.title+"</h2>");$('#events .tab-content-content').html(response.content);$('#events .tab-content-paging').html(response.paging);$('#events .tab-content-message-space').html(response.message_space);if(event_id>0){AWASA.events.bindParticipantList(response);$("select[name=participant-viewer-select-event]").val(event_id);}
AWASA.events.bindViewParticipantsSelect();$("button.participant-viewer-export-list").show().click(function(){var event_id=$(this).attr('name').split(":");event_id=event_id[1];AWASA.events.exportParticipantList(event_id);});});return false;},bindViewParticipantsSelect:function(){$('select[name=participant-viewer-select-event]').change(function(){AWASA.events.getParticipantViewer($(this).val());$("button.participant-viewer-export-list").show().attr('name','event_id:'+$(this).val()).click(function(){var event_id=$(this).attr('name').split(":");event_id=event_id[1];AWASA.events.exportParticipantList(event_id);});});},exportParticipantList:function(event_id){$('body').append('<form id="export_participant_list" action="'+AWASA.init.urls.event+'" method="POST">'+'<input type="hidden" name="class" value="EventController" />'+'<input type="hidden" name="action" value="ExportParticipantList" />'+'<input type="hidden" name="event_id" value="'+event_id+'" />'+'</form>');$("#export_participant_list").submit().remove();},getParticipantList:function(event_id){AWASA.core.JSONRequest({"class":"EventController","action":"GetParticipantList","event_id":event_id},AWASA.init.urls.event,function(response,code){AWASA.core.loadMainBlocks(response,AWASA.events.bindParticipantList,"#events");});},bindParticipantList:function(response){$('.participant-viewer-view-full-registration').click(function(){var name_chunks=$(this).attr('name').split(":");AWASA.core.JSONRequest({"class":"EventController","action":"getFullParticipantView","participant_id":name_chunks[1]},AWASA.init.urls.event,function(response,code){AWASA.events.participant=response.participant;AWASA.core.showOverlay($('#events').offset(),response.content,AWASA.events.bindFullParticipantView,AWASA.events.refreshParticipantView);});if($('select[name=participant-viewer-select-event]').val()!=response.event_id){$('select[name=participant-viewer-select-event] option[val='+response.event_id+']').attr('selected','selected');}});$(".participant-viewer-delete-registration").click(function(){var name_chunks=$(this).attr('name').split(":");if(confirm('Are you sure you wish to remove this registration?\nThis will not delete the user from AWASA, just the event.')){AWASA.core.JSONRequest({"class":"EventController","action":"removeParticipant","participant_id":name_chunks[1]},AWASA.init.urls.event,function(response,code){AWASA.events.refreshParticipantView();});}});$(".participant-viewer-switch-registration").click(function(){var id=$(this).attr('name').split(":");AWASA.core.JSONRequest({"class":"EventController","action":"showChangeRegistration","participant_id":id},AWASA.init.urls.event,function(response,code){AWASA.core.showOverlay({},response.content,AWASA.events.bindChangeRegistration);});});},bindChangeRegistration:function(){$(".participant-viewer-store-switch-registration").click(function(){var id=$(this).attr('name').split(":");id=id[1];AWASA.core.JSONRequest({"class":"EventController","action":"storeChangeRegistration","participant_id":id,"event_id":$("select[name=participant-viewer-switch-registration]").val()},AWASA.init.urls.event,function(response,code){$("button.close").click();AWASA.events.refreshParticipantView();});});},refreshParticipantView:function(){$('select[name=participant-viewer-select-event]').change();},bindFullParticipantView:function(){var p=AWASA.events.participant;if(p.role!=""){$("select[name=participant-details-role] option[value="+p.role+"]").attr("selected","selected");}
if(p.forms_received!="0"){$("input[name=participant-details-forms-received]").attr("checked","true");}
if(p.payment_received!="0"){$("input[name=participant-details-payment-received]").attr("checked","true");}
$("input[name=participant-details-payment-amount]").val(p.payment_amount);$("input[name=participant-details-payment-amount]").blur(function(){AWASA.core.JSONRequest({"class":"EventController","action":"StoreParticipantValue","var":"payment_amount","val":$(this).val(),"participant_id":AWASA.events.participant.participant_id,"use_quotes":true},AWASA.init.urls.event,function(){});});if(p.scholarship_requested!="0"){$("input[name=participant-details-scholarship-requested]").attr("checked","true");}
$("input[name=participant-details-scholarship-requested]").click(function(){var value=($(this).is(":checked"))?1:0;var request={"class":"EventController","action":"StoreParticipantValue","var":"scholarship_requested","val":value,"participant_id":AWASA.events.participant.participant_id}
AWASA.core.JSONRequest(request,AWASA.init.urls.event,function(){});});if(p.scholarship_awarded!="0"){$("input[name=participant-details-scholarship-awarded]").attr("checked","true");$("input[name=participant-details-scholarship-amount]").val(p.scholarship_amount);}
$("input[name=participant-details-scholarship-awarded]").click(function(){var value=($(this).is(":checked"))?1:0;var request={"class":"EventController","action":"StoreParticipantValue","var":"scholarship_awarded","val":value,"participant_id":AWASA.events.participant.participant_id}
AWASA.core.JSONRequest(request,AWASA.init.urls.event,function(){});});$("input[name=participant-details-scholarship-amount]").blur(function(){AWASA.core.JSONRequest({"class":"EventController","action":"StoreParticipantValue","var":"scholarship_amount","val":$(this).val(),"participant_id":AWASA.events.participant.participant_id,"use_quotes":true},AWASA.init.urls.event,function(){});});if(p.canceled!="0"){$("input[name=participant-details-has-cancelled]").attr("checked","true");}
if(p.notes!=""){$("textarea[name=participant-details-notes]").val(p.notes);}
$("textarea[name=participant-details-notes]").blur(function(){AWASA.core.JSONRequest({"class":"EventController","action":"StoreParticipantValue","var":"notes","val":$(this).val(),"participant_id":AWASA.events.participant.participant_id,"use_quotes":true},AWASA.init.urls.event,function(){});});$("input[name=participant-details-forms-received]").click(function(){var value=($(this).is(":checked"))?1:0;var request={"class":"EventController","action":"StoreParticipantValue","var":"forms_received","val":value,"participant_id":AWASA.events.participant.participant_id}
AWASA.core.JSONRequest(request,AWASA.init.urls.event,function(){});});$("input[name=participant-details-has-cancelled]").click(function(){var value=($(this).is(":checked"))?1:0;var request={"class":"EventController","action":"StoreParticipantValue","var":"canceled","val":value,"participant_id":AWASA.events.participant.participant_id}
AWASA.core.JSONRequest(request,AWASA.init.urls.event,function(){});});$("input[name=participant-details-payment-received]").click(function(){var value=($(this).is(":checked"))?1:0;var request={"class":"EventController","action":"StoreParticipantValue","var":"payment_received","val":value,"participant_id":AWASA.events.participant.participant_id}
AWASA.core.JSONRequest(request,AWASA.init.urls.event,function(){});});$("select[name=participant-details-role]").change(function(){AWASA.core.JSONRequest({"class":"EventController","action":"StoreParticipantValue","var":"role","val":$(this).val(),"participant_id":AWASA.events.participant.participant_id,"use_quotes":true},AWASA.init.urls.event,function(){});});},bindDeleteQuestionLink:function(){$('.event-form-delete-question').click(function(){$(this).parent().fadeOut('normal',function(){$(this).remove()});return false;});},bindEditQuestionLink:function(){$('.event-form-edit-question').click(function(){$('textarea[name=event-form-new-question-text]').val($(this).parent().find("div.question-text").text());var li=$(this).parent();if($(this).parent().attr('required')==1){$('input[name=event-form-new-question-required]').attr('checked','true');}else{$('input[name=event-form-new-question-required]').removeAttr('checked');}
$('button[name=event-form-new-question-add]').text('Save Changes').unbind('click').click(function(){$(li).find("div.question-text").text($('textarea[name=event-form-new-question-text]').val());var disp_required=($('input[name=event-form-new-question-required]').val()==1)?"Yes":"No";$(li).attr('required',$('input[name=event-form-new-question-required][checked=true]').val())
$(li).find('div.question-is-required').html("<strong>Required? </strong>"+disp_required);$('textarea[name=event-form-new-question-text]').val("");$('button[name=event-form-new-question-add]').unbind('click').text('Add Question').click(function(){AWASA.events.addCustomQuestion();});});return false;});},addCustomQuestion:function(){if($('ul[name=event-form-custom-question-list]').hasClass('ui-sortable')!=true){$('ul[name=event-form-custom-question-list]').sortable();}
var new_question=new customRegistrationQuestion();new_question.text=$('textarea[name=event-form-new-question-text]').val();new_question.required=($('input[type=checkbox][name=event-form-new-question-required]').is(':checked'))?1:0;var disp_required=(new_question.required==1)?"Yes":"No";$('ul[name=event-form-custom-question-list]').append('<li class="ui-state-default" required="'+new_question.required+'">'
+'<span class="ui-icon ui-icon-arrowthick-2-n-s"></span>'
+'<div class="question-text">'+new_question.text+'</div>'
+'<div class="question-is-required"><strong>Required? </strong>'+disp_required+'</div>'
+'<a href="#" class="event-form-delete-question">Delete Question</a> |'
+'<a href="#" class="event-form-edit-question">Edit this question</a>'
+'</li>');AWASA.events.bindDeleteQuestionLink();AWASA.events.bindEditQuestionLink();$('textarea[name=event-form-new-question-text]').val("");},bindChooseStates:function(){if($("#selected-states-list").length<1){$('#choose-states-popup').append('<span id="selected-states-list" style="display:none"></span>');}
$('#choose-states').selectable({stop:function(){var result=$("#selected-states-list").empty();var first=true;$(".ui-selected",this).each(function(){if(first==true){first=false;}else{result.append(',');}
result.append($(this).find('span').text());});}});$('input.select-states-button').click(function(){var states=$("#selected-states-list").text();$('.event-form-display-states').html(states);$('#choose-states-popup').hide();});},bindSmallContactSearch:function(){$('#small-contact-search input[name=find-small-contact-text]').focus(function(){$(this).val("");});$('#small-contact-search input[name=find-small-contact-button]').click(function(){var search_string=$('#small-contact-search input[name=find-small-contact-text]').val();$('.small-contact-search-output').html(AWASA.core.showSmallSpinner());AWASA.core.JSONRequest({'class':'SearchController','search_type':'contact_simple_short','output':'small_contact_box','search_term':search_string},AWASA.init.urls.search,AWASA.events.loadSmallContactSearch);});},loadSmallContactSearch:function(data,code){$('.small-contact-search-output').html(data.content);$('#contacts-small').selectable();$('li.ui-selectee span').click(function(){$(this).parent().parent().addClass('ui-selected');});$('.small-contact-search-output input[name=use_contact]').click(function(){var contact_id=$('li.ui-selected span.contact_id').text();var selector="span.contact_name_"+contact_id+" span.display_name";var contact_display=$(selector).text();AWASA.events.contact_id=contact_id;AWASA.events.contact_display=contact_display;$('#modify-event .display-contact-name').html(AWASA.events.contact_display).append('<input type="hidden" name="contact_id" value="'+contact_id+' "/>');$('#small-contact-search').hide();});},loadEventFormForEdit:function(response,code){},storeEvent:function(){var form_data={"event_startdate":$("input[name=event-form-start-date]").val(),"event_enddate":$("input[name=event-form-end-date]").val(),"event_nameshort":$("input[name=event-form-short-name]").val(),"event_namefull":$("input[name=event-form-full-name]").val(),"event_state":$("span.event-form-display-states").html(),"event_location":$("input[name=event-form-location]").val(),"event_description":$("textarea[name=event-form-event-description]").val(),"event_reply":$("textarea[name=event-form-reply-text]").val(),"event_maincontactid":$("div.display-contact-name input[name=contact_id]").val(),"event_formto":$("input[name=event-form-email-to]").val(),"registration_opens":$("input[name=event-form-registration-starts]").val(),"registration_closes":$("input[name=event-form-registration-closes]").val(),"redirect_to":$("input[name=event-form-redirect]").val(),"event_nameurl":$("input[name=event-form-event-nameurl]").val()}
form_data.custom_questions=[]
if($('input[name=event-form-use-custom]').is(':checked')){form_data.counter=0;$('ul[name=event-form-custom-question-list] li').each(function(){var q=new customRegistrationQuestion();q.required=$(this).attr('required');q.text=$(this).find('div.question-text').text();form_data.custom_questions[form_data.counter]=q;form_data.counter++;});}
var request={'class':'EventController','action':'storeEvent','json_decode_these[]':'form_data','form_data':JSON.stringify(form_data)}
if(typeof AWASA.events.event!="undefined"){request.event_id=AWASA.events.event.event_id;AWASA.events.event=undefined;}else{request.event_id=$("input[type=hidden][name=event-form-event-id]").val();}
AWASA.core.JSONRequest(request,AWASA.init.urls.event,AWASA.events.onEventStored);},onEventStored:function(response,code){$('.tab-content-area .tab-content-content').fadeOut('normal',function(){$('.tab-content-area .tab-content-content').html("<div class='light-blue-border float-left clear-left ui-corner-all' style='padding:10px'>"+"<div class='clear-left float-left'><strong>"+response.message+"</strong></div>"+"<div class='clear-left float-left'> The url to register for this event is: "+response.link_to_event+"</div>"+"<div class='clear-left float-left'><button type='button' name='event-form-add-new-event'>Create New Event</button></div>"+"</div>").fadeIn();$('button[name=event-form-add-new-event]').click(function(){AWASA.events.clearEventForm();return false;});});return false;},clearEventForm:function(){var form_request={"class":"FormServer","which":"AddEvent"}
AWASA.core.JSONRequest(form_request,AWASA.init.urls.event,function(response,code){response.title="<h2>Add an Event</h2>";response.paging="";response.messaging="";AWASA.core.loadMainBlocks(response,AWASA.events.bindAddEventForm,'#events');});}}