order_total=0, item_num=1, present_item=1, items_ordered=0, total_item_price=0, maxarray=50;
large_shipping = 7.99;
small_shipping = 3.49;

function getCookie (name) {
var dcookie = document.cookie; 
var cname = name + "=";
var clen = dcookie.length;
var cbegin = 0;
        while (cbegin < clen) {
        var vbegin = cbegin + cname.length;
                if (dcookie.substring(cbegin, vbegin) == cname) { 
                var vend = dcookie.indexOf (";", vbegin);
                        if (vend == -1) vend = clen;
                return unescape(dcookie.substring(vbegin, vend));
                }
        cbegin = dcookie.indexOf(" ", cbegin) + 1;
                if (cbegin == 0) break;
        }
return null;
}

function setCookie (name, value, expires) {
        if (!expires) expires = new Date();
document.cookie = name + "=" + escape (value) + 
"; expires=" + expires.toGMTString() +  "; path=/";
}

function delCookie (name) {
var expireNow = new Date();
document.cookie = name + "=" +
"; expires=Thu, 01-Jan-09 00:00:01 GMT" +  "; path=/";
}

var expdate = new Date();
 
expdate.setTime (expdate.getTime() +  (24 * 60 * 60 * 1000 * 365)); 

function setCookieArray(name){
    temp_length = setCookieArray.arguments.length - 1;
        for (var i = 0; i < temp_length; i++) {
        data = setCookieArray.arguments[i + 1]
        setCookie (name + i, data, expdate);
        }        
    }

function item_tot_price(i)
{
 total_item_price = eval((itemlist[i].price * itemlist[i].quan));
 return total_item_price;
}


function all_order_totals()
{
sub_total = 0;

if (item_num > 0)
for (i =1;i < item_num;i++)
 sub_total = sub_total + item_tot_price(i)
   
return sub_total;
}


function all_order_totals_plus_shipping()
{
order_total = 0.00;

if (sub_total < 20)
order_total = sub_total + small_shipping;
else if (sub_total > 100)
order_total = sub_total;
else 
order_total = sub_total +large_shipping;

return order_total;
}


function remove_nil_items(inputlist)
{var i = 0; 
 var j = 1; 
  for (i=1;i<item_num;i++)
   {if (itemlist[i].quan != 0)
    {temp_array[j]=itemlist[i]
     items_ordered =j 
     j=j+1
     } 
   } 
  itemlist = temp_array
  item_num = items_ordered + 1
}

function update_this_page()
{
i = k =0; 

 {for (i = 0;i < parent.body.document.form1.elements.length;i++)
  {for (k=1;k<=items_ordered;k++)
   {
if ((itemlist[k].code ==  parent.body.document.form1.elements[i].name) && (k<=items_ordered) && (i <= parent.body.document.form1.elements.length))
         parent.body.document.form1.elements[i].value = itemlist[k].quan
   }
  }
 }   
}

function item_quan(code)
{
var loc = check_if_in(code)
if (loc > 0)
 var quantities = itemlist[loc].quan
else
 var quantities = 0;
return quantities
}


function createArray(n)
{ this.length = n
  var i = 0
   for (i = 1 ; i < n ; i++) 
    this[i] = null;	
 return this;
}


function product(code,price,desc,quan,url,imageurl)
{ this.code = code
  this.price = price
  this.desc = desc
  this.quan = quan
  this.url = url
  this.imageurl = imageurl
 return this;
}


var itemlist = new createArray(50);
var temp_array = new createArray(50);

function initialize_arrays(arraysa)
 {
  for (i = 1;i < maxarray;i++)
   {
    arraysa[i] = new product('',0,'',0,'')
   }
 }

function updatemain_order()
 {
  parent.frames[0].document.close()
  parent.frames[0].location = "checkout.php";
  parent.frames[0].document.close()
 }

function viewcart()
 {
  parent.frames[0].document.close()
  parent.frames[0].location = "cartcontents.php";
  parent.frames[0].document.close()
 }

function check_if_in(code_check)
 {
  var i=1, loc=0;
  while ((i < item_num) && (itemlist[i].code != code_check))
   i = i + 1;
  if (itemlist[i].code == code_check)
   loc=i;
  else
   loc=-1;
 return loc;
 }

function additem(codes,prices,descrip,url,imageurl)
{
 loc = check_if_in(codes)
 if (loc != -1)
  {
   olditem =  itemlist[loc].quan
   itemlist[loc] = new product(codes,prices,descrip,olditem + 1,url,imageurl)
  }
  else
   {
    olditem =  itemlist[item_num].quan
    itemlist[item_num] = new product(codes,prices,descrip,olditem + 1,url,imageurl);
    items_ordered = item_num
    item_num = item_num + 1
   }
 remove_nil_items(itemlist)

}

function subitem(codes,prices,descrip,url,imageurl)
{
loc = check_if_in(codes)
if ((loc != -1) && (itemlist[loc].quan > 0)) 
 {
  olditem =  itemlist[loc].quan
  itemlist[loc] = new product(codes,prices,descrip,olditem - 1,url,imageurl)}
 }

initialize_arrays(itemlist)
initialize_arrays(temp_array)
