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 = "../../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() {
  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=" + List + "&param2=" + Specs;
  params += "&param3=" + Max + "&param4=" + Min;
  params += "&param5=" + Owners + "&param6=" + Types;
    
  var url = "PHP/getConnected.php";
  request.open("POST", url, true);

  request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  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("Alabama", "Alaska", "Arkansas", "Arizona", "California", "Colorado", "Connecticut", "District of Columbia", "Delaware", "Florida", "Georgia", "Hawaii", "Iowa", "Idaho", "Illinois", "Indiana", "Kansas", "Kentucky", "Louisiana", "Massachusetts", "Maryland", "Maine", "Michigan", "Minnesota", "Missouri", "Mississippi", "Montana", "North Carolina", "North Dakota", "Nebraska", "New Hampshire", "New Jersey", "New Mexico", "Nevada", "New York", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington","Wisconsin", "West Virginia", "Wyoming");
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");
for (k=0; k<3; 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<137;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 < 2480)
	{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 < 2500)
	{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 > 2499) minbeds = 2499;
  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 > 2500 || isNaN(maxbeds)) maxbeds = 2500;
  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 = 2500;
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('Apagestate'); 					// 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(name) {
	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 = "Apagestate";

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";
		}
	}
}
<!-- End of state maintaining code -->
function Map(){
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){
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){
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","zip","statename",
"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;alert(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(){
url = "Details.html";
window.open(url,'Details',"width=750,height=620,resizable=no,scrollbars=no,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no");
}

<!-- ****************** -->
<!-- Security Functions -->
<!-- ****************** -->

function CheckValidity(){
url = "PHP/checkValidity.php";
request2.open("GET", url, true);
request2.onreadystatechange = ValidityResponse;
request2.send(null);
}

function ValidityResponse(){
if (request2.readyState == 4)
 	{
   if (request2.status == 200)
   		{
     	response = request2.responseText;
     	response = trim(response);

     	if (response == "OK") return;
     
     	if (response == "Log" )
     		{
     		message = "You are not logged in.\r\n\r\n";
     		message += "Please log in or\r\nuse the public part\r\nof the site.";
     		alert(response);
     		window.location = "http://www.hospitalregisters/index.html";
     		}
     	}
   else
   		{
     	alert("status is " + request.status);
     	}
 	}
}

function RegainControl()
	{
	url = "PHP/getControl.php";
	request2.open("GET", url, true);
	request2.onreadystatechange = ValidityResponse;
	request2.send(null);
	}

function Add(){
serial = Math.floor(Math.random()*100000) + 100000;
url = "Update.html?" + serial;
window.open(url,'Update',"width = 780,height=580,resizable=no,scrollbars=no,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no");
}
