//
// Shopping Cart Tools
//
// init global variables
//
//  This code is copyright material written by Rich Kroll.
//  All rights reserved.
//
var maxorder = 80;                      // max number of order items
//
var txt = document.location.href;
var nd = txt.lastIndexOf("/");
if (nd == 0)
    {nd = txt.lastIndexOf("\\");};
var gbase = txt.substring(0,nd+1);
//
//  description translation
//
var descval = new Array(10);
descval[1] = "Ceraurus Pleurexanthemus";
descval[2] = "Reineckia and Kosmoceras";
descval[3] = "Arctinurus Boltoni";
descval[4] = "Carcharocles Megalodon";
descval[5] = "Mammilaria Blossfeldiana";
descval[6] = "Echinofossulocactus Arrigens";
descval[7] = "Leaf Mantis";
descval[8] = "Papilio Ulysses";
var desccode = new Array(10);
desccode[1] = "T302";
desccode[2] = "Tc14";
desccode[3] = "T327";
desccode[4] = "Tc63";
desccode[5] = "T169";
desccode[6] = "T1a6";
desccode[7] = "Th24";
desccode[8] = "T343";
//
// size array
//
var sizeval = new Array(5);
sizeval[0] = "small";
sizeval[1] = "medium";
sizeval[2] = "large";
sizeval[3] = "XL";
sizeval[4] = "XXL";
//
// general variables
//
var debug = true;
var gcode = " ";
var gquan = 0;
var gsize = " ";
var gprice = " ";
var gdesc = " ";
var gnum = 0;
var gdiscount = " ";
var gtotal = " ";
var gname = "";
var gaddr1 = "";
var gaddr2 = "";
var gtown = "";
var gcountry = "";
var gcountryc = "";
var gstate = "";
var gstatec = "";
var gzip = "";
var gemail = "";
var gtele = "";
var acode = new Array();
var aquan = new Array();
var asize = new Array();
var aprice = new Array();
var adesc = new Array();
//
//  load order from cookies
//
var aorder = GetCookie("torder");
var aorder2 = GetCookie("torder2");
var aold = GetCookie("oorder");
var aold2 = GetCookie("oorder2");
//
//  check revision level.  If it is new, reset cookies.
//
var rev = "010520";
if (rev != GetCookie("rev"))
   {
    aold = "";
    SetCookie("oorder",aold);
    aorder = "0~~~~~~~";
    SetCookie("torder",aorder);
    aold2 = "";
    SetCookie("oorder2",aold2);
    aorder2 = "0~~~~~~~";
    SetCookie("torder2",aorder2);
    SetCookie("info","");
   };
