
function InitializeEvents()
{
	//javascript detection
	$('#nojavascript').hide();
	$('#yesjavascript').show();
			
	AccordionFunctions();
	CamperInformationEvents();
	MedicalInformationEvents();
	CampSelectionEvents();
	PaymentEvents();
	
	// Prevent double submission
	$("#registrationForm").validate({
		/*
		submitHandler: function(form){
			if(!this.wasSent){
				this.wasSent = true;
				$(':submit', form).val('Please wait...')
								  .attr('disabled', 'disabled')
								  .addClass('disabled');
				form.submit();
			} else {
				return false;
			}
		}*/
		
		invalidHandler: function(form, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				notValid();
				alert("Some information was found to be incomplete. \nComplete the form must to submit your registration.");
				return false;
			}
		}
	});
	
	//Added to try and fix the mis-functioning text box inputs
	$("#registrationForm").valid();
	
	// add * to required field labels
	$('label.required').append('&nbsp;<strong>*</strong>&nbsp;');
 
 
	//$("#paymentinfooptionalgift").mask("99999");
		
	/*
		//Sample Masks
		$("#recordClientPhone").mask("(999) 999-9999");
		$("#recordClientPhoneAlt").mask("(999) 999-9999");
		$("#recordClientZip").mask("99999");
		$("#recordPropertyZip").mask("99999");	
		$("#recordPurchaseZip").mask("99999");	
	*/
};

function AccordionFunctions(){
	var accordion = $("#stepForm").accordion(); 
	var current = 0;
	
	/**/
	$.validator.addMethod("pageRequired", function(value, element) {
		var $element = $(element)
		function match(index) {
			return current == index && $(element).parents("#sf" + (index + 1)).length;
		}
		if (match(0) || match(1) || match(2) || match(3) || match(4) || match(5)) {
			return !this.optional(element);
		}
		return "dependency-mismatch";
	}, $.validator.messages.required)
	
	var v = $("#registrationForm").validate({
		errorClass: "warning",
		onkeyup: false,
		onblur: false,
		submitHandler: function() {
			//alert("Submitted, thanks!");
			//alert($("#registrationForm").valid());
			
			//enable 3 fields for submission (disabled fields pass no data)
			$("input#ageonjuly12010").attr("disabled", "");
			$("input#paymentinfosessionfee").attr("disabled", "");
			$("input#paymenttotal").attr("disabled", "");
				
			$(form).submit();
		}
	});
	
	// back buttons do not need to run validation
	$("#sf2 .prevbutton").click(function(){
		current = 0;
		doAccordion();
		changeStep(2, 1);
	});
	$("#sf3 .prevbutton").click(function(){
		current = 1;
		doAccordion();
		changeStep(3, 2);
	});
	$("#sf4 .prevbutton").click(function(){
		current = 2;
		doAccordion();
		changeStep(4, 3);
	});
	$("#sf5 .prevbutton").click(function(){
		current = 3;
		doAccordion();
		changeStep(5, 4);
	});
	$("#sf6 .prevbutton").click(function(){
		current = 4;
		doAccordion();
		changeStep(6, 5);
	});
	// these buttons all run the validation, overridden by specific targets above
	$(".open5").click(function() {
	  if (v.form()) {
		current = 5;
		doAccordion();
		changeStep(5, 6);
	  }
	});
	$(".open4").click(function() {
	  if (v.form()) {
		current = 4;
		doAccordion();
		changeStep(4, 5);
	  }
	});
	$(".open3").click(function() {
	  if (v.form()) {
		current = 3;
		doAccordion();
		changeStep(3, 4);
	  }
	});
	$(".open2").click(function() {
	  if (v.form()) {
		current = 2;
		doAccordion();
		changeStep(2, 3);
	  }
	});
	$(".open1").click(function() {
	  if (v.form()) {
		current = 1;
		doAccordion();
		changeStep(1, 2);
	  }
	});
	$(".open0").click(function() {
	  if (v.form()) {
		current = 0;
		doAccordion();
		changeStep(0, 1);
	  }
	});

	function doAccordion()
	{
		accordion.accordion("activate", current);
	};
}

