function imagePopup(myimage, title) {
	html = "<HTML><HEAD><TITLE>" + title + "</TITLE>" +
		"</HEAD><BODY LEFTMARGIN=0 " +
		"MARGINWIDTH=0 TOPMARGIN=0 MARGINHEIGHT=0><CENTER>" +
		"<IMG SRC='" + myimage + "' BORDER=0 NAME=image " +
		"onload='window.resizeTo(document.image.width,document.image.height)'></CENTER>" +
		"</BODY></HTML>";
	popup=window.open('','image','toolbar=0,location=0,directories=0,menuBar=0,scrollbars=0,resizable=1');
	popup.document.open();
	popup.document.write(html);
	popup.document.focus();
	popup.document.close();
}

function help(helpPage, h) {
	window.open(helpPage,'Help','resizable=yes,toolbar=no,scrollbars=yes,location=no,width=500,height='+h);
}

function showhide(id) {
	state = document.getElementById(id).style.display;
	state = (state == ''? 'none': '');
	document.getElementById(id).style.display = state;
}

function enable_disable_input_field_displayTrans(chkbox_id, input_id) {
	chkBox = document.getElementById(chkbox_id);
	textInput = document.getElementById(input_id);
	textInput.disabled = chkBox.checked;
}

/* funzioni usate in transazione1.jsp */

function showhideModalitaPagamento(id) {
	var select = document.getElementById(id);
	var selected_option = select.options[select.selectedIndex];
	var id = selected_option.id;
	show('modalita', 'modalita'+id);
	show('pagacon', 'pagacon'+id);
}

function getSelectedModalitaPagamento() {
	var select = document.getElementById('comboModalita');
	var selected_option = select.options[select.selectedIndex];
	return selected_option.value;
}

function setModalitaPagamento() {
	var select = document.getElementById('comboModalita');
	var value = readCookie('modPagamento');		
	for (var i = 0; i < select.options.length; i++) {
		if (select.options[i].value == value) {
			select.selectedIndex = i;
		}
	}	
	showhideModalitaPagamento('comboModalita');
	
}

// Funzioni per visualizzare i tab e gestire i sottoelenchi con i cookie

function showTab(page, pattern, id) {
	for (var i = 0; i <= 10; i++) {
		subelement = document.getElementById(pattern+i);
		if (subelement) {
			if (id == (pattern+i)) {
				subelement.style.display = '';
				createCookie(page+"Tab",id,3653); /* ~10 anni, wow */
			} else {
				subelement.style.display = 'none';
			}
		}
	}
}

function showSezione(pattern, id) {
	for (var i = 0; i <= 10; i++) {
		subelement = document.getElementById(pattern+i);
		if (subelement) {
			if (id == (pattern+i)) {
				subelement.className = "menusinistraSel";
			} else {
				subelement.className = "menusinistraUnsel";
			}
		}
	}
}

function showhideInTab(page, tabId, id) {
	var element = document.getElementById(id);
	if (element == null)
		return;
	
	var img = document.getElementById(id + "_img");
	state = element.style.display;
	state = (state == ''? 'none': '');
	document.getElementById(id).style.display = state;
	if (state == '') {
		createCookie(page+"_"+tabId+"Section",id,3653); /* ~10 anni, wow */
	}
	if (img) {
		img.src = (state == ''? img.src.replace(/Closed/, "Open"): img.src.replace(/Open/, "Closed"));
	}
}

function setTab(page, pattern) {
	var valore = readCookie(page+"Tab");
	var elementVal = null;
	if (valore)
		elementVal = document.getElementById(valore);
	
	for (var i=0; i<=10; i++) {
		subelement = document.getElementById(pattern+i);
		if (subelement) {
			if ((valore == null) || (elementVal == null)) {
				valore = (pattern+i);
				elementVal = document.getElementById(valore);
			}
			if (valore == (pattern+i)) {
				subelement.style.display = '';
			} else {
				subelement.style.display = 'none';
			}
			valore2 = readCookie(page+"_"+pattern+i+"Section");
			if (valore2) {
				element = document.getElementById(valore2);
				if (element) {
					element.style.display = '';
					var img = document.getElementById(valore2 + "_img");
					if (img) {
						img.src = img.src.replace(/Closed/, "Open");
					}
				}
			}
		}
	}
}

