var blk2q = 1;
var blk3n = "";
var bcode = "";
var blkcc = "";
var blk4a = "0.00";
var winpar = "width=550,height=500,scrollbars," +
             "location=0,resizable,status";

var cmax  = 12;            
var cats  = 1;             
var pcat  = 0;             
var ctot  = new Object (); 

var stax  = 8.75;      
var ttxt  = "";     
var ttax  = 0;
var hand; 
var ship;
var qtyd;
var tqty = 0;
var cook;    
var tprc = 0; 
var tozs = 0; 
var bozs = 0; 
var gtot;    
var insx = 0;

var dqty = new Array (); 
var damt = new Array (); 
var dn   = 0;  

var aqty = new Array ();  
var aamt = new Array ();  
var an   = 0;            

var zn;        
var zzip = new Array ();  
var zone = new Array ();

var zip;        
var wte;     
var zne = 0;  
var amt;   
var usps = -1; 
var ups  = -1; 
var insu = -1; 
var itmw = 0;
var SelectedShipping;
var shipOK = 0;


var on  = 1;  
var off = 0;
 

function AddDesc (strn) {  
var c = "";
  if (blk3n.length > 0) c = ", ";
  blk3n = blk3n + escape (c + strn);
}

function AddPrcnt (strn) { 
  SetPrice (blk4a * (1.0 + strn/100.0));  
}

function AddPrice (strn) { 
  blk4a = Dollar (blk4a*1.0 + strn*1.0);
}

function CalcInsu (amt) {  
if (amt == 0) return 0;
if (amt > 0 && amt <= 50) return 1.30;
return (2.20 + Math.floor (amt / 100.0));
}

function CallView () { 
  window.open ("nlscart.html", "local39", winpar);
}

function ClearAll () {  
  blk2q = 1;  
  blk3n = "";  
  blk4a = "0"; 
  bcode = "";  
  bozs  = 0;
  dn    = 0;
  an    = 0;
  pcat  = 0;
}

function ClearCart () { 
var i;
  for (i=1; i<cmax; i++) { 
    ClearEntry (i);
  }
}

function ClearEntry (i) { 
  cook = new Cookie (document, "paycart" + i);
  InitData ();
}

function Cookie(document, name, hours, path, domain, secure) {
  this.$document = document;  
  this.$name = name;   
  if (hours) this.$expiration = 
      new Date((new Date()).getTime() + hours * 3600000);
  else this.$expiration = null;
	this.$path = "/";  
  if (domain) this.$domain = domain; else this.$domain = null;
  if (secure) this.$secure = true;   else this.$secure = false;
}

function _Cookie_store() {  
var cookieval = "";  
  for(var prop in this) { 
    if ((prop.charAt(0) == '$') || 
        ((typeof this[prop]) == 'function')) 
        continue;
    if (cookieval != "") cookieval += '&';
    cookieval += prop + ':' + escape(this[prop]);
  }

  var cookie = this.$name + '=' + cookieval;
  if (this.$expiration)
      cookie += '; expires=' + this.$expiration.toGMTString();
  if (this.$path) cookie += '; path=' + this.$path;
  if (this.$domain) cookie += '; domain=' + this.$domain;
  if (this.$secure) cookie += '; secure';

  this.$document.cookie = cookie;  
}

function _Cookie_load() { 
var i;
var allcookies = this.$document.cookie;
  if (allcookies == "") return false;
 
  var start = allcookies.indexOf(this.$name + '=');
  if (start == -1) return false;   
  start += this.$name.length + 1;  
  var end = allcookies.indexOf(';', start);
  if (end == -1) end = allcookies.length;
  var cookieval = allcookies.substring(start, end);

  var a = cookieval.split('&');  
  for(i=0; i < a.length; i++) 
    a[i] = a[i].split(':');

  for(i=0; i<a.length; i++)
    this[a[i][0]] = unescape(a[i][1]);

  return true;  
}


function _Cookie_remove() {  
var cookie;
  cookie = this.$name + '=';
  if (this.$path) cookie += '; path=' + this.$path;
  if (this.$domain) cookie += '; domain=' + this.$domain;
  cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';

  this.$document.cookie = cookie;  
}

