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

//
// getKeyCode
//
function getKeyCode(args)
{
	// TODO - Questa condizione non e' sicura
	if (navigator.userAgent.toLowerCase().indexOf("msie")!=-1) {
		return event.keyCode;
	} 
	else {
		return args[0].keyCode;
	}
}

function Autore(cognome, nome, prefisso, carica, autore) 
{
	this.prefisso	= prefisso;
	this.cognome  	= cognome;
	this.nome		= nome;
	this.carica		= carica;
	this.autore		= autore;		

	this.getAutore = function()	
	{
		autore = '';
		autore += cognome;

		if (nome!='') {
			autore += ', ';
			autore += nome;
		}
		
		if (prefisso!='') {
			autore += ' (';
			autore += prefisso;
			autore += ')';
		}
		
		if (carica!='') {
			autore += ' (';
			autore += carica;
			autore += ')';
		}
		
		return autore;
	}
}

//
// ASEAutore object
//
function ASEAutore(objPrefisso, objCognome, objNome, objCarica, type)
{
	var arrAutori 					= new Array();
	var type						= type;
	var objNomeInput				= objNome;
	var objSearchInput				= objCognome;
	var caretIndex					= -1;

	// frame x div tendina
	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';

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

	var selectedValue =  objSearchInput.value;
	
	// appendo al cognome la tendina (frame + div)
	objSearchInput.parentNode.appendChild(objSearchResult);
	objSearchInput.parentNode.appendChild(objFrame);
		
	// handler
	objSearchInput.onkeyup   	= keyUp;
	objSearchInput.onkeydown 	= keyDown;
	objSearchInput.onblur 		= blur;

	var strCommand  = strCommand;
	
	// Http Request
	var httpRequest = 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 {
					objSearchResult.childNodes[objSearchResult.selected].childNodes[0].className   = 'aseRowSelected';
				}
				return false;
			// ARROW DOWN
			case 40:
				if (objSearchResult.selected == -1) {
					objSearchResult.selected = 0;
					objSearchResult.childNodes[objSearchResult.selected].childNodes[0].className   = 'aseRowSelected';
				} else if (objSearchResult.selected < objSearchResult.childNodes.length-1) {
					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()
	{
		// svuota lista risultati
		objSearchResult.innerHTML 		= '';
		objSearchResult.style.display 	= 'none';
		objSearchResult.selected 		= -1;
		objFrame.style.display 			= 'none';
	}
	
	//
	// hndl
	// 
	function hndl()
	{
		if (httpRequest.readyState == RS_FINISHED) {

			strSearchTmp = objSearchInput.value;
			
			processing = false;
			if (strSearchTmp != strSearch) {
				cmd();
			} 
			else {
				update(httpRequest.responseText);
			}
		} 
	}
	
	//
	// 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;
			
			strCmd  = "admin/hr.php";
			strCmd += "?obj=search";
			strCmd += "&cmd=autori";
			strCmd += "&str=" + escape(strSearch);
		
			httpRequest.open('post', strCmd);
			httpRequest.onreadystatechange = hndl;
			processing = true;
			httpRequest.send(null);
		}
	}
	
	//
	// update
	//	
	function update(str)
	{
		if (str.length) {
			// parserizzo la stringa dei risultati
			
			// righe
			arrRows = str.split('~R58~');
			// campi
			html = '';
			for (ii=0; ii<arrRows.length; ii++) {
				arrFields = arrRows[ii].split('~F58~');

				arrAutori[ii] = new Autore(arrFields[0], arrFields[1], arrFields[2], arrFields[3]);

				html += '<div><div class="aseRow">';
				html += arrAutori[ii].getAutore();
				html += '</div></div>';
			}
			
			objSearchResult.innerHTML 		= html;
			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';
		}
	}
	
	//
	// HACK - htmlspecialchars
	// 
	function htmlspecialchars(str)
	{
		return str.replace('&amp;', '&');
	}
	
	//
	// setValues
	//
	function setValues(ind)
	{
		
		var autore 			= arrAutori[objSearchResult.selected];
		objNome.value 		= autore.getAutore();
		selectedValue = objSearchInput.value;
	}
	
	//
	// WARNING : 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();
	}
	
	
}