/* ---------------------------------- */

function show(pattern, id) {
	var d = document.getElementById(id);
	for (var i = 0; i <= 100; i++) {
		if (document.getElementById(pattern+i)) {
			document.getElementById(pattern+i).style.display = 'none';
		}
	}
	if (d) {
		d.style.display = '';
	}
}


function hideByPattern(pattern, recursive) {
	// Nascondo tutto quello che matcha il pattern
	for (var i = 0; i <= 100; i++) {
		var concat=pattern+i;	
		if (document.getElementById(concat)) {
			if (document.getElementById(concat).style.display != 'none') {
				document.getElementById(concat).style.display = 'none';
				if (recursive) {
					var pattern2 = concat+'-';
					hideByPattern(pattern2);
				}
			}
		}
	}
}

function hideOtherByPattern(elem, pattern) {
	// Nascondo tutto quello che matcha il pattern
	for (var i = 0; i <= 100; i++) {
		var concat=pattern+i;	
		var d = document.getElementById(concat);
		if (d) {
			if (d != elem && d.style.display != 'none') {
				d.style.display = 'none';
			}
		}
	}
}


function showBlock(pattern, id) {
	// piglio l'elemento indicato e che dovro' poi mostrare
	var d = document.getElementById(id);

	hideByPattern(pattern, true);
	if (d) {
		d.style.display = 'block';
		hideLastApplet();
	} else {
		showLastApplet();
		}
	}

function showBlockNoRec(pattern, id) {
	// piglio l'elemento indicato e che dovro' poi mostrare
	var d = document.getElementById(id);

	hideOtherByPattern(d, pattern);
	
	if (d) {
		if (d.style.display != 'block') {
		d.style.display = 'block';
		} else {
			d.style.display = 'none';
		}		
		hideLastApplet();
	} else {
		showLastApplet();
	}
}

function showSelect(pattern, sid) {
	var v = document.getElementById(sid).value;
	
	var d = document.getElementById(pattern+v);
	for (var i = 0; i <= 100; i++) {
		if (document.getElementById(pattern+i)) {
			document.getElementById(pattern+i).style.display = 'none';
		}
	}
	if (d) {
		d.style.display = '';
	}
}

// Funzioni di gestione alternanza destra/applets

var lastAppletId;

function showPianta() {
	var eth2serial = document.getElementById('eth2serialDiv');
	if (eth2serial != null) {
		eth2serial.style.visibility = 'visible';
	}
	
	var pianta = document.getElementById('appletPiantaDiv');
	if (pianta == null) {
		return;
	}
	
	var mapper = document.getElementById('appletMapperDiv');
	var destra = document.getElementById('destra');
	
	if (mapper != null) {
		mapper.style.visibility = 'hidden';
	}
	
	destra.style.visibility = 'hidden';
	
	pianta.style.visibility = 'visible';
	lastAppletId = 'appletPiantaDiv';
}

function showMapper() {
	var eth2serial = document.getElementById('eth2serialDiv');
	if (eth2serial != null)
		eth2serial.style.visibility = 'visible';

	var mapper = document.getElementById('appletMapperDiv');
	if (mapper == null) {
		return;
	}
	
	var pianta = document.getElementById('appletPiantaDiv');
	var destra = document.getElementById('destra');
	if (pianta != null) {
		pianta.style.visibility = 'hidden';
	}
	
	destra.style.visibility = 'hidden';
	mapper.style.visibility = 'visible';
	lastAppletId = 'appletMapperDiv';
}

function hideApplets() {
	var pianta = document.getElementById('appletPiantaDiv');
	var mapper = document.getElementById('appletMapperDiv');
	var destra = document.getElementById('destra');
	var eth2serial = document.getElementById('eth2serialDiv');
	if (pianta != null)
		pianta.style.visibility = 'hidden';
	if (mapper != null)
		mapper.style.visibility = 'hidden';
	if (eth2serial != null)
		eth2serial.style.visibility = 'hidden';
		
	destra.style.visibility = 'visible';
	
	var appletDiv = document.getElementById('appletDiv');
	appletDiv.style.visibility = "hidden";
	
	lastAppletId = null;
}


