///*************************************************************************************************
//										AJAXComboBox Functions
//*************************************************************************************************/
var tTimer = null;
var TIMOUT = 500;
function AJAXObject(ajaxPage, keys, values)
{
	this.ajaxPage = ajaxPage;
	this.keys = keys;
	this.values = values.replace('&', '%26');

	var _Response = null
	try
	{
		_Response = QueryAJAX(this.ajaxPage, this.keys, this.values);
	}
	catch (e) {}

	this.GetXML = function()
	{
		return _Response;
	}
	this.GetResponse = function()
	{
		return _Response;
	}
	function InitializeAJAX()
	{
		var oRequest;
		try
		{
			// Firefox, Opera 8.0+, Safari
			oRequest = new XMLHttpRequest()
		}
		catch (e)
		{
			//Internet Explorer
			try
			{
				oRequest = new ActiveXObject( "Msxml2.XMLHTTP" );
			}
			catch (e)
			{
				try
				{
					oRequest = new ActiveXObject( "Microsoft.XMLHTTP" );
				}
				catch (e)
				{
					alert( "can't find xmlhttp" );
					throw e
				}
			}
		}
		return oRequest;
	}
	function QueryAJAX(ajaxPage, keys, values)
	{
		var sRequestString = "";
		
		if ( keys.split(',').length != values.split('~').length )
		{
			alert( "QueryAJAX - Keys = Values error" );
			return "";
		}
		
		for ( var i=0; i< keys.split(',').length; i++ )
		{
			sRequestString += "&" + keys.split(',')[i] + "=" + values.split('~')[i];
		}
		
		if ( sRequestString.length > 0 ) 
			sRequestString = sRequestString.substring( 1, sRequestString.length  );
		
		var oRequest = null 
		
		try
		{
			oRequest = InitializeAJAX(); 
		}
		catch(e) { throw e }

		if ( !oRequest )
		{
			return "";
		}
		
		var AJAXURL = ajaxPage + "?" + sRequestString;

		try
		{
			oRequest.open("GET", AJAXURL, false);
			oRequest.send(null);
		}
		catch (e)
		{
			alert("error with request to ajax");
			throw e
		}
		return oRequest.responseXML.documentElement;
	}
}

function AJAXComboListOnClick(textBoxID, holderID, listID, processMethod)
{
	var list = document.getElementById(listID);
	var listHolder = document.getElementById(holderID);
	var textBox = document.getElementById(textBoxID);
	var text = list.options[list.selectedIndex].text;
	var val  = list.options[list.selectedIndex].value;
	textBox.value = text;
	listHolder.style.display = "none";
	listHolder.style.zIndex = "-1";
	try
	{
		if ( processMethod != "" )
		{	
			eval( processMethod + "('" + val + "','" + textBoxID + "')" ); 
		}
	}
	catch (e) {}

	textBox.focus();
}

function AJAXComboOnListLoseFocus(holderID, listID)
{
	var list = document.getElementById(listID);
	var listHolder = document.getElementById(holderID);
	listHolder.style.display = "none";
	listHolder.style.zIndex = "-1";
	if ( list && list.options )
	{
		while ( list.options.length > 0 )
		{
			list.remove(0);
		}
	}
}

function AJAXComboListKeyUp(textBoxID, list, holderID, processMethod, evt)
{
	var listHolder = document.getElementById(holderID);
	var textBox = document.getElementById(textBoxID);
	var keyCode = (evt) ? evt.keyCode : (event) ? event.keyCode : 0;
	
	if ( keyCode == 13 )
	{					
		textBox.value = list.options[list.selectedIndex].text;
		textBox.focus();
		listHolder.style.display = "none";
		listHolder.style.zIndex = "-1";
		try
		{
			if ( processMethod != "" )
			{	
				eval( processMethod + "('" + list.options[list.selectedIndex].value + "','" + textBox.id + "')" ); 
			}
		}
		catch (e) {}

		textBox.focus();
	}
}

function AJAXComboListKeyDown(textBoxID, list, evt)
{
	var textBox = document.getElementById(textBoxID);
	var keyCode = (evt) ? evt.keyCode : (event) ? event.keyCode : 0;
	
	if ( ( keyCode == 38 && list.selectedIndex == 0 ) || ( keyCode == 8 ) )
	{
		textBox.focus();
	}
}

