var len;
var len2;
var lenR;
var wildcard;
var lenSrch;
var strSrch;
var strSearch;
var strShip;
var strDate;
var strPort;
var xmlhttp=null;
var htmlResults="";
var w=window;
var d=document;
function regDtaLoad(){
	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.history.length)w.history.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. JavaScript must be enabled.');
			if(w.history.length)w.history.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.history.length)w.history.back();
		return;
	}
	if(xmlhttp){
		xmlhttp.open('GET','tblrd.xml',true);
		xmlhttp.send(null);
		d.getElementById('strQuery').value="";
		d.getElementById('btnGo').disabled=false;
	}
	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.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.toLowerCase();
	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;
	lenR=len+1;
	if(len<3) {
		d.getElementById('strResult').innerHTML="<table border='0' cellspacing='0' style='position:relative;margin-top:0.8em;'><tr><th class='error'>Data Entry Error</th></tr><tr><td class='error'>Fill in the box with at least 3 characters of the name or abbreviation to be found.";
		d.getElementById('strQuery').focus();
		d.getElementById('btnGo').disabled=false;
		return;
	}
	else{
		count=0;
		xmlhttp.onreadystatechange=checkReadyState;
		if(xmlhttp.readyState==4 && xmlhttp.status=='200')runSearch();
	}
}
function checkReadyState() {
	if(xmlhttp.readyState==4 && xmlhttp.status=='200')runSearch();
	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 strArray=new Array();
	var regDoc=xmlhttp.responseXML;
	var nList=regDoc.getElementsByTagName('n');
	var abList=regDoc.getElementsByTagName('ab');
	var pnList=regDoc.getElementsByTagName('pn');
	var lList=regDoc.getElementsByTagName('l');
	var pList=regDoc.getElementsByTagName('p');
	var rList=regDoc.getElementsByTagName('r');
	var aList=regDoc.getElementsByTagName('a');
	var tList=regDoc.getElementsByTagName('t');
	var vList=regDoc.getElementsByTagName('v');
	var nzList=regDoc.getElementsByTagName('nz');
	var sList=regDoc.getElementsByTagName('s');
	var fList=regDoc.getElementsByTagName('f');
	var listLen=nList.length;
	var count=0;
	var toDisplay=0;
	for(var i=0;i<listLen;i++) {
		strName=nList[i].firstChild.nodeValue.toLowerCase();
		strName2=abList[i].firstChild.nodeValue.toLowerCase();
		strName3=pnList[i].firstChild.nodeValue.toLowerCase();
		var m=strName.indexOf(strSearchCont0);
		var n=strName3.indexOf(strSearchCont0);
		if((m>=0 && strName.indexOf(strSearchCont1,m+lenCont0-1)>=0) || strName2==strSearch || (n>=0 && strName3.indexOf(strSearchCont1,m+lenCont0-1)>=0)){
			strArray[count]="<i>Registration district: </i><b>"+nList[i].firstChild.nodeValue +"</b> ("+ abList[i].firstChild.nodeValue+")<br/>";
			if(lList[i].firstChild.nodeValue != '-')strArray[count] +="<i>District location: </i>"+lList[i].firstChild.nodeValue +"<br/>";
			if(sList[i].firstChild.nodeValue !='-')strArray[count] +="<i>District opened-closed: </i>"+sList[i].firstChild.nodeValue +"-"+fList[i].firstChild.nodeValue +"<br/>";
			if(pList[i].firstChild.nodeValue !='-'){
				strArray [count] +="<i>Part of district: </i>"+pList[i].firstChild.nodeValue +"<br/>";
				for(var j=0;j<listLen;j++){
					if(pList[i].firstChild.nodeValue==nList[j].firstChild.nodeValue) {
						if(rList[j].firstChild.nodeValue !='-')strArray[count] += "<i>Current repository: </i><b>"+rList[j].firstChild.nodeValue + "</b><br/><i>Address: </i>"+aList[j].firstChild.nodeValue + "<br/><i>Telephone: </i>"+tList[j].firstChild.nodeValue + "<br/>";
						if(vList[j].firstChild.nodeValue !='-')strArray[count] += "<i>Available: </i>"+vList[j].firstChild.nodeValue + "<br/>";
						break;
					}
				}
			}
			else{
				if(rList[i].firstChild.nodeValue !='-') strArray[count] += "<i>Current repository: </i><b>"+rList[i].firstChild.nodeValue +"</b><br/><i>Address: </i>"+aList[i].firstChild.nodeValue + "<br/><i>Telephone: </i>"+tList[i].firstChild.nodeValue + "<br/>";
				if(vList[i].firstChild.nodeValue !='-')strArray[count] += "<i>Available: </i>"+vList[i].firstChild.nodeValue + "<br/>";
			}
			if(nzList[i].firstChild.nodeValue !='-')strArray[count] += "<i>Note: </i>"+nzList[i].firstChild.nodeValue +"<br/>";
			strArray[count] +="<br/>";
			count++;
		}
	}
	if(count){
		d.getElementById('strResult').innerHTML='<h4 class="results" style="position:relative;margin-top:0.8em;">Search results <span style="font-size:13px; font-weight:normal;">(total of ' + count + ')</span></h4>';
		for(var i=0;i<count;i++)d.getElementById('strResult').innerHTML +=strArray[i];
		d.getElementById('htmlProgress').innerHTML='View the results below...';
	}
	else{
		d.getElementById('strResult').innerHTML="<h4 class='results' style='position:relative; margin-top:0.8em;'>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.<br/><br/>";
		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 clearForm() {
	d.getElementById('htmlProgress').innerHTML="";
	d.getElementById('strResult').innerHTML="";
}//Copyright(c)2003-2009 FamilyHistorySA.info, Australia