var oli = new Array();
var old = new Array();

function SID(sID)
{
   var s = new String();  s+= document.location;
   if (s.indexOf('checkout') == -1 && s.indexOf('nep_order') == -1 && s.indexOf('itemdetail') == -1)
      DTP_SetCookie('loc',document.location);

   s = "./itemdetail.htm?";
   s += sID; 
   document.location = s;
}


function GoToCart()
{
   var s = new String();  s+= document.location;
   if (s.indexOf('checkout') == -1 && s.indexOf('nep_order') == -1 && s.indexOf('itemdetail') == -1)
      DTP_SetCookie('loc',document.location);
   document.location = "./nep_order.html";
}

function KeepShop()
{
   document.location = DTP_GetCookie('loc','./index.htm');
}

function disMon(nVal)
{
     var sOT = new String();
     sOT += nVal;
     if (sOT.indexOf('.') == -1)
        sOT += ".00&nbsp";
     else if (sOT.indexOf('.') == sOT.length - 2)
        sOT += "0&nbsp";
     else
        sOT += "&nbsp";
     return sOT;
}

function updateTotals()
{
  if (!document.getElementById)
      return;

  var objPrice;
  var objQuant;
  var objTotal;

  var i = -1;
  var nOrdTotal = 0;
  var sItemNo = "";
  var bError = 0;

  while ( (sItemNo = getNextCartItem(sItemNo, 1)) != "")
  {
     if (isItemNull(sItemNo))
        continue;

     i++;

     var nlT = 0;
     var nQuan = 0;
     var sTotal = new String();

     objPrice = document.getElementById("ip"+i);
     objQuant = document.getElementById("iq"+i);
     objTotal = document.getElementById("it"+i);

     nQuan = objQuant.value;
     if (isNaN(nQuan))
     {
        alert("The Quantity for item " +(i+1) + " must be a number.");
        bError = 1;
        break;
     }
     else
     {
        setIQ(sItemNo, nQuan);
        nlT = nQuan * parseFloat(objPrice.innerHTML);
     }

     sTotal += nlT;
     nOrdTotal += nlT;

     if (sTotal.indexOf('.') == -1)
        sTotal += ".00&nbsp";
     else if (sTotal.indexOf('.') == sTotal.length - 2)
        sTotal += "0&nbsp";
     else
        sTotal += "&nbsp";

     objTotal.innerHTML = sTotal;

  }


  //write out order total
  objTotal = document.getElementById("ordt");
  var sTotal = new String();
  sTotal += "<font color=9C0000><b>$";
  if (!bError)
  {
    sTotal += nOrdTotal;
    if (sTotal.indexOf('.') == -1)
       sTotal += ".00&nbsp";
    else if (sTotal.indexOf('.') == sTotal.length - 2)
       sTotal += "0&nbsp";
    else
       sTotal += "&nbsp";
  }
  else
    sTotal += "----";

  sTotal += "</b></font>";
  objTotal.innerHTML = sTotal;

  objTotal = document.getElementById("ship");
  if (sTotal.indexOf("---") > -1)   objTotal.innerHTML = sTotal;
  else { if (nOrdTotal < 50) objTotal.innerHTML = "$10.00&nbsp";
    else if (nOrdTotal < 100) objTotal.innerHTML = "$15.00&nbsp";
    else if (nOrdTotal < 200) objTotal.innerHTML = "$20.00&nbsp";
    else objTotal.innerHTML = "$25.00&nbsp"; }

}


function writeCart(bMiniForm)
{
   document.write(GetCartString(bMiniForm));
}

