/*function output(message)
{
	document.getElementById("outputBox").innerHTML += "<p>" + message + "</p>";
}

function clearOutput()
{
	document.getElementById("outputBox").innerHTML = "<a href=\"#\" onClick=\"clearOutput();\">Clear</a>";
}*/

var isWorking = false;

function setMiddleCategory(category)
{
	if(typeof(category)!="undefined")
	{
		if (category=="x")
		{
			document.getElementById("theMiddle").innerHTML = "";
		}
		else
		{
			document.getElementById("theMiddle").innerHTML = "<h2>" + category + "</h2>";
		}
	}
	else
	{
		document.getElementById("theMiddle").innerHTML = "";
	}
}

function abortRequest()
{
	http.abort;
	isWorking = false;
	document.getElementById("homeSquare").innerHTML = "<p>Stopped!</p>";
	document.getElementById("homeSquare").innerHTML+= "<a class=\"stopped\" href=\"javascript://\" onMousedown=\"getCategories(" + currentCategory + ");\">Back to Categories</a>";
}

function getHTTPObject()
{
	var xmlhttp;
	/*@cc_on 
	@if (@_jscript_version >= 5) 
		try { 
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } 
		catch (e) { 
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (E) { xmlhttp = false; }
		} 
	@else xmlhttp = false; 
	@end @*/  
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		try {
			xmlhttp = new XMLHttpRequest();
			xmlhttp.overrideMimeType("text/xml");
		} catch (e) {xmlhttp = false;}
	}
	return xmlhttp;
}

var http = getHTTPObject(); // We create the HTTP Object
var http2 = getHTTPObject();
var http3 = getHTTPObject();

var group;

var Curl = "includes/getCategories.php";
var currentCategory;


function getCategories(index, method)
{
	if (index == "mainindex")
	{
		document.getElementById('inputaName').focus();
	}
	else
	{
		document.getElementById('inputPostcode').focus();
	}
	
	if (index != "mainIndex")
	{
		document.getElementById('sortBy').className = "visible";
	}
	
	Curl = "includes/getCategories.php?srt=" + method;
	
	currentCategory = index;
	
	if (document.getElementById("catSelectDefault"))
	{
		document.getElementById("catSelectDefault").selected = true;
	}
	
	if (!isWorking && http2)
	{
		if (index!="")
		{
			Curl = Curl + "&index=" + index;
		}
		group="";
		http2.open("GET", Curl, true);
		http2.onreadystatechange = handleHttpResponseCategories;
		isWorking = true;
		http2.send(null);
	}
}

function getQueryVariable(variable)
{
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++)
	{
		var pair = vars[i].split("=");
		if (pair[0] == variable)
		{
			return pair[1];
		}
	}
	return 'undefined';
}

var url = "includes/getSearchData.php?"; // The server-side script
var traderID="xxx";

function setBoxType(linkFrom, method, category)
{
	document.getElementById('inputPostcode').focus();
	
	url = "includes/getSearchData.php?";
	
	if (linkFrom == "mainIndex")
	{
		traderName = getQueryVariable('tNm');
		traderTown = getQueryVariable('tTwn');
		catID = getQueryVariable('ct');
		optionID = getQueryVariable('opt');
		keywords = getQueryVariable('kw');
		
		url = url + "typeID=" + catID + "&aName=" + traderName + "&aTown=" + traderTown;
		url = url + "&option=" + optionID + "&keywords=" + keywords + "&postcode=&distance=" + "&srt=" + method;
		
		if (document.getElementById("inputaName").value == "")
		{
			document.getElementById("inputaName").value = traderName;
		}
		
		if (document.getElementById("inputaTown").value == "")
		{
			document.getElementById("inputaTown").value = traderTown;
		}
		
		if (document.getElementById("inputKeyword").value == "")
		{
			document.getElementById("inputKeyword").value = keywords;
		}
	}
	else if (linkFrom == "catBox")
	{
		document.getElementById('sortBy').className = "hidden"; //hide sorting for level 3
		
		postcode = document.getElementById("inputPostcode").value;
		distance = document.getElementById("inputDistance").value;
		traderName = document.getElementById("inputaName").value;
		traderTown = document.getElementById("inputaTown").value;
		optionID = document.getElementById("optionsSelect").value;

		document.getElementById(category).selected = true;
		
		category = document.getElementById("category").options[document.getElementById("category").selectedIndex].value;
		
		url = url + "type=" + escape(category) + "&postcode=" + escape(postcode) + "&distance=";
		url = url + distance + "&aName=" + escape(traderName) + "&aTown=" + traderTown + "&option=" + optionID + "&srt=" + method + "&ct=" + getQueryVariable('ct');
		
		group = category;
	}
	else
	{
		
		postcode = document.getElementById("inputPostcode").value;
		distance = document.getElementById("inputDistance").value;
		traderName = document.getElementById("inputaName").value;
		traderTown = document.getElementById("inputaTown").value;
		optionID = document.getElementById("optionsSelect").value;
		keywords = document.getElementById("inputKeyword").value;
		
		if (typeof(category)=="undefined")
		{
			if ((document.getElementById("category")) && (document.getElementById("category").options[document.getElementById("category").selectedIndex].value != "x"))
			{
				category = document.getElementById("category").options[document.getElementById("category").selectedIndex].value;
				url = url + "type=" + escape(category);
			}
			else
			{
				category=folder;
				url = url + "typeID=" + escape(folder);
			}
		}
		url = url + "&postcode=" + escape(postcode) + "&distance=";
		url = url + distance + "&aName=" + escape(traderName) + "&aTown=" + traderTown;
		url = url + "&option=" + optionID + "&keywords=" + keywords + "&srt=" + method;
	}
	
	if (!isWorking && http)
	{
		document.getElementById("homeSquare").className="topRowWorking";
		document.getElementById("homeSquare").innerHTML="<p class=\"loader\">Searching... </p>";
		
		if (typeof(type)!="undefined")
		{
			/*document.getElementById("location").innerHTML =  "<p>" + type + "</p>";*/
			group = type;
		}
		
		//output(url);
		//output(group);
		
		http.open("GET", url, true);
		http.onreadystatechange = handleHttpResponse;
		isWorking = true;
		http.send(null);
		//group = type;
	}
}

function updateFilter()
{
	if (group)
	{
		if (!isWorking && http)
		{
			document.getElementById("homeSquare").className="topRowWorking";
			document.getElementById("homeSquare").innerHTML="<p class=\"loader\">Searching... </p>";

			url+= escape(group) + "&postcode=" + escape(document.getElementById("inputPostcode").value);
			url+= "&distance=" + escape(document.getElementById("inputDistance").value);
			url+= "&allhours=" + document.getElementById("inputAllHours").checked;
			url+= "&emergency=" + document.getElementById("inputEmergency").checked;
			http.open("GET", url, true);
			http.onreadystatechange = handleHttpResponse;
			isWorking = true;
			http.send(null);
		}
	}
}

var Murl = "includes/setMiddleTrader.php?ID=";
var hBoxNum =0;
var hBoxOldClass="";

function setMiddle(idnum,boxNum)
{
	if (typeof(idnum)!="undefined")
	{
		if (!isWorking && http2)
		{
			//output(Murl+escape(idnum));
			http2.open("GET", Murl + escape(idnum), true);
			http2.onreadystatechange = handleHttpResponseMiddle;
			isWorking = true;
			http2.send(null);
			
			//clear the old highlight
			if(hBoxNum!="")
			{
				//make sure it's within range or bad things happen
				if(hBoxNum>0 && hBoxNum <20)
				{
					document.getElementById("box" + hBoxNum).className=hBoxOldClass;
				}
			}
			
			//highlight the box the request came from
			hBoxNum = boxNum;
			hBoxOldClass = document.getElementById("box" + boxNum).className;
			document.getElementById("box" + boxNum).className += " highlighted";
		}
		if(document.getElementById("categoriesLink"))
		{
			document.getElementById("categoriesLink").className = "visible";
		}
		traderID="xxx";
	}
}