function showPiantaParent() {
	var eth2serial = parent.document.getElementById('eth2serialDiv');
	if (eth2serial != null)
		eth2serial.style.visibility = 'visible';
		
	var pianta = parent.document.getElementById('appletPiantaDiv');
	if (pianta == null)
		return;
	
	var mapper = parent.document.getElementById('appletMapperDiv');
	var destra = parent.document.getElementById('destra');
	if (mapper != null)
		mapper.style.visibility = 'hidden';
	destra.style.visibility = 'hidden';
	pianta.style.visibility = 'visible';
	
	parent.lastAppletId = 'appletPiantaDiv';
}

/*
function hideAppletssParent() {
	var pianta = parent.document.getElementById('pianta');
	var mapper = parent.document.getElementById('mapper');
	var destra = parent.document.getElementById('destra');
	var eth2serial = parent.document.getElementById('eth2serial');
	
	if (pianta != null)
		pianta.style.visibility = 'hidden';
	if (mapper != null)
		mapper.style.visibility = 'hidden';
	if (eth2serial != null)
		eth2serial.style.visibility = 'hidden';
		
	destra.style.visibility = 'visible';
	
	parent.lastAppletId = null;
}
*/

function showLastApplet() {
	var eth2serial = document.getElementById('eth2serialDiv');
	if (eth2serial != null)
		eth2serial.style.visibility = 'visible';

	if (lastAppletId != null) {
		var lastApplet = document.getElementById(lastAppletId);
		lastApplet.style.visibility = 'visible';
	}
}

function hideLastApplet() {
	var eth2serial = document.getElementById('eth2serialDiv');
	if (eth2serial != null)
		eth2serial.style.visibility = 'hidden';

	if (lastAppletId != null) {
		var lastApplet = document.getElementById(lastAppletId);
		lastApplet.style.visibility = 'hidden';
	} else {
		var pianta = document.getElementById('appletPiantaDiv');
		var mapper = document.getElementById('appletMapperDiv');
		if (pianta != null)
			pianta.style.visibility = 'hidden';
		if (mapper != null)
			mapper.style.visibility = 'hidden';
	}
}

// Funzioni di dialogo con gli applets

function loadEvento(id, tipo) {
	try {
		showPianta();
		document.getElementById('pianta').loadEvento(id, tipo);
	} catch (e) {
		alert("Applet non ancora inizializzato, riprovare tra alcuni secondi...");
	}
}
function entraStore(id) {
	try {
		showPianta();
		document.getElementById('pianta').entraStore(id);
	} catch (e) {
		alert("Applet non ancora inizializzato, riprovare tra alcuni secondi...");
	}
}
function loadSala(id) {
	try {
		showMapper();
		document.getElementById('mapper').loadSala(id);
	} catch (e) {
		alert("Applet non ancora inizializzato, riprovare tra alcuni secondi...");
	}
}

function confermaEsternaParent(id, tipo, ana_id, tipoPrenot) {
	try {
		showPiantaParent();
		parent.document.getElementById('pianta').confermaEsternaPrenotazioni(id, tipo, ana_id, tipoPrenot);
	} catch (e) {
		alert("Pianta non ancora inizializzato, riprovare tra alcuni secondi...");
	}
}

function visualizzazioneEsternaParent(id, tipo, ana_id) {
	try {
		showPiantaParent();
		parent.document.getElementById('pianta').visualizzazioneEsternaVenduti(id, tipo, ana_id);
	} catch (e) {
		alert("Pianta non ancora inizializzato, riprovare tra alcuni secondi...");
	}
}

function checkGT() {
	var gt = false;
	try {
		var gt = document.getElementById('pianta').isGTattivo();
	} catch (e) {
		return false;
	}
	return gt;
}

// Funzioni gestione HTML dinamico (fragment o frame)

function loadFragmentInToElement(fragment_url, element_id) {
	fragment_url = addUnique2Url(fragment_url);
	var element = document.getElementById(element_id);
	element.innerHTML = '<em>Loading ...</em>';
	var xmlhttp;
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp) {
		xmlhttp.open("GET", fragment_url);
		xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				element.innerHTML = xmlhttp.responseText;
			} else {
				element.innerHTML = '<em>HTTP error ' + xmlhttp.status + '</em>';
			}
		}
	}
		xmlhttp.send(null);
	} else {
		element.innerHTML = '<em>xmlhttp null</em>';
	}
	if (element_id == 'destra')
		hideApplets();
}