function AJAXTimerGetData( mainNode, textNode, valueNode, listID, holderID, id, getMethod )
{
	AJAXComboGetData( mainNode, textNode, valueNode, listID, holderID, id, getMethod );
}
function AJAXComboTextKeyUp( mainNode, textNode, valueNode, listID, holderID, textBox, getMethod, evt )
{
	var list = document.getElementById(listID);
	var listHolder = document.getElementById(holderID);
	var browser = GetBrowser();
	var keyCode = (evt) ? evt.keyCode : (event) ? event.keyCode : 0;
	if ( ( keyCode == 38 ) || ( ( /*( keyCode == 8 ) || */ ( keyCode == 33 ) || ( keyCode == 34 ) || ( keyCode == 35 ) || ( keyCode == 36 ) || ( keyCode == 37 ) || ( keyCode == 39 ) || ( keyCode == 45 ) || ( keyCode == 46 ) ) && ( textBox.value.length > 0 ) ) )
	{
		return;
	}
	else if ( keyCode == 40 && list.options.length > 0 )
	{
		list.focus();
	}
	else if ( ( keyCode == 40 ) && ( list.options.length == 0 ) && ( textBox.value == "" ) )
	{
		AJAXComboGetData( mainNode, textNode, valueNode, listID, holderID, textBox.id, getMethod );
	}
	else
	{
		if ( textBox.value.length > 0 )
		{
			if (tTimer) window.clearTimeout(tTimer);
			tTimer = window.setTimeout( "AJAXTimerGetData('" + mainNode + "', '" + textNode + "', '" + valueNode + "', '" + listID + "', '" + holderID + "', '" + textBox.id + "', '" + getMethod + "' )", TIMOUT );
		}
		else if ( listHolder )
		{
			listHolder.style.display = "none";
			listHolder.style.zIndex = "-1";
			if ( list && list.options )
			{
				while ( list.options.length > 0 )
				{
					list.remove(0);
				}
			}
		}
	}
}

function AJAXComboImageClick( mainNode, textNode, valueNode, listID, holderID, textBoxID, getMethod )
{
	document.getElementById(textBoxID).value = "";
	AJAXComboGetData( mainNode, textNode, valueNode, listID, holderID, textBoxID, getMethod );
}

function AJAXComboGetData( mainNode, textNode, valueNode, listID, holderID, textBoxID, getMethod )
{
	var list = document.getElementById(listID);
	var listHolder = document.getElementById(holderID);
	var textBox = document.getElementById(textBoxID);
	
	if (tTimer) 
	{
		window.clearTimeout(tTimer);
		tTimer = null;
	}

	if ( list && list.options )
	{
		while ( list.options.length > 0 )
		{
			list.remove(0);
		}
	}

	var xmlDoc = eval( getMethod + "('" + textBox.value + "')");

	if ( xmlDoc != null )
	{
		var oNodes = xmlDoc.getElementsByTagName(mainNode);

		if ( oNodes != null )
		{	
			var browser = GetBrowser();
			for ( var i = 0; i < oNodes.length; i++ )
			{
				var code = oNodes[i].getElementsByTagName(valueNode)[0].childNodes[0].nodeValue
				var desc = oNodes[i].getElementsByTagName(textNode)[0].childNodes[0].nodeValue

				if ( code != null && desc != null )
				{
					var oOption = document.createElement("option");
				
					oOption.value = code;
					oOption.text = desc;

					
					if ( browser == "MSIE" )
					{
						list.add( oOption, true );
					}
					else
					{
						list.add( oOption, null );
					}

				}
			}
			if ( list.options.length > 0 )
			{
				listHolder.style.zIndex = "0";
				listHolder.style.display = "block";
				list.focus();
			}
		}
	}
}

function GetBrowser()
{
	if ( navigator.appVersion.match( "MSIE" ) != null )
		return "MSIE";
	else if ( navigator.appVersion.match( "Safari" ) != null )
		return "Safari";
	
	return navigator.appName;
}