//Vars for Camper Information
hiddenchurchname = "";
hiddenchurchtown = "";
function CamperInformationEvents() {		
	$('#birthyear').change(function(){ ageCalculation(); });
	$('#birthmonth').change(function(){ ageCalculation(); });
	$('#birthday').change(function(){ ageCalculation();	});

	//
	$("#gradeinfall2010").change(function(){
		//alert($('#gradeinfall2010').val());
		SetGrades();	
	});
	
	$("#attendingchurch").change(function(){
		if ($(this).attr("checked") == true) {
			$("input#attendingchurchname").addClass("pageRequired");
			$("input#attendingchurchtown").addClass("pageRequired");
			$("input#attendingchurchname").attr("disabled", "");
			$("input#attendingchurchtown").attr("disabled", "");
			//alert('Assigning Church Name : ' + hiddenchurchname);
			$('#attendingchurchname').val(hiddenchurchname);
			//alert('Assigning Church town : ' + hiddenchurchtown);
			$('#attendingchurchtown').val(hiddenchurchtown);
		} else {
			$("input#attendingchurchname").removeClass("pageRequired");
			$("input#attendingchurchtown").removeClass("pageRequired");
			$("input#attendingchurchname").attr("disabled", "disabled");
			$("input#attendingchurchtown").attr("disabled", "disabled");
			hiddenchurchname = $('#attendingchurchname').val();
			//alert(hiddenchurchname);
			hiddenchurchtown = $('#attendingchurchtown').val();
			//alert(hiddenchurchtown);
			$('#attendingchurchname').val("none");
			$('#attendingchurchtown').val("none");
		}
	});
}

