// JavaScript Document
var requester = null;
var formRequester = null;
//MOVED BELOW STATEMENT TO BODY TAG
//window.onload = init;

/* Initialisation for JS capable browsers */
function init()
{
//window.alert("init called");

	var orderForm = document.getElementById("orderForm");
    var cat2 = document.getElementById("cat2");
	var cat = document.getElementById("cat");
    var franchiseTypeDescription = document.getElementById("franchiseTypeDescription");
    var IndustrySectorName = document.getElementById("IndustrySectorName");
    var countyId = document.getElementById("countyId");

//the form controls that fire off onChange events. Which call onChangeReceipt(). Signifying user request
franchiseTypeDescription.onchange = onchangeReceipt;
cat2.onchange = onchangeReceipt;
IndustrySectorName.onchange = onchangeReceipt;
countyId.onchange = onchangeReceipt;
cat.onchange = onchangeReceipt;

//calling this here (on page loading) means all franchisors are displayed when page is first displayed.
onchangeReceiptOrder("companyName");
return true;
}

//not passing in a value here causes application to fail in Firefox.
//so created onchangeReceipt() solely to handle event calls above,
function onchangeReceiptOrder(order)
{ //window.alert("onchangeReceipt");

  if(arguments.length == 0)
  {
	 alert("order undefined");
	  orderBy = "companyName";
  }
  else
  {
	   orderBy = order;
   }
  
   
   var orderForm = document.getElementById("orderForm");
   
  
   //alert(orderForm.cat.value);

	if (requester != null && requester.readyState != 0 && requester.readyState != 4)
	{
		requester.abort();
	}

	try
	{
		requester = new XMLHttpRequest();
	}
	catch (error)
	{
		try
		{
			requester = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (error)
		{
			requester = null;

			return false;
		}
	}

	requester.onreadystatechange = onreadystatechangeReceipt;

	requester.open("GET", "/regionDirectories/controller.cfc?method=main&FranchiseTypeDescription=" + orderForm.franchiseTypeDescription.value + "&cat2=" + orderForm.cat2.value + "&IndustrySectorName=" + orderForm.IndustrySectorName.value + "&countyId=" + orderForm.countyId.value + "&cat=" + orderForm.cat.value + "&orderBy=" + orderBy + "&regionId=" + orderForm.regionId.value);
	requester.send(null);

	return true;
}





//change Receipt does the same as above except it always passes companyName as the orderBy value.
function onchangeReceipt()
{ //alert("onchangeReceipt");


   
   var orderForm = document.getElementById("orderForm");
   
    //alert(orderForm.receipt.value);
  //alert(orderForm.cat.value);

	if (requester != null && requester.readyState != 0 && requester.readyState != 4)
	{
		requester.abort();
	}

	try
	{
		requester = new XMLHttpRequest();
	}
	catch (error)
	{
		try
		{
			requester = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (error)
		{
			requester = null;

			return false;
		}
	}

	requester.onreadystatechange = onreadystatechangeReceipt;

	requester.open("GET", "/regionDirectories/controller.cfc?method=main&FranchiseTypeDescription=" + orderForm.franchiseTypeDescription.value + "&cat2=" + orderForm.cat2.value + "&IndustrySectorName=" + orderForm.IndustrySectorName.value + "&countyId=" + orderForm.countyId.value + "&cat=" + orderForm.cat.value + "&orderBy=" + 'companyName' + "&regionId=" + orderForm.regionId.value);
	requester.send(null);

	return true;
}



/*
Excutes when requester.onreadystatechange fires
calls writeDetails() which outputs results to page.

Ready States:
0 = uninitialized
1 = loading
2 = loaded
3 = interactive
4 = complete 
*/
function onreadystatechangeReceipt()
{    
    //show loading gif while loading
	if (requester.readyState == 1)
	{ //alert("readyState1");
	
	document.getElementById("loading").innerHTML = "<img src='/images/loadingGraphic.gif'>";
	//document.getElementById("loadingText").innerHTML = "";
	document.getElementById("loadingRight").innerHTML = "<img src='/images/loadingGraphic.gif'>";
	//document.getElementById("loadingTextRight").innerHTML = "";
	document.getElementById("displayResults").innerHTML = "<img src='/images/loading.gif'><br>Loading...";
	}
	
	// If XMLHR object has finished retrieving the data
	// remove loading notification and display results
	if (requester.readyState == 4)
	{
		document.getElementById("loading").innerHTML = "";
		//document.getElementById("loadingText").innerHTML = "";
		document.getElementById("loadingRight").innerHTML = "";
	    //document.getElementById("loadingTextRight").innerHTML = "";
		//alert("readyState4");
		
		// If the data was retrieved successfully
		try
		{   //http status of 200 indicates successful retrival
			if (requester.status == 200)
			{   //alert("parseXMLRecordSet");
				parseXMLRecordSet();
			}
			// IE returns a status code of 0 on some occasions, so ignore this case
			else if (requester.status != 0)
			{
				alert("There was an error while retrieving the URL: " + requester.statusText + requester.status);
			}
		}
		catch (error)
		{
		}
	}

	return true;
}
var recordsReturnedNode = 0;
var recordsReturnedTextNode = 0;
var recordsReturned= 0;

var company_idNode = 0;
var company_idTextNode = 0;
var company_id= 0;


var company_nameNode = null;
var company_nameTextNode = null;
var company_name= null;

var county_nameNode = null;
var county_nameTextNode = null;
var county_name = null;

var ftype_nameNode = null;
var ftype_nameTextNode = null;
var ftype_name= null;

var mpi_nameNode = null;
var mpi_nameTextNode = null;
var mpi_name = null;

var mti_nameNode = null;
var mti_nameTextNode = null;
var mti_name= null;

var sector_nameNode = null;
var sector_nameTextNode = null;
var sector_name = null;


var displayString;
var displayResultsFound;

/* Writes results to page */

function parseXMLRecordSet()
{
//alert("parseRecordSet");

//get a count of the number of results found 
recordsReturnedNode = requester.responseXML.getElementsByTagName("results_found")[0];
//get the textnode of that element - it should always be in position ZERO 
recordsReturnedTextNode = recordsReturnedNode.childNodes[0];
//get the value of that textNode
recordsReturned = recordsReturnedTextNode.nodeValue;
displayNumberResultsFound = recordsReturned;

//start of html results table 
displayString = "<table cellspacing='0' width='100%' border='0' class='regional_content1'>";

//alert("loop length is");
//alert(requester.responseXML.getElementsByTagName("company_name").length);

//determine length of loop from number of records returned in XML response
 for(i=0;i<requester.responseXML.getElementsByTagName("company_id").length;i++)
{
//alert("loop count is");
//alert(i);
//get the next element in data
company_idNode = requester.responseXML.getElementsByTagName("company_id")[i];
//get the textnode of that element - it should always be in position ZERO 
company_idTextNode = company_idNode.childNodes[0];
//get the value of that textNode
company_id = company_idTextNode.nodeValue;


//get the next element in data
company_nameNode = requester.responseXML.getElementsByTagName("company_name")[i];
//get the textnode of that element - it should always be in position ZERO 
company_nameTextNode = company_nameNode.childNodes[0];
//get the value of that textNode
company_name = company_nameTextNode.nodeValue;

//get the next element in data
county_nameNode = requester.responseXML.getElementsByTagName("county")[i];
//get the textnode of that element - it should always be in position ZERO 
county_nameTextNode = county_nameNode.childNodes[0];
//get the value of that textNode
county_name = county_nameTextNode.nodeValue;

//get the next element in data
ftype_nameNode = requester.responseXML.getElementsByTagName("ftype")[i];
//get the textnode of that element - it should always be in position ZERO 
ftype_nameTextNode = ftype_nameNode.childNodes[0];
//get the value of that textNode
ftype_name = ftype_nameTextNode.nodeValue;

//get the next element in data
mpi_nameNode = requester.responseXML.getElementsByTagName("mpi")[i];
//get the textnode of that element - it should always be in position ZERO 
mpi_nameTextNode = mpi_nameNode.childNodes[0];
//get the value of that textNode
mpi_name = mpi_nameTextNode.nodeValue;

//get the next element in data
mti_nameNode = requester.responseXML.getElementsByTagName("mti")[i];
//get the textnode of that element - it should always be in position ZERO 
mti_nameTextNode = mti_nameNode.childNodes[0];
//get the value of that textNode
mti_name = mti_nameTextNode.nodeValue;

//get the next element in data
sector_nameNode = requester.responseXML.getElementsByTagName("sector")[i];
//get the textnode of that element - it should always be in position ZERO 
sector_nameTextNode = sector_nameNode.childNodes[0];
//get the value of that textNode
sector_name = sector_nameTextNode.nodeValue;


//alert(company_name);
//build middle part of HTML table containing results to be displayed
displayString = displayString + "<tr><td  width='23%'><a href='/franchisorPage.cfm?companyId=" + company_id + "'>" + company_name + "</a></td><td style='padding:4px;' width='15%'>" + county_name + "</td><td width='15%' align='center' >£" + mti_name + "</td><td width='15%' align='center'>£" + mpi_name + "</td><td width='15%'  >" + ftype_name + "</td><td width='15%'>" + sector_name + "</td></tr>";
}
//alert(displayString);
//end of HTML table
displayString = displayString + "</table>";
//document.getElementById("displayResults").innerHTML = displayString;

writeToScreen();
//return true;
}

function writeToScreen()
{   //alert("writeToScreen");
	//display HTML table of returned records
	document.getElementById("displayResults").innerHTML = displayString;
	//display number of records found
	document.getElementById("resultsFound").innerHTML = displayNumberResultsFound;
}


