function nShop(basketDiv, vatrate) {
	this.basketDiv = basketDiv;
	this.basket = new Array();
	this.vatrate = vatrate; // e.g. 15 for 15%, 17.5 for 17.5%
	this.extractCookie();
}

nShop.prototype.basketDiv;
nShop.prototype.basket;
nShop.prototype.vatrate;

nShop.prototype.addToBasket = function(uitmFld, nameFld, descFld, pricFld, quanFld, vatable) {
	if (typeof vatable == "undefined") {
	  vatable = 0;
	}

	var nextSlot = this.basket.length;
	var newitem = true;

	// First check tha the item is not already in the basket
	for (count=0; count<this.basket.length; count++) {
		if (this.basket[count].uitmFld == uitmFld) {
			this.basket[count].quanFld = parseInt(this.basket[count].quanFld,10)+parseInt(quanFld,10);
			newitem = false;
		}
	}

	if (newitem) {
		// 	each item in the basket has UID, Name, Description, Quantity and price
		this.basket[nextSlot] = {uitmFld:0, nameFld:"", descFld:"", quanFld:0, pricFld:4};
		this.basket[nextSlot].uitmFld = uitmFld;
		this.basket[nextSlot].nameFld = nameFld;
		this.basket[nextSlot].descFld = descFld + " (" + uitmFld + ")";
		this.basket[nextSlot].pricFld = pricFld;
		this.basket[nextSlot].quanFld = quanFld;
		this.basket[nextSlot].vatable = vatable;
	}

 	this.showBasket();
}

nShop.prototype.subFromBasket = function (uitmFld, quanFld) {
	document.getElementById("basketformDynamic").innerHTML = "";
	for (count=0; count<this.basket.length; count++) {
		if (this.basket[count].uitmFld == uitmFld) {
			this.basket[count].quanFld = parseInt(this.basket[count].quanFld,10)-parseInt(quanFld,10);

			if (this.basket[count].quanFld < 1) {
				this.basket.splice(count,1);
			}
		}
	}
 	this.showBasket();
}

nShop.prototype.clearBasket = function () {
	if (confirm("This will clear the basket - are you sure?")) {
		this.basket = new Array();
		this.showBasket();
	}
}

nShop.prototype.showBasket = function () {
	var workingDiv = document.getElementById(this.basketDiv);
	workingDiv.innerHTML = "";

	// This is a good opportunity to store the cookie.
	this.createCookie();

	var table = document.createElement("table");
	table.setAttribute("class", "basketTable");
	var row;

	var rowCount = 0;
	var cellCount = 0;
	var total = 0;
	var vat = 0;
	var vatsub = 0;
	var quantity = 0;

	document.getElementById("basketformDynamic").innerHTML = "";

	if (this.basket.length > 0) {
		tr = table.insertRow(rowCount++);
		td = tr.insertCell(cellCount++);
		td.innerHTML = "Item";
		td = tr.insertCell(cellCount++);
		td.innerHTML = "Num";
		td = tr.insertCell(cellCount++);
		td.innerHTML = "Unit";
		td = tr.insertCell(cellCount++);
		td.innerHTML = "SubTot";
		td = tr.insertCell(cellCount++);
		td.innerHTML = "&nbsp;";
	}

	for (count=this.basket.length-1; count>=0; count--) {
		tr = table.insertRow(rowCount++);
		cellCount = 0;
		td = tr.insertCell(cellCount++);
		td.innerHTML = this.basket[count].nameFld;
/*		td = tr.insertCell(cellCount++);
		td.innerHTML = this.basket[count].descFld;*/
		td = tr.insertCell(cellCount++);
		quantity = parseInt(this.basket[count].quanFld,10);
		td.innerHTML = quantity;
		td = tr.insertCell(cellCount++);
		price = parseFloat(this.basket[count].pricFld).toFixed(2);
		td.innerHTML = "&pound;"+price;

		td = tr.insertCell(cellCount++);
		subtotal = price*quantity;
		total += subtotal;
		if (this.basket[count].vatable == 1) {
			vatsub = ((subtotal*100)*(1+(this.vatrate/100))-(subtotal*100));
			vat += (Math.ceil(vatsub)/100);
		}

		td.innerHTML = "&pound;"+subtotal.toFixed(2);

		td = tr.insertCell(cellCount++);
		td.innerHTML = "<a class='nshopSubtract' onclick='nshop.subFromBasket(\""+this.basket[count].uitmFld+"\",1)'>-1</a>";

		// Then add items to form for Google Checkout
		this.addFormItem(count);
		this.createPostageItems();
	}

	if (this.basket.length == 0) {
		document.getElementById("checkoutbutton").style.display = "none";
	} else {
		tr = table.insertRow(rowCount++);
		cellCount = 0;
		td = tr.insertCell(cellCount++);
		td.setAttribute("colspan", "3");
		td.innerHTML = "Total ex VAT:";
		td = tr.insertCell(cellCount++);
		td.innerHTML = "&pound;"+total.toFixed(2);
		td = tr.insertCell(cellCount++);

		tr = table.insertRow(rowCount++);
		cellCount = 0;
		td = tr.insertCell(cellCount++);
		td.setAttribute("colspan", "3");
		td.innerHTML = "VAT:";
		td = tr.insertCell(cellCount++);
		td.innerHTML = "&pound;"+vat.toFixed(2);
		td = tr.insertCell(cellCount++);

		tr = table.insertRow(rowCount++);
		cellCount = 0;
		td = tr.insertCell(cellCount++);
		td.setAttribute("colspan", "3");
		td.innerHTML = "Total inc VAT:";
		td = tr.insertCell(cellCount++);
		var totalincvat = total+vat;
		td.innerHTML = "&pound;"+totalincvat.toFixed(2);
		td = tr.insertCell(cellCount++);

		document.getElementById("checkoutbutton").style.display = "block";
	}

	this.addFormVATItem(vat);

//	document.getElementById("basketformDynamic").appendChild(this.createGoogleItemForm("analyticsdata", ""));
//        document.getElementById("basketformDynamic").appendChild(this.createGoogleItemForm("analytics-data", ""));

	workingDiv.appendChild(table);

}

