//Shopping cart Javascript functions

var MaxCartItems = 10;
var ReferrerLife = 1000 * 60 * 60 * 24 * 5;
var pageloc = self.location;
var storeID='EDTM';

//using ASCII values 182, 183, 184, 185, 186, 187 as separators
var sp1="¶";
var sp2="·";
var sp3="¸";
var sp4="¹";
var sp5="º";
var sp6="»";

function SaveReferrer()
  {
  CookieName = storeID;
  CookieValue = GetCookie(CookieName);
  if(CookieValue == null){CookieValue = document.referrer;}
  var expdate = new Date();
  expdate.setDate(ReferrerLife);
  SetCookie(CookieName,CookieValue,expdate);
  }

function getCookieVal (offset)
  {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1) endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
  }

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 (name, value, expdate)
  {
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = expdate;
  var path = "/";
  var domain = null;
  var secure = false;

  document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
  }

function DeleteCookie(name)
  {
  var exp = new Date();
  exp.setTime (exp.getTime() - 1);  // This cookie is history
  var cval = GetCookie (name);
  if (cval != null){document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString() + ";path=/";}
  }

function AddItem(ID,Sdesc,Price,Qty,Weight,WeightUnit,Spship)
{
if (Qty <= 0)
  {
  var rc = alert('The Quantity must be greater than 0');
  return false;
  }

//confirmation for adding to shopping cart removed 03/22/00, re-added 03/27/00

if (confirm('Please confirm the addition of\n' +Qty+ ' "' +Sdesc+ '" ' +'to the shopping cart.'))
  {
  for(var NumItems = 0; NumItems<=MaxCartItems; NumItems++)
    {
    if(NumItems == MaxCartItems)
      {
      alert('Maximum of '+MaxCartItems+' different items can fit in the shopping cart, please go to the Checkout Page and submit your order.  Then empty your cart and shop some more later.');
      }
    else
      {
      var CookieName = storeID + NumItems;
      var CookieValue = GetCookie(CookieName);
      if(CookieValue == null)
        {
        CookieValue = "["+ID+sp1+Sdesc+sp2+Price+sp3+Qty+sp4+Weight+sp5+WeightUnit+sp6+Spship+"]";
        SetCookie(CookieName,CookieValue);
        break;
        }
      }
    }
  }
return true;
}

function FloatFormat(expr,decplaces)
  {
  var str = "" + Math.round(eval(expr) * Math.pow(10,decplaces));
  while(str.length <= decplaces){str = "0" + str;}
  var decpoint = str.length - decplaces;
  return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
  }

function ShowCart()
  {
  totprice = 0;
  itemlist = 0;
  document.writeln('<form>');
  document.writeln('<font size=2 face=arial color="#dd5555">To change quantity, edit number then click anywhere on this page</font>');
  document.writeln('<table border=2 cellspacing=1 cellpadding=5>');
  document.writeln('<tr><td align=center><b>ID</b></td><td align=center><b>Description</b></td><td align=center><b>Qty</b></td><td align=center><b>Price Each</b></td><td align=center><b>Price Total</b></td><td align=center><b>Action</b></td></tr>');
  for(NumItems = 0; NumItems<MaxCartItems; NumItems++)
    {
    CookieName = storeID +NumItems;
    CookieValue = GetCookie(CookieName);
    if(CookieValue != null)
      {
      for(var i=0;i<=CookieValue.length;i++)
        {
        if (CookieValue.substring(i,i+1) == '[') {itemstart = i+1;}
        else if (CookieValue.substring(i,i+1) == sp1)
          {
          ID = CookieValue.substring(itemstart, i);
          itemstart = i+1;
          }
        else if (CookieValue.substring(i,i+1) == sp2)
          {
          Sdesc = CookieValue.substring(itemstart, i);
          itemstart = i+1;
          }
        else if (CookieValue.substring(i,i+1) == sp3)
          {
          Price = CookieValue.substring(itemstart, i);
          itemstart = i+1;
          }
        else if (CookieValue.substring(i,i+1) == sp4)
          {
          itemend = i;
          Qty = CookieValue.substring(itemstart, itemend);
          ItemTotal = 0;
          ItemTotal = (eval(Price*Qty));
          temptotal = ItemTotal * 100;
          totprice = totprice + ItemTotal;
          itemlist=itemlist+1;
          FItemTotal = FloatFormat(ItemTotal,2);
          document.writeln('<tr><td><font size=2 face=arial>'+ID+'</font></td><td><font size=2 face=arial>'+Sdesc+'</font></td><td align=right><input method=post name=Qty'+NumItems+' type=text size=3 onChange=QtyChange('+NumItems+',Qty'+NumItems+'.value) value='+Qty+'></td><td align=right><font size=2 face=arial>'+Price+'</font></td><td align=right><font size=2 face=arial>'+FItemTotal+'</font></td><td><a href="javascript:RemoveItem('+NumItems+')"><font size=2 face=arial>Remove</font></a></td></tr>');
          }
        else if (CookieValue.substring(i,i+1) == sp5)
          {
          Weight = CookieValue.substring(itemstart, i);
          itemstart = i+1;
          }
        else if (CookieValue.substring(i,i+1) == sp6)
          {
          WeightUnit = CookieValue.substring(itemstart, i);
          itemstart = i+1;
          }
        else if (CookieValue.substring(i,i+1) == ']')
          {
          Spship = CookieValue.substring(itemstart, i);
          itemstart = i+1;
          }
        }
      }
    }
  Ftotprice = FloatFormat(totprice,2);
  document.writeln('<tr><td colspan=4><b>Sub Total</b></td><td align=right>'+Ftotprice+'</td><td></td></tr>');
  document.writeln('</table>');
  document.writeln('</form>');
  }

