var count;
var len;
var len2;
var wildcard;
var lenSrch;
var lenSrch2;
var strSrch;
var strSrch2;
var strSearch;
var strSearch2;
var strSndSurname = "";
var xmlhttp = null;
var htmlResults = "";

function desertDtaLoad() {
	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 unsuitable for using this database. Suitable browsers include recent versions of Microsoft Internet Explorer, Mozilla Firefox	, Netscape and Safari.');
				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 unsuitable for using this database. Suitable browsers include recent versions of Microsoft Internet Explorer, Mozilla Firefox	, Netscape and Safari.');
			if (window.history.length) window.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 (window.history.length) window.history.back();
		return;
	}
	if (xmlhttp) {
		xmlhttp.open('GET', 'deserters.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 unsuitable for using this database. Suitable browsers include recent versions of Microsoft Internet Explorer, Mozilla Firefox	, Netscape and Safari.');
		if (window.history.length) window.history.back();
		return;
	}
	displayHelp();
}

function findQuery() {
	document.getElementById('btnGo').disabled = 'disabled';
	document.getElementById('htmlProgress').innerHTML='';
	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 surname to be found.<br/>";
		document.getElementById('strQuery').focus();
		return;
	}
	else {
		document.getElementById('htmlProgress').innerHTML = 'Downloading the index &amp; searching ...';
		if (document.getElementById('blnSound').checked) strSearch = calculateSound(strSearch);
		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 (window.history.length) window.history.back();
		return;
	}
}

function runSearch() {
	var strName = "";
	var strName2 = "";
	var strNameLower = "";
	var strPrint = "";
	var strArray = new Array();
	var dtrDoc = xmlhttp.responseXML;
	var sList = dtrDoc.getElementsByTagName('s');
	var gList = dtrDoc.getElementsByTagName('g');
	var oList = dtrDoc.getElementsByTagName('o');
	var aList = dtrDoc.getElementsByTagName('a');
	var nList = dtrDoc.getElementsByTagName('n');
	var rList = dtrDoc.getElementsByTagName('r');
	var fList = dtrDoc.getElementsByTagName('f');
	var dtrListLen = sList.length;
	var m;
	var i=0;
	count = 0;
	while (i < dtrListLen) {
		strName = sList[i].firstChild.nodeValue.toUpperCase();
		strName2 = gList[i].firstChild.nodeValue.toLowerCase();
		if (strName2 == '-') strName2 = "";
		if (document.getElementById('blnSound').checked) strName = calculateSound(strName);
		if ((((document.getElementById('blnWildcards').checked && (m = strName.indexOf(strSearchCont0) >= 0) && strName.indexOf(strSearchCont1, m+lenCont0-1) >= 0) || (document.getElementById('blnExact').checked && strName.substr(0, lenCont0) == strSearchCont0 && strName.substr(strName.length-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))))) {
			strArray [count] = sList[i].firstChild.nodeValue;
			if (strName2) strArray [count] += " " + gList[i].firstChild.nodeValue;
			if (oList[i].firstChild.nodeValue != '-') strArray [count] += ", " + oList[i].firstChild.nodeValue;
			if (aList[i].firstChild.nodeValue != '-') strArray [count] += ". " + aList[i].firstChild.nodeValue;
			if (nList[i].firstChild.nodeValue != '-') strArray [count] += ". " + nList[i].firstChild.nodeValue;
			strArray [count] += ". Source: " + rList[i].firstChild.nodeValue;
			if (fList[i].firstChild.nodeValue != '-') strArray [count] += ". " + fList[i].firstChild.nodeValue;
			strArray [count] += "<br/>";
			count++;
		}
		i++;
	}
	if (count) {
		strArray.sort();
		strPrint = '<h3 class="results">Search results <span style="font-size:13px; font-weight:normal;">(total of '+count+')</span></h3>';
		for (var i=0; i<count; i++) strPrint += strArray [i];
		strPrint += '<br /><h3 class="results">Abbreviations used in the results</h3>GG = South Australian Government Gazette<br/>PG = South Australian Police Gazette';
		document.getElementById('strResult').innerHTML = strPrint;
		document.getElementById('htmlProgress').innerHTML = 'View the results below...';
	}
	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++) {
		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' /*&& i != intStrLen-1*/) || 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'; // drop trailing g (or s?), or g when _gn
		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'; // strip leading g, k or p if followed by n
			else strSound +='5';
			if (i==0 && strString.charAt(1)=='C' && strString.charAt(0)=='M') strSound += '0'; // add a if Mc
		}
		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);
			} // omit h & strip g if preceded by vowel & followed by h
		}
		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'; // leading w (if not wr) or y (?group 1?)
			else if (strString.charAt(i)=='Y') strSound +='0'; // omit w or leading y
		}
		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'; // omit leading a', i' or o', trailing e
	}
	strModSound = strSound.charAt(0);
	for (i=1; i<strSound.length; i++) { // strip doubled up codes
		if (strSound.charAt(i)!=strSound.charAt(i-1)) strModSound += strSound.charAt(i);
	}
	return strModSound;
}