nShop.prototype.createPostageItems = function() {

	var numberOfItems = 0;
	var priceArray = new Array();
	
	for (acount=this.basket.length-1; acount>=0; acount--) {
		numberOfItems += parseInt(this.basket[acount].quanFld,10);
	}

	acount = 0;
	if (numberOfItems < 2) {
		priceArray[acount++] = 4.00;
		priceArray[acount++] = 5.00;
		priceArray[acount++] = 10.00;
	} else {
		priceArray[acount++] = 6.00;
		priceArray[acount++] = 10.00;
		priceArray[acount++] = 25.00;
	}
	acount = 0;

	formitem = this.createGoogleItemForm("ship_method_name_1", "UK");
	document.getElementById("basketformDynamic").appendChild(formitem);
	formitem = this.createGoogleItemForm("ship_method_price_1", priceArray[acount++]);
	document.getElementById("basketformDynamic").appendChild(formitem);
	formitem = this.createGoogleItemForm("ship_method_currency_1", "GBP");
	document.getElementById("basketformDynamic").appendChild(formitem);
	formitem = this.createGoogleItemForm("ship_method_country_1", "UK");
	document.getElementById("basketformDynamic").appendChild(formitem);

	formitem = this.createGoogleItemForm("ship_method_name_2", "Ireland");
	document.getElementById("basketformDynamic").appendChild(formitem);
	formitem = this.createGoogleItemForm("ship_method_price_2", priceArray[acount++]);
	document.getElementById("basketformDynamic").appendChild(formitem);
	formitem = this.createGoogleItemForm("ship_method_currency_2", "GBP");
	document.getElementById("basketformDynamic").appendChild(formitem);
	formitem = this.createGoogleItemForm("ship_method_country_2", "IE");
	document.getElementById("basketformDynamic").appendChild(formitem);

	formitem = this.createGoogleItemForm("ship_method_name_3", "Rest Of World");
	document.getElementById("basketformDynamic").appendChild(formitem);
	formitem = this.createGoogleItemForm("ship_method_price_3", priceArray[acount++]);
	document.getElementById("basketformDynamic").appendChild(formitem);
	formitem = this.createGoogleItemForm("ship_method_currency_3", "GBP");
	document.getElementById("basketformDynamic").appendChild(formitem);

//        formitem = this.createGoogleItemForm("checkout-flow-support.merchant-checkout-flow-support.continue-shopping-url", "http://www.lipotrimprestwich.co.uk/order-thank-you.html");
        document.getElementById("basketformDynamic").appendChild(formitem);

	formitem = this.createGoogleItemForm("checkout-flow-support.merchant-checkout-flow-support.shipping-methods.flat-rate-shipping-3.shipping-restrictions.excluded-areas.postal-area-1.country-code", "UK");
	document.getElementById("basketformDynamic").appendChild(formitem);
	formitem = this.createGoogleItemForm("checkout-flow-support.merchant-checkout-flow-support.shipping-methods.flat-rate-shipping-3.shipping-restrictions.excluded-areas.postal-area-2.country-code", "IE");
	document.getElementById("basketformDynamic").appendChild(formitem);
}

