var nMaxPages = 0;
var nCurrentPage = 1;
var nMaxItems = 0;
var nCurrentItem = 1;
var nResultsPerPage = 8;
var bCancelLoad = false;
var txtKeywords;
var txtOscID;
var cLastURL = document.location.href;
var bRedirecting = false;
var nRetryCounter = 0;
var errorCode = null;

var bDebugCheckLocationChanged = false;

Ext.onReady(function()
{
	var aHttpXml = [
	'hxr = new XMLHttpRequest()',
	'hxr = new ActiveXObject(MSXML2.XMLHTTP.3.0)',
	'hxr = new ActiveXObject(MSXML2.XMLHTTP)',
	'hxr = new ActiveXObject(Microsoft.XMLHTTP)'];

	var hxr = null;

	for (var x = 0; x < aHttpXml.length; x++)
	{
		try
		{
		eval(aHttpXml[x]);
		break;
		} catch (ex)
		{}
	}

	//xhr = new XmlHttpRequest();

	if (hxr == null)
	{
		//alert("No XHR available.");
		return;
	}

	// BMS 27/11/2007 - Alters the submission of the form to work with the AJAX search. Default is to perform older php search to cater for javascript-disabled browsers.
	var formSearch = document.getElementsByTagName("form");
	//formSearch[0].setAttribute("onsubmit", "return searchForm_Submit()");
	formSearch[0].onsubmit = searchForm_Submit;
	formSearch[0].setAttribute("action", null);

	
	var oURL = this.MpUrlDecode(document.location.href);

	if(oURL != null && oURL.HashParams != null && oURL.HashParams.keywords != null)
	{
		
		var txtTrimmedKeys = oURL.HashParams["keywords"].replace(/^\s+|\s+$/g, '');
		if(txtTrimmedKeys.length == 0 || oURL.HashParams["pagenumber"] == 'NaN')
		{
			return;
		}
		txtKeywords = oURL.HashParams["keywords"];
		nCurrentPage = parseInt(oURL.HashParams["pagenumber"]);
		document.getElementById("txtKeywords").value = txtKeywords;
		RequestSearch();
	}

	if(oURL != null && oURL.strBaseHref != null)
	{
		var strPage = window.location.pathname;
		if(strPage == "/shopping_cart.php")
		{
			//var aContinue = document.getElementById("Continue");
			//aContinue.onclick = ContinueShopping;
		}
	}


	if(txtKeywords == null &&  oURL.Params != null && oURL.Params["keywords"] != null)
	{
		txtKeywords = oURL.Params["keywords"];
		document.getElementById("txtKeywords").value = txtKeywords;
	}

	
	//document.getElementById("txtKeywords").focus();

	setTimeout("CheckLocationChanged()", 20);
	
	


});

// BMS 21/05/2008 - Added a delay between retry attempts on error.
function ResourceError(eCode, f, g)
{
	//alert("search error.");
	//return;

	if (eCode.ErrorCode == 30)
	{
		window.location.reload();
		return;
	}

	errorCode = eCode;

	if (!confirm("A communications error occurred, or you cancelled the transaction. Do you wish to retry the search?"))
	{
		clickClose();
		return;
	}

	//setTimeout("ResourceErrorRetry()",3000);
	setTimeout("ResourceErrorRetry()",100);
}

function ResourceErrorRetry()
{
	var errorParams = {};
	for(var param in errorCode.Params)
	{
		errorParams[param] = errorCode.Params[param];
	}
	errorParams.responseText = errorCode.Response.responseText;
	errorParams.statusText = errorCode.Response.statusText;
	errorParams.status = errorCode.Response.status;
	errorParams.ErrorCode = errorCode.ErrorCode;
	errorParams.ErrorMessage = errorCode.ErrorMessage;

	var mpxr = new MicroPacific.Net.MpXmlResponse(
		errorParams,
		"search_request_error_report.php",
		this,
		ErrorReportRequestFailed,
		ErrorReportRequestFailed,
		ErrorReportRequestFailed
	);
	clickClose();


	// BMS 11/06/2008 - If errorcode is 20, a general error has occurred. Do no more searches.
	if(errorCode.ErrorCode == 20)
	{
		// MDR 31/07/2008 - Not a comms error, don't retry
		//return false;
		nRetryCounter = 3;
	}

	// BMS 04/02/2008 - Handles HTTP 204 (no results returned) errors. Will retry search until nRetryCounter == 3.
	// BMS 29/02/2008 - Retry for everything but classic search request (ErrorCode 10).
	if(errorCode.ErrorCode != 10)
	{
		if(nRetryCounter < 3)
		{
			++nRetryCounter;
			RequestSearch();
			return;
		}
	}

	alert("search failed.");
	return;

	//if (errorCode.ErrorCode == 10)
	//{
	// MDR 05/12/2007 - Redirect to classic search
	var oURL = this.MpUrlDecode(document.location.href);
	//oURL.strBaseHref + Ext.urlEncode(oURL.Params);
	oURL.strBaseHref = "/advanced_search_result.php?";
	if(errorParams["keywords"].length > 0)
	{
		oURL.Params["keywords"] = errorParams["keywords"];
	}
	oURL.Params["ajax_search_error"] = errorCode.ErrorCode;
	strRedirect = oURL.strBaseHref + Ext.urlEncode(oURL.Params);
	document.location = strRedirect;
	return;
	//}


}