function Dollar (val) {  
var str,pos,rnd=0;
  if (val < .995) rnd = 1;  
  val=Math.round(val*100)/100;
  str = escape (val*1.0 + 0.000501);  
  pos = str.indexOf (".");
  if (pos > 0) str = str.substring (rnd, pos + 3);
  return str;
}


function InitData () { 
  cook.pcat = 0;
  cook.qty  = 0;
  cook.cod  = "";
  cook.des  = "";
  cook.prc  = 0;
  cook.ozs  = 0;
  cook.store();   
}


function InitRoot () {  
var i;
  root.stax = stax;     
  root.usps = usps;    
  root.ups  = ups;  
  root.insu = insu;   
  root.zne  = zne;    
  root.xx_cper = 0;
  root.xx_cur  = "";
  root.xx_dadd = on;
  root.xx_pc   = on;
  root.xx_ship = on;
  root.xx_sum  = on;
  root.xx_tax  = off;
  root.xx_xx   = on;
  root.SelectedShipping = 0;
  root.zip = "Enter ZIP";
  root.shipOK = 0;

    root["hn0"]   = 0;            
    root["hqty0"] = new Array ();  
    root["hamt0"] = new Array (); 

    root["qd0"]   = 0;             
    root["qqty0"] = new Array (); 
    root["qamt0"] = new Array (); 

    root["sn0"]   = 0;           
    root["sqty0"] = new Array (); 
    root["samt0"] = new Array (); 
    root["stxt0"] = "";      

    root["sv0"]   = 0;         
    root["sval0"] = new Array ();  
    root["schg0"] = new Array ();  
  root.store();
}

function PrintCart () {  
var i,wt,p,tmp1,tmp2,s;
  tprc = 0;               
  tozs = 0;          
  gtot = 0;        
  ttax = 0;    
  tqty = 0;    
  insx = 0;   
  ctot["a0"] = 0;      
  ctot["q0"] = 0;    
  ctot["h0"] = 0;     
  ctot["s0"] = 0;  
  ctot["d0"] = 0;   

  var EmptyCart=true;
  for (i=1; i<cmax; i++) {  
    cook = new Cookie (document, "paycart" + i);
    cook.load();
    if (cook.qty > 0) {
      PrintItem (i);
	  EmptyCart=false;
    }
  }
  if (EmptyCart==true) {
  document.write("<tr bgcolor=#ffffff class='CartText'><td align=center width='8%'> - </td><td align=center width='8%'></td><td align=left width='70%'> The cart is empty</td><td width='14%' class='CartPrice'> $ ---.--</td></tr>");
}

  tprc = Dollar (tprc)*1.0;            
//  if (root.zip>90000 && root.zip<=96162) {
  ttax = Dollar (tprc* stax/100.0);
//  }
//  else {ttax=0;}
//  gtot = Dollar (tprc + ttax*1.0)*1.0;  
  gtot = Dollar (tprc)*1.0;  

  hand = 0; 
  ship = 0;
  qtyd = 0;   

    sh  = 0;               
    ha  = 0;
    qt  = 0;

    var q = ctot["q0"]*1.0;    
    var a = ctot["a0"]*1.0;     
  
    tmp1 = root["hqty0"].split(",");
    tmp2 = root["hamt0"].split(",");
    for (i=root["hn0"]-1; i>=0; i--) { 
      if (q >= tmp1[i]*1.0) {    
        ha = tmp2[i]*1.0;       
        break;               
      }
    }
    ctot["h0"] = ha;
    gtot = gtot + ha;
    hand = hand + ha;

    tmp1 = root["sqty0"].split(",");
    tmp2 = root["samt0"].split(",");
    for (i=root["sn0"]-1; i>=0; i--) {  
      if (q >= tmp1[i]*1.0) {              
        sh = tmp2[i]*1.0;                  
        break;
      }
    }
    if (sh == 0) {                         
     tmp1 = root["sval0"].split(",");
     tmp2 = root["schg0"].split(",");
     for (i=root["sv0"]-1; i>=0; i--) { 
       if (a >= tmp1[i]*1.0) {  
         sh = tmp2[i]*1.0;     
         break;
       }
     }
    }
    ctot["s0"] = sh;
    gtot = gtot + sh;
    ship = ship + sh;

    tmp1 = root["qqty0"].split(",");
    tmp2 = root["qamt0"].split(",");
    for (i=root["qd0"]-1; i>=0; i--) {
      if (q >= tmp1[i]*1.0) {
        qt = tmp2[i]*1.0;
        break;
      }
    }
    ctot["d0"] = qt;
    gtot = gtot - qt;
    qtyd = qtyd + qt;
  
  s = 0;  
  wte = Math.floor ((tozs + 15.999) / 16.0);
  if (usps > 0) {
    if (wte > pz2.length) {
      alert (wte + " is greater than length of USPS charge array!");
      s = 99999;
    } else
    s = pzx[zne][wte]; 
  } else
  if (ups  > 0) {
    if (wte > uzx[2].length) {
      alert (wte + " is greater than length of UPS charge array!");
      s = 99999;
    } else
    s = uzx[zne][wte]; 
  }
  ship = ship + s;  
  if (insu > 0) insx = CalcInsu (tprc);

  gtot = Dollar (gtot + s + insx);  
}

