
var Cart_products = new Array()
var Cart_lineItems = new Array()
var Cart_total = 0  // current total 
var Cart_description = "" // single string description of all items in cart
var Cart_allItems = "" // a ; seperated list of product codes and qtys

var Cart_currency = "USD"
var Cart_packSizeIndex = 0
var Cart_packSizeText = "1"
var Cart_rates = new Array()
var Cart_currencySymbols = new Array()

///////////////////////////////////////////////////////////////////
// product data - 


Cart_products["MUSEUM-20K"] = new Product("MUSEUM-20K", "Museum Email Archive - 20K", "/museum/index.html",	195  );
Cart_products["MUSEUM-50K"] = new Product("MUSEUM-50K", "Museum Email Archive - 50K", "/museum/index.html",	 395  );
Cart_products["MUSEUM-Max"] = new Product("MUSEUM-Max", "Museum Email Archive - Max", "/museum/index.html",	495  );


Cart_currency = "USD"
Cart_rates["USD"] = 1
Cart_rates["AUD"] = 1.63
Cart_rates["EUR"] = 0.931
Cart_rates["GBP"] = 0.647
Cart_currencySymbols["USD"] = "$"
Cart_currencySymbols["AUD"] = "$"
Cart_currencySymbols["GBP"] = "£"
Cart_currencySymbols["EUR"] = "&#8364;"

///////////////////////////////////////

function getObj(name)
{
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
  return this;
}



////////////////////////////////////////////

function Product(code, desc, infoUrl, price)
{
	this.code = code;
	this.desc = desc;
	this.prices = new Array(1)
	this.prices[0] = price;
	this.infoUrl = infoUrl;
	return this;
}

function formatPrice(expr)
{
    var decplaces = 2;
    var str = "" + Math.round(eval(expr)*Math.pow(10,decplaces));
    while(str.length <= decplaces)
        str = "0"+str;
    var decpoint = str.length  - decplaces;
    return  Cart_currencySymbols[Cart_currency] + str.substring(0, decpoint)+"."+ str.substring(decpoint, str.length);
}
function formatNum(expr)
{
    var decplaces = 2;
    var str = "" + Math.round(eval(expr)*Math.pow(10,decplaces));
    while(str.length <= decplaces)
        str = "0"+str;
    var decpoint = str.length  - decplaces;
    return  str.substring(0, decpoint)+"."+ str.substring(decpoint, str.length);
}


////////////////////////////////////////////////////////

function Cart_LineItem(product,  qty)
{
	this.product = product;
	this.packSizeIndex = Cart_packSizeIndex ;
	this.packSizeText = Cart_packSizeText;
	this.qty = qty;

	return this;
}
// makes a html table of the current state of the shopping cart
function Cart_makeTable()
{
	var gotone = false
	var s = '<table border="0" cellpadding="5" cellspacing="0" width="742">'
	Cart_total = 0
	Cart_description = ""
	Cart_allItems = ""
	
	for (var i = 0; i < Cart_lineItems.length; i++)
	{

		var li = Cart_lineItems[i]
		if (li == null)
			continue
			
		var amt = li.qty * li.product.prices[li.packSizeIndex]
		
		var desc = ""
		desc = li.qty + " x " ;
		
		s += "<tr>\n";
		s += "<td class = lineitem width = '356'>"
		s += desc
		if (gotone)
			Cart_description += ", "+desc + " " + li.product.desc ;
		else
			Cart_description += desc + " " + li.product.desc ;
	
		Cart_allItems += li.product.code + ";" + li.qty + ";" + li.packSizeText + ";" + Cart_currency + ";" +formatNum(amt *Cart_rates[Cart_currency]) +";"

		s += li.product.desc  
		s += "</td>\n"
		s += "<td class = lineitem width = '130' align=right>"
		Cart_total += amt
		s += formatPrice(amt *Cart_rates[Cart_currency])
		s += "</td>\n"

		s += "<td align=right>"
		s += "<a href=\"javascript:Cart_remove('"+i+"')\">remove from cart</a>"
		s += "</td>\n"

		s += "</tr>\n"
		gotone =true
	}
	if (!gotone)
		return ""
	s += "</table>\n";
	return s
}




// replaces the innerHTML with a table representing the current cart
// looks for a div with a tag of "cart".
function Cart_redraw()
{
	var s  = Cart_makeTable();
	if (s.length == 0)
		s = "Shopping cart is empty. Click on one of the 'add to cart' links above.";
		
	Cart_total *= Cart_rates[Cart_currency]
	s += "<h1>Total <span style = 'width:400; text-align: right;'>"
	s += formatPrice(Cart_total)
	s += "</span></h1>\n"
	var cartDiv = new getObj("cart")
	cartDiv.obj.innerHTML = s
	
}

function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
	        ((expires) ? "; expires=" + expires.toGMTString() : "") +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			((secure) ? "; secure" : "");
}


function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}


function setOurCookie()
{
	var cookie  = "";
	for (var i = 0; i < Cart_lineItems.length; i++)
	{
	 	var li = Cart_lineItems[i]
		if (li == null)
			continue

		cookie += 
			li.product.code + "," +
			li.packSizeIndex + "," +
			li.packSizeText + "," +
			li.qty + ",";
	}
	setCookie("pdfmCart", cookie);
}

// adds an item to the cart
function Cart_add(code)
{
	// first see if it already exists
	for (var i = 0; i < Cart_lineItems.length; i++)
	{
	 	var li = Cart_lineItems[i]
		if (li == null)
			continue

		if (li.product.code == code && li.packSizeIndex == Cart_packSizeIndex )
		{
			li.qty++
			Cart_redraw()
			setOurCookie()
			return
		}
	}
	Cart_lineItems[Cart_lineItems.length] = new Cart_LineItem(Cart_products[code], 1)
	Cart_redraw()
	setOurCookie()
}

// removes an item from the cart
function Cart_remove(i)
{
	delete Cart_lineItems[i];
	Cart_lineItems[i] = null
	Cart_redraw()
	setOurCookie()
}

function Cart_onLoad()
{
	var s = getCookie("pdfmCart");
	if (!s)
		return;

	var arr = s.split(",");
	var oldPackSizeIndex = Cart_packSizeIndex
	var oldPackSizeText  = Cart_packSizeText

	for(var i = 0; i < arr.length; i+=4 )
	{
		var code = arr[i]
		var packIndex = arr[i+1]
		var packText = arr[i+2]
		var qty = arr[i+3]
		Cart_packSizeIndex = packIndex
		Cart_packSizeText = packText
		for (var z = 0; z < qty; z++)
			Cart_add(code);
	}

	Cart_packSizeIndex = oldPackSizeIndex
	Cart_packSizeText = oldPackSizeText
}