var n=0;

function goNextCat(n)
{
	var xmlDocument = http2.responseXML;
	var i=0;
	var numData = xmlDocument.getElementsByTagName('ID');
	
	n++;
	
	document.getElementById("homeSquare").className="topRowWorking";
	document.getElementById("homeSquare").innerHTML="<p class=\"loader\">Searching... </p>";
	
	for(i=n;i<19+n;i++)
	{
		var Category = xmlDocument.getElementsByTagName('Category').item(i).firstChild.data;
		var LogoID = xmlDocument.getElementsByTagName('LogoID').item(i).firstChild.data;
		var ID = xmlDocument.getElementsByTagName('ID').item(i).firstChild.data;
		
		var temparray = document.getElementById("box" + (i-n+1)).className.split(" ");
		document.getElementById("box" + (i-n+1)).className = temparray[0];
		
		document.getElementById("box" + (i-n+1)).className = document.getElementById("box" + (i-n+1)).className + " Logo" + LogoID;
		
		if(document.getElementById("inputDistance"))
		{
			document.getElementById("box" + (i-n+1)).innerHTML = "<div class='sHdiv'><h3 class='squareHeader'><a href='javascript://' onClick=\"setBoxType('catBox','x','" + ID + "');\">" + Category + "</a></h3></div>";	
		}
		else
		{
			document.getElementById("box" + (i-n+1)).innerHTML = "<div class='sHdiv'><h3 class='squareHeader'><a href=\"listings.php?ct=" + ID + "\">" + Category + "</a></h3></div>";
		}
		document.getElementById("box" + (i-n+1)).setAttribute("rel", LogoID);
	}
	
	document.getElementById("homeSquare").className="topRow";

		if (i<numData.length)
		{
			//output(numData.length);
			document.getElementById("homeSquare").innerHTML="<a href='javascript://' class='nextLink' onClick='goNextCat(" + n + ");'><span>Next</span></a><br /><a href='javascript://' class='backLink' onClick='goBackCat(" + n + ");'><span>Back</span></a>";
		}
		else
		{
			document.getElementById("homeSquare").innerHTML="<a href='javascript://' class='backLink' onClick='goBackCat(" + n + ");'><span>Back</span></a>";
		}
}

function goBackCat(n)
{
	var xmlDocument = http2.responseXML;
	var i=0;
	var numData = xmlDocument.getElementsByTagName('ID');
	
	n--;
	
	document.getElementById("homeSquare").className="topRowWorking";
	document.getElementById("homeSquare").innerHTML="<p class=\"loader\">Searching... </p>";
	
	for(i=n;i<19+n;i++)
	{
		var Category = xmlDocument.getElementsByTagName('Category').item(i).firstChild.data;
		var LogoID = xmlDocument.getElementsByTagName('LogoID').item(i).firstChild.data;
		var ID = xmlDocument.getElementsByTagName('ID').item(i).firstChild.data;

		var temparray = document.getElementById("box" + (i-n+1)).className.split(" ");
		document.getElementById("box" + (i-n+1)).className = temparray[0];
		
		document.getElementById("box" + (i-n+1)).className = document.getElementById("box" + (i-n+1)).className + " Logo" + LogoID;
		
		if(document.getElementById("inputDistance"))
		{
			document.getElementById("box" + (i-n+1)).innerHTML = "<div class='sHdiv'><h3 class='squareHeader'><a href='javascript://' onClick=\"setBoxType('catBox','x','" + ID + "');\">" + Category + "</a></h3></div>";	
		}
		else
		{
			document.getElementById("box" + (i-n+1)).innerHTML = "<div class='sHdiv'><h3 class='squareHeader'><a href=\"listings.php?ct=" + ID + "\">" + Category + "</a></h3></div>";
		}
		document.getElementById("box" + (i-n+1)).setAttribute("rel", LogoID);
	}
	
	document.getElementById("homeSquare").className="topRow";
	
	if (i>19)
	{
		document.getElementById("homeSquare").innerHTML="<a href='javascript://' class='nextLink' onClick='goNextCat(" + n + ");'><span>Next</span></a><br /><a href='javascript://' class='backLink' onClick='goBackCat(" + n + ");'><span>Back</span></a>";
	}
	else
	{
		document.getElementById("homeSquare").innerHTML="<a href='javascript://' class='nextLink' onClick='goNextCat(" + n + ");'><span>Next</span></a>";
	}
}

var x=0;

