function is_def( v ) {
	if (typeof(v)=="undefined")
		return false;
	return true;
}

// da finire......
function class_div_autocomp( id, nuovo ) {
	this.base = class_div;
	this.base( id, true );
	this.is_autocomp_divsel = true;
	this.loading_obj_show = false;
}
class_div_autocomp.prototype = new class_div;


function autocomp_inptext_onkeyup( e, elm_inptext, id_divsel, url, params ) {
	// e == evento
	// obj_inptext == oggetto input di testo
	// id_divsel == id del div che conterrą il testo da selezionare
	// url == url della pagina che riepirą id_divsel
	// params == eventuali parametri aggiuntivi per la pagina, ad esempio: tipo=10&toni=5
	var keycode;
	if(window.event) { // IE
		keycode = window.event.keyCode;
	} else if(e.which) { // Netscape/Firefox/Opera
		keycode = e.which;
	}
	switch(keycode) {
		case 13: // tasto Invio
			autocomp_divsel_copytext( id_divsel );
			return false;
			break;
		case 37: // left
			break;
		case 39: // right
			break;
		case 38: // up
		case 40: // down
			autocomp_divsel_key( id_divsel, keycode );
			return false;
	}
	// se l'oggetto non esiste lo creo, se esiste gią lo riuso
	var n = 'obj_divsel_'+id_divsel;
	var obj = null;
	obj = obj_arr_get_from_id( id_divsel );
	if (!obj) {
		obj = new class_div_autocomp( id_divsel );
		obj.elm.style.cursor = 'pointer';
		divpopload_del_obj_add( obj );
		obj.autocomp_old_value="";
		obj.obj_inptext=new class_obj(elm_inptext.id);
		obj.obj_inptext.elm.onkeypress = autocomp_inptext_onkeypress;
		divpopload_del_obj_add( obj.obj_inptext );
		xy = obj.obj_inptext.xy_get();
		wh = obj.obj_inptext.wh_get();
		obj.xy_set( xy.x, xy.y+wh.h );
		obj.wh_set( wh.w, '' );
		obj.elm.onclick = autocomp_divsel_onclick;
		document.body.onclick = autocomp_document_onclick;
//		print_info( 'w='+wh.w );
	}
	var str = string_trim( elm_inptext.value );
	if (str != obj.autocomp_old_value) {
		obj.autocomp_old_value = str;
		if (params)
			params+= '&';
		else
			params = '';
		params+= 'str='+str;
		obj.load_page_completed_function = autocomp_load_page_completed;
		obj.load_page( url, params );
	}
	return true;
}
function autocomp_inptext_onkeypress( e ) {
	// questa funzione mi serve solo per ritornare false alla pressione del tasto invio
	// usando onkyepress questa funzione non vengono catturati i tasti su/giu/dx/sx, (il keycode restituisce undefined)
	// usando onkeydown la pressione del tasto invio fa il submit della form, anche se restituisco false
	// per evitare che invio faccia il submit devo restituire false in onkeydown
	var keycode;
	if (e) { // FF
		keycode = e.which;
	} else { // IE
		keycode = window.event.keyCode;
	}
	if (keycode == 13) { // enter
		return false;
	}
	return true;
}
function autocomp_divsel_onclick() {
	autocomp_divsel_copytext( this.id );
	return false;
}
function autocomp_document_onclick() {
	for (var i=0; i<obj_arr.length; i++) {
		if (obj_arr[i]) {
			if (typeof(obj_arr[i].is_autocomp_divsel)!="undefined")
				obj_arr[i].hide();
		}
	}
}
function autocomp_load_page_completed( err ) {
	if ( err ) {
		this.hide();
		return;
	}
	if (this.html_get()) {
		autocomp_divsel_init_loaded_html( this.elm.id );
		this.show();
	} else {
		this.hide();
	}
}
function autocomp_divsel_init_loaded_html( id_divsel ) {
	var tr = autocomp_divsel_get_first_tr( id_divsel );
	if (!tr)
		return;
	var pn = tr.parentNode;
	for(var i=0; i<pn.childNodes.length; i++) {
		if (pn.childNodes[i].nodeName == "TR") {
			tr = pn.childNodes[i];
			tr.onmouseover = autocomp_tr_onmouseover;
			tr.onmouseout = autocomp_tr_onmouseout;
		}
	}
}
function autocomp_tr_onmouseover() {
	pn = this.parentNode;
	for(var i=0; i<pn.childNodes.length; i++) {
		if (pn.childNodes[i].nodeName == "TR") {
			if ( (typeof(pn.childNodes[i].autocomp_selected)!="undefined") && pn.childNodes[i].autocomp_selected) {
				autocomp_tr_selected_set( pn.childNodes[i], false );
				break;
			}
		}
	}
	autocomp_tr_selected_set( this, true );
}
function autocomp_tr_onmouseout() {
	autocomp_tr_selected_set( this, false );
}