function clearForm() {
	document.getElementById('htmlProgress').innerHTML = "";
	document.getElementById('strResult').innerHTML = "";
}

function displayHelp() {
	if (count) htmlResults = "<br /><br />" + document.getElementById('strResult').innerHTML;
	else htmlResults = "";
	document.getElementById('strResult').innerHTML = "<h3 class='results'>Using the persons lost &amp; found database</h3><h4>Text entry</h4>At least 2 allowed characters must be entered into the surname box before a search can proceed. The given name is optional. If only given name initials are known, enter the first initial only or leave the given name box blank.<br />The search is not case sensitive.<br />The only characters allowed in the boxes are letters of the alphabet, hyphens, spaces, apostrophes and not more than one * wildcard. Any other entered characters are ignored. In particular, the <b>?</b> wildcard and the logical operators (and, or, not, &amp;, |, &lt;, >, +, -) cannot be used.<br /><br /><h4>Searching for a name</h4>The default (and generally most satisfactory) method of searching for a name is the wildcards method. Select [Wildcards] by clicking it, if necessary. Then enter into the surname or place box, sufficient letters to define the name to be found. Click the [ <b>Go</b> ] button to begin the search. If the name you wish to find is not in the database, take into account possible spelling variations, as the people recording the information were sometimes semiliterate.<br />The search behaves as though you have entered a * wildcard at each end of your search text. That is, the search engine looks for the exact text you have entered anywhere within the name. This allows you to take into account some likely mis-spellings or variations of the name of interest. If required, one * wildcard can be added within the text to replace zero or more unknown or doubtful characters.<br />The [<b>Clear</b>] button clears both boxes and places the cursor in the left hand box, ready for your next search.<br /><br /><h4>Searching for surnames using exact matching</h4>If you only wish to find names with exactly the same spelling as you have entered, select [Exact] by clicking it, if necessary. If required, one * wildcard can be added in any position to replace zero or more unknown or doubtful characters.<br />Exact matching of given names is not available. Any entered given name will be matched by the wildcards method.<br /><br /><h4>Searching for surnames using sound matching</h4>If the above techniques fail, as a last resort you may wish to try the sound method of matching. This will allow you to find more variations of spelling and also take into account the pronunciation of the surname. The method used here is a heavily modified version of Soundex code matching which doesn't require the 1st letter of the name to be known and takes into account the full name, not just the beginning of a long name. It takes account of such pronunciation variations as dropped h's and g's. And it also takes into account significant vowels, h and y which are all ignored by Soundex if not the name's 1st letter. You must be prepared to scan the results and reject those which are poor matches. For some shorter names, ones with mainly vowels, and names containing spaces, all the results may be unsatisfactory. Also, the search takes longer with this method because there is considerably more processing to be done.<br />To use sound matching, select [Sound] before clicking [ <b>Go</b> ]. You must enter the full surname required. Wildcards are not permitted.<br />Sound matching of given names is not available. Any entered given name will be matched by the wildcards method." + htmlResults;
	if(!document.getElementById('strQuery').disabled) document.getElementById('strQuery').focus();
}
// Copyright (c) 2006-2007 FamilyHistorySA.info, Australia. All rights reserved