// AH Shopping cart functions
// Last time updated: 09 Nov 2007

// string format shcX:    "item_id|price|quant||item_id|price|quant|..."
// 'price' field is not really used, but it's kept to be compatible with
// previous version of the cart.

var COOKIE_ITEMS1 = 'ah_shc1';
var COOKIE_ITEMS2 = 'ah_shc2';

////////////////////////////

var isIE = document.all ? true:false;
var isNS = document.layers ? true:false;

function onlyDigits (e, decReq)
{
 var key = (isIE) ? window.event.keyCode : e.which;
 var obj = (isIE) ? event.srcElement : e.target;
 var isNum = (key > 47 && key < 58) ? true:false;
 var dotOK = (key==46 && decReq=='decOK' && (obj.value.indexOf(".")<0 || obj.value.length==0)) ? true:false;
 if(key < 32)  return true;
 return (isNum || dotOK);
}

///////////////////////////////////////////////
// new shopping cart functions 11 July 2007
///////////////////////////////////////////////


function  save_cart_cookie (kookie)
{
 var c1 = '';
 var c2 = '';
 var thenewdate = new Date ();

 // Split cookies
 if (kookie.length > 3600)
  {
   c1 = kookie.substring (0, 3600);
   c2 = kookie.substring (3600);
  }
 else
  {
   c1 = kookie;
   c2 = '';
  }

 thenewdate.setTime (thenewdate.getTime() + (365 * 24 * 3600 * 1000));

 SetCookie (COOKIE_ITEMS1, c1, thenewdate, "/");
 SetCookie (COOKIE_ITEMS2, c2, thenewdate, "/");
}

////////////////////////////////////////////////////
// cookies needed by final checkout function

function val_final()
{
 var coupon = finalcheckform.coupon.value;
 var thenewdate = new Date ();
 var country = finalcheckform.country.value;

 thenewdate.setTime (thenewdate.getTime() + (1800 * 1000));  // 30 minutes

 SetCookie ('ah_cart_country', country, thenewdate, "/");
 SetCookie ('ah_cart_coupon', coupon, thenewdate, "/");
 SetCookie ('ah_cart_action', 'finalcheckout', thenewdate, "/");

 event.returnValue = true;
 return true;
}


////////////////////////////////////////////

function reset_final_cookie (param)
{
 var thenewdate = new Date ();

 thenewdate.setTime (thenewdate.getTime() + (1800 * 1000));  // 30 minutes

 if (param > 0)
  {
   SetCookie ('ah_cart_coupon', '', thenewdate, "/");
  }
 SetCookie ('ah_cart_action', 'showcart', thenewdate, "/");
 return true;
}

/////////////////////////////////////////////

function new_update_item (fid)
{
 var cookie = '';

 // just re-create the cookie based on the shopping cart form

 for (var i = 0; i < 400; i++)  // up to 400 records in the cart
  {
   var field = "item_id" + i;
   var o;

   o = document.getElementById (field);
   if (o == null) { continue; }          // no field with this id   
   var v_itemid = o.value;
   if (v_itemid == null) { continue; }   // no field with this id
   if (v_itemid == '')   { continue; }   // no field with this id

   field = "quant" + i;
   o = document.getElementById (field);
   if (o == null) { continue; }          // no field with this id      
   var v_quant = o.value;
   if (v_quant == null)  { continue; }   // no field with this id
   if (v_quant == '')    { continue; }   // no field with this id

   var item_string = '';

   if (v_quant == 0)  { continue; }  // item to be removed, skip it

   item_string = v_itemid + "|55|" + v_quant;

   if (cookie != '')
    {
     cookie = cookie + "||" + item_string;
    }
   else
    {
     cookie = item_string;
    }
  }

 save_cart_cookie (cookie);
 reset_final_cookie (0);

 window.location="/shopping_cart.html";
 return false;
}

/////////////////////////////////////////////

