var httpBase = "http://www.allen-york.com/";

function appendOptionLast(obj,name,id)
{
 	var elOptNew = document.createElement('option');
 	elOptNew.text = name;
  	elOptNew.value = id;
			
  	try {
    	obj.add(elOptNew, null); // standards compliant; doesn't work in IE
  	}
  	catch(ex) {
    	obj.add(elOptNew); // IE only
  	}
}

function displayXML(response,target) {
	var customers = response.getElementsByTagName('item');
	var obj = document.getElementById("int_"+target);

	//alert("Target : int_" + target);
				
	obj.options.length = 0;
	if ( target == "currency" ) {
		appendOptionLast(obj,"Choose currency...",-1)
	} else {
		appendOptionLast(obj,"Every " + target,-1);
	}

	for(var i=0; i<customers.length; i++) {
		var itemName = customers[i].getElementsByTagName('name')[0].childNodes[0].nodeValue;
		var itemID = customers[i].getElementsByTagName('id')[0].childNodes[0].nodeValue;
		// alert("Name = " + itemName + " ID = " + itemID);
		appendOptionLast(obj,itemName,itemID);
	}
}

function ajaxLink(url) {
				
	//alert("ajaxlink : " + url);
	var httpRequest;
				
	url = httpBase + url;
					
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
   		httpRequest = new XMLHttpRequest();
   		if (httpRequest.overrideMimeType) {
			httpRequest.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
   		httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}
	httpRequest.open ('GET', url, true);

	httpRequest.onreadystatechange = function() {
		if (httpRequest.readyState == 4) {
			if ( httpRequest.status == 200 ) {
				var xmlDoc;
				if(window.ActiveXObject){ // If IE Windows
					xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
					xmlDoc.loadXML(httpRequest.responseText);
				} else {
					xmlDoc = httpRequest.responseXML;
				}							
			    var dataTarget = xmlDoc.getElementsByTagName('list')[0].getAttribute("target");
						    
		    	displayXML(httpRequest.responseXML,dataTarget);
	
			} else {
				//alert("Error : " + httpRequest.status);
			}
					    
		} else {
		    // document.getElementById("infoPane").innerHTML = "Loading...";				    
		}
	};
				
	httpRequest.send (null);

}

function clearDropdown(obj,target) {
	obj.options.length = 0;
	appendOptionLast(obj,"Every " + target,-1);
}
			
function populateDropdown(datasource,keyname,keyid) {
			
	var obj = document.getElementById(keyid);
	var id = obj.options[obj.selectedIndex].value;		

	if ( datasource == "area") {
		clearDropdown(document.getElementById("int_country"),"country");
		clearDropdown(document.getElementById("int_region"),"region");
		clearDropdown(document.getElementById("int_county"),"county");
		clearDropdown(document.getElementById("int_town"),"town");
	}
		
	// Need to special case a selection of 'UK'
	if ( datasource == "country") {
		if ( id == 2 ) {
			document.getElementById("uksearch").style.display="block";
			document.getElementById("townsearch").style.display="none";
		} else {
			document.getElementById("uksearch").style.display="none";
			document.getElementById("townsearch").style.display="block";
		}
		clearDropdown(document.getElementById("int_region"),"region");
		clearDropdown(document.getElementById("int_county"),"county");
		clearDropdown(document.getElementById("int_town"),"town");
	}
								
	if ( datasource == "region") {
		clearDropdown(document.getElementById("int_county"),"county");
		clearDropdown(document.getElementById("int_town"),"town");
	}

	if ( datasource == "county") {
		clearDropdown(document.getElementById("int_town"),"town");
	}

	url = "AJAXServer?module=search&view=" + datasource + "&keycol=" + keyname + "&keyid=" + id;
	ajaxLink(url);
}