function PrintItem (i) {  
var tm,t1,t2,wt;

					document.write("<tr bgcolor=#ffffff class='CartText'><td align=center width='8%'>"+cook.qty+"</td><td align=center width='8%'><input type='checkbox' name='d0' value='1' onclick = 'ClearEntry (",
    i, "); location.reload ();'></td><td align=left width='70%'><b><a href='main.php?Menu=Detail&Product="+cook.cod+"' target='main' onmouseover=\"window.status='"+(cook.des)+"'; return true;\" onmouseout=\"window.status=''; return true;\">"+unescape(cook.des)+"</a></b><br> Reference# "+cook.cod+"</td><td width='14%' class='CartPrice'> $	");
  tm = Dollar (cook.prc);
  t1 = cook.qty*1.0;
  t2 = cook.pcat;
  document.write(tm+"</td></tr>");
  tprc = tprc*1.0 + tm*1.0;
  tqty = tqty + 1;
  ctot["a0"] = ctot["a0"] + tm*1.0;
  ctot["q0"] = ctot["q0"] + 1;
  wt = cook.ozs*1.0 * cook.qty;
  tozs = tozs + wt;
}


function SetAmtSH (pcat, a1, c1) {  
var i,sv;
var sval = new Array ();
var schg = new Array ();
  if (pcat >= cats) {     
    alert ("SetAmtSH cat arg too big!");
    return;
  }
  sv = 0;                    
  for (i=1; i<arguments.length; i=i+2) {
    sval[sv] = arguments[i];  
    schg[sv] = arguments[i+1]; 
    sv = sv + 1;           
  }
  root["sv" + pcat]   = sv;  
  root["sval" + pcat] = sval;
  root["schg" + pcat] = schg;
  root.store();           
}


function SetCartHN (pcat, q1, c1) { 
var i,hn;
var hamt = new Array ();
var hqty = new Array ();
  if (pcat >= cats) {    
    alert ("SetCartHN cat arg too big!");
    return;
  }
  hn = 0;                    
  for (i=1; i<arguments.length; i=i+2) {
    hqty[hn] = arguments[i];  
    hamt[hn] = arguments[i+1]; 
    hn = hn + 1;        
  }
  root["hn" + pcat] = hn;    
  root["hqty" + pcat] = hqty;
  root["hamt" + pcat] = hamt;
  root.store();        
}

function SetCartQD (pcat, q1, c1) {  
var i,qd;
var qqty = new Array ();
var qamt = new Array ();
  qd = 0;          
  for (i=1; i<arguments.length; i=i+2) {
    qqty[qd] = arguments[i]; 
    qamt[qd] = arguments[i+1]; 
    qd = qd + 1;      
  }
  root["qd0"] = qd;    
  root["qqty0"] = qqty;
  root["qamt0"] = qamt;
  root.store();        
}

function SetCartSH (pcat, q1, c1) {  
var i,sn;
var sqty = new Array ();
var samt = new Array ();
  if (pcat >= cats) {     
    alert ("SetCartSH cat arg too big!");
    return;
  }
  sn = 0;                
  for (i=1; i<arguments.length; i=i+2) {
    sqty[sn] = arguments[i];   
    samt[sn] = arguments[i+1]; 
    sn = sn + 1;     
  }
  root["sn" + pcat] = sn;    
  root["sqty" + pcat] = sqty;
  root["samt" + pcat] = samt;
  root.store();     
}

