phpFile="/Contacts/listContacts.php";
jsFile="listContacts.js";
includeEdittingLinks = false;
includeLinkingLinks = false;
//formatListRow = formatListRowComplete;
var contactSearchResults = "searchResults";
var searchProgressID = "searchProgress";
var formattingFirstRow = false;
var formattingLastRow = false;
//searchLabel = "Services";

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...
var dataRequest = createRequest();
if (dataRequest == null)
	alert("Unable to create server connections: "+dataRequest);
	
var requestInProgress = false;

function requestContactData(url, updateFn, targetDiv)
{
	if (dataRequest != null && !requestInProgress)
	{
		requestInProgress=true;
		dataRequest.open("GET", url, true);
		dataRequest.onreadystatechange = updateFn;
		contactSearchResults = targetDiv;
		dataRequest.send(null);
	}
	
	//var ajaxRequestElement = document.getElementById("ajaxRequest");
	//ajaxRequestElement.innerHTML = "<p>"+url+"</p>";
	//ajaxRequestElement.style.display = "block";

}


function requestData(url, updateFn, targetDiv)
{
	if (dataRequest != null && !requestInProgress)
	{
		requestInProgress=true;
		dataRequest.open("GET", url, true);
		dataRequest.onreadystatechange = updateFn;
		contactSearchResults = targetDiv;
		dataRequest.send(null);
	}
	
	//var ajaxRequestElement = document.getElementById("ajaxRequest");
	//ajaxRequestElement.innerHTML = "<p>"+url+"</p>";
	//ajaxRequestElement.style.display = "block";

}

var spinner = new Array(5);
spinner[0]="Creating Connection";
spinner[1]="Send Request";
spinner[2]="Processing Request";
spinner[3]="Receiving Response";
spinner[4]="Processing Response";

function endProgress()
{
	var progressElement = document.getElementById(searchProgressID);
	replaceText(progressElement, "");
	progressElement.style.display = "none";
}

function noDataFound()
{
	endProgress();
	
	var noDataElement = document.getElementById("noDataResponse");
	replaceText(noDataElement,"<ul><li class=highlights>There are currently no contacts defined by Port Taranaki.</li></ul>");
	noDataElement.style.display = "block";
}

function dataFound()
{
	endProgress();

	var noDataElement = document.getElementById("noDataResponse");
	replaceText(noDataElement,"");
	noDataElement.style.display = "none";
}

function listGroup(groupid, targetDiv)
{
	if (requestInProgress)
	{
		//alert("listGroup("+groupid+","+targetDiv+"\")");
		setTimeout("listGroup("+groupid+",\""+targetDiv+"\")",100);
	}
	else
	{
		var ajaxRequest=phpFile+"?action=LISTGROUP&groupid="+groupid;
		requestContactData(ajaxRequest, updateContactList, targetDiv);
	}
}

