//---------------------------COSTANTI----------------------------------------//

//Posizione del file audio punto asp
_audio="/Ambienti/audio.asp"; 
_pulsantiera="/Ambienti/Didattica/images/pulsantiera/";

//----------------------------------------------------------------------------//

//Sistema la formattazione all'inizio
function init()
{
  try
  {         
    resize();

    var p=new Object();
    p["METAMBIENTE"]=parseInt(document.getElementById("STD_ID_METAMBIENTE").value);   
    if(isNaN(p["METAMBIENTE"])) p["METAMBIENTE"]="";  //serve per le pagine dove per qualche motivo non viene settato il metambiente
    ajaxSend(p,"/Ambienti/SetMetambiente.asp",true);
       
  }catch(e){}      
}

window.onresize=resize;

function resize()
{
    var altezzaFinestra;
    if(window.innerHeight) altezzaFinestra=window.innerHeight;
    else if (document.documentElement.clientHeight != 0) altezzaFinestra=document.documentElement.clientHeight;
    else if (document.body.clientHeight) altezzaFinestra=document.body.clientHeight;
    else return;      
    
    var tabellaesterna=document.getElementsByTagName("table")[0];
    var righe=tabellaesterna.rows;
    var menuh=righe[0];
    var partecentrale=righe[1];
    var footbar=righe[2];

    //Se la pagina è più piccola, stop
    if(altezzaFinestra < (menuh.offsetHeight+partecentrale.offsetHeight+footbar.offsetHeight)) 
    { 
      document.body.style.overflow="auto"; //se prima è già stato modificato 
      return;
    }
    
    var altezza=altezzaFinestra-28-footbar.offsetHeight-menuh.offsetHeight;
    
    var zonalateraledestra=document.getElementById('zona_laterale_destra');
    //if(zonalateraledestra) altezza-=30;
    
    var metambiente=null;
    if(document.getElementById("STD_ID_METAMBIENTE")) metambiente = document.getElementById("STD_ID_METAMBIENTE").value;
    
    switch(metambiente)
      {       
        case "0": altezza+=26; break;
        case "3": altezza+=26; break;       
        case "7": if(!zonalateraledestra) altezza+=26; else altezza-=25; break;
        case "9": altezza+=28; break;
        case "11": altezza-=6; break;
      }  
    
    
    partecentrale.style.height=altezza+"px";
    
    var altezzaScheda=altezza;
    if(document.getElementById("boxComandiDettaglio"))
      altezzaScheda-=document.getElementById("boxComandiDettaglio").offsetHeight; 
   // altezza-=10;
    if(document.getElementById('scheda') && metambiente !="7") document.getElementById('scheda').style.height=altezzaScheda+"px";
    if(zonalateraledestra) zonalateraledestra.style.height=altezza-12+"px";
    if(document.getElementById('Menu_VSx'))document.getElementById('Menu_VSx').style.height=altezza+"px";
    if(document.getElementById('Menu_DSx'))document.getElementById('Menu_DSx').style.height=altezza+"px";
    
    try
    {
      if(document.getElementById("contenuto_centrale").offsetHeight<document.getElementById("scheda").offsetHeight+document.getElementById("boxComandiDettaglio").offsetHeight+15)
        document.getElementById("scheda").style.height=parseInt(document.getElementById("scheda").style.height)+10+"px";
    } catch(e){}
    
    document.body.style.height=altezzaFinestra+"px";  
    document.body.style.overflow="hidden"; //elimino le barre di scorrimento, tanto sono sicuro che ci sta  
}

function resetSize()
{
  document.body.style.overflow="auto";
  
  var tabellaesterna=document.getElementsByTagName("table")[0];
  var righe=tabellaesterna.rows;
  var partecentrale=righe[1];
  
  partecentrale.style.height="";
  if(document.getElementById('scheda'))document.getElementById('scheda').style.height="";
  if(document.getElementById("zona_laterale_destra")) document.getElementById("zona_laterale_destra").style.height="";
  if(document.getElementById('Menu_VSx'))document.getElementById('Menu_VSx').style.height="";
  if(document.getElementById('Menu_DSx'))document.getElementById('Menu_DSx').style.height="";
  
  
}

//---------------------------------------------------------------------------//

//Apre la pagina url in una nuova finestra, stampa e la chiude
function DialogPrint(url) 
{
  var dlg = window.open(url, "hadialog","toolbar=no,menubar=no,personalbar=no,scrollbars=yes,resizable=yes,modal=yes,dependable=yes,width=800,height=600");
  return true	
}

function eliminaAllegato(id)
{
	if(!confirm("Eliminare l'allegato?")) return;
	window.open("/Ambienti/NodoCms/EliminaAllegato.asp?ID="+id,""); 
	location.reload();
}

//---------------------------------------------------------------------------//

var _audiopopup=null;

