function round_up(val, precision) {
    power = Math.pow(10, precision);
    poweredVal = Math.ceil(val * power);
    result = poweredVal / power;
    return result;
}

function calculate_shipping() {
	if( $('input').is(':checked') == true ) {
		var which_address = 'on';
		if( $('#billing_state').val() == 'TX' ) {
			var taxRate  = .0625;
		}
	} else {
		if( $('input').is(':checked') == false ) {
			var which_address = 'off';
			if( $('#shipping_state').val() == 'TX' ) {
					var taxRate  = 0.0625;
			}
		}
	}

		if( taxRate ) {
			var total = $('.simpleCart_finalTotal').html();
			var tax = parseInt( total.replace( '$', '' ) ) * taxRate;
			var final = round_up( tax, 2 );
		}
				
		if( taxRate ) {
			$('.simpleCart_taxCost').html('$' + final);
		} else {
			$('.simpleCart_taxCost').html('$0.00');
		}
};
	
$(document).ready(function() {
calculate_shipping();

$(function() {
	$('#ship_phone').keyup(function() {
		var curchr = this.value.length;
		var curval = $(this).val();
		if (curchr == 3) {
			$('#ship_phone').val("(" + curval + ")" + "-");
		} else if (curchr == 9) {
			$('#ship_phone').val(curval + "-");
		}
	});
	
	$('#bill_phone').keyup(function() {
		var curchr = this.value.length;
		var curval = $(this).val();
		if (curchr == 3) {
			$('#bill_phone').val("(" + curval + ")" + "-");
		} else if (curchr == 9) {
			$('#bill_phone').val(curval + "-");
		}
	});
});

	$('#shipping_process').click(function() {
		save_addies();
		calculate_shipping();
		return false;
	});

	$('#filter').change(function() {
		var url = $(this).val();
		if(url != '') {
			location.replace(url);
		}
	});
	
	$('#sort').change(function() {
		var url = $(this).val();
		if(url != '') {
			location.replace($(this).val());
		}
	});
	
	$('#same_billing').change(function() {
		if( $('input').is(':checked') == true ) {
			$('#shipping_form input').each(function() {
				$(this).attr('disabled', 'disabled');
			});
			$('#shipping_form select').each(function() {
				$(this).attr('disabled', 'disabled');
			});
			$('#shipping_form input').css('opacity', '0.4');
			$('#shipping_form select').css('opacity', '0.3');
			$('#shipping_form label').css('opacity', '0.3');
		} else {
			$('#shipping_form input').each(function() {
				$(this).attr('disabled', '');
			});
			$('#shipping_form select').each(function() {
				$(this).attr('disabled', '');
			});
			$('#shipping_form input').css('opacity', '1.0');
			$('#shipping_form select').css('opacity', '1.0');
			$('#shipping_form label').css('opacity', '1.0');
		}
		calculate_shipping();
	});
	
	$('#process').click(function() {
		var name = $('#cc_name').val();
		var number = $('#cc_number').val();

		if( name != '' && number != '' ) {
			process_order();
		} else {
			$('#notice').html('Please fill out your name and credit card info before checking out.').fadeIn();
		}
	});
	
	function save_addies() {
		var shipping = $('#shipping_form').serialize();
		var billing = $('#billing_form').serialize();

		$.post( 'account.php?' + shipping );
		$.post( 'account.php?' + billing );
	};
	
	function process_order() {
		save_addies();
	
		$('#notice').hide();
		$('#process').hide();
		$('.process_spinner').fadeIn();
		
		if( $('input').is(':checked') == true ) {
			var which_address = 'on';
		} else {
			var which_address = 'off';
		}
		
		var user_id = $('#user_id').val();
		var cc_name = $('#cc_name').val();
		var cc_number = $('#cc_number').val();
		var ccv_number = $('#ccv_number').val();
		var exp_month = $('#exp_month').val();
		var exp_year = $('#exp_year').val();
		var shipping = $("input[name='shipping_option']:checked").val();
		var tax = $('.simpleCart_taxCost').html();
		
		$.ajax({
			type: 'POST',
			url: 'process.php',
			dataType: 'json',
			data: ({
				address: which_address, 
				user: user_id,
				c_name: cc_name,
				c_number: cc_number,
				cv_number: ccv_number,
				e_month: exp_month,
				e_year: exp_year,
				u_shipping: shipping,
				u_tax: tax,
			}),
			
			success: function(msg) {
				if( msg.status == '2' ) {
					$('#notice').html(msg.message).fadeIn();
					$('#process').fadeIn();
					$('.process_spinner').hide();
				} else {
					if( msg.status == '1' ) {
						simpleCart.items = {};
						simpleCart.update();
						window.location.href = 'thanks.php';
					}
				}
			}
 		});
	};
});

simpleCart.currency = USD;
simpleCart.taxRate  = 0.0;	
simpleCart.cartHeaders = ["Thumb_image_noHeader", "Name" , "Price" , "decrement_noHeader" , "Quantity", "increment_noHeader", "remove_noHeader", "Total" ];
