$(document).ready(function(){

	if($('form').length > 0) {
		$('form').validate();
		
		form.calculate();		

		$('fieldset > div:even').addClass('odd');	// these selectors are zero-based, so the first is 0,
		$('fieldset > div:even').addClass('even');	// which is even.  That's why this looks odd.

		$('form .disabled').bind('click change keypress', function(){
			return false;
		})

		$('form a.morelink').live('click',function(e){
			
			var $more = $(this).siblings('.more');
			
			if($more.is(':visible')) {
				$more.slideUp();
			} else {
				$more.slideDown();
			}
			
			e.preventDefault();
		});
		
		if($('#summary').length > 0) {
			for(var property in prices) {
			
				var price = prices[property];
			
				$('#'+property)
					.siblings('label')
					.append(' + <strong class="price">$'+price+'</strong>')
					.parent()
					.addClass('priced');

			}

			$('.priced input[type=checkbox],.priced input[type=radio]').live('click change',form.calculate);
		}
	}
	
	
/*



$('input[type=checkbox]').each(function(){
	
	var text = '';
	var id = $(this).attr('id');
	var name = $(this).attr('name');
	var label = $(this).siblings('label').text();
	var h3 = $(this).parent().parent().find('h3').text();
	
	text += "if(isset($_POST['"+id+"'])) {\n";
	text += "	$form->fields[] = new FormField('"+label+"','');\n";
	text += "	$subtotal += $pricing['"+id+"'];\n";
	text += "}\n\n";
	
	$('#notes').append(text)
	
});


$('input[type=radio]').each(function(){
	
	var text = '';
	var id = $(this).attr('id');
	var name = $(this).attr('name');
	var label = $(this).siblings('label').text();
	var h3 = $(this).parent().parent().find('h3').text();

	text += "if(isset($_POST['"+name+"']) && $_POST['"+name+"'] == '"+id+"') {\n";
	text += "	$form->fields[] = new FormField('"+h3+"','"+label+"');\n";
	text += "	$subtotal += $pricing['"+id+"'];\n";
	text += "}\n\n";

	$('#notes').append(text)
	
});

	$('input,textarea,select').each(function(){$('#'+$(this).attr('id')).val($(this).siblings('label').text());});

*/
	
});

var form = function(){
	
	return {
		
		calculate: function(){
			
			if($('#summary').length > 0) {
		
				$('#summary').empty();
				var total = 0;
				var gst_total = 0;
		
				$('fieldset').each(function(){
		
					var $fs 			= $(this);
					var $checks			= $fs.find('input[type=checkbox],input[type=radio]');
					var $subtotal 		= $fs.find('.subtotalval');
					var $tax 			= $fs.find('.taxval');
					var fieldset_total 	= 0;
					var tax_total		= 0;
			
					$checks.each(function(){
						if($(this).is(':checked')) {
							var price = prices[$(this).attr('id')];
							fieldset_total += price;
						}
					});
			
					tax_total = fieldset_total * prices.tax;
			
					$subtotal.val(fieldset_total.toFixed(2));
					$tax.val(tax_total.toFixed(2));
					
					total += fieldset_total;
					gst_total += tax_total;
					
					if($checks.length > 0) {
						$('#summary').append('<tr><td>'+$fs.find('h3').text()+'</td><td>$'+fieldset_total.toFixed(2)+'</td></tr>')
					}
			
				});
				
				$('#summary').append('<tr class="tax"><td>Tax</td><td>$'+gst_total.toFixed(2)+'</td></tr>')
				
				$('#summary').append('<tr class="total"><td>Total</td><td>$'+(total + gst_total).toFixed(2)+'</td></tr>')
				$('#summary tr:even').addClass('odd');	// these selectors are zero-based, so the first is 0,
				$('#summary tr:odd').addClass('even');	// which is even.  That's why this looks odd.
			}
		}
	};
	
}();