function play(audio)	
{
	if(_audiopopup && _audiopopup.parentNode) _audiopopup.parentNode.removeChild(_audiopopup);
	_audiopopup=document.createElement("iframe");
	_audiopopup.style.visibility="hidden";
	_audiopopup.style.position="absolute";
	_audiopopup.style.top=-1000;
	_audiopopup.style.left=-1000;
	_audiopopup.style.width=0;
	_audiopopup.style.height=0;
	document.body.appendChild(_audiopopup);
	audio=audio.replace(/&/g,"anastasisanastasis");
	_audiopopup.contentWindow.location=_audio+"?FILE="+audio;
}

//---------------------------------------------------------------------------//

function glossario(parola)
{
  window.location="/main.asp?PAGE=/Ambienti/Glossario.asp&parola="+parola;
}


//-------------------------PULSANTIERA--------------------------------------//
function disactivate(imName)
{
  try 
  {
    var img = document.getElementById(imName);
  	img.src = _pulsantiera+imName+"Dis.gif";	
  	document.getElementById(imName+"_status").value="0";
  	document.getElementById(imName+"_link").href="#";
	}catch(e) {}
}

function activate(imName)
{
	try 
  {
    var img = document.getElementById(imName);
  	img.src = _pulsantiera+imName+".gif";
  	document.getElementById(imName+"_status").value="1";
  }catch(e) {}
}

function check(imName,active)
{
  try { if(active!="1") disactivate(imName); }
  catch(e) {}
}	

function changeImage(imName,active)	
{
	try 
  {
    if(active!="1") return;
  	var img = document.getElementById(imName);
  	img.src = _pulsantiera+imName+"Att.gif";	
	}catch(e) {}
}

function restoreImage(imName,active)	
{
  try 
  {
  	if(active!="1") return;
  	var img = document.getElementById(imName);	
  	img.src = _pulsantiera+imName+".gif";
	}catch(e) {}
}

  // stampa contenuto elemnto 'contenuto'
function stampa(cosa)	{
	var header="<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='it' lang='it'>" +
		"<head><title>Anteprima di stampa</title>" +
		"<link href='/_Themes/Print.css' rel='stylesheet' type='text/css' />" +
//    "<link href='/_Themes/Common.css' rel='stylesheet' type='text/css' />" +
//    "<link href='/_Themes/Theme_7/Theme.css' rel='stylesheet' type='text/css' />" +
//    "<link href='/_Themes/Theme_11/Theme.css' rel='stylesheet' type='text/css' />" +
//    "<link href='/_Themes/Theme_5/Theme.css' rel='stylesheet' type='text/css' />" +
//    "<link href='/_Themes/Theme_8/Theme.css' rel='stylesheet' type='text/css' />" +
    "</head>" +
    "<body>" +
    "<div id='da_stampare'>"
	var w=window.open("","STAMPA","toolbar=no,menubar=yes,personalbar=no,width=640,height=480,resizable=yes,modal=yes,dependable=yes");
	var sc=document.getElementById("scheda");
	var h;
	if(sc)
  {
    h=sc.style.height;
	 sc.style.height="";
	}
	w.document.write(header);
	if(!cosa) cosa="contenuto";
	w.document.write(document.getElementById(cosa).innerHTML);
	w.document.write("</div></body></html>");
	w.document.close();
	if(sc) sc.style.height=h;
	setTimeout(function(){w.print(); /*w.close();*/},1500);      	
} 

var old_width_centrale="64%";
var old_width_destra="28%";
var old_height="";
var old_background="#F3EFE7";		
var old_class="";
var _qualcuno_aperto=false;

