$(document).ready(function(){
		// reset values for refresh
		$('#quantity_1').val("1");
		$('#delivery_1').val("Australia");
		$('#quantity_2').val("1");
		$('#delivery_2').val("Australia");

		// update cost
    $('.variable').change( function() {
			var id = $(this).attr("id");
			var myArray = id.split('_'); 
			var value = myArray[1];
			var price = $('#price_' + value).val();
			var quantity = $('#quantity_' + value).val();
			var delivery = $('#delivery_' + value).val();
			var separator = $('#separator_' + value).val();
			if (delivery=='Australia'){
				var del = 'A';
			} else{
				var del = "O";
			}
			if (quantity>separator){
				var postage = $('#postage_' + del + '_' + value + '_2').val();				
			} else {
				var postage = $('#postage_' + del + '_' + value + '_1').val();				
			}
			var cost = 'Cost: $' + (price*quantity).toFixed(2) + ' + $' + postage + ' postage and packing';
			$('#amount_' + value).text(cost);
    });

		// redirect to purchase details page
		$('.purchase_now').click( function(event) {
			event.preventDefault();
			var id = $(this).attr("id");
			var myArray = id.split('_'); 
			var value = myArray[1];
			var quantity = $('#quantity_' + value).val();
			var delivery = $('#delivery_' + value).val();
			var product_id = $('#product_id_' + value).val();
			window.location = "purchase_details.php?product_id=" + product_id + "&quantity=" + quantity + "&delivery=" + delivery ;
			return false;
   	});
});