function ContinueShopping()
{
	window.history.go(-1);
	return false;
}
function searchForm_Submit()
{
	nCurrentPage = 1;

	// BMS 03/10/2007 - get the keywords and session ID from the POST parameters.
	txtKeywords = document.getElementById("txtKeywords").value;
	
   
	// BMS 09/11/2007 - tests the keywords for content.
	var txtTrimmedKeys = txtKeywords.replace(/^\s+|\s+$/g, '');
	if(txtTrimmedKeys.length == 0)
	{
		return false;
	}
	
	RequestSearch();
	return false;
}

function RequestSearch()
{

	var MpApp = MpApplication.GetApplication();
	txtOscID = document.getElementById("txtOscID").value;
	var _mpldr = this;
	// BMS 11/06/2008 - If keywords or oscsid are empty, send an error back and cancel search.
	if(txtKeywords.length == 0 || txtOscID.length == 0)
	{
		var errorParams = {};
		
		errorParams.responseText = "";
		errorParams.statusText = "RequestSearch() Error";
		errorParams.status = "-1";
		errorParams.ErrorCode = "-1";
		errorParams.ErrorMessage = "txtKeywords.length == 0 || txtOscID.length == 0";

		
		var mpxr = new MicroPacific.Net.MpXmlResponse(
			errorParams,
			"search_request_error_report.php",
			this,
			ErrorReportRequestFailed,
			ErrorReportRequestFailed,
			ErrorReportRequestFailed
		);
		clickClose();
		return;
	}

	// BMS 08/10/2007 - Gets the correct value for the amount of results to return for this page.
	// If the page is the last one, this amount can be different to the one defined as the nResultsPerPage.
	resultsPerPage = nResultsPerPage;
	var cDataUrl = "advanced_search_request.php";
	var pParams = {
		keywords: txtKeywords,
		osCsid: txtOscID,
		currentPage: nCurrentPage,
		nResultsPerPage: resultsPerPage,
		retryCounter: nRetryCounter
	};

	// BMS 26/10/2007 - changes the "xItems" link to the shopping cart.
	ShoppingCartLink(txtKeywords, nCurrentPage);

	// BMS 27/11/2007 - changes the value of the search box to the search keywords.
	document.getElementById("txtKeywords").value = txtKeywords;
	// BMS 03/10/2007 - loads a gif with text to indicate the search is taking place.
	//MpDebug("Loading Screen");
	bCancelLoad = false;
	LoadingScreen();
	var mpxr = new MicroPacific.Net.MpXmlResponse(
		pParams,
		cDataUrl,
		this,
		DataLoaded,
		ResourceError,
		ResourceError
	);

	
};


function ErrorReportRequestFailed(errorCode)
{
	
}


function CheckLocationChanged()
{
	if (bDebugCheckLocationChanged)
	{
		//console.log("Set debug point here.");
	}

	if(bRedirecting)
	{
		cLastURL = document.location.href;
		return;
	}
	if(cLastURL != document.location.href)
	{
		cLastURL = document.location.href;
		var oURL = this.MpUrlDecode(document.location.hash);

		if(oURL != null && oURL.HashParams != null)
		{
			var txtTrimmedKeys = oURL.HashParams["keywords"].replace(/^\s+|\s+$/g, '');
			if(txtTrimmedKeys.length == 0 || oURL.HashParams["pagenumber"] == 'NaN')
			{
				return;
			}
			txtKeywords = oURL.HashParams["keywords"];
			nCurrentPage = parseInt(oURL.HashParams["pagenumber"]);
			RequestSearch();
		}
		else if(oURL.HashParams == null)
		{
			this.closeSearchResults();
		}
	}
	setTimeout("CheckLocationChanged()", 20);
}


