$(document).ready(function() {
		
	
	
	//highlight selected text fields
	var ti = $('.textinput');
	if( ti.length ) {
		ti.focus( function() {
			$(this).removeClass('unhighlight');
			$(this).addClass('highlight');
		});

		ti.blur( function() {
			$(this).removeClass('highlight');
			$(this).addClass('unhighlight');
		});
	}
	

	/*
		Step 1
	*/

	if( $('#Formstep1').length ) {
		
		//we want to populate dropdown2 depending on the selected option in dropdown1
		var dropdown1 = $('#ajax_course');
		var dropdown2 = $('#ajax_locationdate'); 

		//once on load
		updateCourseLocation( dropdown2, dropdown1.val() );

		//and then whenever it changes
		dropdown1.change( function() {
			updateCourseLocation( dropdown2, dropdown1.val() );

			//while we're here, toggle 'other' text field
			
			if( dropdown1.val() == 'other' ) {
				$('#OtherCourse').removeClass('hidden');
				$('#LocStartDateRow').addClass('hidden');
			}
			else {
				$('#OtherCourse').addClass('hidden');
				$('#LocStartDateRow').removeClass('hidden');
			}

		});

		//show the text field if it was set by the session
		if( dropdown1.val() == 'other' ) {
			$('#OtherCourse').removeClass('hidden');
			$('#LocStartDateRow').addClass('hidden');
		}

		//birthdate field
		$('#Birthdate').datePicker({
			year: 1970,
			startDate: '01/01/1900',
			endDate: (new Date()).asString()
		});

		//$('#Birthdate').attr('readonly', true);
		$('#Birthdate').mask("99/99/9999");



		//tooltips
		var helpData = [
			'Select the course you would like to do.',
			'Select the location and start date of the course.',
			'We promise to keep your email address private.'
		]; 

		$('.help').each( function(i) {
			$(this).simpletip({ content: helpData[i] });
		});

		//validation
		$('#Formstep1').validate({
			rules: {
				locationdate: {
					required: function(el) {
						return dropdown1.val() != 'other';
					}
				},
				birthday: {
					required: true,
					date: true
				},
				other: {
					required: function(el) {
						return dropdown1.val() == 'other';
					}
				}
			}
		
		});
	
	} //end step 1


	
	/*
		Step 2
	*/
	if( $('#Formstep2').length ) {
		//hide english assessment stuff
		if( !$('#english').attr('checked') )
			$('#english_extra').hide();
		

		$('#english').click( function() {
			
			if( $('#english').attr('checked') )
				$('#english_extra').slideDown();
			else
				$('#english_extra').slideUp();
		});
		
		//tooltips
		var helpData = [
			'If English is not your first language you may need to complete an English assessment before the course begins.',
			'Does your previous experience cover areas covered in this course?'
		]; 

		$('.help').each( function(i) {
			$(this).simpletip({ content: helpData[i] });
		});

		//validation
		$('#Formstep2').validate({
			debug: false,
			rules: {
				postcode: {
					required: true,
					minlength: 4
				},
				phone: {
					required: true,
					minlength: 8
				},
				english_assessment: {
					required: function() {
						return $('#english').attr('checked')
					}
				}
			},
			errorPlacement: function(error, el) {
				elname = el.attr('name')

				if( elname == 'english_assessment' ) {
					error.insertAfter( $('#english_assessment_no').next() );
				}
				else
					error.insertAfter(el);
			},
			messages: {
				english_assessment: 'Please select one of the options above.'
			}

		});
	
	} //end step 2


	/*
		Step 3
	*/
	if( $('#Formstep3').length ) {
		$('.instructions').hide();

		var p = $('input[name=payment]:radio');
		var checked = $('input[name=payment]:checked').val()

		//when page loads if something was previously selected, show its extra stuff
		if( checked == 'dd' )
			$('#inst_dd').show();
		else if( checked == 'cc' )
			$('#extra_cc').show();
	
		//hide/show things when different options clicked
		p.change( function() { 
			checked = $('input[name=payment]:checked').val()
			$('.instructions').slideUp();
			if( checked == 'dd' )
				$('#inst_dd').slideDown();
			else if( checked == 'cc' )
				$('#extra_cc').slideDown();

		});

		//tooltips
		var helpData = [
			'CCV - The 3 or 4 digit Credit Card Verification number printed on the signature strip.'
		]; 

		$('.help').each( function(i) { $(this).simpletip({ content: helpData[i] }); });


		//mask input for CC
		$('#card_number').mask('9999 9999 9999 9999');

		//validation
		$('#Formstep3').validate({
			debug: false,
			rules: {
				payment: {
					required: true
				},
				card_type: { required: cc_req },
				card_name: { required: cc_req },
				card_number: { required: cc_req },
				card_exp_mo: { required: cc_req },
				card_exp_year: { required: cc_req },
				card_ccv: { required: cc_req, digits: true }
			},
			messages: {
				payment: {
					required: 'Please select a payment method:'
				}
			},
			errorPlacement: function(error, el) {
				elname = el.attr('name')

				if( elname == 'payment' )
					error.insertBefore('#errorBeforeMe');
				else if( elname == 'card_exp_mo' || elname == 'card_exp_year' )
					error.insertAfter('#card_exp_year');
				else
					error.insertAfter(el);

			},
			errorElement: "label",
			groups: {
				ccexp: "card_exp_mo card_exp_year"
			}
		});

	} //end step 3


/*
	Step 4
*/
	if( $('#Formstep4').length ) {
		$('#Formstep4').validate({
			debug: false,
			rules: {
				agree: { required: true }
			},
			messages: {
				agree: {
					required: 'You must read and accept the terms and conditions'
				}
			},
			errorPlacement: function(error, el) {
				error.insertAfter('#agreeText');
			}
		});
	
	} //end step 4


});

// if the credit card radio button selected, require fields entered
function cc_req(el) {
	return $('input[name=payment]:checked').val() == 'cc'
}


/*
	update the course locationdate dropdown with the date/locs for the given course id
*/
function updateCourseLocation( el, courseID ) {
	el.load('/register/AjaxPopulateCourseLocation/'+courseID);
}
