Pages

Wednesday, September 8, 2010

Function for Checkout in the Cart

//---------------------------------------------------------------------||
// FUNCTION:    CheckoutCart                                           ||
// PARAMETERS:  Null                                                   ||
// RETURNS:     Product Table Written to Document                      ||
// PURPOSE:     Draws current cart product table on HTML page for      ||
//              checkout.                                              ||
//---------------------------------------------------------------------||
function CheckoutCart( ) {
   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)
   var strPP          = "";   //Payment Processor Description Field

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

   if ( TaxByRegion ) {
      QueryString_Parse();
      fTax = parseFloat( QueryString( OutputOrderTax ) );
      strTax = moneyFormat(fTax);
   }

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

   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]) );
      if ( !TaxByRegion ) fTax = (fTotal * TaxRate);
      strTotal    = moneyFormat(fTotal);
      if ( !TaxByRegion ) strTax = moneyFormat(fTax);
      strShipping = moneyFormat(fShipping);

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

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

         strOutput += "<td>" + fields[1] + "</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 += "</tr>";
      }

      if ( AppendItemNumToOutput ) {
         strFooter = i;
      } else {
         strFooter = "";
      }


if ( PaymentProcessor == 'pp' ) {
//Process hidden values for PayPal.
strOutput += "<input type=hidden name=\"item_number_"+ strFooter + "\" value=\"" + fields[0] + "\">";
strOutput += "<input type=hidden name=\"quantity_" + strFooter + "\" value=\"" + fields[1] + "\">";
strOutput += "<input type=hidden name=\"amount_" + strFooter + "\" value=\"" + fields[2] + "\">";
strOutput += "<input type=hidden name=\"item_name_" + strFooter + "\" value=\"" + fields[3] + "\">";
if (i == iNumberOrdered) {
strOutput += "<input type=hidden name=\"shipping_" + strFooter + "\" value=\"" + strShipping + "\">";
} else {
strOutput += "<input type=hidden name=\"shipping_" + strFooter + "\" value=\"0.00\">";
}
strOutput += "<input type=hidden name=\"on0_" + strFooter + "\" value=\"" + fields[5] + "\">";
}


      if ( PaymentProcessor != '' ) {
         //Process description field for payment processors instead of hidden values.
         //Format Description of product as:
         // ID, Name, Qty X
         strPP += fields[0] + ", " + fields[3];
         if ( fields[5] != "" )
            strPP += " - " + fields[5];
         strPP += ", Qty. " + fields[1] + "\n";
      } else {
         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></tr><tr><th>"+strSUB+"</th>";
      strOutput += "<td></td><td></td><td></td><td>" + MonetarySymbol + strTotal + "</td>";
      strOutput += "</tr>";

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

      if ( DisplayTaxRow || TaxByRegion ) {
         strOutput += "<tr><th>"+strTAX+"</th>";
         strOutput += "<td></td><td></td><td></td><td>" + MonetarySymbol + strTax + "</td>";
         strOutput += "</tr>";
      }

      strOutput += "<tr><th>"+strTOT+"</th>";
      strOutput += "<td></td><td></td><td></td><td>" + MonetarySymbol + moneyFormat((fTotal + fShipping + fTax)) + "</td>";
      strOutput += "</tr>";

      strOutput += "</tbody></table>";

   }

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