//================================
// NOP Design JavaScript Shopping Cart
// - variables, general and cookie-handling functions.
//                                                                     
// 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.  You must keep this comment unchanged in
// your code.  For more information contact FreeCart@NopDesign.com.
// JavaScript Shop Module, V.4.4.0
//================================

// Global vars.
var database, Token0, Token1, Token2, Token3, Token4, Token5, Token6, Token7, fields;
var fields = new Array;
var totDatabase, totToken0, totToken1, totFields;
var totFields = new Array;
var MaxOrderLines = 12; // Max cart lines (limit: 20 cookies, 4Kb).
var maxChars = 3500;    // To prevent exceeding cookie size limit. 
var cookiesEnabled = false;
var pkgWeight = 25;     // weight of satchel/parcel itself to deduct from intended weight (grams)

// Calculate milliseconds to ensure cookie is expired and thus deleted.
var oneYear = 365 * 24 * 60 * 60 * 1000;
var expDate = new Date();
expDate.setTime(expDate.getTime() - oneYear);

//Options for Everyone:
MonetarySymbol        = '$';
DisplayNotice         = true;
DisplayShippingCol    = false;
DisplayShippingColChk = false; // Chk - for checkout page.
DisplayShippingOpt    = true;  // delivery options row, if this is false must have item shipping amts.
DisplayShippingRow    = true;
DisplayShippingRowChk = true;

DisplayTaxRow         = true;
TaxRate               = 0.10;  // GST (also to update in cart-include.php)
TaxByRegion           = true;
TaxPrompt             = 'For tax purposes, please indicate your residency before continuing.';
TaxablePrompt         = 'Australian Resident (10%)';
NonTaxablePrompt1     = 'Australian External Territory Resident (0%)';
NonTaxablePrompt2     = 'Overseas Resident (0%)';
ShowTaxableAmt        = false;
MinimumOrder          = 0.00; // Minimum order total.
MinimumOrderWeight    = 25.00; // Minimum order weight (grams).
MinimumOrderPrompt    = 'Your order is below our minimum order, please order more before checking out.';

//Payment Processor Options:
PaymentProcessor      = 'PayPal';

//Options for Programmers: (PayPal vars)
OutputItemId          = 'item_number_';
OutputItemName        = 'item_name_';
OutputItemQuantity    = 'quantity_';
OutputItemPrice       = 'amount_';
OutputItemShipping    = 'shipping_';
OutputItemExtraInfoNm = 'on0_';
OutputItemExtraInfoVl = 'Ordering';
OutputItemExtraInfo   = 'os0_';
OutputItemExtraInfoNm2 = 'on1_';
OutputItemExtraInfoVl2 = 'Instructions';
OutputItemExtraInfo2   = 'os1_';

OutputOrderSubtotal   = 'SUBTOTAL';
OutputOrderShipping   = 'handling_cart';
OutputOrderTax        = 'tax_cart';
OutputOrderTotal      = 'TOTAL';
OutputOrderCustom     = 'custom';
AppendItemNumToOutput = true;
HiddenFieldsToCheckout = true;

//================================
// YOU DO NOT NEED TO MAKE ANY MODIFICATIONS BELOW THIS LINE        
//================================
// Language Strings set via includes/cart-include.php - English in this case.

//--------------------------------
// 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)));
}

//--------------------------------
// 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);
}

//--------------------------------
// 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);
}

//--------------------------------
// 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" : "");
}

//--------------------------------
// 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 + "=" + 
         "; expires=" + expDate.toGMTString() +
         ((path) ? "; path=" + path : "") +
         ((domain) ? "; domain=" + domain : "");
   }
}

//--------------------------------
// PARAMETERS:  Number to be formatted
// RETURNS:     Formatted Number
// PURPOSE:     Reformats Dollar Amount to #.## format
// now in libMain.js
//--------------------------------

//--------------------------------
// PARAMETERS:  Radio button to check
// RETURNS:     True if a radio has been checked
// PURPOSE:     Form fillin validation
//--------------------------------

function RadioChecked(radiobutton) {
   var bChecked = false;
   var rlen = radiobutton.length;
   for (i=0; i<rlen; i++) {
      if (radiobutton[i].checked) bChecked = true;
   }    
   return bChecked;
} 

//================================

// Misc functions.
function getDatabaseFields(NewOrder) {
   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);
   Token5 = database.indexOf("|", Token4+1);
   Token6 = database.indexOf("|", Token5+1);
   Token7 = database.indexOf("|", Token6+1);

   fields = new Array;
   fields[0] = database.substring(0, Token0);        // item id
   fields[1] = database.substring(Token0+1, Token1); // qty
   fields[2] = database.substring(Token1+1, Token2); // price (ex-GST)
   fields[3] = database.substring(Token2+1, Token3); // item name
   fields[4] = database.substring(Token3+1, Token4); // shipping
   fields[5] = database.substring(Token4+1, Token5); // weight
   fields[6] = database.substring(Token5+1, Token6); // postal pkg - see nopcart-chk.js
   fields[7] = database.substring(Token6+1, Token7); // price (inc-GST)
   fields[8] = database.substring(Token7+1, database.length); // additional info
}

function getTotDatabaseFields() {
   totDatabase = GetCookie("totInfo");
   if (totDatabase == null) totDatabase = "";

   totToken0 = totDatabase.indexOf("|", 0);
   totToken1 = totDatabase.indexOf("|", totToken0+1);

   totFields = new Array;
   totFields[0] = totDatabase.substring(0, totToken0);           // ship parcel to this zone (AUS, A, B, C & RW [D]) 
   totFields[1] = totDatabase.substring(totToken0+1, totToken1); // ship parcel to this country
   totFields[2] = totDatabase.substring(totToken1+1, totDatabase.length); // if country Australia, 'Regular Post' or 'Express Post' else if OS, Express Courier.

   for (i=0; i<totFields.length; i++) {
      if (totFields[i] == null) totFields[i] = "";
   }
}

// Session cookies must be enabled for shopping cart to work.
function checkCookiesEnabled() {
   SetCookie("TempTestCookie", "yes", null, "/");
   if (document.cookie.indexOf("TempTestCookie=") != -1) {
      cookiesEnabled = true;
      DeleteCookie("TempTestCookie", "/");
   }
}

// When payment completed and thank you page shown.
function clearCart() {
   iOrderLines = GetCookie("OrdLines");
   if (iOrderLines == null) iOrderLines = 0;
   DeleteCookie("OrdLines", "/");
   
   for (i=1; i<=iOrderLines; i++) {
      OrderLn = "OrdLn-" + i;
      DeleteCookie(OrderLn, "/");
   }

   DeleteCookie("totInfo", "/");
   DeleteCookie("GSTInfo", "/");
}
