//JQuery Setup
$(function(){

//Form Functions
//---------------------------------------------------------------------------------
		//Hide all items with class of "hidden-content"
		$('.hidden-content').css('display', 'none');
		
		//Sequential numbering of comment form li's
		$(".sequential-list li").each( function(i) {
		    i = i+1;
		    $(this).prepend('<span class="comment-number">' + i + '. </span>');
		});
		
		//toggle hidden fields on Appointment Request Form
		//New Patient Toggle...
		$("input[name^='Current_Patient']").click( function() {
			 if ($("input[name^='Current_Patient']:checked").val() == 'No')
			 	//not current patient, ask where they found us
			 	$('.hidden-content').fadeIn('slow');
			else 
				//current patient, hide content
				$('.hidden-content').fadeOut();
					if($('#found-other').css('display') != 'none') {
						$('#found-other').fadeOut();
					}
		});
		//Where did you hear about us toggle 
		$('#Found').change(function() {
			var selected = $('#Found option:selected');
			if(selected.val() == 'Other') {
				$('#found-other').fadeIn('slow');
			} else {
				$('#found-other').fadeOut();
			}
		});  

		//Comment form "May We Contact You" toggle
		$("input[name^='Contact_Me']").click( function() {
			 if ($("input[name^='Contact_Me']:checked").val() == 'Yes')
			 	//not current patient, ask where they found us
			 	$('.hidden-content')
			 		.fadeIn('slow');
			else 
				//current patient, hide content
				$('.hidden-content').fadeOut();
		});

		//Referral form "Radiographs Sent" toggle
		$("input[name^='Radiographs_Sent']").click( function() {
			 if ($("input[name^='Radiographs_Sent']:checked").val() == 'Yes')
			 	//not current patient, ask where they found us
			 	$('.hidden-content')
			 		.fadeIn('slow');
			else 
				//current patient, hide content
				$('.hidden-content').fadeOut();
		});
		
		/* Toggle Submit after Agreeing to Terms */
		$('input[name^="Disclaimer"]').click( function() {
			 if ($("input[name^='Disclaimer']:checked").val() == 'I Agree')
			 	//not current patient, ask where they found us
			 	$('.hidden-content').fadeIn('slow');
			else 
				//current patient, hide content
				$('.hidden-content').fadeOut();
		});
		
		//clear form fields with "clearme" class when clicked
		$('.clearme').one("focus", function() {
				$(this).val("");
		});
	
		//add red asterisk to label of required fields
		$('label.required').each(function(i) {
			$(this).append('<em> * </em>');
		});
		
		//Form validation
		//---------------------------------------------------------------------------------
			
		//masked input
		$(".date-mask").mask("99/99/9999");
		$(".phone-mask").mask("(999) 999-9999");
	
		//additional validation methods
		$.validator.addMethod("phone", function(phone_number, element) {
		    phone_number = phone_number.replace(/\s+/g, ""); 
			return this.optional(element) || phone_number.length > 9 &&
				phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
		}, "Please specify a valid phone number");
	
		//validator
		$(".validate").each(function() {
			$(this).validate({
				errorElement: "div",
					success: function(label) {
						label.text("ok!").addClass("success");
					},
				rules: {
					First_Name: {
						required:true,
						minlength: 2
					},
					Last_Name: {
						required:true,
						minlength: 2
					},
					Doctor_First_Name: {
						required:true,
						minlength: 2
					},
					Doctor_Last_Name: {
						required:true,
						minlength: 2
					},
					Patient_First_Name: {
						required:true,
						minlength: 2
					},
					Patient_Last_Name: {
						required:true,
						minlength: 2
					},
					Patient_Email: {
						required: true,
						email:true
					},
					Referred_Patient_First_Name: {
						required:true,
						minlength: 5
					},
					Referred_Patient_Last_Name: {
						required:true,
						minlength: 5
					},
					Doctor_Patient_Referring: {
						required:true,
						minlength: 5
					},
					Doctor_Email: {
						required: true,
						email:true
					},
					Appointment_Email: {
						required: true,
						email:true
					},
					Daytime_Phone: {
						required: true,
						phone: true	
					},
					Alternate_Phone: {
						phone: true
					},
					Patient_Phone: {
						required: true,
						phone: true	
					},
					Message: {
						required:true,
						minlength: 12
					},
					Would_Like_To: {
						required: true
					},
					User_Name: {
						required: true
					},
					Password: {
						required: true
					},
					Question_1: {
						required: true
					},
					Question_2: {
						required: true
					},
					Question_3: {
						required: true
					},
					Question_4: {
						required: true
					},
					Question_5: {
						required: true
					},
					Question_6: {
						required: true
					},
					Question_7: {
						required: true
					},
					Question_8: {
						required: true
					},
					Question_9: {
						required: true
					},
					Question_10: {
						required: true
					},
					Patient_Name: {
						minlength: 5,
						required: "#Contact_Me_Yes:checked"
					},
					Email: {
						email:true,
						required: "#Contact_Me_Yes:checked"
					},
					txtNumber: {
						required:true,
						minlength: 5
					}
				}
			});
		});


});//end document.ready