function SetCode (cd) { 
  bcode = escape (cd);
}

function SetDesc (strn) { 
  blk3n = escape(strn);
}

function SetID (strn) { 
  blk2b = escape (strn);
}

function SetPrice (strn) { 
  blk4a = 0;
  AddPrice (strn);
}

function SetQA (q1, a1) { 
var i;
  an = 0;                   
  for (i=0; i<arguments.length; i=i+2) {
    aqty[an] = arguments[i];  
    aamt[an] = arguments[i+1]; 
    an = an + 1;       
  }
}

function SetQtyD (q1, d1) {  
var i;
  dn = 0;    
  for (i=0; i<arguments.length; i=i+2) {
    dqty[dn] = arguments[i];   
    damt[dn] = arguments[i+1]; 
    dn = dn + 1;   
  }
}

function SetWt (ozs) {  
  bozs = ozs;
}

function SetZone (nbz, zi1, zn1) {  
var i,j;
  zn = 0;                 
  for (var i=1; i<arguments.length; i=i+1+nbz) {
    zzip[zn] = arguments[i];   
	zone[zn] = new Array();
	for (var j=0; j<nbz; j++) { 
	    zone[zn][j] = arguments[i+1+j]; 
	}
    zn = zn + 1;    
  }
}

function StoreCart () {  
var i;
  for (i=1; i<cmax; i++) {  
    cook = new Cookie (document, "paycart" + i);  
    cook.load();            
    if (cook.qty*1.0 > 0) {    
      if (cook.cod == bcode &&  
          cook.des == blk3n &&
          cook.prc*1.0 == blk4a*1.0) {
		  if (cook.qty*1.0 >=1) {
			alert("This Item is already in your cart! \n All jewelry articles sold here are one-of-a-kind pieces \n Only one of each piece can be ordered. \n If you wish to get multiple same pieces, please contact me."); }
        cook.qty = 1.0;
        cook.store();
        ClearAll ();
        return;     
      }
    }
  }
  for (i=1; i<cmax; i++) {  
    cook = new Cookie (document, "paycart" + i);
    cook.load();
    if (cook.qty*1.0 == 0 ||
        isNaN (cook.qty)) {
      cook.pcat = pcat
      cook.qty  = blk2q;
      cook.cod  = bcode;
      cook.des  = blk3n;
      cook.prc  = blk4a;
      cook.ozs  = bozs;
      cook.store();
      ClearAll ();
      if (i == cmax - 1)
        alert ("Warning - The cart is limited to 11 items! \n You will not be able to add anymore item after this one.");
      return;      
    }
  }
  alert ("Error 1 - out of cookies!");
}

function Xbild2 () {  
var i,j;
var tot  = 0;        
var frst = 1;      
var str  = "";     
var disc = new Array ();  
var tmps = root.stxt0+" zone "+root.zne+" ZIP "+root.zip+" "+ttxt;  

  totaldiscount = ctot["d0"];
  j = 0;       
  for (i=1; i<cmax; i++) {  
    cook = new Cookie (document, "paycart" + i);
    cook.load();        
    if (cook.qty > 0) {  
      j = j + 1;    
      str = str + "<input type='hidden' name='quantity_"+j+"' value='"+cook.qty+"'>";
      str = str + "<input type='hidden' name='item_name_"+j+"' value='"+unescape(cook.des)+"'>";
      str = str + "<input type='hidden' name='amount_"+j+"' value='";
      if (j==1 && cook.prc>(totaldiscount+1)) {
	  str = str + Dollar (cook.prc*1.0 - totaldiscount) +"'>";}
	  else {
	  str = str + Dollar (cook.prc*1.0) +"'>";
	  }
      if (cook.cod != "")
        str = str + "<input type='hidden' name='item_number_"+j+"' value='"+cook.cod+"'>";
      if (frst > 0 &&
          (hand*1.0 > 0 ||
          ttax*1.0 > 0 ||
          insx > 0)) {
        str = str + "<input type='hidden' name='handling_"+j+"' value='"+Dollar(Dollar(hand)*1.0 + Dollar(insx)*1.0)+"'>";
      }
      if (frst > 0) {
        str = str + "<input type='hidden' name='shipping_"+j+"' value='"+Dollar(ship)+"'>";
        str = str + "<input type='hidden' name='on0_"+j+"' value='Shipping'>";
        str = str + "<input type='hidden' name='os0_"+j+"' value='"+tmps+"'>";
      } else {
        str = str + "<input type='hidden' name='shipping_"+j+"' value='0.00'>";
      }
      frst = 0;
    }
	} 
  return str;
}

