
/* Shopping Cart */

/*
function isNumeric(val) {

    if (isNaN(parseFloat(val))) {
          return false;
     }
     return true;
}
*/
function isNumeric(expression) {

	var nums = "0123456789";

	if (expression.length==0)return(false);

	for (var n=0; n < expression.length; n++){

		if(nums.indexOf(expression.charAt(n))==-1)return(false);
	}
	return(true);
}


function cItem (id, name, price) {
	this.id = id;
	this.name = name;
	this.price = price;
}

function cCart () {
	

	this.jar = new CookieJar({
		expires:36000, // seconds
		path: '/'
		});

	this.goods = new Array();
	this.inCart = new Object();
	
	
	this.registerItem = function (id, name, price) {
	
		this.goods[id] = new cItem (id, name, price);
		this.inCart[id] = 0;
	}
	
	
	this.add = function (id) {
	
		var item = this.goods[id];
		
		if (item) {			
			this.inCart[id] = this.inCart[id] + 1;			
			this.save();			
			this.write();
			}
			
	
		var answer;

		answer = window.confirm("В корзину добавлен " + item.name + "\n" + "Перейти к оформлению заказа?")
		if (answer)	{
			window.location = "/Cart";
			}

	}
	
	this.remove = function (id) {
	
		var item = this.goods[id];		
		if (item) {
			this.inCart[id] = 0;
			this.save();			
			this.write();			
			}
	}
	
	this.recount = function () {

		for (var n in this.inCart) {
		
			if (this.inCart[n] > 0) {
				var id = "quantity_" + n;
				
				var elem = document.getElementById (id);
				if (elem != null) {
					var val = elem.value;
					if (isNumeric(val)) {
					
						if (val < 1) {
							this.inCart[n] = 0;
							}
						else {
							this.inCart[n] = val;
							}
					}
					else {
						
						this.inCart[n] = 1;
						
					}
				}
			}
		}
		this.save();	
					
		this.write_cart();
	}
	
	this.set = function (id, quantity) {
	
		var item = this.goods[id];
		
		if (item) {
			if (isNumeric (quantity)) {
				this.inCart[id] = quantity;
				
				}
			}
	}
	
	this.load = function () {
		
		var data = this.jar.get ('cart');
		
		if (data != null) {
			for (var n in this.inCart) {
				this.inCart[n] = data[n];
			}
		}		
	}
	
	this.save = function () {	
		this.jar.put ('cart', this.inCart);			
	}
	
	this.write = function () {
	
		var priceId = "cart_price";
		var goodsId = "cart_goods";
		var hiddenId = "cart_hidden";
		
		var elem = document.getElementById (goodsId);
		
		if (elem != null) {
			
			var str = "";
			var inner = "";
			var total = 0;
						
			for (var n in this.inCart) {

				
				var quantity = this.inCart[n];
				var product = this.goods[n];
				
				if (quantity > 0) {
				
					str = "<li>";					
					str = str + "<i>" + product.name + "</i>";
					str = str + " (x" + quantity + ") ";
					str = str + "<input type='submit' class='remove' onclick='cart.remove(" + product.id + "); return false;' value='X' />"
					str = str + "<br style='clear:both;' /><div>" + product.price * quantity + " руб.</div>";	

					str = str + "</li>";
				
					inner = inner + str;
					total = total + product.price * quantity;
				}
			}
			
			if (inner == "") {									
				elem.innerHTML = "Корзина пуста";						
				}
			else {
				elem.innerHTML = "<ol>" + inner + "</ol>";
				}
			
			var priceElem = document.getElementById (priceId);	
			
			if (priceElem != null) {
				
				var buttonElem = document.getElementById ("submit_cart");
				
				if (total > 0) {
					priceElem.innerHTML = total + " руб.";
					buttonElem.style.display = "block";
					}
				else {
					priceElem.innerHTML = "";
					buttonElem.style.display = "none";
					}
				
			}
			
			var hiddenElem = document.getElementById (hiddenId);
			if (hiddenElem != null) {				
				hiddenElem.value = escape (Object.toJSON (this.inCart));
			}
		}
	
	}
	
	
	
	
	this.write_cart = function () {
			
				
				var priceId = "cart_price";
				var goodsId = "cart_goods";
				var hiddenId = "cart_hidden";
		
				var elem = document.getElementById ("cart_table");
				var elemPrice = document.getElementById ("total_price");
				
				var header = "<tr class='header'><td>&nbsp;</td><td>Модель</td><td>Кол-во</td><td>Стоимость</td><td>&nbsp;</td></tr>";
								
				if (elem != null) {
			
					var str = "";
					var inner = "";
					var total = 0;
					var count = 0;
					for (var n in this.inCart) {

				
						var quantity = this.inCart[n];
						var product = this.goods[n];
				
						if (quantity > 0) {
							
							
							str = "<tr>";
							
							count++;
							str = str + "<td width='10px'>" + count + "</td>";				
							str = str + "<td width='200px'>" + product.name + "</td>";
							str = str + "<td width='50px'>" + '<input id="quantity_'+product.id+'" name="quantity_'+product.id+'" value="' + quantity + '" style="width:30px;" />' + "</td>";							
							str = str + "<td width='100px'>" + product.price * quantity + " руб.</td>";	
							str = str + "<td width='30px'>" + '<input type="submit" name="remove_'+product.id+'" onclick="cart.remove(' + product.id + '); return false;" value="X" style="width: 50px" />' + "</td>";
							str = str + "</tr>";
				
							inner = inner + str;
							total = total + product.price * quantity;
						}						
					}
			
					elem.innerHTML = "<table cellspacing='0' cellpadding='0' border='0'>" + header + inner + "</table>";
					elemPrice.innerHTML = "Итого: <b>" + total + " руб.</b>";
					
				}
			}

}

