// ****************************************************************
// ********************** AJAX CART *******************************
// ********** shopping basket management through AJAX *************
// **********************c2009 Nick Brennan ***********************
// ****************************************************************

/////////// loader gif control ////////////
var loaded = false;

function startLoading() {
loaded = false;
//window.setTimeout('showLoadingImage()', 100);
showLoadingImage();
}

function showLoadingImage() {
var el = document.getElementById("loaderbox");
if (el && !loaded) {
    el.innerHTML = '<img src="/images/ajax-loader.gif" width="35" height="9"/>';
    new Effect.Appear('loaderbox',{ duration: 0.3 });
}
}

function stopLoading() {
    Element.hide('loaderbox');
    loaded = true;
  }
  
/////////// add an item to the session basket ////////////  
function ajaxAddItem(w) {

  	var url = '/addItem.php'; 
	// notice the use of a proxy to circumvent the Same Origin Policy. 
	new Ajax.Request(url, { 
		method: 'post', 
		parameters: {pubID: w},
		onCreate : startLoading, 
		onComplete : stopLoading,
		onSuccess: function(transport) { 
			var response = transport.responseText;
			var cart = $('cartcontents'); 
			cart.update(response);
		} 
	}); 
}

/////////// remove an item from the session basket ////////////  
function ajaxRemoveItem(w) {
//	Ajax.Responders.register({ onCreate : startLoading, onComplete : stopLoading });
	var answer = confirm("Are you sure you want to remove this item?")
	if (answer){
		var url = '/removeItem.php'; 
		// notice the use of a proxy to circumvent the Same Origin Policy. 
		new Ajax.Request(url, { 
			method: 'post', 
			parameters: {pubID: w},
			onCreate : startLoading, 
			onComplete : stopLoading,
			onSuccess: function(transport) { 
				var response = transport.responseText;
				var cart = $('cartcontents'); 
				cart.update(response);
			} 
		}); 
	}
}

/////////// remove all items from the session basket ////////////  
function emptyCart() {
//	Ajax.Responders.register({ onCreate : startLoading, onComplete : stopLoading });
	var answer = confirm("Are you sure you want to remove all items from your basket?")
	if (answer){
		var url = '/removeAllItems.php'; 
		// notice the use of a proxy to circumvent the Same Origin Policy. 
		new Ajax.Request(url, { 
			method: 'post', 
			onCreate : startLoading, 
			onComplete : stopLoading,
			onSuccess: function(transport) { 
				var response = transport.responseText;
				var cart = $('cartcontents'); 
				cart.update(response);
			} 
		}); 
	}
}

//////////////////// calculate shipping /////////////////////
function calculateShipping(vfld) 
{
//	Ajax.Responders.register({ onCreate : startLoading, onComplete : stopLoading });
	var region = vfld.value;
	var url = '/calculateShipping.php'; 
	// notice the use of a proxy to circumvent the Same Origin Policy. 
	new Ajax.Request(url, { 
		method: 'post', 
		parameters: {region: region},
		onCreate : startLoading, 
		onComplete : stopLoading,
		onSuccess: function(transport) { 
			var response = transport.responseText;
			var cart = $('cartcontents'); 
			cart.update(response);
		}
	}); 
}

function ajaxAddSubscription(){
	  	$('subs_form').request({
			onCreate : startLoading, 
			onComplete : stopLoading,	
			onSuccess: function(transport) {
					var response = transport.responseText;
					var cart = $('cartcontents'); 
					cart.update(response);
	        }
		});
	  	return false;
}