function string_trim(str) {
	var chars = "\\s";
	// left trim
	str = str.replace(new RegExp("^[" + chars + "]+", "g"), "");
	// right trim
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
 
function autocomp_divsel_key( id_divsel, keycode ) {
	var obj = document.getElementById( id_divsel );
	var obj_tbody = null;
	for(var i=0; i<obj.childNodes.length; i++) {
		if (obj.childNodes[i].nodeType==1) {
			switch (obj.childNodes[i].tagName) {
				case "DIV":
				case "TABLE":
					obj = obj.childNodes[i];
					i=-1;
					break;
				case "TBODY":
					obj_tbody = obj.childNodes[i];
					break;
			}
		}
	}
	if (!obj_tbody)
		return;
	obj = obj_tbody;
	var tr_first = null;
	var tr_prec = null;
	var tr_succ = null;
	var tr_sel = null;
	var tr_prec_tmp = null;
	for(var i=0; i<obj.childNodes.length; i++) {
		if ( (obj.childNodes[i].nodeType==1) && (obj.childNodes[i].tagName=="TR") ) {
			var tr = obj.childNodes[i];
			if (!tr_first)
				tr_first = tr;
			if (tr_sel) { // ho trovato il tr_sel nel ciclo precedente, quindi questo tr e il successivo
				tr_succ = tr;
				break;
			}
			if (tr.autocomp_selected) {
				tr_sel = tr;
				tr_prec = tr_prec_tmp;
			}
			tr_prec_tmp = tr;
		}
	}
	if (tr_sel)
		autocomp_tr_selected_set( tr_sel, false );
	switch(keycode) {
		case 37: // left
			break;
		case 38: // up
			if (tr_prec)
				autocomp_tr_selected_set( tr_prec, true );
			break;
		case 39: // right
			break;
		case 40: // down
			if (tr_succ)
				autocomp_tr_selected_set( tr_succ, true );
			else {
				if (!tr_sel)
					autocomp_tr_selected_set( tr_first, true );
			}
			break;
	}
}

function autocomp_divsel_copytext( id_divsel ) {
	var tr = autocomp_divsel_get_first_tr( id_divsel );
	if (!tr)
		return;
	tbody = tr.parentNode;
	for(var i=0; i<tbody.childNodes.length; i++) {
		if (tbody.childNodes[i].nodeName == "TR") {
			if ( (typeof(tbody.childNodes[i].autocomp_selected)!="undefined") && tbody.childNodes[i].autocomp_selected) {
				// ho trovato il tr correntemente selezionato
				obj = obj_arr_get_from_id( id_divsel );
				obj.obj_inptext.elm.value = tbody.childNodes[i].innerText || tbody.childNodes[i].textContent;
				obj.hide(); // nascondo il div
				return;
			}
		}
	}
}

function autocomp_divsel_get_first_tr( id_divsel ) {
	// restituisce il primo tr presente nel div
	var elm = document.getElementById( id_divsel );
	for(var i=0; i<elm.childNodes.length; i++) {
//		if (elm.childNodes[i].nodeType==1) {
			switch (elm.childNodes[i].nodeName) {
				case "DIV":
				case "TABLE":
				case "TBODY":
					elm = elm.childNodes[i];
					i=-1;
					break;
				case "TR":
					return elm.childNodes[i];
			}
//		}
	}
	return null;
}

function autocomp_tr_selected_set( tr, on ) {
	if (on) {
		tr.autocomp_backgroundColor = tr.style.backgroundColor;
		tr.autocomp_selected = true;
		tr.style.backgroundColor = "#000000";
		for(var i=0; i<tr.childNodes.length; i++) {
//			if ( (tr.childNodes[i].nodeType==1) && (tr.childNodes[i].tagName == "TD") ) { // 1 == ELEMENT_NODE
			if (tr.childNodes[i].nodeName == "TD") {
				var td = tr.childNodes[i];
				td.style_color = td.style.color;
				td.style.color = "#ffffff";
			}
		}
	} else {
		if (typeof(tr.autocomp_backgroundColor)!="undefined") {
			tr.style.backgroundColor = tr.autocomp_backgroundColor;
			tr.autocomp_selected = false;
			for(var i=0; i<tr.childNodes.length; i++) {
//				if ( (tr.childNodes[i].nodeType==1) && (tr.childNodes[i].tagName == "TD") ) { // 1 == ELEMENT_NODE
				if (tr.childNodes[i].nodeName == "TD") { // 1 == ELEMENT_NODE
					var td = tr.childNodes[i];
					td.style.color = td.style_color;
				}
			}
		}
	}
}

