$(document).ready(function() {
 
	/*
	 * Form Validation
	 */
	 
	// Set Defaults
	jQuery.validator.setDefaults({
		errorElement : 'a',
		wrapper : 'li',
		errorLabelContainer : '#form-messages ul',
		focusInvalid: false,
		onfocusout: false,
		highlight: function(element, errorClass) {
			var errorContainer = $(element).parents('div').eq(0),
					existingIcon = $('img.icon', errorContainer);
				
			// Account for groups of questions
			if ($(element).parents('.group').size()) {
				errorContainer = $(element).parents('.group');
			}
			
			// Replace any existing icon with error icon
			if (existingIcon.size()) {
				existingIcon.replaceWith('<img src="' + $.formVariables.errorIcon + '" alt="error" class="icon" />');
			}
			// Otherwise append to container
			else {
				errorContainer.append('<img src="' + $.formVariables.errorIcon + '" alt="error" class="icon" />');
			}
			
			// Highlight field
			$(element).addClass(errorClass);
			// Highlight label if in a list
			if ($(element).is(':checkbox') || $(element).is(':radio')) {
				$('label', $(element).parents('ul').eq(0)).addClass(errorClass);
			}

		},
		unhighlight: function(element, errorClass) {
			var errorContainer = $(element).parents('div').eq(0);
			
			// Account for groups of questions
			if ($(element).parents('.group').size()) {
				errorContainer = $(element).parents('.group');
			}
			
			// Replace icon with that of success
			if ($(':input.error', errorContainer).size() <= 1) {
				$('img.icon', errorContainer).replaceWith('<img src="' + $.formVariables.validIcon + '" alt="Valid" class="icon" />');
			}
			
			// Unhighlight field
			$(element).removeClass(errorClass);
			// Unhighlight label if in a list
			if ($(element).is(':checkbox') || $(element).is(':radio')) {
				$('label', $(element).parents('ul').eq(0)).removeClass(errorClass);
			}
		},	
		showErrors: function(errorMap, errorList) {		
			var numErrors = this.numberOfInvalids();
				
			this.defaultShowErrors();
						
			// Populate/update error message
			$('h4, p', errorContainer).remove();
			errorContainer.prepend('<h4></h4>');
			
			if (numErrors) {
				$('h4', errorContainer).html('<strong>Sorry!</strong> Your form contains ' + numErrors + " error" + ((numErrors == 1) ? '' : 's') + ':');
				$(this.currentForm).removeClass('valid');
			}
			// Success is ours!
			else {
				$('h4', errorContainer)
					.text('All errors have been corrected')
					.after('<p>Please proceed by clicking send</p>');
				$(this.currentForm).addClass('valid');
			}
			// Setup links
			var displayedErrors = $('a', errorContainer);
			for ( var i = 0; this.errorList[i]; i++ ) {
				var error = this.errorList[i],
					displayedError = displayedErrors.eq(i),
					errorEl = $(error.element);
				
				// Add href attribute to link
				displayedError.attr('href', '#' + errorEl.attr('id'));		
			}
			// Focus on click
			$('a', errorContainer).each( function() {
				var el = $(this),
					fieldID = el.attr('href'),
					field = $(fieldID);
				
				el.bind('click', function() {
					field.trigger('focus');
					$('html,body').animate( 
						{scrollTop: field.offset().top - 30}, 100
					);
					return false;
				});
			});
		}
	});
	
	// Add a placeholder for form messages
	var errorContainer = $('#form-messages');
	if (!$.trim(errorContainer.text()).length) {
		errorContainer.hide();
	}
	errorContainer.append('<ul></ul>');
		
	// Bind event to invalid form submission
	$("form").bind("invalid-form.validate", function(e, validator) {
		errorContainer.show();
		$('html,body').animate( 
			{scrollTop: errorContainer.offset().top - 20}, 100
		);		

		errorContainer.focus();
	});
	
	// Override default messages
	$.extend($.validator.messages, {  
		required : "This field is required",
		email : "Please enter a valid email",
		digits : "Please enter a numeric value"
	});
	
	
	/*
	* Initiate Validation Plugin
	*/
	
	// Validation rules for contact us form 2638
	
	// required : true
	// email : true 
	// digits : true
	
	$('#form_email_2639').validate({	 
		rules : {
			// Subject
			'q2639:q1' : {
				required : true
			},
			
			// Feedback
			'q2639:q2' : {
				required : true
			},
			
			// First Name
			'q2639:q3' : {
				required : true
			},
			
			// Last Name
			'q2639:q4' : {
				required : true
			},
		
			// Email Address
			'q2639:q8' : {
				required : true,
				email : true 
			},
			
			// Phone
			'q2639:q7' : {
				digits : true 
			}	
						
		},
		
		messages : {
			// Subject
			'q2639:q1' : {
				required : 'Please enter a Subject'
			},
			
			// Feedback
			'q2639:q2' : {
				required : 'Please enter your Feedback'
			},
			
			// First Name
			'q2639:q3' : {
				required : 'Please enter your First Name'
			},
			
			// Last Name
			'q2639:q4' : {
				required : 'Please enter your Last Name'
			},
			
			// Email Adddress
			'q2639:q8' : {
				required : 'Please enter an Email Addrress',
				email : 'Please enter a valid Email Addrress'
			},
			
			// Phone
			'q2639:q7' : {
				digits : 'Please enter numerals only'
			}
		}
	});
	
	
	/*
	 *  Preload error icons
	 */ 
	jQuery.preloadContent = function(){
	  for(var i = 0; i<arguments.length; i++)
	  {
	    jQuery("<div>").html(arguments[i]);
	  }
	}
	$.preloadContent($.formVariables.errorIcon, $.formVariables.validIcon);
});