function GetCartString(bMiniForm)
{
   var s = new Array();

//header

   s.push("<table cellpadding=5 cellspacing=1 border=0 width=");
   if (bMiniForm == "0")
      s.push("'100%'"); //      s.push("'100%'");
   else
      s.push("'480px'");

   s.push(" style='font-family: Arial, Helvetica, sans-serif; font-size: 13px; font-color:000000;'>");
   if (bMiniForm == "0")
      s.push("<tr bgcolor=CECFCE><td colspan=4><img border=0 align='absmiddle' src='./images/NEPcart1.GIF'>&nbsp&nbsp<b>Your Shopping Cart at Muskoka Pewter</b></td></tr>");
   s.push("<tr bgcolor=EEEEEE>");
   s.push("<td align=center nowrap width='51%'><b>Item</b></td>");
   s.push("<td align=center nowrap width='17%'><b>Unit Price</b></td>");
   s.push("<td align=center nowrap width='17%'><b>Qty.</b></td>");
   s.push("<td align=center nowrap width='15%'><b>Subtotal</b></td>");
   s.push("</tr>");

   var i = -1;
   var sItemNo = "";
   var nItemNo = 0;
   var sImgURL = '#';
   var sItemDetailURL = '#';
   var sItemDescription = '';
   var sItemUnitPrice = '0.00';
   var nItemQuantity = 1;
   var nOrdTotal = 0;

   while ( (sItemNo = getNextCartItem(sItemNo, 1)) != "")
   {
      if (isItemNull(sItemNo))
         continue;

      i++;

      nItemNo = parseInt(sItemNo);   //get from cookie
      nItemQuantity = getIQ(sItemNo);   //get from cookie

      sImgURL = oli[nItemNo*4];
      sItemDetailURL = oli[(nItemNo*4) + 1];
      sItemDescription = oli[(nItemNo*4) + 2];
      sItemUnitPrice = oli[(nItemNo*4) + 3];

      s.push("<tr bgcolor=FFFFFF>");
   //item
      s.push("<td align=left>");
      if (bMiniForm == "0")
      {
        s.push("<table border=0 cellpadding=0 style='font-family: Arial, Helvetica, sans-serif; font-size: 14px'><tr><td>");
        s.push(i+1);
        s.push(")&nbsp</td><td width=70>");
//        s.push("<a href='");
//        s.push(sItemDetailURL);
//        s.push("'>");
        s.push("<img border=1 width=66 height=70 src='");
        s.push(sImgURL);
        s.push("' align=center></a>");
        s.push("</td><td>");
//        s.push("<a href='");
//        s.push(sItemDetailURL);
//        s.push("'>");
        s.push("<font color=000000>");
        s.push(sItemDescription);
        s.push("</font></a><br><br><font size=1>[<a href='javascript:rFC(");
        s.push(nItemNo);
        s.push(");'><font color=0000c0>Remove Item</font></a>]</font></td></tr></table>");
      }
      else
      {
        s.push(i+1);
        s.push(")&nbsp");
        s.push(sItemDescription);
      }
      s.push("</td>");
    //unit price
      s.push("<td id=ip");
      s.push(i);
      s.push(" align=right>");
      s.push(sItemUnitPrice);
      s.push("&nbsp;</td>");
    //input quantity
      s.push("<td align=center>");
      if (bMiniForm == "0")
      {
       s.push("<input type=text size=4 value=");
       s.push(nItemQuantity);
       s.push(" id=iq");
       s.push(i);
       s.push(">");
      }
      else { s.push(nItemQuantity);}      
      s.push("</td>");
    //total

     var nlT = parseInt(nItemQuantity) * parseFloat(sItemUnitPrice);


     nOrdTotal += nlT;
     var sTotal = disMon(nlT);

//     var sTotal = new String();
//     sTotal += nlT;
//     if (sTotal.indexOf('.') == -1)
//        sTotal += ".00&nbsp";
//     else if (sTotal.indexOf('.') == sTotal.length - 2)
//        sTotal += "0&nbsp";
//     else
//        sTotal += "&nbsp";

      s.push("<td align=right id=it");
      s.push(i);
      s.push(">");
      s.push(sTotal);
      s.push("&nbsp;</td>");

      s.push("</tr>");
   }

   if (i == -1)
   {
      s.push("<tr bgcolor=FFFFFF>");
      s.push("<td colspan=4 align=center>Your Cart is currently empty. &nbspPlease continue shopping.");
      s.push("</td>");

      s.push("</tr>");
   }


//foot
   if (bMiniForm == "0")
   {
     s.push("<tr bgcolor=white>");
     s.push("<td colspan=3 align=center>&nbsp;</td>");
     s.push("<td bgcolor=FFFFA0>");
     if (i != -1)
        s.push("<div class=btn><input style='font-size:12px;' type=submit value='Update' onclick='updateTotals();'></div>");
     s.push("</td></tr>");
   }

   s.push("<tr bgcolor=EEEEEE>");
   s.push("<td colspan=3 align=right><font color=9C0000><b>Subtotal of your order:</b></font></td>");
   s.push("<td id=ordt align=right><font color=9C0000><b>$");
   s.push(disMon(nOrdTotal));
   s.push("&nbsp;</b></font></td>");
   s.push("</tr>");

   s.push("<tr bgcolor=EEEEEE>");
   s.push("<td colspan=3 align=right>+ Shipping and Handling:</td>");
   s.push("<td id=ship align=right>$");
   if (nOrdTotal<50)   {s.push("10.00");  nOrdTotal+= 10;}
   else if (nOrdTotal<100)   {s.push("15.00");  nOrdTotal+= 15;}
   else if (nOrdTotal<200)   {s.push("20.00");  nOrdTotal+= 20;}
   else {s.push("25.00");  nOrdTotal+= 25;}
   s.push("&nbsp&nbsp</td>");
   s.push("</tr>");

   if (bMiniForm != "0") //taxes
   {
     if (1==2) { //tax off
     var sCoun;
     var nCan=0;
     if ((sCoun = DTP_GetCookie("coun")) != "")
        nCan = parseInt(sCoun);

     var sTD = "";
     var nT = 0;
     if (nCan)
     {
       sTD = "GST and PST";
       nT = nOrdTotal*15/100;
     }
     else
     {
       sTD = "Canadian GST";
       nT = nOrdTotal*8/100;
     }
     s.push("<tr bgcolor=EEEEEE>");
     s.push("<td colspan=3 align=right>+ ");
     s.push(sTD);

     s.push(":</td>");
     s.push("<td id=ship align=right>$");
     s.push(disMon(nT));
     s.push("&nbsp;</td>");
     s.push("</tr>");
     nOrdTotal += nT;  }

     //grand
     s.push("<tr bgcolor=EEEEEE>");
     s.push("<td colspan=3 align=right><font color=9C0000><b>Your order total:</b></font></td>");
     s.push("<td id=ordt align=right><font color=9C0000><b>$");
     s.push(disMon(nOrdTotal));
     s.push("&nbsp;</b></font></td>");
     s.push("</tr>");
   }

   if (bMiniForm == "0")
   {
     s.push("<tr bgcolor=FFFFFF>");
     s.push("<td colspan=2><b><a href='javascript:updateTotals(); KeepShop();'><font color=0000c0>Continue Shopping</font></a></b></td>");
     s.push("<td colspan=2 bgcolor=A0FFA0>");
     if (i != -1)
        s.push("<div class=btn><input type=submit name=checkoutx value='Sorry - Closed' onclick='updateTotals();'></div>");
//        s.push("<div class=btn><input type=submit name=checkoutx value='Checkout =>' onclick='updateTotals(); gotocheckout();'></div>");
     s.push("</td></tr>");
   }
   s.push("</table>");

//   document.write(s.join(""));
   return (s.join(""));
}

