phpFile="/Containers/N4ContainerQuery.php";
jsFile="/Containers/N4ContainerQuery.js";
includeEdittingLinks = false;
includeLinkingLinks = false;
releaseTrackingPage="releases.html";

var directConnection = true;//directConnection doesn't need the php proxy
var dcUsername = ""; // remove real user before deployment
var dcPassword = ""; // remove real password before deployment
if (window.location.host == 'www.porttaranaki.co.nz')
  directConnection = false;



var VariableSuffix = "SearchVariable";

//Define the searchConfig object
function SearchConfig(_name, _filter, _parameterName, _heading, _noDataMessage, _baseSearch, _isCount, _isSNXQuery)
{
	this.name = _name;
	this.filter = _filter;
	this.parameterName = _parameterName;
	this.heading = _heading;
	this.noDataMessage = _noDataMessage;
	this.baseSearch = _baseSearch;
	this.isCount = _isCount;
	this.isSNXQuery = _isSNXQuery;

	this.stopColumns = new Array();
	this.unitNbrCol = -1;
	this.bookingCol = -1;
	this.dataQueries = new Array();
	this.timesDelayed = 0;

	this.ProgressSuffix = "SearchProgress";
	this.MatchesSuffix = "PossibleMatches";
	this.HeaderSuffix = "Header";
	this.ResultsSuffix = "SearchResults";
	this.VariableSuffix = VariableSuffix;
	this.NoDataSuffix = "NoDataFound";
	this.URLSuffix = "URL";

	this.setupStructures(true);
}

SearchConfig.prototype.setData = function(_data)
{
	this.data = _data;
	this.rows = null;
}

SearchConfig.prototype.getRows = function()
{
	if (this.rows == null)
	{
		if (currentSearch.isSNXQuery)
		{
			this.rows = data.getElementsByTagName("item");
			//alert(this.rows);
		}
		else
		{
			this.rows = data.getElementsByTagName("row");
		}
	}
	return this.rows;
}

SearchConfig.prototype.getHeadings = function()
{
	var headings
	if (currentSearch.isSNXQuery)
	{
		if (this.rows.length > 0)
			headings = this.getRows()[0].attributes;
		//alert("Headings: "+headings);
	}
	else
	{
		headings = data.getElementsByTagName("column");
	}
	return headings
}

SearchConfig.prototype.getFields = function(data)
{
	var fields;
	if (currentSearch.isSNXQuery)
	{
		fields = data.attributes;
	}
	else
	{
	    fields = data.getElementsByTagName("field")
	}
	return fields;
}

SearchConfig.prototype.getText = function(node)
{
	if (node.textContent==null)
		return node.text; // IE specific line :(
	else
		return node.textContent;
}

SearchConfig.prototype.getTitle = function(node)
{
	var title;
	if (this.isSNXQuery)
	{
		title = node.name.replace(/-/g," ");
		var newTitle = "";
		for (var i = 0; i<title.length; i++)
		{
			if (i==0)
				newTitle += title.charAt(0).toUpperCase();
			else if (title.charAt(i-1) == " ")
				newTitle += title.charAt(i).toUpperCase();
			else
				newTitle += title.charAt(i);
		}
		title = newTitle.replace(/ Iso /g, " ISO ");
	}
	else 
	{
		title = this.getText(node);
	}
	return title
}

SearchConfig.prototype.getFieldValue = function(data, field)
{
	var fieldVal;
	fieldVal = this.getText(field);
	fieldVal = fieldVal.replace(/NOM(.)0/g,"$10&prime;").replace(/NOM(.)(.)/,"$1&prime;$2");
	return fieldVal;
}

//Define the getDiv method to ensure we have the divs we need
SearchConfig.prototype.getDiv = function(divName, classes, content, forgiving)
{
	var div = document.getElementById(this.name+divName);
	if (div == null)
	{
		div = document.createElement("div");
		div.setAttribute("id",this.name+divName);
		div.setAttribute("class",classes);
		div.innerHTML = content;
		var baseDiv = document.getElementById(this.baseSearch);
		if (baseDiv == null)
		{
			if  (!forgiving)
			{
				alert("Unable to find base DIV for '"+this.baseSearch+"'.  Add '<div id="+this.baseSearch+"></div>' to your document.");
			}
			return;
		}
		baseDiv.appendChild(div);
	}
	return div;
}

