window.onload = Security;
window.onunload = BakeCookie;

function SelectStates(state){
x = document.forms["states"].elements.length;
for (i = 0; i < x; i++)
	{
	document.forms["states"].elements[i].checked = state;
	}
ShowClick();
}

var regExp = /<\/?[^>]+>/gi;
function ReplaceTags(xStr){
xStr = xStr.replace(regExp,"");
return xStr;
}

var request = false;
try {
 request = new XMLHttpRequest();
} catch (trymicrosoft) {
 try {
   request = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (othermicrosoft) {
   try {
     request = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (failed) {
     request = false;
   }
 }
}
if (!request)
alert("Error initializing XMLHttpRequest!");
/********************************************/
var request2 = false;
try {
 request2 = new XMLHttpRequest();
} catch (trymicrosoft) {
 try {
   request2 = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (othermicrosoft) {
   try {
     request2 = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (failed) {
     request2 = false;
   }
 }
}
if (!request2)
alert("Error initializing XMLHttpRequest!");

/******************/
/* Trim functions */
/******************/
function LTrim( value ) {
var re = /\s*((\S+\s*)*)/;
return value.replace(re, "$1");
}
function RTrim( value ) {
var re = /((\s*\S+)*)\s*/;
return value.replace(re, "$1");
}
function trim( value ) {
return LTrim(RTrim(value));
}
/*************************/
/* End of trim functions */
/*************************/

/***************************/
/* Check for numeric input */
/***************************/

function IsNumeric(strString)
   {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length === 0) return false;
   for (i = 0; i < strString.length && blnResult === true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }
/***************************/
/* End of numeric input */
/***************************/
function Security(){
url = "PHP/checkSecurity.php";
request.open("GET", url, true);
request.onreadystatechange = SecurityResponse;
request.send(null);
}

function SecurityResponse(){
if (request.readyState == 4)
	{
	if (request.status == 200)
      		{
      		response = request.responseText;
      		response = trim(response);

      		if (response != 'M') window.location = "http://www.hospitalregisters.com/index.html";
      		CheckCookies();
      		} 
  	 else
   		{
   		alert("status is " + request.status);
   		}
   	} 
}
/******************************************/
/* These functions check through the form */
/* and calculate the number of hospitals  */
/* Called every time the form is changed  */
/******************************************/
function ShowClick() {

	if (document.getElementById("extra").style.visibility == 'visible')
		{
		SearchChanged(); 
		return;
		}
  
  List = ListCountries();
  Owners = ListOwners();
  Types = ListTypes();
  Specs = CountSpecs();

  if (!List || !Owners || !Types)
  	{
  	document.getElementById("found").innerHTML = 0;
  	return;
  	}
  
  Min = document.min.mininput.value;
  Min = parseInt(Min);
  Max = document.forms.max.maxinput.value;
  Max = parseInt(Max);

  params = "";
  params += "param1=" + encodeURIComponent(List);
  params += "&param2=" +  encodeURIComponent(Specs);
  params += "&param3=" +  encodeURIComponent(Max); 
  params += "&param4=" +  encodeURIComponent(Min);
  params += "&param5=" +  encodeURIComponent(Owners)
  params += "&param6=" +  encodeURIComponent(Types);

  
  var url = "PHP/getConnected.php";
  request.open("POST", url, true);

  request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  request.setRequestHeader("Content-Type", "text/html; charset=utf-8");
  request.setRequestHeader("Content-length", params.length);
  request.setRequestHeader("Connection", "close");

  request.onreadystatechange = updatePage;
  request.send(params);
}

number = 0;
function updatePage() {
if (request.readyState == 4)
 	{
   if (request.status == 200)
   	{
     	number = request.responseText;
     	document.getElementById("found").innerHTML = number;
   	} 
   else
     alert("status is " + request.status);
 	}
}

function ListCountries(){
CountryList = "";
Countries = new Array("Austria","Belgium","Cyprus","Czech Republic","Denmark","Faeroe Islands","Finland","France","Germany","Greece","Greenland","Iceland","Ireland","Italy","Luxembourg","Malta","Monaco","Netherlands","Norway","Portugal","Spain","Sweden","Switzerland","United Kingdom");
for (k=0; k<document.forms["states"].elements.length; k++)
	{
	CountryList = document.forms["states"].elements[k].checked?CountryList + "\'" + Countries[k] + "\',":CountryList;
	}
CountryList = CountryList.substring(0,CountryList.length-1);
return CountryList;
}

function ListOwners(){
OwnerList = "";
Owners = new Array("A","B","C","D");
for (k=0; k<4; k++)
	{
	OwnerList = document.owner.elements[k].checked?OwnerList + "\'" + Owners[k] + "\',":OwnerList;
	}
OwnerList = OwnerList.substring(0,OwnerList.length-1);
return OwnerList;
}

function ListTypes(){
TypeList = "";
Types = new Array("A","B","C","D","E","F");
for (k=0; k<6; k++)
	{
	TypeList = document.typelist.elements[k].checked?TypeList + "\'" + Types[k] + "\',":TypeList;
	}
TypeList = TypeList.substring(0,TypeList.length-1);
return TypeList;
}

function CountSpecs(){
var specs = "";
for (k=6;k<136;k++)
	{
	z = document.images[k].src;
	letter = z.charAt(z.length-5);

	switch (letter)
		{
		case "m":
		specs += "_";
  		break;

		case "y":
		specs += "1";
  		break;
  																			
		case "n":
		specs += "0";
  		break;
  		}
  	}
return specs;
}
/******************************/
/* End of the code to         */
/* find the number of records */
/******************************/

var timer;
function Stopper() {
clearTimeout(timer);
ShowClick();
}

function IncMinBeds () {
beds = document.forms.min.mininput.value;
beds = parseInt(beds);
if (beds < 3280)
	{beds += 20;
	document.forms.min.mininput.value = beds;
	}
maximumbeds = document.forms.max.maxinput.value;
maximumbeds = parseInt(maximumbeds);
if (beds >= maximumbeds)	
	{
	maximumbeds = beds + 1;
	document.forms.max.maxinput.value = maximumbeds;
	}
timer = setTimeout('IncMinBeds()', 250);
}

function DecMinBeds () {
beds = document.forms.min.mininput.value;
beds = parseInt(beds);
if (beds > 19)
	{beds -= 20;
	document.forms.min.mininput.value = beds;
	}
timer = setTimeout('DecMinBeds()', 250);
}

function IncMaxBeds () {
beds = document.forms.max.maxinput.value;
beds = parseInt(beds);
if (beds < 3300)
	{beds += 20;
	document.forms.max.maxinput.value = beds;
	}
timer = setTimeout('IncMaxBeds()', 250);
}

function DecMaxBeds () {
beds = document.forms.max.maxinput.value;
beds = parseInt(beds);
if (beds > 20)
	{beds -= 20;
	document.forms.max.maxinput.value = beds;
	}
minimumbeds = document.forms.min.mininput.value;
minimumbeds = parseInt(minimumbeds);
if (beds <= minimumbeds)	
	{
	minimumbeds = beds - 1;
	document.forms.min.mininput.value = minimumbeds;
	}	
timer = setTimeout('DecMaxBeds()', 250);
}

function CheckMinBeds(){
minbeds = document.forms.min.mininput.value;
maxbeds = document.forms.max.maxinput.value;

  /******************************************/
  /* Get input values & check for stupidity */
  /******************************************/
  minbeds = parseInt(minbeds);
  maxbeds = parseInt(maxbeds);
  if (minbeds < 0 || isNaN(minbeds)) minbeds = 0;
  if (minbeds > 3299) minbeds = 3299;
  if (minbeds >= maxbeds) maxbeds = minbeds + 1;

document.forms.min.mininput.value = minbeds;
document.forms.max.maxinput.value = maxbeds;
ShowClick();
}

function CheckMaxBeds(){
minbeds = document.forms.min.mininput.value;
maxbeds = document.forms.max.maxinput.value;

  /******************************************/
  /* Get input values & check for stupidity */
  /******************************************/
  minbeds = parseInt(minbeds);
  maxbeds = parseInt(maxbeds);
  if (maxbeds > 3300 || isNaN(maxbeds)) maxbeds = 3300;
  if (maxbeds < 1) maxbeds = 1;
  if (maxbeds <= minbeds) minbeds = maxbeds - 1;

document.forms.min.mininput.value = minbeds;
document.forms.max.maxinput.value = maxbeds;
ShowClick();
}

function ResetBeds(){
document.forms.min.mininput.value = 0;
document.forms.max.maxinput.value = 3300;
ShowClick();
}

function Results(){
result = document.getElementById("number").innerHTML;
result = parseInt(result);
if (result < 1) return;
window.location = 'Results.html';
}

<!-- *********************************** -->
<!-- Code to change tri-state checkboxes -->
<!-- *********************************** -->

function Change(x){
<!-- ******************************************* -->
<!-- y = x + 6 acounts for the six other images  -->
<!-- Length of the reference to the image file   -->
<!-- Step back 5 spaces to get the letter        -->
<!-- Position and read the letter                -->
<!-- ******************************************* -->

y = x + 6;
z = document.images[y].src;
letter = z.charAt(z.length-5);

	switch (letter)
		{
		case "m":	 													<!-- *************** -->
  		document.images[y].src = "Images/y.jpg";				<!-- If its a maybe  -->
  		ResetAny(x);													<!-- make it a yes   -->
  		break;															<!-- *************** -->
																			
		case "y":														<!-- *************** -->
  		document.images[y].src = "Images/n.jpg";				<!-- If its a yes,   -->
  		CheckAny(x);													<!-- make it a no    -->
  		break;															<!-- *************** -->
 		
		case "n":														<!-- *************** -->
  		document.images[y].src = "Images/m.jpg";				<!-- If its a no,    -->
  		break;  															<!-- make it a maybe -->
																			<!-- *************** -->
  																			
		default:
  		document.images[y].scr = "Images/y.jpg";
  		}
ShowClick();
}

<!-- ******************************************* -->
<!-- If an 'Any' checkbox is set to yes, then    -->
<!-- set all the other checkboxes in the group   -->
<!-- to maybe (m.jpg).                           -->
<!-- ******************************************* -->
function CheckAny(x){
any = [0,36,56,66,70,83,95,102,111,116,120,127,130];

for (k=0;k<any.length-1;k++)
	{
	z = document.images[any[k]+6].src;
	letter = z.charAt(z.length - 5);
														   
	if (x == any[k] && letter == "n")										
		{
		start = x + 7;
		end = any[k+1] + 7;	  											                       
		for (j=start;j<=end;j++)						
			{																			
			document.images[j].src = "Images/m.jpg";
			}
		}
	}
}

<!-- ***************************************** -->
<!-- If an 'Any' checkbox is set to no, then   -->
<!-- reset it to maybe if any box in the group -->
<!-- is set to yes (y.jpg).                    -->
<!-- ***************************************** -->
function ResetAny(x){
any = [0,36,56,66,70,83,95,102,111,116,120,127,130];
for (k=0;k<any.length-1;k++)
	{
	z = document.images[any[k]+6].src;
	letter = z.charAt(z.length - 5);
	if (x>any[k] && x<any[k+1] && letter == "n" )
		{
		document.images[any[k]+6].src = "Images/m.jpg";
		}
	}									
}

<!-- ********************************** -->
<!-- Cookie functions to maintain state -->
<!-- ********************************** -->

function CheckCookies(){
var pagestate = ReadCookie('pagestate'); 			// Try to get the cookie
if (!pagestate) 											// If it doesn't exist,
	{
	BakeCookie();											// make a new one
	pagestate = ReadCookie();							// and read its value
	}
setPageState(pagestate);								//  Reset the page to the new state
ShowClick();												//  Run the subroutine to get hospital numbers
}
function ReadCookie(pagestate) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function BakeCookie() {

var date = new Date();
date.setTime(date.getTime()+(200*24*60*60*1000));
var expires = "; expires="+date.toGMTString();

value = getPageState();
name = "pagestate";

document.cookie = name+"="+value+expires+"; path=/";
}

function getPageState(){
var countries = "";
for (k=0;k<document.states.length;k++)
	{
	if (document.states.elements[k].checked) countries += k + ",";
	}
	countries = countries.substring(0,countries.length-1);

var size = "";
size = document.min.elements[0].value + "," + document.max.elements[0].value;

var owner = "";
for (k=0;k<document.owner.length;k++)
	{
	if (document.owner.elements[k].checked) owner += k + ",";
	}
  owner = owner.substring(0,owner.length-1);

var type = "";
for (k=0;k<document.typelist.length;k++)
	{
	if (document.typelist.elements[k].checked) type += k + ",";
	}
  type = type.substring(0,type.length-1);

var specs = CountSpecs();

pagestate = countries + "#" + size + "#" + owner + "#" + type + "#" + specs;

return pagestate;
}

function setPageState(pagestate){
var bits = pagestate.split("#");
if (!bits[0]) return;
var countries = bits[0].split(",");
	form = document.states;
	for (k = 0; k < countries.length; k++)
		{
		index = countries[k];
		form.elements[index].checked = true;
		}

var size = bits[1].split(",");
document.min.elements[0].value = size[0];
document.max.elements[0].value = size[1];

var owners = bits[2].split(",");
for (k = 0; k < owners.length; k++)
	{
	index = owners[k];
	document.owner.elements[index].checked = true;
	}
	
var types = bits[3].split(",");
for (k = 0; k < types.length; k++)
	{
	index = types[k];
	document.typelist.elements[index].checked = true;
	}

var specs = bits[4];
for (k = 0; k < specs.length; k++)
	{
	z = specs.charAt(k);
	y = k + 6;

	switch (z)
		{
		case "_":
  		document.images[y].src = "Images/m.jpg";<!-- Its a maybe  -->
  		break;
  		
		case "1":
  		document.images[y].src = "Images/y.jpg";<!-- Its a yes   -->
  		break;
  		
		case "0":
  		document.images[y].src = "Images/n.jpg";<!-- Its a no,    -->
  		break;
  		
		default:
  		document.images[y].scr = "Images/m.jpg";
		}
	}
}

function Map(){
number = document.getElementById("found").innerHTML;
number = parseInt(number);
if (!number || number === 0)return;
url = "MapView.html";
window.open(url,'Map',"width = 820,height=620,resizable=no,scrollbars=no,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no");
}

/**************************************/
/** Code to handle exporting to file **/
/**************************************/

function DandF(state){
number = document.getElementById("found").innerHTML;
number = parseInt(number);
if (!number || number === 0)return;
document.getElementById("dandf").style.visibility = state;
document.getElementById("formattable").style.display = "block";
document.getElementById("selectformat").style.display = "block";
document.getElementById("dandf").style.left = '152px';
document.getElementById("dandf").style.height = '262px';
document.getElementById("dandf").style.backgroundImage = 'url(Images/dandfpanel.jpg)';
document.getElementById("dandfhead").innerHTML = "Select details to export";
}

function ListDetails(state){
number = document.getElementById("found").innerHTML;
number = parseInt(number);
if (!number || number === 0)return;

document.getElementById("dandf").style.visibility = state;
document.getElementById("formattable").style.display = "none";
document.getElementById("selectformat").style.display = "none";
document.getElementById("dandf").style.left = '225px';
document.getElementById("dandf").style.height = '225px';
document.getElementById("dandf").style.backgroundImage = 'url(Images/dpanel.jpg)';
document.getElementById("dandfhead").innerHTML = "Select details for list";
}

function CheckFields(){
FieldNames = new Array("name","street1","street2","town","postcode","region",
"country","telephone","fax","email","beds","ownership","type","web");
FieldList = "";
for (k = 0; k<=13;k++)
	{
	if (document.dandf.elements[k].checked === true)
		{
		FieldList = FieldList + FieldNames[k] + "~";
		}
	}
FieldList = FieldList.substring(0,FieldList.length-1);
return FieldList;
}

function ExportorList(){
position = parseInt(document.getElementById("dandf").style.left);
if (position >= 160) 
	{
	CreateList();
	}
else
	{
	Export();
	}
}

function Export(){
for (k=0; k<document.dandf.out.length; k++)
	{
	if (document.dandf.out[k].checked) filetype = (document.dandf.out[k].value);	
	}
switch(filetype)
	{
	case "Excel":
  		ExportExcel();
  		break;    
	case "Comma":
  		ExportComma();
  		break;
	case "Tab":
  		ExportTab();
  		break;
	}
}

function ExportExcel(){
fields = CheckFields();
if (!fields) return;
url = "PHP/createExcel.php?fields=" + fields;
request.open("GET", url, true);
request.onreadystatechange = ForceDownload;
request.send(null);
DandF('hidden');
}

function ExportComma(){
fields = CheckFields();
if (!fields) return;
url = "PHP/createComma.php?fields=" + fields;
request.open("GET", url, true);
request.onreadystatechange = ForceDownload;
request.send(null);
DandF('hidden');
}

function ExportTab(){
fields = CheckFields();
if (!fields) return;
url = "PHP/createTab.php?fields=" + fields;
request.open("GET", url, true);
request.onreadystatechange = ForceDownload;
request.send(null);
DandF('hidden');
}

function ForceDownload(){
if (request.readyState == 4)
	{
   if (request.status == 200)
      {
      window.location = "PHP/forceDownLoad.php";
      } 
   else
   	{
      alert("status is " + request.status);
      }
   }    
}

/*********************************************/
/* Code to handle creation of printable list */
/*********************************************/
function CreateList(){
fields = CheckFields();
if (!fields) return;
ListDetails('hidden')
url = "ListView.html?"+ fields;
window.open(url,'List',"width = 820,height=620,resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no");
}

<!-- ******************************** -->
<!-- Code to open  detailed data page -->
<!-- ******************************** -->

function Details(){
number = document.getElementById("found").innerHTML;
number = parseInt(number);
if (!number || number == 0)return;

url = "DM.html";

window.open(url,'Details',"width=770,resizable=no,scrollbars=yes,toolbar=no,location=no,directories=no,status=yes,menubar=no,copyhistory=no");
}

/*******************************/
/* Code to open geocoding page */
/*******************************/

function Geocode(){
number = document.getElementById("found").innerHTML;
number = parseInt(number);
if (!number || number == 0)return;

window.location = "Geocode.html"
}

/******************************/
/* Code to create tick sheets */
/******************************/

function Tick(){
number = document.getElementById("found").innerHTML;
number = parseInt(number);
if (!number || number == 0)return;

alert("Tick Sheets");
}

/**********************/
/* Add a new hospital */
/**********************/

function Add(){
newserial = Math.floor(Math.random()*100000) + 900000;
url = "Update.html?" + newserial;
window.open(url,'Update',"width = 780,height=580,resizable=no,scrollbars=no,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no");
}

/**************************/
/* Extra search functions */
/**************************/

function Extra(){
state = document.getElementById("extra").style.visibility;
state = (state == 'visible')?'hidden':'visible';
document.getElementById("extra").style.visibility = state;
document.getElementById("suggestions").style.visibility = state;
if (state == 'visible') SearchChanged();
if (state == 'hidden') ShowClick();
}

function SearchChanged(){
document.getElementById("suggestions").style.visibility = "hidden";
if (document.searchtype.elements[0].checked == true)
	{
	data = document.searchtype.searchvalue.value;
	Name(data);
	}
	
if (document.searchtype.elements[1].checked == true)
	{
	data = document.searchtype.searchvalue.value;
	Town(data);
	}

if (document.searchtype.elements[2].checked == true)
	{
	data = document.searchtype.searchvalue.value;
	Region(data);
	}

if (document.searchtype.elements[3].checked == true)
	{
	data = document.searchtype.searchvalue.value;
	Serial(data);
	}
}

function Name(data){
data = data.toLowerCase();
if (data == "")
	{
	document.getElementById("found").innerHTML = "0";
	document.getElementById("suggestions").style.visibility = "hidden";
	return;
	}
var url = "PHP/getNameData.php?name=" + encodeURIComponent(data);
request.open("GET", url, true);
request.onreadystatechange = writeName;
request.send(null);
}

function writeName(){
if (request.readyState == 4)
	{
	if (request.status == 200)
		{
     	var response = request.responseText;
     	response = trim(response);
     	document.getElementById("found").innerHTML = response;
     	}
	}
}

function Town(data){
data = data.toLowerCase(); 
List = ListCountries();
if (data == "" && List == "")
	{
	document.getElementById("found").innerHTML = "0";
	document.getElementById("suggestions").style.visibility = "hidden";
	return;
	}
	
firstlistedname = "";
if (document.getElementById("towntable"))
	{
	firstlistedname = document.getElementById("towntable").rows[0].cells[0].innerHTML;
	firstlistedname = firstlistedname.toLowerCase();
	}
data = data.replace(/\?/g,"_");

var url = "PHP/getTownData.php?town=" + encodeURIComponent(data);

if (List)
	{
	url += "&list=" + encodeURIComponent(List);
	}

request.open("GET", url, true);
request.onreadystatechange = writeTown;
request.send(null);
}

function writeTown(){
if (request.readyState == 4)
	{
   if (request.status == 200)
   		{
     	var response = request.responseText;
     	response = trim(response);
     	response = decodeURIComponent(response);
     	responseparts = new Array();
     	responseparts = response.split("#");
     	totalresults = responseparts[0];
     	listedresults = responseparts[1];
     	tableresult = responseparts[2];
     	document.getElementById("found").innerHTML = totalresults;
     	if (totalresults == 0) return;

     	document.getElementById("suggestions").style.visibility = "visible";
     	
     	document.getElementById("suggestions").innerHTML = "<h3>Suggestions</h3>" + tableresult;
     	if (listedresults == 1)
     		{
     		result = document.getElementById("towntable").rows[0].cells[0].innerHTML;
     		document.getElementById("searchvalue").value = result;
     		document.getElementById("suggestions").style.visibility = "hidden";
     		document.getElementById("searchvalue").blur();
     		soundPlay('stop');
     		}     
     	} 	
   	else
   		{
     	alert("status is " + request.status);
     	}
 	}
}

//====================================================================== 
var soundEmbed = null; 
//====================================================================== 
function soundPlay(which) 
    { 
    if (!soundEmbed) 
        { 
        soundEmbed = document.createElement("embed"); 
        soundEmbed.setAttribute("src",   which+".wav"); 
        soundEmbed.setAttribute("hidden", true); 
        soundEmbed.setAttribute("autostart", true); 
        } 
    else 
        { 
        document.body.removeChild(soundEmbed); 
        soundEmbed.removed = true; 
        soundEmbed = null; 
        soundEmbed = document.createElement("embed"); 
        soundEmbed.setAttribute("src", which+".wav"); 
        soundEmbed.setAttribute("hidden", true); 
        soundEmbed.setAttribute("autostart", true); 
        } 
    soundEmbed.removed = false; 
    document.body.appendChild(soundEmbed); 
    } 
//====================================================================== 


function MoveTown(k){
result = document.getElementById("towntable").rows[k].cells[0].innerHTML;
document.getElementById("searchvalue").value = result;
document.getElementById("suggestions").style.visibility = "hidden";
document.getElementById("suggestions").innerHTML = "";
SearchChanged();
}

function Region(data){
data = data.toLowerCase();
if (data == "")
	{
	document.getElementById("found").innerHTML = "0";
	document.getElementById("suggestions").style.visibility = "hidden";
	return;
	}
firstlistedname = "";
if (document.getElementById("regiontable"))
	{
	firstlistedname = document.getElementById("regiontable").rows[0].cells[0].innerHTML;
	firstlistedname = firstlistedname.toLowerCase();
	}
data = data.replace(/\?/g,"_");
var url = "PHP/getRegionData.php?region=" + encodeURIComponent(data);
request.open("GET", url, true);
request.onreadystatechange = writeRegion;
request.send(null);	
	
}

function writeRegion(){
if (request.readyState == 4)
	{
   if (request.status == 200)
   		{
     	var response = request.responseText;
     	response = trim(response);
     	responseparts = new Array();
     	responseparts = response.split("#");
     	totalresults = responseparts[0];
     	listedresults = responseparts[1];
     	tableresult = responseparts[2];
     	document.getElementById("found").innerHTML = totalresults;
     	if (totalresults == 0) return;

     	document.getElementById("suggestions").style.visibility = "visible";
     	
     	document.getElementById("suggestions").innerHTML = "<h3>Suggestions</h3>" + tableresult;
     	if (listedresults == 1)
     		{
     		result = document.getElementById("regiontable").rows[0].cells[0].innerHTML;
     		document.getElementById("searchvalue").value = result;
     		document.getElementById("suggestions").style.visibility = "hidden";
     		}     
     	} 	
   	else
   		{
     	alert("status is " + request.status);
     	}
 	}
}

function MoveRegion(k){
result = document.getElementById("regiontable").rows[k].cells[0].innerHTML;
document.getElementById("searchvalue").value = result;
document.getElementById("suggestions").style.visibility = "hidden";
document.getElementById("suggestions").innerHTML = "";
SearchChanged();
}

function Serial(data){
data = parseInt(data);

if (data == "")
	{
	document.getElementById("found").innerHTML = "0";
	return;
	}
var url = "PHP/getSerialData.php?serial=" + escape(data);
request.open("GET", url, true);
request.onreadystatechange = writeSerial;
request.send(null);
}

function writeSerial(){
if (request.readyState == 4)
	{
	if (request.status == 200)
		{
     	var response = request.responseText;
     	response = trim(response);
     	document.getElementById("found").innerHTML = response;
     	}
	}
}

function EvalSound(soundobj) {
var thissound=document.getElementById(soundobj);
thissound.Play();
}

