( function($){ 
jQuery.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(/^(\d-?)?(\(\d{3}\)|\d{3})-?\d{3}-?\d{4}$/);
}, "Oops, your phone number doesn't look right: can you try again please?");

$(document).ready(function(){
fm_div = $('#send').closest('form');

$.validator.setDefaults({
	submitHandler: function() { 
	var now = new Date();
	$('#date').val(now);
	$.post(fm_div.attr('action'), fm_div.serialize());
	$.post('/wp-content/themes/27713/gm.php', fm_div.serialize());
	if ($('#page').val() == 'personaltrainercourses')
	{
		pageTracker._trackEvent("personaltrainercourses", "form submission");
	}
	else
	{
		pageTracker._trackEvent("Quick App", "form submission");
	}
	var axel = Math.random() + "";
	var a = axel * 10000000000000;
	if (window.ppc === undefined) { ppc='mainsite'; }
	$('#code').html('<iframe src="http://fls.doubleclick.net/activityi;src=3023767;type=count188;cat=010qu529;u1=' + lead + ';u4=' + ppc + ';ord=1;num=' + a + '?" width="1" height="1" frameborder="0" style="display:none"></iframe>');
//	_gaq.push(['_trackPageview', juicy_page + 'brochure-download-thanks/']);

	$('#form').html(thanks); 

	  return false;
	}	
});


	$('input, textarea, select').live('mouseenter', function() {$(this).siblings(".jv-error").children("div").show();}).live('mouseout', function() {$(this).siblings(".jv-error").children("div").hide();});

	
	$('#wpcf7-f184-t1-o1 form').validate({
		onfocusout: function(element) { $(element).valid(); },
		errorElement: "div",
		errorPlacement: function(error, element) {
			error.appendTo(element.parent("span"));
		},
		validPlacement: function(error, element) {
			error.appendTo(element.parent("span"));
		},
		success: "jv-valid",
		errorClass: "jv-error",
		invalidHandler: function(form, validator) {
		  $("#jv-errornotice").show();
		  $(".jv-details").html("");
		  $("#jv-errornotice strong").hide();
		  if(validator.numberOfInvalids() == 1){$("#jv-errornotice strong.single").show();}else{$("#jv-errornotice strong.plural").show();}
		  validator.defaultShowErrors();
//		  $(".jv-error:visible div").each(function(){$(".jv-details").append("<li>" + $(this).html() + "</li>");}); 
		  $(".jv-error:visible div").each(function(){if ($(this).html().substring(0, 4).toUpperCase() == '<IMG') return; $(".jv-details").append("<li>" + $(this).html() + "</li>");}); 
    	},
		rules: {
			firstname: "required",
			lastname: "required",
			email: {
				required: true,
				email: true
			},
			phone: {
				required:true,
				phone: true
			}
		},
		messages: {
			firstname: "<div>Please tell us your first name</div><div style='border:none !important; background-color: transparent !important;padding-top:0px; padding-left:0px; bottom:22px;'><img src='/wp-content/themes/27713/images/errordiv1.png' width='209' height='13' alt='img' /></div>",
			lastname: "<div>Please tell us your last name</div><div style='border:none !important; background-color: transparent !important;padding-top:0px; padding-left:0px; bottom:22px;'><img src='/wp-content/themes/27713/images/errordiv1.png' width='209' height='13' alt='img' /></div>",
			email: "<div>Please enter a valid email address</div><div style='border:none !important; background-color: transparent !important;padding-top:0px; padding-left:0px; bottom:22px;'><img src='/wp-content/themes/27713/images/errordiv1.png' width='209' height='13' alt='img' /></div>",
			phone: {
				required: "<div>Please tell us your phone number</div><div style='border:none !important; background-color: transparent !important;padding-top:0px; padding-left:0px; bottom:22px;'><img src='/wp-content/themes/27713/images/errordiv1.png' width='209' height='13' alt='img' /></div>",
				phone: "<div>Oops, your phone number doesn't look right: can you try again please?</div><div style='border:none !important; background-color: transparent !important;padding-top:0px; padding-left:0px; bottom:22px;'><img src='/wp-content/themes/27713/images/errordiv1.png' width='209' height='13' alt='img' /></div>"
			}

		}
	});

});
})( jQuery );

