//show the controls to add a certain product to the quote basket
function showAdd(id){
	document.getElementById('quote_' + id).innerHTML = '<input type="text" size="1" value="1" name="quote_quantity_' + id + '" id="quote_quantity_' + id + '" />&nbsp;<a href="#" onclick="addToQuote(' + id + '); return false;">Add</a>&nbsp;|&nbsp;<a href="#" onclick="cancelAddToQuote(' + id + '); return false;">Cancel</a>';
}

//add a certain product to the quote basket
function addToQuote(id){
	var quantity = document.getElementById('quote_quantity_' + id).value;
	
	if(!isInteger(quantity) || quantity <= 0){
		alert('Invalid quantity - please enter a whole number');
	}
	else{
	
		document.getElementById('quote_' + id).innerHTML = 'Adding...';
		
		var ajax = getAjax();
		ajax.onreadystatechange= function(){
			if(ajax.readyState == 4){
				//request complete
				if(ajax.responseText == '1'){
					document.getElementById('quote_' + id).innerHTML = 'Added to quote';
				}
				else{
					cancelAddToQuote(id);
					alert('An error occurred adding to the quote.\nPlease try again.');
				}
			}
		}
		ajax.open("GET", BASE_URL + 'quote_basket.php?action=add&id=' + id + '&quantity=' + quantity, true);
		ajax.send(null);
		
	}
}

//hide the controls to add a certain product to the quote basket
function cancelAddToQuote(id){
	document.getElementById('quote_' + id).innerHTML = '<a href="#" onclick="showAdd(' + id + '); return false;\">Add To Quote</a>';
}

//get the details for a certain product
function getProductDetails(id){
	_id('product_details').innerHTML = '<p style="text-align: center;">Loading...</p>';
	
	var ajax = getAjax();
	
	ajax.onreadystatechange= function(){
		if(ajax.readyState == 4){
			//request complete
			_id('product_details').innerHTML = ajax.responseText;
		}
	}
	ajax.open("GET", BASE_URL + 'product_details.php?id=' + id, true);
	ajax.send(null);
	return false;
}

//show the controls to change a quantity in a basket
function showChangeBasketQuantity(id, value){
	_id('quantity_' + id).innerHTML = '<input type="text" size="1" id="new_quantity_' + id + '" value="' + value + '" />&nbsp;<a href="#" onclick="changeBasketQuantity(' + id + '); return false;">Save</a>&nbsp;|&nbsp;<a href="#" onclick="cancelChangeBasketQuantity(' + id + ', ' + value + '); return false;">Cancel</a>';
}

//hide the controls to change a quantity
function cancelChangeBasketQuantity(id, value){
	_id('quantity_' + id).innerHTML = '<a href="#" onclick="showChangeBasketQuantity(' + id + ', ' + value + '); return false;">Change</a>';
}

function changeBasketQuantity(id){
	var new_value = _id('new_quantity_' + id).value;
	
	if(!isInteger(new_value) || new_value <= 0){
		alert('Invalid quantity - please enter a whole number');
	}
	else{
		_id('quantity_' + id).innerHTML = 'Saving...';
		
		var ajax = getAjax();
		ajax.onreadystatechange= function(){
			if(ajax.readyState == 4){
				//request complete
				if(ajax.responseText == 1){
					_id('show_quantity_' + id).innerHTML = new_value;
				}
				else{
					alert('An error occurred.\nPlease try again.');
				}
				cancelChangeBasketQuantity(id, new_value);
			}
		}
		ajax.open("GET", BASE_URL + 'quote_basket.php?action=add&id=' + id + '&quantity=' + new_value, true);
		ajax.send(null);
	}
	
}

//remove an item from the basket
function deleteBasketItem(id){
	_id('quantity_' + id).innerHTML = 'Deleting...';
	
	var ajax = getAjax();
	ajax.onreadystatechange= function(){
		if(ajax.readyState == 4){
			//request complete
			if(ajax.responseText == 1){
				_id('show_quantity_' + id).innerHTML = '';
				_id('quantity_' + id).innerHTML = 'Deleted';
				_id('delete_' + id).innerHTML = '';
			}
			else{
				alert('An error occurred.\nPlease try again.');
			}
		}
	}
	ajax.open("GET", BASE_URL + 'quote_basket.php?action=delete&id=' + id, true);
	ajax.send(null);
}