function goNext(x)
{
	//make the highlight follow
	//if(hBoxNum!="")
	//{
		//make sure it's within range or bad things happen
		if(hBoxNum>0 && hBoxNum <20)
		{
			//remove the old one
			document.getElementById("box" + hBoxNum).className=hBoxOldClass;
		}
		
		hBoxNum--;
		//check before breaking stuff
		if(hBoxNum>=1 && hBoxNum<=19)
		{
			hBoxOldClass = document.getElementById("box" + hBoxNum).className;
			document.getElementById("box" + hBoxNum).className += " highlighted";
		}
	//}
	
	var xmlDocument = http.responseXML;
	var i=0;
	var numData = xmlDocument.getElementsByTagName('name');
	
	x++;
	
	document.getElementById("homeSquare").className="topRowWorking";
	document.getElementById("homeSquare").innerHTML="<p class=\"loader\">Searching... </p>";
	
	var numOptions = xmlDocument.getElementsByTagName('options').item(0).getAttribute('count');
	var optionArray = new Array();
	var optionDescArray = new Array();
	
	var id=0;
	
	for (i=0;i<numOptions;i++)
	{
		id=xmlDocument.getElementsByTagName('option').item(i).getAttribute('no');
		optionArray[id] = xmlDocument.getElementsByTagName('option').item(i).getAttribute('pic');
		optionDescArray[id] = xmlDocument.getElementsByTagName('option').item(i).getAttribute('desc');
	}
	
	for(i=x;i<19+x;i++)
	{
		if(xmlDocument.getElementsByTagName('name').item(i))
		{
			var name = xmlDocument.getElementsByTagName('name').item(i).firstChild.data;
			var telno = xmlDocument.getElementsByTagName('telno').item(i).firstChild.data;
			var subscribed = xmlDocument.getElementsByTagName('subscribed').item(i).firstChild.data;
			var ID = xmlDocument.getElementsByTagName('ID').item(i).firstChild.data;
			var accountNo = xmlDocument.getElementsByTagName('accountNo').item(i).firstChild.data;
			var address2 = xmlDocument.getElementsByTagName('address2').item(i).firstChild.data;
			var address3 = xmlDocument.getElementsByTagName('address3').item(i).firstChild.data;
			var town = xmlDocument.getElementsByTagName('town').item(i).firstChild.data;
			var distance = xmlDocument.getElementsByTagName('distance').item(i).firstChild.data;
			var category = xmlDocument.getElementsByTagName('category').item(i).firstChild.data;
			var option1 = xmlDocument.getElementsByTagName('option1').item(i).firstChild.data;
			var option2 = xmlDocument.getElementsByTagName('option2').item(i).firstChild.data;
			var option3 = xmlDocument.getElementsByTagName('option3').item(i).firstChild.data;
			var option4 = xmlDocument.getElementsByTagName('option4').item(i).firstChild.data;
			var option5 = xmlDocument.getElementsByTagName('option5').item(i).firstChild.data;
	
			var quickaddress = "";
			
			if (address2 != "-")
			{
				quickaddress += address2 + ", "
			}
			if (address3 != "-")
			{
				quickaddress += address3 + ", "
			}
			
			quickaddress += town;

	
			if (name.length>20)
			{
				document.getElementById("box" + (i-x+1)).innerHTML = "<div class='sHdiv'><h3 class='squareHeaderSmall'><a href='javascript://' Title='" + quickaddress + "' onClick=\"setMiddle('" + accountNo +"'," + (i-x+1) + ");\">" + name + "</a></h3></div>";
			}
			else
			{
				document.getElementById("box" + (i-x+1)).innerHTML = "<div class='sHdiv'><h3 class='squareHeader'><a href='javascript://' Title='" + quickaddress + "' onClick=\"setMiddle('" + accountNo +"'," + (i-x+1) + ");\">" + name + "</a></h3></div>";
			}
			
			document.getElementById("box" + (i-x+1)).innerHTML+="<p class='detail'>Tel: " + telno + "</p>";
			
			if (typeof(group)=="undefined")
			{
				document.getElementById("box" + (i-x+1)).innerHTML+="<p class='detailCat'>" + category + "</p>";
			}
			else
			{
				if (group=="" || group=="x")
				{
					document.getElementById("box" + (i-x+1)).innerHTML+="<p class='detailCat'>" + category + "</p>";
				}
			}
			
			if (subscribed==1)
			{
				var spanContent = "";
				if(option1!="-")
				{
					spanContent+="<img src=\"images/icons/" + optionArray[option1] + ".gif\" alt=\"Options Icon\" title=\"" + optionDescArray[option1] + "\" />";
				}
				if(option2!="-")
				{
					spanContent+="<img src=\"images/icons/" + optionArray[option2] + ".gif\" alt=\"Options Icon\" title=\"" + optionDescArray[option2] + "\" />";
				}
				if(option3!="-")
				{
					spanContent+="<img src=\"images/icons/" + optionArray[option3] + ".gif\" alt=\"Options Icon\" title=\"" + optionDescArray[option3] + "\" />";
				}
				if(option4!="-")
				{
					spanContent+="<img src=\"images/icons/" + optionArray[option4] + ".gif\" alt=\"Options Icon\" title=\"" + optionDescArray[option4] + "\" />";
				}
				if(option5!="-")
				{
					spanContent+="<img src=\"images/icons/" + optionArray[option5] + ".gif\" alt=\"Options Icon\" title=\"" + optionDescArray[option5] + "\" />";
				}
				if(spanContent!="")
				{
					document.getElementById("box" + (i-x+1)).innerHTML+="<span class=\"optionsListingsBox\">" + spanContent + "</span>";
				}
				if (document.getElementById("box" + (i-x+1)).className.match("subscribed")==null)
				{
					document.getElementById("box" + (i-x+1)).className = document.getElementById("box" + (i-x+1)).className + " subscribed";
				}
			}
			else
			{
				if (document.getElementById("box" + (i-x+1)).className.match("subscribed")!=null)
				{
					document.getElementById("box" + (i-x+1)).className = document.getElementById("box" + (i-x+1)).className.replace(/ subscribed/,"");
				}
			}
			
			document.getElementById("box" + (i-x+1)).innerHTML+= "<span class='distanceBox'>" + distance + " Mi</span>";
		}
	}
	
	document.getElementById("homeSquare").className="topRow";
	
	if (i<numData.length)
	{
		document.getElementById("homeSquare").innerHTML="<a href='javascript://' class='nextLink' onClick='goNext(" + x + ");'><span>Next</span></a><br /><a href='javascript://' class='backLink' onClick='goBack(" + x + ");'><span>Back</span></a>";
	}
	else
	{
		document.getElementById("homeSquare").innerHTML="<a href='javascript://' class='backLink' onClick='goBack(" + x + ");'><span>Back</span></a>";
	}
	
	//output("Moved Next");
}

function goBack(x)
{
	//make the highlight follow
	//if(hBoxNum!="")
	//{
		//make sure it's within range or bad things happen
		if(hBoxNum>0 && hBoxNum <20)
		{
			//remove the old one
			document.getElementById("box" + hBoxNum).className=hBoxOldClass;
		}
		
		hBoxNum++;
		//check before breaking stuff
		if(hBoxNum<=19 && hBoxNum >0)
		{	
			hBoxOldClass = document.getElementById("box" + hBoxNum).className;
			document.getElementById("box" + hBoxNum).className += " highlighted";
		}
	//}
	
	var xmlDocument = http.responseXML;
	var i=0;
	var numData = xmlDocument.getElementsByTagName('name')
	
	x--;
	
	document.getElementById("homeSquare").className="topRowWorking";
	document.getElementById("homeSquare").innerHTML="<p class=\"loader\">Searching... /p>";
	
	var numOptions = xmlDocument.getElementsByTagName('options').item(0).getAttribute('count');
	var optionArray = new Array();
	var optionDescArray = new Array();
	
	var id=0;
	
	for (i=0;i<numOptions;i++)
	{
		id=xmlDocument.getElementsByTagName('option').item(i).getAttribute('no');
		optionArray[id] = xmlDocument.getElementsByTagName('option').item(i).getAttribute('pic');
		optionDescArray[id] = xmlDocument.getElementsByTagName('option').item(i).getAttribute('desc');
	}

	
	for(i=x;i<19+x;i++)
	{
		if(xmlDocument.getElementsByTagName('name').item(i))
		{
			var name = xmlDocument.getElementsByTagName('name').item(i).firstChild.data;
			var telno = xmlDocument.getElementsByTagName('telno').item(i).firstChild.data;
			var subscribed = xmlDocument.getElementsByTagName('subscribed').item(i).firstChild.data;
			var ID = xmlDocument.getElementsByTagName('ID').item(i).firstChild.data;
			var accountNo = xmlDocument.getElementsByTagName('accountNo').item(i).firstChild.data;
			var address2 = xmlDocument.getElementsByTagName('address2').item(i).firstChild.data;
			var address3 = xmlDocument.getElementsByTagName('address3').item(i).firstChild.data;
			var town = xmlDocument.getElementsByTagName('town').item(i).firstChild.data;
			var distance = xmlDocument.getElementsByTagName('distance').item(i).firstChild.data;
			var category = xmlDocument.getElementsByTagName('category').item(i).firstChild.data;
			var option1 = xmlDocument.getElementsByTagName('option1').item(i).firstChild.data;
			var option2 = xmlDocument.getElementsByTagName('option2').item(i).firstChild.data;
			var option3 = xmlDocument.getElementsByTagName('option3').item(i).firstChild.data;
			var option4 = xmlDocument.getElementsByTagName('option4').item(i).firstChild.data;
			var option5 = xmlDocument.getElementsByTagName('option5').item(i).firstChild.data;

			var quickaddress = "";
			
			if (address2 != "-")
			{
				quickaddress += address2 + ", "
			}
			if (address3 != "-")
			{
				quickaddress += address3 + ", "
			}
			
			quickaddress += town;

			if (name.length>20)
			{
				document.getElementById("box" + (i-x+1)).innerHTML = "<div class='sHdiv'><h3 class='squareHeaderSmall'><a href='javascript://' Title='" + quickaddress + "' onClick=\"setMiddle('" + accountNo + "'," + (i-x+1) + ");\">" + name + "</a></h3></div>";
			}
			else
			{
				document.getElementById("box" + (i-x+1)).innerHTML = "<div class='sHdiv'><h3 class='squareHeader'><a href='javascript://' Title='" + quickaddress + "' onClick=\"setMiddle('" + accountNo + "'," + (i-x+1) + ");\">" + name + "</a></h3></div>";
			}
			
			document.getElementById("box" + (i-x+1)).innerHTML+="<p class='detail'>Tel: " + telno + "</p>";
			
			if (typeof(group)=="undefined")
			{
				document.getElementById("box" + (i-x+1)).innerHTML+="<p class='detailCat'>" + category + "</p>";
			}
			else
			{
				if (group=="" || group=="x")
				{
					document.getElementById("box" + (i-x+1)).innerHTML+="<p class='detailCat'>" + category + "</p>";
				}
			}
			
			if (subscribed==1)
			{
				var spanContent = "";
				if(option1!="-")
				{
					spanContent+="<img src=\"images/icons/" + optionArray[option1] + ".gif\" alt=\"Options Icon\" title=\"" + optionDescArray[option1] + "\" />";
				}
				if(option2!="-")
				{
					spanContent+="<img src=\"images/icons/" + optionArray[option2] + ".gif\" alt=\"Options Icon\" title=\"" + optionDescArray[option2] + "\" />";
				}
				if(option3!="-")
				{
					spanContent+="<img src=\"images/icons/" + optionArray[option3] + ".gif\" alt=\"Options Icon\" title=\"" + optionDescArray[option3] + "\" />";
				}
				if(option4!="-")
				{
					spanContent+="<img src=\"images/icons/" + optionArray[option4] + ".gif\" alt=\"Options Icon\" title=\"" + optionDescArray[option4] + "\" />";
				}
				if(option5!="-")
				{
					spanContent+="<img src=\"images/icons/" + optionArray[option5] + ".gif\" alt=\"Options Icon\" title=\"" + optionDescArray[option5] + "\" />";
				}
				if(spanContent!="")
				{
					document.getElementById("box" + (i-x+1)).innerHTML+="<span class=\"optionsListingsBox\">" + spanContent + "</span>";
				}
				if (document.getElementById("box" + (i-x+1)).className.match("subscribed")==null)
				{
					document.getElementById("box" + (i-x+1)).className = document.getElementById("box" + (i-x+1)).className + " subscribed";
				}
			}
			else
			{
				if (document.getElementById("box" + (i-x+1)).className.match("subscribed")!=null)
				{
					document.getElementById("box" + (i-x+1)).className = document.getElementById("box" + (i-x+1)).className.replace(/ subscribed/,"");
				}
			}
			
			document.getElementById("box" + (i-x+1)).innerHTML+= "<span class='distanceBox'>" + distance + " Mi</span>";
		}
	}
	
	document.getElementById("homeSquare").className="topRow";
	
	if (i>19)
	{
		document.getElementById("homeSquare").innerHTML="<a href='javascript://' class='nextLink' onClick='goNext(" + x + ");'><span>Next</span></a><br /><a href='javascript://' class='backLink' onClick='goBack(" + x + ");'><span>Back</span></a>";
	}
	else
	{
		document.getElementById("homeSquare").innerHTML="<a href='javascript://' class='nextLink' onClick='goNext(" + x + ");'><span>Next</span></a>";
	}
	//output("Moved Back");
}