function updateContactList()
{
	var searchProgressElement = document.getElementById(searchProgressID);
	replaceText(searchProgressElement, "<span class=highlights>Searching Contacts: "+spinner[dataRequest.readyState]+"<blink>...</blink></span>");
	if (dataRequest.readyState == 4)
	{
		if (dataRequest.status == 200)
		{
			searchProgressElement.innerHTML = "";
			if (dataRequest.responseText == "")
			{
				noDataFound();
			}
			else
			{
				dataFound();

				var searchResultsElement = document.getElementById(contactSearchResults);
				if (searchResultsElement == null)
				{
					alert("Unable to find page element \""+contactSearchResults+"\"");
				}
				else
				{
					var data = eval("("+dataRequest.responseText+")");
					if (data.rows.length > 0 )
					{
						//Show the data
						var currentRowStyle = "pinkline";
						var otherRowStyle = "whiteline";
						var swapRowStyle = "whiteline";
						var html = "";
						
						formatListRow = data.rows[0].format;
						
						html+=eval(formatListRow+"(null, \""+(data.rows[0].title==null?"":data.rows[0].title)+"\", null, true)");
	
						formattingFirstRow = true;
						for (var i = 0; i < data.rows.length; i++)
						{
							html+=eval(formatListRow+"("+i+", data.rows["+i+"],\""+currentRowStyle+"\")");
							swapRowStyle=currentRowStyle;
							currentRowStyle=otherRowStyle;
							otherRowStyle=swapRowStyle;
							
							formattingFirstRow = false;
							if ((i+1)==data.rows.length)
							{
								formattingFirstRow = true;
							}
						}
						html +="</table>";
	
						//searchResultsElement.innerHTML = "";
						searchResultsElement.innerHTML = html;
						//replaceText(searchResultsElement,html);
						
						formattingFirstRow = false;
						formattingLastRow = false;
					}
					else
					{
						searchResultsElement.innerHTML = "";
						//document.getElementById("searchManual").style.display = "none";
						noDataFound();
					}
				}
			}
			endProgress();
			requestInProgress = false;

		}
		else
		{
			replaceText(searchProgressElement.innerText, "Error processing response "+dataRequest.status);
			requestInProgress = false;
		}
	}
}

function isDefined(item)
{
	return (item != null && item != "" && item != "undefined")
}

