var Products = [
	{"title": "All You really need to know About MRI Physics", "price": 45},
	{"title": "All About MRI Physics Self Test Questions", "price": 18},
	{"title": "All About MRI Physics Home Study", "price": 96},
	{"title": "Home Study Extra Blank Answer Sheets", "price": 64},
	{"title": "All About MRI Physics Lecturer's CD-ROM", "price": 395}
];

var Shipping = [
	{"domestic": 6, "canada": 10, "europe": 13, "everywhere": 16},
	{"domestic": 2, "canada": 3.50, "europe": 7, "everywhere": 7},
	{"domestic": 6},
	{"domestic": 1},
	{"domestic": 6, "canada": 10, "europe": 13, "everywhere": 16}
];

var theCart = null;

function init() {
	createCart();
	populateCart();
}

function checkBizLogic() {
	// Home study only shipped in U.S.
	if((theCart.items[2] > 0 || theCart.items[3] > 0) && document.getElementById("location").value != "domestic") {
		alert("The Home Study Program (and extra answer sheets) are available in the United States only.\n\nPlease either specify a United States location or remove these items from your cart.");
		return false;
	}
		
	return true;
}

function checkout() {
	if(!checkBizLogic())
		return false;
	
	var baseURL = "https://www.paypal.com/cgi-bin/webscr?cmd=_cart&upload=1&business=moriel@simplyphysics.com&rm=0&currency_code=USD&lc=US";
	items_string = "";

	var activeItemCount = 0;
	for(i = 0; i < theCart.items.length; i++) {
		if(theCart.items[i] < 1)
			continue;
			
		activeItemCount++;
		quantity = theCart.items[i];
		name = Products[i].title;
		price = Products[i].price;
		item_string = "item_name_" + activeItemCount + "=" + escape(name) + "&quantity_" + activeItemCount + "=" + quantity + "&amount_" + activeItemCount + "=" + price + "&shipping_" + activeItemCount + "=" + calculateShipping(i);
		items_string = items_string + "&" + item_string;
	}

	location.href = baseURL + items_string;
}

function calculateShipping(id) {
	theLocation = document.getElementById("location").value;
	return eval("Shipping[id]." + theLocation) * theCart.items[id];
}

function createCart() {
	if(!theCart) {
		theCart = new Cart();
		if(theCookie = getCookie("cart"))
			theCart.items = JSON.parse(getCookie("cart"));
	}
}

function populateCart() {
	updateInfo();
	
	var cartBody = "";
		
	for(i = 0; i < theCart.items.length; i++)
		if(theCart.items[i] > 0)
			cartBody += displayItem(i) + "<hr />";
			
	document.getElementById("cart_items").innerHTML = cartBody;
}

function displayItem(id) {
	product = Products[id];
	output = "<div class = \"cart_item\">" + 
			"<h4>" + product.title + "</h4>" +
			quantity_field(id) + " x " +
			formatMoney(product.price) +
			" = <div id=\"" + id + "_subtotal\"" +
			"class = \"subtotal\">" + formatMoney(product.price * theCart.items[id]) + "</div><a href='#' onClick=\"removeItem(" + id +")\">remove</a>" + "</div>";
	return output;		
}

function quantity_field(id) {
	return "<input type=\"text\" size=\"2\" onblur=\"updateSubTotals(" + id +", this.value)\" value=\"" + theCart.items[id] + "\" />";
}


function formatMoney(number) {
	return "$" + number + ".00";
}

function updateInfo() {
	setNumItems(theCart.numItems());
	setTotal(theCart.total());
}

function updateSubTotals(id, quantity) {
	theCart.items[id] = parseInt(quantity);

	populateCart();
	saveCart();
}

function setNumItems(numItems) {
	document.getElementById("numItems").innerHTML = numItems;
}

function setTotal(total) {
	document.getElementById("total").innerHTML = '$' + total + '.00';
}

function addItem(id) {
	theCart.add(id);
	populateCart();
	saveCart();			
}

function removeItem(id) {
	theCart.remove(id);
	populateCart();
	saveCart();
}

function saveCart() {
	var now = new Date();
	now.setTime(now.getTime() + 7 * 24 * 60 * 60 * 1000);
	setCookie("cart", JSON.stringify(theCart.items), now);
}
