
AWASA.contacts={getAddContactForm:function(){AWASA.core.JSONRequest({"class":"FormServer","which":"ModifyContact"},AWASA.init.urls.group,function(response,code){$("#add-edit .tab-content-content").html(response.content);$("#add-edit .tab-content-title h2").html("Add Contact");AWASA.contacts.bindModifyContactForm();});},bindModifyContactForm:function(){$("button[name=modify-contact-store-contact]").click(function(){AWASA.contacts.submitModifyContactForm();});$("button[name=modify-contact-cancel]").click(function(){window.location.href="http://www.ssc.org/";});$(".date-picker").datepicker({showOn:'button',buttonImage:'http://www.ssc.org/awasa/theme/images/calendar.gif',buttonImageOnly:true,changeMonth:true,changeYear:true,closeAtTop:false,yearRange:'-60:0',showButtonPanel:true});$('select[name=modify-contact-student-types-select]').change(function(){if($(this).val()=="not-a-student"){$("select[name=modify-contact-graduation-years-select]").val("-1");}
AWASA.schools.getSchoolsSelect($(this).val());});$('input[name=modify-contact-email-personal]').blur(function(){$(this).removeClass('form_error_highlight');$("div.form_error_message").remove();AWASA.contacts.isEmailRegistered($(this).val(),'input[name=modify-contact-email-personal]');});$('input[name=modify-contact-add-school]').click(function(){AWASA.schools.showAddSchoolForm();});$('input[name=modify-contact-add-group]').click(function(){AWASA.groups.showAddGroupForm();});},isEmailRegistered:function(text,selector){AWASA.core.JSONRequest({"class":"PeopleController","action":"isEmailRegistered","email":text},AWASA.init.urls.group,function(response,code){if(response.is_email_registered==true){$(selector).addClass('form_error_highlight');$(selector).after("<div class='form_error_message'>"+response.message+"</div>");}});},submitModifyContactForm:function(){$("button[name=modify-contact-store-contact]").attr('disabled','true').text("Storing . . .");var contact=AWASA.contacts.gatherRegistrationFormData();if(AWASA.contacts.checkFormData(contact)){AWASA.contacts.storeFormData(contact);}else{$("button[name=modify-contact-store-contact]").removeAttr('disabled').text('Store');}},gatherRegistrationFormData:function(){return{"first_name":$("input[name=modify-contact-first-name]").val(),"last_name":$("input[name=modify-contact-last-name]").val(),"phone_cell":$("input[name=modify-contact-phone-cell]").val(),"phone":$("input[name=modify-contact-phone]").val(),"ok_to_text":$("input[name=modify-contact-receive-texts][checked=true]").val(),"nickname":$("input[name=modify-contact-nickname]").val(),"email_personal":$("input[name=modify-contact-email-personal]").val(),"email_school":$("input[name=modify-contact-email-school]").val(),"dob_display":$("input[name=modify-contact-date-of-birth]").val(),"gender":$("select[name=modify-contact-gender]").val(),"paid_member":$("select[name=modify-contact-is-paid-member]").val(),"member_number":$("input[name=modify-contact-member-number]").val(),"address":$("input[name=modify-contact-address]").val(),"city":$("input[name=modify-contact-city]").val(),"state_abbr":$("select#states").val(),"zip":$("input[name=modify-contact-zip]").val(),"student_type":$("select[name=modify-contact-student-types-select]").val(),"graduation_year":$("select[name=modify-contact-graduation-years-select]").val(),"school_id":$("select[name=schools]").val(),"grp_id":$("select[name=groups]").val(),"password":$("input[name=modify-contact-new-password]").val(),"password_repeated":$("input[name=modify-contact-repeat-password]").val()}},checkFormData:function(contact){var fv=contact;var cF=new FormValidator();var errors=new Array();var c=0;var r;cF.textField(fv.first_name,'first name','input[name=modify-contact-first-name]',1);cF.textField(fv.last_name,'last name','input[name=modify-contact-last-name]',1);cF.emailValid(fv.email_personal,'input[name=modify-contact-email-personal]');if(typeof AWASA.contacts.user=="undefined"){AWASA.contacts.isEmailRegistered(fv.email_personal);}
if(fv.email_school.length>0){cF.emailValid(fv.email_school,'input[name=modify-contact-email-school]');}
if(fv.phone_cell.length<1&&fv.phone.length<1){cF.createError('input[name=modify-contact-phone-cell]','Please enter a phone number where you can be reached.');}
cF.textField(fv.dob_display,"date of birth",'input[name=modify-contact-date-of-birth]');if(fv.paid_member=='yes'&&fv.member_number.length<1){cF.createError('input[name=modify-contact-member-number]','You indicated that you are a paying Sierra Club member.  If this is the case, please enter your member number, found on your magazine mailing label, in the field provided.');}
cF.textField(fv.address,'street address','input[name=modify-contact-address]');cF.textField(fv.city,'city','input[name=modify-contact-city]');cF.textField(fv.zip,'zip or postal code','input[name=modify-contact-zip]');if(fv.student_type.length<1){cF.createError('select[name=modify-contact-student-types-select]',"Please select a student type or, if you are not a student \"I'm not a student.\"");}
if(fv.graduation_year==''){cF.createError('select[name=modify-contact-graduation-years]',"PLease select a graduation year or if you are not a student, 'Not applicable'.");}
if(cF.is_form_valid==false){AWASA.contacts.form_errors=cF.getErrors();AWASA.contacts.showFormErrors();return false;}
return true;},storeFormData:function(c){var user_id=(typeof AWASA.contacts.user!="undefined")?AWASA.contacts.user.id:-1;var request={"class":'PeopleController',"action":'StoreModifyContactFormData',"contact":JSON.stringify(c),"user_id":user_id,"json_decode_these[]":['contact']}
AWASA.core.JSONRequest(request,AWASA.init.urls.group,AWASA.contacts.onDataStore);},showFormErrors:function(){var errors=AWASA.contacts.form_errors;$("input").removeClass('form_error_highlight');$("textarea").removeClass('form_error_highlight');var first=true;var first_index=null;$("div.form_error_message").remove();$('button[name=modify-contact-store-registration]').removeAttr('disabled').text('Store');for(var i=0;i<errors.length;i++){if(typeof errors[i].selector=="string"){if(first==true){first_index=i;first=false;}
$(errors[i].selector).addClass('form_error_highlight');$(errors[i].selector).after("<div class='form_error_message'>"+errors[i].message+"</div>");}}
window.scrollTo(0,$(errors[first_index].selector).scrollTop());},loadEditContact:function(contact){AWASA.contacts.contact=contact;AWASA.core.JSONRequest({"class":"FormServer","which":"ModifyContact"},AWASA.init.urls.group,function(response,code){$("#add-edit .tab-content-content").html(response.content);$("#add-edit .tab-content-title h2").html("Edit Contact");$('input[name=modify-contact-first-name]').val(contact.first_name);$('input[name=modify-contact-last-name]').val(contact.last_name);$('input[name=modify-contact-email-personal]').val(contact.email);$('input[name=modify-contact-email-school]').val(contact.email_school);$('input[name=modify-contact-date-of-birth]').val(contact.dob_display);$('input[name=modify-contact-nickname]').val(contact.nickname);$('input[name=modify-contact-phone-cell]').val(contact.cell_phone);$('input[name=modify-contact-phone]').val(contact.phone);$('input[name=modify-contact-member-number]').val(contact.member_number);$('input[name=modify-contact-address]').val(contact.address);$('input[name=modify-contact-city]').val(contact.city);$('input[name=modify-contact-zip]').val(contact.zip);$('select[name=modify-contact-gender] option[value='+contact.gender+']').attr('selected','selected');$('select[name=is-paid-member] option[value='+contact.paid_member+']').attr('selected','selected');$('select#states option[value='+contact.state_abbr+']').attr('selected','selected');$('select[name=modify-contact-graduation-years-select] option[value='+contact.college_graduation_date+']').attr('selected','selected');$('select[name=modify-contacts-student-types-select] option[value='+contact.student_type+']').attr('selected','selected');AWASA.registration.getSchoolSelect(contact.student_type);if(contact.ok_to_text==1){$('input[name=modify-contact-receive-texts]').attr('checked','checked');}else{$('input[name=modify-contact-receive-texts]').removeAttr('checked');}
$('form[name=modify-contact]').append("<input type='hidden' name='registration-form-user-type' value='existing' /><input type='hidden' name='registration-form-contact-id' value='"+contact.id+'" />');AWASA.contacts.bindModifyContactForm();});},onDataStore:function(response,code){$("#add-edit .tab-content-message-space").html(response.message);$("button[name=modify-contact-store-contact]").removeAttr('disabled').text('Store');AWASA.contacts.getAddContactForm();},bindSearchResultSet:function(){$(".search-result-people-edit").click(function(){var contact_id=$(this).attr('name');AWASA.core.JSONRequest({"class":"PeopleController","action":"LoadContact","contact_id":contact_id},AWASA.init.urls.group,function(response,code){$(AWASA.mainAdminInterface.tabs).tabs('select','#add-edit');AWASA.contacts.loadEditContact(response.contact);return false;});});$(".search-result-people-delete").click(function(){if(confirm("Are you sure you want to delete this user from AWASA?")){var contact_id=$(this).attr('name');AWASA.core.JSONRequest({"class":"PeopleController","action":"hideContact","id":contact_id},AWASA.init.urls.group,function(response,code){$("#search .tab-content-message-space").html(response.message);AWASA.search.showPage(AWASA.search.settings.page_num);});}});$(".search-contacts-view-notes").click(function(){var contact_id=$(this).attr('name');var text=$("div.search-contacts-notes[name="+contact_id+"]").html();var content="<div class='input-group light-blue-border ui-corner-all search-contacts-notes-viewer float-left'>\n"+"<div class='input-group-label ui-corner-all'>Edit Notes</div>"+"<textarea class='midsize search-contacts-notes-viewer-textarea' name='"+$(this).attr('name')+"'>"+text+"</textarea>\n"+"<button type='button' class='float-left clear-left close light-blue-border ui-corner-all'><img src='theme/images/disk.png' alt='Save and Close' />&nbsp;Save &amp; Close</button>"+"</div>";AWASA.core.showOverlay({},content,AWASA.search.bindNotesOverlay)});$(".search-contacts-view-events").click(function(){var contact_id=$(this).attr('name');AWASA.core.JSONRequest({"class":"EventController","action":"getContactEventsList","contact_id":contact_id},AWASA.init.urls.group,function(response,code){AWASA.core.showOverlay({},response.content,AWASA.contacts.bindContactEventsList,function(){});});});$(".search-results-edit-group").click(function(){var grp_id=$(this).attr('name');AWASA.core.JSONRequest({"class":"GroupHandler","action":"loadGroup","grp_id":grp_id},AWASA.init.urls.group,function(response,code){$(AWASA.mainAdminInterface.tabs).tabs('select','#add-edit');AWASA.groups.loadEditGroup(response.group);return false;});});},bindContactEventsList:function(){$('.search-people-add-event-add').click(function(){var event_id=$('select[name=search-people-add-event-event-id]').val();var contact_id=$(this).attr('name');AWASA.core.JSONRequest({"class":"EventController","action":"addContactToEvent","contact_id":contact_id,"event_id":event_id},AWASA.init.urls.group,AWASA.contacts.reloadContactEventsList);});},reloadContactEventsList:function(response,code){var contact_id=$('.search-people-add-event-add').attr('name');$("#overlay").remove();AWASA.core.JSONRequest({"class":"EventController","action":"getContactEventsList","contact_id":contact_id},AWASA.init.urls.group,function(response,code){AWASA.core.showOverlay({},response.content,AWASA.contacts.bindContactEventsList,function(){});});}}