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

// we'll use this to keep track of whether the tab or shift+tab keys were pressed
var tab_action = '';
// 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();
	
	// hook up back and next buttons
	$("#quote_form a.button").click(function(){
		var fieldset_id = $(this).attr('href');
		fieldset_id = fieldset_id.replace(/^[^#]*/, ""); // for IE

		// hide all but the requested fieldset
		$("#quote_form fieldset").hide();
		$(fieldset_id).fadeIn();

		// prevent 'jumping'
		return false;
	});

	// hook up tabination between panes
	//  (we must have perfectly consecutive and no overlapping
	//   tabstops in the html for this to work)
	var tabbables = $("#quote_form fieldset [tabindex]");
	tabbables.blur(function(){
		if (just_blurred == true) return; // handle IE
		just_blurred = true;
		
		if (tab_action == 'shift-tab') {
			tab_delta = -1;
		} else if (tab_action == 'tab') {
			tab_delta = 1;
		} else {
			return;
		}

		var this_tabindex = Number($(this).attr('tabindex'));
		var next_tabindex = this_tabindex+tab_delta;
		var next_input = $("[tabindex="+(next_tabindex)+"]");
		if (next_tabindex > 0 && next_input.length) {
			// we have another input to switch to
			var parent_fieldset = next_input.parents('fieldset');
			
			
			if ( parent_fieldset.is(':hidden') ) {
				// we have a hidden form section to show
				$("#quote_form fieldset").hide();
				parent_fieldset.fadeIn(200, function(){
					next_input.focus();
					just_blurred = false;

				});
			} else {
				just_blurred = false;
			}
		} else {
			just_blurred = false;
		}
	});
	
	// 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');
				//console.log('focus '+$(this).val());
			})
			.blur(function(){
				if ( $(this).val() == '') $(this).val(helptext).addClass('ghosted');
				//console.log('blur '+$(this).val());
			});
	});
		
	// 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",
			contact_email: {
				required_nohelptext: true,
				email: true
			}
		},
		submitHandler: function(form) { // on valid submission
			form.submit();
		}
	});

}


$(document).ready(function() {

	//hide anchor titles in nav
	$("#nav a").attr('title','');


	// look for tab presses
	document.onkeydown = function (evt){
		if (!evt) evt = event;
		if (evt.keyCode == 9){ //TAB
			if (evt.shiftKey){ //Shift+TAB
				tab_action = 'shift-tab';
				//console.log("Shift+TAB");
			} else {
				tab_action = 'tab';
				//console.log("TAB");
			}
		} else {
			tab_action = '';
		}
	}
	document.onmousedown = function (evt){
		tab_action = '';
	}

});




