function LocateBanker(){
	var pcode = document.getElementById("postcode");
	var pcformat = /^[1-9]\d{3}$/;
	var proxyURL = 'http://www.achl.com.au/lib/lpbproxy.php?pcode=';
	// validate postcode
	if (pcformat.test(pcode.value)){
		// send rquest
		document.getElementById('writeroot').innerHTML = 'Search is in progress, please wait....';
		makeRequest(proxyURL + pcode.value);
	}else{
		// invalid postcode
		alert('The postcode you have entered is not valid, please try again.');
	}
}

function PCValidate(){
	var pcode = document.getElementById("postcode");
	var srch = document.getElementById("search");
	var pcformat = /^[1-9]\d{3}$/;
	// validate postcode
	if (pcformat.test(pcode.value)){
		// show search option
		srch.style.display = 'block';
	}else{
		// invalid postcode
		srch.style.display = 'none';
		clearResult();
	}
}

function clearResult(){
	// clear any previous result
	removeAllChildNodes(document.getElementById('writeroot'));
}

function makeRequest(url) {
        var httpRequest;

        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');
                // See note below about this line
            }
        } 
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch (e) {
                           try {
                                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                               } 
                             catch (e) {}
                          }
                                       }

        if (!httpRequest) {
            alert('Unable to create required components..');
            return false;
        }
        httpRequest.onreadystatechange = function() { ProcessResult(httpRequest); };
        httpRequest.open('GET', url, true);
        httpRequest.send(null);

}

function alertContents(httpRequest) {

        if (httpRequest.readyState == 4) {
            if (httpRequest.status == 200) {
                alert(httpRequest.responseText);
            } else {
                alert('There was a problem with the request.');
            }
        }

}

function ProcessResult(httpRequest) {
		if (httpRequest.readyState == 4) {
			// always clear result
			clearResult();
            if (httpRequest.status == 200) {
				// process bankers in response
				var bankers = httpRequest.responseXML.getElementsByTagName('banker');
                 //alert(httpRequest.responseText);
				if (bankers == null || bankers.length < 1){
					var x = document.createElement('div');
					x.id = 'results';
					x.className = 'banker';
					var y = document.createElement('p');
					y.innerHTML = 'Sorry, we were unable to locate any personal bankers in your area.<br/><br/><strong>Please contact ACHL directly on 1300 797 338</strong>';	
					x.appendChild(y);
					document.getElementById('writeroot').appendChild(x);
				}else{
					//process bankers	
					document.getElementById('writeroot').innerHTML = '<div style="padding-left:6px;">Your Results:<br/></div>';
					for (var i=0;i<bankers.length;i++)
					{
						var x = document.createElement('div');
						x.id = 'results';
						x.className = 'banker';
						var y = document.createElement('p');
						y.innerHTML = '<strong>' + getNodeValue(bankers[i],'AddrSuburb') + 
							'</strong><br />' + getNodeValue(bankers[i],'Contact') + '<br />' + ' Ph: ' + getNodeValue(bankers[i],'ContactNumber');
						//y.appendChild(document.createTextNode(getNodeValue(bankers[i],'AddrSuburb')));
						x.appendChild(y);
						//var z = document.createElement('p');
						//z.className = 'moreInfo';
						//z.innerHTML = getNodeValue(bankers[i],'Contact') + '<br />' + ' Ph: ' + getNodeValue(bankers[i],'ContactNumber');
						//z.appendChild(document.createTextNode(getNodeValue(bankers[i],'Contact') + ' Ph: ' + getNodeValue(bankers[i],'ContactNumber')));
						//x.appendChild(z);
						document.getElementById('writeroot').appendChild(x);
					}					
				}
            } else {
				// always clear result
				clearResult();
                alert('We are currently unable to perform your search, please try again soon.');
            }
        }
}

// catch enter key
function handleKeyPress(e,form){
	var key=e.keyCode || e.which;
	if (key==13){
		LocateBanker();
	}
}

// utility function
function getNodeValue(obj,tag)
{
	return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
}

function removeAllChildNodes(node) {
	if (node && node.hasChildNodes && node.removeChild) {
		while (node.hasChildNodes()) {
			node.removeChild(node.firstChild);
		}
	}
} // removeAllChildNodes()
