var t,e;
var len,len2,len3;
var lenCont0,lenCont1;
var strSearchCont0,strSearchCont1;
var wildcard;
var wildcardType;
var lenSrch,lenSrch2,lenSrch3;
var strSrch,strSrch2,strSrch3;
var strSearch,strSearch2,strSearch3;
var strSndSurname="";
var xmlhttp=null;
var w=window;
var h=history;
var d=document;
function dtaLoad(){
var strErrMsg='Your internet browser is unable to search this database. Suitable browsers include recent versions of Internet Explorer, Firefox and Safari.'
  if(w.ActiveXObject){
    try{
      xmlhttp=new ActiveXObject('Msxml2.XMLHTTP');
    }
    catch(e){
      try{
        xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
      }
      catch(e){
        xmlhttp=false;
        alert(strErrMsg);
        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(strErrMsg);
      if(w.h.length)w.h.back();
      return;
    }
  }
  else{
    d.body.style.cursor='default';
    alert(strErrMsg);
    if(w.h.length)w.h.back();
    return;
  }
  if(xmlhttp){
    xmlhttp.open('GET','tbldb.xml',true);
    xmlhttp.send(null);
    d.getElementById('strQry').value="";
    d.getElementById('strQry2').value="";
    d.getElementById('strQry3').value="";
    d.getElementById('btnGo').disabled=false;
    d.getElementById('blnWildcards').checked="checked";
    d.getElementById('blnSortAlpha').checked="checked";
  }
  else{
    d.body.style.cursor='default';
    alert(strErrMsg);
    if(w.h.length)w.h.back();
    return;
  }
}
function findQuery(){
  var i;
  d.getElementById('btnGo').disabled='disabled';
  lenSrch=d.getElementById('strQry').value.length;
  if(lenSrch>28)lenSrch=28;
  lenSrch2=d.getElementById('strQry2').value.length;
  if(lenSrch2>24)lenSrch2=24;
  lenSrch3=d.getElementById('strQry3').value.length;
  if(lenSrch3>12)lenSrch3=12;
  strSrch=d.getElementById('strQry').value.toUpperCase();
  strSrch2=d.getElementById('strQry2').value.toLowerCase();
  strSrch3=d.getElementById('strQry3').value;
  len=0;
  lenCont0=0;
  lenCont1=0;
  len2=0;
  len3=0;
  strSearch="";
  strSearchCont0="";
  strSearchCont1="";
  strSearch2="";
  strSearch3="";
  wildcard=0;
  wildcardType="";
  for(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)=="*" || strSrch.charAt(i)=="?"){
      strSearch +=strSrch.charAt(i);
      len++;
    }
  }
  for(i=0;i<len;i++){
    switch (wildcard){
      case 0:
        if(strSearch.charAt(i)=="*" || strSearch.charAt(i)=="?")break;
        strSearchCont0 +=strSearch.charAt(i);
        lenCont0++;
        break;
      case 1:
        if(strSearch.charAt(i)=="*" || strSearch.charAt(i)=="?")break;
        strSearchCont1 +=strSearch.charAt(i);
        lenCont1++;
        break;
      default:
        if(strSearch.charAt(i)=="*" || strSearch.charAt(i)=="?")break;
        strSearchCont1 +=strSearch.charAt(i);
        lenCont1++;
        break;
    }
    if(strSearch.charAt(i)=="*"){
      wildcard++;
      wildcardType="*";
    }
    else if(strSearch.charAt(i)=="?"){
      wildcard++;
      wildcardType="?";
    }
  }
  len=lenCont0+lenCont1;
  for(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++;
    }
  }
  for(i=0;i<lenSrch3;i++){
    if((strSrch3.charAt(i)>='0' && strSrch3.charAt(i)<='9') || strSrch3.charAt(i)==" " || strSrch3.charAt(i)=="-"){
      strSearch3 +=strSrch3.charAt(i);
      len3++;
    }
  }
  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.</td></tr></table>";
    d.getElementById('btnGo').disabled=false;
    d.getElementById('strQry').focus();
    return;
  }
  else{
    d.getElementById('htmlProgress').innerHTML='Downloading the indexes ...';
    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 i,j;
var count=0;
var strName="";
var strName2="";
var strDate="";
var strArray=[];
var strPrint="";
var hlDoc=xmlhttp.responseXML;
var nHList=hlDoc.getElementsByTagName('n');
var gHList=hlDoc.getElementsByTagName('g');
var dHList=hlDoc.getElementsByTagName('d');
var aHList=hlDoc.getElementsByTagName('a');
var pHList=hlDoc.getElementsByTagName('p');
var rHList=hlDoc.getElementsByTagName('r');
var cHList=hlDoc.getElementsByTagName('c');
var hlLen=nHList.length;
var s=d.getElementById('blnSortAlpha').checked;
var k,m,n,q;
var intSrchType;
  rslt=0;
  if(d.getElementById('blnWildcards').checked)intSrchType=0;
  else if(d.getElementById('blnExact').checked)intSrchType=1;
  else if(d.getElementById('blnSound').checked)intSrchType=2;
  j=0;
  while(j<hlLen){
    strName=nHList[j].firstChild.nodeValue;
    if(intSrchType==2)strName=calculateSound(strName);
    strName2=gHList[j].firstChild.nodeValue.toLowerCase();
    strDate=dHList[j].firstChild.nodeValue;
    k=strName.length;
    m=strName.indexOf(strSearchCont0);
    n=strName.indexOf(strSearchCont1, m+lenCont0-1);
    q=n-m;
    if(strDate<'1852' && ((!intSrchType && (!wildcard || wildcardType=='*') && m>=0 && n>=0) || (!intSrchType && wildcardType=='?' && (q==lenCont0+1 || lenCont0==0 || lenCont1==0) && m>=0 && n>=0) || (intSrchType==1 && (!wildcard || wildcardType=='*') && strName.substr(0,lenCont0)==strSearchCont0 && strName.substr(k-lenCont1,lenCont1)==strSearchCont1) || (intSrchType==1 && wildcardType=='?' && k==(lenCont0+1+lenCont1) && strName.substr(0,lenCont0)==strSearchCont0 && strName.substr(k-lenCont1, lenCont1)==strSearchCont1) || (intSrchType==2 && strName==strSearch)) && (len2==0 || (len2>1 && strName2.indexOf(strSearch2)>=0) || (len2==1 && strName2.charAt(0)==strSearch2.charAt(0))) && strDate.indexOf(strSearch3)>=0){
      if(s){
        strArray[count]="<p>"+nHList[j].firstChild.nodeValue;
        if(gHList[j].firstChild.nodeValue !='?')strArray[count]+=" "+gHList[j].firstChild.nodeValue;
        strArray[count]+=" died "+dHList[j].firstChild.nodeValue;
      }
      else{
        strArray[count]="<p>"+dHList[j].firstChild.nodeValue+" death of "+nHList[j].firstChild.nodeValue;
        if(gHList[j].firstChild.nodeValue !='?')strArray[count]+=" "+gHList[j].firstChild.nodeValue;
      }
			if(aHList[j].firstChild.nodeValue !='?')strArray[count]+=" aged "+aHList[j].firstChild.nodeValue;
			if(pHList[j].firstChild.nodeValue !='?')strArray[count]+=" at "+pHList[j].firstChild.nodeValue;
			if(rHList[j].firstChild.nodeValue !='?')strArray[count]+="; relative "+rHList[j].firstChild.nodeValue;
			switch(cHList[j].firstChild.nodeValue){
				case 'w':
					strArray[count]+="; buried Adelaide West Terrace Cemetery";
					break;
				case 'hi1':
					strArray[count]+="; buried Hindmarsh Cemetery";
					break;
				case 'al0':
					strArray[count]+="; buried Alberton St Paul Anglican Cemetery";
					break;
				case 'wa5':
					strArray[count]+="; buried Walkerville Wesleyan Methodist Cemetery";
					break;
				case 'bl3':
					strArray[count]+="; buried Blakiston St James Anglican Cemetery";
					break;
			}
      strArray[count]+="</p>";
      count++;
    }
    j++;
  }
  if(count){
    strArray.sort();
    strPrint='<h3>Death search results <span style="font:87% normal;">(total of '+count+')</span></h3>';
    for(i=0;i<count;i++)strPrint +=strArray[i];
    d.getElementById('strResult').innerHTML=strPrint;
    d.getElementById('htmlProgress').innerHTML='View the results below...';
  }
  else{
    d.getElementById('strResult').innerHTML="<h3>Search results <span style='font:93% normal;'>(total of 0)</span></h3><p></p>";
    d.getElementById('htmlProgress').innerHTML='There are no results to view.';
  }
  if(!d.getElementById('strQry').disabled) d.getElementById('strQry').focus();
  d.getElementById('btnGo').disabled=false;
}
function calculateSound(strString){
var strModSound="";
var strSound="";
var i;
var intStrLen=strString.length;
  for(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(){
  d.getElementById('htmlProgress').innerHTML="";
  d.getElementById('strResult').innerHTML="";
}
function wo(path){
	path="./cemeteries/"+path;
  imgWin=window.open(path,'imgWin','width=720,height=600');
  imgWin.focus();
}//Copyright(c)2003-2012 B Leadbeater, Australia
