/*
*
* Active Search Element version 1.0.3 - CEC
*
*/

//
// getKeyCode
//
function getKeyCode(args)
{
	// TODO - Sistemare per gli altri browser
	if(navigator.userAgent.toLowerCase().indexOf("msie")!=-1){
		return event.keyCode;
	}else{
		return args[0].keyCode;
	}
}

//
// leftTrim
//
function leftTrim(str)
{
	while (str.substring(0,1) == ' '){
		str = str.substring(1, str.length);
	}
	return str;
}

//
// TRY
//
var TRY = 
{ 
	these: function()
	{ 
		var returnValue;

		for (var i = 0; i < arguments.length; i++){
			var lambda = arguments[i];
			try {
				returnValue = lambda();
				break;
			} 
			catch (e){}
		}
		
		return returnValue;
	}
}

//
// getTransport
// 	
getTransport = function()
{
	var hr =  TRY.these(	function() {return new ActiveXObject('Msxml2.XMLHTTP')},
							function() {return new ActiveXObject('Microsoft.XMLHTTP')},
							function() {return new XMLHttpRequest()}) || null;
	return hr;
}

//
// ASE object
//
function ASEArgomento(strID, objSearch, objName, objHidden, strCommand)
{
	var objNameInput				= objName;
	var objSearchInput				= objSearch;
	var id							= strID;
	var caretIndex					= -1;
	
	var strSearch					= ''; // stringa da cercare
	
	var objFrame					= document.createElement('iframe');
	objFrame.frameBorder			= '0';
	objFrame.scrolling				= 'no';
	objFrame.style.height			= '0px';
	objFrame.style.width			= '0px';
	objFrame.style.position			= 'absolute';
	objFrame.style.display 			= 'none';

	var objSearchResult				= document.createElement('div');
	objSearchResult.selected 		= -1;
	objSearchResult.className 		= 'aseContainer';
	objSearchResult.style.display 	= 'none';

	var selectedValue =  objSearchInput.value;
	
	objSearchInput.parentNode.appendChild(objSearchResult);
	objSearchInput.parentNode.appendChild(objFrame);
		
	objSearchInput.onkeyup   	= keyUp;
	objSearchInput.onkeydown 	= keyDown;
	objSearchInput.onblur 		= blur;

	var objHidden	= objHidden;

	var strCommand  = strCommand;
	
	// Http Request
	var httpRequest = getTransport();

	var RS_UNINITIALIZED	= 0;
	var RS_LOADING 			= 1;
	var RS_LOADED 			= 2;
	var RS_INTERACTIVE 		= 3;
	var RS_FINISHED 	 	= 4;
	
	// semaforo
	var processing = false;
	
	// self
	var self = this;
	
	//
	// keyDown
	//
	function keyDown()
	{
		var keyCode = getKeyCode(arguments);

		switch(keyCode){
			// RETURN
			case 13:
				if(objSearchResult.selected!=-1){
					setValues(objSearchResult.selected);
					blur();
				}
				return false;
			// ARROW UP
			case 38:
				if (objSearchResult.selected > 0) {
					objSearchResult.childNodes[objSearchResult.selected].childNodes[0].className   = 'aseRow';
					objSearchResult.childNodes[--objSearchResult.selected].childNodes[0].className = 'aseRowSelected';
				} 
				else {
					if (objSearchResult.childNodes[objSearchResult.selected]) {
						objSearchResult.childNodes[objSearchResult.selected].childNodes[0].className   = 'aseRowSelected';
					}
				}
				return false;
			// ARROW DOWN
			case 40:
				if (objSearchResult.selected == -1) {
					objSearchResult.selected = 0;
					if (objSearchResult.childNodes[objSearchResult.selected])
						objSearchResult.childNodes[objSearchResult.selected].childNodes[0].className   = 'aseRowSelected';
				} else if(objSearchResult.selected < objSearchResult.childNodes.length-1) {
					if (objSearchResult.childNodes[objSearchResult.selected]) {
						objSearchResult.childNodes[objSearchResult.selected].childNodes[0].className   = 'aseRow';
						objSearchResult.childNodes[++objSearchResult.selected].childNodes[0].className = 'aseRowSelected';
					}
				}
				return false;
		}
	}
	
	// keyUp
	function keyUp()
	{
		var keyCode = getKeyCode(arguments);
		
		switch(keyCode){
			// RETURN
			case 13:
			// ALT
			case 18:
			// ARROW UP
			case 38:
			// ARROW DOWN
			case 40:
				return;
			// ESC
			case 27:
				this.blur();
				return true;
		}
		
		cmd();
	}
	
	// blur
	function blur()
	{
		argomento = objSearchInput.value;
		if (argomento!='') {
			// gestione selezione obbligatoria
			/*if (fSearchArgomento(argomento)) {
				if (!fAddArgomento(argomento)) {
					objSearchInput.value 	 = '';
					objSearchResult.selected = -1;
				}
			}*/
		}
		
		// svuota lista risultati
		objSearchResult.innerHTML 		= '';
		objSearchResult.style.display 	= 'none';
		objSearchResult.selected 		= -1;
		
		objFrame.style.display = 'none';
	}
	
	// hndl
	function hndl()
	{
		if (httpRequest.readyState == RS_FINISHED) {

			processing = false;
			
			if (strSearch==objSearchInput.value) {
				update((httpRequest.responseText));
				sz = (httpRequest.responseText);
				//alert(sz);
			} 
			else {
				cmd();
			}
		} 
	}
	
	// cmd
	function cmd()
	{
		if (!processing) {
			if (objSearchInput.value=='') {
				objSearchResult.innerHTML 		= '';
				objSearchResult.style.display	= 'none';
				objSearchResult.selected 		= -1;
				
				objFrame.style.display 	= 'none';
				return;
			}

			strSearch = objSearchInput.value;
							
			processing = true;
			
			strCmd  = "admin/hr.php";
			strCmd += "?obj=search";
			strCmd += "&cmd=" + strCommand;
			strCmd += "&str=" + escape(strSearch);
			
			if (strCommand=='collane') {
				strCmd += "&codice_editore=" + $('COLEDITORECODICE').value;
			}
			httpRequest.open('post', strCmd);
			httpRequest.onreadystatechange = hndl;
			httpRequest.send(null);
		}
	}

	// 
	function update(str) {
		if (str.length) {
			objSearchResult.innerHTML 		= str;
			objSearchResult.style.display 	= 'block';
			objSearchResult.selected 		= 0;
			objSearchResult.childNodes[objSearchResult.selected].childNodes[0].className = 'aseRowSelected';

			objFrame.style.display 	= 'block';
			objFrame.style.height 	= objSearchResult.clientHeight+2+'px';
			objFrame.style.width  	= objSearchResult.clientWidth +2+'px';
			
			for (ind=0; ind<objSearchResult.childNodes.length; ind++) {
				objSearchResult.childNodes[ind].onmouseover = self.mouseover;
				objSearchResult.childNodes[ind].onmousedown = self.click;
				objSearchResult.childNodes[ind].index 		= ind;
			}
		} else {
			// svuota lista risultati
			objSearchResult.innerHTML 		= '';
			objSearchResult.style.display 	= 'none';
			objSearchResult.selected 		= -1;
			objFrame.style.display 			= 'none';
		}
	}
	
	// htmlspecialchars - HACK
	function htmlspecialchars(str)
	{
		return str.replace('&amp;', '&');
	}
	
	// setValues
	function setValues(ind)
	{
		if (objNameInput) {
			strArray = objSearchResult.childNodes[ind].childNodes[0].innerHTML.split(',');
			objSearchInput.value = objSearchResult.childNodes[ind].childNodes[0].innerHTML;
			/*if (strArray[1]) {
				objNameInput.value = leftTrim(strArray[1]);
			} else {
				objNameInput.value = '';
			}*/
		} else if(objHidden) {
			objHidden.value      = objSearchResult.childNodes[objSearchResult.selected].childNodes[1].value;
			objSearchInput.value = htmlspecialchars(objSearchResult.childNodes[ind].childNodes[0].innerHTML);
		} else {
			if (objSearchInput.value.split(';').length>1) {
				arrStr = objSearchInput.value.split(';');
				arrStr[caretIndex] = objSearchResult.childNodes[ind].childNodes[0].innerHTML; 		
				objSearchInput.value = arrStr.join(';');
			} else {
				objSearchInput.value = objSearchResult.childNodes[ind].childNodes[0].innerHTML;	
			}
		}
		
		selectedValue = objSearchInput.value;
	}
	
	// TODO : Funziona perche' il this cambia 
	this.mouseover = function()
	{
		objSearchResult.childNodes[objSearchResult.selected].childNodes[0].className = 'aseRow';
		objSearchResult.selected = this.index;
		objSearchResult.childNodes[objSearchResult.selected].childNodes[0].className = 'aseRowSelected';	
	}
	
	// click
	this.click = function()
	{
		setValues(this.index);
		blur();
	}
}