function DataLoaded(xmlDataSchema)
{
	// BMS 29/02/2008 - Reset the retry counter variable.
	nRetryCounter = 0;


	// BMS 03/10/2007 - Remove the loading screen scrolling event.
	window.onscroll = null;

	
	if(bCancelLoad)
	{
		bCancelLoad = false;
		return;
	}
	
	var divLoading = document.getElementById("divLoading");
	document.body.removeChild(divLoading);

	
	// BMS 03/10/2007 - Any current data attached to the search results must be removed.
	var divContent = document.getElementById("divContent");
	var divSearchUI = document.getElementById("divSearchUI");
	var divSearchResults = document.getElementById("divSearchResults");
	if(document.getElementById("tableSearchResults"))
	{
		var tableSearchResults = document.getElementById("tableSearchResults");
		divSearchResults.removeChild(tableSearchResults);
	}	
	divSearchResults.style.display = 'block';

	var oTable = document.createElement("table");
	var oTBody = document.createElement("tbody");
	var trSearchHead = document.createElement("tr");
	var trSearchFoot = document.createElement("tr");
	var count = 0;
	var xmlProducts = xmlDataSchema.getElementsByTagName("product");
	var img_info_button = xmlDataSchema.getElementsByTagName("images")[0].getElementsByTagName("img_info_src")[0].firstChild.nodeValue;
	var img_buy_button = xmlDataSchema.getElementsByTagName("images")[0].getElementsByTagName("img_buy_now_src")[0].firstChild.nodeValue;



	oTable.setAttribute("id", "tableSearchResults");
	oTable.setAttribute("width", "100%");


	//Print the header of the searchUI
	PrintHeader(xmlDataSchema, trSearchHead, {bSearchTerms: true, bNResults: true, bNav: true, bNoResults: true });
	oTBody.appendChild(trSearchHead);
	//Print the list of results
	for(var i=0; i < xmlProducts.length; ++i)
	{
		var trSeparator = document.createElement("tr");
		var tdSeparator = document.createElement("td");
		var imgSeparator = document.createElement("img");
		var trRow = document.createElement("tr");
		
		tdSeparator.setAttribute("class", "sepbgh");
		tdSeparator.setAttribute("className", "sepbgh");
		tdSeparator.setAttribute("colspan", "2");
		
		imgSeparator.setAttribute("src", "images/m25.gif");
		imgSeparator.setAttribute("width", "480");
		imgSeparator.setAttribute("height", "1");

		PrintRow(xmlProducts[i], trRow, img_info_button, img_buy_button);
		
		
		oTBody.appendChild(trRow);
		tdSeparator.appendChild(imgSeparator);
		trSeparator.appendChild(tdSeparator);
		if(i != (xmlProducts.length -1))
		{
			oTBody.appendChild(trSeparator);
		}
		++count;
	}
	var trSearchHead = document.createElement("tr");
	PrintHeader(xmlDataSchema, trSearchHead, {bSearchTerms: false, bNResults: true, bNav: true, bNoResults: false });
	if(!bCancelLoad)
	{
		oTBody.appendChild(trSearchHead);
	}
	oTable.appendChild(oTBody);
	divContent.style.display = 'none';
	divSearchResults.appendChild(oTable);
	
	// BMS 20/11/2007 - The assignment of both the cLastURN and the hash value of the window's location I believe cannot be interrupted by the 
	// setTimeout function.
	bRedirecting = true;
	window.location.hash = "keywords=" + txtKeywords + "&pagenumber=" + nCurrentPage;
	cLastURL = window.location.href;
	setTimeout("CheckLocationChanged()", 20);
	bRedirecting = false;

	// BMS 29/02/2008 - Forces scroll to the top of the page.
	window.scroll(0, 0);

}

function RemoveSearchResults()
{
	var tableSearchResults = document.getElementById("tableSearchResults");
	divSearchResults.removeChild(tableSearchResults);
}

