/////////////////// Utils /////////////////

function storeCookie(key, txt)
{
	parent.window.document.cookie = key+"="+txt;
	parent.window.document.expiration = null;
	parent.window.document.cookie.domain = null;
	parent.window.document.cookie.path = null;
}
function getCookie(key)
{
	var allcookies = parent.window.document.cookie;
	if (allcookies == "")
		return "";
	var start = allcookies.indexOf(key + '=');
	if (start == -1)
		return "";
	start += key.length +1;
	var end = allcookies.indexOf(';', start);
	if (end == -1)
		end = allcookies.length;
	return allcookies.substring(start, end);
}
function preloadImages() 
{
  var d = document;
	if (d.images) { 
		if (!d.MM_p)
			d.MM_p = new Array();
		
    var i, j = d.MM_p.length, a = preloadImages.arguments;
		for (i = 0; i < a.length; i++)
			if (a[i].indexOf("#") != 0)
			{
				d.MM_p[j] = new Image;
				d.MM_p[j++].src = a[i];
			}
	}
}
function swapImgRestore() 
{
  var i, x, a = document.MM_sr;
	for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++)
		x.src = x.oSrc;
}
function findObj(n, d) 
{
  var p,i,x;
	if (!d)
		d = document;
	if ((p = n.indexOf("?")) > 0 && parent.frames.length)
	{
    d = parent.frames[n.substring(p+1)].document;
		n = n.substring(0,p);
	}
  if (!(x = d[n]) && d.all)
		x = d.all[n];
	for (i = 0; !x && i < d.forms.length; i++)
		x = d.forms[i][n];
  for (i = 0; !x && d.layers && i < d.layers.length; i++)
		x = findObj(n, d.layers[i].document);
  if (!x && d.getElementById)
		x = d.getElementById(n);
	return x;
}
function swapImage() 
{
  var i, j = 0, x, a = swapImage.arguments;
	
	document.MM_sr = new Array;
	for (i = 0; i < (a.length-2); i += 3) 
	{
		if ((x = findObj(a[i])) != null) 
		{
			document.MM_sr[j++] = x;
			if (!x.oSrc)
				x.oSrc = x.src;
			x.src = a[i+2];
		}
	}
}
// Limita il testo di una textarea al valore specificato
function textAreaLimit(obj, maxlen)
{
	var p=obj.value;
	if (p.length > maxlen)
		obj.value = obj.value.substr(0, maxlen+1);
	return;
}
// Max dieci decimali
function formatDbl(valore, decimali)
{
	valore=getCleanedDbl(valore);
	valore=Math.round(Math.pow(10,decimali)*(valore-0))+"";
	if (decimali > 0) {
		if (valore == "0")
			valore=valore+("00000000000000000").substr(0,decimali);
		else
			valore="00000000000000000"+valore;
		valore=(valore.substr(0,valore.length-decimali)-0)+","+valore.substr(valore.length-decimali,decimali);
	}
	return valore;
}
// Max dieci decimali
function signedFormatDbl(valore, decimali)
{
	var segno = "";
	if (valore.substr(0,1) == "-")
		 segno = "-";
	valore=getCleanedDbl(valore);
	valore=Math.round(Math.pow(10,decimali)*(valore-0))+"";
	if (decimali > 0) {
		if (valore == "0")
			valore=valore+("00000000000000000").substr(0,decimali);
		else
			valore="00000000000000000"+valore;
		valore=(valore.substr(0,valore.length-decimali)-0)+","+valore.substr(valore.length-decimali,decimali);
	}
	valore=segno+valore;
	return valore;
}
// Max dieci decimali
function formatDblDot(valore, decimali)
{
	valore=formatDbl(valore, decimali);
	return valore.replace(",",".");
}
// Max dieci decimali
function roundDbl(valore, decimali) 
{
  cifra = valore-0;
  cifra *= Math.pow(10,decimali);
  cifra = Math.round(cifra);
  cifra /= Math.pow(10,decimali); 
	return cifra-0;
}

function apriGlossario(chiave)
{
  var wh = 220;
  var ww = 500;
  var wxpos = (screen.availWidth - ww)/2;
  var wypos = (screen.availHeight - wh)/2;
  var w = window.open("/corsi/glossario.php?chiave="+chiave, "Glossario", "width="+ww+",height="+wh+",statusbar=no,z-lock=yes,status=no,resizable=no");
  w.moveTo(wxpos, wypos);
  w.focus();
}
