// This stuff controls the ajax calls for the shopping bag.
// Heavy stuff.

//////
// Global cart
//////
function addToCart() {
	//setTimeout(doAddToCart, 800);
	// alert("We're doing a little maintenance on the store. This should be finished shortly, so please come back in an hour or two.");
	
	doAddToCart();
}
function doAddToCart() {


		s = $('product-size').value;
		p = $('colour-options').value;		
		openToolBoxShoppingBasket(false);
		url = '/cart/?action=add&s='+s+'&p='+p;
		var myAjax = new Ajax(url, {
			method: 'get', 
			update: $('cart-contents'),
			onComplete: hideSpinner,
			evalScripts: true
		}).request();

}

function hideSpinner() {
	var basketDelay = showBasket.delay(600);
}

var removeAjax = '';
function removeFromCart(which) {
	hideBasket();
	url = '/cart/?action=remove&p='+which;
	removeAjax = new Ajax(url, {
		method: 'get', 
		update: $('cart-contents'),
		onComplete: hideSpinner,
		evalScripts: true
	});
	var removeDelay = doRemoveFromCart.delay(600);
}
function doRemoveFromCart() {
	removeAjax.request();
}

function changeCurrency(which) {
	url = '/catalogue/ajax.cfm?action=changeCurrency&c='+which;
	var myAjax = new Ajax(url, {
		method: 'get',
		update: $('price'),
		onComplete: hideSpinner
	}).request();	
}

//////
// Global login
//////
function login() {
	hideMyAccount();
	setTimeout(doLogin, 800);
}
function doLogin() {
	u = $('tb-username').value;
	p = $('tb-password').value;	
	url = '/members/?action=login_ajax&u='+u+'&p='+p;
	var loginAttempt = new Ajax(url, {
		method: 'get',
		update: $('toolbox-login'),
		onComplete: showMyAccount,
		evalScripts: true
	}).request();
}
//////


//////
// Payment path
//////
function proceed() {
	panel = whichBasketContentOpen+1;
	openPanel(panel);
}

function saveCart() {
	postUrl = '/cart/ajax.cfm?action=saveCart&'+$('cartFrm').toQueryString();
	var myAjax = new Ajax(postUrl, {
		method: 'post',
		onComplete: proceed
	}).request();	
}

function saveShipping() {
	postUrl = '/cart/ajax.cfm?action=deliveryMethod&'+$('shippingFrm').toQueryString();
	var myAjax = new Ajax(postUrl, {
		method: 'post',
		onComplete: proceed,	
		evalScripts: true
	}).request();
}

function validatePayment() {
	// validate payment details before moving on
	formReplaceButton()
	postUrl = '/cart/ajax.cfm?action=validatePayment&'+$('paymentFrm').toQueryString();
	var myAjax = new Ajax(postUrl, {
		method: 'post',
		onComplete: formReplaceSpinner,
		update: $('paymentErrorContainer'),	
		evalScripts: true
	}).request();
}

function savePayment() {
	postUrl = '/cart/ajax.cfm?action=paymentMethod&'+$('paymentFrm').toQueryString();
	var myAjax = new Ajax(postUrl, {
		method: 'post',
		onComplete: proceed,
		update: $('order-summary'),	
		evalScripts: true
	}).request();	
}

function submitOrder() {
	postUrl = '/cart/ajax.cfm?action=confirmOrder';
	var myAjax = new Ajax(postUrl, {
		method: 'post',
		update: $('order-summary'),	
		evalScripts: true
	}).request();
}