// USPS priority zone charges
var pz1  = new Array (0, 3.85,  3.95,  4.75);
var pz2  = new Array (0, 3.85,  3.95,  4.75);
var pz3  = new Array (0, 3.85,  3.95,  4.75);
var pz4  = new Array (0, 3.85,  4.55,  6.05);
var pz5  = new Array (0, 3.85,  4.90,  6.85);
var pz6  = new Array (0, 3.85,  5.05,  7.15);
var pz7  = new Array (0, 3.85,  5.40,  7.85);
var pz8  = new Array (0, 3.85,  5.75,  8.55);
var pzx  = new Array (0,pz1,pz2,pz3,pz4,pz5,pz6,pz7,pz8);

// USPS Express mail charges per weight

function CalcCost () {  // given weight and ZIP, calc cost
  wte = Math.floor ((tozs + 15.999) / 16.0);
  if (root.usps > 0) {   // user selected USPS Priority shipping
    SetUspsZ ();    // set up USPS Priority mail zones
  } else
  if (root.ups > 0) {    // user has selected UPS ground shipping
    SetUpsZ ();     // set up UPS ground shipping zones
  }
  for (var i=zn-1; i>=0; i--) { // run table backwards
    if (root.zip.substring (0, 3) >= zzip[i]) {       // zip value
	  if (root.usps >0) zne = zone[i][root.usps-1];            // corresponding zone
	  if (root.ups >0) zne = zone[i][root.ups-1]; 
      break;                    // get out, now
    }
  }
  if (root.usps > 0) {
    if (wte > pz2.length) {
      alert (wte + " is greater than length of USPS charge array!");
      amt = 99999;
    } else
    amt = pzx[zne][wte]; // USPS charges to that zone
  } else
  if (root.ups  > 0) {
    if (wte > uzx[2].length) {
      alert (wte + " is greater than length of UPS charge array!");
      amt = 99999;
    } else
    amt = uzx[zne][wte]; // UPS charges
  }
}

function SetUspsZ () {  // set USPS zone chart in memory
  // built from USPS 95060 zone chart
  SetZone (1, 000, 8,
    375, 7, 376, 8, 380, 7, 382, 8, 386, 7,
	388, 8, 389, 7, 393, 8, 420, 7, 421, 8,
	498, 7, 510, 6, 514, 7, 515, 6, 520, 7,
	530, 8, 535, 7, 570, 6, 580, 7, 583, 6,
	590, 5, 592, 6, 594, 5, 600, 8, 610, 7,
	664, 6, 667, 7, 668, 6, 673, 7, 674, 6,
	700, 8, 705, 7, 730, 6, 733, 7, 734, 6,
	740, 7, 746, 6, 747, 7, 748, 6, 749, 7,
	760, 6, 765, 7, 768, 6, 770, 7, 790, 6,
	798, 5, 833, 4, 834, 5, 836, 4, 838, 5,
	840, 4, 845, 5, 860, 4, 865, 5, 881, 6,
	882, 5, 889, 4, 894, 3, 898, 4, 900, 3,
	919, 4, 930, 3, 936, 2, 939, 1, 942, 2,
	943, 1, 955, 3, 956, 2, 960, 3, 962, 1,
	967, 8, 970, 4, 980, 5, 986, 4, 988, 5,
	995, 8, 998, 7, 999, 6);
}


function zipcode(obj1) {
	root.load();
	zip = obj1.value;
	if (zip.toString().length!=5 || isNaN(zip)) {alert("Invalid ZIP code"); obj1.value=""; return;}
	root.zip = zip;
	root.store();
	}