function gotocheckout()
{
   document.location = "./nep_checkout1.html";
}


function DTP_SetCookie(name,value)
{
   var myCookie = document.cookie.substring(document.cookie.indexOf("=")+1,document.cookie.length);
   var search = name + "|" + value + "~";
   var offset = myCookie.indexOf(search);
   if (offset > -1)
      return; //
 
   search = name + "|";
   offset = myCookie.indexOf(search);
   if (offset == -1)
   { if (value!=null) {myCookie += name;  myCookie += "|"; myCookie += value;  myCookie += "~"; }}
   else
   {
     var tmp = myCookie.substring(0,offset);
     if (value!=null) 
     {  tmp += name;  tmp += "|"; tmp += value;  tmp += "~";  }

     myCookie = myCookie.substring(offset, myCookie.length);
     offset = myCookie.indexOf("~");
     tmp += myCookie.substring(offset+1, myCookie.length);

     myCookie = tmp;
   }
   document.cookie = "NEPord=" + myCookie + ";";
}

function DTP_GetCookie(name, defaultVal)
{
   var sRet = defaultVal;
   if (!defaultVal || defaultVal == null || defaultVal == "undefined")
      sRet = "";

   var myCookie = document.cookie.substring(document.cookie.indexOf("=")+1,document.cookie.length);
   var search = name + "|";
   if (myCookie.length > 0)
   {
      var offset = myCookie.indexOf(search);
      if (offset !=-1)
      {
         offset += search.length;
         var end = myCookie.indexOf("~",offset);
         if (end == -1)
            end = myCookie.length;
         
         sRet = myCookie.substring(offset,end);
         if (sRet == "null")
            sRet = "";
      }
    }
    return sRet;
}

