//===========================================================================
//  Shopping Cart technology copyright (c) 1997-2001 wirejunkie
//  DHTML scrollers copyright (c) 2001 wirejunkie
//  All JavaScript copyright (c) 1997-2002 wirejunkie
//  
//  Unauthorized use, copying, distribution, modification and
//  derivation is prohibited.  All rights reserved.
//  
//  If you want to use it, email us at info@wirejunkie.com, or
//  check out our web site at http://www.wirejunkie.com/
//===========================================================================

//---------------------------------------------------------------------------
// Shop
//---------------------------------------------------------------------------

function item(productType, id, name, price, qty)
{
	this.productType = productType;
	this.id = id;
	this.name = name;
	this.price = price;
	this.qty = qty;
	return this;
}

function basketFindItem(id)
{
	for (var i = 0; i < this.items.length; ++i)
	{
		if (this.items[i].id == id)
		{
			return i;
		}
	}
	return -1;
}

function basketSetQuantity(id, qty)
{
	var index = this.findItem(id);
	if (index >= 0)
	{
		if (qty > 0)
		{
			this.items[i].qty = qty;
		}
		else
		{
			basketRemoveItem(id);
		}
	}
}

function basketAddItem(productType, id, name, price, qty)
{
	var i = this.findItem(id);
	if (i >= 0)
	{
		this.items[i].qty += qty;
	}
	else
	{
		this.items[this.items.length] = new item(productType, id, name, price, qty);
	}
}

function basketRemoveItem(id)
{
	var index = this.findItem(id);
	if (index >= 0)
	{
		for (var i = index; i < this.items.length-1; ++i)
		{
			this.items[i] = this.items[i+1];
		}
		this.items.length--;		//! TODO - check this works
	}
}

function basketSaveInForm(f)
{
	var first = true;
	var bsk = '';
	for (var i = 0; i < this.items.length; ++i)
	{
		if (first)
		{
			first = false;
		}
		else
		{
			bsk += ',';
		}
		bsk += this.items[i].qty + 'x' + this.items[i].id;
	}
	f.bsk.value = bsk;
	f.resellerid.value = this.resellerId;
	f.orderedfrom.value = this.orderedFrom;
	f.site.value = this.site;
//	f.tshirt.value = (this.tshirt ? '1' : '0');

	return true;
}

// d = document, e = true to allow edits, p = product type
function basketPrintItems(d, e, pt)
{
	var first = true;
	var total = 0;
	for (var i = 0; i < this.items.length; ++i)
	{
		if (this.items[i].productType == pt)
		{
			if (first)
			{
				d.write('<tr><td colspan="');
				if (e) d.write('10'); else d.write('9');
				d.writeln('" class="prodtype"><small>' + pt + '</small></td></tr>');
				first = false;
			}
			d.writeln('<tr valign="top">');
			d.writeln('<td class="basket"><img src="images/spacer" width="4" height="1" alt="" /></td>');
			d.writeln('<td class="basket" width="160">' + this.items[i].name + '<br />&nbsp;</td>');
			d.writeln('<td class="basket">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>');
			d.writeln('<td class="basket" align="right">$' + decimalString(this.items[i].price) + '</td>');
			d.writeln('<td class="basket">&nbsp;</td>');
			d.writeln('<td class="basket" align="right">' + this.items[i].qty + '</td>');
			var subtotal = this.items[i].price * this.items[i].qty;
			total += subtotal;
			d.writeln('<td class="basket">&nbsp;</td>');
			d.writeln('<td class="basket" align="right">$' + decimalString(subtotal) + '</td>');
			d.writeln('<td class="basket">&nbsp;</td>');
			if (e) d.writeln('<td class="basket"><a href="javascript:deleteItem(' + this.items[i].id + ');">delete</a>&nbsp;</td>');
			d.writeln('</tr>');
		}
	}
	return total;
}

function basketHasOnlyMagazines()
{
	for (var i = 0; i < this.items.length; ++i)
	{
		if (this.items[i].productType != 'Magazines')
		{
			return false;
		}
	}
	return true;
}

