Pages

Wednesday, September 8, 2010

Function to print total products in the cart

//---------------------------------------------------------------------||
// FUNCTION: Print_total                                               ||
// PARAMETERS: true/false if you want MonetarySymbol added to string   ||
// RETURNS: Total cost currently racked up by shopper                  ||
// PURPOSE: Aesthetics                                                 ||
//---------------------------------------------------------------------||
function Print_total(bSymbol) {
var strOutput = ""; //String to be written to page
var strTotal = ""; //Total cost formatted as money
var fTotal = 0;
var iNumberOrdered = 0; //Number of products ordered

iNumberOrdered = GetCookie("NumberOrdered");
if ( iNumberOrdered == null )
iNumberOrdered = 0;

for ( i = 1; i <= iNumberOrdered; i++ ) {

NewOrder = "Order." + i;
database = "";
database = GetCookie(NewOrder);

Token0 = database.indexOf("|", 0);
Token1 = database.indexOf("|", Token0+1);
Token2 = database.indexOf("|", Token1+1);
Token3 = database.indexOf("|", Token2+1);
Token4 = database.indexOf("|", Token3+1);

fields = new Array;
fields[0] = database.substring( 0, Token0 ); // Product ID
fields[1] = database.substring( Token0+1, Token1 ); // Quantity
fields[2] = database.substring( Token1+1, Token2 ); // Price
fields[3] = database.substring( Token2+1, Token3 ); // Product Name/Description
fields[4] = database.substring( Token3+1, Token4 ); // Weight
fields[5] = database.substring( Token4+1, database.length ); //Additional Information

fTotal += (parseInt(fields[1]) * parseFloat(fields[2]) );
}
strTotal = moneyFormat(fTotal);
strOutput+=strTotal;
if ( bSymbol )
   strOutput = MonetarySymbol + strOutput
document.write(strOutput);
}