function AjaxCallbacks()
{
	this.dynamics = new Array;

	this.FillCombo = function (action, doc, selectbox, selectedValue, pattern)
	{		
		if (selectbox)
		{		
			if (typeof(selectbox)=="string") selectbox = document.getElementById(selectbox);				

			for(var i=selectbox.childNodes.length-1;i>-1;i--)
			{
				selectbox.removeChild(selectbox.childNodes[i]);
			}			

			if (!pattern) pattern = "//response[@action='"+action+"']/item";
			var nodelist = doc.selectNodes(pattern);			
			var node = nodelist.Next();
			while (node)
			{							
				var opt = document.createElement("option");			
				opt.innerHTML = node.text;			
				opt.value = node.getAttribute("value");
				if (selectedValue) 
				{
					if (selectedValue==opt.value) opt.selected = true;
				}
				else
				{
					if (node.getAttribute("selected") == "true") opt.selected = true;
				}
				selectbox.appendChild(opt);				
				node = nodelist.Next();
			}
			//selectbox.onclick = null;
		}
	}
	
	this.DynamicContent = function (action, doc, ident, invoker, reciver, pattern)
	{			
		var src = document.getElementById(invoker);	
		var rec = document.getElementById(reciver);	
		var storage = Util.AjaxCallbacks.dynamics[ident];
		if (!storage) 
		{
			Util.AjaxCallbacks.dynamics[ident] = new Object();
			storage = Util.AjaxCallbacks.dynamics[ident];
			storage.doc = doc;
			storage.ident = ident;
			storage.invoker = src;			
			storage.reciver = rec;			
			storage.pattern = pattern;				
			rec.lastSelectedIndex = -1;
			rec.onkeyup = function(e) 
			{
				if(!e) e=window.event; 
				src = document.getElementById(invoker);
				if(e.keyCode==13 && this.selectedIndex>-1) 
				{
					src.value=this.options[this.selectedIndex].text; 
					this.style.display='none';
					src.focus();
				}
				if (e.keyCode==38)				
				{				
					if (this.selectedIndex==0 && this.lastSelectedIndex==0)	
					{						
						this.style.display='none';					
						src.focus();
					}
					else if (this.selectedIndex==0 && this.lastSelectedIndex!=0)
					{
						this.lastSelectedIndex = 0;
					}
					else this.lastSelectedIndex = this.selectedIndex;
				}
			}
		}		
		if (src)
		{
			tsCompat.addEvent(src,"onkeyup",function (e) {Util.AjaxCallbacks.DynamicLoad(ident, e)});			
		}
	}
	
	this.DynamicLoad = function (ident, e)
	{
		var storage = Util.AjaxCallbacks.dynamics[ident];
		if (e.keyCode!=40)
		{					
			var filter = storage.invoker.value;
			var pattern = storage.pattern;
			var s = pattern.replace(/\{0\}/gi, "'"+filter+"'");		
			document.title = s;			
			for(var i=storage.reciver.childNodes.length-1;i>-1;i--)
			{
				storage.reciver.removeChild(storage.reciver.childNodes[i]);
			}			
			this.FillCombo(ident, storage.doc, storage.reciver, '', s);
			if (storage.reciver.style.display=="none") storage.reciver.style.display = "block";
		}
		else
		{
			if (storage.reciver.style.display=="none") storage.reciver.style.display = "block";
			storage.reciver.focus();
			storage.reciver.selectedIndex = 0;
		}		
		
		//alert(storage.doc.root.collection.length);
	}
}

function AjaxHandlers()
{
	this.LoadComboboxContent = function(action_handler_name, additional_parameters, reciever, currently_selectedvalue)
	{
		Util.AjaxLib.ASyncGET(action_handler_name, additional_parameters, Util.AjaxCallbacks.FillCombo, reciever, currently_selectedvalue);
	}
	
	this.CreateDynamicList = function(action_handler_name, additional_parameters, ident, invoker, reciver, pattern)
	{
		Util.AjaxLib.ASyncGET(action_handler_name, additional_parameters, Util.AjaxCallbacks.DynamicContent, ident, invoker, reciver, pattern);
	}

	this.GetString = function(action_handler_name, additional_parameters)
	{
		return Util.AjaxLib.SyncGET(action_handler_name, additional_parameters, null);
	}
}

if (typeof(Util)=="undefined") Util = new Object();
Util.AjaxCallbacks = new AjaxCallbacks();
Util.AjaxHandlers = new AjaxHandlers();

