/* Visualizza messaggio */
function showMessage(message) {
  if (false) {
    var win = window.open('', 'GisMasterWebSystem');
    win.parent.SystemCore.showMessageSystem(message);
  }
}

/* (Public) Apertura della finestra GisMasterWebSystem */
function openGisMasterWebSystem(href, idCliente) {
	var win = openGisMasterWebSystemWinName();
	openGisMasterWebSystemSet(win, href, idCliente, '', '', '');
}

/* (Public) Posizionamento cartografico */
function geoPos(idCliente, selType, idItem, geoSel) {
	var win = openGisMasterWebSystemWinName();
	var needSet = false;
	try {
		if (win.parent.SystemCore.getIdCliente() == idCliente) {
			win.parent.SystemCore.geoPosSystem(selType, idItem, geoSel);
		} else {
			needSet = true;
		}
	} catch (e) {
		needSet = true;
	}
	
	if (needSet) {
		openGisMasterWebSystemSet(win, '', idCliente, selType, idItem, geoSel);
	}
}

/* (Public) Apertura semplice della finestra GisMasterWebSystem */
function openGisMasterWebSystemWin() {
  var win = window.open('', 'GisMasterWebSystem', 'width=800, height=600, resizable=no, scrollbars=yes, location=no, status=yes');
  win.focus();
  return win;
}

/* Settaggio del name della finestra GisMasterWebSystem */
function openGisMasterWebSystemWinName() {
  var win = openGisMasterWebSystemWin();
  if (!isWindowOpen(win)) {
    win.name = '';
  }
  return win;
}

/* Settaggio dei parametri della finestra GisMasterWebSystem */
function openGisMasterWebSystemSet(win, href, idCliente, selType, idItem, geoSel) {
	if (href.length == 0) {
		href = 'http://www.technicaldesign.it/GisMasterWeb/System/System.jsp';
	}
	
 	href += '?IdCliente=' + idCliente;
 	if ((idItem.length > 0) && (geoSel.length > 0)) {
		href += '&amp;SelType=' + selType;
		href += '&amp;IdItem=' + idItem;
		href += '&amp;GeoSel=' + geoSel;
	}
	win.location.href = href;
}

/* Restituisce true se la finestra win era gia' aperta */
function isWindowOpen(win) {
  var isOpen = true;
  try {
  	if (win.location.href == 'about:blank') { /* La finestra era chiusa (sullo stesso server o su un altro) */
      isOpen = false;
  	}
  } catch (e) {} /* Se si genera un errore usando win.location.href significa che si apre la finestra su un altro server e la finestra era aperta */
  return isOpen;
}

/* Apertura o attivazione di una finestra mediante nome */
function openWindow(windowName, href, width, height, mode, refreshHref) {
  var c;
  if (mode == "ry") {
	c = 'width=' + width + ', height=' + height + ', resizable=yes, scrollbars=yes, location=no, status=yes';
  } else
  if (mode == "rn") {
	c = 'width=' + width + ', height=' + height + ', resizable=no, scrollbars=yes, location=no, status=yes';
  } else {
	c = 'fullscreen=yes, resizable=yes, scrollbars=yes, location=no, status=yes';
  }

  /* Apre la finestra o restituisce l'handle della finestra se e' gia' aperta */
  var win = window.open('', windowName, c);
  win.focus();

  if (!isWindowOpen(win)) { /* La finestra era chiusa */
    win.location.href = href; /* Aggiorna il contenuto */
  } else { /* La finestra era aperta */
    if (refreshHref) { /* Deve aggiornare il contenuto */
      win.location.href = href;
    }
  }

  return win;
}

/* Inverte l'espansione di un ramo */
function toggleToolExp(elem) {
	var eExpG = document.getElementById(elem + '_ExpG');
	var eExpT = document.getElementById(elem + '_ExpT');
	var eDiv = document.getElementById(elem + '_Div');
	
	/* Imposta immagine del nodo e espansione del ramo */
	if (eExpG.src.indexOf("ToolExpOpened") >= 0) {
		eExpG.src = "../Files/Images/ToolExpClosed.gif";
		eExpT.innerHTML = "Apri dettagli";
		eDiv.style.display = "none";
	} else {
		eExpG.src = "../Files/Images/ToolExpOpened.gif";
		eExpT.innerHTML = "Chiudi dettagli";
		eDiv.style.display = "block";
	}
}

/* Converte un numero in una stringa formattandolo */
/* sFormat: es. '000' */
function formatNumber(n, sFormat) {
	var s = sFormat + n;
	return s.substring(s.length - sFormat.length, s.length);;
}

/* Seleziona il tab iTab-esimo */
function selectTab(pref, iTab, iMax) {
	for (var i = 0; i < iMax; i++) {
		var si = formatNumber(i, '000');
		if (i == iTab) {
			document.getElementById(pref + si).className = 'TabSelected';
			document.getElementById(pref + si + '_Div').style.display = 'block';
		} else {
			document.getElementById(pref + si).className = 'TabNotSelected';
			document.getElementById(pref + si + '_Div').style.display = 'none';
		}
	}
}

/* Imposta il foglio di stile */
function setActiveStyleSheet(title) 
{
  var es = document.getElementsByTagName("link");
  var i, e;

  for (i = 0; i < es.length; i++) {
    e = document.getElementsByTagName("link")[i];
    if ((e.getAttribute("rel").indexOf("style") != -1) && e.getAttribute("title")) {
      e.disabled = true;
      if (e.getAttribute("title") != title) {
        e.disabled = true;
      } else {
        e.disabled = false;
      }
    }
  }
}

/* Restituisce il foglio di stile attivo */
function getActiveStyleSheet() {
  var es = document.getElementsByTagName("link");
  var i, e;
  var title = "";

  for(i = 0; i < es.length; i++) {
    e = document.getElementsByTagName("link")[i];
    if((e.getAttribute("rel").indexOf("style") != -1) && e.getAttribute("title") && (!e.disabled)) {
      title = e.getAttribute("title");
    }
  }
  return title;
}

/* Imposta un cookie */
function setCookie(name, value, days) {
  var expires = "";

  if (days) {
    var date = new Date();
    date.setTime(date.getTime() + (days*24*60*60*1000));
    expires = "; expires=" + date.toGMTString();
  }
  document.cookie = name + "=" + value + expires + "; path=/";
}

/* Restituisce un cookie */
function getCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  var s = "";

  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) {
      s = c.substring(nameEQ.length, c.length);
    }
  }
  return s;
}

/* Imposta il foglio di stile al momento del load della pagina */
function loadStyle() {
  var s = getCookie("Style");
  if (	(s != 'GraphicNormal') && 
  		(s != 'GraphicBig') && 
  		(s != 'TextImgNormal') && 
  		(s != 'TextImgBig') && 
  		(s != 'TextNoiNormal') &&
  		(s != 'TextNoiBig')) {
  	s = 'GraphicNormal';
  }
  setActiveStyleSheet(s);
}

/* Memorizza il foglio di stile al momento dell'unload della pagina */
function unloadStyle() {
  var s = getActiveStyleSheet();
  setCookie("Style", s, 365);
}

