var t;
var toDisplay;
var len;
var len2;
var wildcard;
var lenSrch;
var lenSrch2;
var strSrch;
var strSrch2;
var strSearch;
var strSearch2;
var strSndSurname="";
var lenCont0;
var lenCont1;
var strSearchCont0;
var strSearchCont1;
var xmlhttp=null;
var htmlResults="";
var results=false;
var w=window;
var h=history;
var d=document;
function dtaLoad(){
  if(w.ActiveXObject){
    try{
      xmlhttp=new ActiveXObject('Msxml2.XMLHTTP');
    }
    catch(e){
      try{
        xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
      }
      catch(e){
        xmlhttp=false;
        alert('Your internet browser is unsuitable for using this database. Suitable browsers include recent versions of Microsoft Internet Explorer, Mozilla Firefox and Safari.');
        if(w.h.length)w.h.back();
        return;
      }
    }
  }
  else if(w.XMLHttpRequest){
    try{
      xmlhttp=new XMLHttpRequest();
      xmlhttp.overrideMimeType('text/xml');
    }
    catch(e){
      xmlhttp=false;
      alert('Your internet browser is unsuitable for using this database. Suitable browsers include recent versions of Microsoft Internet Explorer, Mozilla Firefox and Safari.');
      if(w.h.length)w.h.back();
      return;
    }
  }
  else{
    alert('Your internet browser is unsuitable for using this database. Suitable browsers include recent versions of Microsoft Internet Explorer, Mozilla Firefox and Safari.');
    if(w.h.length)w.h.back();
    return;
  }
  if(xmlhttp){
    xmlhttp.open('GET', 'landselectors.xml', true);
    xmlhttp.send(null);
    d.getElementById('dbType').value="s";
    d.getElementById('strQuery').value="";
    d.getElementById('strQuery2').value="";
    d.getElementById('btnGo').disabled=false;
    d.getElementById('strQuery').focus();
  }
  else{
    alert('Your internet browser is unsuitable for using this database. Suitable browsers include recent versions of Microsoft Internet Explorer, Mozilla Firefox and Safari.');
    if(w.h.length) w.h.back();
    return;
  }
}
function changeSelection(){
  if(d.getElementById('dbType').value=="sl"){
    d.getElementById('strMain').innerHTML="hundred&nbsp;<sup>(required)</sup>";
    d.getElementById('strGiven').innerHTML="county/ag&nbsp;area&nbsp;<sup>(optional)</sup>";
    d.getElementById('strQuery2').value="";
  } else{
    d.getElementById('strMain').innerHTML="surname&nbsp;<sup>(required)</sup>";
    d.getElementById('strGiven').innerHTML="given&nbsp;name&nbsp;<sup>(optional)</sup>";
    d.getElementById('strQuery2').value="";
  }
  d.getElementById('strQuery').focus();
}
function findQuery(){
  d.getElementById('btnGo').disabled='disabled';
  d.getElementById('htmlProgress').innerHTML='';
  lenSrch=d.getElementById('strQuery').value.length;
  if(lenSrch>24)lenSrch=24;
  lenSrch2=d.getElementById('strQuery2').value.length;
  if(lenSrch2>24)lenSrch2=24;
  strSrch=d.getElementById('strQuery').value.toUpperCase();
  strSrch2=d.getElementById('strQuery2').value.toLowerCase();
  len=0;
  lenCont0=0;
  lenCont1=0;
  len2=0;
  strName="";
  strName2="";
  strNameLower="";
  strSearch="";
  strSearchCont0="";
  strSearchCont1="";
  strSearch2="";
  wildcard=0;
  for (var i=0; i<lenSrch; i++){
    if((strSrch.charAt(i) >= 'A' && strSrch.charAt(i) <= 'Z') || strSrch.charAt(i) == "'" || strSrch.charAt(i) == "-" || strSrch.charAt(i) == " " || strSrch.charAt(i) == "*"){
      strSearch += strSrch.charAt(i);
      len++;
    }
  }
  for (var i=0; i<len; i++){
    switch (wildcard){
      case 0:
        if(strSearch.charAt(i) == "*") break;
        strSearchCont0 += strSearch.charAt(i);
        lenCont0++;
        break;
      case 1:
        if(strSearch.charAt(i) == "*") break;
        strSearchCont1 += strSearch.charAt(i);
        lenCont1++;
        break;
      default:
        if(strSearch.charAt(i) == "*") break;
        strSearchCont1 += strSearch.charAt(i);
        lenCont1++;
        break;
    }
    if(strSearch.charAt(i) == "*") wildcard++;
  }
  len=lenCont0 + lenCont1;
  for (var i=0; i<lenSrch2; i++){
    if((strSrch2.charAt(i) >= 'a' && strSrch2.charAt(i) <= 'z') || (strSrch2.charAt(i) >= '0' && strSrch2.charAt(i) <= '9') || strSrch2.charAt(i) == " " || strSrch2.charAt(i) == "-" || strSrch2.charAt(i) == "'" || strSrch2.charAt(i) == "("){
      strSearch2 += strSrch2.charAt(i);
      len2++;
    }
  }
  if(len < 2){
    d.getElementById('strResult').innerHTML="<table border='0' cellspacing='0'><tr><th class='error'>Data Entry Error</th></tr><tr><td class='error'>Fill in the left hand box with at least 2 characters of the names to be found.<br/>";
    d.getElementById('strQuery').focus();
    return;
  }
  else{
    d.getElementById('htmlProgress').innerHTML='Downloading the index ...';
    if(d.getElementById('blnSound').checked) strSearch=calculateSound(strSearch);
    xmlhttp.onreadystatechange=checkReadyState;
    if(xmlhttp.readyState == 4 && xmlhttp.status == '200'){
      d.getElementById('htmlProgress').innerHTML='Searching ...';
      if(t)w.clearTimeout(t);
      t=w.setTimeout('runSearch()',200);
    }
  }
}