function loadFragmentInToParentElement(fragment_url, element_id) {
	fragment_url = addUnique2Url(fragment_url);
	var element = parent.document.getElementById(element_id);
	element.innerHTML = '<em>Loading ...</em>';
	var xmlhttp;
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp) {
		xmlhttp.open("GET", fragment_url);
		xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				element.innerHTML = xmlhttp.responseText;
			} else {
				element.innerHTML = '<em>HTTP error ' + xmlhttp.status + '</em>';
			}
		}
	}
		xmlhttp.send(null);
	} else {
		element.innerHTML = '<em>xmlhttp null</em>';
	}
	if (element_id == 'destra')
		hideApplets();
}

function loadFragmentInToIFrame(fragment_url, element_id, refresh) {
	fragment_url = addUnique2Url(fragment_url);
	if (refresh)
		refresh = '&refresh=true';
	document.getElementById(element_id).innerHTML = '<iframe frameborder="0" name="' + element_id + '" src="/biglietteria/iframe.jsp?url=' + encodeURIComponent(fragment_url) + refresh + '"></iframe>';
	if (element_id == 'destra')
		hideApplets();
}

function loadFragmentInToParentIFrame(fragment_url, element_id) {
	fragment_url = addUnique2Url(fragment_url);
	parent.document.getElementById(element_id).innerHTML = '<iframe frameborder="0" name="' + element_id + '" src="/biglietteria/iframe.jsp?url=' + encodeURIComponent(fragment_url) + '"></iframe>';
	if (element_id == 'destra')
		parent.hideApplets();
}

function addUnique2Url(url) {
	var sep = '?';
	if (url.indexOf(sep) >= 0)
		sep = '&';
	var d = new Date();
	//return url + sep + 'uniqueCode=' + d.getYear() + d.getMonth() + d.getDay() + d.getHours() + d.getMinutes() + d.getSeconds();
	return url + sep + 'uniqueCode=' + (Math.abs(Math.random() * 1000000000));
}

// Funzione per nascondere le tabelle e mostare 'Loading...'

function showLoading() {
	var elements = document.getElementsByTagName('table');
	for (i = 0; i < elements.length; i ++) {
		element = elements.item(i);
   		element.style.display = 'none';
    }
    
    var newdiv = document.createElement('div');
    newdiv.innerHTML = '<em>Loading ...</em>';
	var first = document.body.childNodes.item[0];
	document.body.insertBefore(newdiv, first);
}

// Cookies

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

// Funzioni varie

function formatAsMoney(mnt) {
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
}

function focusTo(element_id) {
	var element = document.getElementById(element_id);
	element.focus();
}

function settaChange() {
	document.getElementById('flagChange').value = "true";
}

function changeProprietario() {
	var select = document.getElementById('comboProp');
	var idx = select.selectedIndex;
	if (idx == 0) {	
		document.getElementById('anagProprietario').style.display = '';
	} else {
		document.getElementById('anagProprietario').style.display = 'none';
	}
}
function nuovoProprietario() {
	document.getElementById('newProp').style.display = 'none';
	document.getElementById('creazioneProprietario').value = 'true';
	checkNuovoProprietario();
}
function nuovoIstituto() {
	document.getElementById('newIstit').style.display = 'none';
	document.getElementById('creazioneIstituto').value = 'true';
	checkNuovoIstituto();
}

function checkNuovoProprietario() {
	var value = document.getElementById('creazioneProprietario').value;
	if (value == 'true') {
		document.getElementById('anagProprietario').style.display = '';
	}
}	
function checkNuovoIstituto() {
	var value = document.getElementById('creazioneIstituto').value;
	if (value == 'true') {
		document.getElementById('anagIstituto').style.display = '';
	}
}	

