// JavaScript Document

// item database
var itemDatabase = {
	1:{
		nameEN:		"LBC Avid Keyboard",
		nameDE:		"LBC Avid Tastatur",
		priceGBP:	28.95,
		priceEUR:	37.95
	},
	2:{
		nameEN:		"LBC Final Cut Keyboard",
		nameDE:		"LBC Final Cut Tastatur",
		priceGBP:	25.95,
		priceEUR:	35.95
	},
	3:{
		nameEN:		"LBC Adobe Premiere Pro Keyboard",
		nameDE:		"LBC Adobe Premiere Pro Tastatur",
		priceGBP:	28.95,
		priceEUR:	35.95
	}
}

var currencyCodes = {
	EUR:"&euro;",
	GBP:"&pound;"
};

function Cart(language) {
	
	this.language = language;
	this.currency = "EUR";
	
	this.items = [];
	this.itemCount = 0;
	
	this.read = function() {
		if (document.cookie == "") return;
		var coo = document.cookie.split(";");

		var i;
		for (i = 0; i < coo.length; i++)
			if (coo[i].indexOf("CartItems") >= 0) break;
		if (i == coo.length) return; // no items info found

		var itemsString = coo[i].split("=")[1];
		if (itemsString == "" || itemsString == undefined) return;  // no items in the cart
		
		var items = itemsString.split("|");

		this.currency = items[items.length-1];
		for (i = 0; i < items.length-1; i++) {
			if (items[i].indexOf(":") < 1) continue;
			var itemInfo = items[i].split(":");
			this.items[i] = new CartItem(parseInt(itemInfo[0]), this.language, this.currency);
			this.items[i].count = parseInt(itemInfo[1]);
			this.itemCount += this.items[i].count;
		}
		
	}

	this.itemIndexOf = function(id) {
		for (var i = 0; i < this.items.length; i++)
			if (this.items[i].id == id) return i;
		return -1;
	}
	
	this.addItem = function(id) {
		var index = this.itemIndexOf(id);
		if (index == -1) this.items.push(new CartItem(id, this.language, this.currency));
		else this.items[index].count++;
		this.save();
	}

	this.update = function(id, count) {
		count = parseInt(count);
		if (isNaN(count) || count < 1)
			location.reload();
		else {
			var index = this.itemIndexOf(id);
			if (index > -1 && this.items[index].count != count) {
				this.items[index].count = count;
				this.save();
			}
		}
	}
	
	this.remove = function(id) {
		var index = this.itemIndexOf(id);
		if (index > -1)
			this.items.splice(index,1);
		this.save();
	}
	
	this.save = function() {
		var cookieString = "CartItems=";
		for (var i = 0; i < this.items.length; i++) {
			if (i > 0) cookieString += "|";
			cookieString += this.items[i].id + ":" + this.items[i].count;
		}
		cookieString += "|" + this.currency;
		var now = new Date();
		now.setTime(now.getTime() + 24 * 60 * 60 * 1000);  // save for 1 day
		cookieString += ";path=/;expires=" + now.toGMTString() + ";";
		document.cookie = cookieString;
		location.href = "view_cart.html";
	}
	
	this.display = function(ele) {
		if (this.items.length == 0) {
			ele.innerHTML = "<div>Your shopping cart is empty</div>";
			ele.innerHTML += "<a href='shop.html' class='continue_shopping'></a><div style='clear:both'></div>";
			return;
		}
		var subtotal = 0;
		var html = "<div class='currency_selector'>Your current currency type is <select id='currency_selector'>"
		for (var cur in currencyCodes) {
			html += "<option value='" + cur + "'";
			if (cur == this.currency) html += " selected";
			html += ">" + cur + "</option>";
		}
		html += "</select> <input type='button' value='change' onclick=\"cart.updateCurrency(document.getElementById('currency_selector').value)\" /></div>";
		html += "<div class='cart_caption'><div class='name'>ITEM NAME</div><div class='price'>PRICE</div><div class='quantity'>QUANTITY</div><div class='remove'>REMOVE</div><div class='amount'>AMOUNT</div></div>";
		for (var i = 0; i < this.items.length; i++) {
			html += this.items[i];
			subtotal += itemDatabase[this.items[i].id]["price" + this.currency] * this.items[i].count;
		}
		html += "<div class='subtotal'>SUBTOTAL: <span class='price'>" + formatCurrency(subtotal, this.currency) + "</span></div>";

		// generate buttons
		html += "<a href='shop.html' class='continue_shopping'></a>";
		html += "<a href='check_out.html' class='check_out'></a>";
		html += "<a href='javascript:cart.paypalCheckOut()' class='check_out_paypal'></a>";
		html += "<div style='clear:both'></div>";

		// generate form for paypal payments
		html += "<form target='paypal' id='paypal_form' action='https://www.paypal.com/cgi-bin/webscr' method='post'>";
		html += "<input type='hidden' name='cmd' value='_xclick'>";
		html += "<input type='hidden' name='business' value='lbcommunications@yahoo.com'>";
		if (this.language=="EN")
			html += "<input type='hidden' name='shipping' value='9.95'>";
		html += "<input type='hidden' name='no_shipping' value='0'>";
		html += "<input type='hidden' name='no_note' value='1'>";
		html += "<input type='hidden' name='lc' value='" + (this.language=="EN" ? "GB" : "DE") + "'>";
		html += "<input type='hidden' name='currency_code' value='" + this.currency + "'>";
		html += "<input type='hidden' name='item_name' value='";
		for (i = 0; i < this.items.length; i++) {
			if (i > 0) html += ", ";
			html += itemDatabase[this.items[i].id]["name" + this.language];
			html += "(" + this.items[i].count + ")";
		}
		html += "'>";
		html += "<input type='hidden' name='amount' value='" + subtotal + "'>";
		html += "</form>";
		
		ele.innerHTML = html;
	}
	
	this.getItemCount = function() {
		return this.itemCount;
	}
	
	this.getAddToCartHTML = function(id) {
		html = "<div>Price: <span class='price'>" + currencyCodes[this.currency] + itemDatabase[id]["price"+this.currency] + "</span></div>"
		html += "<a class=\"add_to_cart\" href=\"javascript:cart.addItem(" + id + ")\"></a>";
		return html;
	}
	
	this.paypalCheckOut = function() {
		this.items.length = 0;  // empty cart
		this.save();
		document.getElementById("paypal_form").submit();
	}
	
	this.updateCurrency = function(cur) {
		this.currency = cur;
		this.save();
	}
	
	this.read();
}

