//if the browser is capable of supporting SJS, this makes the relevant elements disappear and such
function sjs_initialise()
{
	/*//find anchor and whether or not it needs to not be disappeared
	if(location.hash != '')
	{
		var element;
		if(element = document.getElementById(location.hash.substr(1)))
		{
			sjs_pulsate(location.hash.substr(1));
		}
	}*/
	allElements = document.getElementsByTagName("*");
	var anchors;
	for(i = 0; i < allElements.length; i++)
	{
		//if it's a link to an anchor, add an onclick for smooth scrolling
		if(allElements[i].href && allElements[i].href.indexOf(location.href+"#") == 0)
		{
			allElements[i].onclick = function(){sjs_scroll(this.href.substr(location.href.length + 1));};
		}
		//if it's a form asking to be AJAXed, add the relevant JavaScript
		//(I think it's better to test for AJAX later to avoid load on the browser)
		if(allElements[i].hasClassName('ajaxable'))// && allElements[i].tagName == 'FORM')
		{
			//make it call AJAX on submit
			allElements[i].onsubmit = function() {sjs_ajaxform(); return false;};
		}
	}
	//document.getElementById('buyform').onsubmit = function() {return !sjs_ajaxform();};
	
	return true;
}

function sjs_validateform(form)
{
	
}

function sjs_ajaxform()
{
	//this is the place where a status mesage will be displayed
	var notice = $('buystatus');
	notice.update('Buying...')
	//validate the form
	//check that the number of products is a number
	var num = $F('buynum');
	if (isNaN(num)) {notice.update('Please specify a valid number of products.'); return false;}
	var url = '/basket/add/';
	new Ajax.Request(url, {
	  method: 'get',
	  parameters: $('buyform').serialize(true),
	  onSuccess: function(transport) {
		if (transport.responseText == 'success')
		{
		 	notice.update('Added to <a href="/basket/">your basket</a>.').setStyle({ background: '#d0f0' });
		}
		else
		{
			//notice.update('ERROR—please try again.').setStyle({ background: '#f00' });
			notice.update(transport.responseText).setStyle({ background: '#f00' });
		}
	  }
	});
	return false;
}


/*
function sjs_pulsate(elementId)
{
	new Effect.Pulsate(elementId, { pulses: 3, duration: 1.5 });
}

//this slides down an invisible element, sliding up any others which may be visible beforehand
function sjs_show(elementId)
{
	
} */

function sjs_scroll(elementId)
{
	new Effect.ScrollTo(elementId);
	sjs_pulsate(elementId);
}


//making the search box do cool things (colour changes managed through CSS)
function clearText(formfield)
{
	if(formfield.value == "search")
	{
		formfield.value = "";
	}

}

function unclearText(formfield)
{
	if(formfield.value == "")
	{
		formfield.value = "search";
	}
}

// http://www.maratz.com/blog/archives/2006/06/07/preload-images-with-javascript/
function preload_images()
{
	var preloaded = new Array();
	for (var i = 0; i < arguments.length; i++)
	{
		preloaded[i] = document.createElement("img");
		preloaded[i].setAttribute("src",arguments[i]);
	};
};