function checkprice(itemID, itemQty)
{
var pindex=getindex(itemID);
var prices=pitem[pindex].price.split('|');

if (itemQty<=4) return prices[0];
else if (itemQty<=9) return prices[1];
else return prices[2];
}

function QtyChange(itemnum,Qty)
  {
  CookieName = storeID +itemnum;
  CookieValue = GetCookie(CookieName);
  for(var i=0;i<=CookieValue.length;i++)
    {
    if (CookieValue.substring(i,i+1) == '['){itemstart = i+1;}
    else if (CookieValue.substring(i,i+1) == sp1)
      {
      ID = CookieValue.substring(itemstart, i);
      itemstart = i+1;
      }
    else if (CookieValue.substring(i,i+1) == sp2)
      {
      Sdesc = CookieValue.substring(itemstart, i);
      itemstart = i+1;
      }
    else if (CookieValue.substring(i,i+1) == sp3)
      {
      Price = CookieValue.substring(itemstart, i);
      itemstart = i+1;
      }
//CookieValue = "["+ID+sp1+Sdesc+sp2+Price+sp3+Qty+sp4+Weight+sp5+WeightUnit+sp6+Spship+"]";
    else if (CookieValue.substring(i,i+1) == sp4)
      {
      OldQty = CookieValue.substring(itemstart, itemend);
      itemstart = i+1;
      }
    else if (CookieValue.substring(i,i+1) == sp5)
      {
      Weight = CookieValue.substring(itemstart, i);
      itemstart = i+1;
      }
    else if (CookieValue.substring(i,i+1) == sp6)
      {
      WeightUnit = CookieValue.substring(itemstart, i);
      itemstart = i+1;
      }
    else if (CookieValue.substring(i,i+1) == ']')
      {
      Spship = CookieValue.substring(itemstart, i);
      itemstart = i+1;
      }
    }

  //for EDTM with quantity discounts

  Price = checkprice(ID, Qty)

  //end EDTM quantity discount checking

  NewCookieValue = "["+ID+sp1+Sdesc+sp2+Price+sp3+Qty+sp4+Weight+sp5+WeightUnit+sp6+Spship+"]";
  if(Qty <= 0){DeleteCookie(CookieName);}
  else{SetCookie(CookieName,NewCookieValue);}
  self.location = pageloc;
  }

function RemoveItem(itemnum)
  {
  CookieName = storeID +itemnum;
  DeleteCookie(CookieName);
  self.location = pageloc;
  }

function EmptyCart()
  {
  if(confirm('Are you sure you want to empty the shopping cart?'))
    {
    for(NumItems = 0; NumItems<MaxCartItems; NumItems++)
      {
      CookieName = storeID +NumItems;
      DeleteCookie(CookieName);
      }
    self.location = pageloc;
    }
  }

function Dollarize(expr)
  {
  return "$" + format(expr,2);
  }


var rsize = 10;		// rounding size
var oz_factor = 1;		// ounces to ounces
var lbs_factor = 0.0625;		// ounces to pounds
var mg_factor = 28349.5;		// ounces to miligrams
var g_factor = 28.3495;		// ounces to grams
var kg_factor = .0283495;	// ounces to kilograms
var UnitSize;	// weight number being converted
var OldUnit;	// converting from
var NewUnit;	// converting to
var NewUnitSize;	// new weight in new unit