function handleHttpResponse()
{
	if (http.readyState == 4)
	{
		//output("Status:" + http.status);
		var xmlDocument = http.responseXML;
		var i=0;		
		
		document.getElementById("homeSquare").className="topRowWorking";
		document.getElementById("homeSquare").innerHTML="<p class=\"loader\">Searching... </p><p><a class=\"stopped\" href='#' onClick='abortRequest();'> Cancel Search</a></p>";
		
		var numData = xmlDocument.getElementsByTagName('name');
		var counted = numData.length;
		
		var numOptions = xmlDocument.getElementsByTagName('options').item(0).getAttribute('count');
		var optionArray = new Array();
		var optionDescArray = new Array();
		
		/*if(xmlDocument.getElementsByTagName('output').item(0).firstChild)
		{
			var output =  xmlDocument.getElementsByTagName('output').item(0).firstChild.data;
		}
		
		if(output=="793")
		{
			alert("There was an error with the postcode for at least one result, results with errors are not displayed");
			output = "";
		}*/
		
		//output("Number of options: " + numOptions);
		//output("XML Value: " + xmlDocument.getElementsByTagName('option').item(0).getAttribute('no'));
		
		var id=0;
		
		for (i=0;i<numOptions;i++)
		{
			id=xmlDocument.getElementsByTagName('option').item(i).getAttribute('no');
			optionArray[id] = xmlDocument.getElementsByTagName('option').item(i).getAttribute('pic');
			optionDescArray[id] = xmlDocument.getElementsByTagName('option').item(i).getAttribute('desc');
		}
		
		//output("ID Value: " + id);
		//output("Array Value: " + optionArray[1])
		
		if (numData.length>19)
		{
			for(i=0;i<19;i++)
			{
				var name = xmlDocument.getElementsByTagName('name').item(i).firstChild.data;
				var telno = xmlDocument.getElementsByTagName('telno').item(i).firstChild.data;
				var subscribed = xmlDocument.getElementsByTagName('subscribed').item(i).firstChild.data;
				var ID = xmlDocument.getElementsByTagName('ID').item(i).firstChild.data;
				var accountNo = xmlDocument.getElementsByTagName('accountNo').item(i).firstChild.data;
				var address2 = xmlDocument.getElementsByTagName('address2').item(i).firstChild.data;
				var address3 = xmlDocument.getElementsByTagName('address3').item(i).firstChild.data;
				var town = xmlDocument.getElementsByTagName('town').item(i).firstChild.data;
				var distance = xmlDocument.getElementsByTagName('distance').item(i).firstChild.data;
				var category = xmlDocument.getElementsByTagName('category').item(i).firstChild.data;
				var option1 = xmlDocument.getElementsByTagName('option1').item(i).firstChild.data;
				var option2 = xmlDocument.getElementsByTagName('option2').item(i).firstChild.data;
				var option3 = xmlDocument.getElementsByTagName('option3').item(i).firstChild.data;
				var option4 = xmlDocument.getElementsByTagName('option4').item(i).firstChild.data;
				var option5 = xmlDocument.getElementsByTagName('option5').item(i).firstChild.data;

				var quickaddress = "";
				
				if (address2 != "-")
				{
					quickaddress += address2 + ", "
				}
				if (address3 != "-")
				{
					quickaddress += address3 + ", "
				}
				
				quickaddress += town;

				var temparray = document.getElementById("box" + (i+1)).className.split(" ");
				document.getElementById("box" + (i+1)).className = temparray[0];

				if (name.length>20)
				{
					document.getElementById("box" + (i+1)).innerHTML = "<div class='sHdiv'><h3 class='squareHeaderSmall'><a href='javascript://' Title='" + quickaddress + "' onClick=\"setMiddle('" + accountNo +"'," + (i+1) + ");\">" + name + "</a></h3></div>";
				}
				else
				{
					document.getElementById("box" + (i+1)).innerHTML = "<div class='sHdiv'><h3 class='squareHeader'><a href='javascript://' Title='" + quickaddress + "' onClick=\"setMiddle('" + accountNo +"'," + (i+1) + ");\">" + name + "</a></h3></div>";
				}
				
				document.getElementById("box" + (i+1)).innerHTML+="<p class='detail'>Tel: " + telno + "</p>";
				
				if (typeof(group)=="undefined")
				{
					document.getElementById("box" + (i+1)).innerHTML+="<p class='detailCat'>" + category + "</p>";
				}
				else
				{
					if (group=="" || group=="x")
					{
						document.getElementById("box" + (i+1)).innerHTML+="<p class='detailCat'>" + category + "</p>";
					}
				}
				
				if (subscribed==1)
				{
					var spanContent = "";
					if(option1!="-")
					{
						spanContent+="<img src=\"images/icons/" + optionArray[option1] + ".gif\" alt=\"Options Icon\" title=\"" + optionDescArray[option1] + "\" />";
					}
					if(option2!="-")
					{
						spanContent+="<img src=\"images/icons/" + optionArray[option2] + ".gif\" alt=\"Options Icon\" title=\"" + optionDescArray[option2] + "\" />";
					}
					if(option3!="-")
					{
						spanContent+="<img src=\"images/icons/" + optionArray[option3] + ".gif\" alt=\"Options Icon\" title=\"" + optionDescArray[option3] + "\" />";
					}
					if(option4!="-")
					{
						spanContent+="<img src=\"images/icons/" + optionArray[option4] + ".gif\" alt=\"Options Icon\" title=\"" + optionDescArray[option4] + "\" />";
					}
					if(option5!="-")
					{
						spanContent+="<img src=\"images/icons/" + optionArray[option5] + ".gif\" alt=\"Options Icon\" title=\"" + optionDescArray[option5] + "\" />";
					}
					if(spanContent!="")
					{
						document.getElementById("box" + (i+1)).innerHTML+="<span class=\"optionsListingsBox\">" + spanContent + "</span>";
					}
					document.getElementById("box" + (i+1)).className = document.getElementById("box" + (i+1)).className + " subscribed";
				}
				
				document.getElementById("box" + (i+1)).innerHTML+= "<span class='distanceBox'>" + distance + " Mi</span>";
			}
			document.getElementById("homeSquare").innerHTML="<p>Query Complete!<br />" + counted + " Results Found</p><a href='javascript://' class='nextLink' onClick='goNext(0);'><span>Next</span></a>";
		}
		else
		{
			for(i=0;i<numData.length;i++)
			{
				var name = xmlDocument.getElementsByTagName('name').item(i).firstChild.data;
				var telno = xmlDocument.getElementsByTagName('telno').item(i).firstChild.data;
				var subscribed = xmlDocument.getElementsByTagName('subscribed').item(i).firstChild.data;
				var ID = xmlDocument.getElementsByTagName('ID').item(i).firstChild.data;
				var accountNo = xmlDocument.getElementsByTagName('accountNo').item(i).firstChild.data;
				var address2 = xmlDocument.getElementsByTagName('address2').item(i).firstChild.data;
				var address3 = xmlDocument.getElementsByTagName('address3').item(i).firstChild.data;
				var town = xmlDocument.getElementsByTagName('town').item(i).firstChild.data;
				var distance = xmlDocument.getElementsByTagName('distance').item(i).firstChild.data;
				var category = xmlDocument.getElementsByTagName('category').item(i).firstChild.data;
				var option1 = xmlDocument.getElementsByTagName('option1').item(i).firstChild.data;
				var option2 = xmlDocument.getElementsByTagName('option2').item(i).firstChild.data;
				var option3 = xmlDocument.getElementsByTagName('option3').item(i).firstChild.data;
				var option4 = xmlDocument.getElementsByTagName('option4').item(i).firstChild.data;
				var option5 = xmlDocument.getElementsByTagName('option5').item(i).firstChild.data;
				
				var quickaddress = "";
				
				if (address2 != "-")
				{
					quickaddress += address2 + ", "
				}
				if (address3 != "-")
				{
					quickaddress += address3 + ", "
				}
				
				quickaddress += town;
				
				var temparray = document.getElementById("box" + (i+1)).className.split(" ");
				document.getElementById("box" + (i+1)).className = temparray[0];

				if (name.length>20)
				{
					document.getElementById("box" + (i+1)).innerHTML = "<div class='sHdiv'><h3 class='squareHeaderSmall'><a href='javascript://' Title='" + quickaddress + "' onClick=\"setMiddle('" + accountNo +"'," + (i+1) + ");\">" + name + "</a></h3></div>";
				}
				else
				{
					document.getElementById("box" + (i+1)).innerHTML = "<div class='sHdiv'><h3 class='squareHeader'><a href='javascript://' Title='" + quickaddress + "' onClick=\"setMiddle('" + accountNo +"'," + (i+1) + ");\">" + name + "</a></h3></div>";
				}
				
				document.getElementById("box" + (i+1)).innerHTML+="<p class='detail'>Tel: " + telno + "</p>";
				
				if (typeof(group)=="undefined")
				{
					document.getElementById("box" + (i+1)).innerHTML+="<p class='detailCat'>" + category + "</p>";
				}
				else
				{
					if (group=="" || group=="x")
					{
						document.getElementById("box" + (i+1)).innerHTML+="<p class='detailCat'>" + category + "</p>";
					}
				}
				
				if(subscribed==1)
				{
					var spanContent="";
	
					if(option1!="-")
					{
						spanContent+="<img src=\"images/icons/" + optionArray[option1] + ".gif\" alt=\"Options Icon\" title=\"" + optionDescArray[option1] + "\" />";
					}
					if(option2!="-")
					{
						spanContent+="<img src=\"images/icons/" + optionArray[option2] + ".gif\" alt=\"Options Icon\" title=\"" + optionDescArray[option2] + "\" />";
					}
					if(option3!="-")
					{
						spanContent+="<img src=\"images/icons/" + optionArray[option3] + ".gif\" alt=\"Options Icon\" title=\"" + optionDescArray[option3] + "\" />";
					}
					if(option4!="-")
					{
						spanContent+="<img src=\"images/icons/" + optionArray[option4] + ".gif\" alt=\"Options Icon\" title=\"" + optionDescArray[option4] + "\" />";
					}
					if(option5!="-")
					{
						spanContent+="<img src=\"images/icons/" + optionArray[option5] + ".gif\" alt=\"Options Icon\" title=\"" + optionDescArray[option5] + "\" />";
					}
					if(spanContent!="")
					{
						document.getElementById("box" + (i+1)).innerHTML+="<span class=\"optionsListingsBox\">" + spanContent + "</span>";
					}
					document.getElementById("box" + (i+1)).className = document.getElementById("box" + (i+1)).className + " subscribed";
				}
				
				document.getElementById("box" + (i+1)).innerHTML+= "<span class='distanceBox'>" + distance + " Mi</span>";
			}
			
			for (i=numData.length+1;i<20;i++)
			{
				document.getElementById("box" + (i)).innerHTML = "";
				var temparray2 = document.getElementById("box" + (i)).className.split(" ");
				document.getElementById("box" + (i)).className = temparray2[0];
			}
			document.getElementById("homeSquare").innerHTML="<p>Query Complete!<br />" + counted + " Results Found</p>";
		}
		document.getElementById("homeSquare").className="topRow";
		setMiddleCategory(group);
		isWorking = false;
		
		document.getElementById("hideOptions").className = "visible";
		
		if (document.getElementById("categoriesLink"))
		{
			document.getElementById("categoriesLink").className = "visible";
		}
		
		if (traderID!="xxx")
		{
			setMiddle(traderID);
		}
		
		if (numData.length<1)
		{
			document.getElementById("box1").innerHTML = "There are no results in this category...";
		}
	}
	
	if (http.readyState != 4)
	{
		document.getElementById("homeSquare").className="topRowWorking";
		document.getElementById("homeSquare").innerHTML="<p class=\"loader\">Searching... </p><p><a class=\"stopped\" href='#' onClick='abortRequest();'> Cancel Search</a></p>";
	}
}

