var t;
var len;
var len2;
var wildcard;
var strSearch;
var strSearch2;
var strSndSurname="";
var xmlhttp=null;
var htmlResults="";
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 , Netscape 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 , Netscape 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 , Netscape and Safari.');
    if(w.h.length) w.h.back();
    return;
  }
  if(xmlhttp){
    xmlhttp.open('GET', 'ww1servicemen.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 , Netscape and Safari.');
    if(w.h.length) w.h.back();
    return;
  }
}
function changeSelection(){
  d.getElementById('strQuery').focus();
  if(d.getElementById('dbType').value=="sl"){
    d.getElementById('strMain').innerHTML="<b>place</b> (suburb/town/district)";
    d.getElementById('strGiven').innerHTML="";
    d.getElementById('strQuery').value="";
    d.getElementById('strQuery').style.width='18em';
    d.getElementById('strQuery2').value="";
    d.getElementById('strQuery2').disabled=true;
    d.getElementById('strQuery2').style.visibility='hidden';
    d.getElementById('blnSound').disabled=true;
    d.getElementById('blnS').disabled=true;
    d.getElementById('blnExact').disabled=true;
    d.getElementById('blnE').disabled=true;
  }
  else{
    d.getElementById('strMain').innerHTML="<b>surname</b>&nbsp;<sup>(required)</sup>";
    d.getElementById('strGiven').innerHTML="<b>given&nbsp;name</b>&nbsp;<sup>(optional)</sup>";
    d.getElementById('strQuery').value="";
    d.getElementById('strQuery').style.width='12em';
    d.getElementById('strQuery2').value="";
    d.getElementById('strQuery2').disabled=false;
    d.getElementById('strQuery2').style.visibility='visible';
    d.getElementById('blnSound').disabled=false;
    d.getElementById('blnS').disabled=false;
    d.getElementById('blnExact').disabled=false;
    d.getElementById('blnE').disabled=false;
  }
}
function findQuery(){
  d.getElementById('btnGo').disabled='disabled';
  d.getElementById('htmlProgress').innerHTML='';
  var lenSrch=d.getElementById('strQuery').value.length;
  if(lenSrch>24)lenSrch=24;
  var lenSrch2=d.getElementById('strQuery2').value.length;
  if(lenSrch2>24)lenSrch2=24;
  var strSrch=d.getElementById('strQuery').value.toUpperCase();
  var strSrch2=d.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){
    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 surname 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=200;
  var count=0;
  var m;
  var toDisplay;
  var strName="";
  var strName2="";
  var strNameLower="";
  var strPrint="";
  var strArray=new Array();
  var dthDoc=xmlhttp.responseXML;
  var sList=dthDoc.getElementsByTagName('s');
  var gList=dthDoc.getElementsByTagName('g');
  var jList=dthDoc.getElementsByTagName('j');
  var kList=dthDoc.getElementsByTagName('k');
  var fList=dthDoc.getElementsByTagName('f');
  var mList=dthDoc.getElementsByTagName('m');
  var oList=dthDoc.getElementsByTagName('o');
  var hList=dthDoc.getElementsByTagName('h');
  var lList=dthDoc.getElementsByTagName('l');
  var wList=dthDoc.getElementsByTagName('w');
  var wdList=dthDoc.getElementsByTagName('wd');
  var wnList=dthDoc.getElementsByTagName('wn');
  var eList=dthDoc.getElementsByTagName('e');
  var nList=dthDoc.getElementsByTagName('n');
  var rList=dthDoc.getElementsByTagName('r');
  var uList=dthDoc.getElementsByTagName('u');
  var qList=dthDoc.getElementsByTagName('q');
  var tList=dthDoc.getElementsByTagName('t');
  var vList=dthDoc.getElementsByTagName('v');
  var dList=dthDoc.getElementsByTagName('d');
  var pList=dthDoc.getElementsByTagName('p');
  var cList=dthDoc.getElementsByTagName('c');
  var aList=dthDoc.getElementsByTagName('a');
  var bList=dthDoc.getElementsByTagName('b');
  var xList=dthDoc.getElementsByTagName('x');
  var yList=dthDoc.getElementsByTagName('y');
  var iList=dthDoc.getElementsByTagName('i');
  var nzList=dthDoc.getElementsByTagName('nz');
  var dthListLen=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;
  if(d.getElementById('dbType').value=='s'){
    var i=0;
    while (i<dthListLen){
      strName=sList[i].firstChild.nodeValue.toUpperCase();
      if(gList[i].firstChild.nodeValue != '-') strName2=gList[i].firstChild.nodeValue.toLowerCase();
      else strName2="";
      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><i>Surname: </i><b>"+sList[i].firstChild.nodeValue;
        strArray [count] +="</b><br /><i>Given name(s): </i><b>"+gList[i].firstChild.nodeValue+"</b>";
        if(jList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Birth date: </i>"+jList[i].firstChild.nodeValue;
        strArray [count] +="<br /><i>Birth place: </i>"+kList[i].firstChild.nodeValue;
        if(fList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Father: </i>"+fList[i].firstChild.nodeValue;
        if(mList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Mother: </i>"+mList[i].firstChild.nodeValue;
        if(oList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Occupation: </i>"+oList[i].firstChild.nodeValue;
        if(hList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Residence: </i>"+hList[i].firstChild.nodeValue;
        if(lList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Religion: </i>"+lList[i].firstChild.nodeValue;
        if(wList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Marital status: </i>"+wList[i].firstChild.nodeValue;
        if(wdList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Marriage date: </i>"+wdList[i].firstChild.nodeValue;
        if(wnList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Wife: </i>"+wnList[i].firstChild.nodeValue;
        if(eList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Enlistment date: </i>"+eList[i].firstChild.nodeValue;
        if(nList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Service number: </i>"+nList[i].firstChild.nodeValue;
        strArray [count] +="<br /><i>Rank: </i>"+rList[i].firstChild.nodeValue;
        strArray [count] +="<br /><i>Unit: </i>"+uList[i].firstChild.nodeValue;
        if(qList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Embarkation date: </i>"+qList[i].firstChild.nodeValue;
        if(tList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Embarkation port: </i>"+tList[i].firstChild.nodeValue;
        if(vList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Embarkation ship: </i>"+vList[i].firstChild.nodeValue;
        strArray [count] +="<br /><i>Death date: </i><b>"+dList[i].firstChild.nodeValue+"</b>";
        strArray [count] +="<br /><i>Death place: </i>"+pList[i].firstChild.nodeValue;
        strArray [count] +="<br /><i>Cause of death: </i>"+cList[i].firstChild.nodeValue;
        strArray [count] +="<br /><i>Age at death: </i>"+aList[i].firstChild.nodeValue;
        if(bList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Burial or memorial: </i>"+bList[i].firstChild.nodeValue;
        if(xList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>BDM death registration date: </i>"+xList[i].firstChild.nodeValue;
        if(yList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>BDM death registration district: </i>"+yList[i].firstChild.nodeValue;
        else strArray [count] +="<br /><i>BDM death registration district: </i>Death not registered SA"
        if(iList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>BDM death registration informant: </i>"+iList[i].firstChild.nodeValue;
        if(nzList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Notes: </i>"+nzList[i].firstChild.nodeValue;
        strArray [count] +="</p>";
        count++;
      }
      i++;
    }
  }
  else{
    var i=0;
    while (i<sList.length){
      if(kList[i].firstChild.nodeValue != '-')strName=kList[i].firstChild.nodeValue.toUpperCase();
      else strName="";
      if(hList[i].firstChild.nodeValue != '-')strName2=hList[i].firstChild.nodeValue.toUpperCase();
      else strName2="";
      if((strName && (m=strName.indexOf(strSearchCont0)>=0) && strName.indexOf(strSearchCont1, m+lenCont0-1)>=0) || (strName2 && (n=strName2.indexOf(strSearchCont0)>=0) && strName2.indexOf(strSearchCont1, n+lenCont0-1)>=0)){
        strArray [count]="<p><i>Surname: </i><b>"+sList[i].firstChild.nodeValue;
        strArray [count] +="</b><br /><i>Given name(s): </i><b>"+gList[i].firstChild.nodeValue+"</b>";
        if(jList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Birth date: </i>"+jList[i].firstChild.nodeValue;
        strArray [count] +="<br /><i>Birth place: </i>"+kList[i].firstChild.nodeValue;
        if(fList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Father: </i>"+fList[i].firstChild.nodeValue;
        if(mList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Mother: </i>"+mList[i].firstChild.nodeValue;
        if(oList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Occupation: </i>"+oList[i].firstChild.nodeValue;
        if(hList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Residence: </i>"+hList[i].firstChild.nodeValue;
        if(lList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Religion: </i>"+lList[i].firstChild.nodeValue;
        if(wList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Marital status: </i>"+wList[i].firstChild.nodeValue;
        if(wdList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Marriage date: </i>"+wdList[i].firstChild.nodeValue;
        if(wnList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Wife: </i>"+wnList[i].firstChild.nodeValue;
        if(eList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Enlistment date: </i>"+eList[i].firstChild.nodeValue;
        if(nList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Service number: </i>"+nList[i].firstChild.nodeValue;
        strArray [count] +="<br /><i>Rank: </i>"+rList[i].firstChild.nodeValue;
        strArray [count] +="<br /><i>Unit: </i>"+uList[i].firstChild.nodeValue;
        if(qList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Embarkation date: </i>"+qList[i].firstChild.nodeValue;
        if(tList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Embarkation port: </i>"+tList[i].firstChild.nodeValue;
        if(vList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Embarkation ship: </i>"+vList[i].firstChild.nodeValue;
        strArray [count] +="<br /><i>Death date: </i><b>"+dList[i].firstChild.nodeValue+"</b>";
        strArray [count] +="<br /><i>Death place: </i>"+pList[i].firstChild.nodeValue;
        strArray [count] +="<br /><i>Cause of death: </i>"+cList[i].firstChild.nodeValue;
        strArray [count] +="<br /><i>Age at death: </i>"+aList[i].firstChild.nodeValue;
        if(bList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Burial or memorial: </i>"+bList[i].firstChild.nodeValue;
        if(xList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>BDM death registration date: </i>"+xList[i].firstChild.nodeValue;
        if(yList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>BDM death registration district: </i>"+yList[i].firstChild.nodeValue;
        else strArray [count] +="<br /><i>BDM death registration district: </i>Death not registered SA"
        if(iList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>BDM death registration informant: </i>"+iList[i].firstChild.nodeValue;
        if(nzList[i].firstChild.nodeValue != '-')strArray [count] +="<br /><i>Notes: </i>"+nzList[i].firstChild.nodeValue;
        strArray [count] +="</p>";
        count++;
      }
      i++
    }
  }
  if(count){
    strArray.sort();
    if(count > limit)toDisplay=limit;
    else toDisplay=count;
    strPrint='<h3 class="results">Search results <span style="font-size:87%; font-weight:normal;">(total of '+toDisplay+')</span></h3>';
    for(var i=0; i<toDisplay; i++) strPrint +=strArray [i];
    strPrint +='</p><h4 class="results">Abbreviations used in the results and other notes</h4><p>DC = Death Certificate<br/>ROH = <a href="http://www.awm.gov.au/database/roh.asp">Australian War Memorial Roll of Honour</a><br/>Embark Roll = <a href="http://www.awm.gov.au/database/awm8/">Australian War Memorial Embarkation Roll</a><br/>CWGC = <a href="http://www.cwgc.org/">Commonwealth War Graves Commission</a> which has details of the location and history of the cemeteries.<br/>NA = <a href="http://www.naa.gov.au/">National Archives of Australia</a> which has detailed service records</p>';
    d.getElementById('strResult').innerHTML=strPrint;
    d.getElementById('htmlProgress').innerHTML='View the results below...';
  }
  else{
    d.getElementById('strResult').innerHTML="<h4 class='results'>Search results <span style='font-size:93%; font-weight:normal;'>(total of 0)</span></h4><p></p>";
    d.getElementById('htmlProgress').innerHTML='There are no results to view.';
  }
  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)2006-2011 B Leadbeater, Australia
