var STR_shopping_cart_is_empty = "Il carrello è vuoto. Fare clic su uno dei 'aggiungi al carrello' link qui sopra.";
var STR_add_to_cart  = "aggiungi al carrello"
var STR_remove_from_cart = "elimina dal carrello"

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_packSizeIndex = 0
var Cart_packSizeText = "1"
var Cart_rates = new Array()
var Cart_currencySymbols = new Array()

///////////////////////////////////////////////////////////////////
// product data - 

// ------- being auto generated by calcprices.php


Cart_products["PDFM-OFF"] = new Product("PDFM-OFF", "pdfMachine office", "/genp_it/pdfmachine_office.html", "/images/pdf_toolbar_office.gif", 
	new Array( 29 ,118.367346939 ,195.306122449 ,369.897959184 ,651.020408163 ,1183.67346939 ,2367.34693878 ,2959.18367347 ,3551.02040816 ,4734.69387755));
Cart_products["PDFM-PRO"] = new Product("PDFM-PRO", "pdfMachine pro", "/genp_it/pdfmachine_pro.html", "/images/pdf_toolbar_pro.gif", 
	new Array( 49 ,200 ,330 ,625 ,1100 ,2000 ,4000 ,5000 ,6000 ,8000));
Cart_products["PDFM-ULT"] = new Product("PDFM-ULT", "pdfMachine ultimate", "/genp_it/pdfmachine_ultimate.html", "/images/pdf_toolbar_ultimate.gif", 
	new Array( 99 ,404.081632653 ,666.734693878 ,1262.75510204 ,2222.44897959 ,4040.81632653 ,8081.63265306 ,10102.0408163 ,12122.4489796 ,16163.2653061));
Cart_products["PDFM-VP1"] = new Product("PDFM-VP1", "pdfMachine version protection 1 additional year", "/genp_it/pdfmachine_version_protection.html", "", 
	new Array( 19 ,77.5510204082 ,127.959183673 ,242.346938776 ,426.530612245 ,775.510204082 ,1551.02040816 ,1938.7755102 ,2326.53061224 ,3102.04081633));
Cart_products["PDFM-VP2"] = new Product("PDFM-VP2", "pdfMachine version protection 2 additional years", "/genp_it/pdfmachine_version_protection.html", "", 
	new Array( 34 ,138.775510204 ,228.979591837 ,433.673469388 ,763.265306122 ,1387.75510204 ,2775.51020408 ,3469.3877551 ,4163.26530612 ,5551.02040816));
Cart_products["PDFM-O2U"] = new Product("PDFM-O2U", "pdfMachine upgrade - office to ultimate", "", "", 
	new Array( 79 ,322.448979592 ,532.040816327 ,1007.65306122 ,1773.46938776 ,3224.48979592 ,6448.97959184 ,8061.2244898 ,9673.46938776 ,12897.9591837));
Cart_products["PDFM-P2U"] = new Product("PDFM-P2U", "pdfMachine upgrade - pro to ultimate", "", "", 
	new Array( 59 ,240.816326531 ,397.346938776 ,752.551020408 ,1324.48979592 ,2408.16326531 ,4816.32653061 ,6020.40816327 ,7224.48979592 ,9632.65306122));
//----- end auto generated code

Cart_currency = "EUR"
Cart_rates["USD"] = 1
Cart_rates["AUD"] = 1.15
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, imgUrl, prices)
{
	this.code = code;
	this.desc = desc;
	this.prices = new Array()
	this.prices = prices;
	this.infoUrl = infoUrl;
	this.imgUrl = imgUrl;
	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 makeProductHref(prod)
{
	if (prod.infoUrl.length == 0)
		return prod.desc;
	var s = "<a href = \"javascript:var x=window.open('"+ prod.infoUrl + "','pdfMachine', 'scrollbars,width=850,height=600,left=0,top=0')\">" ;
	if (prod.imgUrl.length == 0)
		return s + prod.desc + "</a>";

	return s + "<img border=0 src='"+ prod.imgUrl + "'></a>";
}
	
// iterate thru products array building a html table
function Product_makePriceTable()
{
	
	var s = ""
	s += '<table border="0" cellpadding="5" cellspacing="0" width="742">\n'
	

	for (var code in Cart_products)
	{
		var prod = Cart_products[code];
		s += "<tr>\n";


		s += "<td width = '356'>\n"
		s += makeProductHref(prod);
		s += "</td>\n"

		s += "<td width = '130' align=right>\n"
		s += formatPrice(prod.prices[Cart_packSizeIndex] *Cart_rates[Cart_currency]) 
		s += "</td>\n"

		s += "<td align=right>\n"


		s += "<a href=\"javascript:Cart_add('"+prod.code+"')\">" + STR_add_to_cart + "</a>"
		s += "</td>\n"

		s += "</tr>\n"
	}
	s += "</table>"
	return s
	
}


// replaces the innerHTML with a price table for the document identifed by 'tagName'
function Products_redraw(tagName)
{
	var priceTableDiv = new getObj(tagName)	;
	priceTableDiv.obj.innerHTML = Product_makePriceTable()
}

////////////////////////////////////////////////////////

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 = ""
		if (li.packSizeIndex  > 0)
			desc = li.qty + " x " + li.packSizeText + " pack "
		else
			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]) +";"

		if (li.product.infoUrl.length)
			s += "<a href = " + li.product.infoUrl +  ">"+ li.product.desc + "</a>"
		else
			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+"')\">" + STR_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 = STR_shopping_cart_is_empty;

		
	Cart_total *= Cart_rates[Cart_currency]
	s += "<h1>Total <span style = 'width:445; 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
}