function PrintHeader(xmlDataSchema, trRow, oOptions)
{ 
	var tableSearchHead = document.createElement("table");
	var tbodySearchHead = document.createElement("tbody");
	var tdRowHeader = document.createElement("td");
	var tdHrule1 = document.createElement("td");
	var tdHrule2 = document.createElement("td");
	
	nCurrentItem = nCurrentPage * nResultsPerPage - nResultsPerPage + 1;
	nMaxThisPage = nCurrentItem + nResultsPerPage -1;
	nMaxItems = xmlDataSchema.getElementsByTagName("results_count")[0].firstChild.nodeValue;
	nMaxPages = Math.round(nMaxItems / nResultsPerPage);
	if(nMaxItems % nResultsPerPage == 0)
	{
		--nMaxPages;
	}
	if(nMaxItems == 0)
	{
		if(oOptions.bNoResults)
		{
			var trNoResults = document.createElement("tr");
			var tdNoResults = document.createElement("td");

			tdNoResults.setAttribute("class", "pageHeading3");
			tdNoResults.innerHTML = "Sorry, there are no results for your search.";

			trNoResults.appendChild(tdNoResults);
			tbodySearchHead.appendChild(trNoResults);
			tableSearchHead.appendChild(tbodySearchHead);
			tdRowHeader.appendChild(tableSearchHead);
			trRow.appendChild(tdRowHeader);
		}
		return;
	}
	if(nMaxItems%8 < 4)
	{
		nMaxPages++;
	}
	if(nCurrentItem + nResultsPerPage > nMaxItems)
	{
		nMaxThisPage = nMaxItems;
	}


	tableSearchHead.setAttribute("class", "SearchHead");
	tableSearchHead.setAttribute("className", "SearchHead");


	// BMS 08/10/2007 - Include the search term information.
	if(oOptions.bSearchTerms)
	{
		var trSearchHead = document.createElement("tr");
		var tdSearchHead = document.createElement("td");
		var spanCloseSearch = document.createElement("span");
		var imgCloseSearch = document.createElement("img");

		tdSearchHead.setAttribute("class", "sc1");
		tdSearchHead.setAttribute("className", "sc1");

		spanCloseSearch.setAttribute("class", "closeSearch");
		spanCloseSearch.setAttribute("className", "closeSearch");
		spanCloseSearch.onmouseover = mouseoverSearchClose;
		spanCloseSearch.onclick = closeSearchResults;

		imgCloseSearch.setAttribute("src", "images/close_button.gif");
		imgCloseSearch.setAttribute("alt", "close");

		spanCloseSearch.appendChild(imgCloseSearch);
		tdSearchHead.appendChild(spanCloseSearch);
		tdSearchHead.appendChild(document.createTextNode("Displaying search results for '" +  document.getElementById("txtKeywords").value + "':"));
		trSearchHead.appendChild(tdSearchHead);
		tbodySearchHead.appendChild(trSearchHead);
	}
	// BMS 08/10/2007 - Include the number of results information.
	if(oOptions.bNav)
	{
		var trNavigation = document.createElement("tr");
		var trHrule1 = document.createElement("tr");
		var trHrule2 = document.createElement("tr");
		var tdNavigation = document.createElement("td");

		var cComma = ", ";

		tdNavigation.setAttribute("class", "navigation");
		tdNavigation.setAttribute("className", "navigation");

		tdHrule1.setAttribute("class", "sepbgh");
		tdHrule1.setAttribute("className", "sepbgh");
		
		tdHrule2.setAttribute("class", "sepbgh");
		tdHrule2.setAttribute("className", "sepbgh");

		trHrule1.appendChild(tdHrule1);
		trHrule2.appendChild(tdHrule2);
		// BMS 03/10/2007 - create the "first" and "prev" links if there is a page to travel to before the current page, otherwise create the text only.
		if((nCurrentPage - 1) > 0)
		{
			var aPageLink = document.createElement("a");
			aPageLink.href = window.location.href;
			aPageLink.setAttribute("id", "1");
			aPageLink.onclick = redirect;
			aPageLink.setAttribute("class", "navigation");
			aPageLink.setAttribute("className", "navigation");
			aPageLink.innerHTML = "[&#171; first] ";
			tdNavigation.appendChild(aPageLink);

			var aPageLink = document.createElement("a");
			aPageLink.href = window.location.href;
			aPageLink.setAttribute("id", (nCurrentPage - 1));
			aPageLink.onclick = redirect;
			aPageLink.setAttribute("class", "navigation");
			aPageLink.setAttribute("className", "navigation");
			aPageLink.innerHTML = " [&#139; prev] ";
			tdNavigation.appendChild(aPageLink);
		}
		else
		{
			var spanNavBack = document.createElement("span");
			spanNavBack.innerHTML = "[&#171; first] [&#139; prev] ";
			tdNavigation.appendChild(spanNavBack);
		}


		
		// BMS 03/10/2007 - Check if there are "..." required before the first navigational page number
		if((nCurrentPage - 3) > 1 && nMaxPages > 7)
		{
			tdNavigation.appendChild(document.createTextNode(" ... "));
		}

		// BMS 03/10/2007 - Create the previous three (if they exist) page numbers and appropriate links
		var navNumbers;
		// BMS 08/10/2007 - Calculates if extra navigational links before the current page should be printed to compensate for the lack of navigational links after the current page.
		var remainderHigh = 3;
		var remainderLow = 3;
		for(var i = 1; i < 4; ++i)
		{
			if((nCurrentPage + i) <= nMaxPages)
			{
				--remainderHigh;
			}
		}
		for(var i = 3; i > 0; --i)
		{
			if((nCurrentPage - i) > 0)
			{
				--remainderLow;
			}
		}
		for(var i = 3; i > (0 - remainderHigh); --i)
		{
			if((nCurrentPage - i - remainderHigh) > 0)
			{
				var aPageLink = document.createElement("a");
				aPageLink.href = window.location.href - window.location.hash;
				aPageLink.setAttribute("id", (nCurrentPage - i - remainderHigh));
				aPageLink.onclick = redirect;
				aPageLink.setAttribute("class", "navigation");
				aPageLink.setAttribute("className", "navigation");
				aPageLink.innerHTML = " " + (nCurrentPage - i - remainderHigh) + cComma;
				tdNavigation.appendChild(aPageLink);
			}
		}
		// BMS 03/10/2007 - print the current page number.
		tdNavigation.appendChild(document.createTextNode(nCurrentPage));

		// BMS 03/10/2007 - print the next three (if they exist) page numbers and appropriate links
		for(var i = 1; i < (4 + remainderLow); ++i)
		{
			if((nCurrentPage + i) <= nMaxPages)
			{
				
				var aPageLink = document.createElement("a");
				aPageLink.href = window.location.href;
				aPageLink.setAttribute("id", (nCurrentPage + i));
				aPageLink.onclick = redirect;
				aPageLink.setAttribute("class", "navigation");
				aPageLink.setAttribute("className", "navigation");
				aPageLink.innerHTML = ", " + (nCurrentPage + i);
				tdNavigation.appendChild(aPageLink);
			}
		}
		if((nCurrentPage + 3) < nMaxPages && nMaxPages > 7)
		{
			tdNavigation.appendChild(document.createTextNode(" ... "));
		}

		// BMS 03/10/2007 - create the "next" and "last" links if there is a page to travel to after the current page, otherwise create the text only.
		if((nCurrentPage + 1) <= nMaxPages)
		{
			var aPageLink = document.createElement("a");
			aPageLink.href = window.location.href;
			aPageLink.setAttribute("id", (nCurrentPage + 1));
			aPageLink.onclick = redirect;
			aPageLink.setAttribute("class", "navigation");
			aPageLink.setAttribute("className", "navigation");
			aPageLink.innerHTML = "&nbsp;[next &#155;] ";
			tdNavigation.appendChild(aPageLink);

			var aPageLink = document.createElement("a");
			aPageLink.href = window.location.href;
			aPageLink.setAttribute("id", nMaxPages);
			aPageLink.onclick = redirect;
			aPageLink.setAttribute("class", "navigation");
			aPageLink.setAttribute("className", "navigation");
			aPageLink.innerHTML = " [last &#187;] ";
			tdNavigation.appendChild(aPageLink);
		}
		else
		{
			var spanNavBack = document.createElement("span");
			spanNavBack.innerHTML = "&nbsp;[next &#155;] [last &#187;] "
			tdNavigation.appendChild(spanNavBack);
		}
		
		trNavigation.appendChild(tdNavigation);
		tbodySearchHead.appendChild(trHrule1);
		tbodySearchHead.appendChild(trNavigation);
		if(!oOptions.bNResults)
		{
			tbodySearchHead.appendChild(trHrule2);
		}
	}
	if(oOptions.bNResults)
	{
		var trNResults = document.createElement("tr");
		var tdNResults = document.createElement("td");
		tdNResults.setAttribute("class", "nResults");
		tdNResults.setAttribute("className", "nResults");
		tdNResults.innerHTML = "results " + nCurrentItem + " - " + nMaxThisPage + " (" + nMaxItems + " total)";

		trNResults.appendChild(tdNResults);
		tbodySearchHead.appendChild(trNResults);
		tbodySearchHead.appendChild(trHrule2);
	}
	
	tableSearchHead.appendChild(tbodySearchHead);
	tdRowHeader.appendChild(tableSearchHead);
	trRow.appendChild(tdRowHeader);
}

