function showPEBResult(fset,LayerName) {      
  var theDiv = document.getElementById("ResultTableDiv");
  var content = theDiv.innerHTML;
  ShowByID("ResultTableDiv",true);
    
  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];
      content = content + "<tr><td align='center'> " ;  
      if (LayerName == "Planning") {
				content = content + "<a target='_blank' href='http://planningexplorer.gov.gg/portal/servlets/ApplicationSearchServlet?PKID="  + graphic.attributes.LINK + "' style='color:black; font-weight: bold;' >" ;      
      } else {
				content = content + "<a target='_blank' href='http://buildingexplorer.gov.gg/northgate/BCWebServer/Redirect.aspx?PARAM0="  + graphic.attributes.LINK + "' style='color:black; font-weight: bold;' >" ;      
      } 
      content = content + LayerName + " : " + graphic.attributes.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 FindKeysPlanning(evt) {
  queryTask = new esri.tasks.QueryTask(MapServicePath + "/" + PlanningLayer);  
  query = new esri.tasks.Query();
  query.returnGeometry = true;
  query.outFields = ["Key","LINK"];
  query.geometry = evt.mapPoint;
  queryTask.execute(query, function(fset) {  showPEBResult(fset,"Planning");  });  
}//----------------------------------------------------------------------------------------- 
function FindKeysBuildingControl(evt) {
  queryTask = new esri.tasks.QueryTask(MapServicePath + "/" + BuildingControlLayer );  
  query = new esri.tasks.Query();
  query.returnGeometry = true;
  query.outFields = ["Key","LINK"];
  query.geometry = evt.mapPoint;
  queryTask.execute(query, function(fset) {  showPEBResult(fset,"BuildingControl");  });  
}//-----------------------------------------------------------------------------------------
function SearchTypeClick(ID) {   
	var Default = ""  ;
  ShowByID("NoneFoundDiv",false);
  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 == "planning") { 
		Content = "Searching for a Planning Application:<br/>e.g. 'FULL/2009/1234'. The map will zoom in on the property concerned (Outlined in light blue).";
	} else if (SearchTool == "building") { 
		Content = "Searching for a Building Application:<br/>e.g. 'FP/2009/1234'. The map will zoom in on the property 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 == "planning")    { Search(PlanningLayer); }
  else if (SearchTool == "building")    { Search(BuildingControlLayer); }  
}//-----------------------------------------------------------------------------------------
function FindKeys(evt) {
  var theDiv = document.getElementById("ResultTableDiv");
  var content = "" ;
  ShowByID("ResultTableDiv",false);
  theDiv.innerHTML = content;
  map.graphics.clear(); 
  FindKeysPlanning(evt);
  FindKeysBuildingControl(evt);
  Searching = 2;
  ShowSearching();
}//-----------------------------------------------------------------------------------------
function Search(ID) {
  Pcode = SearchTxt.replace(/[/\\]/g,"-"); 
  findTask = new esri.tasks.FindTask(MapServicePath);
  findParams = new esri.tasks.FindParameters();
  findParams.returnGeometry = true;
  findParams.layerIds = [ID];
  findParams.searchFields = ["Key"];
  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 Max = 10;
  var Warn = false ;
  
  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 = 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 width='100%'>"               
              
    Max = results.length;
    if (Max > (MaxItems-2))  { 
      Max = MaxItems-2;
      content = content + "<tr><td align='center' bgcolor='red' onclick='HideSearch();'>Too many results, refine your search</td></tr>" ;
    }
    for (var i=0, il=Max; i<il; i++)
      {
      var curFeature = results[i];
      attribs = curFeature.feature.attributes;   
      content = content + "<tr><td align='center' onclick='ResultClick(" + i + ");'>" +  attribs.Key + "</td></tr>" ;
        
			var graphic = curFeature.feature;
			symbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_NULL, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0,255,255]), 4), new dojo.Color([0,255,255,0.25]));
			graphic.setSymbol(symbol);
			map.graphics.add(graphic);
      }
    content = content + "</table>"    
    resulttxt = document.getElementById("RoadChoiceDiv");
    resulttxt.innerHTML = content;     
    document.PCodeForm.button.focus()
		} else {   
      ShowByID("NoneFoundDiv",true);
      ShowByID("RoadChoiceDiv",false);
    }  
   if (RoadResults.length == 1) { // one result only
      ShowByID("NoneFoundDiv",false);
      RoadClick(0);
      }
  if (Searching > 0) { Searching = 0 ; }  
  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);
}//-----------------------------------------------------------------------------------------
