	/**
	 * 
	 * Javascript Module_Autocomplete functions
	 * 
	 * @name Module_Autocomplete
	 * @author Vincent Cantin Bellemre
	 * @since 2007-11-11
	 * @version 1.0.0
	 * @package cbwebdevframework
	 * 
	 */
	 
	var KEY_ASCII_LEFT 	= 37;
	var KEY_ASCII_RIGHT = 39;
	var KEY_ASCII_UP 	= 38;
	var KEY_ASCII_DOWN 	= 40;
	var KEY_ASCII_ENTER = 13;
	var KEY_ASCII_ESC 	= 27;
	var AUTOCOMPLETE_A 	= null;
	var CURRENT_CURSOR 	= 1;
	
	function Autocomplete()
	{		
		this.update_cursor = function(current_link)
		{
			var current_div = current_link.parentNode;
			links = current_div.getElementsByTagName('a');
			
			for(i = 0 ; i < links.length; i++) 
			{
				
				if(links[i].innerHTML == current_link.innerHTML)
				{
					CURRENT_CURSOR 		= i;
					links[i].className = 'acItem';
				}
				else
				{
					links[i].className = 'acItem';
				}
			}
		
			current_link.className = 'acItemOver';
		}
		
		this.move = function(current_input,is_up)
		{
			var current_div = current_input.parentNode;
			var array_a_temp = current_div.getElementsByTagName('a');	
			var array_a = Array();
			var nb = 0;		
			
			for(var i = 0; i < array_a_temp.length; i++)
			{
				if(typeof(array_a_temp[i].getAttribute('autocomplete_link')) == 'string')
				{					
					array_a.push(array_a_temp[i]);	
				}
			}

			max_cursor = array_a.length;
			min_cursor = 0;			
			
			if(! is_up)
			{
				if(CURRENT_CURSOR == null)
				{
					CURRENT_CURSOR = 0;
				}
				else
				{
					if((CURRENT_CURSOR + 1) >= max_cursor )
					{
						CURRENT_CURSOR = min_cursor;	
					}
					else
					{
						CURRENT_CURSOR++;
					}
				}
			}
			else
			{
				if(CURRENT_CURSOR == null)
				{
					CURRENT_CURSOR = max_cursor - 1;
				}
				else
				{
					if(CURRENT_CURSOR <= min_cursor )
					{
						CURRENT_CURSOR = max_cursor - 1;	
					}
					else
					{
						CURRENT_CURSOR--;
					}
				}
			}
			
			for( var i = min_cursor; i < max_cursor; i++)
			{
				if(CURRENT_CURSOR == i)
				{
					array_a[i].className = 'acItemOver';	
					AUTOCOMPLETE_A = array_a[i];
				}
				else
				{
					array_a[i].className = 'acItem';	
				}
			}			
		}
		
		this.add = function(input,e)
		{
			if(window.event)
			{
				keyAscii = e.keyCode;	
			}
			else if(e.which)
			{
				keyAscii = e.which;
			}
			
			if (keyAscii == KEY_ASCII_LEFT || keyAscii == KEY_ASCII_RIGHT)
			{
				return false;
			}
			
			if (keyAscii == KEY_ASCII_DOWN)
			{
				this.move(input,false);
				return true;
			}
			
			if (keyAscii == KEY_ASCII_UP)
			{
				this.move(input,true);
				return true;
			}
			
			if (keyAscii == KEY_ASCII_ENTER)
			{
				this.autocomplete_enter(input);
				return true;
			}
			
			if (keyAscii == KEY_ASCII_ESC)
			{
				//AutoCompleteHide(acField.name);
				return true;
			}
	
			if(typeof(input.value) == 'string' )
			{
				var action 		= input.getAttribute('autocomplete_action');
				var filename 	= input.getAttribute('autocomplete_filename');
				var output		= input.getAttribute('autocomplete_output');
				this.current_input = input;				
				
				complete_filename = Request.get('basepath') + '/' + CURRENT_LANG + '/' + Request.get('module') + '/' + action;
				
				var req = new AJAX.Request(complete_filename,
				{
					method: 'POST',
					json : true,
					parameters: 
					[
					 'value=' 	+ escape(input.value),
					 'name=' 	+ escape(input.name),
 					 'output=' 	+ escape(output),
					 'form=' 	+ escape(input.form.name),
 					 'id=' 		+ escape(input.id),
					 ],
					
					onComplete : function(response) {Autocomplete.eval_response(response.responseJSON)} 
				}			
				);
			}
			else
			{
				trace(input.value);
			}
		}
		
		this.eval_response = function(response)
		{
			Autocomplete.fill_autocomplete(response.values);	
		}
				
		this.fill_autocomplete = function (autocomplete_values)
		{
			element 		= this.current_input;
			CURRENT_CURSOR 	= null;
			
			if(typeof(element['id']) != 'undefined')
			{
				current_input = $(element['id']);
			}
			else
			{
				current_input = eval('document.' + element['form'] + '.' +  element['name']); 
			}
			
			if(current_input.value == "")
			{
				autocomplete_values = Array();
			}
			
			test_div = current_input.parentNode.parentNode.getElementsByTagName('div');
			found = false;

			
			for(var i = 0 ; i< test_div.length; i++)
			{
				if(test_div[i].className == 'acPopup')
				{
					current_div = test_div[i];
					found = true;
					current_div.innerHTML = "";
					break;
				}
			}

			if(!found)
			{
				var current_div = document.createElement('div');
				current_div.className = 'acPopup';
				current_input.parentNode.parentNode.appendChild(current_div);
			}
			
			var autocomplete_mode = false;
			
			for(var i in autocomplete_values)
			{ 
				if
				(
					typeof(autocomplete_values[i]['onclick']) == 'undefined' || 
					autocomplete_values[i]['onclick'] == null
				)
				{
					var onclick_event = 'javascript:Autocomplete.fill_field(this)';
				}
				else
				{
					var onclick_event = autocomplete_values[i]['onclick'];
				}

				if
				(
					typeof(autocomplete_values[i].updated_label) == 'undefined' || 
					autocomplete_values[i].updated_label == null
				)
				{
					var updated_title = autocomplete_values[i].label
				}
				else
				{				
					var updated_title = autocomplete_values[i].updated_label;
				}

				if(updated_title != '' && typeof(updated_title) != 'undefined')
				{					
					updated_title = encode64(updated_title);
					autocomplete_mode = true;
					
					current_div.innerHTML += 
					'<a href="javascript:;" autocomplete_link="autocomplete_link" updated_title="' + updated_title + '" autocomplete_id=' + autocomplete_values[i]['value'] + ' class="acItem" ' +
					'onmouseover="Autocomplete.update_cursor(this)" ' +
					'onpress_event="' + onclick_event + '" '+
					'onclick="' + onclick_event + '" >'
					+ autocomplete_values[i]['label']
					+ '</a>';		 
				}
			}
						
			if(autocomplete_values && autocomplete_mode)
			{
				current_input.parentNode.insertBefore(current_div,current_input);
			}
			else
			{				
				Autocomplete.remove(current_input.parentNode);
			}
			
			if(current_div.innerHTML == '')
			{
				current_div.style.display = 'none';
			}
			else
			{
				current_div.style.display = '';
			}
		}		
		
		this.blur_remove = function(current_td)
		{
			CURRENT_TD = current_td 
			setTimeout('Autocomplete.remove()',500);
		}
		
		this.remove = function(current_td)
		{
			if(current_td == null)
			{
				current_td = CURRENT_TD;
			}
			
			var current_divs = current_td.getElementsByTagName('div');

			if(current_divs.length > 0)
			{
				for(var i in current_divs)
				{
					if(typeof(current_divs[i]) != 'undefined'  && current_divs[i].className == 'acPopup')
					{
						current_divs[i].parentNode.removeChild(current_divs[i])
						break;
					}
				}
			}
		}
		
		this.autocomplete_enter = function(current_input)
		{		
			CURRENT_INPUT = current_input;
			
			if(typeof(AUTOCOMPLETE_A) == 'object' && AUTOCOMPLETE_A != null )
			{ 
				var autocomplete_action 	= AUTOCOMPLETE_A.getAttribute('onpress_event');
				var reg_this 				= new RegExp("this", "g");
				var autocomplete_action		= autocomplete_action.replace(reg_this,'AUTOCOMPLETE_A') ;				
				eval(autocomplete_action);
			}
			else
			{
				Autocomplete.submit_action(current_input);
			}
		}
		
		this.submit_action = function(current_input) 
		{
			try
			{
				var onsubmit_action = current_input.form.getAttribute('onsubmit_action')							
				eval(onsubmit_action);
			}
			catch(e)
			{
				trace('No autocomplete submit_action');
			}
		}
		
		this.fill_field = function(current_a)
		{			
			try
			{
				var current_td 	= current_a.parentNode.parentNode;
				var inputs 		= current_td.getElementsByTagName('input');			
			}
			catch(e)
			{
				Autocomplete.submit_action(CURRENT_INPUT);
				return false;	
			}
			
			if(typeof(inputs[0]) != 'undefined')
			{
				var current_intput = inputs[0];
			}
			else
			{
				Autocomplete.submit_action(CURRENT_INPUT);
				return false;
			}

			current_input.value = decode64(current_a.getAttribute('updated_title'));
			current_input.setAttribute('autocomplete_id',current_a.getAttribute('autocomplete_id'));
			eval(current_input.getAttribute('onchange_function'));
			hidden_field 				= current_input.getAttribute('autocomplete_hidden_field');
			
			if(typeof(hidden_field) != 'undefined' && hidden_field != null && hidden_field.length > 0)
			{
				eval('hidden_input = current_input.form.' + hidden_field + ';');
				
				if(typeof(hidden_input) == 'undefined')
				{				
					var hidden_autocomplete 	= document.createElement('input');
					hidden_autocomplete.type 	= 'hidden';
					hidden_autocomplete.value 	= current_a.getAttribute('autocomplete_id');
					hidden_autocomplete.name 	= hidden_field;
					hidden_autocomplete.id 		= hidden_field;
					current_input.form.appendChild(hidden_autocomplete);
				}
				else
				{
					hidden_input.value = current_a.getAttribute('autocomplete_id');
				}			
			}

			this.remove(current_input.parentNode)
		}
		
		this.onchange = function (input,to_eval)
		{
			this.input_onchange = input;
			
			try
			{
				setTimeout(to_eval,20);
			}
			catch(e)
			{
				trace(e);	
			}
		}
	}
	
	Autocomplete = new Autocomplete();

