// vim: set expandtab tabstop=4 shiftwidth=4:
// +------------------------------------------------------------------------+
// +------------------------------------------------------------------------+
// | Authors..: J. Lawson  john.lawson@scriobha.im                         	|
// | Date.....: 06.16.2006													|
// | Notes....: Retrieve XML based documents of key/value pairs and populate|
// | 			the specified selection form elelment.						|
// | 			Use this as an include and call loadXMLDoc() with required  |
// | 			parameters.													|
// | 			Usage:														|
// |			Between the <head></head tags place:						|
// |			<script type="text/javascript" src="scripts/AjaxLib.js" />	|
// |			Assuming that you are calling from an element of [name]Code,|
// |			call the main function:										|
// |			onClick="loadXMLDoc(this.form, '[name]Code',				|
// |				'[value appended to URL]', '[URL that returns XML]');"	|
// |			NOTE: The loadXMLDoc assumes you have a function available	|
// |			called match. This is a basic regex function if you want to |
// |			create your own.
// | 			Works in IE 5.5 and Mozilla 0.7/0.7.1						|
// +------------------------------------------------------------------------+

//-- AJAX population of Lead Owner Selection box ---//
function loadXMLDoc(form, name, value, url) 
{
	//-- process basic info
	formName = form;
	if(name.match('Code')){
		elementName = replace(name,'Code','');
	}
	else if(name.match('Display')){
		elementName = replace(name,'Display','');
	}


	//-- prep the URL string
	sendURL = url + value;
	req = false;
	//-- Make sure we have the final var and we are dealing with a valid request
	if(value){
		//--- branch for native XMLHttpRequest object (Firefox)
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
			req.onreadystatechange = processReqChangeXML;
			req.open("GET", sendURL, true);
			req.send(null);
		//--- branch for Windows ActiveX version (IE)
		} else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
			if (req) {
				req.onreadystatechange = processReqChangeXML;
				req.open("GET", sendURL, true);
				req.send();
			}
		}
	}
	//-- Null value passed, reset the form element
	else{
		eval("document."+formName.name+"."+elementName+"Select.options.length = 0");
		eval("document."+formName.name+"."+elementName+"Select.options[0]=new Option('Please change the previous item','')");
	}
}
//-- Process the data coming back --//
// This function is called on the event change of 
// Ready State Change from the previous function
function processReqChangeXML() 
{
	var code;
	var display;
	var response;
	optionArray = new Array();
	//-- Let the user know that something is happening
	eval("document."+formName.name+"."+elementName+"Select.options.length = 0");
	eval("document."+formName.name+"."+elementName+"Select.options[0]=new Option('Searching...', '')");
	
	//-- only if req shows "complete"
	if (req.readyState == 4) {
        //-- only if "OK" from web server
		if (req.status == 200) {
			response  = req.responseXML.documentElement;
			//-- Loop through and populate the array with the option data
			for(a=0; a<response.getElementsByTagName('display').length; a++){
				display   = response.getElementsByTagName('display')[a].firstChild.data;
				code    = response.getElementsByTagName('code')[a].firstChild.data;
				optionArray.push(new Option(display, code));
			}
			//-- If data is returned populate away
			if(display != 'no_results' && display){
				eval("document."+formName.name+"."+elementName+"Select.options.length = 0");
				for (var i=0; i < optionArray.length; i++) {
					eval("document."+formName.name+"."+elementName+"Select.options[i]=optionArray[i]");
				}

			}
			//-- No data was found, display a message to the user
			else{
				eval("document."+formName.name+"."+elementName+"Select.options.length = 0");
				eval("document."+formName.name+"."+elementName+"Select.options[i]=new Option('No Results Found','')");
			}
        }
        //-- Full error message, URL doesn't exist, bad parameters, catch all
		else {
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
	else{
		//-- Don't do anything.
	}
	//-- If the select element is empty then then populate with this
	if(eval("document."+formName.name+"."+elementName+"Select.options.length") <=0){
		eval("document."+formName.name+"."+elementName+"Select.options.length = 0");
		eval("document."+formName.name+"."+elementName+"Select.options[0]=new Option('No Results Found...', '')");
		eval("document."+formName.name+"."+elementName+"Select.options[1]=new Option('Try less infomation to search', '')");
	}
}