function trimWebsite(website)
{
	return website.replace(/^http[s]*:\/\//, "");
}

function formatListRowSimple(index, row, rowStyle,header)
{
	var html = "";
	html +="";
	
	if(header)
	{
		var tableDefinition = "          <table class=\"subcontent\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">\n";
		var headerDefinition = "            <tr class=headerline>\n              <td>"+row+"</td>\n              <td></td>\n              <td>Mobile</td>\n";
		return "\n"+tableDefinition+"\n"+headerDefinition;
	}
	else
	{	
	    html +="        <tr class="+rowStyle+"> \n";
	    html +="			<td><b>"+row.name+"</b></td>\n";
	    html +="			<td>"+row.position+"<br><a href=\"mailto:"+row.email+"\">"+row.email+"</a></td>\n";
	    html +="			<td>"+row.mobile+"</td>\n";
	    html +="		</tr>\n";	
	}
	return html;
}

function formatListRowStandard(index, row, rowStyle,header)
{
	var html = "";
	html +="";
	
	if(header)
	{
		var tableDefinition = "<table cellspacing=\"0\" width=100% style=\"font:small\">";
		var headerDefinition = "        <tr class=headerline>\n					<td>"+row+"</td>\n					<td>Telephone</td>\n					<td>Facsimile</td>\n					<td>Mobile</td>\n				</tr>\n";
		return "\n"+tableDefinition+"\n"+headerDefinition;
	}
	else
	{	
	    html +="        <tr class="+rowStyle+"> \n";
	    html +="			<td rowspan=2><b>"+row.organisation+"</b></br>\n";
	    if (row.name!=row.organisation)
	    {
	    	html +="				"+row.name+"</br>\n";
	    }
	    html +=(row.position!=""?"				&mdash;"+row.position+",</br>\n":"");
	    html +=(row.department!=""?"				"+row.department+"</br>\n":"");
	    html +=(row.address!=""?"				"+row.address+"</br>\n":"");
	    if (isDefined(row.email))
		    html +="				<a href=\"mailto:"+row.email+"\">"+row.email+"</a><br>\n";
	    if (isDefined(row.website))
	    	html +="				<a href=\""+row.website+"\" target=_new>"+trimWebsite(row.website)+"</a>\n";
	    html +="			</td>\n";
	    html +="			<td>";
	    if (isDefined(row.phone))
	    	html +="				"+row.phone;
	    if (isDefined(row.phone) && isDefined(row.freecall))
		    html +=" or ";
	    if (isDefined(row.freecall))
		    html +="				"+row.freecall;
	    html +="			</td>\n";
	    html +="			<td>"+row.fax+"</td>\n";
	    html +="			<td>"+row.mobile+"</td>\n";
	    html +="		</tr>\n";	
	    html +="        <tr class="+rowStyle+"> \n";
	    if (row.linkblurb != null && row.linkblurb != "")
		    html +="			<td colspan=3 class=advertising>"+row.linkblurb+"</td>\n";
		else if (row.blurb != null && row.blurb != "")
		    html +="			<td colspan=3 class=advertising>"+row.blurb+"</td>\n";
		else
			html +="			<td colspan=3></td>\n";
	    html +="		</tr>\n";	
	}
	return html;
}

function formatListRowJustWebsites(index, row, rowStyle,header)
{
	var html = "";
	html +="";
	
	if(header)
	{
		var tableDefinition = "<table class=sitegroup align=center width=\"100%\">";
		var headerDefinition = "        <tr>\n					<td colspan=2 class=\"name\">"+row+"</td>\n					</tr>\n";
		return "\n"+tableDefinition+"\n"+headerDefinition;
	}
	else
	{	
	    html +="        <tr> \n";
	    html +="			<td width='55%'>"+row.organisation+"</td>\n";
	    html +="			<td><a href=\""+row.website+"\" target=_new>"+trimWebsite(row.website)+"</a></td>\n";
	    html +="		</tr>\n";	
	}
	return html;
}

function formatListRowStandardWithTelex(index, row, rowStyle,header)
{
	var html = "";
	html +="";
	
	if(header)
	{
		var tableDefinition = "<table cellspacing=\"0\" width=100% style=\"font:small\">";
		var headerDefinition = "        <tr class=headerline>\n					<td>"+row+"</td>\n					<td>Tel/Mob</td>\n					<td>Facsimile</td>\n					<td>Telex</td>\n				</tr>\n";
		return "\n"+tableDefinition+"\n"+headerDefinition;
	}
	else
	{	
	    html +="        <tr class="+rowStyle+"> \n";
	    html +="			<td rowspan=2><b>"+row.organisation+"</b></br>\n";
	    html +=(row.organisation!=row.name?"				"+row.name+"</br>\n":"");
	    html +=(row.position!=""?"				&mdash;"+row.position+",</br>\n":"");
	    html +=(row.department!=""?"				"+row.department+"</br>\n":"");
	    html +=(row.address!=""?"				"+row.address+"</br>\n":"");
	    html +="				<a href=\"mailto:"+row.email+"\">"+row.email+"</a><br>\n";
	    html +="				<a href=\""+row.website+"\" target=_new>"+trimWebsite(row.website)+"</a>\n";
	    html +="			</td>\n";
	    html +="			<td>"+row.allphones+"</td>\n";
	    html +="			<td>"+row.fax+"</td>\n";
	    html +="			<td>"+row.telex+"</td>\n";
	    html +="		</tr>\n";	
	    html +="        <tr class="+rowStyle+"> \n";
	    if (row.linkblurb != null && row.linkblurb != "")
		    html +="			<td colspan=3 class=advertising>"+row.linkblurb+"</td>\n";
		else if (row.blurb != null && row.blurb != "")
		    html +="			<td colspan=3 class=advertising>"+row.blurb+"</td>\n";
		else
			html +="			<td colspan=3></td>\n";
	    html +="		</tr>\n";	
	}
	return html;
}

function formatListRowStandardTrailingBlurb(index, row, rowStyle,header)
{
	var html = "";
	html +="";
	
	if(header)
	{
		var tableDefinition = "<table cellspacing=\"0\" width=100% style=\"font:small\">";
		var headerDefinition = "        <tr class=headerline>\n					<td>"+row+"</td>\n					<td style=\"width: 20em; text-align: center;\">Telephone</td>\n					<td style=\"width: 20em; text-align: center;\">Facsimile</td>\n					<td style=\"width: 20em; text-align: center;\">Mobile</td>\n				</tr>\n";
		return "\n"+tableDefinition+"\n"+headerDefinition;
	}
	else
	{	
	    html +="        <tr class=\"pinkline\"> \n";
	    html +="			<td><b>"+row.organisation+"</b></br>\n";
	    html +=(row.address!=""?"				"+row.address+"</br>\n":"");
	    html +="				<a href=\"mailto:"+row.email+"\">"+row.email+"</a><br>\n";
	    html +="				<a href=\""+row.website+"\" target=_new>"+trimWebsite(row.website)+"</a>\n";
	    html +="			</td>\n";
	    html +="			<td style=\"text-align: center;\">"+(row.freecall!=null&&row.freecall!=""?row.freecall+" or<br>":"")+row.phone+"</td>\n";
	    html +="			<td style=\"text-align: center;\">"+row.fax+"</td>\n";
	    html +="			<td style=\"text-align: center;\">"+row.mobile+"</td>\n";
	    html +="		</tr>\n";	
	    html +="				<tr><td colspan=4>&nbsp;</td></tr>";
	    html +="        <tr class=\"whiteline\"> \n";
	    if (row.linkblurb != null && row.linkblurb != "")
		    html +="			<td colspan=4>"+row.linkblurb+"</td>\n";
		else if (row.blurb != null && row.blurb != "")
		    html +="			<td colspan=4>"+row.blurb+"</td>\n";
		else
			html +="			<td colspan=4></td>\n";
	    html +="		</tr>\n";	
	    html +="				<tr><td colspan=4>&nbsp;</td></tr>";
	}
	return html;
}


function formatListRowBlock(index, row, rowStyle, header)
{
    var html = "";

	if(header)
	{
		//var tableDefinition = "          <table class=\"subcontent\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">\n";
		var tableDefinition = "";
		var headerDefinition = "";
		return "\n"+tableDefinition+"\n"+headerDefinition;
	}
	else
	{	
	    html +="                <table class=address width=50% align=\"left\">";
	    html +="					 <tr>";
	    html +="						<td></td>";
	    html +="						<td colspan=2>";
	    html +="							"+row.name+"<br>";
	    html +="							"+row.address+"";
	    html +="						</td>";
	    html +="					 </tr>";
	    html +="					 <tr class=\"smaller\"><td></td>";
	    html +="					 <td>Telephone:</td><td>"+row.phone+"</td>";
	    html +="					 </tr>";
	    html +="                <tr class=\"smaller\"><td></td>";
	    html +="					 <td>Fax:</td><td>"+row.fax+"</td>";
	    html +="					 </tr>";
	    html +="					 </table>";
	    html +="                <p>"+row.linkblurb+"";
	    html +="        <br clear=\"all\">";
	}
    return html;
}

function formatListRowProper(index, row, rowStyle, header)
{
    var html = "";

	if(header)
	{
		var tableDefinition = "				<table class=subcontent>";
		var headerDefinition = "";
		return "\n"+tableDefinition+"\n"+headerDefinition;
	}
	else
	{	
	    html +="				<tr class=contact>\n";
	    if (isDefined(row.title))
	    {
		    html +="					<td class=method>"+row.title+"</td>\n";
		    html +="					<td class=address>"+row.name+"</td>\n";
	    }
	    else
	    {
		    html +="					<td colspan=2 class=address>"+row.name+"</td>\n";
	    }
	    html +="				</tr>\n";
	    if (isDefined(row.position))
	    {
		    html +="				<tr class=contact>\n";
		    html +="					<td class=method></td>\n";
		    html +="					<td class=address>&mdash; "+row.position+"</td>\n";
		    html +="				</tr>\n";
		}
	    if (isDefined(row.linkblurb) || isDefined(row.blurb))
	    {
		    html +="				<tr class=contact>\n";
		    html +="					<td colspan=2 class=method></td>\n";
		    html +="				</tr>\n";
		    html +="				<tr class=contact>\n";
		    html +="					<td colspanclass=method></td>\n";
		    html +="					<td colspan class=method>\n";
		    if (isDefined(row.linkblurb))
			    html +="						"+row.linkblurb;
			else
			    html +="						"+row.blurb;				
		    html +="					</td>\n";
		    html +="				</tr>\n";
		    html +="				<tr class=contact>\n";
		    html +="					<td colspan=2 class=method></td>\n";
		    html +="				</tr>\n";
		}
	    if (isDefined(row.address))
	    {
		    html +="				<tr class=contact>\n";
		    html +="					<td colspan=2 class=address>"+row.address+"</td>\n";
		    html +="				</tr>\n";
		}
	    if (isDefined(row.phone))
	    {
		    html +="				<tr class=contact>\n";
		    html +="					<td class=method>Phone</td>\n";
		    html +="					<td class=address>"+row.phone+"</td>\n";
		    html +="				</tr>\n";
		}
	    if (isDefined(row.ddi) && row.ddi != row.phone)
	    {
		    html +="				<tr class=contact>\n";
		    html +="					<td class=method>Direct Dial</td>\n";
		    html +="					<td class=address>"+row.ddi+"</td>\n";
		    html +="				</tr>\n";
		}
	    if (isDefined(row.freecall) && row.freecall != row.phone)
	    {
		    html +="				<tr class=contact>\n";
		    html +="					<td class=method>Callfree</td>\n";
		    html +="					<td class=address>"+row.freecall+"</td>\n";
	    	html +="				</tr>\n";
		}
	    if (isDefined(row.fax))
	    {
		    html +="				<tr class=contact>\n";
		    html +="					<td class=method>Fax</td>\n";
	    	html +="					<td class=address>"+row.fax+"</td>\n";
		    html +="				</tr>\n";
		}
	    if (isDefined(row.mobile) && row.mobile != row.phone)
	    {
		    html +="				<tr class=contact>\n";
		    html +="					<td class=method>Mobile</td>\n";
	    	html +="					<td class=address>"+row.mobile+"</td>\n";
		    html +="				</tr>\n";
		}
	    if (isDefined(row.email))
	    {
		    html +="				<tr class=contact>\n";
		    html +="					<td class=method>Email</td>\n";
	    	html +="					<td class=address><a href=\"mailto:"+row.email+"\">"+row.email+"</a></td>\n";
		    html +="				</tr>\n";
		}
	    if (isDefined(row.generalemail))
	    {
		    html +="				<tr class=contact>\n";
		    html +="					<td class=method>General Email</td>\n";
	    	html +="					<td class=address><a href=\"mailto:"+row.generalemail+"\">"+row.generalemail+"</a></td>\n";
		    html +="				</tr>\n";
		}
	    if (isDefined(row.website))
	    {
		    html +="				<tr class=contact>\n";
		    html +="					<td class=method>Website</td>\n";
	    	html +="					<td class=address><a href=\""+row.website+"\" target=_new>"+trimWebsite(row.website)+"</a></td>\n";
		    html +="				</tr>\n";
		}
	    //html +="				<tr class=contact>\n";
	    //html +="					<td colspan=2>&nbsp;</td>\n";
	    //html +="				</tr>\n";
	}
    return html;
}


function formatListRowArticleWithProper(index, row, rowStyle, header)
{
    var html = "";

	if(header)
	{
		var tableDefinition = "";
		var headerDefinition = "";
		return "\n"+tableDefinition+"\n"+headerDefinition;
	}
	else
	{	
		if (!formattingFirstRow)
			html +="<hr>";
	    html +=""+unescape(row.linkarticle==null||row.linkarticle==""?row.article:row.linkarticle)+"";
	    if (row.phone != "" ||row.ddi != "" ||row.freecall != "" ||row.address != "" ||row.email != "")
	    {
		    html +="<p>For more information, please contact:</p>";
		    html +=formatListRowProper(index, row, rowStyle, true);
		    html +=formatListRowProper(index, row, rowStyle, false);
			html +="</table>";
		}
	}
    return html;
}

function formatListRowComplete(index, row, rowStyle,header)
{
	var html = "";

	if(header)
	{
		var tableDefinition = "          <table class=\"subcontent\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">\n";
		var headerDefinition = "            <tr class=headerline>\n              <td>"+row+"</td>\n              <td>Contact</td>\n              <td>All Phones</td>\n              <td>Phone</td>\n              <td>DDI</td>\n              <td>Mobile</td>\n              <td>Freecall</td>\n              <td>Fax</td>\n              <td>Telex</td>\n              <td></td>\n            </tr>\n";
		return "\n"+tableDefinition+"\n"+headerDefinition;
	}
	else
	{	
		html +="";
		
	    html +="        <tr class="+rowStyle+"> \n";
	    html +="          <td rowspan=2>"+(includeEdittingLinks?"<a href=\"javascript:editContact("+row.contactid+")\">Edit: </a>":"");
	    html +=""+(includeLinkingLinks?"<a href=\"javascript:editContactLink("+row.itemid+")\">Change: </a>":"")+"</td>\n";
	    html +="          <td rowspan=2><b>"+row.name+"</b><br>\n";
	    if (!row.nickname == "")
	    {
		    html +="						("+row.nickname+")<br>\n";
		}
	    if (!row.imageurl == "")
	    {
		    html +="						<img src='"+row.imageurl+"'><br>\n";
		}
	    html +="						"+row.organisation+"<br>\n";
	    html +="						"+row.department;
	    if (!row.position == "")
	    {
		    html +="						<br>\n&mdash; "+row.position+",<br>\n";
		}
		else
		{
			html +="<br>\n";
		}
	    html +="						"+row.address+"<br>\n";
	    html +="						<a href=\"mailto:"+row.email+"\">"+row.email+"</a><br>\n";
	    html +="						<a href=\"mailto:"+row.generalemail+"\">"+row.generalemail+"</a><br>\n";
	    html +="						<a href=\""+row.website+"\" target=_new>"+trimWebsite(row.website)+"</a></td>\n";
	    html +="					<td>\n";
	    html +="						"+row.allphones+"</td>\n";
	    html +="					<td>\n";
	    html +="						Ph:"+row.phone+"</td>\n";
	    html +="					<td>\n";
	    html +="						DDI:"+row.ddi+"</td>\n";
	    html +="					<td>\n";
	    html +="						Mo:"+row.mobile+"</td>\n";
	    html +="					<td>\n";
	    html +="						Free:"+row.freecall+"</td>\n";
	    html +="					<td>\n";
	    html +="						Fax:"+row.fax+"</td>\n";
	    html +="					<td>\n";
	    html +="						Tlx:"+row.telex+"</td>\n";
	    html +="          <td rowspan=2>"+(includeEdittingLinks?"<a href=\""+phpFile+"?action=DELETE&contactid="+row.contactid+"\"> DELETE</a>":"")
	    html +=""+(includeLinkingLinks?"<a href=\"javascript:deleteLink("+row.itemid+")\"> UNLINK</a>":"")+"</td>\n";
	    html +="        </tr>\n";
	    html +="        <tr class="+rowStyle+">\n";
	    html +="					<td colspan=7 class=advertising>";
	    if (row.linkblurb != null && row.linkblurb != "")
	    {
		    html +="						"+row.linkblurb+"\n";
		}
		else
		{
	    	html +="						"+row.blurb+"\n";
	    }
	    html +="					</td>\n";
	    html +="        </tr>\n";
	    html +="        <tr class="+rowStyle+">\n";
	    html +="					<td colspan=10 class=advertising>";
	    if (row.linkarticle != null && row.linkarticle != "")
	    {
	    	html +="						"+row.linkarticle+"\n";
	 	}
		else
		{
		    html +="						"+unescape(row.article)+"\n";
	    }
	    html +="					</td>\n";
	    html +="				</tr>\n";
	}
	return html;
}

