window.addEvent('domready', function(){

	function remove(identifier,des,bcid){
		identifier = identifier.replace("rm-","");
		//alert('Remove item: ' + des);
		var rem = new Request({
			method: 'post',
			url: 'cart.php',
			onSuccess: function(txt){
				var response = new Element('div',{id: 'cart-'+identifier, text:txt});
				var resultbox = $('result');
				cartitem = 'cart-'+identifier;
				resultbox.set('html', txt);
				var highlightdiv = $('updateresults');
				highlightdiv.set('html', '<div class="good">Item <b>'+ des +'</b> removed from cart!</div>');
				highlightdiv.highlight('#afa');
				// need to re-establish mootools addEvents
				cartRemove = $$('.cart_body_rm');
				cartRemove.addEvent('click', function(el) {
					des = this.get('rel');
					bcid = this.get('lang');
					//alert(this.id+" "+des+" "+bcid);
					remove(this.id,des,bcid);
				});
				cartChange = $$('.cart_body_change');
				cartChange.addEvent('change', function(el) {
					des = this.get('rel');
					bcid = this.get('lang');
					//alert(this.id+" "+des+" "+bcid);
					change(this.id,this.value,des,bcid);
				});
				checkout = $$('.checkout');
				checkout.addEvent('change', function(el) {
					checkout_go();
				});
			},
			onFailure: function(){
				$('result').set('text', '<div class="error">You could not remove this item to the cart!</div>');
			}
		});
		rem.send("action=delete&id="+identifier+"&bcid="+bcid);
	}
	
	function checkout_go(){
		//alert('Go to checkout');
		window.location="index.php?page=checkout&action=checkout";
	}
	
	function change(identifier,quantity,des,bcid){
		identifier = identifier.replace("ch-","");
		//alert('Item ' + des + ' quantity changed to '+ quantity);
		var chg = new Request({
			method: 'post',
			url: 'cart.php',
			onSuccess: function(txt){
				var resultbox = $('result');
				resultbox.set('html', txt);
				var highlightdiv = $('updateresults');
				highlightdiv.set('html', '<div class="good">Item <b>'+ des +'</b> - updated! New quantity is <b>' + quantity + '</b>.</div>');
				highlightdiv.highlight('#afa');
				// need to re-establish mootools addEvents
				cartRemove = $$('.cart_body_rm');
				cartRemove.addEvent('click', function(el) {
					des = this.get('rel');
					bcid = this.get('lang');
					//alert(this.id+" "+des+" "+bcid);
					remove(this.id,des,bcid);
				});
				cartChange = $$('.cart_body_change');
				cartChange.addEvent('change', function(el) {
					des = this.get('rel');
					bcid = this.get('lang');
					//alert(this.id+" "+des+" "+bcid);
					change(this.id,this.value,des,bcid);
				});
				checkout = $$('.checkout');
				checkout.addEvent('click', function(el) {
					checkout_go();
				});
			},
			onFailure: function(){
				$('result').set('text', '<div class="error">Error: You could not change this item\'s quantity!</div>');
			}
		});
		chg.send("action=update&id="+identifier+"&quantity="+quantity+"&bcid="+bcid);
	}

	$$('.trigger').addEvent('change', function(el) {
		var thisid = this.get('id');
		var val = $(thisid).value;
		var req = new Request({
			method: 'post',
			url: 'cart.php',
			onSuccess: function(txt){
				var response = new Element('div',{id: 'cart-'+thisid, text:txt}); // don't think we need this but keep it just incase!
				var resultbox = $('result');
				cartitem = 'cart-'+thisid;
				resultbox.set('html', txt);
				var cartid = 'cart-'+thisid;
				var attribs = 'attribs-'+thisid;
				var thisprice = $(attribs).getFirst('input').value;
				var thisdesc = $(attribs).getLast('input').value;
				var highlightdiv = $('updateresults');
				highlightdiv.set('html', '<div class="good">'+val+' x '+thisdesc+' - added to cart!</div>');
				highlightdiv.highlight('#afa');
				$('cart-'+thisid).tween('background-color', '#fff', '#b2dde3');
				var ind = $(thisid).selectedIndex; 
				if (ind > 0) {
					$(thisid).selectedIndex = 0;
				}
				// need to re-establish mootools addEvents
				cartRemove = $$('.cart_body_rm');
				cartRemove.addEvent('click', function(el) {
					des = this.get('rel');
					bcid = this.get('lang');
					//alert(this.id+" "+des+" "+bcid);
					remove(this.id,des,bcid);
				});
				cartChange = $$('.cart_body_change');
				cartChange.addEvent('change', function(el) {
					des = this.get('rel');
					bcid = this.get('lang');
					//alert(this.id+" "+des+" "+bcid);
					change(this.id,this.value,des,bcid);
				});
				checkout = $$('.checkout');
				checkout.addEvent('click', function(el) {
					checkout_go();
				});
			},
			onFailure: function(){
				$('result').set('text', '<div class="error">You could not add this item to the cart!</div>');
			}
		});
		if (val==''){
			return false;
		} else {
			var cartid = 'cart-'+thisid;
				var attribs = 'attribs-'+thisid;
				var thisprice = $(attribs).getFirst('input').value;
				var thisdesc = $(attribs).getLast('input').value;
			req.send("action=add&qty="+val+"&id="+thisid+"&price="+thisprice+"&desc="+thisdesc+"");
		}
	});
	
	/* remove from cart */
	cartRemove = $$('.cart_body_rm');
	cartRemove.addEvent('click', function(el) {
		des = this.get('rel');
		bcid = this.get('lang');
		//alert(this.id+" "+des+" "+bcid);
		remove(this.id,des,bcid);
	});
	
	/* change cart item qty */
	cartChange = $$('.cart_body_change');
	cartChange.addEvent('change', function(el) {
		des = this.get('rel');
		bcid = this.get('lang');
		//alert(this.id+" "+des+" "+bcid);
		change(this.id,this.value,des,bcid);
	});
	
	/* Checkout */
	checkout = $$('.checkout');
	checkout.addEvent('click', function(el) {
		checkout_go();
	});
	
});