SetCookie("rev",rev);
//
ordersplit();                           // get order from cookies
window.onerror = reportError;           // set browser to report errors
//
//
function reportError()
{
if (debug == true) {return false}
else {return true};
};
//
function GetCookie (key)
{
//
// This function will return either the value associated with the key or a
// single blank.
//
txt = document.cookie;                      //  txt = the cookie
var keyt = key + "=";                       //  keyt = search string
var klen = keyt.length;                     //  klen = l'search string
var spos = txt.indexOf(keyt);               //  spos = the start pos of key
if (spos > -1)                              //  if the key was found
   {                                        //     look for the value
    vpos = spos+klen;                       //     vpos = value start pos
    var epos = txt.indexOf(";", vpos);      //     epos = value end pos
    if (epos == -1)                         //     if at end of cookie
      {epos = txt.length};                  //        epos = end of cookie
    return unescape(txt.substring(vpos, epos)); // return the value
   }
else                                        //  if the key was not found
   {return " ";};                           //     return a blank
};
//
function SetCookie (key, val)
{
//
// This function will add the value associated with the key to a cookie
// that lasts six weeks.
//
var exp = new Date();                       //  exp = today's date
exp.setTime(exp.getTime() + (6*7*24*60*60*1000)); // exp = today + 6 weeks (mil)
document.cookie = key + "=" + escape(val) + "; expires=" + exp.toGMTString() + "; path=/";                                //  write cookie 
};
//
function RemCookie (key)
{
//
// This function will remove a cookie.
//
var exp = new Date();                       //  exp = today's date
document.cookie = key + "=" + escape(" ") + "; expires=" + exp.toGMTString() + "; path=/";                                 //  write cookie 
};
//
function GetDesc (code)
{
//
// This function will return the description associate with product code
//
for (i=1; i<=descval.length; i++)
   {
    if (code == desccode[i])
      {
       return descval[i];
      };
   };
};
//
function ordersplit ()
{
//
//  this function will split aorder and aorder2
//  up into five arrays of entry values.
//
for (i=1; i<=maxorder; i++)
   {
    aquan[i] = "";
    asize[i] = "";
    acode[i] = "";
    aprice[i] = "";
    adesc[i] = "";
   };
var txt = aorder + aorder2;
if (txt == " " || txt == "")
   {gnum = 0;}
else
   { 
    var ndpos = txt.indexOf("~");
    gnum = parseInt(txt.substring(0,ndpos));
    if (isNaN(gnum)) {gnum=0;}
    else
    {   for (i=1; i<=gnum; i++)
          {
           var stpos = ndpos + 1;
           ndpos = txt.indexOf("~",stpos);
           aquan[i] = txt.substring(stpos,ndpos);
           stpos = ndpos + 1;
           ndpos = txt.indexOf("~",stpos);
           asize[i] = txt.substring(stpos,ndpos);
           stpos = ndpos + 1;
           ndpos = txt.indexOf("~",stpos);
           acode[i] = txt.substring(stpos,ndpos);
           stpos = ndpos + 1;
           ndpos = txt.indexOf("~",stpos);
           aprice[i] = txt.substring(stpos,ndpos);
           stpos = ndpos + 1;
           ndpos = txt.indexOf("~",stpos); 
           adesc[i] = parseInt(txt.substring(stpos,ndpos));
          };
      };
   };
};
//
function orderbuild ()
{
var txt = gnum + "~";
var nd = gnum;
if (gnum > parseInt(maxorder/2))
   {nd = parseInt(maxorder/2)};
for (i=1; i<=nd; i++)
   {
    txt = txt + aquan[i] + "~" + asize[i] + "~" + acode[i] + "~" + aprice[i] + "~" + adesc[i] + "~";
   };
aorder = txt;
SetCookie("torder",aorder);
if (gnum > parseInt(maxorder/2))
   {
    txt = "";
    for (i=nd+1; i<=gnum; i++)
       {
        txt = txt + aquan[i] + "~" + asize[i] + "~" + acode[i] + "~" + aprice[i] + "~" + adesc[i] + "~";
       };
    aorder2 = txt;
    SetCookie("torder2",aorder2);
   };
};
//
function infosplit ()
{
//
//  This function will break up the info cookie into customer information
//
var txt = GetCookie("info");
if (txt != " " && txt != "")
   { 
    var ndpos = txt.indexOf("~");
    gname = unescape(txt.substring(0,ndpos));
    var stpos = ndpos + 1;
    var ndpos = txt.indexOf("~",stpos);
    gemail = unescape(txt.substring(stpos,ndpos));
    stpos = ndpos + 1;
    ndpos = txt.indexOf("~",stpos);
    gaddr1 = unescape(txt.substring(stpos,ndpos));
    stpos = ndpos + 1;
    ndpos = txt.indexOf("~",stpos);
    gaddr2 = unescape(txt.substring(stpos,ndpos));
    stpos = ndpos + 1;
    ndpos = txt.indexOf("~",stpos);
    gtown = unescape(txt.substring(stpos,ndpos));
    stpos = ndpos + 1;
    ndpos = txt.indexOf("~",stpos);
    gstate = unescape(txt.substring(stpos,ndpos));
    stpos = ndpos + 1;
    ndpos = txt.indexOf("~",stpos);
    gstatec = txt.substring(stpos,ndpos);
    stpos = ndpos + 1;
    ndpos = txt.indexOf("~",stpos);
    gcountry = unescape(txt.substring(stpos,ndpos));
    stpos = ndpos + 1;
    ndpos = txt.indexOf("~",stpos);
    gcountryc = txt.substring(stpos,ndpos);
    stpos = ndpos + 1;
    ndpos = txt.indexOf("~",stpos);
    gzip = unescape(txt.substring(stpos,ndpos));
    stpos = ndpos + 1;
    ndpos = txt.indexOf("~",stpos);
    gtele = unescape(txt.substring(stpos,ndpos));
   };
};
//
function infobuild ()
{
//
//  This function will put the customer information together into the
//  info cookie.
//
txt = escape(gname) + "~" + escape(gemail) + "~" + escape(gaddr1) + "~" + escape(gaddr2) + "~"  + escape(gtown) + "~" + escape(gstate) + "~" + gstatec + "~" + escape(gcountry) + "~" + gcountryc + "~" + escape(gzip) + "~"+ escape(gtele) + "~";
SetCookie("info",txt);
};
//
function ReptoCart(idx,siz,quan)
{
//
//  This function will replace the elements of an order
//
gsize = siz;
gquan = quan;
aquan[idx] = gquan;
asize[idx] = gsize;
};
//
function UpdtCart()
{
//
//  This function will update the cart with any changes made to the order
//
var form = document.order;
for (var i=1; i<=gnum; i++)
   {
    var siz = form["size" + i].selectedIndex - 1;
    var quant = parseInt(form["quan" + i].value);
    if (isNaN(quant) || quant == "")
       {
        form["quan" + i].value=0;
        quant = 0;
       };
ReptoCart(i,siz,quant);
   };
orderbuild();
fillForm();
};
//
function fillForm()
{
//
//  Fill in form
//
var form = document.order;
var subtot = 0.00;
var gcnt = 0;
//
// fill in individual entries
//
for (i=1; i<=gnum; i++)
   {
    form["code" + i].value = acode[i];
    var quant = parseInt(aquan[i]);
    if (isNaN(quant) || quant == "")
       {
        quant = 0;
        aquan[i] = 0;
       };
    form["quan" + i].value = quant;
    gcnt = gcnt + parseInt(aquan[i]);
    var szindx = parseInt(asize[i])+1;
    form["size" + i].options[szindx].selected = true;
    form["desc" + i].value = descval[adesc[i]];
    form["price" + i].value = formatBucks(aprice[i]);
    var t = aprice[i] * aquan[i];
    form["tot" + i].value = formatBucks(t);

    subtot = subtot + t;
   };
//
//  fill in total entries
//
document.order.subtotalb.value = formatBucks(subtot);
SetCookie("quantity",gcnt);
gdiscount = "0%";
if (gcnt >= 3 && gcnt < 10)
   {
    gdiscount = "5%";
    subtot = parseInt((subtot * 95) / 100);
   };
if (gcnt >= 10)
   {
    gdiscount = "10%";
    subtot = parseInt((subtot * 90) / 100);
   };
document.order.discount.value = gdiscount;
document.order.subtotal.value = formatBucks(subtot);
SetCookie("subtot",subtot);
if (document.order.countries.options[document.order.countries.selectedIndex].text == "United States" &&
    document.order.states.options[document.order.states.selectedIndex].value == "CA")
   {
    var tax = parseInt((subtot * 8)/100);
   }
else
   {
    tax = 0;
   };
document.order.salestax.value = formatBucks(tax);
SetCookie("tax",tax);
if (document.order.countries.options[document.order.countries.selectedIndex].text == "United States" || document.order.countries.options[document.order.countries.selectedIndex].text == "Canada")
   {var ship = 500 + 70 * (gcnt-1);}
else
   {ship = 900 + 115 * (gcnt-1)};
if (gcnt == 0)
   {ship = 0};
document.order.shipping.value = formatBucks(ship);
SetCookie("shipping",document.order.shipping.value);
var temptot = parseInt(eval(subtot + tax + ship));
document.order.total.value = formatBucks(temptot);
SetCookie("total",document.order.total.value);
setInfo();
};
//
function formatBucks (amt)
{
//
//  This function will format a dollar amount
//
var dollars = parseInt(eval(amt/100));
var cents = parseInt(eval(amt - dollars*100));
if (cents < 10) {cents2="0"}
else {cents2=""};
return "$" + dollars + "." + cents2 + cents;
};
//
function checkForm()
{
//
//  This function will check the specification to be certain the form is
//  complete.
//
var retval = true;
if (document.order.email.value.length < 1)
   {
    alert ("Please fill in your email address and resubmit.");
    retval = false;
   };
if (document.order.nam.value.length < 1)
   {
    alert ("Please fill in your name and resubmit.");
    retval = false;
   };
if (document.order.addr1.value.length < 1)
   {
    alert ("Please fill in your street address and resubmit.");
    retval = false;
   };
if (document.order.town.value.length < 1)
   {
    alert ("Please fill in your city or town and resubmit.");
    retval = false;
   };
if (document.order.zip.value.length < 1)
   {
    alert ("Please fill in your ZIP or postal code and resubmit.");
    retval = false;
   };
if (document.order.addr2.value.length > 0)
   {document.order.cmma.value = ", ";}
else
   {document.order.cmma.value = "";}
setInfo();
return retval;
};
//
function setInfo()
{
//
//  This function will save current customer information
//
gname = document.order.nam.value;
gemail = document.order.email.value;
gaddr1 = document.order.addr1.value;
gaddr2 = document.order.addr2.value;
gtown = document.order.town.value;
gcountry = document.order.countries.options[document.order.countries.selectedIndex].text;
gcountryc = document.order.countries.selectedIndex;
gstate = document.order.states.options[document.order.states.selectedIndex].text;
gstatec = document.order.states.selectedIndex;
gzip = document.order.zip.value;
gtele = document.order.phone.value;
infobuild();
};
//
function getInfo ()
{
//
//  This function will get customer information, if any
//
infosplit();
if (gname != "") {document.order.nam.value=gname};
if (gemail != "") {document.order.email.value=gemail};
if (gaddr1 != "") {document.order.addr1.value=gaddr1};
if (gaddr2 != "") {document.order.addr2.value=gaddr2};
if (isNaN(gcountryc) || gcountryc == "" || gcountryc == " ")
   {gcountryc = 188};
document.order.countries.options[gcountryc].selected=true;
document.order.country.value = document.order.countries.options[gcountryc].text;
if (gtown != "") {document.order.town.value=gtown};
if (isNaN(gstatec) || gstatec == "" || gstatec == " ")
   {}
else
   {
    if (document.order.country.value == "United States" ||
        document.order.country.value == "Canada")
       { 
        document.order.states.options[gstatec].selected=true;
        document.order.state.value = document.order.states.options[gstatec].text;
       };
   };
if (gzip != "") {document.order.zip.value=gzip};
if (gtele != "") {document.order.phone.value=gtele};
infobuild();
}; 
//
// The following functions animate the update button
//
//    global variables
var update1 = new Image;
var update2 = new Image;
var update3 = new Image;
var update4 = new Image;
update1.src = "update.gif";
update2.src = "update2.gif";
update3.src = "update3.gif";
update4.src = "update4.gif";
var gfr = " ";
//
//      cover flip - 1
function animatu (fr)
{
gfr = fr;
form = document.order;
form[fr].src=update3.src;
t = setTimeout("animatv();",44);
};
//     cover flip - 2
function animatv ()
{
form = document.order;
form[gfr].src=update1.src;
};
//
//     update action - 1
function animatw (fr)
{
gfr = fr;
form = document.order;
form[fr].src=update4.src;
t = setTimeout("animatx();",24);
};
//     update action - 2
function animatx ()
{
UpdtCart();
form = document.order;
form[gfr].src=update1.src;
};
//
//  The following functions animate the place order button
//
var plac = new Image;
var placf = new Image;
var plac1 = new Image;
var plac2 = new Image;
var plac3 = new Image;
plac.src = "place.gif";
placf.src = "placef.gif";
plac1.src = "placex1.gif";
plac2.src = "placex2.gif";
plac3.src = "placex3.gif";
//
// animate the place order button action
// 
function animate()
{
document.order.placeorder.src=plac1.src;
var t = setTimeout("animate2()",14);
};
function animate2()
{
document.order.placeorder.src=plac2.src;
var t = setTimeout("animate3()",37);
};
function animate3()
{
document.order.placeorder.src=plac3.src;
var t = setTimeout("animate4()",18);
};
function animate4()
{
document.order.placeorder.src=plac.src;
var t = setTimeout("animate5()",1);
};
function animate5()
{
UpdtCart();
if(checkForm())
   {document.order.submit();};
};
//
