<!--
/* Copyright Muchachamaca SCP, Spain - All rights reserved */

//------------------------------------------------------------------

function Product (inProdId, inCategory, inName, inPrice, inShippingBase, inShippingXtra, inColor, inImageDir, inShortDescription, inLongDescription, inDealerShippingBase, inDealerShippingXtra, inDealerDiscountApplies)
{
	this.mProdId = inProdId;
	this.mCategory = inCategory;
	this.mName = inName;
	this.mPrice = inPrice;
	this.mShippingBase = inShippingBase;
	this.mShippingXtra = inShippingXtra;
	this.mColor = inColor;
	this.mImageDir = inImageDir;
	this.mShortDescription = inShortDescription;
	this.mLongDescription = inLongDescription;
	this.mQuantity = 0;
	this.mDealerShippingBase = inDealerShippingBase;
	this.mDealerShippingXtra = inDealerShippingXtra;
	this.mDealerDiscountApplies = inDealerDiscountApplies;

	this.mId = 0;
	this.mText1 = "";
	this.mText2 = "";
	this.mVisibleAshes = false;
}

//------------------------------------------------------------------

function Shop (inName)
{
	this.mItems = new Array();
	this.RegisterProduct = shop_RegisterProduct;
	this.GetProductInfo = shop_GetProductInfo;
	this.ObtainProduct = shop_ObtainProduct;
}


function shop_RegisterProduct (inProdId, inCategory, inName, inPrice, inShippingBase, inShippingXtra, inColor, inImageDir, inShortDescription, inLongDescription, inDealerShippingBase, inDealerShippingXtra, inDealerDiscountApplies)
{
	this.mItems[inProdId] = new Product(inProdId, inCategory, inName, inPrice, inShippingBase, inShippingXtra, inColor, inImageDir, inShortDescription, inLongDescription, inDealerShippingBase, inDealerShippingXtra, inDealerDiscountApplies);
}

function shop_ObtainProduct (inProdId)
{
	var theProto = this.mItems[inProdId];
	if (theProto == null)
	{
		alert ("Product not found: " + inProdId);
		return null;
	}
	var theProduct = new Product(theProto.mProdId, theProto.mCategory, theProto.mName, theProto.mPrice, theProto.mShippingBase, theProto.mShippingXtra, theProto.mColor, theProto.mImageDir, theProto.mShortDescription, theProto.mLongDescription, theProto.mDealerShippingBase, theProto.mDealerShippingXtra, theProto.mDealerDiscountApplies);
	return theProduct;
}

function shop_GetProductInfo (inProdId)
{
	return this.mItems[inProdId];
}

//------------------------------------------------------------------

function Cart (inName)
{
	this.mName = inName;
	this.mIdCounter = 1;
	this.mItems = new Array();
	this.getIndexFromId = cart_GetIndexFromId;
	this.getTotal = cart_GetTotal;
	this.add = cart_Add;
	this.addItem = cart_AddItem;
	this.remove = cart_Remove;
	this.increase = cart_Increase;
	this.decrease = cart_Decrease;
	this.setCount = cart_SetCount;
	this.write = cart_Write;
	this.read = cart_Read;
	this.empty = cart_Empty;
	this.findSimilarItem = cart_FindSimilarItem;
}


function cart_GetIndexFromId (inId)
{
	for (i = 0; i < this.mItems.length; i++)
		if (this.mItems[i].mId == inId)
			return i;
			
	return -1;
}


function cart_FindSimilarItem (inItem)
{
	for (i = 0; i < this.mItems.length; i++)
	{
		if (this.mItems[i].mProdId != inItem.mProdId) continue;
		if (this.mItems[i].mText1 != inItem.mText1) continue;
		if (this.mItems[i].mText2 != inItem.mText2) continue;
		if (this.mItems[i].mVisibleAshes != inItem.mVisibleAshes) continue;
		return this.mItems[i];
	}
			
	return null;
}


function cart_GetTotal()
{
	var theTotal = 0;
	
	for (i = 0; i < this.mItems.length; i++)
		theTotal += this.mItems[i].mQuantity * this.mItems[i].mPrice;
		
	return theTotal;
}


