var items = new Array(2);
var numitems = 0;
var lastCurrency;

function Item(desc,currency,price,prodcode,qty1)
{
	this.MC_desc = desc;
	this.MC_currency = currency;
	this.MC_price = price;
	this.MC_quantity = qty1;
	this.MC_code = prodcode;
}

function cSymbol(c)
{
	if (c = 'AUD')
	{
		return "$A"; 
	}	
	else
	{
		return "$";
	}
	lastCurrency = c;
}


function additem(desc,currency,price,prodcode,quantity)
{
	var x = items.length - 1;
	var found = (x == 0);
	var y = new Item(desc,currency,price,prodcode,quantity);
	var z = y;

	while((x != 0) && (found == false))
	{
		z = items[x];

		if(z != null)
		{
			if(z.MC_desc == desc)
			{
				found = true;
			}
			else
			{
				x--;
			}
		}
		else
		{
			x--;
		}
	}


	if(found == false)
	{
		items[++numitems] = y;
	}
	else
	{
		items[x].MC_quantity += quantity;
	}

	displaycart();
}

function displaycart()
{
	var totalcost=0;
	var i;
	var q = 0;

	for(i=1;i<=numitems;i++)
	{
		if(items[i] != null)
		{
			q += items[i].MC_quantity;
		}
	}

	with (parent.cart.document)
	{
		open();
		write("<html><head><link rel=stylesheet type=text/css href=\"comic.css\">");
		write("</head><body class='cart'>");
		write("<form name='form1' method=post target = 'wpDetails' action='http://www.pennantsw.com/details.php'>");
		write("<h2>Shopping Cart</h2>");
		if ((numitems==0) || (q==0))
		{
			<!-- This message is displayed when the shopping cart is empty. -->
			write("<p>Click on a 'BUY NOW' link, at right, to start adding products to your cart.</p>");
			close();
			return;
		}
		write("<TABLE align=center BORDER=0 CELLSPACING=0 CELLPADDING=3>")
		write("<TR><TD colspan = '4'><B>(<I>If you change the qty, tab to recalculate</I>)</B></TD></TR>");
		for(i=1;i<=numitems;i++)
		{
			if(items[i] != null)
			{
				if(items[i].MC_quantity > 0)
				{
					write("<TR><TD>");
					write("<INPUT name='qty' TYPE='TEXT' SIZE=2 VALUE=");
					write(items[i].MC_quantity + " onBlur='return parent.products.finalcart();'>");
					write("</TD><TD colspan=\"3\">" + items[i].MC_desc + "</TD></TR>");
					write("<TR><TD></TD><TD></TD><FONT SIZE=-1><TD>");
					write("<A HREF=\"javascript:parent.products.additem\('" + items[i].MC_desc + "','" + items[i].MC_currency + "'," + items[i].MC_price + ",'" + items[i].MC_code + "',1\);\" class='cart'>ADD</A>");
					write("</TD><TD><FONT SIZE=-1><A HREF=\"javascript:parent.products.additem\('" + items[i].MC_desc + "','" + items[i].MC_currency + "'," + items[i].MC_price + ",'" + items[i].MC_code + "',-1\);\" class='cart'>REMOVE</A>");
					write("</TD></TR>");
					totalcost += (items[i].MC_price * items[i].MC_quantity);
				}
			}
		}
		write("<tr></tr>");
		totalcost = roundit(totalcost,2);
		write("<TR><TD COLSPAN=2 ALIGN=RIGHT><FONT SIZE=-1><B><p style=text-align:right>Total Cost:</p></B>");
		write("</FONT></TD><TD ALIGN=RIGHT><FONT SIZE=-1>$A" + totalcost);
		write("</FONT></TD><TD COLSPAN=1>&nbsp;</TD></TR></TABLE>");
		write("<TABLE align=center BORDER=0 CELLSPACING=0 CELLPADDING=3 <tr><td><br><p style=text-align:center>");
		write("<input type=hidden name='currency' value='" + items[1].MC_currency + "'>");
		write("<input type=hidden name='testMode' value='" + parent.header.testMode + "'>");

		for(i=1;i<=numitems;i++)
		{
			if(items[i] != null)
			{
				if(items[i].MC_quantity > 0)
				{
					write("<input type=hidden name='price[" + items[i].MC_desc + "]' value='" + items[i].MC_price + "'>");
					write("<input type=hidden name='qty[" + items[i].MC_desc + "]' value='" + items[i].MC_quantity + "'>");
					write("<input type=hidden name='prodCode[" + items[i].MC_desc + "]' value='" + items[i].MC_code + "'>");
				}
			}
		}
		write("<input type=submit value='Complete Order'>");
		write("</td></tr></table>");
		write("</body></html>");
		close();
	}

}