// d = document, e = true to allow edits
function basketPrintBasket(d, e)
{
	d.writeln('<table class="basket" border="0" cellpadding="2" cellspacing="0">');
	d.writeln('<tr>');
	d.writeln('<th class="basket" align="left" colspan="3">Title</th>');
	d.writeln('<th class="basket" align="right">Price</th>');
	d.writeln('<th class="basket" align="right">&nbsp;&nbsp;</th>');
	d.writeln('<th class="basket" align="right">Qty' + (e ? '*' : '') + '</th>');
	d.writeln('<th class="basket" align="right">&nbsp;&nbsp;</th>');
	d.writeln('<th class="basket" align="right">Total</th>');
	d.write('<th class="basket" align="right"');
	if (e) d.write (' colspan="2"');
	d.writeln('>&nbsp;</th>');
	d.writeln('</tr>');
	var total = 0;
	total += this.printItems(d, e, 'Video');
	total += this.printItems(d, e, 'CD-ROM');
	total += this.printItems(d, e, 'DVD');
	total += this.printItems(d, e, 'Magazines');
	total += this.printItems(d, e, 'Toys');
	total += this.printItems(d, e, 'T-Shirts');
	total += this.printItems(d, e, 'Bongs');
	if (this.tshirt)
	{
		d.writeln('<tr>');
		d.writeln('<td class="basket"><img src="images/spacer" width="4" height="1" alt="" /></td>');
		d.writeln('<td class="basket">AAV T-Shirt</td>');
		d.writeln('<td class="basket">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>');
		d.writeln('<td class="basket" colspan="4">&nbsp;</td>');
		d.writeln('<td class="basket" align="right">');
		if (total < 100)
		{
			d.writeln('$' + decimalString(25));
			total += 25;
		}
		else
		{
			d.writeln('FREE');
		}
		d.writeln('</td>');
		d.writeln('<td class="basket">&nbsp;</td>');
		if (e) d.writeln('<td class="basket">&nbsp;</td>');
		d.writeln('</tr>');
	}
	if (this.hasOnlyMagazines())
	{
		d.writeln('<tr>');
		d.writeln('<td class="subtotal" colspan="4" align="right">P &amp; H</td>');
		d.writeln('<td class="subtotal">&nbsp;</td>');
		d.writeln('<td class="subtotal" align="right" colspan="3">\$' + decimalString(5) + '</td>');
		d.write('<td class="subtotal"');
		if (e) d.write(' colspan="2"');
		d.writeln('>&nbsp;</td>');
		d.writeln('</tr>');
		total += 5;
	}
	d.writeln('<tr>');
	d.writeln('<td class="subtotal" colspan="4" align="right">Subtotal</td>');
	d.writeln('<td class="subtotal">&nbsp;</td>');
	d.writeln('<td class="subtotal" align="right" colspan="3">\$' + decimalString(total) + '</td>');
	d.write('<td class="subtotal"');
	if (e) d.write(' colspan="2"');
	d.writeln('>&nbsp;</td>');
	d.writeln('</tr>');
	d.writeln('<tr>');
	d.writeln('<td class="subtotal" colspan="4" align="right"><small>Australian customers only</small> 10% GST</td>');
	d.writeln('<td class="subtotal">&nbsp;</td>');
	d.writeln('<td class="subtotal" align="right" colspan="3">\$' + decimalString(total * 0.1) + '</td>');
	d.write('<td class="subtotal"');
	if (e) d.write(' colspan="2"');
	d.writeln('>&nbsp;</td>');
	d.writeln('</tr>');
	d.writeln('<tr>');
	d.writeln('<td class="total" colspan="4" align="right">Total</td>');
	d.writeln('<td class="total">&nbsp;</td>');
	d.writeln('<td class="total" align="right" colspan="3">\$' + decimalString(total + (total * 0.1)) + '</td>');
	d.write('<td class="subtotal"');
	if (e) d.write(' colspan="2"');
	d.writeln('>&nbsp;</td>');
	d.writeln('</tr>');
	d.writeln('</table>');
}

function Basket()
{
	this.items = new Array();
	this.addItem = basketAddItem;
	this.removeItem = basketRemoveItem;
	this.findItem = basketFindItem;
	this.saveInForm = basketSaveInForm;
	this.printBasket = basketPrintBasket;
	this.printItems = basketPrintItems;
	this.hasOnlyMagazines = basketHasOnlyMagazines;
	this.resellerId = '';
	this.orderedFrom = 1;		// 1 = Web site, 0 = CD-ROM
	this.site = '';
	this.tshirt = false;
}

var theBasket = new Basket();

function decimalString(num)
{
	var n = parseFloat(num);
	n = Math.round(n * Math.pow(10, 2)) / Math.pow(10, 2);
	var s = new String(); s = '' + n;
	if (s.indexOf('.') == -1)
	{
		s = s + '.';
	}
	while (s.charAt(s.length-(3)) != '.')
	{
		s = s + '0';
	}
	for (var i = s.length - 6; i > 0; i -= 3)
	{
		s = s.substring(0, i) + ',' + s.substring(i, s.length);
	}
	return s;
}

//---------------------------------------------------------------------------
// Misc
//---------------------------------------------------------------------------

function findFirstForm(p)
{
	var d;
	if (p == null) d = window.document;
	else d = p;

	if (d.forms.length > 0)
	{
		return d.forms[0];
	}
	if (caps.ns && caps.lyr)
	{
		for (var i = 0; i < d.layers.length; ++i)
		{
			var f = findFirstForm(d.layers[i].document);
			if (f != null)
			{
				return f;
			}
		}
	}
	return null;
}

