Pages

Wednesday, September 8, 2010

Manage Cart Function of Online Fresh Food Store

//---------------------------------------------------------------------||
// FUNCTION:    ManageCart                                             ||
// PARAMETERS:  Null                                                   ||
// RETURNS:     Product Table Written to Document                      ||
// PURPOSE:     Draws current cart product table on HTML page          ||
//---------------------------------------------------------------------||
function ManageCart( ) {
   var iNumberOrdered = 0;    //Number of products ordered
   var fTotal         = 0;    //Total cost of order
   var fTax           = 0;    //Tax amount
   var fShipping      = 0;    //Shipping amount
   var strTotal       = "";   //Total cost formatted as money
   var strTax         = "";   //Total tax formatted as money
   var strShipping    = "";   //Total shipping formatted as money
   var strOutput      = "";   //String to be written to page
   var bDisplay       = true; //Whether to write string to the page (here for programmers)

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

   if ( bDisplay )
      strOutput = "<table id=\"myTable\"><thead><tr><th></th>" +
                  "<th>"+strILabel+"</th>" +
                  "<th>"+strDLabel+"</th>" +
                  "<th>"+strQLabel+"</th>" +
                  "<th>"+strPLabel+"</th>" +
                  (DisplayShippingColumn?"<th>"+strSLabel+"</th>":"") +
                  "<th>"+strRLabel+"</th></tr></thead><tbody>";

   if ( iNumberOrdered == 0 ) {
      strOutput += "<tr><td colspan=\"7\" class=\"alertText\"><center>"+strCEmpty+"</center></td></tr>";
      bDisplay = false;
      strOutput += "</tbody></table>";
   }

   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 );          // Shipping Cost
      fields[5] = database.substring( Token4+1, database.length ); //Additional Information

      fTotal     += (parseInt(fields[1]) * parseFloat(fields[2]) );
      fShipping  += (parseInt(fields[1]) * parseFloat(fields[4]) );
      fTax        = (fTotal * TaxRate);
      strTotal    = moneyFormat(fTotal);
      strTax      = moneyFormat(fTax);
      strShipping = moneyFormat(fShipping);

      if ( bDisplay ) {
         strOutput += "<tr><th></th><td>"  + fields[0] + "</td>";

         if ( fields[5] == "" )
            strOutput += "<td>"  + fields[3] + "</td>";
         else
            strOutput += "<td>"  + fields[3] + " - "+ fields[5] + "</td>";

         strOutput += "<td><input type=\"text\" name=\"Q\" class=\"formBox\" size=\"2\" value=\"" + fields[1] + "\" onChange=\"ChangeQuantity("+i+", this.value);\"></td>";
         strOutput += "<td>"+ MonetarySymbol + moneyFormat(fields[2]) + "/item</td>";

         if ( DisplayShippingColumn ) {
            if ( parseFloat(fields[4]) > 0 )
               strOutput += "<td>"+ MonetarySymbol + moneyFormat(fields[4]) + "/item</td>";
            else
               strOutput += "<td>N/A</td>";
         }

         strOutput += "<td><input type=\"image\" src\=\"..\/_img\/btn\/btn_remove.gif\" value=\" "+strRButton+" \" onClick=\"RemoveFromCart("+i+")\"></td></tr>";
      }

      if ( AppendItemNumToOutput ) {
         strFooter = i;
      } else {
         strFooter = "";
      }
      if ( HiddenFieldsToCheckout ) {
         strOutput += "<input type=hidden name=\"" + OutputItemId        + strFooter + "\" value=\"" + fields[0] + "\">";
         strOutput += "<input type=hidden  name=\"" + OutputItemQuantity  + strFooter + "\" value=\"" + fields[1] + "\">";
         strOutput += "<input type=hidden name=\"" + OutputItemPrice     + strFooter + "\" value=\"" + fields[2] + "\">";
         strOutput += "<input type=hidden name=\"" + OutputItemName      + strFooter + "\" value=\"" + fields[3] + "\">";
         strOutput += "<input type=hidden name=\"" + OutputItemShipping  + strFooter + "\" value=\"" + fields[4] + "\">";
         strOutput += "<input type=hidden name=\"" + OutputItemAddtlInfo + strFooter + "\" value=\"" + fields[5] + "\">";
      }

   }

   if ( bDisplay ) {
      strOutput += "<tr><th>"+strSUB+"</th>";
      strOutput += "<td></td><td></td><td></td><td colspan=3>" + MonetarySymbol + strTotal + "</td>";
      strOutput += "</tr>";

      if ( DisplayShippingRow ) {
         strOutput += "<tr><th>"+strSHIP+"</th>";
         strOutput += "<td></td><td></td><td></td><td colspan=3>" + MonetarySymbol + strShipping + "</td>";
         strOutput += "</tr>";
      }

      if ( DisplayTaxRow || TaxByRegion ) {
         if ( TaxByRegion ) {
            strOutput += "<tr><th>"+strTAX+"</th>";
            strOutput += "<td></td><td></td><td></td><td colspan=3>";
            strOutput += "<input type=radio name=\""+OutputOrderTax+"\" value=\"" + strTax + "\">";
            strOutput += TaxablePrompt + ": " + MonetarySymbol + strTax;
            strOutput += "<BR><input type=radio name=\""+OutputOrderTax+"\" value=\"0.00\">";
            strOutput += NonTaxablePrompt + ": " + MonetarySymbol + "0.00";
            strOutput += "</td>";
            strOutput += "</tr>";
         } else {
            strOutput += "<tr><th>"+strTAX+"</th>";
            strOutput += "<td colspan=5>" + MonetarySymbol + strTax + "</td>";
            strOutput += "</tr>";
         }
      }

      if ( !TaxByRegion ) {
         strOutput += "<tr><th>"+strTOT+"</th>";
         strOutput += "<td colspan=5>" + MonetarySymbol + moneyFormat((fTotal + fShipping + fTax)) + "</td>";
         strOutput += "</tr>";
      }
      strOutput += "<tr><th></th><td colspan=6><center><input type\=\"image\" src\=\"..\/_img\/btn\/btn_checkout.gif\" border=\"0\"></center></td></tr>";
      strOutput += "</tbody></table>";

      if ( HiddenFieldsToCheckout ) {
         strOutput += "<input type=hidden name=\""+OutputOrderSubtotal+"\" value=\""+ MonetarySymbol + strTotal + "\">";
         strOutput += "<input type=hidden name=\""+OutputOrderShipping+"\" value=\""+ MonetarySymbol + strShipping + "\">";
         strOutput += "<input type=hidden name=\""+OutputOrderTax+"\"      value=\""+ MonetarySymbol + strTax + "\">";
         strOutput += "<input type=hidden name=\""+OutputOrderTotal+"\"    value=\""+ MonetarySymbol + moneyFormat((fTotal + fShipping + fTax)) + "\">";
      }
   }
   g_TotalCost = (fTotal + fShipping + fTax);

   document.write(strOutput);
   document.close();
}