function total_charge(unitCost_quantity)
{
		unit_cost = unitCost_quantity.split('_');
	total = parseInt(unit_cost[1]);	
	
	perUnit = (parseInt(unit_cost[1]) / parseInt(unit_cost[0]));

	$('selectQuantityCharge').innerHTML = '$' + total.toFixed(2) + " + tax";
	
	$('costPerUnit').innerHTML = '$' + perUnit.toFixed(2) + ' each';
	
}


function total_cost_old(current_total,unit_cost,uid)
{
	var unit_cost = unit_cost.split('_');
	var grandTotal = $('grandTotal').innerHTML;

	// grandtotal minus current item
	var a = parseFloat(grandTotal.replace('$','')) - parseFloat(current_total.replace('$',''));
	
	total = parseInt(unit_cost[1]);
	divTag = "totalCost" + uid;
	$(divTag).innerHTML = '$' + total.toFixed(2);
	
	//update the per unit cost
	var per_unit_cost =  (parseInt(unit_cost[1])/parseInt(unit_cost[0]));
	$('perUnitCost'+uid).innerHTML = '$' + per_unit_cost.toFixed(2);
	
	newGrandTotal = total + a;
	$('grandTotal').innerHTML = '$' + newGrandTotal.toFixed(2);
}



function total_cost(current_total,new_quantity,unit_cost,uid,paper_percent)
{
	
	//var unit_cost = unit_cost.split('_');
	var grandTotal = $('grandTotal').innerHTML;
	current_total = removeComma(current_total);
	current_total_num = parseFloat(current_total.replace('$',''));
	grandTotal = removeComma(grandTotal);
	grandTotal_num = parseFloat(grandTotal.replace('$',''));

	// grandtotal minus current item
	var grandtotal_minus_current = grandTotal_num - current_total_num;
	new_current_total = new_quantity * unit_cost;
	


	//update paper cost cell
	if(paper_percent>0)
	{	
		paper_cost = new_quantity * unit_cost * paper_percent / 100;
		paper_cost = Math.round( (paper_cost  * 100).toFixed(2)) /100;
		new_current_total = new_current_total + paper_cost;
	}


	$('totalCost'+uid).innerHTML = '$' + formatCurrency(new_current_total);
	newGrandTotal = new_current_total + grandtotal_minus_current;
	$('grandTotal').innerHTML = '$' + formatCurrency(newGrandTotal); //.toFixed(2);
	setSessionPreTaxTotal(newGrandTotal); //this is defined in cart2.php
}

//As replace function cannot replace a comma, following is a solution to replace a comma !!
function removeComma(num) {
var temp1 = 0;
var xarray = num.split(",");
if ( xarray[2] != null ) {
   temp1 = xarray[0]+xarray[1]
+xarray[2]; return temp1 } 
else if ( xarray[1] != null ) {
          temp1 = xarray[0]
+xarray[1]; return temp1}
       else
{temp1 = xarray[0]; return temp1 }         
}



function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + num + '.' + cents);
}