function ConvertUnit(UnitSize,OldUnit,NewUnit)
{
// possible units
// oz
// lbs
// mg
// g
// kg

// if not oz, convert to oz first

NewUnitSize = 0;
TmpUnitSize = 0;

if(OldUnit != 'oz')
  {
  if(OldUnit == 'lbs'){TmpUnitSize = UnitSize / lbs_factor;}
  else if(OldUnit == 'mg'){TmpUnitSize = UnitSize / mg_factor;}
  else if(OldUnit == 'g'){TmpUnitSize = UnitSize / g_factor;}
  else if(OldUnit == 'kg'){TmpUnitSize = UnitSize / kg_factor;}
  }
else
  {
  // if oz, keep as is
  TmpUnitSize = UnitSize;
  }

// since TmpUnitSize is now oz, convert to NewUnit;
if(NewUnit == 'oz'){NewUnitSize = TmpUnitSize;}
else if(NewUnit == 'lbs'){NewUnitSize = TmpUnitSize * lbs_factor;}
else if(NewUnit == 'mg'){NewUnitSize = TmpUnitSize * mg_factor;}
else if(NewUnit == 'g'){NewUnitSize = TmpUnitSize * g_factor;}
else if(NewUnit == 'kg'){NewUnitSize = TmpUnitSize * kg_factor;}
}

