var bfstore = new BFStore();
dojo.addOnLoad( function(){ bfstore.setUpEvents(); });

function BFStore()
{	

	//********************************************************************************
	// Set up events
	//********************************************************************************
	this.setUpEvents = function()
	{
		dojo.query('img[name^="increment_"], img[id^="increment_"]').forEach( this.setupClick );
		dojo.query('input[name^="quantity_"], input[id^="quantity_"]').forEach( this.setupChanged );
		dojo.query('a[name^="category_"], a[id^="category_"]').forEach( this.setupCategoryLink );
		dojo.query('div[name^="category_"], div[id^="category_"]').forEach( this.setupCategoryLink );
		dojo.query('div[name^="product_"], div[id^="product_"]').forEach( this.setupProductDetails );
		dojo.query('img[name^="product_"], img[id^="product_"]').forEach( this.setupProductDetails );
		this.refreshCart();
	}
	//--------------------------------------------------------------------------------
	this.setupClick = function( obj )
	{
		obj.onclick = function(){ bfstore.incrementClicked(obj); };
		obj.style.cursor = 'pointer';
	}
	//--------------------------------------------------------------------------------
	this.setupCategoryLink = function( obj )
	{
		dojo.connect( obj, 'onclick', function(){ bfstore.categoryLink(obj);} );
		obj.style.cursor = 'pointer';
	}
	//--------------------------------------------------------------------------------
	this.setupProductDetails = function( obj )
	{
		dojo.connect( obj, 'onclick', function(){
			var prodcode = bfstore.prodcode(obj);
			window.location.href = 'product.asp?productcode=' + prodcode + '&store=' + bfstore.store();
		});
		dojo.connect( obj, 'onmouseover', function(){ dojo.addClass(obj,'bfstore-highlight');} );
		dojo.connect( obj, 'onmouseout', function(){ dojo.removeClass(obj,'bfstore-highlight');} );
		obj.style.cursor = 'pointer';
	}
	//--------------------------------------------------------------------------------
	this.setupChanged = function(obj)
	{
		dojo.connect( obj, 'onkeyup', function(){ bfstore.changeQuantity(obj);} );
		dojo.connect( obj, 'onkeypress', bfstore.requireInteger );
	}
	//--------------------------------------------------------------------------------
	this.setupCartChanged = function(obj)
	{
		dojo.connect( obj, 'onkeyup', function(){ bfstore.changeCartQuantity(obj);} );
		dojo.connect( obj, 'onkeypress', bfstore.requireInteger );
	}

	
	//********************************************************************************
	// Store control events
	//********************************************************************************
	this.requireInteger = function(e)
	{
		var key = window.event ? window.event.keyCode : e.charCode;
		return !key || (key>=48 && key<=57);
	}
	//--------------------------------------------------------------------------------
	this.categoryLink = function(elem)
	{
		var catcode = this.prodcode(elem);
		window.location.href = 'catalog.asp?store=' + this.store() + '&categoryid=' + catcode;
	}	
	//--------------------------------------------------------------------------------
	this.productDetails = function(elem)
	{
		var prodcode = this.prodcode(elem);
		window.location.href = 'product.asp?productcode=' + prodcode + '&store=' + this.store();
	}	
	//--------------------------------------------------------------------------------
	this.incrementClicked = function(elem)
	{
		var prodcode = this.prodcode(elem);
		var q = this.getQuantityObj(prodcode);
		if (!q)
			this.refreshCart( '&productcode=' + prodcode + '&quantity=+1');
		else {
			if ( !q.value || q.value=='' || isNaN(q.value) )
				q.value = '1';
			else
				q.value = parseInt(q.value) + 1;
			this.changeQuantity(q);
		}
	}	
	//--------------------------------------------------------------------------------
	this.changeQuantity = function(elem)
	{
		var v = elem.value || '0';
		this.refreshCart( '&item=' + this.prodcode(elem) + '&quantity==' + v);
	}
	//--------------------------------------------------------------------------------
	this.changeCartQuantity = function(elem)
	{
		var prodcode = this.prodcode(elem);
		var q = this.getQuantityObj(prodcode)
		if (q)
			q.value = elem.value;
		this.changeQuantity(elem);
	}
	//--------------------------------------------------------------------------------
	
	//********************************************************************************
	// Cart methods
	//********************************************************************************
	this.refreshCart = function(query)
	{
		query = query || '';

        dojo.xhrGet({
            url: 'cart.asp?' + 'ts=' + Date().toString() + '&store=' + this.store() + '&cancelurl=' + escape(this.$v('cancelurl')) + '&successurl=' + escape(this.$v('successurl'))	+ query,
            handleAs: "text",
            preventCache: true,
            load: function(txt) {
				var odiv = document.getElementById('cart');
				if (odiv)
					odiv.innerHTML = txt;	
				dojo.query('input[name^="cart_"], input[id^="cart_"]').forEach( bfstore.setupCartChanged );
			}
		});
	}
	//--------------------------------------------------------------------------------

	//********************************************************************************
	// Utility methods
	//********************************************************************************
	this.$ = function(id){ return document.getElementById(id) || document.getElementsByName(id)[0]; }
	this.$v = function(id){ return this.$(id) ? this.$(id).value : '' }
	this.getQuantityObj = function(prodcode){ return this.$('quantity_' + prodcode ); }
	this.prodcode = function(obj)
	{
		var re = /([^_]+)_(.+)$/
		if (re.test(obj.id))
			return obj.id.match(re)[2];
		else if (re.test(obj.name))
			return obj.name.match(re)[2];
		else
			return '';
	}
	this.store = function() { return this.$('store') ? this.$v('store') : ''; }
	//--------------------------------------------------------------------------------
		
	return this;
}