//Vars for Medical Information
hiddenallergydetails = "";
hiddencurrentmedicationdetails = "";
hiddenhospitalizeddetails = "";
hiddenbehaviouralissuesotherdetails = "";
hiddenemotionalorphysicalissuesdetails = "";
hiddenoneononecounselordetails = "";
function MedicalInformationEvents() {
	//Set Custom Validation Events (if necessary)
	/*
	$("#registrationForm").validate({
		rules: {
			manitobahealthid: {
				pageRequired: true,
				maxlength: 6
			}
		}
	});
*/

	$('input[name=allergies]').change(function(){
		if ($('input[name=allergies]:checked').val()=='Yes') {
			$("textarea#allergydetails").addClass("pageRequired");
			$("textarea#allergydetails").attr("disabled", "");
			$('#allergydetails').val(hiddenallergydetails);
		} else {
			$("textarea#allergydetails").removeClass("pageRequired");
			$("textarea#allergydetails").attr("disabled", "disabled");
			hiddenallergydetails = $('#allergydetails').val();
			$('#allergydetails').val("");
		}
	});
	$('input[name=currentmedication]').change(function(){
		if ($('input[name=currentmedication]:checked').val()=='Yes') {
			$("textarea#currentmedicationdetails").addClass("pageRequired");
			$("textarea#currentmedicationdetails").attr("disabled", "");
			$('#currentmedicationdetails').val(hiddencurrentmedicationdetails);
		} else {
			$("textarea#currentmedicationdetails").removeClass("pageRequired");
			$("textarea#currentmedicationdetails").attr("disabled", "disabled");
			hiddencurrentmedicationdetails = $('#currentmedicationdetails').val();
			$('#currentmedicationdetails').val("");
		}
	});
	$('input[name=hospitalized]').change(function(){
		if ($('input[name=hospitalized]:checked').val()=='Yes') {
			$("textarea#hospitalizeddetails").addClass("pageRequired");
			$("textarea#hospitalizeddetails").attr("disabled", "");
			$('#hospitalizeddetails').val(hiddenhospitalizeddetails);
		} else {
			$("textarea#hospitalizeddetails").removeClass("pageRequired");
			$("textarea#hospitalizeddetails").attr("disabled", "disabled");
			hiddenhospitalizeddetails = $('#hospitalizeddetails').val();
			$('#hospitalizeddetails').val("");
		}
	});
	$("#medicalconditionother").change(function(){
		if ($(this).attr("checked") == true) {
			$("textarea#medicalconditionotherdetails").addClass("pageRequired");
			$("textarea#medicalconditionotherdetails").attr("disabled", "");
			$('#medicalconditionotherdetails').val(hiddenmedicalconditionotherdetails);
		} else {
			$("textarea#medicalconditionotherdetails").removeClass("pageRequired");
			$("textarea#medicalconditionotherdetails").attr("disabled", "disabled");
			hiddenmedicalconditionotherdetails = $('#medicalconditionotherdetails').val();
			$('#medicalconditionotherdetails').val("");
		}
	});
	$("#behaviouralissuesother").change(function(){
		if ($(this).attr("checked") == true) {
			$("textarea#behaviouralissuesotherdetails").addClass("pageRequired");
			$("textarea#behaviouralissuesotherdetails").attr("disabled", "");
			$('#behaviouralissuesotherdetails').val(hiddenbehaviouralissuesotherdetails);
		} else {
			$("textarea#behaviouralissuesotherdetails").removeClass("pageRequired");
			$("textarea#behaviouralissuesotherdetails").attr("disabled", "disabled");
			hiddenbehaviouralissuesotherdetails = $('#behaviouralissuesotherdetails').val();
			$('#behaviouralissuesotherdetails').val("");
		}
	});
	$('input[name=emotionalorphysicalissues]').change(function(){
		if ($('input[name=emotionalorphysicalissues]:checked').val()=='Yes') {
			$("textarea#emotionalorphysicalissuesdetails").addClass("pageRequired");
			$("textarea#emotionalorphysicalissuesdetails").attr("disabled", "");
			$('#emotionalorphysicalissuesdetails').val(hiddenemotionalorphysicalissuesdetails);
		} else {
			$("textarea#emotionalorphysicalissuesdetails").removeClass("pageRequired");
			$("textarea#emotionalorphysicalissuesdetails").attr("disabled", "disabled");
			hiddenemotionalorphysicalissuesdetails = $('#emotionalorphysicalissuesdetails').val();
			$('#emotionalorphysicalissuesdetails').val("");
		}
	});
	$('input[name=oneononecounselor]').change(function(){
		if ($('input[name=oneononecounselor]:checked').val()=='Yes') {
			$("textarea#oneononecounselordetails").addClass("pageRequired");
			$("textarea#oneononecounselordetails").attr("disabled", "");
			$('#oneononecounselordetails').val(hiddenoneononecounselordetails);
		} else {
			$("textarea#oneononecounselordetails").removeClass("pageRequired");
			$("textarea#oneononecounselordetails").attr("disabled", "disabled");
			hiddenoneononecounselordetails = $('#oneononecounselordetails').val();
			$('#oneononecounselordetails').val("");
		}
	});
}