SearchConfig.prototype.getVariable = function()
{
	var variableID = this.name+this.VariableSuffix;
	var variableElement = document.getElementById(variableID);
	if (variableElement!=null)
		return variableElement.value;
	else
		alert("getVariable: "+variableID+" not found");
	return "";
}

SearchConfig.prototype.startProgress = function()
{
	this.setupStructures(false);
	this.searchProgressDiv.innerHTML = "<img src='/images/ajax-loader.gif'>";
	this.searchProgressDiv.style.display = "block";
	return this.searchProgressDiv;
}

SearchConfig.prototype.endProgress = function()
{
	this.setupStructures(false);
	this.searchProgressDiv.innerHTML = "";
	this.searchProgressDiv.style.display = "none";
}

SearchConfig.prototype.showURL = function(url)
{
	this.setupStructures(true);
	this.urlDiv.style.display="block";
	this.urlDiv.innerHTML=url;
}

SearchConfig.prototype.setupStructures = function(forgiving)
{
	if (this.headerDiv == null)
		this.headerDiv = this.getDiv(this.HeaderSuffix, "trackingHeader trackingDiv", this.heading, forgiving);
	if (this.noDataDiv == null)
		this.noDataDiv = this.getDiv(this.NoDataSuffix, "trackingDiv", this.noDataMessage, forgiving);
	if (this.possibleMatchesDiv == null)
		this.possibleMatchesDiv = this.getDiv(this.MatchesSuffix, "trackingDiv", "", forgiving);
	if (this.resultsDiv == null)
		this.resultsDiv = this.getDiv(this.ResultsSuffix, "trackingDiv trackingResult", "", forgiving);
	if (this.searchProgressDiv == null)
		this.searchProgressDiv = this.getDiv(this.ProgressSuffix, "trackingDiv", "", forgiving);
	if (this.urlDiv == null)
		this.urlDiv = this.getDiv(this.URLSuffix, "trackingDiv", "", forgiving);
}
//Define the searchConfig.run() method
SearchConfig.prototype.run = function(display)
{
	this.setupStructures(false);

	if (display)
	{
		if (this.name != this.baseSearch)
		{
			var baseSearchVar = document.getElementById(this.baseSearch+this.VariableSuffix);
			if (baseSearchVar == null)
			{
				alert("Unable to find base search variable '"+this.baseSearch+this.VariableSuffix+"'");
				return;
			}
			var searchVar = document.getElementById(this.name+this.VariableSuffix);
			if (searchVar == null)
			{
				searchVar = document.createElement("input");
				searchVar.setAttribute("type","hidden");
				searchVar.setAttribute("id",this.name+this.VariableSuffix);
				searchVar.setAttribute("name",this.name);
				baseSearchVar.parentNode.appendChild(searchVar);
			}
			searchVar.value=baseSearchVar.value;
		}
		
		this.headerDiv.style.display="block";
		this.resultsDiv.innerHTML="";
		this.resultsDiv.style.display="block";

		getSearchResults(this);
	}
	else
	{
		this.headerDiv.style.display="none";
		this.resultsDiv.style.display="none";
		this.noDataDiv.style.display="none";			
	}
}

var containerAndHistoryConfig = new SearchConfig("container","EQUIPMENTLISTFORWEB","PARM_PARTIALCNTR", "Container","No Such Container Found","container", true,false);
containerAndHistoryConfig.dataQueries.push(new SearchConfig("container","EQUIPMENTDETAILSFORWEB","PARM_PARTIALCNTR","Container", "No Such Container Found", "container",false,false));
containerAndHistoryConfig.dataQueries.push(new SearchConfig("visits","CNTRDETAILSEQUIPMENTFORWEB","PARM_PARTIALCNTR","Visits", "No Such Container Found", "container",false,false));
containerAndHistoryConfig.dataQueries.push(new SearchConfig("clearance","CNTRDETAILSCLEARANCEFORWEB","PARM_PARTIALCNTR","Clearances", "No Such Container Found", "container",false,false));
containerAndHistoryConfig.dataQueries.push(new SearchConfig("cntrmove","CNTRHISTORYDETAILSFORWEB","PARM_PARTIALCNTRMOVE","Moves", "No Container Moves Found", "container",false,false));