function PrintRow(xmlProducts, trSuperRow, img_info_button, img_buy_button)
{
	var products_id = xmlProducts.getElementsByTagName("products_id")[0].firstChild.nodeValue;
	var products_name = xmlProducts.getElementsByTagName("products_name")[0].firstChild.nodeValue;
	var products_link = xmlProducts.getElementsByTagName("products_link")[0].firstChild.nodeValue;
	var products_image = xmlProducts.getElementsByTagName("products_image_src")[0].firstChild.nodeValue;
	var products_image_w = xmlProducts.getElementsByTagName("products_image_w")[0].firstChild.nodeValue;
	var products_image_h = xmlProducts.getElementsByTagName("products_image_h")[0].firstChild.nodeValue;
	var is_superceded = xmlProducts.getElementsByTagName("is_superceded")[0].firstChild.nodeValue;
	var products_model = xmlProducts.getElementsByTagName("products_model")[0].firstChild.nodeValue;
	var products_ex_price = xmlProducts.getElementsByTagName("products_ex_price")[0].firstChild.nodeValue;
	var products_inc_price = xmlProducts.getElementsByTagName("products_inc_price")[0].firstChild.nodeValue;
	var products_rrp_price = xmlProducts.getElementsByTagName("products_rrp_price")[0].firstChild.nodeValue;
	var products_buy_button_href = xmlProducts.getElementsByTagName("products_buy_button_href")[0].firstChild.nodeValue;
	var products_description = "";
	if(xmlProducts.getElementsByTagName("products_description")[0].childNodes.length != 0)
	{
		products_description = xmlProducts.getElementsByTagName("products_description")[0].firstChild.nodeValue;
	}
	

	var tableRow = document.createElement("table");
	var tableProdData = document.createElement("table");
	var tablePriceData = document.createElement("table");
	var tbodyProdData = document.createElement("tbody");
	var tbodyRow = document.createElement("tbody");
	var tbodyPriceData = document.createElement("tbody");
	var trRow = document.createElement("tr");
	var trImageSpace = document.createElement("tr");
	var trProdHeader = document.createElement("tr");
	var trDescriptionSpace = document.createElement("tr");
	var trProdDescription = document.createElement("tr");
	var trPriceData = document.createElement("tr");
	var trPrice = document.createElement("tr");
	var tdRow = document.createElement("td");
	var tdImageSpace = document.createElement("td");
	var tdImage = document.createElement("td");
	var tdProdData = document.createElement("td");
	var tdProdHeader = document.createElement("td");
	var tdDescriptionSpace = document.createElement("td");
	var tdProdDescription = document.createElement("td");
	var tdPriceData = document.createElement("td");
	var tdPrice = document.createElement("td");
	var tdProdButton = document.createElement("td");
	var aImageLink = document.createElement("a");
	var aProdModel = document.createElement("a");
	var aProdName = document.createElement("a");
	var aInfoButton = document.createElement("a");
	var aBuyNowButton = document.createElement("a");
	var imgPicture = document.createElement("img");
	var imgInfoButton = document.createElement("img");
	var imgBuyNowButton = document.createElement("img");
	var divPrice1 = document.createElement("div");
	var divPrice2 = document.createElement("div");
	var divPrice3 = document.createElement("div");
	var spanProdHeader = document.createElement("span");
	var spanPriceDollar1 = document.createElement("span");
	var spanPriceDollar2 = document.createElement("span");
	var spanPriceDollar3 = document.createElement("span");
	var spanPriceEx = document.createElement("span");
	var spanPriceInc = document.createElement("span");
	var spanPriceRrp = document.createElement("span");
	var brPrice1 = document.createElement("br");
	var brPrice2 = document.createElement("br");
	var txtPrice1 = document.createTextNode(products_ex_price);
	var txtPrice2 = document.createTextNode(products_inc_price);
	var txtPrice3 = document.createTextNode(products_rrp_price);
	
	// BMS 18/09/2007 - Assign attributes for the container row table.
	tableRow.setAttribute("class", "noPadding");
	tableRow.setAttribute("className", "noPadding");
	
	// BMS 18/09/2007 - Create the blank space cell/row.
	tdImageSpace.setAttribute("class", "cellSpace");
	tdImageSpace.setAttribute("className", "cellSpace");

	// BMS 18/09/2007 - Set attributes for the image cell.
	tdImage.setAttribute("width", "100");
	tdImage.setAttribute("align", "center");
	tdImage.setAttribute("class", "vAlignTop");
	tdImage.setAttribute("className", "vAlignTop");

	// BMS 18/09/2007 - Set attributes for the image anchor.
	var cProdInfo = products_link;
	// BMS 01/07/2008 - Check if osCsid is already in link.
	if(cProdInfo.indexOf("osCsid") == -1 && txtOscID.length > 0)
	//if(txtOscID.length > 0)
	{
		//cProdInfo += "&osCsid=" + txtOscID;
	}
	aImageLink.setAttribute("href", cProdInfo);
	//aImageLink.setAttribute("target", "_BLANK");
	
	//imgPicture.setAttribute("width", products_image_w);
	//imgPicture.setAttribute("height", products_image_h);
	//imgPicture.setAttribute("src", products_image);
	
	// BMS 18/09/2007 - Working on the second cell of the row, the ProdData table, row and cell.
	tableProdData.setAttribute("class", "noPadding");
	tableProdData.setAttribute("className", "noPadding");
	tableProdData.setAttribute("width", "100%");
	tableProdData.setAttribute("height", "100%");
	tableProdData.setAttribute("class", "prod");
	tableProdData.setAttribute("className", "prod");

	tdProdHeader.setAttribute("align", "left");
	tdProdHeader.setAttribute("valign", "top");
	tdProdHeader.setAttribute("height", "8");
	tdProdHeader.setAttribute("class", "sc2");
	tdProdHeader.setAttribute("className", "sc2");

	aProdModel.setAttribute("href", cProdInfo);
	//aProdModel.setAttribute("target", "_BLANK");
	aProdModel.setAttribute("class", "ml5");
	aProdModel.setAttribute("className", "ml5");
	

	spanProdHeader.setAttribute("class", "blue");
	spanProdHeader.setAttribute("className", "blue");
	

	aProdName.setAttribute("href", cProdInfo);
	//aProdName.setAttribute("target", "_BLANK");
	aProdName.setAttribute("class", "ml5");
	aProdName.setAttribute("className", "ml5");
	
	// BMS 18/09/2007 - Create the blank cell/row between header and description.
	tdDescriptionSpace.setAttribute("height", "2");

	// BMS 18/09/2007 - Set attributes for the product description.
	tdProdDescription.setAttribute("height", "0");
	tdProdDescription.setAttribute("class", "productDescription");
	tdProdDescription.setAttribute("className", "productDescription");


	tdPriceData.setAttribute("align", "left");
	tdPriceData.setAttribute("height", "15");
	tdPriceData.setAttribute("class", "sc2");
	tdPriceData.setAttribute("className", "sc2");

	tablePriceData.setAttribute("class", "fill");
	tablePriceData.setAttribute("className", "fill");

	tdPrice.setAttribute("width", "120");
	tdPrice.setAttribute("class", "sc2");
	tdPrice.setAttribute("className", "sc2");

	spanPriceDollar1.setAttribute("class", "currency_symbol");
	spanPriceDollar1.setAttribute("className", "currency_symbol");

	spanPriceDollar2.setAttribute("class", "currency_symbol");
	spanPriceDollar2.setAttribute("className", "currency_symbol");

	spanPriceDollar3.setAttribute("class", "currency_symbol");
	spanPriceDollar3.setAttribute("className", "currency_symbol");

	// BMS 25/09/2007 - Set attributes for the Info and BuyNow buttons.
	tdProdButton.setAttribute("align", "left");
	aInfoButton.setAttribute("href", cProdInfo);
	//aInfoButton.setAttribute("target", "_BLANK");
	aBuyNowButton.setAttribute("href", products_buy_button_href + "&keywords=" + txtKeywords + "&pagenumber=" + nCurrentPage);

	tableRow.setAttribute("width", "100%");


	// BMS 18/09/2007 - Assign all relevent innerHTML data
	aImageLink.innerHTML = products_image;
	aProdModel.innerHTML = products_model;
	aProdName.innerHTML = products_name;
	tdProdDescription.innerHTML = products_description;
	var stringbuilder;
	spanProdHeader.innerHTML = "&#187;";
	spanPriceDollar1.innerHTML = "$";
	spanPriceEx.innerHTML = "&nbsp;ex GST";
	spanPriceDollar2.innerHTML = "$";
	spanPriceInc.innerHTML = "&nbsp;inc GST";
	spanPriceDollar3.innerHTML = "$";
	spanPriceRrp.innerHTML = "&nbsp;RRP";
	aInfoButton.innerHTML = img_info_button;
	aBuyNowButton.innerHTML = img_buy_button;


	//BMS 18/09/2007 - Form the HTML structure through appending all children.

	trImageSpace.appendChild(tdImageSpace);
	tdImage.appendChild(aImageLink);
	trRow.appendChild(tdImage);
	tdProdHeader.appendChild(aProdModel);
	tdProdHeader.appendChild(document.createTextNode(" "));
	tdProdHeader.appendChild(spanProdHeader);
	tdProdHeader.appendChild(document.createTextNode(" "));
	tdProdHeader.appendChild(aProdName);
	trProdHeader.appendChild(tdProdHeader);
	trDescriptionSpace.appendChild(tdDescriptionSpace);
	trProdDescription.appendChild(tdProdDescription);
	tdProdButton.appendChild(document.createElement("br"));
	tdProdButton.appendChild(aInfoButton);
	if(is_superceded == 1)
	{
		divPrice1.appendChild(spanPriceDollar1);
		divPrice1.appendChild(txtPrice1);
		divPrice1.appendChild(spanPriceEx);
		divPrice1.appendChild(brPrice1);
		divPrice2.appendChild(spanPriceDollar2);
		divPrice2.appendChild(txtPrice2);
		divPrice2.appendChild(spanPriceInc);
		if(products_rrp_price > 0)
		{
			//divPrice3.appendChild(brPrice2);
			divPrice3.appendChild(spanPriceDollar3);
			divPrice3.appendChild(txtPrice3);
			divPrice3.appendChild(spanPriceRrp);
		}
		tdProdButton.appendChild(document.createTextNode(" "));
		tdProdButton.appendChild(aBuyNowButton);
	}
	tdPrice.appendChild(divPrice1);
	tdPrice.appendChild(divPrice2);
	tdPrice.appendChild(divPrice3);
	trPrice.appendChild(tdPrice);
	trPrice.appendChild(tdProdButton);
	tbodyPriceData.appendChild(trPrice);
	tablePriceData.appendChild(tbodyPriceData);
	tdPriceData.appendChild(tablePriceData);
	trPriceData.appendChild(tdPriceData);
	tbodyProdData.appendChild(trProdHeader);
	tbodyProdData.appendChild(trDescriptionSpace);
	tbodyProdData.appendChild(trProdDescription);
	tbodyProdData.appendChild(trPriceData);
	tableProdData.appendChild(tbodyProdData);
	tdProdData.appendChild(tableProdData);
	trRow.appendChild(tdProdData);
	tbodyRow.appendChild(trImageSpace);
	tbodyRow.appendChild(trRow);
	tableRow.appendChild(tbodyRow);
	tdRow.appendChild(tableRow);
	trSuperRow.appendChild(tdRow);

	
}