function trimString(t)
{
	var s = t;
	var i = s.indexOf(" ");
	while (i == 0)
	{
		s = s.substring(1, s.length);
		i = s.indexOf(" ");
	}
	i = s.lastIndexOf(" ");
	while ((s.length > 0) && (i == s.length - 1))
	{
		s = s.substring(0, s.length - 1);
		i = s.lastIndexOf(" ");
	}
	return s;
}

var orderWindow = null;
function openOrderWindow()
{
	//! Verify this closing method
	if (orderWindow != null)
	{
		if (!orderWindow.closed)
		{
			orderWindow.close();
		}
	}
	orderWindow = window.open('', 'orderWindow', 'toolbar=1,location=1,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=780,height=420');
}


function esignPopup(url)
{
	var sealWin=window.open(url,"win",'toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=500,height=450');
}

var popupWindow = null;
function openWindow(url, width, height)
{
	var w = 665 || width;
	var h = 420 || height;

	if (caps.ns)
	{
		h += 30;
	}

	//! Verify this closing method
	if (popupWindow != null)
	{
		if (!popupWindow.closed)
		{
			popupWindow.close();
		}
	}
	popupWindow = window.open(url, 'popupWindow', 'toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=0,resizable=1,width='+w+',height='+h);
}

function loadNav(n)
{
	top.nav.location.replace(n);
}

function loadCnt(c)
{
	top.cnt.location.href = c;
}

function loadBothFrames(n, c)
{
	loadNav(n);
	loadCnt(c);
}

function getRadioValue(r)
{
	for (var i = 0; i < r.length; ++i)
	{
		if (r[i].checked)
		{
			return r[i].value;
		}
	}
	return null;
}

function SearchData(skw, smt, spt, sin)
{
	this.skw = skw;
	this.smt = smt;
	this.spt = spt;
	this.sin = sin;
	return this;
}

var searchData = new SearchData('', 0, 0, 0);

// e = email address, m = show message if true
function validateEmail(e, m)
{
	var msg = 'Please enter a valid email address';
	var invalidChars = '/:,;';			//! TODO: Expand this list
	// Check for invalid characters
	for (var i = 0; i < invalidChars.length; ++i)
	{
		if (e.indexOf(invalidChars.charAt(i),0) >= 0)
		{
			if (m) { alert(msg); }
			return false;
		}
	}
	var at = e.indexOf('@', 0);
	// @ must exist, and must not be the first character
	if (at < 1)
	{
		if (m) { alert(msg); }
		return false;
	}
	++at;
	// There can be only one...
	if (e.indexOf('@', at) != -1)
	{
		if (m) { alert(msg); }
		return false;
	}
	var dot = e.indexOf('.', at);
	// Must have a dot, there must be chars between the at and dot.
	if ( (dot == -1 ) || (dot == at) )
	{
		if (m) { alert(msg); }
		return false;
	}
	// Must have at least two chars after the dot
	if ( (dot + 3) > e.length)
	{
		if (m) { alert(msg); }
		return false;
	}
	return true;
}

function printFrameset(d, nav, cnt)
{
	var cntWidth = 216;
	var scrolling = 'no';
	var cntMargin = 0;
	
	if (!caps.dhtml)
	{
		cntWidth = 232;
		scrolling = 'auto';
		cntMargin = 16;
	}
	d.write('<frameset cols="' + cntWidth + ',*" border="0">');
	d.write('<frame name="nav" src="' + nav + '" frameborder="0"');
	d.write(' marginwidth="0" marginheight="0" noresize="noresize"');
	d.writeln(' scrolling="' + scrolling + '" />');
	d.write('<frame name="cnt" src="' + cnt + '" frameborder="0"');
	d.write(' marginwidth="' + cntMargin + '" marginheight="' + cntMargin + '" noresize="noresize"');
	d.writeln(' scrolling="' + scrolling + '" />');
	d.writeln('</frameset>');
}

function printHomeButton(d, bottom, href, target)
{
	if (caps.dhtml) d.writeln('<div class="align-bottom">');
	d.write('<table border="0" cellpadding="10" cellspacing="0"');
	if (bottom)
	{
		d.write(' height="100%"');
	}
	d.writeln(' width="100%"><tr>');
	d.writeln('<td colspan="2" valign="bottom" align="center"><a href="' + href + '" target="' + target + '"');
	d.writeln(' onmouseover="window.status=\'Go to the home page\'; return true;"');
	d.writeln(' onmouseout="window.status=\'\'; return true;"><img');
	d.writeln(' src="images/homenew.gif" width="100" height="90" border="0" alt="Home" /></a></td>');
	d.writeln('</tr></table>');
	if (caps.dhtml) d.writeln('</div>');
}

var scrollerAnimateCount = 0;