function checkReadyState(){
  if(xmlhttp.readyState == 4 && xmlhttp.status == '200'){
    d.getElementById('htmlProgress').innerHTML='Searching ...';
    if(t)w.clearTimeout(t);
    t=w.setTimeout('runSearch()',200);
  }
  else if(xmlhttp.readyState == 4 && xmlhttp.status != '200'){
    alert('An error occurred while loading the index');
    if(w.h.length) w.h.back();
    return;
  }
}
function runSearch(){
  var limit=400;
  var i, m;
  var count=0;
  var strArray=new Array();
  var strPrint="";
  var lndDoc=xmlhttp.responseXML;
  var sList=lndDoc.getElementsByTagName('s');
  var gList=lndDoc.getElementsByTagName('g');
  var cList=lndDoc.getElementsByTagName('c');
  var hList=lndDoc.getElementsByTagName('h');
  var nList=lndDoc.getElementsByTagName('n');
  var aList=lndDoc.getElementsByTagName('a');
  var pList=lndDoc.getElementsByTagName('p');
  var dList=lndDoc.getElementsByTagName('d');
  var zList=lndDoc.getElementsByTagName('z');
  var rList=lndDoc.getElementsByTagName('r');
  var listLen=sList.length;
  var intSrchType;
  if(d.getElementById('blnWildcards').checked)intSrchType=0;
  else if(d.getElementById('blnExact').checked)intSrchType=1;
  else if(d.getElementById('blnSound').checked)intSrchType=2;
  i=0;
  while (i < listLen){
    if(d.getElementById('dbType').value == 's'){
      strName=sList[i].firstChild.nodeValue.toUpperCase();
      if(gList[i].firstChild.nodeValue) strName2=gList[i].firstChild.nodeValue.toLowerCase();
      if(intSrchType==2)strName=calculateSound(strName);
      if((((!intSrchType && (m=strName.indexOf(strSearchCont0) >= 0) && strName.indexOf(strSearchCont1, m+lenCont0-1) >= 0) || (intSrchType==1 && strName.substr(0, lenCont0) == strSearchCont0 && strName.substr(strName.length-lenCont1, lenCont1) == strSearchCont1) || (intSrchType==2 && strName == strSearch)) && (len2 == 0 || (len2 > 1 && strName2.indexOf(strSearch2) >= 0) || (len2 == 1 && strName2.charAt(0) == strSearch2.charAt(0))))){
        strArray [count]="<p>"+sList[i].firstChild.nodeValue;
        strArray [count] += " " + gList[i].firstChild.nodeValue;
        strArray [count] += "; " + cList[i].firstChild.nodeValue;
        strArray [count] += "; Hd " + hList[i].firstChild.nodeValue;
        strArray [count] += "; Sect " + nList[i].firstChild.nodeValue;
        if(aList[i].firstChild.nodeValue !='-')strArray [count] += "; total area " + aList[i].firstChild.nodeValue + " acres";
        if(pList[i].firstChild.nodeValue !='-')strArray [count] += "; " + pList[i].firstChild.nodeValue;
        if(dList[i].firstChild.nodeValue !='-')strArray [count] += "; " + dList[i].firstChild.nodeValue;
        if(zList[i].firstChild.nodeValue !='-')strArray [count] += "; Notes: " + zList[i].firstChild.nodeValue;
        strArray [count] += "; Source: " + rList[i].firstChild.nodeValue + "</p>";
        count++;
      }
      i++;
    }
    else{
      if(hList[i].firstChild.nodeValue) strName=hList[i].firstChild.nodeValue.toUpperCase();
      if(cList[i].firstChild.nodeValue) strName2=cList[i].firstChild.nodeValue.toLowerCase();
      if(d.getElementById('blnSound').checked) strName=calculateSound(strName);
      if((((!intSrchType && (m=strName.indexOf(strSearchCont0) >= 0) && strName.indexOf(strSearchCont1, m+lenCont0-1) >= 0) || (intSrchType==1 && strName.substr(0, lenCont0) == strSearchCont0 && strName.substr(strName.length-lenCont1, lenCont1) == strSearchCont1) || (intSrchType==2 && strName == strSearch)) && strName2.indexOf(strSearch2) >= 0)){
        strArray [count]="<p>"+sList[i].firstChild.nodeValue;
        strArray [count] += " " + gList[i].firstChild.nodeValue;
        strArray [count] += "; " + cList[i].firstChild.nodeValue;
        strArray [count] += "; Hd " + hList[i].firstChild.nodeValue;
        strArray [count] += "; Sect " + nList[i].firstChild.nodeValue;
        if(aList[i].firstChild.nodeValue !='-')strArray [count] += "; total area " + aList[i].firstChild.nodeValue + " acres";
        if(pList[i].firstChild.nodeValue !='-')strArray [count] += "; " + pList[i].firstChild.nodeValue;
        if(dList[i].firstChild.nodeValue !='-')strArray [count] += "; " + dList[i].firstChild.nodeValue;
        if(zList[i].firstChild.nodeValue !='-')strArray [count] += "; Notes: " + zList[i].firstChild.nodeValue;
        strArray [count] += "; Source: " + rList[i].firstChild.nodeValue + "</p>";
        count++;
      }
      i++;
    }
  }
  if(count){
    if(count > limit) toDisplay=limit;
    else toDisplay=count;
    strArray.sort();
    strPrint='<h3 class="results">Search results <span style="font-size:12px; font-weight:normal;">(total of ' + toDisplay + ')</span></h3>';
    for (i=0; i<toDisplay; i++) strPrint += strArray [i];
    d.getElementById('strResult').innerHTML=strPrint;
    d.getElementById('htmlProgress').innerHTML='View the results below...';
    results=true;
  }
  else{
    d.getElementById('strResult').innerHTML="<h3 class='results'>Search results <span style='font-size:12px; font-weight:normal;'>(total of 0)</span></h3><p>The data you entered is not in this database.</p>";
    d.getElementById('htmlProgress').innerHTML='There are no results to view.';
    results=false;
  }
  if(!d.getElementById('strQuery').disabled) d.getElementById('strQuery').focus();
  d.getElementById('btnGo').disabled=false;
}
function calculateSound(strString){
  var strModSound="";
  var strSound="";
  var intStrLen=strString.length;
  for(var i=0; i<intStrLen; i++){
    var strChar=strString.charAt(i);
    if(strChar=='P' || strChar=='F' || strChar=='V')strSound +='1';
    else if(strChar=='B'){
      if(strString.substr(intStrLen-2,2)!='MB' && strString.substr(intStrLen-3,3)!='MBE' && strString.substr(intStrLen-4,4)!='MBES' && strString.substr(intStrLen-3,3)!='MBS')strSound +='1';
    }
    else if(strChar=='C' || (strChar=='S') || strChar=='K' || (strChar=='G' && i != intStrLen-1 && (!(i == intStrLen-2 && strString.charAt(intStrLen-1)=='N'))) || strChar=='J' || strChar=='Q' || strChar=='X' || strChar=='Z')strSound +='2';
    else if(strChar=='D' || strChar=='T')strSound +='3';
    else if(strChar=='L') strSound +='4';
    else if(strChar=='M' || strChar=='N'){
      if(i==1 && strString.charAt(1)=='N' && (strString.charAt(0)=='G' || strString.charAt(0)=='K' || strString.charAt(0)=='P'))strSound='5';
      else strSound +='5';
      if(i==0 && strString.charAt(1)=='C' && strString.charAt(0)=='M')strSound += '0';
    }
    else if(strChar=='R')strSound +='6';
    else if(strChar=='H'){
      if(i>1 && strString.charAt(i-1)=='G' && (strString.charAt(i-2)=='A' || strString.charAt(i-2)=='I' || strString.charAt(i-2)=='O' || strString.charAt(i-2)=='U')){
        strSound=strSound.substring(0,strSound.length-1);
      }
    }
    else if(strChar=='W' || strChar=='Y'){
      if(i==0 && ((strString.charAt(0)=='W' && strString.charAt(1)!='R') || strString.charAt(0)=='Y')) strSound +='9';
      else if(strChar=='Y')strSound +='0';
    }
    else if(((strChar=='A' || strChar=='I' || strChar=='O') && (i>0 || (i==0 && strString.charAt(1)!="'"))) || (i != intStrLen-1 && strChar=='E') || strChar=='I' || strChar=='U')strSound +='0';
  }
  strModSound=strSound.charAt(0);
  for (i=1; i<strSound.length; i++){
    if(strSound.charAt(i)!=strSound.charAt(i-1)) strModSound += strSound.charAt(i);
  }
  return strModSound;
}
function clearForm(){
  d.getElementById('htmlProgress').innerHTML="";
  d.getElementById('strResult').innerHTML="";
}// Copyright (c) 2006-2011 B Leadbeater, Australia
