$(function() {
	if (typeof(document.getElementById('cart'))!='undefined') {
		update_cart();
	}
	max_box_height = 0;
	$(".categories_product_box_packages_top").each(function() {
		if ($(this).height() > max_box_height)
			max_box_height = $(this).height();
	});
	$(".categories_product_box_packages_top").each(function() {
		$(this).height(max_box_height);
	});
	if ($(".outer_main").height() < 600) $(".footer_line").css("margin-top", (800-$(".outer_main").height())+"px");
});

function update_cart() {
	if (typeof(no_cart_details)=='undefined') {
		no_cart_details = 0;
	}
	$.post('hgw/cart.php', {'details': no_cart_details}, function(data) {
		$('#cart').html(data);
	});
}
function add_product(products_id) {
	url = 'hgw/cart.php';
	qty = document.getElementById('qty_'+products_id).value;
	$.post(url, {'do': 'add_products', 'products_id': products_id, 'qty': qty}, function(data) {
		$('#cart').html(data);
		if (data.indexOf("PER ORDER") > -1) {
			alert($('.order_box_title:first').html());
		}
	});
}

function remove_product(products_id, qty) {
	if (typeof (qty) == 'undefined') qty = -1;
	url = 'hgw/cart.php';
	$.post(url, {'do': 'remove_products', 'products_id': products_id, 'qty': qty}, function(data) {
		$('#cart').html(data);
		$('#products_id_'+products_id).css("display", "none");
		$('#products_id_'+products_id+' .cart_item').css("display", "none");
		change_subtotal_price(0);
	});
}

function update_product(products_id, qty) {
	if (typeof (qty) == 'undefined') qty = -1;
	url = 'hgw/cart.php';
	$.post(url, {'do': 'update_product', 'products_id': products_id, 'qty': qty}, function(data) {
		$('#cart').html(data);
		if (data.indexOf("PER ORDER") > -1) {
			alert($('.order_box_title:first').html());
		}
	});
}

function change_subtotal_price(products_id) {
	temp = $('#individual_price_'+products_id);
	if (temp)
		price_raw = $('#individual_price_'+products_id).val();
	temp = $('#qty_'+products_id);
	if (temp)
		qty_raw = temp.val();
	subtotal_price = 0;
	if (isNaN(price_raw) || isNaN(qty_raw)) {
		$('#subtotal_'+products_id).html('0.00');
	}
	else {
		price = parseFloat(price_raw);
		qty = parseFloat(qty_raw);
		if (qty < 0) qty = 0;
		if (isNaN(price) || isNaN(qty)) {
			$('#subtotal_'+products_id).html('0.00');
		}
		else {
			temp = price*qty;
			subtotal_price = price * qty;
			$('#subtotal_'+products_id).html(subtotal_price.toFixed(2));
		}
	}
	totals = 0;
	$('.cart_item').each(function() {
		if ($(this).css("display") != "none") {
			temp2 = $(this).attr("id");
			temp2 = temp2.substring(4);
			qty2 = $(this).val();
			if (qty2 < 0) qty2  = 0;
			price2 = parseFloat($('#individual_price_'+temp2).val());
			if (!isNaN(qty2)) {
				totals += price2 * qty2;
			}
		}
	});
	var delivery_cost;
	if (totals >= 180 || $("body").html().indexOf('Self') > -1) delivery_cost = 0;
	else if (totals >= 100) delivery_cost = 10;
	else delivery_cost = 20;
	$('#delivery_cost').html('$'+delivery_cost.toFixed(2));
	
	gst1 = (totals+delivery_cost)*0.07;
	temp = totals;
	temp = temp.toFixed(2);
	$('#subtotal').html('$'+temp);
	$('#gst').html('$'+gst1.toFixed(2));
	var total;
	total = totals + gst1 + delivery_cost;
	$('#total').html('$'+total.toFixed(2));
}