function CampSelectionEvents() {
	$('input[name=session]').change(function() {
		//Optional Mission Point, transportation options
		if ($('input[name=session]:checked').val().indexOf('Mission Point,') !=-1) {
			//Show and enable transportation options
			$('#missionpointoptions').show();
			$("input#missionpointtransportation_yes").addClass("pageRequired");
			$("input#missionpointtransportation_no").addClass("pageRequired");
		} else {
			//Hide and disable transportation options
			$('#missionpointoptions').hide();
			$("input#missionpointtransportation_yes").removeClass("pageRequired");
			$("input#missionpointtransportation_no").removeClass("pageRequired");
		};
		SetTransportationOptions();
		
		//Optional Hockey options
		if ($('input[name=session]:checked').val().indexOf('Hockey Camp') !=-1) {
			//Show and enable Hockey options
			$('#hockeyoptions').show();
			$("select#hockeydivision").addClass("pageRequired");
			$("select#hockeyjerseysize").addClass("pageRequired");
			$("select#hockeyposition").addClass("pageRequired");
		} else {
			//Hide and disable Hockey options
			$('#hockeyoptions').hide();
			$("select#hockeydivision").removeClass("pageRequired");
			$("select#hockeyjerseysize").removeClass("pageRequired");
			$("select#hockeyposition").removeClass("pageRequired");
		};
		
		CalculatePayment();
	});
	
	$('input[name=missionpointtransportation]').change(function() {
		SetTransportationOptions();
	});
}

function PaymentEvents() {

	$("input[name=paymentinfodeposit]").change(function(){
		CalculatePayment();
	});
	$("#paymentinfooptionalgift").change(function(){
		CalculatePayment();
	});
	$("input[name=paymentinfofamilyratededuction]").change(function(){
		CalculatePayment();
	});
	
	//June 21 2010: verify that the submit and payment options have the same id on the ldf application.
	$("#paymentoption").change(function(){
		//alert($("#paymentoption").val());
		ToggleSubmitButtonText($("#paymentoption").val());
	});
	
}

function ToggleSubmitButtonText(paymentOption) {
	if (paymentOption=="Credit Card") $("#submit-btn").val("Proceed to Payment");
	else $("#submit-btn").val("Submit Registration");	
}

function CalculatePayment() {
	cost = 0;
	deposit = 0;
	total = 0;
	payment = 0;
	
	if ($('input[name=session]:checked').val().indexOf('Day Camp,') !=-1) {
		cost = 65;
		deposit = 25;
	} else if ($('input[name=session]:checked').val().indexOf('Little Buddies,') !=-1) {
		cost = 90;
		deposit = 25;
	} else if ($('input[name=session]:checked').val().indexOf('Hockey Camp,') !=-1) {
		cost = 370;
		deposit = 100;
	} else if ($('input[name=session]:checked').val().indexOf('Intermediate,') !=-1) {
		cost = 220;
		deposit = 50;
	} else if ($('input[name=session]:checked').val().indexOf('Paintball,') !=-1) {
		cost = 270;
		deposit = 50;
	} else if ($('input[name=session]:checked').val().indexOf('Teens,') !=-1) {
		cost = 220;
		deposit = 50;
	} else if ($('input[name=session]:checked').val().indexOf('Junior,') !=-1) {
		cost = 200;
		deposit = 50;
	} else if ($('input[name=session]:checked').val().indexOf('Junior 2 day,') !=-1) {
		cost = 90;
		deposit = 50;
	} else if ($('input[name=session]:checked').val().indexOf('LDP,') !=-1) {
		cost = 330;
		deposit = 100;
	};
		
	//Paying full amount or just the deposit
	if ($('input[name=paymentinfodeposit]:checked').val() == "yes") {
		total = deposit;
	} else {
		total = cost;
	};
	
	//If WBC supplies transportation to Mission Point then add $40
	if (($('input[name=missionpointtransportation]:checked').val()=='Yes') && ($('input[name=session]:checked').val().indexOf('Mission Point,') !=-1)) {
		total += 40;
	};
	
	$('#paymentinfosessionfee').val(total);
	
	//if donation then add
	if (isNaN(parseInt($('#paymentinfooptionalgift').val()))) {
		$('#paymentinfooptionalgift').val("0");
	} else if (parseInt($('#paymentinfooptionalgift').val()) <= 0) {
		$('#paymentinfooptionalgift').val("0");	
	} else {
		$('#paymentinfooptionalgift').val(parseInt($('#paymentinfooptionalgift').val()));
		total += parseInt($('#paymentinfooptionalgift').val());
	};
	
	//if family rate deduction then subtract $30
	if ($('input[name=paymentinfofamilyratededuction]:checked').val() == "yes") {
		total -= 30;
	}
	
	//Set Total
	$('#paymenttotal').val(total);
}

