////////// Global Variables //////////
var idx = 0;
var ProductName = new Array();
var ProductDesc = new Array();
var ProductCost = new Array();
var ProductAmount = new Array();

////////// Set Cookie //////////
// Called locally (do not call in main page)
// Sets a cookie for the
// shopping cart.
////////////////////////////////
function SetCookie(Name,Data)
{
	document.cookie = Name + '=' + Data + ';' + "expires=Monday,04-Apr-2010 05:00:00 GMT";
}

////////// Get Cookie //////////
// Called locally (do not call in main page)
// Gets the cookie for the
// shopping cart.
////////////////////////////////
function GetCookie(Name)
{
	var Index,Start,End,Data;
	Index = document.cookie.indexOf(Name);
	Start = (document.cookie.indexOf('=',Index) + 1);
	End = document.cookie.length;
	Data = document.cookie.substring(Start,End);
	return(Data);
}

////////// Parse Array //////////
// Called locally (do not call in main page)
// Converts the string returned by
// the cookie into an array.
/////////////////////////////////
function ParseArray(Data)
{
	var i = -1,CurrentVar = 0,LastEntry = 0;
	var PBuffer = new Array();
	Data = Data + ';';
	do
	{
		i++;
		if(Data.charAt(i) == ',' || Data.charAt(i) == ';')
		{
			PBuffer[CurrentVar] = Data.substring(LastEntry,i);
			CurrentVar++;
			LastEntry = i + 1;
		}
	}while(Data.charAt(i) != ';')
	return(PBuffer);
}

////////// Add Item //////////
// Adds an item to the shopping
// cart.
// Name - The name of the product.
// Desc - Description of the product.
// Price - Cost per item.
// Amount - Number of products to add.
//////////////////////////////
function AddItem(Name,Desc,Price,Amount)
{
	var i = 0,Flag = false,tr_kol = 0;
	do
	{
		if(ProductName.length == 0)
		{
			ProductName[0] = Name;
			ProductDesc[0] = Desc;
			ProductCost[0] = Price;
			if (ProductAmount[0]>=1) {
				ProductAmount[0] = ProductAmount[0]*1 + Amount*1;
			} else {
				ProductAmount[0] = Amount*1;
			}
			Flag = true;
		}
		else
		{
			if(ProductName[i] == Name)
			{
				if (ProductAmount[i]>=1) {
					ProductAmount[i] = ProductAmount[i]*1 + Amount*1;
				} else {
					ProductAmount[i] = Amount*1;
				}
				Flag = true;
			}
			else
				i++;
			if(i == ProductName.length)
			{
				ProductName[i] = Name;
				ProductDesc[i] = Desc;
				ProductCost[i] = Price;
				if (ProductAmount[i]>=1) {
					ProductAmount[i] = ProductAmount[i]*1 + Amount*1;
				} else {
					ProductAmount[i] = Amount*1;
				}
				Flag = true;
			}
		}
	}while(!Flag);
}

////////// Change Item //////////
// Changes an item amount from the
// shopping cart.
// Name - Name of the product.
// Amount - Number of products to remove.
/////////////////////////////////
function ChangeItem(Name,Amount)
{
	var i = 0,Flag = false;
	do
	{
		if(ProductName.length == 0)
			break;
		if(ProductName[i] == Name)
		{
			if(ProductAmount[i] >= 1)
				ProductAmount[i] = Amount;
			Flag = true;
		}
		else
			i++;
	}while(!Flag);
}

function RemoveItem(idx)
{
	ProductName[idx]="";
	ProductDesc[idx] = "";
	ProductCost[idx] = 0;
	ProductAmount[idx] = 0;
}

function RemoveOneItem(idx)
{
	ProductAmount[idx] = ProductAmount[idx] - 1;
}

function AddOneItem(idx)
{
	ProductAmount[idx] = ProductAmount[idx]*1 + 1;
}

////////// Initialize Cart /////
// Loads cookies for the cart.
////////////////////////////////
function InitCart()
{
	var Buffer;
	Buffer = GetCookie("CartName");
	ProductName = ParseArray(Buffer);
	Buffer = GetCookie("CartDesc");
	ProductDesc = ParseArray(Buffer);
	Buffer = GetCookie("CartCost");
	ProductCost = ParseArray(Buffer);
	Buffer = GetCookie("CartAmount");
	ProductAmount = ParseArray(Buffer);
}

////////// Close Cart //////////
// Saves cookies for the cart.
////////////////////////////////
function CloseCart()
{
	SetCookie("CartName",ProductName);
	SetCookie("CartDesc",ProductDesc);
	SetCookie("CartCost",ProductCost);
	SetCookie("CartAmount",ProductAmount);
}

////////// Remove Cart /////////
// Removes all cart data from cookie.
////////////////////////////////
function RemoveCart()
{
	var i;
	for(i = 0;i < ProductName.length;i++)
	{
		ProductName[i] = "";
		ProductDesc[i] = "";
		ProductCost[i] = 0;
		ProductAmount[i] = 0;
	}
	document.cookie = "CartName" + '=' + ' ' + ';' + "expires=Monday,04-Apr-1910 05:00:00 GMT";
	document.cookie = "CartDesc" + '=' + ' ' + ';' + "expires=Monday,04-Apr-1910 05:00:00 GMT";
	document.cookie = "CartCost" + '=' + ' ' + ';' + "expires=Monday,04-Apr-1910 05:00:00 GMT";
	document.cookie = "CartAmount" + '=' + ' ' + ';' + "expires=Monday,04-Apr-1910 05:00:00 GMT";
}