var containerBookingAndHistory = new SearchConfig("booking","CNTRLISTFORWEB","PARM_PARTIALBOOKING", "Release", "No Such Release or Booking Exists","booking",true,false);
containerBookingAndHistory.dataQueries.push(new SearchConfig("bookingvisits","CNTRDETAILSEQUIPMENTFORWEB","PARM_PARTIALBOOKING","Visits", "No Such Container Found", "booking",false,false));
containerBookingAndHistory.dataQueries.push(new SearchConfig("bookingclearances","CNTRDETAILSCLEARANCEFORWEB","PARM_PARTIALBOOKING","Clearances", "No Such Container Found", "booking",false,false));
containerBookingAndHistory.dataQueries.push(new SearchConfig("bookingmove","CNTRHISTORYDETAILSFORWEB","PARM_PARTIALBOOKINGMOVE","Moves", "No Container Moves Found", "booking",false,false));
//containerBookingAndHistory.dataQueries.push(new SearchConfig("booking","CNTRDETAILSFORWEB","PARM_PARTIALBOOKING","Visits", "No Container Has Visited on that Booking", "booking",false));
//containerBookingAndHistory.dataQueries.push(new SearchConfig("bookingmove","CNTRHISTORYDETAILSFORWEB","PARM_PARTIALBOOKINGMOVE","Moves", "No Container Moves Found", "booking",false));

var releaseAndContainers = new SearchConfig("edo","LISTEDOSFORWEB","PARM_PARTIALEDO","Release","No Such Release Exists","edo",true,false);
releaseAndContainers.dataQueries.push(new SearchConfig("edo","EDODETAILSFORWEB","PARM_PARTIALEDO","Release", "No Such Release Found", "edo",false,false));
releaseAndContainers.dataQueries.push(new SearchConfig("edoitems","EDODETAILSFORWEB","PARM_PARTIALEDO","Release Items","No Order Items Defined","edo",false,true));
releaseAndContainers.dataQueries.push(new SearchConfig("delivery","CNTRDETAILSFORWEBRELEASETRACKING","PARM_PARTIALBOOKING","Gate Outs", "No Such Container Found", "edo",false,false));
releaseAndContainers.dataQueries.push(new SearchConfig("receival","CNTRDETAILSFORWEBBOOKINGTRACKING","PARM_PARTIALBOOKING","Gate Ins", "No Such Container Found", "edo",false,false));

var receivalsAndDeliveries = new SearchConfig("rnd","EQUIPMENTLISTFORWEB","PARM_PARTIALCNTR","Release","No Such Release Exists","rnd",true,false)
receivalsAndDeliveries.dataQueries.push(new SearchConfig('rnd','EQUIPMENTDETAILSFORWEB','PARM_PARTIALCNTR',"Containers", "No Such Container Found", "rnd",false,false));
receivalsAndDeliveries.dataQueries.push(new SearchConfig('rndDelivery','CNTRDETAILSFORWEBRELEASETRACKING','PARM_PARTIALCNTR',"Gate Outs", "No Such Container Found", "rnd",false,false));
receivalsAndDeliveries.dataQueries.push(new SearchConfig('rndReceival','CNTRDETAILSFORWEBBOOKINGTRACKING','PARM_PARTIALCNTR',"Gate Ins", "No Such Container Found", "rnd",false,false));


var currentSearch = new Object();

var dataRequest;
var requestInProgress = false;

var formattingFirstRow = false;
var formattingLastRow = false;

