var AjaxBasket = function()
{
	/**
         * functions called by buttons etc
         * @param stockcode the stockcode to add
         * @param quantity how many to add
         * @param partquantity i don't really know
         * @return nothing
         */
	this.add = function(basketData)
	{
            console.log('adding');
		var scope = this;
		$.ajax({
			type : 'POST',
			url : '/ajaxBasket.php',
			data : 'action=add&' + basketData,
			success : function(response) { scope.addSuccess(json_decode(response)); },
			error : function(response) { scope.addFailure(json_decode(response)); }
		});
	};
	
	this.remove = function(basketData)
	{
		var scope = this;
		$.ajax({
			type: 'POST',
			url : '/ajaxBasket.php',
			data : 'action=remove&' + basketData,
			success : function(response) { scope.removeSuccess(json_decode(response)); },
			error : function(response) { scope.removeFailure(json_decode(response)); }
		});
	};
	
	
	// called by ajax object
	
	this.addSuccess = function(response)
	{
		if(response.type == 'error') { return this.addFailure(response) }
		if(typeof this.addSuccessCallback == 'function') { this.addSuccessCallback(response); }
	};
	
	this.addFailure = function(response)
	{ 
		if(typeof this.addFailureCallback == 'function') { this.addFailureCallback(response); }
	};
	
	this.removeSuccess = function(response)
	{
		if(response.type == 'error') { return this.addFailure(response); }
		if(typeof this.removeSuccessCallback == 'function') { this.removeSuccessCallback(response); }
	};
	
	this.removeFailure = function(response)
	{
		if(typeof this.removeFailureCallback == 'function') { this.removeFailureCallback(response); }
	};
	
	// helper functions
	
	this.contains = function(stcode)
	{
		var found = false;
		$('#baskettable tr').each(function(){ if(this.className == 'basket_' + stcode) { found = true; } });
		if(found) { return true; }
		return false;
	};
}



/***************************************************
*
*	customise these if needed

****************************************************/

refreshBasketAdd = function(response)
{

	if(this.contains(response.laststockcode))
	{
		// update row
		var thisRow = $('#baskettable .basket_' + response.laststockcode);
		thisRow.stop(true,true).effect("highlight", { color : '#ffd966' }, 1000);
		var quanspan = thisRow.find('.quantity');
		var quan = quanspan.html();
		quan = Number(quan) + Number(response.lastquantity);
		quanspan.html(quan);

	}
	else
	{
		// add a new row
		var newRow = $('#baskettable .master').clone();
		newRow.removeClass().addClass('basket_' + response.laststockcode);
		newRow.hide();

		$(newRow).find('.stockdesc').html(response.laststockdesc);
		$(newRow).find('.quantity').html(response.lastquantity);
		$(newRow).find('#remove #stockcode').value = response.laststockcode;

		$('#baskettable').append(newRow);
		$(newRow).find('#remove #stockcode').val(response.laststockcode);
		newRow.effect("highlight", { color : '#ffd966' }, 1000);
		newRow.slideDown();

		$('#baskettable #remove').submit(function() {
                    var BasketRemoveString = $(this).serialize();
                    myAjaxBasket.remove(BasketRemoveString);
                    return false;
                });

	}

        $('.baskettotal').html(response.total);


}

refreshBasketRemove = function(response)
{

	var thisRow = $('#baskettable .basket_' + response.laststockcode);
	thisRow.stop(true,true).effect("highlight", { color : '#ff5457' }, 500);
	thisRow.slideUp('fast', function() { thisRow.remove(); });

}

addBasket = function(stcode, quantity)
{
	myAjaxBasket.add(stcode, quantity);
}

AjaxBasket.prototype.removeSuccessCallback = refreshBasketRemove;
AjaxBasket.prototype.addSuccessCallback = refreshBasketAdd;



