sooji.plugin.BOH_register = function(_id, _uid) {

	var uid = _uid;
	var id = _id;
	
	$(function(){
	
		$('form#reg-Form')
			.find('button')
				.button()
				.end()
			.bind('submit', registerFormSubmit);
		
		/**
		 * Buttons
		 *
		 */
		$('img#'+_id)
			.click(propertiesButtonClick);
			
		/**
		 * Dialogs
		 *
		 */
		
		$('#reg-Volunteer')
			.bind('change',volunteerClick)
		
	});
	
	
	var propertiesButtonClick = function(e) {
		
		alert('There are no properties for this plugin');
		
	}
	
	
	var registerFormSubmit = function(e) {
 		e.preventDefault();
		
		var form = $(this);

		var isvolunteer = $('#reg-Volunteer').is(':checked');

		var data = {
			
			UID: $('#UID'+uid).val(),
			RegisterID: '',
			FirstName: $('#reg-FirstName').val(),
			LastName: $('#reg-LastName').val(),
			Email: $('#reg-Email').val(),
			Postcode: $('#reg-Postcode').val(),
			Phone: $('#reg-Phone').val(),
			Volunteer: isvolunteer ? '1' : '0',
			VolunteerDOB: isvolunteer ? $('#reg-VolunteerDOB').val() : '',
			VolunteerMobile: isvolunteer ? $('#reg-VolunteerMobile').val() : '',
			VolunteerEmergencyContact: isvolunteer ? $('#reg-VolunteerEmergencyContact').val() : '',
			VolunteerEmergencyPhone: isvolunteer ? $('#reg-VolunteerEmergencyPhone').val() : '',
			VolunteerHealthIssues: isvolunteer ? $('#reg-VolunteerHealthIssues').val() : '',
			VolunteerHasFirstAidCertificate: isvolunteer ? $('#reg-VolunteerHasFirstAidCertificate').val() : '',
			VolunteerRoleApplyingFor: isvolunteer ? $('#reg-VolunteerRoleApplyingFor').val() : '',
			VolunteerPreviousExperience: isvolunteer ? $('#reg-VolunteerPreviousExperience').val() : '',
			VolunteerAvailability: isvolunteer ? $('#reg-VolunteerAvailability').val() : '',
			VolunteerNTQUnsubscribe: isvolunteer ? $('#reg-VolunteerNTQUnsubscribe').is(':checked') ? "1" : "0" : '',
			VolunteerTraining: isvolunteer ? $('#reg-VolunteerTraining').val() : '',
			PrivacyPolicy: $('#reg-PrivacyPolicy').is(':checked') ? "1" : "0",
			Subscribe: $('#reg-Subscribe').is(':checked') ? "1" : "0"
			
		}
		
		if (sooji.validate(this) == false) return;
		
		// Validate the date
		if (isvolunteer) {
			
			if (Date.parseExact(data.VolunteerDOB, 'd-M-yyyy') == null) {
			
				$('#reg-VolunteerDOB')
					.focus()
					.select();
					
				alert('It appears you have entered an invalid DOB. Please ensure you enter a date that follows the format dd-mm-yyyy and try again.');
				return;
			}
			
		}
			
		sooji.showActivity();
		
		$.post('/s/plugins/BOH_register/save', data, function(r){
		
			sooji.hideActivity();
			
			if (r != null && r['error']) {					
				alert(r.error);
				return;
			}
			
			form.slideUp();
			$('div#PostThankYouMessage').slideDown();
			
		}, 'json');

	}
	
	var volunteerClick = function(e) {
	
		var d = $('#volunteer-form');
	
		var dob = $('#reg-VolunteerDOB');
		var mobile = $('#reg-VolunteerMobile');
		var emcontact = $('#reg-VolunteerEmergencyContact');
		var emphone = $('#reg-VolunteerEmergencyPhone');
		var health = $('#reg-VolunteerHealthIssues');
			
	
		if ($(this).is(':checked')) {
				
			d.slideDown();
			
			dob.attr('required','required');
			mobile.attr('required','required');
			emcontact.attr('required','required');
			emphone.attr('required','required');
			health.attr('required','required');
			
			$('div.sooji-validate-title').hide();
		
		} else {
		
			d.slideUp();
			
			dob.attr('required','');
			mobile.attr('required','');
			emcontact.attr('required','');
			emphone.attr('required','');
			health.attr('required','');
			
			$('div.sooji-validate-title').hide();
		
		}
		
	}

}
