var items=new Array;
var cart=new Array;

function additem(name,cost,quantityincrement) {
	if (!quantityincrement) quantityincrement=1; 
	var index=items.length;
	items[index]=new Object;
	items[index].name=name;
	items[index].cost=cost;
	items[index].quantityincrement=quantityincrement;

	document.write("<span id='itembuttons" + index + "'></span>");
	
	return index;
}

function addoption(name) {
	var item=items[items.length-1]; // get the last item
	if (!item.options) item.options=new Array;
	var index=item.options.length;
	item.options[index]=new Object;
	item.options[index].name=name;
}

function addvolumediscount(quantity,cost) {
	var item=items[items.length-1];
	item.discountquantity=quantity;
	item.discountcost=cost;
}

function getcost(itemid,quantity) {

	var item=items[itemid];
	if (item.discountquantity && quantity>=item.discountquantity) {
		return item.discountcost;
	} else {
		return item.cost;
	}
}

function writeitems() {
	var i;
	for (i=0; i<items.length; i++) {
		var item=items[i];
		var placeholder=document.getElementById("itembuttons" + i);
		var s="<p>";

		// options, if any
		if (item.options) {
			s=s+"<select id='options"+i+"'>";
			var j;
			for (j=0; j<item.options.length; j++) {
				s=s+"<option value='"+item.options[j].name+"'>"+item.options[j].name+"</option>";
			}
			s=s+"</select>&nbsp;&nbsp;&nbsp;";
		}

		// add to cart
		s=s+"Quantity: <input id='quantity"+i+"' size='3'/> ";
		s=s+"<input type='submit' value='Add to Cart' onclick='addtocart("+i+"); return false;'/></p>";


		placeholder.innerHTML=s;
	}
	refreshcart(false);
}

function validatequantity(itemid,quantity,message) {
	var ok=false;
	var item=items[itemid];
	var minimum;
	var quantityincrement;

	if (quantity) {
		if (quantity.match(/^[0-9]+$/)){	
			if (item.minimum) minimum=item.minimum; else minimum=1;
			var quantityint=parseInt(quantity);
			if (quantityint>=minimum) {
				if (item.quantityincrement) quantityincrement=item.quantityincrement; else quantityincrement=1;
				if (quantity % quantityincrement == 0) {
					ok = true;
				}
			}
		}
	}

	if (!ok && message) {
		var messagetext="Please enter a Quantity";
		if (minimum > 1) messagetext=messagetext + " of at least " + minimum;
		if (minimum > 1 && quantityincrement > 1) message=message + " and";
		if (quantityincrement > 1) messagetext=messagetext + " which is a multiple of " + quantityincrement;
		messagetext=messagetext + ".";
		alert(messagetext);
	}
	return ok;
}

function addtocart(itemid) {
	var item=items[itemid];
	// validate quantity
	var quantity=document.getElementById("quantity"+itemid);
	var quantityvalue=quantity.value;
	if (quantityvalue=="") quantityvalue="1";
	if (validatequantity(itemid,quantityvalue,true)) {
		// see if item already exists in cart
		var option=document.getElementById("options"+itemid);

		var i;
		var found=false;
		for (i=0; i<cart.length; i++) {
			if (cart[i].itemid==itemid) {
				if (!option || option.options[option.selectedIndex].value==cart[i].option) {
					found=true;
					cart[i].quantity=cart[i].quantity+parseInt(quantityvalue);
					break;
				}
			}
		}

		if (!found) {
			var index=cart.length;
			cart[index]=new Object;
			cart[index].itemid=itemid;
			cart[index].quantity=parseInt(quantityvalue);
			if (option) cart[index].option= option.options[option.selectedIndex].value;
		}

		// refresh
		refreshcart(false);

		alert("The item has been added to your cart. To view, change your cart, or check out, scroll to the bottom of this page.");
		quantity.value="";
	}
}

function getshipping() {
	var total=0;
	var i;
	for (i=0; i<cart.length; i++) {
		total=total+getcost(cart[i].itemid,cart[i].quantity)*cart[i].quantity;
	}
	if (total<10) {
		return 2;
	} else {
		return 0;
	}
}

function gettotal() {
	var total=0;
	var i;
	for (i=0; i<cart.length; i++) {
		total=total+getcost(cart[i].itemid,cart[i].quantity)*cart[i].quantity;
	}
	return total+getshipping();
}

function refreshcart(checkform) {
	var i;
	if (checkform) {
		// update quantities
		for (i=0; i<cart.length; i++) {
			var quantity=document.getElementById("cartquantity"+i);
			if (quantity) {
				if (validatequantity(cart[i].itemid,quantity.value,false)){
					cart[i].quantity=parseInt(quantity.value);
				}
			}
		}
	}

	// refresh table

	var s="<table cellspacing='0' cellpadding='0'>";
	s=s+"<tr class='cartheader'><td>Item</td><td>Quantity</td><td>Unit Cost</td><td>Total Cost</td><td>&nbsp;</td></tr>";
	var odd=false;
	for (i=0; i<cart.length; i++) {
		var itemid=cart[i].itemid;
		var item=items[itemid];
		if (odd) {
			s=s+"<tr class='cartodd'>";
		} else {
			s=s+"<tr>";
		}
		odd=!odd;

		var cost=getcost(itemid,cart[i].quantity);

		s=s+"<td>"+item.name;
		if (cart[i].option) {
			s=s+"<br/><span class='cartoption'>"+cart[i].option+"</span>";
		}
		s=s+"</td>";
		s=s+"<td><input id='cartquantity"+i+"' value='"+cart[i].quantity+"' size='3'/></td>";
		s=s+"<td>"+cost.toFixed(2)+"</td>";
		s=s+"<td>"+(cart[i].quantity * cost).toFixed(2)+"</td>";
		s=s+"<td><input type='submit' value='Remove' onclick='remove("+i+"); return false;'/></td>";

		s=s+"</tr>";

	}

	if (cart.length>0) {
		s=s+"<tr><td><b>Shipping</b></td><td>&nbsp;</td><td>&nbsp;</td><td>"+getshipping().toFixed(2)+"</td><td>&nbsp;</td></tr>";
		s=s+"<tr class='cartheader'><td>Total</td><td>&nbsp;</td><td>&nbsp;</td><td>"+gettotal().toFixed(2)+"</td><td>&nbsp;</td></tr>";
	} else {
		s=s+"<tr><td colspan='5'><i>There are no items in your cart.</i></td></tr>"
	}

	s=s+"</table>";

	document.getElementById("cart").innerHTML=s;
}

	function remove(cartid) {
		cart.splice(cartid,1);
		refreshcart(true);
	}
	
	function fillsummary(text) {
		if (text.length>2550) {
			return false;
		} else {			
			for (i=0; i<10; i++) {
				document.getElementById("cartsummary" + (i+1)).value=text.substr(i*255,255);
			}
			return true;
		}
	}
	
	function validate() {
		if (cart.length<=0) {
			alert("There are no items in your cart.");
			return false;
		} else {
			// set totals
			
			document.getElementById("cartamount").value=gettotal();
			var summary="store|";
			var i;
			for (i=0; i<cart.length; i++) {
				summary=summary + items[cart[i].itemid].name + "|";
				if (cart[i].option) summary=summary + cart[i].option;
				summary=summary + "|" + cart[i].quantity;
				summary=summary + "|" + getcost(cart[i].itemid,cart[i].quantity) + "|";
			}
			summary=summary + getshipping();
			
			if (!fillsummary(summary)) {
				alert("There are too many items in your cart. Please remove some items, and try again.")
				return false;
			}
			
			return true;
		}
	}

