var orderCookie = 'order';

// duration of cookie (1 day)
var orderCookieDuration = 1000*60*60*24;

// 'https://secure.qwk.net/~ceedge/cgi-bin/checkout.cgi?cart_list=' + cvalue ;

// returns the specified cookie
function getCookie(cookie_name) {
  var prefix = cookie_name + "=";
  var cookieStartIndex = document.cookie.indexOf(prefix);
  if (cookieStartIndex == -1) return null;

  var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
  if (cookieEndIndex == -1) cookieEndIndex = document.cookie.length;
  return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

// returns the serialized cookie
function getSerializedCookie(cookieName) {
 var cookStr = getCookie(cookieName);
 // init if needed to PHP compatible serialization
 if (cookStr == null || cookStr.indexOf('a:') !=0)
     cookStr = 'a:0:{}';
   
 return cookStr;
 
}

// returns the number of items in the serialized str
function getNumItems(theStr) {
  numItems = theStr.substr(2, theStr.indexOf(':', 2) - 2);
  return numItems ;
}

// returns the serialized item
// ex: s:7:"Skinnys";s:1:"1";
function serializedItem(itemCode, itemQty) {
 return 's:' + itemCode.length + ':%22' + itemCode + '%22;s:' + itemQty.length + ':%22' + itemQty + '%22;';
}

// returns the fully serialized array
// ex:  a:3:{s:7:"Skinnys";s:1:"1";s:6:"Fatsos";s:1:"1";s:8:"HT-09123";s:1:"1";}
function insertItem(sStr, itemCode, qty) {
  var sItem = serializedItem(itemCode, '1');
  numItems = getNumItems(sStr);
  numItems ++;
  return 'a:' + numItems + ':' +
         sStr.substr(sStr.indexOf('{'), sStr.lastIndexOf('}')-sStr.indexOf('{')) +
         sItem + '}';
  
}

function addCart(itemCode, itemText, itemPrice) {

 // get current cookie
 var currCookie = getSerializedCookie(orderCookie);
     
 var itemAlreadyInCart = (currCookie.indexOf(itemCode) != -1) ;
 
 if (!itemAlreadyInCart) {
   currCookie= insertItem(currCookie, itemCode, '1');
   
   numItems = getNumItems(currCookie) ;

   // establish expiration and write cookie
   var exp = new Date();
   exp.setTime(exp.getTime() + orderCookieDuration );
   document.cookie = orderCookie + '=' + escape(currCookie);
   // +';expires=' + exp.toGMTString();
 }
 
 openCart(itemCode, itemText, itemPrice, itemAlreadyInCart);
 updateSC();
 
}

function openCart(ItemCode,ItemText,ItemPrice, itemAlreadyInCart) {
  var displayHeader = "<html><head><title>Order Pad</title> <style TYPE=\"text/css\"><!-- A:hover\{text-decoration:underline\;color:#cc0000\;\}A \{ text-decoration:underline \}  --></style>";
  cart = window.open('', 'displayOrderPad','width=500,height=280, top=175, left=125, status=yes, resizable=no');
  cart.opener = self;
  cart.window.moveTo(125, ( (isOpera) ? 25 : 160 ));
  var d= cart.document;
  d.open();
  d.write(displayHeader);

  d.write('</head><body bgcolor=#ffffee leftmargin=10 link=#3636aa alink=#3636aa vlink=#3636aa style="font-family:Arial" onblur="self.close();">');
  d.write('<table width=480 align=center><tr><td>');

  d.write('<div style="font-size:14pt; color:#696969; font-family:Arial">' +
          'This item has ' + ((itemAlreadyInCart) ? '<b>ALREADY</b> ' : '') +
          'been added to your shopping cart:</b></div><br><br>');
  
  d.write('<div align=center style="font-size:16pt;color:black"><b>' +
          ItemText + ' $' + ItemPrice + ' each</b><br><br></div>');
  
  d.write('<div style="font-size=10pt">When you checkout, you will be able to review and delete any or all items while selecting quantities and shipping method.<br><br></div>');
 
  d.write('<a href="javascript:self.close()">CONTINUE SHOPPING</a><br>');
  
  d.write("<font size=0 color=#696996 face=Times Roman><p>website ORDER PAD</font>");
  d.write("</td></tr></table></body></html>");
  d.close();
 
  cart.focus();  // needed for Opera
  
}


function updateSC() {

  var scf = document.forms['frmSC'] ;
  if ( typeof(scf) != 'undefined') {
   var i = scf.elements['scNumI'];
   var numItems = getNumItems(getSerializedCookie(orderCookie)) ;
   i.value = numItems;
  }  
}

function xxupdateSC() { }  
