function removeOrderProduct(id) {
	$('#orderProductRow'+id).fadeOut();
	$.post("removeOrderProduct.php", { sessionid: "<?php echo session_id(); ?>", row: id } );
}


function updateOrderCost(id, duration, durationType, product_id, quantity) {
	
	$.post("updateOrderProductCost.php", { sessionid: "<?php echo session_id(); ?>", id: id, duration: duration, durationType: durationType, product_id: product_id, quantity: quantity },
   	function(returnCost) {
		var numCost = Number(returnCost);		
		numCost = CurrencyFormatted(numCost);
		numCost = CommaFormatted(numCost);		
		$('#orderCost'+id).html('<h2>$' + numCost + '</h2>');
   	});

}

function justalerttext() {
	alert ('on change');	
}

function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

function CommaFormatted(amount)
{
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}

function saveOrderProduct(id, start_date_month, start_date_day, duration, duration_type, quantity, user_id) {
	$.post("saveOrderProduct.php", { sessionid: "<?php echo $_SESSION['public_user']; ?>", id: id, start_date_month: start_date_month, start_date_day: start_date_day, duration: duration, duration_type: duration_type, quantity: quantity }, function() {
		$.post("calculateTotalCost.php", { sessionid: "<?php echo $_SESSION['public_user']; ?>", user_id: user_id },
		function(returnSubtotal) {
			Subtotal = null;
			numSubtotal = null;
			Waiver = null;
			GST = null;
			PST = null;
			Total = null;
			
			Subtotal = returnSubtotal;		
			numSubtotal = Number(Subtotal);
			Waiver = numSubtotal*0.10;
			GST = numSubtotal*0.05;
			PST = numSubtotal*0.05;
			Total = Number(numSubtotal+Waiver+GST+PST);		
			
			numSubtotal = CurrencyFormatted(numSubtotal);
			numSubtotal = CommaFormatted(numSubtotal);
			Waiver = CurrencyFormatted(Waiver);
			Waiver = CommaFormatted(Waiver);
			GST = CurrencyFormatted(GST);
			GST = CommaFormatted(GST);
			PST = CurrencyFormatted(PST);
			PST = CommaFormatted(PST);
			Total = CurrencyFormatted(Total);
			Total = CommaFormatted(Total);
			
			$('#OrderSubtotal').html('<h2>' + numSubtotal + '</h2>');
			$('#OrderWaiver').html(Waiver);
			$('#OrderGST').html(GST);
			$('#OrderPST').html(PST);
			$('#OrderTotal').html('<h2>' + Total + '</h2>');
		});
	
	});
	
}


function calculateTotalCost(user_id) {
	$.post("calculateTotalCost.php", { sessionid: "<?php echo session_id(); ?>", user_id: user_id },
	function(returnSubtotal) {
		var Subtotal = returnSubtotal;		
		var numSubtotal = Number(Subtotal);
		var Waiver = numSubtotal*0.10;
		var GST = numSubtotal*0.05;
		var PST = numSubtotal*0.05;
		var Total = Number(numSubtotal+Waiver+GST+PST);		
		
		numSubtotal = CurrencyFormatted(numSubtotal);
		numSubtotal = CommaFormatted(numSubtotal);
		Waiver = CurrencyFormatted(Waiver);
		Waiver = CommaFormatted(Waiver);
		GST = CurrencyFormatted(GST);
		GST = CommaFormatted(GST);
		PST = CurrencyFormatted(PST);
		PST = CommaFormatted(PST);
		Total = CurrencyFormatted(Total);
		Total = CommaFormatted(Total);
		
		$('#OrderSubtotal').html('<h2>' + numSubtotal + '</h2>');
		$('#OrderWaiver').html(Waiver);
		$('#OrderGST').html(GST);
		$('#OrderPST').html(PST);
		$('#OrderTotal').html('<h2>' + Total + '</h2>');
	});
}
