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

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

function updateOrderCost(product_id, shipping_id, shipping_method, quantity, product_price) {
	$.post("updateShippingCost.php", { sessionid: "<?php echo session_id(); ?>", product_id: product_id, shipping_id: shipping_id, shipping_method: shipping_method, quantity: quantity, product_price: product_price }, 	
	function(returnShippingCost) {
		numShippingCost = null;
		ShippingCost = null;
		ShippingCost = returnShippingCost;
		numShippingCost = Number(ShippingCost);
		displayShippingCost = numShippingCost;
		//displayShippingCost = CommaFormatted(numShippingCost);
		$('#shippingCost').html('$' + displayShippingCost + ' US');	
		$('input[name=shipping]').val(displayShippingCost);	
		
		productPrice = null;
		productPrice = Number(product_price);
		productQuantity = Number(quantity);
		displayProductPrice = productPrice*productQuantity;		
		//displayProductPrice = CommaFormatted(displayProductPrice);
		//alert(displayProductPrice);
		$('#productDisplayPrice').html('$' + displayProductPrice + ' US');
		
		orderTotal = null;
		orderTotal = displayProductPrice + displayShippingCost;
		//orderTotal = CommaFormatted(orderTotal);
		$('#orderTotalPrice').html('$' + orderTotal + ' USD');	
		
		
	});	
}


function updateOrderCost_v2(product_id, quantity, product_price) {
	$.post("updateShippingCost_v2.php", { sessionid: "<?php echo session_id(); ?>", product_id: product_id, quantity: quantity, product_price: product_price }, 	
	function(returnShippingCost) {
		/*numShippingCost = null;
		ShippingCost = null;
		ShippingCost = returnShippingCost;
		numShippingCost = Number(ShippingCost);
		displayShippingCost = numShippingCost;
		//displayShippingCost = CommaFormatted(numShippingCost);
		$('#shippingCost').html('$' + displayShippingCost + ' US');	
		$('input[name=shipping]').val(displayShippingCost);	*/
		
		productPrice = null;
		productPrice = Number(product_price);
		productQuantity = Number(quantity);
		displayProductPrice = productPrice*productQuantity;		
		//displayProductPrice = CommaFormatted(displayProductPrice);
		//alert(displayProductPrice);
		$('#productDisplayPrice').html('$' + displayProductPrice + ' US');
		
		orderTotal = null;
		//orderTotal = displayProductPrice + displayShippingCost;
		orderTotal = displayProductPrice;
		//orderTotal = CommaFormatted(orderTotal);
		$('#orderTotalPrice').html('$' + orderTotal + ' USD');	
		$('input[name=amount_start]').val(orderTotal);
		
	});	
}

function updateOrderCost_v3(shipping_price, product_price, shipping_name) {
	numShippingCost = Number(shipping_price);
	displayShippingCost = numShippingCost;	
	$('#shippingCost').html('$' + displayShippingCost + ' US');	
	//$('input[name=shipping]').val(shipping_price);
	
	displayProductPrice = Number(product_price);
	orderTotal = null;
	orderTotal = displayProductPrice + displayShippingCost;
	//orderTotal = CommaFormatted(orderTotal);
	orderTotal = roundNumber(orderTotal,2)
	$('#orderTotalPrice').html('$' + orderTotal + ' USD');	
	$('input[name=item_name]').val('RealSign (Shipping: ' + shipping_name + ')');
}


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)
{
	//alert(amount);
	var delimiter = ","; // replace comma if desired
	//var a = amount.split('.',2)
	var a = amount;
	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 roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function saveOrderProduct(id, start_date_month, start_date_day, duration, duration_type, 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 }, 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>');
	});
}