function new_remove_item (fid)
{
 var cookie = '';
 var  field = "quant" + fid;
 var o = document.getElementById (field);

 if (o == null) { return false; }
 o.value = '0';                   // set quantity to zero so it will be removed
 return new_update_item (fid);
}


/////////////////////////////////////////////

function add_item_ch (itemid)
{
 var cookie1 = GetCookie (COOKIE_ITEMS1);
 var cookie2 = GetCookie (COOKIE_ITEMS2);
 var cookie  = '';
 var quant   = 1;

 if (cookie1 != null)
   {
    cookie = cookie1;
    if (cookie2 != null) { cookie = cookie1 + cookie2; }
   }

 if (itemid == null)  { return false; }
 if (itemid == '')    { return false; }

 var item_string = itemid + "|55|" + quant;

 if (cookie != '')
  {
   cookie = cookie + "||" + item_string;
  }
 else
  {
   cookie = item_string;
  }

 save_cart_cookie (cookie);
 reset_final_cookie (0);

 window.location="/shopping_cart.html";
 return false;
}

///////////////////////////////////////////////

function  add2cart (f1)   // used by our own  script _add2cart.html
{
 var list = add2cart_form.toadd.value;
 var pass = add2cart_form.password.value;

 if (pass != "blackgate") { return false; }

 if (list == null)  { return false; }
 if (list == '')    { return false; }

 var lines = list.split ("\n");
 var i = 0;

 var cookie1 = GetCookie (COOKIE_ITEMS1);
 var cookie2 = GetCookie (COOKIE_ITEMS2);
 var cookie = '';

 if (cookie1 != null)
   {
    cookie = cookie1;
    if (cookie2 != null) { cookie = cookie1 + cookie2; }
   }

 for (i = 0; i < lines.length; i++)
  {
   var itemid = lines[i];
   itemid = itemid.replace (/\r/, "");   

   if ((itemid != null) && (itemid != ''))
    {
     var item_string = itemid + "|55|1";

     if (cookie != '')
      {
       cookie = cookie + "||" + item_string;
      }
     else
      {
       cookie = item_string;
      }
    }
  }

 save_cart_cookie (cookie);
 reset_final_cookie (0);

 return false;
}

/////////////////////////////////////

function clear_shopping_cart()
{
 save_cart_cookie ("");
 window.location="/shopping_cart.html";
}

/////////////////////////////////////////////

function add_item_id_quant(fid)
{
 ///var cookie = '';
 var cookie1 = GetCookie (COOKIE_ITEMS1);
 var cookie2 = GetCookie (COOKIE_ITEMS2);
 var cookie  = '';
 ///var quant   = 1;

 if (cookie1 != null)
   {
    cookie = cookie1;
    if (cookie2 != null) { cookie = cookie1 + cookie2; }
   }

 // just re-create the cookie based on the shopping cart form

 for (var i = 0; i < 400; i++)  // up to 400 records in the cart
  {
   var field = "item_id" + i;
   var o;

   o = document.getElementById (field);
   if (o == null) { continue; }          // no field with this id   
   var v_itemid = o.value;
   if (v_itemid == null) { continue; }   // no field with this id
   if (v_itemid == '')   { continue; }   // no field with this id

   field = "quant" + i;
   o = document.getElementById (field);
   if (o == null) { continue; }          // no field with this id      
   var v_quant = o.value;
   if (v_quant == null)  { continue; }   // no field with this id
   if (v_quant == '')    { continue; }   // no field with this id

   var item_string = '';

   if (v_quant == 0)  { continue; }  // item to be removed, skip it

   item_string = v_itemid + "|55|" + v_quant;

   if (cookie != '')
    {
     cookie = cookie + "||" + item_string;
    }
   else
    {
     cookie = item_string;
    }
  }

 save_cart_cookie (cookie);
 reset_final_cookie (0);

 window.location="/shopping_cart.html";
 return false;
}

/////////////////////////////////////////////

