//=====================================================================||

//               NOP Design JavaScript Shopping Cart                   ||

//                                                                     ||

// For more information on SmartSystems, or how NOPDesign can help you ||

// Please visit us on the WWW at http://www.nopdesign.com              ||

//                                                                     ||

// Javascript portions of this shopping cart software are available as ||

// freeware from NOP Design under the GPL.  You must keep this comment ||

// unchanged in your code.  For more information contact               ||

// Scott@NopDesign.com                                                 ||

//                                                                     ||

// JavaScript Shop Module, V.4.0.0                                     ||

//=====================================================================||



//---------------------------------------------------------------------||

// FUNCTION:    CKquantity                                             ||

// PARAMETERS:  Quantity to                                            ||

// RETURNS:     Quantity as a number, and possible alert               ||

// PURPOSE:     Make sure quantity is represented as a number          ||

//---------------------------------------------------------------------||

function CKquantity(checkString) {

   strNewQuantity = "";



   for ( i = 0; i < checkString.length; i++ ) {

      ch = checkString.substring(i, i+1);

      if ( (ch >= "0" && ch <= "9") || (ch == '.') )

         strNewQuantity += ch;

   }



   if ( strNewQuantity.length < 1 )

      strNewQuantity = "1";



   return(strNewQuantity);

}





//---------------------------------------------------------------------||

// FUNCTION:    AddToCart                                              ||

// PARAMETERS:  Form Object                                            ||

// RETURNS:     Cookie to user's browser, with prompt                  ||

// PURPOSE:     Adds a product to the user's shopping cart             ||

//---------------------------------------------------------------------||

function AddToCart(a,b,c,d,e,f,g) {

   iNumberOrdered = 0;

   iNumberOrdered = GetCookie("NumberOrdered");

   iNumberOrdered++;

   itotal = g.value;

   notice = '';

   

for (i=0; i<itotal;i++)

  {

	if (b[i].value != "") 

	{

		//alert(a[i].value);

      	if ( a[i] == null )

	         strID_NUM    = "";

	      else

	         strID_NUM    = a[i].value;

	      

	      if ( b[i] == null )

	         strQUANTITY  = "1";

	      else

	         strQUANTITY  = b[i].value;

      

	      //if ( c[i] == null )

	         strPRICE     = "0.00";

	      //else

	      //   strPRICE     = c[i].value;

	      

	      if ( d[i] == null )

	         strNAME      = "";

	      else

	         strNAME      = d[i].value;

	      

	     // if ( e[i] == null )

	         strSHIPPING  = "0.00";

	     // else

	     //    strSHIPPING  = e[i].value;

	      

	     // if ( f[i] == null )

	         strADDTLINFO = "";

	     // else

	     //    strADDTLINFO = f[i].value;



	      dbUpdatedOrder = strID_NUM    + "|" + 

	                       strQUANTITY  + "|" +

	                       strPRICE     + "|" +

	                       strNAME      + "|" +

	                       strSHIPPING  + "|" +

	                       strADDTLINFO;

	

	      strNewOrder = "Order." + iNumberOrdered;

	      SetCookie(strNewOrder, dbUpdatedOrder, null, "/");

	      SetCookie("NumberOrdered", iNumberOrdered, null, "/");

	      notice = notice + strQUANTITY + " " + strNAME + " added to your cart.\n";

		iNumberOrdered++;

	  }

    }

	if (notice != "")

	      alert(notice);

	else

		alert ("Please enter a product quantity for your Quotation Request");



}





//---------------------------------------------------------------------||

// FUNCTION:    getCookieVal                                           ||

// PARAMETERS:  offset                                                 ||

// RETURNS:     URL unescaped Cookie Value                             ||

// PURPOSE:     Get a specific value from a cookie                     ||

//---------------------------------------------------------------------||

function getCookieVal (offset) {

   var endstr = document.cookie.indexOf (";", offset);



   if ( endstr == -1 )

      endstr = document.cookie.length;

   return(unescape(document.cookie.substring(offset, endstr)));

}





//---------------------------------------------------------------------||

// FUNCTION:    FixCookieDate                                          ||

// PARAMETERS:  date                                                   ||

// RETURNS:     date                                                   ||

// PURPOSE:     Fixes cookie date, stores back in date                 ||

//---------------------------------------------------------------------||

function FixCookieDate (date) {

   var base = new Date(0);

   var skew = base.getTime();



   date.setTime (date.getTime() - skew);

}





//---------------------------------------------------------------------||

// FUNCTION:    GetCookie                                              ||

// PARAMETERS:  Name                                                   ||

// RETURNS:     Value in Cookie                                        ||

// PURPOSE:     Retrieves cookie from users browser                    ||

//---------------------------------------------------------------------||

function GetCookie (name) {

   var arg = name + "=";

   var alen = arg.length;

   var clen = document.cookie.length;

   var i = 0;



   while ( i < clen ) {

      var j = i + alen;

      if ( document.cookie.substring(i, j) == arg ) return(getCookieVal (j));

      i = document.cookie.indexOf(" ", i) + 1;

      if ( i == 0 ) break;

   }



   return(null);

}