function handleHttpResponseCategories()
{
	if (http2.readyState == 4)
	{
		//clear the old highlight
		if(hBoxNum!="")
		{
			//make sure it's within range or bad things happen
			if(hBoxNum>0 && hBoxNum <20)
			{
				//remove the old one
				document.getElementById("box" + hBoxNum).className=hBoxOldClass;
			}
		}

		var xmlDocument = http2.responseXML;
		
		var i=0;
		var numData = xmlDocument.getElementsByTagName('ID');
		
		if (numData.length>19)
		{
			for(i=0;i<19;i++)
			{
				var Category = xmlDocument.getElementsByTagName('Category').item(i).firstChild.data;
				var LogoID = xmlDocument.getElementsByTagName('LogoID').item(i).firstChild.data;
				var ID = xmlDocument.getElementsByTagName('ID').item(i).firstChild.data;
				
				if (document.getElementById("box" + (i+1)).className.match("subscribed"))
				{
					document.getElementById("box" + (i+1)).className=document.getElementById("box" + (i+1)).className.replace(/ subscribed/,"");
				}
				
				if (currentCategory!="mainindex")
				{
					document.getElementById("box" + (i+1)).innerHTML = "<div class='sHdiv'><h3 class='squareHeader'><a href='javascript://' onClick=\"setBoxType('catBox','x','" + ID + "');\">" + Category + "</a></h3></div>";	
					document.getElementById("box" + (i+1)).className = document.getElementById("box" + (i+1)).className + " Logo" + LogoID;
					if (currentCategory=="shoppingcentre")
					{
						document.getElementById("box" + (i+1)).innerHTML+= "<a href=\"javascript://\" onClick=\"\" class=\"SCinfoLink\"><span>info</span></a>";
					}
				}
				else
				{
					document.getElementById("box" + (i+1)).innerHTML = "<div class='sHdiv'><h3 class='squareHeader'><a href='listings.php?ct=" + ID + "'>" + Category + "</a></h3></div>";	
					document.getElementById("box" + (i+1)).className = document.getElementById("box" + (i+1)).className + " Logo" + LogoID;
				}																				  
			}
			document.getElementById("homeSquare").innerHTML = "<p>Choose a category</p><a href='javascript://' class='nextLink' onClick='goNextCat(" + x + ");'><span>Next</span></a>";
		}
		else
		{
			for(i=0;i<numData.length;i++)
			{
				var Category = xmlDocument.getElementsByTagName('Category').item(i).firstChild.data;
				var LogoID = xmlDocument.getElementsByTagName('LogoID').item(i).firstChild.data;
				var ID = xmlDocument.getElementsByTagName('ID').item(i).firstChild.data;
				
				if (document.getElementById("box" + (i+1)).className.match("subscribed"))
				{
					document.getElementById("box" + (i+1)).className=document.getElementById("box" + (i+1)).className.replace(/ subscribed/,"");
				}
				
				if (currentCategory!="mainindex")
				{
					document.getElementById("box" + (i+1)).innerHTML = "<div class='sHdiv'><h3 class='squareHeader'><a href='javascript://' onClick=\"setBoxType('catBox','x','" + ID + "');\">" + Category + "</a></h3></div>";	
					document.getElementById("box" + (i+1)).className = document.getElementById("box" + (i+1)).className + " Logo" + LogoID;
					if (currentCategory=="shoppingcentre")
					{
						document.getElementById("box" + (i+1)).innerHTML+= "<a href=\"javascript://\" onClick=\"\" class=\"SCinfoLink\"><span>info</span></a>";
					}
				}
				else
				{
					document.getElementById("box" + (i+1)).innerHTML = "<div class='sHdiv'><h3 class='squareHeader'><a href='listings.php?ct=" + ID + "'>" + Category + "</a></h3></div>";	
					document.getElementById("box" + (i+1)).className = document.getElementById("box" + (i+1)).className + " Logo" + LogoID;
				}
			}
			
			for (i=numData.length+1;i<20;i++)
			{
				document.getElementById("box" + (i)).innerHTML = "";
			}
			document.getElementById("homeSquare").innerHTML = "<p>Choose a category</p>";
		}
		
		document.getElementById("homeSquare").className="topRow";
		
		/*document.getElementById("location").innerHTML = "";*/
		
		isWorking = false;
		
		if (currentCategory!="mainindex")
		{
			//if(document.getElementById("theMiddle").lastChild.nodeName=="P")
			//{
				if(CatName)
				{
					document.getElementById("theMiddle").innerHTML = "<h2>" + CatName + "</h2>";
					setAds();
				}
			//}
			document.getElementById("hideOptions").className = "visible";
			if (document.getElementById("categoriesLink"))
			{
				document.getElementById("categoriesLink").className = "hidden";
			}
		}
	}
}

