	var over = new Array();
	over['camp'] = '';
	over['fieldText'] = '';
	over['selectie'] = '';
	over['idDB'] = '';
	var campCurr = '';
	var timeout;
	var inputValue = new Array();	
	var campuri = new Array();
	var urlRequest = new Array();
	var emboss = new Array();

	function ul_blur(camp){
//		alert($(camp).value);
		if($('intinzatorul_' + camp)){
			$('intinzatorul_' + camp).innerHTML='XXXX';
		}
		$('aux_' + camp).value='';
		if ( isNaN(over['selectie'])){
				clean('ul_' + camp);
		}
		over['fieldText'] = '';
		over['selectie'] = '';
	}
	
	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 setValue(camp, text, idDB){
		if($('aux_' + camp)){
			if(text.length){
				reg = eval('/&quot;/g');
				strippedValue = text.replace(reg, '"').replace(/\\\'/g, '\'');
				addItemToList( idDB, strippedValue, camp);
				$('aux_' + camp).value = '';
				over['camp'] = '';
				over['fieldText'] = '';
				over['selectie'] = '';
//				debug(camp);
				ul_blur(camp);
//				alert('kkk');
//debug(camp);
				doFocus(camp);
			}
			clean('ul_' + camp);
		}
		return false;
	}
	
	function disableEnterKey(e,camp){
	    var key;
	
//	  	campul = document.getElementById(camp);
//	    campul.form.onsubmit = function (){return false;};
		
	     if(window.event){
	          	key = window.event.keyCode;     //IE
		}else
	          key = e.which;     //firefox
	
	     if(key == 13) {
	     	return false;
	     }else{
	        return true;
	     }
	}
	function selectFromList(){
		setValue(over['camp'], over['fieldText'], over['idDB']);
		updateOver('', '');
	}

	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();
	     		break;
	     	case 40:
	     		moveSelection(1);
	     		break;
	     	case 38:
	     		moveSelection(-1);
	     		break;
	     	default:
	     		checkRequest(camp);
	     }
	
	     return false;
	}
	
	
	function moveSelection(pas){
		index = over['selectie'];
		camp = $('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 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){
		clearTimeout(timeout);
		var funct = "request1('" + camp + "');"
//		alert(funct);
		timeout=setTimeout(funct,250);
			
	}
	function request1(camp){
//		setIdDB(camp, '')
		campCurr = camp;
//		alert(camp);

		for(i in campuri){
//			debug('ul_' + i);
			clean('ul_' + i);
		}
		updateOver(camp,'');
		function catchResponse(rasp) {
			procesare_lista(camp, rasp);
		}
		
		string=escape_belele($('aux_' + camp).value);
//		debug(string.length);
		if(string.length>2){
			var params = { search: string, field: camp, ajxF: 1, responseType: 'json'};
//			alert(urlRequest[camp]);
			new Ajax.Request(urlRequest[camp], 
			{
				method:'POST',
				parameters:params,
				onSuccess:catchResponse
			});
		}else{
			clean('ul_' + camp);
		}
	}
	
	function checkRequest(camp){
		string=$('aux_' + camp).value;
		if((string != inputValue[camp])){
			request(camp);
			inputValue[camp] = string;
		}
	}
	
	function procesare_lista(camp, rasp){
		var objResponse = rasp.responseText.evalJSON();
		list = $('ul_' + camp);
//		list.style.top = $('div_' + camp).height;
//		alert(list.style.top);
		clean('ul_' + camp);
//		debug('');
//		deb = document.getElementById('debug');
//		deb.innerHTML =  '';

		full = 0;
		for( key in objResponse){
			if(key){
				addItem(camp, objResponse[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');
//		debug('value='+value+'/index='+index+'/key='+key+'<br />');
		string = value.split('<span>');
		strippedValue = string[0];
		if(string[0].length == 0){
			strikes++;
		}
//		alert(value);
		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);
		strippedValue = strippedValue.replace(/\"/g, '&quot;').replace(/\'/g, '\\\'');
		key =key.replace(/\"/g, '&quot;').replace(/\'/g, '\\\'');
		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 addItemToList(itemId, itemName, camp){
			if(!itemId || !itemName) return false;
			if( $( 'bit-' + camp + '-' + itemId ) ) return false;
			
			var item = '';
			item = '<li id="bit-' + camp + '-' + itemId + '" class="bit-box">' + itemName + '<span class="bclose" onclick="return removeMe(this.parentNode);">x</span><input type="hidden" name="' + camp + '[' + itemId + ']" value="' + itemName + '"/></li>';
			$('holder_' + camp).innerHTML += item;
		}
		

//function adjustSize(camp){
//	if($("please_stretch_" + camp).style && $("intinzatorul_" + camp).scrollWidth){
//		$("please_stretch_" + camp).style.width = $("intinzatorul_" + camp).scrollWidth;
//	}
//}


function doFocus(camp)
{
	$('please_stretch_' + camp).style.display='block';
	$('aux_' + camp).focus();
}


function removeMe(me)
{
	me.parentNode.removeChild(me)
}


//function intinde(camp)
//{
////	alert(document.getElementById('intinzatorul_' + input));
//	if($('intinzatorul_' + camp).innerHTML && $('aux_' + camp).value){
//		$('intinzatorul_' + camp).innerHTML=$('aux_' + camp).value+'XYXX';
//	}
////	checkRequest(camp);
//	
////	testFriend(input.value);
//}

function escape_belele(string){
				string = string.replace(/'(\s*),(\s*)'/g, '*$1,$2*');
				string = string.replace(/"(\s*),(\s*)"/g, '#$1,$2#');
				string = string.replace(/'(\s*),(\s*)"/g, '*$1,$2#');
				string = string.replace(/"(\s*),(\s*)'/g, '#$1,$2*');
				return string;
			}
