$(document).ready(function(){
	
	$('#nav li').hoverClass('over');
	
	
	// inputs (checkboxes) with a class of update will
	// use the value of the associated hiddn input (which is the checkoxes ID + Fee)
	// to change the total
	$('input.update').click(function() 
	{
		var target_id = '#'+$(this).attr('id')+'Fee';
		var linked_value = parseFloat($(target_id).attr('value'));
		var current_total = parseFloat($('input#total').attr('value'));
		var new_total = 0;
		if ($(this).attr('checked'))
		{
			new_total = current_total+linked_value;
		}
		else
		{
			new_total = current_total-linked_value;
		}
		$('input#total').attr('value', (new_total.toFixed(2)));
	});

	
	$('form.check_required').submit(function() 
	{
		var errors = false;
		var first_error_id = '';
		$('.required').each(function()
		{
			$(this).removeClass('error');
			
			if ($(this).attr('value') == '')
			{
				$(this).addClass('error');
				errors = true;
				if (first_error_id == '')
				{
					first_error_id = $(this).attr('name');
					// since these forms don't have ID"s in the input tag
					$(this).attr('id', first_error_id);
				}
			}
		});
		
		if (errors == true) 
		{
			alert('Please fill all required fields.');
			$('#'+first_error_id).focus();
			return false;
		}
	});
	
	$("input#autofillBill").click(function()
	{
		if ($(this).is(':checked'))
		{
			$('input#Billing_Address').attr('value', $('input[name="Contact_and_Mailing_Address"]').attr('value'));
			$('input#Billing_City').attr('value', $('input[name="Contact_and_Mailing_City"]').attr('value'));
			$('input#Billing_State').attr('value', $('input[name="State"]').attr('value'));
			var target_value = ($('select[name="Country"]').attr('value'));
			$('select[name="Billing_Country"] option[value="'+target_value+'"]').attr('selected', 'selected');
			$('input#Billing_Zip_Code').attr('value', $('input[name="Contact_and_Mailing_Zip_Code"]').attr('value'));
		}
		else
		{
			$('input.autofillBill').attr('value', '');
			$('select[name="Billing_Country"]').attr('selectedIndex', 0);
		}
	});
	

	
});

$.fn.hoverClass = function(c) {
	return this.each(function(){
		$(this).hover( 
			function() { $(this).addClass(c); },
			function() { $(this).removeClass(c); }
		);
	});
};