function LoadingScreen()
{
	
	var divLoading = document.createElement("div");
	var tableMain = document.createElement("table");
	var theadMain = document.createElement("thead");
	var tbodyMain = document.createElement("tbody");
	var trMain = document.createElement("tr");
	var trTip = document.createElement("tr");
	var trClose = document.createElement("tr");
	var tdMain = document.createElement("td");
	var tdTip = document.createElement("td");
	var tdClose = document.createElement("td");
	var imgLoading = document.createElement("img");
	var imgClose = document.createElement("img");

	divLoading.setAttribute("id", "divLoading");

	tableMain.style.marginLeft = "auto";
	tableMain.style.marginRight = "auto";
	tableMain.style.marginTop = "auto";
	tableMain.style.marginBottom = "auto";
	tableMain.style.position = "relative";
	tableMain.style.top = "40%";
	tableMain.setAttribute("class", "imgLoading");
	tableMain.className = "imgLoading";

	imgClose.setAttribute("src", "images/close_button.gif");
	imgClose.setAttribute("alt", "close");
	imgClose.onmouseover = mouseoverClose;
	imgClose.onclick = clickClose;
	imgClose.setAttribute("class", "closeBtn");
	imgClose.className = "closeBtn";

	tdClose.setAttribute("class", "imgLoadingHeader");
	tdClose.setAttribute("width", "400px");

	
	imgLoading.setAttribute("src", "images/loading.gif");
	imgLoading.setAttribute("alt", "loading");
	imgLoading.setAttribute("id", "imgLoading");
	imgLoading.setAttribute("class", "loading");
	imgLoading.className = "loading";

	tdMain.setAttribute("width", "400px");
	tdMain.setAttribute("class", "imgLoading");
	tdMain.className = "imgLoading";

	divLoading.setAttribute("id", "divLoading");
	var browser = whichBrs();
	if(browser == "Safari")
	{
		divLoading.className = "divLoading";
	}
	divLoading.style.position = "absolute";
	divLoading.style.left = "0px";
	var offsetHeight = document.body.scrollTop;
	divLoading.style.top = offsetHeight + divLoading.offsetHeight;
	divLoading.style.width = "100%";
	divLoading.style.height = "100%";
	divLoading.style.textAlign = "center";
	divLoading.appendChild(tableMain);

	tdClose.appendChild(imgClose);
	trClose.appendChild(tdClose);
	tdMain.appendChild(imgLoading);
	tdMain.appendChild(document.createElement("br"));
	tdMain.appendChild(document.createTextNode("Loading..."));
	tdTip.appendChild(document.createTextNode("To exit the search, click \"close\" from the search results page."));
	trMain.appendChild(tdMain);	
	trTip.appendChild(tdTip);
	theadMain.appendChild(trClose);
	tbodyMain.appendChild(trMain);
	tbodyMain.appendChild(trTip);
	tableMain.appendChild(theadMain);
	tableMain.appendChild(tbodyMain);
	divLoading.appendChild(tableMain);
	document.body.appendChild(divLoading);
	window.onscroll = scrollLoadingScreen;
}