function DTP_DeleteCookie(name)
{
    DTP_SetCookie(name, null);
}

function getNextCartItem(lastItem, bNumsOnly)
{
   var sItem = new String("");
   var myCookie = document.cookie.substring(document.cookie.indexOf("=")+1,document.cookie.length);
   if (myCookie.length > 0)
   {
      var localLI = lastItem;
      while (1)
      {
        sItem = "";
        if (localLI == "")
        {
           var end = myCookie.indexOf("|");
           if (end == -1)
              end = myCookie.length;
           sItem = myCookie.substring(0,end);
        }
        else
        {
           var search = localLI + "|";
           var offset = myCookie.indexOf(search);
           if (offset !=-1)
           {
              offset += search.length;
              var end = myCookie.indexOf("~",offset);
              if (end != -1)
              {
                 offset = end+1;
                 end = myCookie.indexOf("|",offset);
                 if (end == -1)
                 {
                    sItem = "";  //no more left
                    break;
                 }
                 else
                    sItem = myCookie.substring(offset,end);
              }
           }
        }
        localLI = sItem;
        if (!(bNumsOnly && isNaN(localLI)))
           break;
      }
   }

   return sItem;
}

function isItemNull(item)
{
   var bRet = 0;
   var sInCart = DTP_GetCookie(item);
   if (sInCart == "NaN" || sInCart == ""  || sInCart == null)
      bRet = 1;

   return bRet;
}

function getIQ(item)
{
   var nInCart = 0;
   var sInCart = DTP_GetCookie(item);

   if (sInCart != "NaN" && sInCart != ""  && sInCart != null)
     nInCart = parseInt(sInCart);

   return nInCart;
}

function setIQ(item, quantity)
{
   var nQ = parseInt(quantity);
   if (!isItemNull(item) || nQ > 0)
   {
      if (parseInt(getIQ(item)) != nQ)
      {
         DTP_SetCookie(item, quantity);
      }
   }
}

function aTC(item, quantity)
{
   if (!isItemNull(item) || (parseInt(quantity) > 0))
   {
      var nInCart = 0;
      nInCart = getIQ(item) + parseInt(quantity);
      if (nInCart < 0)
         nInCart = 0;

      DTP_SetCookie(item, nInCart);
   }
}

function rFC(item)
{
   DTP_DeleteCookie(item);
   document.location = document.location;
}

function hasItems()
{
  var bY = 0;
  var sItemNo = "";
  while ( (sItemNo = getNextCartItem(sItemNo, 1)) != "")
  {
     if (isItemNull(sItemNo))
        continue;

     bY = 1; break;
  }
  return bY;
}

function nullCart()
{
  var sItemNo = "";
  while ( (sItemNo = getNextCartItem(sItemNo, 1)) != "")
  {
     if (isItemNull(sItemNo))
        continue;

     DTP_DeleteCookie(sItemNo);

     sItemNo = "";
  }
}

