// NOTE: We perform the ajax load inline, to getit loaded as quickly as possible

// explorer fires a blur immediately after a focus from jquery - use this to handle
var just_blurred = false;


// disallow required field helptext
jQuery.validator.addMethod("required_nohelptext", function(value, element) {
	return this.optional(element) || value != '* Required'; 
}, "Required");


// load form content
$("#loadtarget_quoteform").load('/includes/quote_form.php?ajax', ajax_post_init );


// initialize all form elements after AJAX load
function ajax_post_init() {
	// hide/show dynamic elements
	$(".init_hideme").css('color','red').hide();
	$(".init_showme").show();
	
	// set up help text
	$("#quote_form [helptext]").each(function(){
		var helptext = $(this).attr('helptext');
		$(this)
			.val(helptext).addClass('ghosted')
			.focus(function(){
				if ( $(this).val() == helptext) $(this).val('').removeClass('ghosted');
			})
			.blur(function(){
				if ( $(this).val() == '') $(this).val(helptext).addClass('ghosted');
			});
	});
		
	// set up validation
	$("#quote_form").validate({
		debug: true,
		focusInvalid: false,
		errorElement: "em", //prevent our labels from being eaten:  http://www.nabble.com/Validate-form-labels-disappear-td22238762s27240.html#a22423468
		errorClass: "invalid_form_element",
		errorPlacement: function(error, element) {
				//void
			},
		highlight: function(element, errorClass) {
			$(element).parents('label').addClass(errorClass);
		},
		unhighlight: function(element, errorClass) {
			$(element).parents('label').removeClass(errorClass);
		},
		rules: {
			contact_name: "required_nohelptext",
			business_name: "required_nohelptext",
			contact_email: {
				required_nohelptext: true,
				email: true
			}
		},
		submitHandler: function(form) { // on valid submission
			form.submit();
		}
	});

}