function hideAndShow(id) {

	 //se c'è già
  if (document.getElementById("anastasis_hideShow"+id).className=="aperto")
  {
    _qualcuno_aperto=false;
    document.getElementById("anastasis_hideShow"+id).className="NoDisplay";
    document.getElementById('titoloContenitoreCMS_'+id).className=old_class;
    document.getElementById('postoTitoloContenitoreCMS').innerHTML="";
    document.getElementById('img_chiudi').innerHTML="";
    try 
    {      
      document.getElementById("contenuto_centrale").style.width=old_width_centrale;
      document.getElementById("zona_laterale_destra").style.width=old_width_destra;
      document.getElementById("zona_laterale_destra").style.backgroundColor=old_background;
      document.getElementById("zona_laterale_destra").style.height = old_height;
  /*    document.getElementById("zona_laterale_destra").style.height="";*/
    } catch(e){alert(e)}
    
    var divs=document.getElementsByTagName('div');
    for (var i in divs) 
    {
      if (divs[i].id && divs[i].id.match('contenitoreCMS_'))
       divs[i].className='contenitoreCMS';
    }
  }
    //se non c'è
  else  
  {    
    var divs=document.getElementsByTagName('div');
    for (var i in divs) 
    {
     if (divs[i].id && divs[i].id.match('anastasis_hideShow'))
       divs[i].className='NoDisplay';
      if (divs[i].id && divs[i].id.match('contenitoreCMS_'))
       divs[i].className='NoDisplay';
    }    	 
    try 
    {
      if (!_qualcuno_aperto)
      {
        old_width_centrale=document.getElementById("contenuto_centrale").style.width;
        old_width_destra=document.getElementById("zona_laterale_destra").style.width;
        old_background=document.getElementById("zona_laterale_destra").style.backgroundColor;
        old_class=document.getElementById('titoloContenitoreCMS_'+id).className;
      }
      
      var metambiente=0;
      if(document.getElementById("STD_ID_METAMBIENTE")) metambiente = document.getElementById("STD_ID_METAMBIENTE").value;
      var chiudi="/images/chiudi.gif"     
      if (metambiente==16 || metambiente==18) chiudi="/images/chiudi_bianco.gif";
      
      document.getElementById('anastasis_hideShow'+id).className="aperto";
      document.getElementById('postoTitoloContenitoreCMS').innerHTML=document.getElementById('titoloContenitoreCMS_'+id).innerHTML
      document.getElementById('img_chiudi').innerHTML='<a href="#" style="border: 0px;" title="chiudi" onclick="hideAndShow(\''+id+'\')" onkeypress="hideAndShow(\''+'\')"><img src="'+chiudi+'" alt="chiudi" /></a>';      
      document.getElementById('titoloContenitoreCMS_'+id).className="NoDisplay";    
      document.getElementById('contenitoreCMS_'+id).className="contenitoreCMS";
    
      document.getElementById("contenuto_centrale").style.width="63%";
      document.getElementById("zona_laterale_destra").style.width="28%";
      old_height = document.getElementById("zona_laterale_destra").style.height;
      if(document.getElementById("contenuto_centrale").offsetHeight>document.getElementById("zona_laterale_destra").offsetHeight)
        document.getElementById("zona_laterale_destra").style.height=document.getElementById("contenuto_centrale").offsetHeight+"px";                   
      document.getElementById("zona_laterale_destra").style.backgroundColor="#F3EFE7";
    } catch(e){alert(e)}
    _qualcuno_aperto=true;
  }  
  resetSize();
  resize();
  return false;
}

//----------------------------------------------------------------------------//
function ControllaDatiNewsLetter(theForm)
{
  if (!theForm.email.value)
  {
    alert("Occorre inserire il campo  \"E-mail\" .");
    theForm.email.focus();
    return false;
  }
  else
  {
    if (!theForm.email.value.match("@") || !theForm.email.value.match(/\./)) 
    {
		alert("Controlla la Email che hai digitato !!!");
		theForm.email.focus();
		return false;	
    }
  }  
  return true;
}	

//------------------------COMUNICAZIONI---------------------------------------//

_xmlHttp=null;

//Crea un nuov oggetto xmlHTTP (va rifatto ogni volta)
function ajaxResetSender()
{
try { _xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");}         //IE nuovi
    catch (e) 
    { 
      try { _xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }    //IE vecchi
      catch (E) 
      {
        try
        {
        _xmlHttp=new XMLHttpRequest();                             //Mozilla
        }
        catch(err){} 
      }                        
    }
}

//Invio sincrono o asincrono di dati via XMLHttp
//Parametri:
// - array param: param[id]=contenuto, dove id è l'id che avrà nella queryString
// - receiver: l'indirizzo a cui mandarlo
// - async: asincrono?
//Restituisce l'output della pagina receiver sotto forma di testo (se la richiesta è sincrona)

function ajaxSend(param,receiver,async,method)
{
  try
  {             
    ajaxResetSender();
    if(method=="GET")
    {
      _xmlHttp.open("GET",receiver,async);    //(method, url, async)
      //_xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    }
    else //di default lo fa POST
    {
      _xmlHttp.open("POST",receiver,async);    //(method, url, async)
      _xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    }
    var qs="";
    var tmp;
    var primo=false;
    for(var par in param)
    {
      if(!primo) primo=true; else qs+="&";    
      tmp=encodeURIComponent(param[par]); //Encode con tutti i % 
      //tmp=tmp.replace(/&/g,"%26");   //Sostituisce gli &, perrhcè encodeURI non lo fa      
      qs+=par+"="+tmp; //Attenzione alla sostituzione degli &!!!
    }    
    _xmlHttp.send(qs);
    
    if(!async)
    {
    var response = _xmlHttp.responseText;
    _xmlHttp=null;
    return response;
    }
  }catch(err){alert(translate("An error occured during saving (AJAX) \n:"+err))}
}

/************************** gestione font size con ingrandimento ********/


function creaCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function leggiCookie(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;
}

// attenzione: il cookie resta oltre la sessione!!!
function setDimensioneCarattere(value) {
document.getElementsByTagName('body').item(0).style['fontSize'] = value;
creaCookie('fontsize',value,365);
resetSize();
resize();
}

function carattereNormale() {
setDimensioneCarattere('100%');
}

function carattereIngrandito1() {
setDimensioneCarattere('110%');
}

function carattereIngrandito2() {
setDimensioneCarattere('125%');
}

window.onload = function(e) {
  var cookie = leggiCookie("fontsize");
  if (cookie) {
   setDimensioneCarattere(cookie);
   }
  else {
    carattereNormale();
  }
}

