// JavaScript Document
var debug = false;

function debugTools(t) {
	alert(t.responseText);
}

function cartHandle(t) {
	j = eval("(" + t.responseText + ")");
	var action = j['action'];
	var status = j['status'];
	var cart = j['cart'];
	
	switch(action) {
		case "show":
			cartRefresh(cart);
			break;
		case "add":
			cartRefresh(cart);
			break;
		case "empty":
			cartEmpty();
			break;
		case "display":
			cartDisplay();
			break;
		default :
			cartRefresh(cart);
			break;
	}
}

function cartDisplay() {
	var strHtml = "";
		strHtml += "<span id='cart_head' class='cartHead'>Your Cart<br></span>";
		strHtml += "<span id='cart_info'> Cart items: " + c['count'] + ". Total $" + c['total'] + ".<br>";
		if(c['count']>0) {
			strHtml += "<a href=\"https://www.amesrgi.com/order.php\" style='font-size: 7pt' id='cart_checkout'>Check out.</a>";
		}
		strHtml += "</span>";
	$('cart_container').innerHTML = strHtml;
}

function cartAdd(action,id,amt) {
	if(action) {
		var param = "action=" + action + "&id=" + id + "&amt=" +amt;
		new Ajax.Request('/shared/php/ajax_cart.php', {parameters: param, onSuccess:cartHandle});
	} else {
	}
}

function cartRefresh(c) {
	/* This func to be called upon loading the HTML div with cartAdd('show','','');
		The action is passed: cartAdd --> ajax_cart.php --> cartAdd -->cartHandle --> actual refresh action */
	if(debug) { alert("cartRefresh starts..."); }
	var strHtml = "";
		strHtml += "<span id='cart_head' class='cartHead'>Your Cart<br></span>";
		strHtml += "<span id='cart_info'> Cart items: " + c['count'] + ". Total $" + c['total'] + ".<br>";
		if(c['count']>0) {
			strHtml += "<a href=\"https://www.amesrgi.com/order.php\" style='font-size: 7pt' id='cart_checkout'>Check out.</a>";
		}
		strHtml += "</span>";
	$('cart_container').innerHTML = strHtml;
	new Element.scrollTo('cart_container');
	new Effect.Highlight('cart_container');
}

function cartEmpty() {
	$('cart_container').innerHTML = "";
}