function cart_AddItem (inNewItem, inMergeSimilar)
{
	if (inMergeSimilar)
	{
		var theItem = this.findSimilarItem (inNewItem);
		if (theItem)
		{
			theItem.mQuantity += inNewItem.mQuantity;
			this.write();
			return theItem.mId;
		}
	}

	inNewItem.mId = this.mIdCounter++;
	this.mItems.push (inNewItem);
	this.write();
	return inNewItem.mId;
}


function cart_Add (inProdId, inCount, inMergeSimilar)
{
	var theNewItem = theShop.ObtainProduct (inProdId);
	theNewItem.mQuantity = parseInt(inCount);
	
	return this.addItem (theNewItem, inMergeSimilar);
}


function cart_Remove (inId)
{
	var theIndex = this.getIndexFromId (inId);
	this.mItems.splice (theIndex, 1);
	this.write();
}


function cart_Increase (inId, inCount)
{
	var theIndex = this.getIndexFromId (inId);
	this.mItems[theIndex] += inCount;
	this.write();
}


function cart_Decrease (inId, inCount)
{
	var theIndex = this.getIndexFromId (inId);
	this.mItems[theIndex] -= parseInt(inCount);
	if (this.mItems[theIndex] < 0)
		this.mItems[theIndex] = 0;
	this.write();
}


function cart_SetCount (inId, inCount)
{
	var theIndex = this.getIndexFromId (inId);
	this.mItems[theIndex].mQuantity = parseInt(inCount);
	this.write();
}


function cart_Write()
{
	var theValue = ""; // Math.random() + ";";	// uniquify the cookie so it can be used to determine the ref-code
	
	for (i = 0; i < this.mItems.length && theValue.length < 4000; i++)
	{
		theItem = "";
		theItem += this.mItems[i].mProdId + ",";
		theItem += this.mItems[i].mQuantity + ",";
		theItem += "\"" + escape(this.mItems[i].mText1) + "\"" + ",";
		theItem += "\"" + escape(this.mItems[i].mText2) + "\"" + ",";
		theItem += this.mItems[i].mVisibleAshes + ";";

		if (theValue.length + theItem.length < 4000)
			theValue += theItem;
		else
			alert ("Warning: too many items in the cart - some may have fallen off.");
	}
	
	setCookie (this.mName, theValue, 5);
}


function cart_Read (inShop)
{
	var theValue = getCookie (this.mName);
	
	if (theValue == null)
		return;

	var theItemList = theValue.split (';')
	
	for (var i = 0; i < theItemList.length - 1; i++)
	{
		var thePropertyList = theItemList[i].split (',');
		var theNewItem = inShop.ObtainProduct(thePropertyList[0]);

		if (theNewItem != null && theNewItem != 'undefined')
		{
			theNewItem.mQuantity = parseInt(thePropertyList[1]);
			theNewItem.mText1 = unescape(thePropertyList[2].slice(1,thePropertyList[2].length-1));
			theNewItem.mText2 = unescape(thePropertyList[3].slice(1,thePropertyList[3].length-1));
			theNewItem.mVisibleAshes = thePropertyList[4] == "true";
					
			theNewItem.mId = this.mIdCounter++;
			this.mItems.push (theNewItem);
		}
	}
}


function cart_Empty()
{
	this.mItems.splice(0,this.mItems.length);
	this.write();
}

//------------------------------------------------------------------


function setCookie(c_name,value,expiredays)
{
	var exdate = new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie = c_name+ "=" + escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
} 


function getCookie(c_name)
{
	if (document.cookie.length > 0)
	{
		c_start = document.cookie.indexOf (c_name + "=")
		if (c_start!=-1)
		{
			c_start=c_start + c_name.length+1
			c_end = document.cookie.indexOf(";",c_start)
			if (c_end==-1) c_end = document.cookie.length
			return unescape(document.cookie.substring(c_start,c_end))
		} 
	}
	return null
}

//-->
