
function Cookie()
{
	var nIndexStart			= 0;
	var nItemIndex			= 0;

	var szCookieText		= new String();
	var szFirstPart			= new String();
	var szSecondPart		= new String();
	var szThirdPart			= new String();
	var szProductPart		= new String();
	var szSearchStr			= new String();
	
	this.GetCookieText		= GetCookieText;
	this.SetCookieText		= SetCookieText;
	this.GetCookieItemValue	= GetCookieItemValue;
	this.ReplaceCookieItem	= ReplaceCookieItem;
	this.SetCookieItem		= SetCookieItem;
	this.DeleteCookieItem	= DeleteCookieItem;
	this.GetProductInfo		= GetProductInfo;
}

function GetCookieText(szCookieName)
{
	this.szCookieText = '';

	if(!szCookieName)
	{
		return false;
	}

	var szCookies	= document.cookie;
	var nStart		= szCookies.indexOf(szCookieName + '=');

	if(nStart == -1) 
	{
		return false;
	}

	nStart		= szCookies.indexOf('=', nStart) + 1;
	var nEnd	= szCookies.indexOf(';', nStart);

	if(nEnd == -1) 
	{
		nEnd = szCookies.length;
	}

	this.szCookieText = unescape(szCookies.substring(nStart, nEnd));

	return this.szCookieText;
}

function SetCookieText(szCookieName)
{
	if(!szCookieName)
	{
		return;
	}

	var Days	= new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
	var Months	= new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	var EDate	= new Date((new Date()).getTime() + 86500000 * 6);

	var ExpiryDate	= Days[EDate.getDay()] + ', ' + EDate.getDate() + ' ' + Months[EDate.getMonth()] + ' ' + EDate.getUTCFullYear() + ' 00:00:00 GMT';
	//document.cookie = szCookieName + '=' + this.szCookieText + ';expires=' + ExpiryDate + ';path=/;';
	document.cookie = szCookieName + '=' + this.szCookieText + ';path=/;';
}

function GetCookieItemValue(szProdID, szOption)
{
	var nResult = this.GetProductInfo(szProdID, szOption);

	if(nResult != -1 && this.nItemIndex >= 0)		
	{
		this.nItemIndex += this.szSearchStr.length;

		var nItemIndexEnd	= this.szProductPart.indexOf('|^', this.nItemIndex);		
		var nValue			= Number(this.szProductPart.substr(this.nItemIndex, (nItemIndexEnd - this.nItemIndex)));

		return nValue;
	}

	return false;
}

function ReplaceCookieItem(szProdID, szOption, szRepOption, nQuantity)
{
	var nRepQty = 0;

	if(!nQuantity)
	{
		nQuantity = Number(this.GetCookieItemValue(szProdID, szOption));
	}

	if(szOption != szRepOption)
	{
		nRepQty		= Number(this.GetCookieItemValue(szProdID, szRepOption));
		nQuantity	= Number(nQuantity) + nRepQty;
		
		this.DeleteCookieItem(szProdID, szOption);
	}

	this.SetCookieItem(szProdID, szRepOption, nQuantity);
}

function SetCookieItem(szProdID, szOption, nQuantity)
{
	this.szFirstPart	= this.szSecondPart = this.szThirdPart = this.szCookieText = '';
	var nResult			= this.GetProductInfo(szProdID, szOption);

	if(nResult == -1)
	{
		this.szSecondPart	= szProdID + '^';
		this.szThirdPart	= '&' + this.szThirdPart;				
		this.szCookieText	= this.szFirstPart + this.szSecondPart + this.szThirdPart;

		this.SetCookieText('TSH');

		nResult = this.GetProductInfo(szProdID, szOption);
	}

	if(nResult == -1)
	{
		return false;
	}
	
	if(this.nItemIndex >= 0)
	{
		ItemIndexTemp		= (this.nItemIndex + this.szSearchStr.length);
		var nItemIndexEnd	= this.szProductPart.indexOf('|^', ItemIndexTemp);
	
		if(!nQuantity)
		{
			nQuantity = Number(this.GetCookieItemValue(szProdID, szOption));
		}
			
		nQuantity			= (nQuantity > 0) ? nQuantity : 1;
		this.szSecondPart	= this.szSecondPart.substr(0, (this.nIndexStart + ItemIndexTemp))	+ nQuantity +
								this.szSecondPart.substr((this.nIndexStart + nItemIndexEnd), (this.szSecondPart.length - (this.nIndexStart + nItemIndexEnd)));
	}
	else
	{
		this.nIndexStart++;
		nQuantity			= (nQuantity > 0 ) ? nQuantity : 1;
		this.szSecondPart	= this.szSecondPart.substr(0, this.nIndexStart) + szOption + 'QTY:' + nQuantity + '|^' +
								this.szSecondPart.substr(this.nIndexStart, this.szSecondPart.length);
	}

	this.szCookieText = this.szFirstPart + this.szSecondPart + this.szThirdPart;
	this.SetCookieText('TSH');

	return true;
}

function DeleteCookieItem(szProdID, szOption)
{
	var nResult = this.GetProductInfo(szProdID, szOption);

	if(nResult != -1 && this.nItemIndex >= 0)
	{
		this.nIndexStart++;
		var nItemIndexEnd = this.szProductPart.indexOf('|^', this.nItemIndex) + 1;

		this.szSecondPart = this.szSecondPart.substr(0, (this.nIndexStart + this.nItemIndex)) + 
							this.szSecondPart.substr((this.nIndexStart + nItemIndexEnd), 
							(this.szSecondPart.length - (this.nIndexStart + nItemIndexEnd)));

		var Length = szProdID.length + 1;

		if((this.szSecondPart == String(szProdID + '^')) || (this.szSecondPart.length ==  Length))
		{
			this.szSecondPart	= '';
			this.szThirdPart	= this.szThirdPart.substr(1, this.szThirdPart.length);			
		}
		
		this.szCookieText = this.szFirstPart + this.szSecondPart + this.szThirdPart;
		this.SetCookieText('TSH');

		return true;
	}

	return false;
}

function GetProductInfo(szProdID, szOption)
{
	this.szFirstPart = this.szSecondPart = this.szThirdPart = '';
	this.GetCookieText('TSH');
	
	var nProdIndex = this.szCookieText.indexOf(szProdID);

	if(nProdIndex < 0)
	{
		this.szThirdPart = this.szCookieText;
		return -1;
	}

	var IndexAmp		= this.szCookieText.indexOf('&', nProdIndex);
	this.szFirstPart	= this.szCookieText.substr(0, nProdIndex);
	this.szSecondPart	= this.szCookieText.substr(nProdIndex, (IndexAmp - nProdIndex));
	this.szThirdPart	= this.szCookieText.substr(IndexAmp, this.szCookieText.length);
	this.nIndexStart	= this.szSecondPart.indexOf(szProdID);	

	if(this.nIndexStart < 0)
	{
		return -1;
	}

	this.nIndexStart	+= szProdID.length;
	this.szProductPart	= this.szSecondPart.substr(this.nIndexStart, this.szSecondPart.length);	
	this.szSearchStr	= '^' + szOption + 'QTY:';		
	this.nItemIndex		= this.szProductPart.indexOf(this.szSearchStr);

	return true;
}