function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return '';
}

function SetCookie (name, value, exp) {
	var expires = exp || null;
	var cookie = name + '=' + escape (value)+';';
	if(expires != null)
		cookie += 'expires='+expires.toUTCString();
	cookie += '; path = /;'; // domain=.'+document.domain+';
	document.cookie = cookie;
}

/*------------------------------------------------------------*/

function ShoppingCart(cookiename, cookielifetime, cookieUnset){
	this.cookieName = cookiename || "cart";
	this.cookieUnsetName = cookieUnset || "empty_cart";
	this.cookieExpTime = new Date();
	this.cookiePath = '/';
	this.cookieSeparator1 = "|";
	this.cookieSeparator2 = ":";
	this.cartContents = "";
	today = new Date();
	this.cookieExpTime.setTime(today.getTime() + (cookielifetime || 30) * (24 * 3600 * 1000));
	delete today;

/*---- Эти методы должны быть перегружены !!! ----------------*/
	this.OnEmpty = function(){};
	this.OnFull = function(){};
	this.GetPrice = function(id){return 0;};
	this.GetQuant = function(id){return 1;};
/*------------------------------------------------------------*/
//	function ShoppingCart.test()
	this.test = function(){
		if(this.cartContents.length == 0)
			this.OnEmpty();
		else
			this.OnFull();
	}

//	function ShoppingCart.SetCartContents() <-- х.з. как правильно работает setter
	this.SetCartContents = function(val){
		this.cartContents = val;
		this.test();
	}

	if(GetCookie(this.cookieUnsetName) == "1"){
		this.SetCartContents("");
		SetCookie(this.cookieName, "", this.cookieExpTime);
		SetCookie(this.cookieUnsetName, "");
	}else{
		this.SetCartContents(GetCookie(this.cookieName));
	}

//	function ShoppingCart.GetCurrentQuant()
	this.GetCurrentQuant = function(id){
		id += "";	//BUGFIX: id должен быть строкой!
		var a = this.cartContents.indexOf(this.cookieSeparator1 + id + this.cookieSeparator2);
		if(a >= 0){
			a += this.cookieSeparator1.length + id.length + this.cookieSeparator2.length;
			var b = this.cartContents.indexOf(this.cookieSeparator1, a);
			if(b >= 0)
				var t = this.cartContents.substring(a, b);
			else
				var t = this.cartContents.substr(a);
			return t;
		}
		return 0;
	}

//	function ShoppingCart.GetAmount()
	this.GetAmount = function(id){
		return this.GetPrice(id) * this.GetCurrentQuant(id);
	}

//	function ShoppingCart.GetTotalPrice()
	this.GetTotalPrice = function(){
		var i=0;
		var j=0;
		var id = '';
		var total = 0;

		while(i >= 0){
			i = this.cartContents.indexOf(this.cookieSeparator1, i);
			if(i >= 0){
				j = this.cartContents.indexOf(this.cookieSeparator2, i);
				i += this.cookieSeparator1.length;
				id = this.cartContents.substring(i, j);
				total += this.GetAmount(id);
			}
		}
		return total;
	}

//	function ShoppingCart.GetItemsCount()
	this.GetItemsCount = function(){
		var i=0;
		var id = '';
		var cnt = 0;
		while(i >= 0){
			i = this.cartContents.indexOf(this.cookieSeparator1, i);
			if(i >= 0){
				j = this.cartContents.indexOf(this.cookieSeparator2, i);
				i += this.cookieSeparator1.length;
				id = this.cartContents.substring(i, j);
				cnt += parseInt(this.GetCurrentQuant(id));
			}
		}
		return cnt;
	}

//	function ShoppingCart.Add()
	this.Add = function(id){
		var count = this.GetQuant(id);//this.GetCurrentQuant(id) +
		this.Remove(id);
		if(count <= 0) return;
		this.SetCartContents(this.cartContents + this.cookieSeparator1 + id + this.cookieSeparator2 + count);
		SetCookie(this.cookieName, this.cartContents, this.cookieExpTime, this.cookiePath);
		return true;
	}

//	function ShoppingCart.Remove()
	this.Remove = function(id){
		var a = this.cartContents.indexOf(this.cookieSeparator1 + id + this.cookieSeparator2);
		if(a == -1) return;

		var b = this.cartContents.indexOf(this.cookieSeparator1, a+1);

		var before = this.cartContents.substr(0, a);
		if(b >= 0)
			var after = this.cartContents.substr(b);
		else
			var after = '';

		this.SetCartContents(before + ((b >= 0) ? after : ''));
		SetCookie(this.cookieName, this.cartContents, this.cookieExpTime, this.cookiePath);
	}

//	function ShoppingCart.Revert()
	this.Revert = function(id){		// а надо ли???
		if(this.GetCurrentQuant(id) > 0)
			this.Remove(id);
		else
			this.Add(id);
	}
}	//ShoppingCart