nShop.prototype.addFormItem = function (arrayId) {
	// Then add items to form for Google Checkout
	formitem = this.createGoogleItemForm("item_name_"+(parseInt(arrayId,10)+1), this.basket[arrayId].nameFld);
	document.getElementById("basketformDynamic").appendChild(formitem);
	formitem = this.createGoogleItemForm("item_description_"+(parseInt(arrayId,10)+1), this.basket[arrayId].descFld);
	document.getElementById("basketformDynamic").appendChild(formitem);
	formitem = this.createGoogleItemForm("item_price_"+(parseInt(arrayId,10)+1), this.basket[arrayId].pricFld);
	document.getElementById("basketformDynamic").appendChild(formitem);
	formitem = this.createGoogleItemForm("item_quantity_"+(parseInt(arrayId,10)+1), this.basket[arrayId].quanFld);
	document.getElementById("basketformDynamic").appendChild(formitem);
	formitem = this.createGoogleItemForm("item_currency_"+(parseInt(arrayId,10)+1), "GBP");
	document.getElementById("basketformDynamic").appendChild(formitem);
}

nShop.prototype.addFormVATItem = function (vat) {
	// Then add items to form for Google Checkout
	formitem = this.createGoogleItemForm("item_name_"+(this.basket.length+1), "VAT");
	document.getElementById("basketformDynamic").appendChild(formitem);
	formitem = this.createGoogleItemForm("item_description_"+(this.basket.length+1), "UK VAT at "+this.vatrate+"%");
	document.getElementById("basketformDynamic").appendChild(formitem);
	formitem = this.createGoogleItemForm("item_price_"+(this.basket.length+1), vat);
	document.getElementById("basketformDynamic").appendChild(formitem);
	formitem = this.createGoogleItemForm("item_quantity_"+(this.basket.length+1), 1);
	document.getElementById("basketformDynamic").appendChild(formitem);
	formitem = this.createGoogleItemForm("item_currency_"+(this.basket.length+1), "GBP");
	document.getElementById("basketformDynamic").appendChild(formitem);
}

nShop.prototype.createGoogleItemForm = function (Name, Value) {
	var formitem = document.createElement("input");
	formitem.setAttribute("type", "hidden");
	formitem.setAttribute("name", Name);
	formitem.setAttribute("value", Value);
	return formitem;
}

nShop.prototype.createCookie = function () {
	if (navigator.cookieEnabled) {
		var cookieString = "nShop_GreaseJunkie=";
		var arrayver = new Array();
		var count = 0;
		for (i=0; i<this.basket.length; i++) {
			for (var label in this.basket[0]) {
				cookieString += escape(label+","+i)+"="+escape(this.basket[i][label])+"|";
			}
		}
  		document.cookie = cookieString+"; path=/";//expires="+cookie_expire_date;
	} else {
		alert("Cookies are not enabled, your basket will be lost if you navigate away from this page.");
	}
}

nShop.prototype.extractCookie = function () {
	nameOfCookie = "nShop_GreaseJunkie";
	this.basket = new Array();
	if (document.cookie.length > 0) {
		startC = document.cookie.indexOf(nameOfCookie+"=");
		if (startC != -1) {
			startC += nameOfCookie.length+1;
			endC = document.cookie.indexOf(";", startC);
			if (endC == -1) {
				endC = document.cookie.length;
			}
			cookieContent = unescape(document.cookie.substring(startC, endC));
			var allvars = new Array();
			allvars = cookieContent.split("|");
			var arrayVersion = new Array();
			var varDetails = new Array();
			var idArray = new Array();
			for (count=0; count<allvars.length; count++) {
				varDetails = allvars[count].split("=");
				varDetails[0] = unescape(varDetails[0]);
				varDetails[1] = unescape(varDetails[1]);
				idArray = varDetails[0].split(",");
				if(typeof this.basket[idArray[1]] == "undefined") {
					this.basket[idArray[1]] = {uitmFld:0, nameFld:"", descFld:"", quanFld:0, pricFld:4, vatable:0};
				}
				this.basket[idArray[1]][idArray[0]] = varDetails[1];
			}
			this.showBasket();
		}
	} else {
// 		alert("Cookie not set!");
	}

}
