/**
 * @author red
 */

(function() {
	var price_recalc = function() {
		var total = 00;
		
		$(':selected, :checked').each(function() {
			var $$ = $(this);
			var value = $$.attr('class') || 0;
			if (value) {
				reg = /.*value_([0-9]*).*/;
				if (reg.exec(value)) {
					value = RegExp.$1;
					total += parseInt(value);
				}
			} else value = 0;
		});
		
		$('#total_price').html('$' + total);
		$('#total').val('$' + total);
	}
	
	var validate_form = function() {
		var okay = true;
		var message = '';
		$('input.required').each(function() {
			var $$ = $(this);
			if (!$$.val()) {
				if (!message) $$.focus(),
				message += 'Please enter a value for the ' + $$.attr('id') + ' field.\n';
				okay = false;
			}
		});
		if (message) alert(message);
		return okay;
	}
	
	$(document).ready(function() {
		$('select').change(price_recalc);
		$('input.radio').click(price_recalc);	
		$('form').submit(validate_form);
		$('#commit').show();
		price_recalc();
	});
}())
