var t;
var len;
var wildcard;
var lenSrch;
var strSrch;
var strSearch;
var strSndSurname;
var lenCont0;
var lenCont1;
var strSearchCont0;
var strSearchCont1;
var xmlhttp=null;
var results=false;
var w=window;
var d=document;
var errMsg='Your internet browser is unable to search this database. Suitable browsers include recent versions of Microsoft Internet Explorer, Mozilla Firefox and Safari. JavaScript must be enabled.'
function deathDtaLoad(){
  if(w.ActiveXObject){
    try{
      xmlhttp=new ActiveXObject('Msxml2.XMLHTTP');
    }
    catch(e){
      try{
        xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
      }
      catch(e){
        xmlhttp=false;
        alert(errMsg);
        if(w.history.length)w.history.back();
        return;
      }
    }
  }
  else if(w.XMLHttpRequest){
    try{
      xmlhttp=new XMLHttpRequest();
      xmlhttp.overrideMimeType('text/xml');
    }
    catch(e){
      xmlhttp=false;
      alert(errMsg);
      if(w.history.length)w.history.back();
      return;
    }
  }
  else{
    alert(errMsg);
    if(w.history.length)w.history.back();
    return;
  }
  if(xmlhttp){
    xmlhttp.open('GET', 'gazettedeaths.xml', true);
    xmlhttp.send(null);
    d.getElementById('strQuery').value="";
    d.getElementById('btnGo').disabled=false;
    d.getElementById('strQuery').focus();
  }
  else{
    alert(errMsg);
    if(w.history.length)w.history.back();
    return;
  }
}
function findQuery(){
  d.getElementById('btnGo').disabled='disabled';
  lenSrch=d.getElementById('strQuery').value.length;
  if(lenSrch>24)lenSrch=24;
  strSrch=d.getElementById('strQuery').value.toUpperCase();
  len=0;
  lenCont0=0;
  lenCont1=0;
  strSearch="";
  strSearchCont0="";
  strSearchCont1="";
  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;
  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 <b>surname</b> box with at least 2 characters of the name to be found.<br />";
    d.getElementById('btnGo').disabled=false;
    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.history.length)w.history.back();
    return;
  }
}
function runSearch(){
  var strName="";
  var strName2="";
  var strPrint="";
  var strArray=new Array();
  var dthDoc=xmlhttp.responseXML;
  var sList=dthDoc.getElementsByTagName('s');
  var gList=dthDoc.getElementsByTagName('g');
  var dList=dthDoc.getElementsByTagName('d');
  var rList=dthDoc.getElementsByTagName('r');
  var pList=dthDoc.getElementsByTagName('p');
  var aList=dthDoc.getElementsByTagName('a');
  var listLen=sList.length;
  var i=0;
  var count=0;
  while (i<listLen){
    strName=sList[i].firstChild.nodeValue;
    strName2=aList[i].firstChild.nodeValue.toLowerCase();
    var j=0;
    while(strName.charAt(j) != ' ' && strName.charAt(j) != '/' && strName.charAt(j))j++;
    if(d.getElementById('blnSound').checked){
      strName=calculateSound(strName.substr(0,j));
    }
    else if(d.getElementById('blnExact').checked){
      strName=strName.substr(0,j);
    }
    if(!wildcard)lenCont0=j;
    var m=strName.indexOf(strSearchCont0);
    if((d.getElementById('blnWildcards').checked && m>=0 && strName.indexOf(strSearchCont1, m+lenCont0-1)>=0) || (d.getElementById('blnExact').checked && strName.substr(0,lenCont0)==strSearchCont0 && strName.substr(j-lenCont1,lenCont1)==strSearchCont1) || (d.getElementById('blnSound').checked && strName==strSearch) || (strName2 !="" && !d.getElementById('blnExact').checked && strName2.indexOf(strSearch)>=0)){
      strArray [count]="<p><i>Surname: </i>"+sList[i].firstChild.nodeValue;
      if(gList[i].firstChild.nodeValue != '-')strArray[count] +="<br /><i>Given name(s): </i>"+gList[i].firstChild.nodeValue;
      if(aList[i].firstChild.nodeValue != '-')strArray[count] +="<br /><i>Alternative name(s): </i>"+aList[i].firstChild.nodeValue;
      if(dList[i].firstChild.nodeValue != '-')strArray[count] +="<br /><i>Death date: </i>"+dList[i].firstChild.nodeValue;
      if(pList[i].firstChild.nodeValue != '-')strArray[count] +="<br /><i>Death place: </i>"+pList[i].firstChild.nodeValue;
      if(rList[i].firstChild.nodeValue != '-')strArray[count] +="<br /><i>Date reported: </i>"+rList[i].firstChild.nodeValue;
      strArray[count] +="</p>";
      count++;
    }
    i++;
  }
  if(count){
    strArray.sort();
    strPrint='<h4 class="results">Search results <span style="font-size:13px; font-weight:normal;">(total of '+count+')</span></h4>';
    for(var i=0;i<count;i++)strPrint +=strArray[i];
    d.getElementById('strResult').innerHTML=strPrint;
    d.getElementById('htmlProgress').innerHTML='View the results below...';
    results=true;
  }
  else{d.getElementById('strResult').innerHTML="<h4 class='results'>Search results <span style='font-size:13px; font-weight:normal;'>(total of 0)</span></h4>The data you entered, <b><i>"+d.getElementById('strQuery').value+"</i></b>, is not in this database.";
  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++){
    if(strString.charAt(i)=='P' || strString.charAt(i)=='F' || strString.charAt(i)=='V')strSound +='1';
    else if(strString.charAt(i)=='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(strString.charAt(i)=='C' || (strString.charAt(i)=='S') || strString.charAt(i)=='K' || (strString.charAt(i)=='G' && i != intStrLen-1 && (!(i == intStrLen-2 && strString.charAt(intStrLen-1)=='N'))) || strString.charAt(i)=='J' || strString.charAt(i)=='Q' || strString.charAt(i)=='X' || strString.charAt(i)=='Z')strSound +='2';
    else if(strString.charAt(i)=='D' || strString.charAt(i)=='T') strSound +='3';
    else if(strString.charAt(i)=='L') strSound +='4';
    else if(strString.charAt(i)=='M' || strString.charAt(i)=='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(strString.charAt(i)=='R')strSound +='6';
    else if(strString.charAt(i)=='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(strString.charAt(i)=='W' || strString.charAt(i)=='Y'){
      if(i==0 && ((strString.charAt(0)=='W' && strString.charAt(1)!='R') || strString.charAt(0)=='Y'))strSound +='9';
      else if(strString.charAt(i)=='Y')strSound +='0';
    }
    else if(((strString.charAt(i)=='A' || strString.charAt(i)=='I' || strString.charAt(i)=='O') && (i>0 || (i==0 && strString.charAt(1)!="'"))) || (i != intStrLen-1 && strString.charAt(i)=='E') || strString.charAt(i)=='I' || strString.charAt(i)=='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)2003-2011 B Leadbeater, Australia
