function showProtectedResult(fset,LayerName) {      
  var theDiv = document.getElementById("ResultTableDiv");
  var content = theDiv.innerHTML;
  var key
  ShowByID("ResultTableDiv",true);
  
  //alert("(13) " + LayerName + " has " + fset.features.length);
    
  if (content == "")
		{   
    content = content + "<div style='position: absolute; top:3px; right:3px;'>"
    content = content + " <img src='/images/CloseButton.gif' onclick='HideResultTableDiv();' align='right' />"
    content = content + "</div>"
    content = content + "<table height='25px' width='100%'>"
    content = content + "  <tr bgcolor=#2F67A0 style='text-align:center; color:white;'>"
		content = content + "    <td align='center' width='250'>"
		content = content + "      Search results, click one "
		content = content + "    </td>"
		content = content + "  </tr>"
		content = content + "</table>"      
		}
  content = content + "<table width='100%'>"
  var resulttxt;  
  if (fset.features.length != 0) 
    {
    featureSet = fset;
    var numFeatures = featureSet.features.length;  
    for (var i=0, il=numFeatures; i<il; i++)
      {
      var graphic = featureSet.features[i];      
      switch (LayerName) 
				{
      	case "Tree"     : key = graphic.attributes.Ref;		break;
				case "Building" : key = graphic.attributes.RefNo;	break;
				case "Monument" : key = graphic.attributes.Refno;	break;
				default					: key = 'unknown layer!';

				}
      content = content + "<tr><td align='center'> " ;   
      content = content + "<a target='_blank' href='http://planningexplorer.gov.gg/portal/servlets/LBSearchServlet?PKID="  + graphic.attributes.LINK + "' style='color:black; font-weight: bold;' >" ;
      content = content + LayerName + " : " + key;
      content = content + "</a> <br/>" ;      
      content = content + "</td></tr>"      
      }   
    } 
  else 
    {
    content = content + "<tr><td align='left'> " ;  
    content = content + LayerName + " : None found";     
    content = content + "</td></tr>"   
    };    
	content = content + "</table>"   		
  theDiv.innerHTML = content;
  if (Searching > 0) {
    Searching = Searching - 1 ;
  }
  ShowSearching();
}//----------------------------------------------------------------------------------------- 
function FindKeysTrees(evt) {
  queryTask = new esri.tasks.QueryTask(MapServicePath + "/3");  
  query = new esri.tasks.Query();
  query.returnGeometry = true;
  query.outFields = ["Key","LINK"];
  query.geometry = evt.mapPoint;
  queryTask.execute(query, function(fset) {  showProtectedResult(fset,"Tree");  });  
}//----------------------------------------------------------------------------------------- 
function FindKeysBuildings(evt) {
  queryTask = new esri.tasks.QueryTask(MapServicePath + "/4");  
  query = new esri.tasks.Query();
  query.returnGeometry = true;
  query.outFields = ["Key","LINK"];
  query.geometry = evt.mapPoint;
  queryTask.execute(query, function(fset) {  showProtectedResult(fset,"Building");  });  
}//----------------------------------------------------------------------------------------- 
function FindKeysMonuments(evt) {
  queryTask = new esri.tasks.QueryTask(MapServicePath + "/1"); 
  query = new esri.tasks.Query();
  query.returnGeometry = true;
  query.outFields = ["Key","LINK"];
  query.geometry = evt.mapPoint;
  queryTask.execute(query, function(fset) {  showProtectedResult(fset,"Monument");  });    
}//----------------------------------------------------------------------------------------- 


