<!--// BuilderDepot Javascript Functions //
	// --------------------------------- //
	
function onSubmitPressed(formName, action)
{
	// Retrieve form
	var form = document.getElementById(formName);
	
	form.action = action;
	
	// Submit form
	form.submit();
	
	// Return true
	return true;
}

function getHeight(tableName)
{
	var table = document.getElementById(tableName);
	
	return table.clientHeight;
}

function getWidth(tableName)
{
	var table = document.getElementById(tableName);
	
	return table.clientWidth;
}

function adjustCategoryHeight()
{
	var categoryDiv = document.getElementById('category');
	var contentTableHeight = ( getHeight('content') - 50 );
	
	categoryDiv.style.height = contentTableHeight + 'px';
	
	// Call initSearchText function since adjustCategoryHeight is called on page load
	initSearchText();
	
	return true;
}

function onSelectChange(menuObj)
{
	var dept = document.getElementById('department');
	var dept_index = dept.selectedIndex;
	var dept_value = dept.options[dept_index].value;
	
	var obj = menuObj.selectedIndex;
	var obj_value = menuObj.options[obj].value;
	
	if( obj_value != 0 && obj_value != -100 )
	{
		if( menuObj.options[obj].value > 0 )
		{
			window.location = 'shopbyprice.ihtml?step=2&range=' + obj_value + '&catid=' + dept_value;
		}
	}
	else
	{
		alert( 'Sorry, but the selection that you have made is invalid.' );
	}
}

function onKeyPressed(form, event)
{
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	
	if( keyCode == 13 )
	{
		form.submit();
	} 
}

function onKeyPressed_addToBasket(form, event)
{
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	
	if( keyCode == 13 )
	{
		addToBasket(form);
	} 
}

function setInputValue(inputbox, text)
{
	var obj = document.getElementById(inputbox);
		
	if( obj != null )
	{
		obj.value = text;
	
		alert( 'Set ' + obj + ' to ' + text  );
		
		return true;
	}
	
	alert( 'Error: ' + inputbox + ' could not be found.' );
	
	return false;
}

function onSortChanged(select)
{
	if( select != null )
	{
		var obj_index = select.selectedIndex;
		var obj_value = select.options[obj_index].value;
		
		if( obj_value != -1 )
		{
			window.location = obj_value;
		}
		
		return true;
	}
	
	alert( 'Failed to change sorting methods.' );
	
	return false;
}

function initSearchText()
{
	var searchtext=document.getElementById('psearchterm');
	
	if( searchtext==null )
	{
		alert( 'Error reading input field: psearchterm' );
	}
	else
	{
		
		if( 1 > searchtext.value.length )
		{
			// Set text value of search input box
			searchtext.value="";				
		}
		
		// Focus and select search text
		searchtext.focus();
		
		return true;
	}
	
	return false;
}

// addToBasket constants
var wasClicked = false;