//---------------------------------------------------------------------||

// FUNCTION:    SetCookie                                              ||

// PARAMETERS:  name, value, expiration date, path, domain, security   ||

// RETURNS:     Null                                                   ||

// PURPOSE:     Stores a cookie in the users browser                   ||

//---------------------------------------------------------------------||

function SetCookie (name,value,expires,path,domain,secure) {

   document.cookie = name + "=" + escape (value) +

                     ((expires) ? "; expires=" + expires.toGMTString() : "") +

                     ((path) ? "; path=" + path : "") +

                     ((domain) ? "; domain=" + domain : "") +

                     ((secure) ? "; secure" : "");

}





//---------------------------------------------------------------------||

// FUNCTION:    DeleteCookie                                           ||

// PARAMETERS:  Cookie name, path, domain                              ||

// RETURNS:     null                                                   ||

// PURPOSE:     Removes a cookie from users browser.                   ||

//---------------------------------------------------------------------||

function DeleteCookie (name,path,domain) {

   if ( GetCookie(name) ) {

      document.cookie = name + "=" +

                        ((path) ? "; path=" + path : "") +

                        ((domain) ? "; domain=" + domain : "") +

                        "; expires=Thu, 01-Jan-70 00:00:01 GMT";

   }

}





//---------------------------------------------------------------------||

// FUNCTION:    MoneyFormat                                            ||

// PARAMETERS:  Number to be formatted                                 ||

// RETURNS:     Formatted Number                                       ||

// PURPOSE:     Reformats Dollar Amount to #.## format                 ||

//---------------------------------------------------------------------||

function moneyFormat(input) {

   var dollars = Math.floor(input);

   var tmp = new String(input);



   for ( var decimalAt = 0; decimalAt < tmp.length; decimalAt++ ) {

      if ( tmp.charAt(decimalAt)=="." )

         break;

   }



   var cents  = "" + Math.round(input * 100);

   cents = cents.substring(cents.length-2, cents.length)

           dollars += ((tmp.charAt(decimalAt+2)=="9")&&(cents=="00"))? 1 : 0;



   if ( cents == "0" )

      cents = "00";



   return(dollars + "." + cents);

}





//---------------------------------------------------------------------||

// FUNCTION:    RemoveFromCart                                         ||

// PARAMETERS:  Order Number to Remove                                 ||

// RETURNS:     Null                                                   ||

// PURPOSE:     Removes an item from a users shopping cart             ||

//---------------------------------------------------------------------||

function RemoveFromCart(RemOrder) {

   if ( confirm("Click 'Ok' to remove this product from your shopping cart.") ) {

      NumberOrdered = GetCookie("NumberOrdered");

      for ( i=RemOrder; i < NumberOrdered; i++ ) {

         NewOrder1 = "Order." + (i+1);

         NewOrder2 = "Order." + (i);

         database = GetCookie(NewOrder1);

         SetCookie (NewOrder2, database, null, "/");

      }

      NewOrder = "Order." + NumberOrdered;

      SetCookie ("NumberOrdered", NumberOrdered-1, null, "/");

      DeleteCookie(NewOrder, "/");

      location.href=location.href;

   }

}





//---------------------------------------------------------------------||

// FUNCTION:    GetFromCart                                            ||

// PARAMETERS:  Null                                                   ||

// RETURNS:     Product Table Written to Document                      ||

// PURPOSE:     Draws current cart product table on HTML page          ||

//---------------------------------------------------------------------||

function GetFromCart( fShipping ) {

   if( fShipping )

      WriteToForm( true, fShipping );

   else

      WriteToForm( true, 0 );

}





//---------------------------------------------------------------------||

// FUNCTION:    WriteToForm                                            ||

// PARAMETERS:  Null                                                   ||

// RETURNS:     Product hidden fields Written to Document              ||

// PURPOSE:     Draws current cart product hidden fields on HTML form  ||

//              if bDisplay == true, shows cart output as HTML table   ||

//---------------------------------------------------------------------||

