/*===============================================================================================================
	Owner:	Hill's Pet Nutrition
	Title:		FIB > Testimonial Form Javascript Document
	Author:	Steve Kiernan
	Date: 		12/29/09
	Disc:		This document is responsible for the client-side functionality used during the FIB testimonial submission
					process...	(i.e.  Form Validation, AJAX, jQuery) 
	Note(s):	The following jQuery Libraries/Plug-ins are REQUIRED...
						- jquery-1.3.2.js (or jquery-1.3.2.min.js)
						- jquery.form.js (version: 2.36)
						- jquery.validate.js (version: 1.5.5)
					The javascript calls need to be loaded into the HTML in the following order within the <head></head> tags.
						<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
						<script type="text/javascript" src="jquery.form.js"></script>
						<script type="text/javascript" src="jquery.validate.js"></script>
						<script type="text/javascript" src="FIB_testimonialForm.js"></script>
===============================================================================================================*/

$(document).ready(function() {
	//Load the form (HTML form generated by java)
	$("#testimonialForm_container").load("share-your-story-form.do", function(){
		//Initialize client-side validation
		$("#errorDisplay").addClass("hide");
		validateForm();
	});
});

//==== START: INITIALIZE FORM VALIDATION RULES  =====================================================//
function validateForm() {
	var container = $("div.errorContainer");
	var options = { 
		target: '#testimonialForm_container',
		success: function(){
			validateForm();
			$("#errorCount").css("display", "none");
			$("#errorDisplay").css("display", "block");
		}
	}; 
	var validator = $("#petStory").validate({
		debug: true,
		errorContainer: container,
		errorLabelContainer: $("ol", container),
		wrapper: "li",
		meta: "validate",
		
		rules: {
			firstName: "required",
			lastName: "required",
			postalCode: {
				required: true,
				digits: true,
				minlength: 5,
				maxlength: 5
			},
			email: {
				required: true,
				email: true
			},
			userType: "required",
			petType: "required",
			petName: "required",
			story: "required",
			otherCondition: {
				required: function(element) {
					return $("#condition").val() == "other";
				}
			},
			contact: "required",
			share: "required"
		},
		messages: {
			firstName: "Please enter your <a href='javascript:void(0);' onclick='document.petStory.firstName.focus(); return false;'>First Name</a>",
			lastName: "Please enter your <a href='javascript:void(0);' onclick='document.petStory.lastName.focus(); return false;'>Last Name</a>",
			postalCode: {
				required: "Please enter your <a href='javascript:void(0);' onclick='document.petStory.postalCode.focus(); return false;'>Zip Code</a>",
				digits: "<a href='javascript:void(0);' onclick='document.petStory.postalCode.focus(); return false;'>Zip Code:</a> Only numeric values between 0-9 are accepted",
				minlength: "<a href='javascript:void(0);' onclick='document.petStory.postalCode.focus(); return false;'>Zip Code:</a> Please enter at least 5 digits",
				maxlength: "<a href='javascript:void(0);' onclick='document.petStory.postalCode.focus(); return false;'>Zip Code:</a> Please do not enter more than 5 digits"
			},
			email: {
				required: "Please enter your <a href='javascript:void(0);' onclick='document.petStory.email.focus(); return false;'>Email Address</a>",
				email: "Your <a href='javascript:void(0);' onclick='document.petStory.email.focus(); return false;'>Email Address</a> must be in the format of name@domain.com"
			},
			userType: "Please select your <a href='javascript:void(0);' onclick='document.petStory.userType.focus(); return false;'>User Type</a>",
			petType: "Please select the <a href='javascript:void(0);' onclick='document.petStory.petType.focus(); return false;'>Type of Pets</a> you own",
			petName: "Please enter your <a href='javascript:void(0);' onclick='document.petStory.petName.focus(); return false;'>pet's name</a>",
			story: "Please <a href='javascript:void(0);' onclick='document.petStory.story.focus(); return false;'>share your story</a>",
			otherCondition: "Condition: <a href='javascript:void(0);' onclick='document.petStory.otherCondition.focus(); return false;'>Please describe</a>",
			contact: "Please select if we can contact you about your story",
			share: "Please select if we can share your testimonial with others"
		},
		showErrors: function() {
			$("#errorCount").text(validator.numberOfInvalids() + " error(s) found");
			this.defaultShowErrors();
		},
		invalidHandler: function() {
			$("#errorCount").text(validator.numberOfInvalids() + " error(s) found");
		},
		submitHandler: function(form) {
			$("#errorCount").text(validator.numberOfInvalids() + " error(s) found");
			$("#petStory").ajaxSubmit(options);
		}
	});
}
//==== END: INITIALIZE FORM VALIDATION RULES  =====================================================//