function addToBasket(form, qty, pid, storeid, custid, button)
{
	if( wasClicked )
		return false;
			
	if( form == null )
		return false;
	
	if( qty == null )
		qty = form.qty.value;
	
	if( pid == null )
		pid = form.pid.value;
	
	if( storeid == null )
		storeid = form.prodstoreid.value;
		
	if( custid == null )
		custid = form.customerid.value;
		
	if( button == null )
		button = document.getElementById('addtobasket');
	
	if( button != null )
		buttonSwap( button );

	var xmlhttp = false;
	var status = false;
	var url = "?qty=" + qty + "&custid=" + custid + "&pid=" + pid + "&storeid=" + storeid + "&prodstoreid=" + storeid;
	
	wasClicked = true;
	//window.location = "#top";
	
	if( ! ( isNumeric( qty ) ) )
	{
		alert( 'Your quantity must be a positive number.\nPlease try again.' );
		
		var qtyField = document.getElementById('qty');
		
		qtyField.focus();
		qtyField.select();
		
		wasClicked = false;
		buttonSwap( button );
		
		return false;
	}
	
	try {
		xmlhttp = new ActiveXObject( "Msxml2.XMLHTTP" );
	}
	catch(e) {
		try {
			xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );
		} catch(E) {
			xmlhttp = false;
		}
	}
	
	if( !xmlhttp && typeof XMLHttpRequest != 'undefined' )
	{
		try {
			xmlhttp = new XMLHttpRequest();
		}
		catch (e) {
			xmlhttp=false;
		}
	}
	
	if( !xmlhttp && window.createRequest ) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	
	try {
		xmlhttp.open("GET","/functions/addtobasket.ihtml" + url,true );
	}
	catch(EX)
	{
		wasClicked = false;
		buttonSwap( button );
		
		form.submit();
	}
	
	xmlhttp.onreadystatechange = function()
	{
		if( xmlhttp.readyState == 4 )
		{
			var result = xmlhttp.responseText;
			//document.getElementById('basketprice').innerHTML = result;
			
			//alert( 'Your basket has been updated.  Your total is now' + result + '.  Click the Checkout button on the top right when you are finished shopping.' );

			$('#popupContent').html(result);

			dopopup();

			$('#basketprice').html($('#returnedbprice').html());
			
			wasClicked = false;
			buttonSwap( button );
		}
	}

	xmlhttp.send(null);
	
	// Prevent form submission
	return false;
}

// constants to define the title of the alert and button text.
var ALERT_TITLE = "Thank you!";
var ALERT_BUTTON_TEXT = "Ok";

// over-ride the alert method only if this a newer browser.
// Older browser will see standard alerts
if(document.getElementById)
{
	window.alert = function(txt) {
		createCustomAlert(txt);
	}
}

function createCustomAlert(txt) {
	// shortcut reference to the document object
	d = document;

	// if the modalContainer object already exists in the DOM, bail out.
	if(d.getElementById("modalContainer")) return;

	// create the modalContainer div as a child of the BODY element
	mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
	mObj.id = "modalContainer";
	
	// make sure its as tall as it needs to be to overlay all the content on the page
	mObj.style.height = document.documentElement.scrollHeight + "px";

	// create the DIV that will be the alert 
	alertObj = mObj.appendChild(d.createElement("div"));
	alertObj.id = "alertBox";
	
	// MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
	if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + "px";

	// center the alert box
	alertObj.style.left = ( ( d.body.clientWidth - alertObj.offsetWidth ) / 2 ) + "px";
	alertObj.style.top = ( ( d.body.clientHeight  ) / 2 ) - 100 + "px";
	
	// create an H1 element as the title bar
	h1 = alertObj.appendChild(d.createElement("h1"));
	h1.appendChild(d.createTextNode(ALERT_TITLE));

	// create a paragraph element to contain the txt argument
	msg = alertObj.appendChild(d.createElement("p"));
	msg.appendChild(d.createTextNode(txt));

	// create button div
	btnDiv = alertObj.appendChild(d.createElement("div"));
	btnDiv.id = "proceedBox";
	btnDiv.align = "center";
	
	// create an img element to use as the confirmation button.
	btn = btnDiv.appendChild(d.createElement("img"));
	btn.src = "images/proceed.png";
	
	// set up the onclick event to remove the alert when the anchor is clicked
	btn.onclick = function()
	{
		removeCustomAlert();
		return false;
	}
}

// removes the custom alert from the DOM
function removeCustomAlert() {
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
}

function isNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;


	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	
	return IsNumber;
}

// Global variable
var swapped = false;

// Swap function for add to basket button
function buttonSwap(buttonid)
{
	// Variables
	var button = buttonid;
	var result = false;
	
	// Error checking
	if( button == null )
		return result;
		
	// Swap images
	if( ! ( swapped ) )
	{
		button.src = "images/pleasewait.png";
		swapped = true;
	}
	else
	{
		button.src = "images/addtobasket.png";
		swapped = false;
	}
	
	// Return
	result = true;
	
	return result;
}
//-->