//function showMonumentResult(fset,LayerName) {    
//  var resulttxt;  
//  if (fset.features.length != 0) 
//    {
//    featureSet = fset;
//    var numFeatures = featureSet.features.length;  
//    for (var i=0, il=numFeatures; i<il; i++)
//      {
//      var graphic = featureSet.features[i];    
//      Content = Content + "<a href='http://planningexplorer.gov.gg/portal/servlets/LBSearchServlet?PKID="  + graphic.attributes.LINK + "' style='color:black; font-weight: bold;' >" ;
//      Content = Content + LayerName + " : " + graphic.attributes.Refno;
//      Content = Content + "</a> <br/>" ;
//      }   
//    } 
//  else 
//    {
//    Content = Content + "<div style='color:#404040'>"
//    Content = Content + LayerName + " : None found <br/>";
//    Content = Content + "</div>"
//    };
//  resulttxt = document.getElementById("ResultDiv");
//  resulttxt.innerHTML = Content;
//  if (Searching > 0) {
//    Searching = Searching - 1 ;
//  }
//  ShowSearching();
//}//-----------------------------------------------------------------------------------------
//function showBuildingResult(fset,LayerName) {    
//  var resulttxt;  
//  
//  if (fset.features.length != 0) 
//    {
//    featureSet = fset;
//    var numFeatures = featureSet.features.length;  
//    for (var i=0, il=numFeatures; i<il; i++)
//      {
//      var graphic = featureSet.features[i];      
//      Content = Content + "<a href='http://planningexplorer.gov.gg/portal/servlets/LBSearchServlet?PKID="  + graphic.attributes.LINK + "' style='color:black; font-weight: bold;' >" ;
//      Content = Content + LayerName + " : " + graphic.attributes.RefNo;
//      Content = Content + "</a> <br/>" ;
//      }   
//    } 
//  else 
//    {    
//    Content = Content + "<div style='color:#404040'>"
//    Content = Content + LayerName + " : None found <br/>";
//    Content = Content + "</div>"
//    };
//  resulttxt = document.getElementById("ResultDiv");
//  resulttxt.innerHTML = Content;
//  if (Searching > 0) {
//    Searching = Searching - 1 ;
//  }
//  ShowSearching();
//}//----------------------------------------------------------------------------------------- 
function SearchTypeClick(ID) {     
	var Default = ""  ;  
  resulttxt = document.getElementById("SearchByDiv");
  content = "";
  content = content + "<table width='100%'>";
  content = content + "<tr><td>";
  content = content + "<font style='color:Red;font-size:10pt; font-weight:bold;'> Click here to change what is searched</font>" ;  
  content = content + "</td><td rowspan='2'><img src='/images/SearchWhich.gif' align='right' /></td></tr>" ;
  content = content + "<tr><td>";
  content = content + "current search is on <font style='color:Blue; font-size:12pt; font-weight:bold;'> " + ID + " </font>" ;  
  content = content + "</td></tr>";
  content = content + "</table>"  ;
  document.PCodeForm.PCode.value = "";   
  resulttxt.innerHTML = content;   
  SearchTool = ID.toLowerCase();   
  ShowByID("SelectSearchTypeDiv",false); 
  ShowByID("SearchByDiv",true);
  ShowByID("InputDiv",true);
  ShowByID("ToolBarHolderDiv",true);   
  if (SearchTool == "road") {         
		Content = "Searching for a Road:<br/>You can search using part of the road name e.g. 'grande' and you may be given a list of choices if there is more than one that matches your search. Choosing a road name from the list will zoom in on the road on the map.";
	} else if (SearchTool == "postcode") {
		Content = "Searching for a Postcode:<br/>The map will zoom in on the centre of the postcode area.";
	} else if (SearchTool == "tree")        {
		Content = "Searching for a Protected Tree:<br/>enter the number e.g. 123.The map will zoom in on the item concerned (Outlined in light blue).";
	} else if (SearchTool == "monument")    {
		Content = "Searching for a Protected Monument:<br/>enter the number e.g. 123.The map will zoom in on the item concerned (Outlined in light blue).";
	} else if (SearchTool == "building")    {
	Content = "Searching for a Protected Building:<br/>enter the number e.g. 123.The map will zoom in on the item concerned (Outlined in light blue).";
	}  	
  Content = Default + Content ;
  resulttxt = document.getElementById("InfoDiv");
  resulttxt.innerHTML = Content ;   
  document.PCodeForm.PCode.focus();
}//-----------------------------------------------------------------------------------------
function FindLocation() {
  ShowByID("NoneFoundDiv",false);
  ShowBlankResult();
  SearchTxt = document.PCodeForm.PCode.value.toUpperCase();   
  //SearchTxt = SearchTxt.replace("'","''");  
       if (SearchTool == "road")     { RoadSearch(); }
  else if (SearchTool == "postcode") { PostCodeSearch(); }  
  else if (SearchTool == "tree")     { Search(TreeLayer); }
  else if (SearchTool == "monument") { Search(MonumentLayer); }
  else if (SearchTool == "building") { Search(ListedBuildingLayer); }  
}//-----------------------------------------------------------------------------------------
function FindKeys(evt) {
  var theDiv = document.getElementById("ResultTableDiv");
  var content = "" ;
  ShowByID("ResultTableDiv",false);  
  theDiv.innerHTML = content;  
  map.graphics.clear();  
  FindKeysTrees(evt);
  FindKeysMonuments(evt);
  FindKeysBuildings(evt);
  Searching = 3;
  ShowSearching();
}//-----------------------------------------------------------------------------------------
function IsNumeric(sText) {
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char; 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}//-----------------------------------------------------------------------------------------
function Search(ID) {
  var FieldName = "";
  var PreFix = "..." ;
  if (ID == MonumentLayer)
		{
		FieldName = "Refno" ; 
		PreFix = "PM" ;
		}
  if (ID == TreeLayer)
		{FieldName = "Ref" ;
		PreFix = "PT" ;
		};
  if (ID == ListedBuildingLayer)
		{FieldName = "RefNo" ; 
		PreFix = "PB" ;
		}
  if (FieldName == "") 
		{
			alert("Search type not determined")
		} else {
		CurrentLayerID = ID;
		Pcode = SearchTxt;  
		if (IsNumeric(Pcode))
		{
			Pcode = PreFix + Pcode ;
		}
		
	if (ID == ListedBuildingLayer)
		{
		if (Pcode.indexOf("-") > -1)
			{
			Pcode = Pcode.substring(Pcode.indexOf("-") + 1,Pcode.length);			
			}
		}
		//alert(Pcode);
//alert(IsNumeric(Pcode) + " " + Pcode + " - " + PreFix);			
		findTask = new esri.tasks.FindTask(MapServicePath);
		findParams = new esri.tasks.FindParameters();
		findParams.contains = false;
		//findParams.contains = true;
		findParams.returnGeometry = true;
		findParams.layerIds = [ID];
		findParams.searchFields = [FieldName];
		findParams.searchText = Pcode; 
		Searching = 1 ;
		ShowSearching();        
		findTask.execute(findParams,ShowResults);	
		}
}//-----------------------------------------------------------------------------------------
function ShowResults(results)    {
  //find results return an array of findResult.      
  map.graphics.clear();
  var dataForGrid = [];
  var Factor = 200; 
  var content = "" ;
  var Warn = false ;
  var Key;
  
  var theDiv = document.getElementById("RoadChoiceDiv");
  var height = theDiv.offsetHeight;
  var Max = 0;
  var MaxItems = (height-80) / 25;
    
  ShowByID("RoadChoiceDiv",true);
  RoadResults = results ;
  if (RoadResults.length > 1) { // more than one result    
    ShowByID("NoneFoundDiv",false);
    
    //content = "<table border='0' width='100%'><tr><td>"
    //content = content + "<table width='100%'>"
    //content = content + "<tr><td align='center' bgcolor=#2F67A0 >Search results, click one </td>"
    //content = content + "<td align='right'><img src='/images/CloseButton.gif' onclick='HideSearch();' align='right' /> </td></tr>"
    //content = content + "</table>  </td></tr>"  
    
    content = content + "<div style='position: absolute; top:3px; right:3px;'>"
		content = content + " <img src='/images/CloseButton.gif' onclick='HideSearch();' align='right' />"
		content = content + "</div>"
		content = content + "<table height='25px' width='100%'>"
		content = content + "  <tr bgcolor=#2F67A0 style='text-align:center; color:white;'>"
		content = content + "    <td align='center' width='250'>"
		content = content + "      Search results, click one "
		content = content + "    </td>"
		content = content + "  </tr>"
		content = content + "</table>"  
    content = content + "<table border='0' width='100%'>"
              
              
    Max = results.length;
    if (Max > (MaxItems-2))  { 
      Max = MaxItems-2;
      content = content + "<tr><td align='center' bgcolor='red' onclick='HideSearch();' >Too many! click here to refine your search</td></tr>" ;
      }
    for (var i=0, il=Max; i<il; i++)
      {
      var curFeature = results[i];
      attribs = curFeature.feature.attributes;  
      if (CurrentLayerID == TreeLayer)            {Key = attribs.Ref};
      if (CurrentLayerID == MonumentLayer)        {Key = attribs.Refno};
      if (CurrentLayerID == ListedBuildingLayer)  {Key = attribs.RefNo};
       
      content = content + "<tr><td align='center' onclick='ResultClick(" + i + ");'>" +  Key + "</td></tr>" ;
      }
    content = content + "</table>"    
    resulttxt = document.getElementById("RoadChoiceDiv");
    resulttxt.innerHTML = content;     
    document.PCodeForm.button.focus()
} else {
    if (RoadResults.length == 1) { // one result only
      ShowByID("NoneFoundDiv",false);
      ResultClick(0);
  } else {// no results
      ShowByID("NoneFoundDiv",true);
      ShowByID("RoadChoiceDiv",false);
    }
  }
  if (Searching > 0) { Searching = 0 ; }   
  //alert('Searching done'); 
  ShowSearching();     
}//-----------------------------------------------------------------------------------------
function ResultClick(Index) {
    var curFeature = RoadResults[Index];
    var graphic = curFeature.feature;
    var theExtent = curFeature.feature.geometry.getExtent();
    theExtent.xmax = theExtent.xmax + 100
    theExtent.ymax = theExtent.ymax + 100
    theExtent.xmin = theExtent.xmin - 100
    theExtent.ymin = theExtent.ymin - 100
    map.setExtent(theExtent);
    ShowByID("RoadChoiceDiv",false);
    document.PCodeForm.PCode.focus();
    Hilight(graphic);
}//-----------------------------------------------------------------------------------------


