d = document;
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
addLoadEvent(adatta);

/* Nasconde tutti le liste (ul) con class impostato a "compatta" */
function adatta() {
	pageHeight = 0;
	if (self.innerHeight) {
		if ($('colonnasx').scrollHeight < $('colonnadx').scrollHeight) {
			pageHeight = $('colonnadx').scrollHeight + 50;
			onresize = function() { adatta(); }
		} else {
			pageHeight = $('colonnasx').scrollHeight + 50;
			onresize = function() { adatta(); }
		}
	} else {
		if ($('colonnasx').clientHeight < $('colonnadx').clientHeight) {
			pageHeight = $('colonnadx').clientHeight + 50;
			onresize = function() { adatta(); }
		} else {
			pageHeight = $('colonnasx').clientHeight + 50;
			onresize = function() { adatta(); }
		}
	}
	if (pageHeight != 0) {
		pageHeight = Math.round(pageHeight) + 'px';
		$('pagina').style.height = pageHeight;
	}

	if ($('focus1') && $('focus2')) {
		divHeight = new Array();
		maxHeight = 0;
		if ($('focus1').offsetHeight) {
			if ($('focus1').offsetHeight > $('focus2').offsetHeight) {
				$('focus2').style.height = $('focus1').offsetHeight + "px";
			} else {
				$('focus1').style.height = $('focus2').offsetHeight + "px";
			}
		} else if($('focus1').style.pixelHeight) {
			if ($('focus1').style.pixelHeight > $('focus2').style.pixelHeight) {
				$('focus2').style.pixelHeight = $('focus1').style.pixelHeight;
			} else {
				$('focus1').style.pixelHeight = $('focus2').style.pixelHeight;
			}
		}

		maxHeight = Math.max(maxHeight,divHeight);

		if ($('focus1').style.height > $('focus2').style.height) {
			$('focus2').style.height = $('focus1').style.height;
		} else {
			$('focus1').style.height = $('focus2').style.height;
		}
	}
}

// funzione per l'apertura di una finestra in popup
function popup(link, windowWidth, windowHeight) {
	var windowWidth = windowWidth + 18;
	var windowHeight = windowHeight + 18;

	var aw = (screen.availWidth - windowWidth ) / 2;
	var ah = (screen.availHeight - windowHeight) / 2;

	var winAttr = "width=" + windowWidth + ",height=" + windowHeight + ",left=" + aw + ",top=" + ah + "scrollbars=0,status=0,resizable=0";

	newWindow=window.open(link, "Anteprima", winAttr);
}

function showDiv(val_id) {
	/* funzione per visualizzare un div nascosto */
	var val_id;
	if ($(val_id)) {
		if ($(val_id).style.display != "block") {
			$(val_id).style.display = "block";
		} else {
			$(val_id).style.display = "none";
		}
	}
	adatta();
}

function nascondiDiv(val_id) {
	var val_id;
	if ($(val_id)) {
		$(val_id).style.display = "none";
	}
	adatta();
}

function mostraDiv(val_id) {
	var val_id;
	if ($(val_id)) {
		$(val_id).style.display = "block";
	}
	adatta();
}

/* Contatti agenti*/
function swap(regione) {
	/* rollover cartina */
	$('italia').src = "immagini/italia.gif";
	var imm = "immagini/" + regione + ".gif";
	$('italia').src = imm;
	$('italia').alt = imm.toString();
	$('italia').title = imm.toString();
	$('italia').style.cursor = 'hand';
}

function swapBack() {
	$('italia').src = "immagini/italia.gif";
	$('italia').style.cursor = '';
}

// link al click sulla regione
function go(regione) {
	// pesco i dati via AJAX
	var url = "xml/xml_get_agenti.php";
	var pars = "regione=" + regione;
	var MyAjax = new Ajax.Updater(
		{success: 'div_agenti'},
		url,
		{
			method: 'post',
			parameters: pars,
			onFailure: errore('div_agenti'),
			onSuccess: function() {
				setTimeout(
					function() {
						adatta();
						new Effect.ScrollTo('div_agenti', {offset: 100});
					}, 100
				);
			}
		}
	);
}

// chiamata di gestione dell'errore Ajax
function errore(request, div) {
	if ($(div)) {
		$(div).innerHTML = "Errore nella chiamata Ajax";
	}
}