	var over = new Array();
	over['camp'] = '';
	over['fieldText'] = '';
	over['selectie'] = '';
	over['idDB'] = '';
	var campCurr = '';
	var inputValue = new Array();	
	var campuri = new Array();
	var functie = new Array();
	var emboss = new Array();

	function disableEnterKey(e){
	     var key;
	
	     if(window.event)
	          key = window.event.keyCode;     //IE
	     else
	          key = e.which;     //firefox
	
	     if(key == 13)
	          return false;
	     else
	          return true;
	}
	
	function catchKey(e,camp){
	     var key;     
	     if(window.event){
	     		
	          key = window.event.keyCode; //IE
//	          debug (key);
	     }else{
	          key = e.which; //firefox
//	          debug(key);
	     }
	 
	    switch (key){ 
	    	case 13:
	     		selectFromList();
//	     		aaa(document.getElementById('id_'+camp).value,"id_"+camp);
	     		break;
	     	case 40:
	     		moveSelection(1);
	     		break;
	     	case 38:
	     		moveSelection(-1);
	     		break;
	     	default:
	     		checkRequest(camp);
	     }
	
	     return false;
	}
	
	function selectFromList(){
		setValue(over['camp'], over['fieldText'], over['idDB']);
		updateOver('', '');
	}

	function moveSelection(pas){
		index = over['selectie'];
		camp = document.getElementById('ul_'+ over['camp']);
//		debug('ul_'+ over['camp']);
		curr = camp.childNodes.length;
//		debug(camp.childNodes[index]);
		if(isNaN(index)){
			currVal = (pas>0?0:curr-1);
		}else{
			currVal = parseInt(index) + parseInt(pas);
		}
//		debug(currVal);
		if(currVal ==curr){
			index = currVal - parseInt(curr);
		}else
		if(currVal >=0){
			index = currVal;
		}else{
			index = currVal + parseInt(curr);
		}
		updateOver(over['camp'], index);
//		debug(over['selectie'] + '/' + camp.childNodes[over['selectie']].innerHTML);
	}

	function ul_blur(camp){
		if ( isNaN(over['selectie'])){
			clean('ul_' + camp)
		}
		over['camp'] = '';
		over['fieldText'] = '';
		over['selectie'] = '';
	}
	
	function updateOver(camp, index){
//		debug(index);
		ul = document.getElementById('ul_' + camp);
		if(ul && (over['selectie']>=0) && ul.childNodes[over['selectie']]){
//			debug(over['selectie']);
			ul.childNodes[over['selectie']].className = 'elemListaAutocomplete_default';
		}

		over['camp'] = camp;
		over['selectie'] = parseInt(index);
		over['fieldText'] = '';
		over['idDB'] = '';
		
		if(ul && (over['selectie']>=0) && ul.childNodes[over['selectie']]){
			ul.childNodes[over['selectie']].className = 'elemListaAutocomplete_over';
			over['fieldText'] = ul.childNodes[over['selectie']].title;
			id = ul.childNodes[over['selectie']].id;
			regs = eval('/' + camp +'_/g');
			over['idDB'] = id.replace(regs, '');
		}
//		debug(over['camp']+'/'+over['fieldText']+'/'+over['selectie']);
//		debug(camp+'/'+value);
	}
	
	function arata_simplu(id_div){
		if(document.getElementById(id_div)){
			document.getElementById(id_div).style.display = 'block';
		}
	}
	
	function ascunde_simplu(id_div){
		if(document.getElementById(id_div)){
			document.getElementById(id_div).style.display = 'none';
		}
	}
	
	function debug(text){
		if(!(camp = document.getElementById('debug'))){
			document.body.innerHTML += '<div id="debug"/></div>';
			camp = document.getElementById('debug');	
		}
//		camp.innerHTML += '<br />' + text;
		camp.innerHTML +=  '#' + text;
	}
	
	function request(camp){
		setIdDB(camp, '')
		campCurr = camp;
		for(i in campuri){
//			debug('ul_' + i);
			clean('ul_' + i);
		}
		updateOver(camp,'');
		function catchResponse(rasp) {
			procesare_lista(camp, rasp)
		}
		
		string=document.getElementById(camp).value;
//		debug(string.length);
		if(string.length>2){
			sentStr = eval(functie[camp]);
			sentStr = sentStr.replace(/\(/g, '("' + string + '"');
			compute_and_respond(sentStr, 'highlight("' + string + '");', catchResponse);
		}else{
			clean('ul_' + camp);
		}
	}
	
	function checkRequest(camp){
		string=document.getElementById(camp).value;
		if((string != inputValue[camp])){
			request(camp);
			inputValue[camp] = string;
		}
	}
	
	function clean(ul_camp){
//		debug(ul_camp);
		ul = document.getElementById(ul_camp);
		while (ul && ul.childNodes.length > 0) {
	  		ul.removeChild(ul.lastChild);
 		}
 		ascunde_simplu(ul_camp);
	}
	
	function procesare_lista(camp, rasp){
		list = document.getElementById('ul_' + camp);
		clean('ul_' + camp);
		
		full = 0;
		for( key in rasp){
			if(key){
				addItem(camp, rasp[key], full, key);
				full++;
			}
		}
		document.getElementById('ul_' + camp).innerHTML = document.getElementById('ul_' + camp).innerHTML;
//		debug(document.getElementById('ul_' + camp).innerHTML);
//		debug(document.getElementById('ul_' + camp).innerHTML);
		if(full){		
			arata_simplu('ul_' + camp);
		}
	}
	
	function addItem(camp, value, index, key) {
		var strikes = 0; //daca ajunge la 2 inseamna ca avem un match perfect si updatam campul cu id-uri
		var ul = document.getElementById('ul_' + camp);
		var li = document.createElement('li');
		
		string = value.split('<span>');
		strippedValue = string[0];
		if(string[0].length == 0){
			strikes++;
		}
		
		var newTextNode = document.createTextNode(string[0]);
		li.appendChild(newTextNode);
		
//		debug(string.length);
		for (i=0; i<string.length-1; i++){
			bucati = string[i+1].split('</span>');
//			debug(bucati[0] +'/'+bucati[1]);
			var span = document.createElement('span');
//				debug(emboss[camp]);
				if(emboss[camp]){
					span.setAttribute('class', 'autocompleteHighlight');
				}
				var newTextNode = document.createTextNode(bucati[0]);
				span.appendChild(newTextNode);
			li.appendChild(span);
			strippedValue += bucati[0];
			
			if(bucati[1].length == 0){
				strikes++;
			}
			
			var newTextNode = document.createTextNode(bucati[1]);
			li.appendChild(newTextNode);
			strippedValue += bucati[1];
		}
		
		if(strikes == 2){
			setIdDB(camp, key);
		}
		
//		debug(strippedValue);
		li.setAttribute('onmouseover', 'javascript:this.onmousemove=function test(){updateOver(\'' + camp +'\', \'' + index + '\');}');
		li.setAttribute('title', strippedValue);
		li.setAttribute('index', index);
		li.setAttribute('id', camp + '_' + key);
		li.setAttribute('onclick', 'javascript:setValue(\'' + camp +'\', \'' + strippedValue + '\', \'' + key + '\');updateOver(\'\', \'\');');
		li.setAttribute('onmouseout', 'javascript:updateOver(\'' + camp +'\', \'\');');
		li.setAttribute('class', 'elemListaAutocomplete_default');


		ul.appendChild(li);
	}
	
	function setIdDB(camp, valoare){
		if(document.getElementById(camp)){
			fieldIdDB = document.getElementById('id_' + camp);
			if(fieldIdDB.value.length && valoare.length){
				fieldIdDB.value += ',' + valoare;
			}else{
				fieldIdDB.value = valoare;
			}
		}
	}
	
	function setValue(camp, text, idDB){
		if(document.getElementById(camp)){
			input = document.getElementById(camp);
			fieldIdDB = document.getElementById('id_' + camp);
			clean('ul_' + camp);
			if(text.length){
				input.value = text;
				fieldIdDB.value = idDB;
			}
			if(campuri[camp] != '' && document.getElementById(campuri[camp])){
				nextField = document.getElementById(campuri[camp]);
				nextField.focus();
				if(nextField.type == 'text' || nextField.type == 'textarea'){
					nextField.select();
				}
				campCurr = campuri[camp];
			}
			
		}
		return false;
	}