/*

*******************************************************************
File: home_form.js
Date: 01/04/2005
Purpose: Provides routines for the suburb search panel on the Simple and Contents Calculators

REQUIRES these javascript files to also be included in the host page:
	- scripts/event.js
	- scripts/error.js
	- scripts/string.js
	- scripts/postcode.js
	- scripts/xml_services.js
	
HISTORY:
*******************************************************************

*/

function suburbSearchSubmit()
{
	document.forms["suburb-search"].submit();	
}

function suburbSearchResizeForSearch()
{
	document.getElementById('suburb-search').style.height = '64px';
	document.getElementById('container-search-results').style.display = 'none';
}

function suburbSearchResizeForResults()
{
	document.getElementById('suburb-search').style.height = '214px';
	document.getElementById('container-search-results').style.display = 'block';
}

function suburbSearchOnMouseOver()
{
	mbHE('hidden');	
}

function suburbSearchOnSubmit()
{
	mbHE('hidden');
	
	if (document.getElementById('suburb').value.trim() == '')
		suburbSearchResizeForSearch();
	else
	{
		// perform the search
		updateMatches(document.getElementById('suburb').value.trim());
		return false;	
	}
}

function showSuburbSearch()
{
	suburbSearchResizeForSearch();
	
	mbHE('hidden');
	
	if (document.getElementById('suburb').value.trim() != '')
	{
		suburbSearchOnSubmit();
	}
	document.getElementById('suburb-search').style.visibility = 'visible';
	document.getElementById('suburb').select();
}

function hideSuburbSearch()
{
	mbHE('visible');
	
	document.getElementById('suburb-search').style.visibility = 'hidden';
	document.getElementById('postcode').focus();
}

/* XMLHttpRequest Methods */


function processReqChange() 
{
	var i;
	var j;
	var strListHTML;
	var aLocalities;
	var intPostcode;
	var containerSearchResults;
	var strSuburb;
	
	strSuburb = document.getElementById('suburb').value.trim();
	
	var reReplace = new RegExp(strSuburb, "i")
	
	var strCSSClass = 'light';
	
    // only if req shows "complete"
    if (req.readyState == 4) 
	{
        // only if "OK"
		
		// first, remove all list items
		
		
		containerSearchResults = document.getElementById('container-search-results')
		containerSearchResults.innerHTML = '';
		//containerSearchResults.style.visibility = 'hidden';
		
        if (req.status == 200) 
		{
        	response = req.responseXML.documentElement;
			
			if (response.nodeName == 'localities')
			{	
				// locality request
				nodesLocalities = response.getElementsByTagName('locality');
				
				strListHTML = '<table id="search-results" cellspacing="0">';
				
				if (strSuburb.isNumeric())
				{
					// display postcode version of results header
					strListHTML += '<tr><td class="dark">Postcode</td><td class="light">Suburb / City</td><td class="dark"></td></tr>';
				}
				else
				{
					// display the locality version of the results header					
					strListHTML += '<tr><td class="dark">Suburb / City</td><td class="light">Postcode</td><td class="dark"></td></tr>';
				}
				
				
				
				for (i=0; i<nodesLocalities.length; i++)
				{
					nodeLocality = nodesLocalities[i];
					
					if (nodeLocality.nodeType == 1)
					{
						strCSSClass = strCSSClass == 'light' ? 'dark' : 'light';
						
						// this is an element
						if (strSuburb.isNumeric())
						{
							// display postcode version of results
							strListHTML += '<tr class="' + strCSSClass + '"><td class="postcode">' + nodeLocality.getAttribute("postcode").replace(reReplace, '<span style="color: red">' + document.getElementById('suburb').value.trim() + '</span>') + '</td><td class="name">' + nodeLocality.getAttribute("name") + ' (' + nodeLocality.getAttribute("state").toUpperCase().trim() + ')</td><td class="control"><a href="#" onclick="selectPostcode(' + nodeLocality.getAttribute("postcode") + ');"><img src="/gio/img/assets/3607/button_small_select.gif" alt="select"  /></a></td></tr>';
						
						}
						else
						{
							// display the locality version of the results
							
							strListHTML += '<tr class="' + strCSSClass + '"><td class="name">' + nodeLocality.getAttribute("name").replace(reReplace, '<span style="color: red">' + document.getElementById('suburb').value.trim().properCase() + '</span>') + ' (' + nodeLocality.getAttribute("state").toUpperCase().trim() + ')</td><td class="postcode">' + nodeLocality.getAttribute("postcode") + '</td><td class="control"><a href="#" onclick="selectPostcode(' + nodeLocality.getAttribute("postcode") + ');"><img src="/gio/img/assets/3607/button_small_select.gif" alt="select"  /></a></td></tr>';
						}
						
					}
				}	
				
				strListHTML += '</table>';
				
				containerSearchResults.innerHTML = strListHTML;
				
				if (nodesLocalities.length > 6)
				{
					suburbSearchResizeForResults();
					//resize the box to fit scrollbars
					containerSearchResults.style.width = '358px';
					
					//alert(containerSearchResults.style.width );
				}
				else if (nodesLocalities.length == 0)
				{
					suburbSearchResizeForSearch();
				}
				else
				{
					suburbSearchResizeForResults();
					// resize the box for no scrollbars (neatens the display);
					containerSearchResults.style.width = '341px';
				}
				
				
				//containerSearchResults.style.visibility = 'visible';
		
				containerSearchResults.hash = '';
			}
			
		} 
		else 
		{
            //alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}

function selectPostcode(intPostcode)
{
	document.forms["home-information"].elements["postcode"].value = intPostcode;
	hideSuburbSearch();
	document.forms["home-information"].elements["postcode"].focus();
}

function updateMatches(strLocality)
{
	var url;
	
	if (strLocality.length >= 2)
	{
		url = 'services/locality_lookup.asp?locality=' + strLocality;
		loadXMLDoc(url);
	}
}

function suburbSearchOnKeyUp(e)
{
	var evt = getEvent(e);
	
	if (evt.keyCode == 27) 
	{
  		hideSuburbSearch();
	}
	else
	{
		suburbSearchOnSubmit();
	}
}