function handleHttpResponseMiddle()
{
	if (http2.readyState == 4)
	{
		var xmlDocument = http2.responseXML;
		
		var numOptions = xmlDocument.getElementsByTagName('options').item(0).getAttribute('count');
		var traderID = xmlDocument.getElementsByTagName('trader').item(0).getAttribute('id');
		var optionArray = new Array();
		var optionDescArray = new Array();
		
		var id=0;
		
		for (i=0;i<numOptions;i++)
		{
			id=xmlDocument.getElementsByTagName('option').item(i).getAttribute('no');
			optionArray[id] = xmlDocument.getElementsByTagName('option').item(i).getAttribute('pic');
			optionDescArray[id] = xmlDocument.getElementsByTagName('option').item(i).getAttribute('desc');
		}
		
		var ID_No = xmlDocument.getElementsByTagName('id_no').item(0).firstChild.data;
		var name = xmlDocument.getElementsByTagName('name').item(0).firstChild.data;
		var telno = xmlDocument.getElementsByTagName('telno').item(0).firstChild.data;
		var mobno = xmlDocument.getElementsByTagName('mobno').item(0).firstChild.data;
		var faxno = xmlDocument.getElementsByTagName('faxno').item(0).firstChild.data;
		var subscribed = xmlDocument.getElementsByTagName('subscribed').item(0).firstChild.data;
		var address1 = xmlDocument.getElementsByTagName('address1').item(0).firstChild.data;
		var address2 = xmlDocument.getElementsByTagName('address2').item(0).firstChild.data;
		var address3 = xmlDocument.getElementsByTagName('address3').item(0).firstChild.data;
		var address4 = xmlDocument.getElementsByTagName('address4').item(0).firstChild.data;
		var town = xmlDocument.getElementsByTagName('town').item(0).firstChild.data;
		var county = xmlDocument.getElementsByTagName('county').item(0).firstChild.data;
		var postcode = xmlDocument.getElementsByTagName('postcode').item(0).firstChild.data;
		var logoID = xmlDocument.getElementsByTagName('logoID').item(0).firstChild.data;
		var freetype = xmlDocument.getElementsByTagName('freetype').item(0).firstChild.data;
		var cat_id = xmlDocument.getElementsByTagName('cat_id').item(0).firstChild.data;
		var logged_in = xmlDocument.getElementsByTagName('Lin').item(0).firstChild.data;
		var web = xmlDocument.getElementsByTagName('web').item(0).firstChild.data;
		var email = xmlDocument.getElementsByTagName('email').item(0).firstChild.data;
		var option1 = xmlDocument.getElementsByTagName('option1').item(0).firstChild.data;
		var option2 = xmlDocument.getElementsByTagName('option2').item(0).firstChild.data;
		var option3 = xmlDocument.getElementsByTagName('option3').item(0).firstChild.data;
		var option4 = xmlDocument.getElementsByTagName('option4').item(0).firstChild.data;
		var option5 = xmlDocument.getElementsByTagName('option5').item(0).firstChild.data;

		if (subscribed==1)
		{
			$middleCode = "<h2>" + name + "</h2>";
			$middleCode+= "<div id=\"listingOptions\">";
			$middleCode+= "<div class=\"listingOptionsLeft\">";
			$middleCode+= "<a href=\"javascript:window.print()\"><img src=\"images/printer.png\" alt=\"printer icon\" />Print</a>";
			$middleCode+= " &bull; <a href=\"http://maps.google.co.uk/maps?q=" + postcode + "\"><img src=\"images/map.png\" alt=\"map icon\" />Map</a>";
			if (logged_in=="1")
				$middleCode+= " &bull; <a href=\"javascript:AddtoAddressBook('" + ID_No + "','1')\"><img src=\"images/address_book.png\" alt=\"add icon\" />Address Book</a>";
			$middleCode+= "</div>";
			$middleCode+= "<a class=\"listingOptionsRight\" href=\"listingsMoreDetail.php?id=" + ID_No + "\"><img src=\"images/information.png\" alt=\"add icon\" />More Details</a>";
			$middleCode+= "<div class=\"clear\"></div>";
			$middleCode+= "</div>";
			if (logoID == "-")
			{
				$middleCode+= "<div id=\"Address\">";
				$middleCode+= "<h4>Address:</h4>";
				$middleCode+= "<address>";
				if (address1!="-")
					$middleCode+=address1 + "<br />";
				if (address2!="-")
					$middleCode+=address2 + "<br />";
				if (address3!="-")
					$middleCode+=address3 + "<br />";
				if (address4!="-")
					$middleCode+=address4 + "<br />";
				if (town!="-")
					$middleCode+=town + "<br />";
				if (county!="-")
					$middleCode+=county + "<br />";
				if (postcode!="-")
					$middleCode+=postcode + "<br />";
				$middleCode+= "</address>";
				$middleCode+= "</div>";
				$middleCode+= "<div id=\"contactDetails\">";
				$middleCode+= "<h4>Contact Details</h4>";
				if (telno!="-")
					$middleCode+= "<p class=\"phoneNum\">Tel: " + telno + "</p>";
				if (mobno!="-")
					$middleCode+= "<p class=\"mobileNum\">Mobile: " + mobno + "</p>";
				if (faxno!="-")
					$middleCode+= "<p class=\"faxNum\">Fax: " + faxno + "</p>";
				if (email!="-")
					$middleCode+= "<p class=\"SubMailLink\"><a href=\"mailto:" + email + "\">E-Mail</a></p>";
				if (web!="-")
					$middleCode+= "<p class=\"SubWebLink\"><a href=\"" + web + "\" target=\"blank\">Website</a></p>";
				
				$middleCode+= "<p class=\"SubVCardLink\"><a href=\"includes/vcard.php?tel=" + telno + "&amp;n=" + name + "&amp;a1=" + address1 + 
				"&amp;t=" + town + "&amp;c=" + county + "&amp;pc=" + postcode + "&amp;em=" + email + "&amp;ft=" + freetype + 
				"&amp;ws=" + web + "\">vCard</a></p>";
				
				$middleCode+= "</div>";
				if (option1!="-" || option2!="-" || option3!="-" || option4!="-" || option5!="-")
				{
					$middleCode+= "<div id=\"optionDetails\">";
					$middleCode+= "<h4>Options</h4><ul id=\"middleOptionsList\">";
					if(option1!="-")
					{
						$middleCode+="<li id=\"optionsCheck" + option1 + "\">" + optionDescArray[option1] + "</li>";
					}
					if(option2!="-")
					{
						$middleCode+="<li id=\"optionsCheck" + option2 + "\">" + optionDescArray[option2] + "</li>";
					}
					if(option3!="-")
					{
						$middleCode+="<li id=\"optionsCheck" + option3 + "\">" + optionDescArray[option3] + "</li>";
					}
					if(option4!="-")
					{
						$middleCode+="<li id=\"optionsCheck" + option4 + "\">" + optionDescArray[option4] + "</li>";
					}
					if(option5!="-")
					{
						$middleCode+="<li id=\"optionsCheck" + option5 + "\">" + optionDescArray[option5] + "</li>";
					}
					$middleCode+= "</ul></div>";
				}
			}
			else
			{
				$middleCode+= "<img src=\"Registration/Company_Logos/" + logoID + "\" alt=\"Company Logo\" id=\"companyLogo\"/>"
				$middleCode+= "<div id=\"AddressPic\">";
				$middleCode+= "<h4>Address:</h4>";
				$middleCode+= "<address>";
				if (address1!="-")
					$middleCode+=address1 + "<br />";
				if (address2!="-")
					$middleCode+=address2 + "<br />";
				if (address3!="-")
					$middleCode+=address3 + "<br />";
				if (address4!="-")
					$middleCode+=address4 + "<br />";
				if (town!="-")
					$middleCode+=town + "<br />";
				if (county!="-")
					$middleCode+=county + "<br />";
				if (postcode!="-")
					$middleCode+=postcode + "<br />";
				$middleCode+= "</address>";
				$middleCode+= "</div>";
				$middleCode+= "<div id=\"contactDetailsPic\">";
				$middleCode+= "<h4>Contact Details</h4>";
				if (telno!="-")
					$middleCode+= "<p class=\"phoneNum\">Tel: " + telno + "</p>";
				if (mobno!="-")
					$middleCode+= "<p class=\"mobileNum\">Mobile: " + mobno + "</p>";
				if (faxno!="-")
					$middleCode+= "<p class=\"faxNum\">Fax: " + faxno + "</p>";
				if (email!="-")
					$middleCode+= "<p class=\"SubMailLink\"><a href=\"mailto:" + email + "\">E-Mail</a></p>";
				if (web!="-")
					$middleCode+= "<p class=\"SubWebLink\"><a href=\"" + web + "\" target=\"blank\">Website</a></p>";
					
				$middleCode+= "<p class=\"SubVCardLink\"><a href=\"includes/vcard.php?tel=" + telno + "&amp;n=" + name + "&amp;a1=" + address1 + 
				"&amp;t=" + town + "&amp;c=" + county + "&amp;pc=" + postcode + "&amp;em=" + email + "&amp;ft=" + freetype + 
				"&amp;ws=" + web + "\">vCard</a></p>";
				
				$middleCode+= "</div>";
			
				
				if (option1!="-" || option2!="-" || option3!="-" || option4!="-" || option5!="-")
				{
					$middleCode+= "<div id=\"optionDetails\">";
					$middleCode+= "<h4>Options</h4><ul>";
					if(option1!="-")
					{
						$middleCode+="<li id=\"optionsCheck" + option1 + "\">" + optionDescArray[option1] + "</li>";
					}
					if(option2!="-")
					{
						$middleCode+="<li id=\"optionsCheck" + option2 + "\">" + optionDescArray[option2] + "</li>";
					}
					if(option3!="-")
					{
						$middleCode+="<li id=\"optionsCheck" + option3 + "\">" + optionDescArray[option3] + "</li>";
					}
					if(option4!="-")
					{
						$middleCode+="<li id=\"optionsCheck" + option4 + "\">" + optionDescArray[option4] + "</li>";
					}
					if(option5!="-")
					{
						$middleCode+="<li id=\"optionsCheck" + option5 + "\">" + optionDescArray[option5] + "</li>";
					}
					$middleCode+= "</ul></div>";
				}
			}
			
			if (freetype!="-")
			{
				var freetypeNew = freetype.replace(/\[b\]/gi, "<strong>").replace(/\[\/b\]/gi, "</strong>")
											.replace(/\[i\]/gi, "<em>").replace(/\[\/i\]/gi, "</em>")
											.replace(/\[u\]/gi, "<u>").replace(/\[\/u\]/gi, "</u>")
											.replace(/\n\n/gi, "<br />").replace(/\n/gi, "<br />")
											.replace(/\[\*\]/gi, "&nbsp;&nbsp;&bull;&nbsp;&nbsp;");
				$middleCode+= "<div id=\"freeType\">";
				$middleCode+= "<h4>About</h4><p>" + freetypeNew + "</p>";
				$middleCode+= "</div>";
			}
			$middleCode+= "<p class=\"tID\">Listing ID: " + traderID + "</p>";
			document.getElementById("theMiddle").innerHTML = $middleCode;
		}
		else // Not Subscribed
		{
			$middleCode = "<h2>" + name + "</h2>";
			$middleCode+= "<div id=\"listingOptions\">";
			$middleCode+= "<a href=\"javascript:window.print()\"><img src=\"images/printer.png\" alt=\"printer icon\" />Print this listing</a> | ";
			$middleCode+= "<a href=\"http://maps.google.co.uk/maps?q=" + postcode + "\"><img src=\"images/map.png\" alt=\"map icon\" />View Map</a>";
			if (logged_in=="1")
				$middleCode+= " | <a href=\"javascript:AddtoAddressBook('" + ID_No + "')\"><img src=\"images/address_book.png\" alt=\"add icon\" />Address Book</a>";
			$middleCode+= "</div>";
			$middleCode+= "<div id=\"AddressUnSub\">";
			$middleCode+= "<h4>Address:</h4>";
			$middleCode+= "<address>";
			if (address1!="-")
				$middleCode+=address1 + "<br />";
			if (address2!="-")
				$middleCode+=address2 + "<br />";
			if (address3!="-")
				$middleCode+=address3 + "<br />";
			if (address4!="-")
				$middleCode+=address4 + "<br />";
			if (town!="-")
				$middleCode+=town + "<br />";
			if (county!="-")
				$middleCode+=county + "<br />";
			if (postcode!="-")
				$middleCode+=postcode + "<br />";
			if (telno!="-")
					$middleCode+= "<p class=\"center\">Tel: " + telno + "</p>";
			$middleCode+= "</address>";
			$middleCode+= "</div>";
			$middleCode+= "<div id=\"unSubText\">This is a free listing, <a href=\"benefits.php?id=" + traderID + "\">Click Here</a> to find out about the benefits that a paid listing could offer.</div>";
			$middleCode+= "<p class=\"tID\">Listing ID: " + traderID + "</p>";
			
			$middleCode+="<div id=\"listingAd\">Advertisement:";
			if (folder!="x")
			{
				j = Math.floor(Math.random() * 9+1);
				$middleCode+= "<a href=\"ads.php?f=" + folder + "&amp;i=" + j + "\" class=\"adLink\"><img class=\"ad\" alt=\"(banner)\" title=\"Banner Advertisement\" src=\"images/ads/" + folder + "/Banner" + j + ".gif\" /></a>";
			}
			else
			{
				j = Math.floor(Math.random() * 9+1);
				$middleCode+= "<a href=\"ads.php?f=" + cat_id + "&amp;i=" + j + "\" class=\"adLink\"><img class=\"ad\" alt=\"(banner)\" title=\"Banner Advertisement\" src=\"images/ads/" + cat_id + "/Banner" + j + ".gif\" /></a>";
			}
			$middleCode+="</div>";
			
			document.getElementById("theMiddle").innerHTML = $middleCode;
		}
		isWorking = false;
		//output("Set Middle Trader");
	}
}

