﻿var count = 0;
function chkchk(obj) {
	cb = $("form#nForm input#n" + obj.attr("id"));
	cbchk = cb.attr("checked");
	if (count < 6 && !cbchk) {
		count++;
	}
	else if (cbchk) {
		count--;
	}
	else {
		return false;
	}
	cb.attr("checked", (!cbchk));
	obj.toggleClass("ui-state-active", !cbchk);
	if (count == 6) {
		$("form#nForm input.submit").removeAttr("disabled");
	}
	else
	{
		$("form#nForm input.submit").attr("disabled", "disabled");
	}
}

$(document).ready(function() {
	$("form#nForm :checkbox").each(function() {
		$(this).attr("checked", false);
		$(this).before('<a href="#" id="' + $(this).attr('value') + '" class="n ui-state-default ui-corner-all">' + $(this).attr('value') + '</a>');
	});
	$("a.n").bind("click", function() {
		chkchk($(this));
	});
    $("form#nForm").ajaxForm({
       target: "#result",
       beforeSubmit: function() {
       	$("div#result div.output").html("Calculating...");
			_gaq.push(['_trackEvent', 'Activity', 'Form submission']);
       }
    });
    $("form#nForm input.reset").bind("click", function() {
    	$("a.n").removeClass("ui-state-active");
    	count = 0;
    	$("div#result div.output").html("&pound;0");
    	$("form#nForm input.submit").attr("disabled", "disabled");
    });
});