function mostraConNull(size) {
	var found = 1;
	var indexFound = -1;
	for (var i = 0; i < size; i++) {
		var ns = document.getElementById('idT_' + i);
		if (ns.value == null || ns.value == '') {
			document.getElementById('tb_' + i).style.display = 'none';
			found = 0;
			if (indexFound == -1)
				indexFound = i;
		} else {
			document.getElementById('tb_' + i).style.display = '';
		}
	}
	if (found == 0) {
		document.getElementById('idT_'+indexFound).value = '';
		document.getElementById('tb_'+indexFound).style.display = '';
		
	}
}

function mostraConZero(size) {
	var found = 1;
	var indexFound = -1;
	for (var i = 0; i < size; i++) {
		var ns = document.getElementById('idT_' + i);
		if (ns.value == '0' || ns.value == '') {
			document.getElementById('tb_' + i).style.display = 'none';
			found = 0;
			if (indexFound == -1)
				indexFound = i;
		} else {
			document.getElementById('tb_' + i).style.display = '';
		}
	}
	if (found == 0) {
		document.getElementById('idT_'+indexFound).value = '0';
		document.getElementById('tb_'+indexFound).style.display = '';
		
	}
}

function conferma(text, link) {
	var ret = confirm(text);
	if (ret) {
		window.location = link;
	}
	return false;
}

function visualizzaSeCheck(nameIdChecked, nameObjet) {
	var isChecked = document.getElementById(nameIdChecked).checked;
	if (isChecked) {
		document.getElementById(nameObjet).style.display = '';
	} else {
		document.getElementById(nameObjet).style.display = 'none';
	}
	return true;
}

function settaSeVisualizzabile(nameObjet, valore) {
	if (valore == '1')
		document.getElementById(nameObjet).style.display = '';
	else
		document.getElementById(nameObjet).style.display = 'none';
	
	return true;
}



// Funzioni per la selezione e deselezione multipla dei CheckBox

// Parametri in ingresso:
// currentCheckBoxId = il checkbox da cui si � attivata l'azione
// checkBoxTuttiId = il checkbox per selezionare/deselezionare tutti
function uncheckCheckBoxTutti(currentCheckBoxId, checkBoxTuttiId) {
	if (document.getElementById(currentCheckBoxId).checked == false) {
		document.getElementById(checkBoxTuttiId).checked = false;
	}
}

// Parametri in ingresso:
// nomiCheckBox = il nome che compare in tutti i checkbox da selezionare/deselezionare
// checkBoxTuttiId = il checkbox per selezionare/deselezionare tutti
// boolGestoreBase = 1 se c'� un checkbox base da reimpostare, 0 altrimenti
function multicheckOnOff(nomiCheckBox, checkBoxTuttiId, boolCheckBoxBase) {
	var diventa = document.getElementById(checkBoxTuttiId).checked;
	var inputs = document.getElementsByTagName('input');
	for (var i=0; i < inputs.length; i++) {
		if ((inputs[i].type == "checkbox") && (inputs[i].id.indexOf(nomiCheckBox) != -1)) {
			inputs[i].checked = diventa;
		}
	}
	if (boolCheckBoxBase == 1) {
		checkBoxBase.checked = true;
	}
}

function togglaSinistra() {

	var sin = document.getElementById('sinistra');
	var sinAlt = document.getElementById('sinistra-alt');
	var destraBack = document.getElementById('destraBack');
	var destra = document.getElementById('destra');
	var appletDiv = document.getElementById('appletDiv');

	if (sin.className==''  || sin.className=='largo') {
		sin.className='stretto';
		sinAlt.className='largo';

		appletDiv.className='largo';
		destraBack.className='largo';
		destra.className = 'largo'
		
	} else {
		sin.className='largo';
		sinAlt.className='stretto';

		appletDiv.className='stretto';
		destraBack.className='stretto';
		destra.className = 'stretto'
	}
}

function ensureDestraLargo() {
	var sin = document.getElementById('sinistra');
	var sinAlt = document.getElementById('sinistra-alt');
	var destraBack = document.getElementById('destraBack');
	var destra = document.getElementById('destra');
	var appletDiv = document.getElementById('appletDiv');

	sin.className='largo';
	sinAlt.className='stretto';

	appletDiv.className='stretto';
	destraBack.className='stretto';
	destra.className = 'stretto'
}