function appendMiddle(content)
{
	document.getElementById("theMiddle").innerHTML += content;
}

function popup_scroll(url, width, height)
{
	wnd = window.open(url, "popup", "width="+width + ",height="+height + ",left=450,top=350,resizable=no,scrollbars=yes");
	wnd.focus();
}



// Add to Address Book
	var ABurl = "includes/setAddressBook.php";
	
	function AddtoAddressBook(accAB)
	{
		ABurl = "includes/setAddressBook.php";
					
		if (!isWorking && http3)
		{
			if (accAB!="")
			{
				ABurl = ABurl + "?accAB=" + accAB;
			}
			http3.open("GET", ABurl, true);
			http3.onreadystatechange = handleHttpResponseAddtoAddressBook;
			isWorking = true;
			http3.send(null);
		}
	}
	
	function handleHttpResponseAddtoAddressBook()
	{
		if (http3.readyState == 4)
		{
			var xmlDocument = http3.responseXML;
			
			var result_ab = xmlDocument.getElementsByTagName('Result_AB').item(0).firstChild.data;
	
			if(result_ab == "True")
			{
				document.getElementById("transBG").className = "visible";
				document.getElementById("popup_content").innerHTML = "The current listing has successfully been added to your Address Book.";
			}
			else
			{
				alert("The selected listing is already in your Address Book.");
			}
		}
		isWorking = false;
	
	}
	