function ShoppingCartLink(strKeywords, nCurrentPage)
{
	var aTarget = document.getElementById("cartLink");
	// BMS 26/11/2007 - aTarget2 is the shopping cart button in header.php
	var aTarget2 = document.getElementById("shoppingLink");
	var oURL = this.MpUrlDecode(aTarget.href);
	var oURL2 = this.MpUrlDecode(aTarget2.href);
	var strNewTarget = "";

	if (strKeywords != null)
	{
		strKeywords = escape(strKeywords);
	}

	if(oURL.Params != null)
	{
		oURL.Params["keywords"] = strKeywords;
		oURL.Params["pagenumber"] = nCurrentPage;

		var cParams = Ext.urlEncode(oURL.Params);
		if (cParams.length != 0)
		{
			cParams = "?" + cParams;
		}
		aTarget.href = oURL.strBaseHref + cParams;
	}
	else
	{
		var Params = {};
		Params["keywords"] = strKeywords;
		Params["pagenumber"] = nCurrentPage;
		aTarget.href = oURL.strBaseHref + "?" + Ext.urlEncode(Params);
	}

	if(oURL2.Params != null)
	{
		oURL2.Params["keywords"] = strKeywords;
		oURL2.Params["pagenumber"] = nCurrentPage;

		var cParams = Ext.urlEncode(oURL.Params);
		if (cParams.length != 0)
		{
			cParams = "?" + cParams;
		}
		aTarget2.href = oURL2.strBaseHref + cParams;
	}
	else
	{
		var Params2 = {};
		Params2["keywords"] = escape(strKeywords);
		Params2["pagenumber"] = nCurrentPage;
		aTarget2.href = oURL2.strBaseHref + "?" + Ext.urlEncode(Params2);
	}
}

