Pages

Wednesday, September 8, 2010

Function to print total products in Online Fresh Food Store

//---------------------------------------------------------------------||
// FUNCTION: Print_total_products                                      ||
// PARAMETERS: true/false if you want "item(s)" added to string        ||
// RETURNS: Total cost currently racked up by shopper                  ||
// PURPOSE: Aesthetics                                                 ||
//---------------------------------------------------------------------||
function Print_total_products(bVerbose) {
var strOutput = ""; //String to be written to page
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]));
}

strOutput+=fTotal;
if ( bVerbose ) {
   if (fTotal == 1) {
      strOutput+=" </span> item</a></p>"
   }
   else {
      strOutput+=" </span> items</a></p>"
   }
}
document.write(strOutput);
}