// Remove From Address Book
	var ABurl = "includes/delete_address_book.php";
	
	function RemoveFromAddressBook(accAB)
	{
	if(confirm("Are you sure you wish to remove this listing from your address book?"))
		{
			ABurl = "includes/delete_address_book.php";
						
			if (!isWorking && http3)
			{
				if (accAB!="")
				{
					ABurl = ABurl + "?accAB=" + accAB;
				}
				http3.open("GET", ABurl, true);
				http3.onreadystatechange = handleHttpResponseRemoveFromAddressBook;
				isWorking = true;
				http3.send(null);
			}
		}
		else
		{
		}
	}
	
	function handleHttpResponseRemoveFromAddressBook()
	{

		if (http3.readyState == 4)
		{
			var xmlDocument = http3.responseXML;
			
			var result_ab = xmlDocument.getElementsByTagName('Result_AB').item(0).firstChild.data;
	
	
			if(result_ab == "True")
			{
				document.getElementById("transBG").className = "visible";
				document.getElementById("popup_content").innerHTML = "The selected record has been removed from your Address Book.";
			}
			else
			{
				alert("No such listing is contained within your Address Book!");
			}
		}
		isWorking = false;
	
	}

// Emptry Address Book
	var ABurl = "includes/empty_address_book.php";
	
	function EmptyAddressBook()
	{
	if(confirm("Are you sure you wish to clear your address book of all records?"))
		{
			ABurl = "includes/empty_address_book.php";
						
			if (!isWorking && http3)
			{
				http3.open("GET", ABurl, true);
				http3.onreadystatechange = handleHttpResponseEmptyAddressBook;
				isWorking = true;
				http3.send(null);
			}
		}
		else
		{
		}
	}
	
	function handleHttpResponseEmptyAddressBook()
	{

		if (http3.readyState == 4)
		{
			var xmlDocument = http3.responseXML;
			
			var result_ab = xmlDocument.getElementsByTagName('Result_AB').item(0).firstChild.data;
	
	
			if(result_ab == "True")
			{
				document.getElementById("transBG").className = "visible";
				document.getElementById("popup_content").innerHTML = "Your Address Book has been successfully emptied of all records.";
			}
			else
			{
				alert("Your address book is already empty!");
			}
		}
		isWorking = false;
	
	}

function sortBy(location, method)
{
	if(location != "default.php")
	{
		if (method != "none")
		{
			document.location = location + "&srt=" + method;
		}
		else
		{
			document.location = location + "&srt=x";
		}
	}
	else
	{
		if (method != "none")
		{
			document.location = location + "?srt=" + method;
		}
		else
		{
			document.location = location + "?srt=x";
		}

	}
}