function CartItem(id, lan, cur) {
	this.id = id;
	this.language = lan;
	this.currency = cur;
	this.count = 1;
}

CartItem.prototype.toString = function() {
	return "<div class='cart_item'><div class='name'>" + 
	"<a href='shop.html'>" + itemDatabase[this.id]["name" + this.language] + "</a></div><div class='price'>" +
	formatCurrency(itemDatabase[this.id]["price" + this.currency], this.currency) + 
	"</div><div class='quantity'><input maxlength='2' size='1' id='" + itemDatabase[this.id]["name" + this.language].replace(/ /g, '_') + "' value='" + this.count + "' />&nbsp;<input type='button' value='update' onclick=\"cart.update(" + this.id + ", document.getElementById('" + itemDatabase[this.id]["name" + this.language].replace(/ /g, '_') + "').value)\" /></div>" +
	"<div class='remove'><input type='button' value='remove' onclick=\"cart.remove(" + this.id + ")\" />&nbsp;</div>" +
	"<div class='price amount'>" + 
	formatCurrency(itemDatabase[this.id]["price" + this.currency]*this.count, this.currency) + "</div></div>"
}

function formatCurrency(number, cur) {
	number = Math.round(number*100)/100;
	var s = String(number);
	if (s.indexOf(".") < 0) number += ".00";
	else if (s.lastIndexOf(".") == s.length-1) number += "00";
	else if (s.lastIndexOf(".") == s.length-2) number += "0";
	return (cur == "GBP" ? "&pound;" : "&euro;") + number;
}