function finalcart()
{
	updatecart();
	return true;
}

function updatecart()
{
	var y = new Array(1);
	var z = 1;

	for(var x = 1; x <= numitems; x ++)
	{

		if(items[x])
		{
			if(items[x].MC_quantity > 0)
			{
				y[z] = items[x];
				z++;
			}
		}
	}

	y[0] = items[0];
	items = y;
	numitems = z - 1;


	for (i=numitems; i>0; i--)
	{

		if(items[i] != null)
		{

			if (numitems == 1)
			{
				items[i].MC_quantity=parent.cart.document.form1.qty.value;
			}
			else
			{
				items[i].MC_quantity=parent.cart.document.form1.qty[i-1].value;
			}

		}

	}

	displaycart();

}

function roundOff(value, precision)
{
	return places(value,1,precision);
}

function places(X, M, N)
{
	var T, S=new String(Math.round(X*Number("1e"+N)))
	while (S.length<M+N) S='0'+S
	var y = S.substr(0, T=(S.length-N));
	if(N>0)
	{
		y += '.' + S.substr(T, N);
	}

	return y;

}

function popup(page,name,w,h,winstring)
{
	var win=window.open(page,name,winstring + ',width=' + w + ',height=' + h);

	if (navigator.appname == "Netscape")
	{
		if (navigator.appVersion.indexOf('3')>=0)
		win.focus();
		if (navigator.appVersion.indexOf('3')<0)
		win.opener = self;
	}

}

// function to round the results of calculations
function roundit(Num, Places) {
   if (Places > 0) {
      var NumString;
      if ((Num.toString().length - Num.toString().lastIndexOf('.')) > (Places + 1)) {
         var Rounder = Math.pow(10, Places);
         Num = Math.round(Num * Rounder) / Rounder;
      }
      NumString = Num.toString();
      if (NumString.lastIndexOf('.') == -1) {
      	 NumString = NumString + '.';
      }
      while ((NumString.length - NumString.lastIndexOf('.')) < (Places + 1)) {
         NumString = NumString + '0';
      }
   }
   else NumString = Math.round(Num);
   return NumString;
}

function displayproducts()
{
	var k;
	var header;
	document.write("<p>You can purchase using your credit card, using the WorldPay on-line secure server. Click on the 'BUY NOW' link(s) for the product(s) you wish to purchase. When you have selected your products, click on the 'Complete Purchase' button.");
	header = ProdTab[0][0];
	document.write("<p><b>" + header + "</b></p>\n");
	document.write("<UL>\n");
	for (k=0; k<ProdTab.length; k++) 
	with (document)
	{
		if (header != ProdTab[k][0])
		{
			write("</UL>");
			header = ProdTab[k][0];
			write("<p><b>" + header + "</b></p>\n");
			write("<UL>\n");
		}
		write("<li><b>");
		write(ProdTab[k][1]);
		write("</b><UL><li>Price: ");
		write(cSymbol(ProdTab[k][3]));
		write(ProdTab[k][4]);
		write("&nbsp;&nbsp;&nbsp;&nbsp;<A HREF=\"javascript:parent.products.additem('" + ProdTab[k][2] + "','" + ProdTab[k][3] + "'," + ProdTab[k][4] + ",'" + ProdTab[k][5] + "',1) \")>");
		write("<b>BUY NOW</b></a></li></UL>");
	}
	document.write("</UL>");
}
