/*
	-----------------------------------------
	| RSGBIOTA.org AJAX functions.		|
	| Note for use: no content on the site  |
	| may be browser-dependent.  If 	|
	| implementing AJAX, ensure that content|
	| remains accessible without JavaScript!|
	-----------------------------------------

	-----------------------------------------
	(c) Radio Society of Great Britain, 2005
	Coding: Dominic Smith M0BLF - Nov-05
	Enquiries: m0blf@domsmith.co.uk
	-----------------------------------------

*/

var req;

function displayResult(text)
{
// Sets the value of the reusult area
statuszone = document.getElementById('ajaxResultDiv');
if (text=='')
	{
	statuszone.innerHTML = '';
	}
else
	{	
	statuszone.innerHTML = statuszone.innerHTML + '<br />' + text;
	}
}


function loadXMLDoc(url) 
{
	// branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) 
	{
	//displayResult('XMLHttpRequest');
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
	resultzone = document.getElementById('ajaxResults');
	resultzone.style.display = 'block';
	submitButt = document.getElementById('submitButtSpan');
	submitButt.style.display = 'none';
	statuszone = document.getElementById('ajaxResultDiv');
	}
    // branch for IE/Windows ActiveX version
     else if (window.ActiveXObject) 
	{
        req = new ActiveXObject("Microsoft.XMLHTTP");
	//displayResult('XMLHTTP');
        if (req) 
		{
            	req.onreadystatechange = processReqChange;
           	req.open("GET", url, true);
            	req.send();
		resultzone = document.getElementById('ajaxResults');
		resultzone.style.display = 'block';
		submitButt = document.getElementById('submitButtSpan');
		submitButt.style.display = 'none';
		statuszone = document.getElementById('ajaxResultDiv');
		}
    	}

}



function checkIsName(input, response)
	{
	//displayResult('checkref');
 	if (response != '')
		{ 
  		//displayResult('Checked ' + response);
    		// Response mode
    		if (isArray(response))
			{
			arraylength = response.length;
			displayResult('');
 			displayResult('The following results were returned:');
			var resultTable = '<br /><table><thead><tr><th>Island Name</th><th>Group Name</th><th>Ref. Num.</th></tr></thead><tbody>';
			for ($i=0; $i<arraylength; $i++)
				{
				isname = result[$i][0];
				grpname = result[$i][1];
				refno = result[$i][2];
			resultTable += '<tr><td>' + isname + '</td><td>' + grpname + '</td><td><a href=\"http://www.rsgbiota.org/info/groupinfo.php?refno=' + refno + '\">' + refno + '</a></td></tr>';
				}
			resultTable += '</tbody></table>';
			displayResult(resultTable);
			}
		else
			{
 			displayResult('I\'m sorry, but your search returned no results.   <a href=\'../help/island_name_search.php\'>Help</a>');
    			} 
  		}
	else
		{
  		//displayResult('Requesting ' + input + ' ');
    		// Input mode
    		url = 'http://www.rsgbiota.org/functions/ajaxResults.php?q=' + input;
   		//displayResult('Loading ' + url);
    		loadXMLDoc(url);
  		}
	}



function processReqChange() 
	{
    	// only if req shows "complete"
    	if (req.readyState == 4) 
		{
        	// only if "OK"
        	if (req.status == 200) 
			{
   			//displayResult("Received");
            		// ...processing statements go here...
      			response  = req.responseXML.documentElement;
     			method    = response.getElementsByTagName('method')[0].firstChild.data;
   			//displayResult(method);
			result    = response.getElementsByTagName('result')[0];
			resultnum = result.getElementsByTagName('thisresult').length;
			if (resultnum == 0)
				{
				displayResult('');
				displayResult('Sorry, your search did not return any results.   <a href=\'../help/island_name_search.php\'>Help</a>');
				}
			else
				{
				resultArray = new Array();

				for ($i=0; $i<(resultnum); $i++)
					{
				   	thisresult    = result.getElementsByTagName('thisresult')[$i];
					isname    = thisresult.getElementsByTagName('isname')[0].firstChild.data;
		   			grpname    = thisresult.getElementsByTagName('grpname')[0].firstChild.data;
			   		refno    = thisresult.getElementsByTagName('refno')[0].firstChild.data;
					thisresultarray = new Array(isname,grpname,refno);
					resultArray[$i] = thisresultarray;
					}
				result = resultArray;
				eval(method + '(\'\', result)');
        			}
			}
		else 
			{
		        alert("There was a problem retrieving the XML data:\n" + req.statusText);
        		}
    		}
	}



function monitorField(myField) 
	{
      	vCurrentRequiredField = myField;
      	vCheckInterval = setInterval('monitorFieldSniffer()', 100);
   	}

function unMonitorField(myField)
	{
	vCheckInterval = null;
	}

function monitorFieldSniffer()
	{
	currentSearch = vCurrentRequiredField.value;
	lastSearchField = document.getElementById('lastSearch');
	// We use the hidden field lastSearch in the form to store the last search variable (at the last timeout)
	lastSearch = lastSearchField.value;
	if (currentSearch.length > 1)
	{
		if (currentSearch == lastSearch)
			{
			// Do nothing
			}
		else
			{
			displayResult("Please wait while AJAX data loads... Searching for " + currentSearch);
			lastSearchField.value = currentSearch;
			checkIsName(currentSearch,'');
			}
		}
	}

function isArray() 
	{
	// Hack for Safari
	var agt=navigator.userAgent.toLowerCase();
	if (agt.indexOf("safari") != -1)
	{
		return true;
	}
	else
	{
		
	// Thanks to http://www.planetpdf.com/developer/article.asp?ContentID=6383
	if (typeof arguments[0] == 'object') 
		{
		var criterion = arguments[0].constructor.toString().match(/array/i); 
 		return (criterion != null);  
		}
	return false;
	}
	
	// Ends Safari hack
	}