function ShowInvoice()
{
totprice = 0;
itemlist = 0;
TotQty = 0;
TotWeight = 0;
FinalTotWeight = 0;
icount = 0;

document.writeln('<table border=2 cellspacing=5 cellpadding=5>');
document.writeln('<tr><td align=center><b>ID</b></td><td align=center><b>Description</b></td><td align=center><b>Qty</b></td><td align=center><b>Price Each</b></td><td align=center><b>Price Total</b></td><td align=center><b>Comments</b></td></tr>');

for(NumItems = 0; NumItems<MaxCartItems; NumItems++)
  {
  CookieName = storeID +NumItems;
  CookieValue = GetCookie(CookieName);

  if(CookieValue != null)
    {
    for(var i = 0;i <= CookieValue.length; i++)
      {
      if (CookieValue.substring(i,i+1) == '['){itemstart = i+1;}
      else if (CookieValue.substring(i,i+1) == sp1)
        {
        ID = CookieValue.substring(itemstart, i);
        itemstart = i+1;
        }
      else if (CookieValue.substring(i,i+1) == sp2)
        {
        Sdesc = CookieValue.substring(itemstart, i);
        itemstart = i+1;
        }
      else if (CookieValue.substring(i,i+1) == sp3)
        {
        Price = CookieValue.substring(itemstart, i);
        itemstart = i+1;
        }
      else if (CookieValue.substring(i,i+1) == sp4)
        {
        Qty = CookieValue.substring(itemstart, i);
        itemstart=i+1;
        }
      else if (CookieValue.substring(i,i+1) == sp5)
        {
        Weight = CookieValue.substring(itemstart, i);
        itemstart = i+1;
        }
      else if (CookieValue.substring(i,i+1) == sp6)
        {
        WeightUnit = CookieValue.substring(itemstart, i);
        itemstart = i+1;
        }
      else if (CookieValue.substring(i,i+1) == ']')
        {
        Spship = CookieValue.substring(itemstart, i);
        itemstart = i+1;
        TotQty = TotQty + parseInt(Qty);
        TotWeight = Weight * Qty;

        ItemTotal = 0;
        ItemTotal = (eval(Price*Qty));
        temptotal = ItemTotal * 100;
        totprice = totprice + ItemTotal;

        FItemTotal = FloatFormat(ItemTotal,2);
        FTotWeight = FloatFormat(TotWeight,2);

        ConvertUnit(TotWeight,WeightUnit,'oz');
        ConvertedWeight = NewUnitSize;
        FinalTotWeight = FinalTotWeight + NewUnitSize;
        FConvertedWeight = FloatFormat(ConvertedWeight,2);
        icount = icount + 1;
        document.writeln('<input type=hidden name="ID'+icount+'" value="'+ID+'">');
        document.writeln('<input type=hidden name="Description'+icount+'" value="'+Sdesc+'">');
        document.writeln('<input type=hidden name="Qty'+icount+'" value="'+Qty+'">');
        document.writeln('<input type=hidden name="Price Each'+icount+'" value="'+Price+'">');
        document.writeln('<input type=hidden name="Total Price Qty item(s)'+icount+'" value="'+FItemTotal+'">');
        document.writeln('<tr><td><font size=2 face=arial>'+ID+'</font></td><td><font size=2 face=arial>'+Sdesc+'</font></td><td align=right><font size=2 face=arial>'+Qty+'</font></td><td align=right><font size=2 face=arial>'+Price+'</font></td><td align=right><font size=2 face=arial>'+FItemTotal+'</font></td><td><input type=text name="Comments'+icount+'" size=10></td></tr>');
        }
      }
    }
  }

//if no items are put in shopping cart, go to the viewcart page
if (TotQty==0)
  {
  alert('There are no items in your shopping cart.\nPlease go to a product page and select the desired item(s) before trying to check out.');
  location.replace('viewcart.htm');
  }

Ftotprice = FloatFormat(totprice,2);
document.writeln('<tr><td colspan=4><b>Sub Total</b></td><td align=right>' + Ftotprice + '</td></tr>');
document.writeln('<input type=hidden name="Sub Total" value="' + Ftotprice +  '">');

residence = document.orderform.residence.value;
Tax = totprice * parseFloat(document.orderform.statetax.value);
CTax = 0;
FTax = FloatFormat(Tax,2);
FCTax = FloatFormat(CTax,2);
OCShipping = 0;
Shipping=0;

FShipping = FloatFormat(Shipping,2);
FOCShipping = FloatFormat(OCShipping,2);
document.writeln('<tr><td colspan=4><b>Shipping</b> &nbsp;(Shipping Charges will be calculated at time of shipping)</td><td align=right>&nbsp;</td></tr>');
document.writeln('<input type=hidden name="Shipping" value="' + FShipping + '">');

NatFGrandTotal = FloatFormat(totprice + CTax + Shipping,2);
ONatFGrandTotal = FloatFormat(totprice + Shipping + OCShipping,2);
FGrandTotal = FloatFormat(totprice + Tax + CTax + Shipping,2);
document.writeln('<tr><td colspan=4><b> Sales Tax</b></td><td align=right>'+FTax+'</td></tr>');
document.writeln('<input type=hidden name=" Sales Tax" value="'+FTax+'">');
document.writeln('<tr><td colspan=4><b><font color=red>Total for Non-residents of ' + residence + ' (EXCLUDING SHIPPING)</font></b></td><td align=right><font color=red>$ '+NatFGrandTotal+'</font></td></tr>');
document.writeln('<input type=hidden name="Total for Non-residents" value="$ '+NatFGrandTotal+'">');
document.writeln('<tr><td colspan=4><b><font color=red>Total for residents of ' + residence + ' (EXCLUDING SHIPPING)</font></b></td><td align=right><font color=red>$ '+FGrandTotal+'</font></td></tr>');
document.writeln('<input type=hidden name="Total for residents" value="$ '+FGrandTotal+'">');
document.writeln('<tr><td colspan=4><b><font color=red>Total for sales outside of the US (EXCLUDING SHIPPING)</font></b></td><td align=right><font color=red>$ '+ONatFGrandTotal+'</font></td></tr>');
document.writeln('<input type=hidden name="Total for sales outside of the US" value="$ '+ONatFGrandTotal+'">');
document.writeln('</table>');
}

//this function switches domains to Solarstop domain for secure server transactions

var newURL="HTTP://www.solarstop.net/";
var secURL="HTTPs://www.solarstop.net/secure";

function setURL(checkURL)
{
var thisURL= top.location.href;

//added to work on files locally - Lee
if (thisURL.indexOf('127.0.0.1') > -1) return;

thisURL=thisURL.toLowerCase();
newURL=newURL.toLowerCase();
secURL=secURL.toLowerCase();

var length=newURL.length;
var testURL=thisURL.substring(0,length);

if(newURL==testURL){return;}

var length=secURL.length;
var testURL=thisURL.substring(0,length);

if(secURL==testURL){return;}

var dot = thisURL.indexOf(".",1);
var slash = thisURL.indexOf("/",dot);
var onepage = thisURL.substring(slash);

newURL= newURL + checkURL + onepage;
top.location.href=newURL;
}

//setURL("edtm");

if (window.location.hostname.indexOf('www') == -1) location.replace('http://www.edtm.com'+location.pathname);