function WriteToForm( bDisplay, fShipping ) {

   iNumberOrdered = 0;

   fTotal         = 0;

   strTotal       = "";

   strShipping    = "";

   strOutput      = "";

   h_color        = "#69ABA7"

   bg_color       = "#B0D2D1"

   iNumberOrdered = GetCookie("NumberOrdered");



   if ( bDisplay )

      strOutput = "<TABLE CLASS=\"nopcart\" width=600 border=1 cellpadding=0 cellspacing=0><TR>" +

                  "<TD CLASS=\"nopheader\" bgcolor="+ h_color +"><font size=2>&nbsp;<B>Product Id</B>&nbsp;</font></TH>" +

                  "<TD CLASS=\"nopheader\" bgcolor="+ h_color +"><font size=2>&nbsp;<B>Product Name/Description</B>&nbsp;</font></TH>" +

                  "<TD CLASS=\"nopheader\" bgcolor="+ h_color +"><font size=2>&nbsp;<B>Quantity</B>&nbsp;</font></TH>" +

                  "<TD CLASS=\"nopheader\" bgcolor="+ h_color +" align=center><font size=2>&nbsp;Remove From Cart&nbsp;</font></TH></TR>";



   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 );

      fields[1] = database.substring( Token0+1, Token1 );

      fields[2] = database.substring( Token1+1, Token2 );

      fields[3] = database.substring( Token2+1, Token3 );

      fields[4] = database.substring( Token3+1, Token4 );

      fields[5] = database.substring( Token4+1, database.length );



      fTotal     += (parseInt(fields[1]) * parseFloat(fields[2]) );

      fShipping  += (parseInt(fields[1]) * parseFloat(fields[4]) );

      strTotal    = moneyFormat(fTotal);

      strShipping = moneyFormat(fShipping);



      if ( bDisplay ) {

         strOutput += "<TR><TD CLASS=\"nopentry\" bgcolor="+ bg_color +">"  + fields[0] + "</TD>";

         if ( fields[5] == "" )

            strOutput += "<TD CLASS=\"nopentry\" bgcolor="+ bg_color +">"  + fields[3] + "</TD>";

         else

            strOutput += "<TD CLASS=\"nopentry\" bgcolor="+ bg_color +">"  + fields[3] + " - <I>"+ fields[5] + "</I></TD>";

        strOutput += "<TD CLASS=\"nopentry\" bgcolor="+ bg_color +">"  + fields[1] + "</TD>"

//                      +"<TD CLASS=\"nopentry\" bgcolor="+ bg_color +">$" + moneyFormat(fields[2]) + "/ea &nbsp;</TD>";



//         if ( parseFloat(fields[4]) > 0 )

//            strOutput += "<TD CLASS=\"nopentry\" bgcolor="+ bg_color +">$" + moneyFormat(fields[4]) + "/ea &nbsp;</TD>";

//         else

//            strOutput += "<TD CLASS=\"nopentry\" bgcolor="+ bg_color +">N/A</TD>";



         strOutput += "<TD CLASS=\"nopentry\" align=center bgcolor="+ bg_color +"><input type=button value=\"Remove\" onClick=\"RemoveFromCart("+i+")\" class=\"nopbutton\"></TD></TR>";

      }



      strOutput += "<input type=hidden name=\"ID_"       + i + "\" value=\"" + fields[0] + "\">";

      strOutput += "<input type=hidden name=\"QUANTITY_" + i + "\" value=\"" + fields[1] + "\">";

      strOutput += "<input type=hidden name=\"NAME_"     + i + "\" value=\"" + fields[3] + "\">";

      strOutput += "<input type=hidden name=\"ADDTLINFO_"+ i + "\" value=\"" + fields[5] + "\">";

   }

   if ( bDisplay ) {

      strOutput += "<input type=hidden name=\"p_num\" value=\"" + i + "\">";

      strOutput += "</TABLE><br>";

     // strOutput += "<br><TABLE CLASS=\"nopcart\" width=600 border=0 cellpadding=0 cellspacing=0><tr align=right><td>"

     // strOutput += "<TABLE CLASS=\"nopcart\" border=1 cellpadding=0 cellspacing=0>"

     // strOutput += "<TR><TD CLASS=\"noptotal\" COLSPAN=4 bgcolor="+ h_color +" align=left><font size=2>&nbsp;<B>SUBTOTAL</B>&nbsp;:</font></TD>";

     // strOutput += "<TD CLASS=\"noptotal\" COLSPAN=2 align=right bgcolor="+ bg_color +"><B>$" + strTotal + "</B></TD>";

     // strOutput += "</TR>";

     // strOutput += "<TR><TD CLASS=\"noptotal\" COLSPAN=4 bgcolor="+ h_color +" align=left><font size=2>&nbsp;<B>SHIPPING</B>&nbsp;:</font></TD>";

     // strOutput += "<TD CLASS=\"noptotal\" COLSPAN=2 align=right bgcolor="+ bg_color +"><B>$" + strShipping + "</B></TD>";

     // strOutput += "</TR>";

     // strOutput += "<TR><TD CLASS=\"noptotal\" COLSPAN=4 bgcolor="+ h_color +" align=left><font size=2>&nbsp;<B>TOTAL</B>&nbsp;:</font></TD>";

     // strOutput += "<TD CLASS=\"noptotal\" COLSPAN=2 align=right bgcolor="+ bg_color +"><B>$" + moneyFormat((fTotal + fShipping)) + "</B></TD>";

     // strOutput += "</TR>";

     // strOutput += "</TABLE></TABLE><";

     // strOutput += "<input type=hidden name=\"SUBTOTAL\" value=\"$" + strTotal + "\">";

     // strOutput += "<input type=hidden name=\"SHIPPING\" value=\"$" + strShipping + "\">";

     // strOutput += "<input type=hidden name=\"TOTAL\"    value=\"$" + moneyFormat((fTotal + fShipping)) + "\">";

   }



   document.write(strOutput);

   document.close();

}



//=====================================================================||

//               END NOP Design SmartPost Shopping Cart                ||

//                              V.4.0.0                                ||

//=====================================================================||