function createRequest()
{
	var request = null;
	try{
		request = new XMLHttpRequest();
	}catch(microsoft){
		try{
			request = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(othermicrosoft){
			try{
				request = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(failed){
				request = null;
			}
		}
	}
	if (request == null)
		alert("Unable to proceed due to failure to create 'Request' object");
	return request;
}

// Get on with creating it already...
dataRequest = createRequest();
if (dataRequest == null)
	alert("Unable to create server connections: "+dataRequest);	

function requestData(config)
{
	var parameterValue = config.getVariable();
	
	
	if (dataRequest != null && !requestInProgress && parameterValue != "" && parameterValue != "**")
	{
		requestInProgress=true;
		currentSearch = config;

		var ajaxRequest;
		if (directConnection)
			ajaxRequest="/n4api/apex/api/query?";
		else
			ajaxRequest="N4ContainerQuery.php?";
			
		ajaxRequest+="filtername="+config.filter+"&";
		//ajaxRequest+=""+parameterName+"="+parameterValue.replace(/\*/g,"").replace(/\?/g,"")+"&";
		if (config.isSNXQuery)
			ajaxRequest+="mode=SNX&";
		ajaxRequest+=""+config.parameterName+"="+parameterValue+"&";
		ajaxRequest+="operatorId=PTL&";
		ajaxRequest+="complexId=NZNPL&";
		ajaxRequest+="facilityId=NPL&";
		ajaxRequest+="yardId=BCT";
		//alert(ajaxRequest);
		
		if (directConnection)
			dataRequest.open("GET", ajaxRequest, true, dcUsername, dcPassword);
		else
			dataRequest.open("GET", ajaxRequest, true);
		
		if (config.isCount)
		{
			dataRequest.onreadystatechange = processListOfContainers;
		}
		else
		{
			dataRequest.onreadystatechange = processResults;
		}
		dataRequest.send(null);

		//currentSearch.showURL(ajaxRequest);
		
		return true;
	}
	else
	{
		return false;
	}
}

function noDataFound()
{
	currentSearch.endProgress();
	var noDataElement = currentSearch.getDiv(currentSearch.NoDataSuffix,"trackingDiv",currentSearch.noDataMessage);
	noDataElement.style.display = "block";
}

function dataFound()
{
	currentSearch.endProgress();

	var noDataElement = currentSearch.getDiv(currentSearch.NoDataSuffix,"trackingDiv",currentSearch.noDataMessage);
	if (noDataElement == null)
		alert(search.prefix+search.NoDataSuffix+" element not found");
	else
	{
		noDataElement.style.display = "none";
	}
}

//Create the array to stored delayed searches
var delayedSearches = new Array();

function getSearchResults(searchConfig)
{
	if (requestInProgress)
	{
		searchConfig.timesDelayed++;
		delayedSearches.push(searchConfig);
	}
	else
	{
		//alert(searchConfig.name+": "+searchConfig.getVariable());
		requestData(searchConfig);
	}
}

// Create the function to run the delayed searches
function runQueuedSearches()
{
	if (delayedSearches.length > 0)
	{
		//get the first element
		var search = delayedSearches[0];
		//"shift" all the searches up one
		delayedSearches.shift();
		//run the search again
		getSearchResults(search);
	}
	//make sure we run it again
	setTimeout(runQueuedSearches, 100);
}

// Start the queue running
runQueuedSearches();


var previousPartialContainer="";
var previousPartialBooking="";
var previousPartialEDO="";

function clearHistory()
{
	previousPartialContainer="";
	previousPartialBooking="";
	previousPartialEDO="";
}

function getNumberOfCntrMatches()
{
	if (requestInProgress)
	{
		//This function is fired off continually at the bottom 
		//so we don't need a delay phase
	}
	else
	{
		// Matches for Containers
		var config = containerAndHistoryConfig;
		var partialContainer = config.getVariable();
		if (partialContainer!=previousPartialContainer)
		{
			requestData(config);
			
			previousPartialContainer = partialContainer;
		}
	}
	
	// and repeat
	setTimeout("getNumberOfCntrMatches()",100);
}

function getNumberOfBookingMatches()
{
	if (requestInProgress)
	{
		//This function is fired off continually at the bottom 
		//so we don't need a delay phase
	}
	else
	{
		// Matches for Containers
		var config = containerBookingAndHistory;
		var partialBooking = config.getVariable();
		if (partialBooking!=previousPartialBooking)
		{
			requestData(config);
			
			previousPartialBooking = partialBooking;
		}
	}
	
	// and repeat
	setTimeout("getNumberOfBookingMatches()",100);
}

function getNumberOfEDOMatches()
{
	if (requestInProgress)
	{
		//This function is fired off continually at the bottom 
		//so we don't need a delay phase
	}
	else
	{
		// Matches for bookings
		var config = releaseAndContainers;
		var partialEDO = "*"+config.getVariable()+"*";
		if (partialEDO != previousPartialEDO && partialEDO != "**")
		{
			requestData(config);
			
			previousPartialEDO = partialEDO;
		}
	}
	
	// and repeat
	setTimeout("getNumberOfEDOMatches()",100);
}

function getNumberOfRnDMatches()
{
	if (requestInProgress)
	{
		//This function is fired off continually at the bottom 
		//so we don't need a delay phase
	}
	else
	{
		// Matches for bookings
		var config = receivalsAndDeliveries;
		var partialContainer = "*"+config.getVariable()+"*";
		if (partialContainer!=previousPartialContainer && partialContainer != "**")
		{
			requestData(config);
			
			previousPartialContainer = partialContainer;
		}
	}
	
	// and repeat
	setTimeout("getNumberOfRnDMatches()",100);
}

function searchForContainerAndHistory(display)
{
	processDataQueries(containerAndHistoryConfig,display);
}

function searchForContainerBookingAndHistory(display)
{
	processDataQueries(containerBookingAndHistory,display);
}

function searchForReleaseAndContainers(display)
{
	processDataQueries(releaseAndContainers,display);
}

function searchForRnD(display)
{
	processDataQueries(receivalsAndDeliveries,display);
}

function processDataQueries(config, display)
{
	for (var i = 0; i<config.dataQueries.length;i++ )
	{
		config.dataQueries[i].run(display);
	}
}

function processResults()
{
	var searchProgressElement = currentSearch.startProgress();
	
	if (dataRequest.readyState == 4)
	{
		if (dataRequest.status == 200)
		{
			searchProgressElement.innerHTML = "";
			if (dataRequest.responseText == "")
			{
				noDataFound();
			}
			else
			{
				dataFound();

				var searchResultsElement = currentSearch.getDiv(currentSearch.ResultsSuffix);
				if (searchResultsElement == null)
				{
					alert("Unable to find page element \""+currentSearch.parameterName+currentSearch.ResultsSuffix+"\"");
				}
				else
				{
					data = dataRequest.responseXML;
					
					if (data != null )
					{
						//Show the data
						var currentRowStyle = "pinkline";
						var otherRowStyle = "whiteline";
						var swapRowStyle = "whiteline";
						var html = "";
						
						formatListRow = "formatListRowSimple"
						
						currentSearch.setData(data);
						var rows = currentSearch.getRows();
						
						//if (currentSearch.isSNXQuery)
						//{
						//	rows = data.getElementsByTagName("item");
							//alert(rows);
						//}
						//else
						//{
						//	rows = data.getElementsByTagName("row");
						//}
						
						if (rows.length > 0)
						{
							html+=formatListRowSimple(null, currentSearch.getHeadings(),null,true);
						}
						
						formattingFirstRow = true;
						if (rows.length == 0)
						{
							noDataFound();
						}
						else
						{
							for (var i = 0; i < rows.length; i++)
							{
								html+=formatListRowSimple(i, rows[i],currentRowStyle);
								swapRowStyle=currentRowStyle;
								currentRowStyle=otherRowStyle;
								otherRowStyle=swapRowStyle;
								
								formattingFirstRow = false;
								if ((i+1)==rows.length)
								{
									formattingLastRow = true;
								}
							}
						}
						html +="</table>";
						
						// Display the results
						searchResultsElement.innerHTML = html;
						
						formattingFirstRow = false;
						formattingLastRow = false;
					}
					else
					{
						searchResultsElement.innerHTML = "";
						noDataFound();
					}
				}
			}
			currentSearch.endProgress();
			searchProgressElement.style.display = "none";
			requestInProgress = false;

		}
		else
		{
			searchProgressElement.innerHTML = "Error processing response "+dataRequest.status;
			requestInProgress = false;
		}
	}
}

function processListOfContainers()
{
	var searchProgressElement = currentSearch.startProgress();

	if (dataRequest.readyState == 4)
	{
		if (dataRequest.status == 200)
		{
			if (dataRequest.responseText == "")
			{
				noDataFound();
			}
			else
			{
				dataFound();

				var searchMatchesElement = currentSearch.getDiv(currentSearch.MatchesSuffix);
				if (searchMatchesElement == null)
				{
					alert("Unable to find page element \""+currentSearch.name+currentSearch.MatchesSuffix+"\"");
				}
				else
				{
					data = dataRequest.responseXML;
					if (data != null )
					{
						var rows = data.getElementsByTagName("row");

						processDataQueries(currentSearch,false);
						
						searchMatchesElement.innerHTML = "Found "+rows.length;
						
						if (rows.length<11 && rows.length>0)
						{
							processDataQueries(currentSearch, true);
						}

					}
					else
					{
						searchMatchesElement.innerHTML = "";
						noDataFound();
					}
				}
			}
			requestInProgress = false;

		}
		else
		{
			requestInProgress = false;
		}
	}
	currentSearch.endProgress();
}

/*
<?xml version="1.0" encoding="UTF-8"?>
<query-response>
  <data-table filter="ACTIVE_UNIT_DETAILS" count="1">
    <columns>
      <column>Category</column>
      <column>Eq GradeID</column>
      <column>Frght Kind</column>
      <column>I/B Actual Visit</column>
      <column>Last Move</column>
      <column>Line Op</column>
      <column>O/B Actual Visit</column>
      <column>POD</column>
      <column>Position</column>
      <column>Reqs Power</column>
      <column>Stop-Rail</column>
      <column>Stop-Road</column>
      <column>Stop-Vsl</column>
      <column>T-State</column>
      <column>Type ISO</column>
      <column>Unit Nbr</column>
      <column>V-State</column>
    </columns>
    <rows>
      <row primary-key="71814">
        <field>Export</field>
        <field />
        <field>FCL</field>
        <field>WDS</field>
        <field>2010-May-14 1516</field>
        <field>MSK</field>
        <field>BRI1008</field>
        <field>TPP</field>
        <field>Y-BCT-Z05E.1</field>
        <field>false</field>
        <field>false</field>
        <field>true</field>
        <field>true</field>
        <field>Yard</field>
        <field>4300</field>
        <field>MSKU6261060</field>
        <field>Active</field>
      </row>
    </rows>
  </data-table>
</query-response>

for EDO items, use &mode=SNX and this format:
<argo:argo xsi:schemaLocation="http://www.navis.com/argo snx.xsd">
	<equipment-delivery-order number="601000151" line="MSK" shipper-id="FONTERRA" dispatch-reserved="FIRST" ignore-damages="N" ignore-holds="N" prevent-type-subst="N">
		<items>
			<item quantity="4" eq-iso-group="GP" eq-size="NOM20" eq-height="NOM86" eq-grade="DB" eq-material="UNKNOWN" is-powered="N">
				<serial-ranges/>
				<reserved-empty-containers>
					<reserved-empty-container equip-number="MSKU7025032"/>
					<reserved-empty-container equip-number="MSKU7276866"/>
					<reserved-empty-container equip-number="MSKU3980373"/>
					<reserved-empty-container equip-number="PONU0902119"/>
				</reserved-empty-containers>
				<hazards/>
			</item>
			<item quantity="1" eq-iso-group="GP" eq-size="NOM20" eq-height="NOM86" eq-grade="USA" eq-material="UNKNOWN" is-powered="N">
				<serial-ranges/>
				<reserved-empty-containers>
					<reserved-empty-container equip-number="TRLU3749284"/>
				</reserved-empty-containers>
				<hazards/>
			</item>
		</items>
	</equipment-delivery-order>
</argo:argo>
*/

function getText(node)
{
	if (currentSearch.isSNXQuery)
		return node.name;
	else if (node.textContent==null)
		return node.text; // IE specific line :(
	else
		return node.textContent;
}

Array.prototype.contains = function(search){
  for (var i=0; i<this.length; i++)
    if (this[i] == search) return true;
		
  return false;
} 

function formatListRowSimple(index, data, rowStyle,header)
{
	var i;
	var html = "";
	html +="";
	
	if(header)
	{
		var tableDefinition = "          <table class=\"trackingtable\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\" width=\"100%\">\n";
		var headerDefinition = "            <tr class=trackingheader>\n";
		
		currentSearch.numberOfColumns=0;
		currentSearch.unitNbrCol = null;
		currentSearch.bookingCol = null;
		currentSearch.stopColumns = new Array();
		
		headerDefinition += "              <td></td>\n";
		for (i=0; i<data.length;i++)
		{
			var colTitle = currentSearch.getTitle(data[i]);
			
			headerDefinition += "              <td>"+colTitle+"</td>\n";
			
			if (colTitle == "Unit Nbr")
			{
					currentSearch.unitNbrCol = i;
			}
			if (colTitle == "Equip Nbr")
			{
					currentSearch.unitNbrCol = i;
			}
			else if (colTitle == "Booking Number")
			{
					currentSearch.bookingCol = i;
			}
			else if (colTitle == "Unit Booking Number")
			{
					currentSearch.bookingCol = i;
			}
			else if (colTitle.search(/Stop-/)==0)
			{
					currentSearch.stopColumns.push(i);
			}
			
			currentSearch.numberOfColumns++;
		}

		headerDefinition += "            </tr>\n";
		return "\n"+tableDefinition+"\n"+headerDefinition;
	}
	else
	{	
	    html +="        <tr class="+rowStyle+"> \n";
	    
	    var fields = currentSearch.getFields(data);
		/*if (currentSearch.isSNXQuery)
		{
			fields = data.attributes;
		}
		else
		{
		    fields = data.getElementsByTagName("field")
		}*/
		
	    html +="			<td class=indexcolumn>"+(index+1)+"</td>\n";
	    
		for (i=0; i<fields.length;i++)
		{
			var fieldVal = currentSearch.getFieldValue(data, fields[i]);
			/*if (currentSearch.isSNXQuery)
			{
				fieldVal = data.getAttribute(fields[i]);
			}
			else
			{
				fieldVal = getText(fields[i]);
			}*/
			
			if (currentSearch.stopColumns.contains(i))
			{
				if (fieldVal=="true")
				    html +="			<td><img src=\"/images/stop.png\"></td>\n";
				else if (fieldVal=="false")
				    html +="			<td><img src=\"/images/go.png\"></td>\n";
				else if (fieldVal=="undefined")
				    html +="			<td><img src=\"/images/unknown.gif\"></td>\n";
			}
			else if (currentSearch.bookingCol != null && currentSearch.bookingCol == i && fieldVal!= "")
			{
		    	html +="			<td><a href=\""+releaseTrackingPage+"?edo="+fieldVal+"\">"+fieldVal+"</a>";
		    	html +="<br><a href=\""+releaseTrackingPage+"?edo="+fieldVal+"\"><img src=\"/images/release.gif\" border=\"0\"></a>";
		    	html +="<a href=\"N4ContainerTracking.html?booking="+fieldVal+"\"><img src=\"/images/Container.gif\" border=\"0\"></a></td>\n";
		    }
			else if (currentSearch.unitNbrCol != null && currentSearch.unitNbrCol == i && fieldVal!= "")
			{
		    	html +="			<td><a href=\"N4ContainerTracking.html?container="+fieldVal+"\">"+fieldVal+"</a>";
		    	html +="<br><a href=\""+releaseTrackingPage+"?rnd="+fieldVal+"\"><img src=\"/images/release.gif\" border=\"0\"></a>";
		    	html +="<a href=\"N4ContainerTracking.html?container="+fieldVal+"\"><img src=\"/images/Container.gif\" border=\"0\"></a></td>\n";
		    }
		    else
		    	html +="			<td>"+fieldVal+"</td>\n";
		}
		
			
	    html +="		</tr>\n";	
	    //alert(html);
	}
	return html;
}

function setVariable(parameterName, value)
{
	var variableID = parameterName+VariableSuffix;
	var variableElement = document.getElementById(variableID);
	if (variableElement==null)
	{
		variableElement = document.getElementById(parameterName);
	}
		
	if (variableElement!=null)
		variableElement.value = value
	else
		alert("Unable to set variable "+variableID);
}

function fillFormFieldsFromUrl(url)
{
	var queryParts = (new String(url)).split("?");
	if (queryParts.length > 1)
	{
		var allParameters = queryParts[1];
		var namesAndValues = (allParameters.match(/&/)?allParameters.split("&"):new Array(allParameters));
		for (i=0; i<namesAndValues.length; i++)
		{
			var pair = namesAndValues[i];
			var parts = pair.split("=");
			var name = parts[0];
			var value = parts[1];
			setVariable(name,value.replace("'",""));
		}
	}
}
