
AWASA.registration={bindForms:function(){AWASA.registration.bindLogInForm();$("#show-preregistered-login").click(function(){$("#first-question").fadeOut('slow',AWASA.registration.showLogInForm());});$("#show-signup-form").click(function(){AWASA.registration.showRegistrationForm();});AWASA.registration.bindShowLostPasswordForm();},showLogInForm:function(){var request={'class':'FormServer','which':'ContactLogIn'}
AWASA.core.JSONRequest(request,AWASA.init.urls.registration,function(resp,code){$('#visible-form-holder').hide().html(resp.content);$('#preregistered-login').css('display','block');$('#visible-form-holder').fadeIn();AWASA.registration.bindLogInForm();AWASA.registration.bindShowLostPasswordForm();});},bindShowLostPasswordForm:function(){$('.show-lost-password-form').click(function(){var request={"class":"FormServer","which":"GetLostPassword"}
AWASA.core.JSONRequest(request,AWASA.init.urls.registration,AWASA.registration.showLostPasswordForm);});},showLostPasswordForm:function(resp,code){$('#first-question').fadeOut('slow',function(){$("#visible-form-holder").html(resp.content).fadeIn('slow');$('button[name=submit-get-password-form]').click(function(){AWASA.core.JSONRequest({"class":"PasswordHandler","action":"getNewPassword","email":$('input[name=retrieve-password-email]').val()},AWASA.init.urls.registration,AWASA.registration.showLostPasswordStatusMessage);});});},showLostPasswordStatusMessage:function(response,code){$('#lost-password-message-space').html(response.message);if(response.email_sent==true){$('button[name=goto-login-form]').fadeIn().click(function(){AWASA.registration.showLogInForm();});}else{$('button[name=show-registration-form]').fadeIn('normal').click(function(){AWASA.registration.showRegistrationForm();});}
return false;},bindLogInForm:function(){$("#preregistered-login button[name=login]").click(function(){var request_data={"email":$("input[name=login-email]").val(),"password":$("input[name=login-password]").val(),"event_id":AWASA.registration.event_id,"class":"RegistrationController","action":"LogIn"}
AWASA.core.JSONRequest(request_data,AWASA.init.urls.registration,AWASA.registration.onLogIn);return false;});},onLogIn:function(resp,code){if(resp.success==false||resp.already_registered==true){$("#loginform-message-space").html(resp.message);return false;}
AWASA.registration.showRegistrationForm(resp);},isEmailRegistered:function(email,selector){if(email.length<1){return false;}
if($('input[name=registration-form-user-type]').val()=="existing"){return false;}
var request={"class":"RegistrationController","action":"IsEmailRegistered","email":email,"event_id":AWASA.registration.event_id}
AWASA.core.JSONRequest(request,AWASA.init.urls.registration,function(response,code){if(response.email_registered==true){$(selector).addClass('form_error_highlight').after('<div class="form_error_message">'+response.message+'</div>');}
return false;});},showRegistrationForm:function(resp){$("button[name=store-registration]").click(function(){$(this).attr('disabled','true').text("Storing . . .");AWASA.registration.submitRegistrationForm();});$("button[name=cancel-registration]").click(function(){window.location.href="http://www.ssc.org/sprog/";});$(".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=student_types]').change(function(){AWASA.registration.getSchoolSelect($(this).val());});$('input[name=email-personal]').blur(function(){$(this).removeClass('form_error_highlight');$("div.form_error_message").remove();AWASA.registration.isEmailRegistered($(this).val(),'input[name=email-personal]');});$('input[name=add-school]').click(function(){AWASA.registration.showAddSchoolForm();});$('input[name=add-group]').click(function(){AWASA.registration.showAddGroupForm();});$("#first-question").fadeOut('slow',function(){if(typeof resp!="undefined"){AWASA.registration.user=resp.contact;AWASA.registration.initializeForm(resp,'#registration-form ')}else{$('#registration-new-password').show();}
$("#visible-form-holder").html($('#basic-info-form').css('display','block').get(0)).fadeIn();});},showAddSchoolForm:function(){AWASA.core.JSONRequest({'class':'FormServer','which':'AddSchool'},AWASA.init.urls.registration,function(response,code){var coords={'top':$('input[name=add-school]').offset().top,'left':$('input[name=add-school]').offset().left}
AWASA.core.showOverlay(coords,response.content,function(){$('button[name=add-school-store]').click(function(){AWASA.schools.storeNewSchool();});});});},showAddGroupForm:function(){var school_id=$('select[name=schools]').val();var school_name=$('select[name=schools] option:selected').text();AWASA.core.JSONRequest({'class':'FormServer','which':'AddGroup','school_name':school_name},AWASA.init.urls.registration,function(response,code){var coords={'top':$('input[name=add-group]').offset().top,'left':$('input[name=add-group]').offset().left}
AWASA.core.showOverlay(coords,response.content,function(){$('button[name=add-group-store]').click(function(){AWASA.groups.storeNewGroup();});});});},getSchoolSelect:function(school_type){var request={'school_type':school_type,"class":"RegistrationController","action":'GetSchoolsSelect',"selector":"#school-select-wrapper"}
AWASA.core.showSmallSpinner("select[name=schools]");AWASA.core.JSONRequest(request,AWASA.init.urls.registration,AWASA.registration.loadSchoolsSelect);},loadSchoolsSelect:function(resp,code){$(resp.selector).html(resp.content).css('class','float-left');AWASA.core.hideSmallSpinner('select[name=schools]');$('select[name=schools]').change(function(){var request={"school_id":$(this).val(),"class":'RegistrationController',"action":'GetGroupsSelect',"selector":"#groups-select-wrapper"}
AWASA.core.showSmallSpinner('select[name=groups]');AWASA.core.JSONRequest(request,AWASA.init.urls.registration,AWASA.registration.loadGroupsSelect);});if(typeof AWASA.registration.user!="undefined"){$('select[name=schools] option[value='+AWASA.registration.user.school_id+']').attr('selected','selected').change();}
if(typeof AWASA.temp!="undefined"){$('select[name=schools] option[value='+AWASA.temp.school_id+']').attr('selected','selected').change();AWASA.temp=undefined;}},loadGroupsSelect:function(resp,code){$(resp.selector).html(resp.content);if(typeof AWASA.registration.user!="undefined"){$('select#groups option[value='+AWASA.registration.user.grp_id+']').attr('selected','selected');}},gatherRegistrationFormData:function(){var contact={"first_name":$("input[name=first-name]").val(),"last_name":$("input[name=last-name]").val(),"email_personal":$("input[name=email-personal]").val(),"email_school":$("input[name=email-school]").val(),"dob_display":$("input[name=date-of-birth]").val(),"nickname":$("input[name=nick-name]").val(),"phone_cell":$("input[name=phone-cell]").val(),"phone":$("input[name=phone]").val(),"paid_member":$("select[name=is-paid-member]").val(),"member_number":$("input[name=member-number]").val(),"address":$("input[name=address]").val(),"city":$("input[name=city]").val(),"zip":$("input[name=zip]").val(),"gender":$("select[name=registration-form-gender]").val(),"paid_member":$("select[name=is-paid-member]").val(),"state_abbr":$("select#states").val(),"graduation_year":$("select[name=graduation_years]").val(),"student_type":$("select[name=student_types]").val(),"password":$("input[name=new_password]").val(),"password_repeated":$("input[name=repeat_password]").val(),"ok_to_text":$("input[name=receive-texts][checked=true]").val(),"school_id":$("select[name=schools]").val(),"grp_id":$("select[name=groups]").val()}
var event_registration={"event_id":AWASA.registration.event_id,"diet":$("input[name=diet]").val(),"ride":$("input[name=ride][checked=true]").val(),"howheard":$("select[name=howheard]").val()}
event_registration.custom_questions=[];var counter=0;$("textarea.custom-registration-answer").each(function(){var name=$(this).attr('name');var question=new customRegistrationQuestion();question.setQuestionId($(this).attr('name'));question.answer_content=$(this).val();event_registration.custom_questions[counter]=question;counter++;});var fullFormData={"contact_info":contact,"registration_info":event_registration}
return fullFormData;},initializeForm:function(resp,selector){$('input[name=first-name]').val(resp.contact.first_name);$('input[name=last-name]').val(resp.contact.last_name);$('input[name=email-personal]').val(resp.contact.email.personal);$('input[name=email-school]').val(resp.contact.email.school);$('input[name=date-of-birth]').val(resp.contact.dob_display);$('input[name=nick-name]').val(resp.contact.nickname);$('input[name=phone-cell]').val(resp.contact.phone.cell);$('input[name=phone]').val(resp.contact.phone.phone);$('input[name=member-number]').val(resp.contact.member_number);$('input[name=address]').val(resp.contact.address.address);$('input[name=city]').val(resp.contact.address.city);$('input[name=zip]').val(resp.contact.address.zip);$('select[name=registration-form-gender] option[value='+resp.contact.gender+']').attr('selected','selected');$('select[name=is-paid-member] option[value='+resp.contact.paid_member+']').attr('selected','selected');$('select#states option[value='+resp.contact.address.state_abbr+']').attr('selected','selected');$('select[name=graduation_years] option[value='+resp.contact.college_graduation_date+']').attr('selected','selected');$('select[name=student_types] option[value='+resp.contact.student_type+']').attr('selected','selected');AWASA.registration.getSchoolSelect(resp.contact.student_type);$('#registration-reset-password').show();AWASA.registration.bindChangePasswordButton();if(resp.contact.ok_to_text==1){$('input[name=receive-texts]').attr('checked','checked');}else{$('input[name=receive-texts]').removeAttr('checked');}
$('#registration-form').append("<input type='hidden' name='registration-form-user-type' value='existing' /><input type='hidden' name='registration-form-contact-id' value='"+resp.contact.id+'" />');},bindChangePasswordButton:function(){$('button[name=reset-password]').click(function(){var request={"class":"FormServer","which":"ResetPassword"}
AWASA.core.JSONRequest(request,AWASA.init.urls.registration,function(resp,code){var coords={"top":$('button[name=reset-password]').offset().top,"left":$('button[name=reset-password]').offset().left}
AWASA.core.showOverlay(coords,resp.content,AWASA.registration.bindChangePasswordForm);});});},bindChangePasswordForm:function(){$('button[name=submit-reset-password]').click(function(){var request={'old_password':$('input[name=old_password]').val(),'new_password':$('#a_new_password_fools').val(),'new_password_r':$('input[name=new_password_r]').val(),'class':'PasswordHandler','action':'changePassword','user_id':AWASA.registration.user.id}
AWASA.core.JSONRequest(request,AWASA.init.urls.registration,AWASA.registration.onPasswordChanged);});},onPasswordChanged:function(resp,code){if(resp.password_changed==false){$('#reset-password-message-space').html(resp.message);return false;}
if(resp.password_changed==true){$('#overlay div.replace-form-here').html("<div class='message'>"+resp.message+"</div>");return false;}},submitRegistrationForm:function(){AWASA.registration.form_data=AWASA.registration.gatherRegistrationFormData();if(AWASA.registration.checkFormData()){AWASA.registration.storeFormData();}},checkFormData:function(){var fv=AWASA.registration.form_data.contact_info;var cF=new FormValidator();var errors=new Array();var c=0;var r;cF.textField(fv.first_name,'first name','input[name=first-name]',1);cF.textField(fv.last_name,'last name','input[name=last-name]',1);cF.emailValid(fv.email_personal,'input[name=email-personal]');if(typeof AWASA.registration.user=="undefined"){AWASA.registration.isEmailRegistered(fv.email_personal);}
if(fv.email_school.length>0){cF.emailValid(fv.email_school,'input[name=email-school]');}
if(fv.phone_cell.length<1&&fv.phone.length<1){cF.createError('input[name=phone-cell]','Please enter a phone number where you can be reached.');}
cF.textField(fv.dob_display,"date of birth",'input[name=date-of-birth]');if(fv.paid_member=='yes'&&fv.member_number.length<1){cF.createError('input[name=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=address]');cF.textField(fv.city,'city','input[name=city]');cF.textField(fv.zip,'zip or postal code','input[name=zip]');if(typeof AWASA.registration.user=="undefined"||AWASA.registration.user.id<1){cF.textField(fv.password,'password','input[name=new_password]',4);cF.textField(fv.password_repeated,'password repeated','input[name=repeat_password]',4);if(fv.password!=fv.password_repeated){cF.createError('input[name=repeat_password]','The second password you typed does not match the first.');}}
if(fv.student_type.length<1){cF.createError('input[name=student_types]',"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(typeof AWASA.registration.form_data.registration_info!="undefined"){var reg=AWASA.registration.form_data.registration_info;if(typeof reg.custom_questions!="undefined"){for(var i in reg.custom_questions){if(reg.custom_questions[i].answer_content.length<1){cF.createError("textarea[name='question_id:"+reg.custom_questions[i].question_id+"']","All essay questions must be answered.");}}}}
if(cF.is_form_valid==false){AWASA.registration.form_errors=cF.getErrors();AWASA.registration.showFormErrors();return false;}
return true;},storeFormData:function(){var contact=AWASA.registration.form_data.contact_info;var registration=AWASA.registration.form_data.registration_info;var user_id=(typeof AWASA.registration.user!="undefined")?AWASA.registration.user.id:-1;var request={"class":'RegistrationController',"action":'StoreEventFormData',"contact":JSON.stringify(contact),"registration":JSON.stringify(registration),"user_id":user_id,"json_decode_these[]":['contact','registration']}
var synchronous=true;AWASA.core.JSONRequest(request,AWASA.init.urls.registration,AWASA.registration.onDataStore,synchronous,AWASA.registration.onRegistrationFailed);},showFormErrors:function(){var errors=AWASA.registration.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=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($(errors[first_index].selector).scrollTop(),0);},onDataStore:function(resp,code){$('#visible-form-holder').fadeOut('normal',function(){$('#visible-form-holder').css({"width":"400px"}).html('<div class="message">'+resp.message+'</div>').fadeIn('normal');window.scrollTo(0,0);});return false;},onUserConfirmationEmailed:function(response,code){$('#visible-form-holder').append('<div class="message">'+response.message+'</div>');var request={"class":"RegistrationController","action":"EmailEventContacts","participant_id":AWASA.registration.participant_id}
AWASA.core.JSONRequest(request,AWASA.init.urls.registration,AWASA.registration.onEventContactsEmailed,false,AWASA.registration.onRegistrationFailed);},onEventContactsEmailed:function(response,code){$('#visible-form-holder').append('<div class="message">'+response.message+'</div>');},onRegistrationFailed:function(xmlreq,textStatus,errorThrown){var msg="There was a technical problem submitting your application.  There are number of options to try: <ul><li>Try again later. This may be due slow network speeds</li><li>Try using Firefox 3.  This application works best in that browser.</li><li>If neither of these options works, please call our office at <strong>1-888-JOIN-SSC</strong> so that we can make sure we received your application.</li></ul>.";$('#visible-form-holder').fadeOut('normal',function(){$(this).html('<div class="message" style="float:left;position:relative;">'+msg+'</div>').fadeIn('normal');});}}