function Shipper (obj1) {  // use either amt or qty based shipping
//var pos;
  SetAmtSH (0);  // assume the worst
  SetCartSH (0);
  if (obj1.selectedIndex==0) {  document.location.reload ();
	return;}
  SelectedShipping = obj1.selectedIndex;
  root.stxt0 = obj1.options[SelectedShipping].text;
  SetAmtSH (0,0,0);  // LEAVE ALONE!!
  root.SelectedShipping = SelectedShipping;
  root.store;
  CalcShipping();
  }

function CalcShipping () {
  root.load;
  SelectedShipping = root.SelectedShipping;
  if (SelectedShipping == 1) {    //1st option USPS Priority Mail w/ insurance
    root.usps = 1;
    root.ups  = -1;
    root.insu = 1;
    zip = root.zip;
	SetCartHN (0, 1, 1, 3, 1.5, 5, 2, 10, 3);  // $1 handling for entire cart
    if (isNaN (zip)) {  // keep them honest
      alert ("Please enter a valid ZIP code!");
	  root.shipOK = 0;
	  root.store ();
      return;
    }
    CalcCost ();
    root.zne = zne;
    root.shipOK = 1;
	root.store ();
  } else
  if (SelectedShipping == 2) {    // 2nd option USPS Priority Mail no insurance
    root.usps = 1;
    root.ups  = -1;
    root.insu = -1;
    zip = root.zip;
	SetCartHN (0, 1, 1, 3, 1.5, 5, 2, 10, 3);  // $1 handling for entire cart
    if (isNaN (zip)) {  // keep them honest
      alert ("Please enter a valid ZIP code!");
	  root.shipOK=0;
	  root.store ();
      return;
    }
    CalcCost ();
    root.zne = zne;
	root.shipOK=1;
    root.store ();
  } else
  if (SelectedShipping == 3) {    // 3rd option USPS Express Mail
    root.usps = -1;
    root.ups  = -1;
    root.insu = -1;
	zip = root.zip;
	SetCartHN (0, 1, 1, 3, 1.5, 5, 2, 10, 3);  // $1 handling for entire cart
    if (isNaN (zip)) {  // keep them honest
      alert ("Please enter a valid ZIP code!");
	  root.shipOK=0;
	  root.store ();
      return;
    }
    wte = 0;
    SetAmtSH (0, 0, 0);
    SetCartSH (0, 1, 13.65, 2, 17.85);
	root.shipOK=1;
    root.store ();
  } else
  if (SelectedShipping == 4) {    // 4th option
    root.usps = -1;
    root.ups  = -1;
    root.insu = -1;
    wte = 0;
	SetCartHN (0, 1, 1, 3, 1.5, 5, 2, 10, 3);  // $1 handling for entire cart
    SetAmtSH (0, 0, 0);
    SetCartSH (0, 1, 21);
	root.shipOK=1;
	root.store();
  } else
  if (SelectedShipping == 5) {   // 5th option
    root.usps = -1;
    root.ups  = -1;
    root.insu = -1;
	root.zip = 95060;
	SetCartHN (0, 1, 0);  
    SetAmtSH (0, 0, 0);
    SetCartSH (0, 0, 0);
	root.shipOK=1;
	root.store();
  }
  document.location.reload ();  // show the latest info...
}

// executed at load time
new Cookie (document, "xcxc");  // (backward compatibility)
Cookie.prototype.store  = _Cookie_store;   // load proto methods
Cookie.prototype.load   = _Cookie_load;
Cookie.prototype.remove = _Cookie_remove;

var root = new Cookie (document, "paycart0");  // special root cookie
  if (!root.load () ||
      !root.stax) {
    InitRoot ();                  // gotta create it
  } else {                        // load up globals
    stax  = root.stax;
    usps  = root.usps;
    ups   = root.ups;
    insu  = root.insu;
    zne   = root.zne;
	SelectedShipping = root.SelectedShipping;
	zip = root.zip;
	shipOK = root.shipOK;
  }

for (var i=1; i<cmax; i++) {  // load or init data cookies
  cook = new Cookie (document, "paycart" + i);
  if (!cook.load () ||
      !cook.des) {
    InitData ();              // create it
  }
}