

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

function getCookie(name) {
	if (document.cookie.length > 0) {
		var start = document.cookie.indexOf(name + "=");
		if (start != -1) {
			start = start + name.length + 1;
			var end = document.cookie.indexOf(";", start);
			if (end == -1)
				end = document.cookie.length;
			return unescape(document.cookie.substring(start, end));
		}
	}
	return "";
}


function Item() {
	this.title = null;
	this.quantity = null;
	this.price = null;
}

function Cart(linesId, cartId) {

	this.items = new Array();
	this.linesId = linesId;
	this.cartId = cartId;


	this.render = function() {
		var html = '';
		var total = 0.0;
		var quantity = 0.0;

		for (var i = 0; i < this.items.length; i++) {
			var item = this.items[i];
			var t = item.quantity * item.price;
			total += t;
			t = Math.round(t * 100);
			t = t / 100;
			quantity += item.quantity;
			html += '<tr>';
			html += '<td>' + item.title + '</td>';
			html += '<td><input id="items.' + i + '.quantity" class="text" type="text" value="' + item.quantity + '" onChange="cart.onQuantityChange(' + i + ');"></input></td>';
			html += '<td>' + item.price + '</td>';
			html += '<td>' + t + '</td>';
			html += '<td><input class="button" type="button" value="DELETE" onClick="cart.deleteItem(' + i + '); return false;"></input></td>';
			html += '</tr>';
		}

		total = Math.round(total * 100);
		total = total / 100;

		var element = document.getElementById(this.linesId);
		if (element != null)
			element.innerHTML = html;

		element = document.getElementById('total');
		if (element != null)
			element.innerHTML = total;
		element = document.getElementById('total-quantity');
		if (element != null)
			element.innerHTML = quantity;
	}

	this.addItem = function(title, quantity, price) {
		for (var i = 0; i < this.items.length; i++) {
			if (this.items[i].title == title) {
				this.items[i].quantity += quantity;
				return;
			}
		}
		var item = new Item();
		item.title = title;
		item.quantity = quantity;
		item.price = price;
		this.items[this.items.length] = item;
	}

	this.deleteItem = function(index) {
		this.items.splice(index, 1);
		this.save();
		this.render();
		this.refreshLink();
	}

	this.add = function(item) {
		this.addItem(item.title, 1, item.price);
	}

	this.expirationInDays = 10;
	this.MAX_ITEMS = 128;
	this.MAX_PRICE = 1024;
	this.MAX_QUANTITY = 128;

	this.save = function() {
		for (var i = 0; i < this.items.length; i++) {
			setCookie('items.' + i + '.title', this.items[i].title, this.expirationInDays);
			setCookie('items.' + i + '.price', this.items[i].price, this.expirationInDays);
			setCookie('items.' + i + '.quantity', this.items[i].quantity, this.expirationInDays);
		}
		setCookie('items.length', this.items.length);
	}

	this.getTotal = function() {
		var total = 0;
		for (var i = 0; i < this.items.length; i++)
			total += this.items[i].price * this.items[i].quantity;
		total = Math.round(total * 100);
		total = total/100;
		return total;
	}

	this.getQuantity = function() {
		var result = 0;
		for (var i = 0; i < this.items.length; i++)
			result += this.items[i].quantity;
		return result;
	}

	this.load = function() {
		this.items = new Array();
		var length = getCookie('items.length');
		if ((length == null) || (length == '') || ((new Number(length)) <= 0) || ((new Number(length)) >= this.MAX_ITEMS))
			return;
		for (var i = 0; i < (new Number(length)); i++) {
			var title = getCookie('items.' + i + '.title');
			var price = getCookie('items.' + i + '.price');
			var quantity = getCookie('items.' + i + '.quantity');
			if ((title == null) || (title == '') || (price == null) || (price == '') || (quantity == null) || (quantity == ''))
				continue;
			price = new Number(price);
			quantity = new Number(quantity);
			if ((price <= 0) || (price > this.MAX_PRICE) || (quantity <= 0) || (quantity > this.MAX_QUANTITY))
				continue;
			this.addItem(title, quantity, price);
		}
	}

	this.refreshLink = function() {
		var element = document.getElementById(this.cartId);
		if (element == null)
			return;
		element.innerHTML = '' + this.getQuantity() + ' items, $' + this.getTotal() + ' USD';
	}

	this.onQuantityChange = function(index) {
		var element = document.getElementById('items.' + index + '.quantity');
		if ((element != null) && (element.value != null) && (element.value != '')) {
			var quantity = new Number(element.value);
			if (quantity == 0) {
				this.deleteItem(index);
				return;
			}
			if (quantity <= this.MAX_QUANTITY)
				this.items[index].quantity = quantity;
		}
		this.save();
		this.render();
		this.refreshLink();
	}
}

var cart = new Cart('cart-lines', 'cart-link');
cart.load();

