$("#model_currency").change(function(e){
	if ($(this).val() != ''){
		window.location.href = '?currency=' + $(this).val();
	}
	e.preventDefault();
});

$('input.do_subtotal').change(function(){
	var price = $(this).attr('price');
	var quantity = $(this).val();
	var subtotal = price * quantity;
	
	var product = $(this).attr('id');
	
	if (product == 'sale_deluxe_pack'){
		$('#deluxe_pack_subtotal').text(subtotal)
	}
	if (product == 'sale_pleatmaker'){
		$('#pleatmaker_subtotal').text(subtotal)
	}
	if (product == 'sale_easy_clips_2'){
		$('#easy_clips_2_subtotal').text(subtotal)
	}
	if (product == 'sale_easy_clips'){
		$('#easy_clips_subtotal').text(subtotal)
	}
	
	var total = parseFloat($('#deluxe_pack_subtotal').text()) + parseFloat($('#pleatmaker_subtotal').text()) + parseFloat($('#easy_clips_2_subtotal').text()) + parseFloat($('#easy_clips_subtotal').text());
	
	$("#final_subtotal").text(total.toFixed(2));
	
	if ($("#discounted_subtotal").length > 0){
		var discount = $("#discounted_subtotal").attr('rel');
		$("#discounted_subtotal").text((total*((1 - discount/100))).toFixed(2));
	}
}).change();

if($('tbody.buyProduct').length){
	$('tbody.buyProduct td.quantity input')[0].focus();
}

$('form.table_validation').submit(function(){
	// Update Expiration Date
	$("#transaction_credit_card_expiration_date").val($('#expiration_date_month').val() + '/' + $('#expiration_date_year').val());
	
	$('form.table_validation tr').removeClass('field_with_errors');
	error = 0;
	
	$('form.table_validation :input.required').each(function(){
		if (!$(this).val()){
			$(this).parents('tr').first().addClass('field_with_errors');
			error++;
		}
	});
	
	if (error > 0){
		alert("There was a problem sending your order. Please make sure you complete the order form.\n\nThe fields you left blank are highlighted in red.");
		return false;
	}
});

$("#clickToRc").click(function(){
	$("#easy_clips_wrapper").fadeIn();
	return false;
});
$("#rc-close_w").click(function(){
	$("#easy_clips_wrapper").fadeOut();
	return false;
})

$(function(){
	$("#carrier_fields input").bind('change click', function(){
		if ($("#sale_carrier_").is(':checked')){
			$("#other_carrier_field").show().removeAttr('disabled', 'disabled').removeClass('disabled_bg');
		}
		else{
			$("#other_carrier_field").addClass('disabled_bg').attr('disabled', 'disabled').hide();
		}
	});
})
