
var AFDSERVER = "http://pipserv.afd.co.uk/";

function ParseString(strSource, strKey)
{
	var nIndex, strTemp, strResult, nKeyLen;

	strResult="";
	if( (strSource.length ==0) || (strKey.length ==0) ) return strResult;

	nKeyLen = strKey.length;
	strTemp = strKey + "=";
	nIndex = strSource.indexOf(strTemp);
	if(nIndex == -1) return strResult;
	strTemp = strSource.substring(nIndex+nKeyLen);
	strResult = strTemp.substring(1, strTemp.indexOf("&"));
	return strResult;
}


function GetDoc()
{
	if(document.layers)
		return window.document.AFD;
	else
		return window.document.all;
}


function GetDocItem(strItemName)
{
	var objReturn, objDoc;

	if(document.getElementById)
	{
		objReturn = document.getElementById(strItemName);
	}
	else if(document.layers)
	{
		objDoc = window.document.AFD;
		objReturn = objDoc[strItemName];
	}
	else if(document.all)
	{
		objDoc = window.document.all;
		objReturn = objDoc[strItemName];
	}

	return objReturn;
}

//-----------------------------------------------------------------------

function DrawCaption()
{
	var objDoc, objTemp; 
	var strTemp, strText, strValue, nRecNo, nRecCount;
	var strPrompt, nTotal, nLastRec;

	var strURL = location.href;


	nRecNo    = ParseString(strURL, "StartRec");
	nRecCount = ParseString(strURL, "RecCount");

	//Generate pick list caption
	nTotal = ParseString(strURL, "TotalRecs");
	nLastRec = ( parseInt(nRecNo) + parseInt(nRecCount) ) -1;
	strPrompt = "Records " + nRecNo + "-" + nLastRec + " of " + nTotal;
	objTemp = GetDocItem("AFDpicklistprompt");
	objTemp.value=strPrompt;
}

function DrawListbox()
{
	var objDoc, objListbox, objTemp; 
	var nLooper, strTemp, strText, strValue, nRecNo, nRecCount;
	var strPostcode, strLocality, strTown, strCounty, strP1, strP2;
	var strPrompt, nTotal, strCRLF, strTownCounty;
	var addrNo, locNo, startPos, endPos, lastStr;
	var nOverLocNo, strOverLoc, strOverLocValue;

	var strURL = location.href;

	objListbox = GetDocItem("AFDpicklist");

	strPostcode = unescape(ParseString(strURL, "P"));
	strLocality = unescape(ParseString(strURL, "L"));
	strTown     = unescape(ParseString(strURL, "T"));
	strCounty   = unescape(ParseString(strURL, "C"));
	strP1 = strPostcode + "    ";

	strTownCounty="";
	if(strTown.length > 0) {
		if(strTownCounty.length > 0) strTownCounty = strTownCounty + ", ";
		strTownCounty = strTownCounty + strTown; }
	if(strCounty.length > 0) {
		if(strTownCounty.length > 0) strTownCounty = strTownCounty + ", ";
		strTownCounty = strTownCounty + strCounty; } 

	nRecNo=ParseString(strURL, "StartRec");
	nRecCount=ParseString(strURL, "RecCount");

	addrNo = nRecNo - 1;
	locNo = 1;
	lastStr = "";
	for(nLooper=0;nLooper<nRecCount;nLooper++)
	{
		strTemp = "k" + nRecNo.toString();
		strValue = ParseString(strURL, strTemp);
		if(strValue != "") 
                {
		  if (strValue.length > 1) 
                  {
    	            locNo = strValue.substring(0, 1);
//		    addrNo = strValue.substring(strValue.length - 1) - 1; //old
		    addrNo = (strValue.substring(1,strValue.length)) -1 ; //new, take digits after 1st
		  }
		  else 
                  {
		    locNo++; //RK Inc locno if no locno given
		  }
//  	 	  addrNo=0;
		}

		addrNo++;
		if (addrNo < 10) {
	  	  strValue = ParseString(strURL, "P") + locNo + "00" + addrNo;
		}
		else if (addrNo < 100) {
	  	  strValue = ParseString(strURL, "P") + locNo + "0" + addrNo;
		}
		else {
	  	  strValue = ParseString(strURL, "P") + locNo + addrNo;
		}
		
		strValue=unescape(strValue);

		strTemp = "l" + nRecNo.toString();
		strText = ParseString(strURL, strTemp);
		strText = unescape(strText);
		if (strText.substring(0,1) == '*') 
		  strText = strText.substring(1);
		else {
		  startPos = strText.substring(0, 2);
		  endPos = strText.substring(2, 4);
		  strText = lastStr.substring(0, startPos) + strText.substring(4);
		  strText = strText + lastStr.substring(endPos, 100);

		}
		lastStr = strText;

		//See if override locality provided
		strOverLoc="LOC" + (nLooper+1).toString();
		strOverLocValue=unescape(ParseString(strURL, strOverLoc));
  		if(strOverLocValue.length > 0)
			strTemp = strOverLocValue;
		else
			strTemp = strLocality; 

		strP2 = "";
		if(strTemp.length > 0) {
			if(strP2.length > 0) strP2 = strP2 + ", ";
			strP2 = strP2 + strTemp; }
		if(strTownCounty.length > 0) {
			if(strP2.length > 0) strP2 = strP2 + ", ";
			strP2 = strP2 + strTownCounty; }
	
		if(strText.length > 0) { strP2 = ", " + strP2;}

	
		strText = strP1 + strText + strP2; //Add Common data

		strTemp = "<OPTION VALUE='" + strValue + "'>" + strText;
		document.write(strTemp);
		nRecNo++;
	}
	DrawCaption();
}