function redirect(evt)
{
	//nCurrentPage = source.id;
	var evt = window.event||evt;
	var source = evt.srcElement||evt.target;
	cLastURL = "keywords=" + txtKeywords + "&pagenumber=" + source.id;
	nCurrentPage = parseInt(source.id);
	bRedirecting = true;
	RequestSearch();
	return false;
}

function mouseoverClose(evt)
{
	var evt = window.event||evt;
	var source = evt.srcElement||evt.target;
	source.style.cursor = "hand";
	source.setAttribute("style", "cursor: pointer;");
}

function clickClose()
{
	bCancelLoad = true;
	window.onscroll = null;
	var loadingScreen = document.getElementById("divLoading");
	if(document.getElementById("divLoading"))
	{
		document.body.removeChild(loadingScreen);
	}
}

function closeSearchResults()
{ 
	bDebugCheckLocationChanged = true;
	var divContent = document.getElementById("divContent");
	var divSearchResults = document.getElementById("divSearchResults");
	divContent.style.display = 'block';
	divSearchResults.style.display = 'none';
	//console.log("href 1: " + window.location.href);

	// MDR 29/07/2008 - firefox has problems with empty # and no page arguments
	//window.location.hash = "";
	window.location.hash = "#";
	cLastURL = window.location.href;

	ShoppingCartLink(null, null);
	//console.log("href 2: " + window.location.href);

	// MDR 31/07/2008 - Clear search terms
	document.getElementById("txtKeywords").value = "";
}

function mouseoverSearchClose(evt)
{
	var evt = window.event||evt;
	var source = evt.srcElement||evt.target;
	source.style.cursor = "hand";
	source.setAttribute("style", "cursor: pointer;");
}

function scrollLoadingScreen()
{
	var divLoading = document.getElementById("divLoading");
	var tableLoading = document.getElementById("imgLoading");
	var offsetHeight = document.body.scrollTop;
	divLoading.style.top = offsetHeight + tableLoading.offsetHeight;
}


function whichBrs() 
{
	var agt=navigator.userAgent.toLowerCase();
	if (agt.indexOf("opera") != -1) return 'Opera';
	if (agt.indexOf("staroffice") != -1) return 'Star Office';
	if (agt.indexOf("webtv") != -1) return 'WebTV';
	if (agt.indexOf("beonex") != -1) return 'Beonex';
	if (agt.indexOf("chimera") != -1) return 'Chimera';
	if (agt.indexOf("netpositive") != -1) return 'NetPositive';
	if (agt.indexOf("phoenix") != -1) return 'Phoenix';
	if (agt.indexOf("firefox") != -1) return 'Firefox';
	if (agt.indexOf("safari") != -1) return 'Safari';
	if (agt.indexOf("skipstone") != -1) return 'SkipStone';
	if (agt.indexOf("msie") != -1) return 'Internet Explorer';
	if (agt.indexOf("netscape") != -1) return 'Netscape';
	if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
	if (agt.indexOf('\/') != -1) {
	if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
	return navigator.userAgent.substr(0,agt.indexOf('\/'));}
	else return 'Netscape';} else if (agt.indexOf(' ') != -1)
	return navigator.userAgent.substr(0,agt.indexOf(' '));
	else return navigator.userAgent;
}