function getAge(BirthDay, BirthMonth, BirthYear) {
	// get current date information
	var d = new Date();
	var curr_date = d.getDate();
	var curr_month = d.getMonth();
	var curr_year = d.getFullYear();
	
	// get difference
	var day_diff = 1 - BirthDay;
	var month_diff = 7 - BirthMonth;
	var year_diff = 2010 - BirthYear;
	
	if (month_diff < 0) year_diff--;
	else if ((month_diff==0) && (day_diff < 0)) year_diff--;
	
	return year_diff;
}
function ageCalculation() {
	var BirthDay = $('#birthday').val();
	var BirthMonth = $('#birthmonth').val();
	var BirthYear = $('#birthyear').val();
	if( BirthDay == '' || BirthMonth == '' || BirthYear == '' ) return;
	
	var age = getAge(BirthDay, BirthMonth, BirthYear);
	//alert(age);
	$('#ageonjuly12010').val(age);
};
function changeStep(stepfrom, stepto) {

	//Validate step - if valid then move to the next.
	$("#step" + stepfrom).removeClass("ActiveStep");
	$("#step" + stepfrom).addClass("InactiveStep");
	$("#step" + stepto).removeClass("InactiveStep");
	$("#step" + stepto).addClass("ActiveStep");			
};

function SetGrades() {
		$('.gradesk2').hide();
		$('.grades13').hide();
		$('.grades36').hide();
		$('.grades49').hide();
		$('.grades79').hide();
		$('.grades1012').hide();
		switch($('#gradeinfall2010').val()) {
			case 'k':
				$('.gradesk2').show();
				break;
			case '1': case '2':
				$('.gradesk2').show();
				$('.grades13').show();
				break;
			case '3':
				$('.grades13').show();
				$('.grades36').show();
				break;
			case '4': case '5': case '6':
				$('.grades36').show();
				$('.grades49').show();
				break;
			case '7': case '8': case '9':
				$('.grades79').show();
				$('.grades49').show();
				break;
			case '10': case '11': case '12':
				$('.grades1012').show();
				break;
			default: caption ="default";
		};	
}

function SetTransportationOptions() {
	if (($('input[name=missionpointtransportation]:checked').val()=='Yes') && ($('input[name=session]:checked').val().indexOf('Mission Point,') !=-1)) {
		//alert('enabling transportation options');
		$("input#missionpointdropoff_winkler").addClass("pageRequired");
		$("input#missionpointdropoff_winnipeg").addClass("pageRequired");
		$("input#missionpointpickup_winkler").addClass("pageRequired");
		$("input#missionpointpickup_winnipeg").addClass("pageRequired");
	
		$("input#missionpointdropoff_winkler").attr("disabled", "");
		$("input#missionpointdropoff_winnipeg").attr("disabled", "");
		$("input#missionpointpickup_winkler").attr("disabled", "");
		$("input#missionpointpickup_winnipeg").attr("disabled", "");
	} else {
		//alert('disabling transportation options');
		$("input#missionpointdropoff_winkler").removeClass("pageRequired");
		$("input#missionpointdropoff_winnipeg").removeClass("pageRequired");
		$("input#missionpointpickup_winkler").removeClass("pageRequired");
		$("input#missionpointpickup_winnipeg").removeClass("pageRequired");
	
		$("input#missionpointdropoff_winkler").attr("disabled", "disabled");
		$("input#missionpointdropoff_winnipeg").attr("disabled", "disabled");
		$("input#missionpointpickup_winkler").attr("disabled", "disabled");
		$("input#missionpointpickup_winnipeg").attr("disabled", "disabled");
	};

}