function AddList_OnClick()
{
	objForm = document.forms["AFD"];
	objForm.submit();
}


function Previous_OnClick()
{
	var nRecNo, nRecCount, nRecRecs, strURL, strSession, strTemp;
	var objDoc, objForm, strTarget;

	var strURL = location.href;

	objTemp = GetDocItem("AFDrecordsperpage");
	nRecCount=objTemp.value;
	nRecRecs=ParseString(strURL, "RecCount");

	nRecNo=ParseString(strURL, "StartRec");
	if(nRecNo == 1) return;

	strTarget = AFDSERVER + "lookup.pl?Command=Prev";
	strSession=ParseString(strURL, "Session");
	strTarget += "&Session=" + strSession;
	strTarget += "&NumRecords=" + nRecCount;
	nRecNo = eval(nRecNo) - eval(nRecRecs);
	if(nRecNo < 1) { nRecNo =1; }

	strTarget += "&StartRecord=" + nRecNo;

	objTemp = GetDocItem("AFDpage1");
	strTemp = objTemp.value;
	strTarget += "&AFDpage1=" + strTemp;

	objTemp = GetDocItem("AFDserialnumber");
	strTemp = objTemp.value;
	strTarget += "&AFDserialnumber=" + strTemp;

	objTemp = GetDocItem("AFDhideredirect");
	if(objTemp != null) {
		strTemp = objTemp.value;
		strTarget += "&AFDhideredirect=" + strTemp;}

	objTemp = GetDocItem("AFDbgcolor");
	if(objTemp != null) {
		strTemp = objTemp.value;
		strTarget += "&AFDbgcolor=" + strTemp;}

	window.location =strTarget;
}


function Next_OnClick()
{
	var nRecNo, nRecCount, nRecRecs, nTotalRecs, objTemp;
	var objDoc, objForm, strTarget, strURL, strSession, strTemp;

	var strURL = location.href;

	objTemp = GetDocItem("AFDrecordsperpage");
	nRecCount=objTemp.value;
	nRecRecs=ParseString(strURL, "RecCount");
	nRecNo=ParseString(strURL, "StartRec");
	nTotalRecs=ParseString(strURL, "TotalRecs");

	if( (eval(nRecNo) + eval(nRecRecs)) > eval(nTotalRecs) ) return;

	strTarget = AFDSERVER + "lookup.pl?Command=Next";
	strSession=ParseString(strURL, "Session");
	strTarget += "&Session=" + strSession;
	strTarget += "&NumRecords=" + nRecCount;
	nRecNo = eval(nRecNo) + eval(nRecRecs);
	strTarget += "&StartRecord=" + nRecNo;

	objTemp = GetDocItem("AFDpage1");
	strTemp = objTemp.value;
	strTarget += "&AFDpage1=" + strTemp;

	objTemp = GetDocItem("AFDserialnumber");
	strTemp = objTemp.value;
	strTarget += "&AFDserialnumber=" + strTemp;

	objTemp = GetDocItem("AFDhideredirect");
	if(objTemp != null) {
		strTemp = objTemp.value;
		strTarget += "&AFDhideredirect=" + strTemp;}

	objTemp = GetDocItem("AFDbgcolor");
	if(objTemp != null) {
		strTemp = objTemp.value;
		strTarget += "&AFDbgcolor=" + strTemp;}

	window.location =strTarget;
}



