function cambia_css(quale) 
{
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
	{
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title"))
		{
			a.disabled = true;
		    if(a.getAttribute("title") == arguments[0]) a.disabled = false;
		}
	}
}

function aggiungi_testo(argomenti)
{
	var br;
	var primo_figlio=document.body.firstChild;
	
	var ancora=document.createElement("a");
	ancora.setAttribute("name","ancora");
	
	var elemento=document.createElement("div");
	
	elemento.setAttribute("id","stampa");
	elemento.style.textAlign="center";
	elemento.style.borderBottom="dotted #666 1px";
	elemento.style.padding="5px";
	var titolo=document.createElement("h2");
	titolo.style.margin="3px";
	titolo.style.lineHeight="0px";
	var testo=document.createTextNode("Anteprima di stampa.");
	titolo.appendChild(testo);
	elemento.appendChild(titolo);
	br=document.createElement("br");
	elemento.appendChild(br);
	testo=document.createTextNode("Questo messaggio non verra stampato.");
	elemento.appendChild(testo);
	br=document.createElement("br");
	elemento.appendChild(br);
	br=document.createElement("br");
	elemento.appendChild(br);
	var link=document.createElement("a");
	link.setAttribute("class","interno");
	link.setAttribute("href","javascript:stampa_pagina('" + argomenti + "')");
	testo=document.createTextNode("Clicca qui per stampare.");
	link.appendChild(testo);
	elemento.appendChild(link);
	
	var contieni_ancora=document.createElement("div");
	contieni_ancora.setAttribute("id","ancora1");
	br=document.createElement("br");
	contieni_ancora.appendChild(ancora);
	contieni_ancora.appendChild(br);
	
	document.body.insertBefore(contieni_ancora,primo_figlio);
	document.body.insertBefore(elemento,primo_figlio);


}

function stampa_pagina(argomenti)
{
	window.print();
	
	var padre=document.getElementById("stampa").parentNode;
	padre.removeChild(document.getElementById("stampa"));
	var br=document.createElement("br");
	var elemento=document.createElement("div");
	elemento.setAttribute("id","stampa");
	elemento.style.textAlign="center";
	elemento.style.borderBottom="dotted #666 1px";
	elemento.style.padding="5px";
	var titolo=document.createElement("h2");
	titolo.style.margin="3px";
	titolo.style.lineHeight="0px";
	var testo=document.createTextNode("Stampa completata.");
	titolo.appendChild(testo);
	elemento.appendChild(titolo);
	elemento.appendChild(br);
	var link=document.createElement("a");
	link.setAttribute("class","interno");
	link.setAttribute("href","javascript:torna_indietro('" + argomenti +"')");
	testo=document.createTextNode("Torna indietro");
	link.appendChild(testo);
	elemento.appendChild(link);
	
	padre.insertBefore(elemento,padre.firstChild);
	var ancora=document.createElement("a");
	var contieni_ancora=document.createElement("div");
	contieni_ancora.setAttribute("id","ancora2");
	ancora.setAttribute("name","ancora");
	br=document.createElement("br");
	contieni_ancora.appendChild(ancora);
	contieni_ancora.appendChild(br);
	padre.insertBefore(contieni_ancora,elemento);
}

function stampa(argomenti)
{
	
	cambia_css('stampa');
	
	aggiungi_testo(argomenti);
	

}

function torna_indietro(argomenti)
{
	if (argomenti=='tutto') history.back();
	else
	{
		document.getElementById("ancora1").parentNode.removeChild(document.getElementById("ancora1"));
		document.getElementById("ancora2").parentNode.removeChild(document.getElementById("ancora2"));
		document.getElementById("stampa").parentNode.removeChild(document.getElementById("stampa"));
		cambia_css('default');
		
	}
}

