
$(document).ready(function(){
	
	// Form Behaviour - Selecting an option from list checks the associated box	
	$("#no_indvs").change( function() {
		if ($("#no_indvs").attr("value"))
			$("#indv").attr("checked", true);
			$("#indv").check_combo_legit(["#self"])
			$("#instant_quote").validate().element( "#indv" );
			
	});
	
	$("#turnover").change( function() {
		if ($("#turnover").attr("value")) {
			$("#busi").attr("checked", true);
			$("#busi").check_combo_legit(["#self"]);
			$("#instant_quote").validate().element( "#busi" );
		}
	});
	
	$("#years").change( function() {
		if ($("#years").attr("value"))
			$("#feeinsure").attr("checked", true);
			$("#instant_quote").validate().element( "#feeinsure" );
	});

	//uncheck is an array containing IDs of elements to uncheck, if the calling object is checked
	jQuery.fn.check_combo_legit = function(uncheck) {
		if ($(this).attr("checked")){
			jQuery.each(uncheck, function(index, item) {
					$(item).attr("checked", false);
			});
		} else {
			return false;
		}
	};
	
	//check at least one of the fields in the group is selected
	jQuery.validator.addMethod('required_group', function(val, el) {
			var $module = $(el).parents('fieldset');
			return $module.find('.required_group:checked').length;
	}, 'Please fill out at least one of these fields.');

	
	$("#indv").change( function() { //functionality where checking #indv means #self can't be checked
		$("#indv").check_combo_legit(["#self"]);
	});

	$("#self").change( function() {
		$("#self").check_combo_legit(["#busi", "#indv"]);
	});
	
	$("#busi").change( function() {
		$("#busi").check_combo_legit(["#self"]);
	});

	// Form Validation
	/*
	// show a simple loading indicator
	var loader = jQuery('<div id="loader"><img src="images/loading.gif" alt="loading..." /></div>')
		.css({position: "relative", top: "1em", left: "25em"})
		.appendTo("body")
		.hide();
	jQuery().ajaxStart(function() {
		loader.show();
	}).ajaxStop(function() {
		loader.hide();
	}).ajaxError(function(a, b, e) {
		throw e;
	});
	
	*/
	
	$("#instant_quote").validate({
		rules: {
			no_indvs: { //#no_indvs is required if $indv is checked
				required: "#indv:checked"
			},

			turnover: {
				required: "#busi:checked"
			},

			years: {
				required: "#feeinsure:checked, #feeinsureplus:checked" 
			},
		
		},
		
		groups: {
			insurance_type: "audit auditlegal feeinsure feeinsureplus",
			insurance_pack: "busi self indv"
		},
		
		errorPlacement: function(error, element) {
		     if (element.attr("name") == "audit" || element.attr("name") == "auditlegal" || element.attr("name") == "feeinsure" || element.attr("name") == "feeinsureplus") {
		     	error.insertBefore("#insurance_type"); }
		     else if (element.attr("name") == "indv" || element.attr("name") == "busi" || element.attr("name") == "self" ) {
		     	error.insertBefore("#insurance_packages"); }
			 else {
		       error.insertAfter(element); }
		},
		
		// show a simple loading indicator
		submitHandler: function(form) {			
			$(form).ajaxSubmit({
				target: "#result"
			});
			
		}
	
		
	});

});
