var t;
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 results=false;
function gdDtaLoad(){
  if(window.ActiveXObject){
    try{
      xmlhttp=new ActiveXObject('Msxml2.XMLHTTP');
    }
    catch(e){
      try{
        xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
      }
      catch(e){
        xmlhttp=false;
        alert('Your internet browser is unable to use this database. Suitable browsers include recent versions of Microsoft Internet Explorer, Mozilla Firefox  , Netscape and Safari. JavaScript must be enabled.');
        if(window.history.length)window.history.back();
        return;
      }
    }
  }
  else if(window.XMLHttpRequest){
    try{
      xmlhttp=new XMLHttpRequest();
      xmlhttp.overrideMimeType('text/xml');
    }
    catch(e){
      xmlhttp=false;
      alert('Your internet browser is unable to use this database. Suitable browsers include recent versions of Microsoft Internet Explorer, Mozilla Firefox  , Netscape and Safari. JavaScript must be enabled.');
      if(window.history.length) window.history.back();
      return;
    }
  }
  else{
    alert('Your internet browser is unable to use this database. Suitable browsers include recent versions of Microsoft Internet Explorer, Mozilla Firefox  , Netscape and Safari. JavaScript must be enabled.');
    if(window.history.length)window.history.back();
    return;
  }
  if(xmlhttp){
    xmlhttp.open('GET', 'tblgd.xml', true);
    xmlhttp.send(null);
    document.getElementById('strQuery').value="";
    document.getElementById('strQuery2').value="";
    document.getElementById('btnGo').disabled=false;
    document.getElementById('strQuery').focus();
  }
  else{
    alert('Your internet browser is unable to use this database. Suitable browsers include recent versions of Microsoft Internet Explorer, Mozilla Firefox  , Netscape and Safari.');
    if(window.history.length)window.history.back();
    return;
  }
}
function findQuery(){
  document.getElementById('btnGo').disabled='disabled';
  lenSrch=document.getElementById('strQuery').value.length;
  if(lenSrch>24)lenSrch=24;
  lenSrch2=document.getElementById('strQuery2').value.length;
  if(lenSrch2>24)lenSrch2=24;
  strSrch=document.getElementById('strQuery').value.toUpperCase();
  strSrch2=document.getElementById('strQuery2').value.toLowerCase();
  len=0;
  lenCont0=0;
  lenCont1=0;
  len2=0;
  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) == " " || strSrch2.charAt(i) == "-" || strSrch2.charAt(i) == "'"){
      strSearch2 += strSrch2.charAt(i);
      len2++;
    }
  }
  if(len < 2){
    document.getElementById('strResult').innerHTML="<table border='0' cellspacing='0'><tr><th class='error'>Data Entry Error</th></tr><tr><td class='error'>Fill in the <b>surname</b> box with at least 2 characters of the name to be found. A given name is optional. If only initials are known, enter the 1st initial only.<br />";
    document.getElementById('btnGo').disabled=false;
    document.getElementById('strQuery').focus();
    return;
  }
  else{
    document.getElementById('htmlProgress').innerHTML='Downloading the index ...';
    if(document.getElementById('blnSound').checked) strSearch=calculateSound(strSearch);
    xmlhttp.onreadystatechange=checkReadyState;
    if(xmlhttp.readyState == 4 && xmlhttp.status == '200'){
      document.getElementById('htmlProgress').innerHTML='Searching ...';
      if(t)window.clearTimeout(t);
      t=window.setTimeout('runSearch()',200);
    }
  }
}
function checkReadyState(){
  if(xmlhttp.readyState == 4 && xmlhttp.status == '200'){
    document.getElementById('htmlProgress').innerHTML='Searching ...';
    if(t)window.clearTimeout(t);
    t=window.setTimeout('runSearch()',200);
  }
  else if(xmlhttp.readyState == 4 && xmlhttp.status != '200'){
    alert('An error occurred while loading the index');
    if(window.history.length) window.history.back();
    return;
  }
}
function runSearch(){
  var limit=100;
  var count=0;
  var toDisplay;
  var strName="";
  var strName2="";
  var strNameC="";
  var strNameC2="";
  var strNameN="";
  var strArray=new Array();
  var strPrint="";
  var gdDoc=xmlhttp.responseXML;
  var eList=gdDoc.getElementsByTagName('e');
  var sList=gdDoc.getElementsByTagName('s');
  var gList=gdDoc.getElementsByTagName('g');
  var cList=gdDoc.getElementsByTagName('c');
  var fList=gdDoc.getElementsByTagName('f');
  var rList=gdDoc.getElementsByTagName('r');
  var dList=gdDoc.getElementsByTagName('d');
  var wList=gdDoc.getElementsByTagName('w');
  var nList=gdDoc.getElementsByTagName('n');
  var listLen=sList.length;
  var i=0;
  while(i < listLen){
    strName=sList[i].firstChild.nodeValue.toUpperCase();
    strName2=gList[i].firstChild.nodeValue.toLowerCase();
    strNameC=cList[i].firstChild.nodeValue.toUpperCase();
    strNameC2=fList[i].firstChild.nodeValue.toLowerCase();
    strNameN=nList[i].firstChild.nodeValue;
    var j=0;
    while(strName.charAt(j) != ' ' && strName.charAt(j) != '/' && strName.charAt(j)) j++;
    var k=0;
    while (strNameC.charAt(k) != ' ' && strNameC.charAt(k) != '/' && strNameC.charAt(k)) k++;
    if(document.getElementById('blnSound').checked){
      strName=calculateSound(strName.substr(0,j));
      strNameC=calculateSound(strNameC.substr(0,k));
    }
    else if(document.getElementById('blnExact').checked){
      strName=strName.substr(0,j);
      strNameC=strNameC.substr(0,k);
    }
    if(!wildcard){
      if(j > k) lenCont0=j;
      else lenCont0=k;
    }
    var m=strName.indexOf(strSearchCont0);
    var mC=strNameC.indexOf(strSearchCont0);
    var mN=strNameN.indexOf(strSearchCont0);
    if((((document.getElementById('blnWildcards').checked && m >= 0 && strName.indexOf(strSearchCont1, m+lenCont0-1) >= 0) || (document.getElementById('blnExact').checked && strName.substr(0, lenCont0) == strSearchCont0 && strName.substr(j - lenCont1, lenCont1) == strSearchCont1) || (document.getElementById('blnSound').checked && strName == strSearch)) && (len2 == 0 || (len2 > 1 && strName2.indexOf(strSearch2) >= 0) || (len2 == 1 && strName2.charAt(0) == strSearch2.charAt(0)))) || (((document.getElementById('blnWildcards').checked && mC >= 0 && strNameC.indexOf(strSearchCont1, mC+lenCont0-1) >= 0) || (document.getElementById('blnExact').checked && strNameC.substr(0, lenCont0) == strSearchCont0 && strNameC.substr(k - lenCont1, lenCont1) == strSearchCont1) || (document.getElementById('blnSound').checked && strNameC == strSearch)) && (len2 == 0 || (len2 > 1 && strNameC2.indexOf(strSearch2) >= 0) || (len2 == 1 && strNameC2.charAt(0) == strSearch2.charAt(0)))) || (mN >= 0 && strNameN.indexOf(strSearchCont1, mN+lenCont0-1) >= 0 && strNameN.indexOf(strSearch2) >= 0)){
      if(sList[i].firstChild.nodeValue != '-') strArray [count]=sList[i].firstChild.nodeValue + " " + gList[i].firstChild.nodeValue;
      else strArray [count]="[not recorded]";
      if(wList[i].firstChild.nodeValue != '-'){
        if(eList[i].firstChild.nodeValue == '0') strArray [count] += " deposited " + wList[i].firstChild.nodeValue + " of gold";
        else strArray [count] += " consigned " + wList[i].firstChild.nodeValue + " of gold";
      }
      else if(eList[i].firstChild.nodeValue == 'x') strArray [count] += " consigned gold";
      else strArray [count] += " deposited gold";
      if(cList[i].firstChild.nodeValue != '-'){
        strArray [count] += " to " + cList[i].firstChild.nodeValue + " "+ fList[i].firstChild.nodeValue;
        if(rList[i].firstChild.nodeValue != '-') strArray [count] += " of " + rList[i].firstChild.nodeValue;
      }
      if(eList[i].firstChild.nodeValue > '0' && eList[i].firstChild.nodeValue < '4') strArray [count] += " which arrived by escort " + eList[i].firstChild.nodeValue;
      if(dList[i].firstChild.nodeValue != '-') strArray [count] += " on " + dList[i].firstChild.nodeValue;
      if(nList[i].firstChild.nodeValue != '-') strArray [count] += ". Note: " + nList[i].firstChild.nodeValue;
      strArray [count] += ".<br />";
      count++;
    }
    i++;
  }
  if(count){
    if(count > limit){
      toDisplay=limit;
    }
    else toDisplay=count;
    strArray.sort();
    strPrint='<h3 class="results">Search results <span style="font-size:13px; font-weight:normal;">(total of ' + toDisplay + ')</span></h3><p>';
    for(var i=0; i<toDisplay; i++) strPrint += strArray [i];
    document.getElementById('strResult').innerHTML=strPrint+'</p>';
    document.getElementById('htmlProgress').innerHTML='View the results below...';
    results=true;
  }
  else{
    document.getElementById('strResult').innerHTML="<h3 class='results'>Search results <span style='font-size:13px; font-weight:normal;'>(total of 0)</span></h3>The data you entered, <b><i>" + document.getElementById('strQuery').value + " " + document.getElementById('strQuery2').value + "</i></b>, is not in this database.";
    document.getElementById('htmlProgress').innerHTML='There are no results to view.';
  }
  if(!document.getElementById('strQuery').disabled) document.getElementById('strQuery').focus();
  document.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 && strChar=='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' && strChar=='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 && ((strChar=='W' && strString.charAt(1)!='R') || strChar=='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);
  strSoundLen=strSound.length;
  for(i=1; i<strSoundLen; i++){
    if(strSound.charAt(i)!=strSound.charAt(i-1)) strModSound += strSound.charAt(i);
  }
  return strModSound;
}
function clearForm(){
  document.getElementById('htmlProgress').innerHTML="";
  document.getElementById('strResult').innerHTML="";
}//Copyright(c)2003-2011 B Leadbeater, Australia
