// JavaScript support routines
// © 4Mation 2002, 2003, 2005, 2007

var domainPath = "; path=/; domain=.4mation.co.uk;";
var vatRate = 0.15;

if (document.domain.indexOf(".development.") >= 0)
{
	domainPath = "; path=/; domain=.development.4mation.co.uk;";
//    alert("Using test mode" + domainPath);
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g, "");
}

function BasketItem(itemDesc, itemCost, itemVat, sku)
{
	this.desc = itemDesc;
	this.cost = itemCost;
	if (itemVat == "0")
		this.vat = 0;
	else
		this.vat = vatRate;
	this.sku = sku;
}

BasketItem.prototype.toString = function()
{
	return "{" + this.desc + ", £" + this.cost + ", " + (this.vat * 100) + "% }";
}

function toFixed(val, decimals)
{
	var cst = "" + val;

	if (isNaN(val))
		return cst;

	var start = 0;

	if (Math.floor(val) == val)
		cst += ".";
	else
	{
		var i = Math.pow(10, decimals)
		var v = Math.floor((val * i) + 0.5) / i;
		cst = "" + v;
		start = String(cst).lastIndexOf(".")

		if (start < 0)
		{
			start = 0;
			cst += ".";
		}
		else
			start += 1;
		start = cst.length - start;
	}

	for( var i = start; i < decimals; i++)
		cst += "0";
	return cst;
}

function GetCookie(sName)
{
	var aCookie = document.cookie.split(";");
	for (var i=0; i < aCookie.length; i++)
	{
		var aCrumb = aCookie[i].split("=");
		if (sName == trim(aCrumb[0]))
		{
			if (aCrumb[1])
				return unescape(aCrumb[1]);
			return null;
		}
	}
	return null;
}

function SetCookie(sName, sValue)
{
	document.cookie = sName + "=" + escape(sValue) + domainPath;
}

function emptyBasket()
{
	SetCookie("basket", "")
	itemsInBasket = new Array()
	setupBasket()
}

function setupBasket()
{
	var basket = GetCookie("basket");
	if (basket && basket != "")
	{
		var aCrumb = basket.split("¬");
		for (var i=0; i < aCrumb.length; i++)
		{
			var item = aCrumb[i].split("|")
			var sku = '';
			if (item.length > 4) {
				sku = item[4];
			}
			itemsInBasket[i] = new BasketItem(item[0], item[1], item[2], sku);
		}
	}
}

function removeItem(i)
{
	if (i > itemsInBasket.length)
		return;

	var basket = new Array()

	for (var x = 0; x < i; x++)
		basket[x] = itemsInBasket[x]

	if(i < itemsInBasket.length)
		for (x = i+1; x < itemsInBasket.length; x++)
		{
			basket[x-1] = itemsInBasket[x]
		}

	itemsInBasket = basket

	var basket = "";
	if (itemsInBasket.length > 0) {
		basket = itemsInBasket[0].desc + "|" + itemsInBasket[0].cost + "|" + itemsInBasket[0].vat + "|1|" + itemsInBasket[0].sku;
		for (x = 1; x < itemsInBasket.length; x++)
			basket += "¬" + itemsInBasket[item].desc + "|" + itemsInBasket[item].cost + "|" + itemsInBasket[item].vat + "|1|" + itemsInBasket[item].sku;
	}
	SetCookie("basket", basket)

	bt.innerHTML = formatbasket();
	box.msg.value = formatbasket2();
}

var messageWindow;

function popupmsg(msg)
{
	var t = 90;
	if (window.screenTop) t = t + window.screenTop;
	var l = window.screenLeft + document.body.clientWidth / 2 - 140;
	if (l < 0) l = 40;
	var attr = 'top=' + t + ',left=' + l + ',height=120,width=280';

	messageWindow=window.open('','Message',attr);

	var tmp = messageWindow.document;
	tmp.writeln('<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml11/dtd/xhtml1-transitional.dtd">');
	tmp.writeln('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">');
	tmp.write('<html><head><title>Basket</title>');
	tmp.write('<link rel="stylesheet" href="/styles.css">');
	tmp.write('</head><body onblur="javascript:self.close()"><p>');
	tmp.write(msg);
	tmp.write('</p>');
	tmp.write('<form align="center"><input type="button" align="center" onclick="javascript:self.close()" value="Close" /></form>');
	tmp.write('</body></html>');
	tmp.close();
	if (window.focus) messageWindow.focus();
}

function addtobasket(item)
{
	var basket = GetCookie("basket");
	if (basket && basket != "")
	{
		var a=true;
		var aCrumb = basket.split("¬");
		if (aCrumb.length >= 6)
		{
			alert("Your shopping basket is full. You can have a maximum of 6 items in your basket.");
			return;
		}
		for (var i=0; i < aCrumb.length && a; i++)
		{
			var desc = aCrumb[i].split("|");
			if (products[item].title == desc[0])
				a = false;
		}
		if (a)
			basket += "¬" + products[item].title + "|" + products[item].cost + "|" + products[item].vat + "|1|" + products[item].sku;
	}
	else
		basket = products[item].title + "|" + products[item].cost + "|" + products[item].vat + "|1|" + products[item].sku;

	SetCookie("basket", basket)
	var chk = GetCookie("basket");
	if (chk != basket)
	{
		//alert("document.domain = " + document.domain);
	//	alert(chk + " != " + basket);
		alert("There was a problem using 'cookies'. You need to allow the 4Mation site to use cookies for online ordering to work. This can usually be done by changing your privacy settings.");
	}
	else
		popupmsg("Added <em>" + products[item].title + "</em> to your basket.")
}

function showbask()
{
	var basket = GetCookie("basket");
	var basketF = document.getElementById("basketForm");
	basketF.items.value = basket;
	basketF.submit();
}

function getEmail()
{
	var name = "";

	if (navigator.userProfile)
		name = navigator.userProfile.getAttribute("vCard.Email");

	return name;
}

function getName()
{
	var name = "";

	if (navigator.userProfile)
		name = navigator.userProfile.getAttribute("vCard.DisplayName");

	return name;
}

function getAddress()
{
	var name = "";

	if (navigator.userProfile)
		name = navigator.userProfile.getAttribute("vCard.Business.StreetAddress");

	return name;
}
