//Calcola l'ascissa del punto sulla pagina in cui si è effettuata la chiamata alla funzione
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}



//Calcola l'ordinata del punto sulla pagina in cui si è effettuata la chiamata alla funzione
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}



///////////////////////////////////////////////////////////////////////////
//
//	I N D I C A T O R E    D I    C A R I C A M E N T O

//Mostra l'indicatore di caricamento per chiamate ajax
function mostra_indicatore() {
	if (document.getElementById('gif_indicatore')) nascondi_indicatore();
	var gif_indicatore = document.createElement('img');
	var indicatore = document.getElementById('indicatore');
	gif_indicatore.src = '/grafica/intranet/indicator.gif';
	gif_indicatore.id = 'gif_indicatore';
	gif_indicatore.setAttribute('border',0);
	indicatore.appendChild(gif_indicatore);
}



//Nasconde l'indicatore di caricamento per chiamate ajax
function nascondi_indicatore() {
	if (document.getElementById('gif_indicatore')) {
		var indicatore = document.getElementById('indicatore');
		var gif_indicatore = document.getElementById('gif_indicatore');
		indicatore.removeChild(gif_indicatore);
	}
}



///////////////////////////////////////////////////////////////////////////
//
//   I L    M I T I C O    L O A D    C O N T E N T

//LoadContent
function loadContent(file) {
	//mostra_indicatore();
	var head = document.getElementsByTagName('head').item(0);
	var scriptTag = document.getElementById('loadScript');
	if(scriptTag) head.removeChild(scriptTag);
	script = document.createElement('script');
	var ranNum= Math.round(Math.random()*Math.random()*1000);
	script.src = file + "&random=" + ranNum;
	script.type = 'text/javascript';
	script.id = 'loadScript';
	head.appendChild(script);
}


///////////////////////////////////////////////////////////////////////////
//
//   G R A N D E Z Z A    D E L L A    F I N E S T R A

//Restituisce un array con larghezza ed altezza della finestra del browser
function getWindowSize() {
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	arrayPageSize = new Array(windowWidth,windowHeight)
	return arrayPageSize;
}

// Returns array with page width, height and window width, height
function getPageSize(){

     var xScroll, yScroll;

     if (window.innerHeight && window.scrollMaxY) {
          xScroll = document.body.scrollWidth;
          yScroll = window.innerHeight + window.scrollMaxY;
     } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
          xScroll = document.body.scrollWidth;
          yScroll = document.body.scrollHeight;
     } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
          xScroll = document.body.offsetWidth;
          yScroll = document.body.offsetHeight;
     }

     var windowWidth, windowHeight;
     if (self.innerHeight) {     // all except Explorer
          windowWidth = self.innerWidth;
          windowHeight = self.innerHeight;
     } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
          windowWidth = document.documentElement.clientWidth;
          windowHeight = document.documentElement.clientHeight;
     } else if (document.body) { // other Explorers
          windowWidth = document.body.clientWidth;
          windowHeight = document.body.clientHeight;
     }

     // for small pages with total height less then height of the viewport
     if(yScroll < windowHeight){
          pageHeight = windowHeight;
     } else {
          pageHeight = yScroll;
     }

     // for small pages with total width less then width of the viewport
     if(xScroll < windowWidth){
          pageWidth = windowWidth;
     } else {
          pageWidth = xScroll;
     }

     arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
     return arrayPageSize;
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll)
	return arrayPageScroll;
}

///////////////////////////////////////////////////////////////////////////
//
//   F U N Z I O N I    D I    A H A H

//Funzione di invio di un form con AHAH
function submit(FILE, METHOD,target,form){
//	var num = document.forms[0].elements.length;
	var num = document.getElementById(form).elements.length;
	var url = "";

	//radio button
	var j = 0;
	var a = 0;
	var radio_buttons = new Array();
	var nome_buttons = new Array();
//	var the_form = window.document.forms[0];
	var the_form = document.getElementById(form);
	for(var i=0; i< the_form.length; i++){
		var temp = the_form.elements[i].type;
		if ( (temp == "radio") && ( the_form.elements[i].checked) ) {
			nome_buttons[a] = the_form.elements[i].name;
			radio_buttons[j] = the_form.elements[i].value;
			j++;
			a++;
		}
	}
	for(var k = 0; k < radio_buttons.length; k++) {
		url += nome_buttons[k] + "=" + radio_buttons[k] + "&";
	}
	//checkbox
	var j = 0;
	var a = 0;
	var check_buttons = new Array();
	var nome_buttons = new Array();
//	var the_form = window.document.forms[0];
	var the_form = document.getElementById(form);
	for(var i=0; i< the_form.length; i++){
		var temp = the_form.elements[i].type;
		if ( (temp == "checkbox") && ( the_form.elements[i].checked) ) {
			nome_buttons[a] = the_form.elements[i].name;
			check_buttons[j] = the_form.elements[i].value;
			j++;
			a++;
		}
	}
	for(var k = 0; k < check_buttons.length; k++) {
		url += nome_buttons[k] + "=" + check_buttons[k] + "&";
	}

	//select (multiselect)
	var j = 0;
	var a = 0;
	var multiselect = new Array();
	var nome_select = new Array();
//	var the_form = window.document.forms[0];
	var the_form = document.getElementById(form);
	for(var i=0; i< the_form.length; i++){
		var temp = the_form.elements[i].type;
		if ( the_form.elements[i].options != undefined )
		{
			for(var y=0; y < the_form.elements[i].options.length; y++)
			{
				if(the_form.elements[i].options[y].selected)
				{
					nome_select[a] = the_form.elements[i].name;
					multiselect[j] = the_form.elements[i].options[y].value;
					j++;
					a++;
				}
			}
		}
	}
	for(var k = 0; k < multiselect.length; k++) {
		url += nome_select[k] + "=" + multiselect[k] + "&";
	}

	for (var i = 0; i < num; i++){

//		var chiave = document.forms[0].elements[i].name;
//		var valore = document.forms[0].elements[i].value;
//		var tipo = document.forms[0].elements[i].type;
		var chiave = document.getElementById(form).elements[i].name;
		var valore = document.getElementById(form).elements[i].value;
		//if (chiave=='abstract') alert(valore);
		valore = escape(valore);
		valore = valore.replace(/\+/gi, '%2B');
		//if (chiave=='abstract') alert(valore);
		//valore = valore.replace(/&/g,"__#and#__");
//		valore = valore.replace(/(\w|\W)/g,
//			   function (s, n) {
//			     c=n.charCodeAt(0);
//			     return (c==38?'&amp;':(c>127?'&#'+c+';':n));
//			   }
//			);
		var tipo = document.getElementById(form).elements[i].type;

		if ( (tipo == "submit") || (tipo == "radio") || (tipo == "checkbox") || (document.getElementById(form).elements[i].options != undefined) ){}
		else {
			url += chiave + "=" + valore + "&";
		}
	}
	var parameters = url;
	url = FILE + "?" + url;
	if (METHOD == undefined) { METHOD = "GET"; 	}
	if (METHOD == "GET") { ahah(url, target, '', METHOD); }
	else { ahah(FILE, target, '', METHOD, parameters); }
}



//AHAH
function ahah(url, target, delay, method, parameters) {

  if (method == undefined) {
	  //document.getElementById(target).innerHTML = 'caricamento in corso...';
	  var req;

	  mostra_indicatore();
	  if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	  } else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	  }
	  if (req) {
		req.onreadystatechange = function() {
			ahahDone(url, target, req, delay, method, parameters);
		};
		req.open("GET", url, true);
		req.send("");
	  }
  }
  if ( (method == "GET") || (method == "get") )
  {
	  //document.getElementById(target).innerHTML = 'caricamento in corso...';
	  mostra_indicatore();
	  if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	  } else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	  }
	  if (req) {
		req.onreadystatechange = function() {
			ahahDone(url, target, req, delay, method, parameters);
		};
		req.open(method, url, true);
		req.send("");
	  }
  }

  if ( (method == "POST") || (method == "post") )
  {
	  //document.getElementById(target).innerHTML = 'caricamento in corso...';
	  mostra_indicatore();
	  if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	  } else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	  }
	  if (req) {
		req.onreadystatechange = function() {
			ahahDone(url, target, req, delay, method, parameters);
		};
		req.open(method, url, true);
		req.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
//		req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		req.send(parameters);
	  }
  }
}



//AHAH
function ahahDone(url, target, req, delay, method, parameters) {
  if (req.readyState == 4)
  {
  	var div = document.getElementById(target);
    if (req.status == 200)
    {
    	div.innerHTML = req.responseText;
    	execJS(div);
    }
    else
    {
      div.innerHTML="ahah error:\n"+req.statusText;
    }
  	nascondi_indicatore();
  }
}


//Esegue gli script presenti nell'html scritto da ahah; prende come parametro il
//getelementbyid del div contenente l'html generato

function execJS(node){
	var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
	var bOpera = (navigator.userAgent.indexOf('Opera') != -1);
	var bMoz = (navigator.appName == 'Netscape');

	var st = node.getElementsByTagName('script');
	var strExec;

	for(var i=0;i<st.length;i++){
		if(bSaf){
			strExec = st[i].innerHTML;
		}else if(bOpera){
			strExec = st[i].text;
		}else if(bMoz){
			strExec = st[i].textContent;
		}else{
			strExec = st[i].innerHTML;
		}

		try{
			eval(strExec);
		}catch(e){
			//alert(i);
			alert(e);
		}
	}
}

///////////////////////////////////////////////////////////////////////////
//
//	Funzione per la richiesta di cancellazione
//	Se viene passato anche l'id allora viene fatta una richiesta tramite ahah
function cancella(messaggio, url, id)
{
	if(confirm(messaggio))
	{
		if(!id)
			location.href = url;
		else
		{
			if(id == 'loadContent')
				loadContent(url);
			else
				ahah(url, id);
		}
	}
}

function display(id, inline)
{
	var div = document.getElementById(id);
	if(div.style.display != 'none')
		div.style.display = 'none';
	else
		if(inline)
			div.style.display = 'inline';
		else
			div.style.display = 'block';
}

function abilita(campi)
{
	for(var k = 0; k < campi.length; k++)
	{
		var variabile = document.getElementById(campi[k]);
		variabile.disabled = false;
	}
}

function disabilita(campi)
{
	for(var k = 0; k < campi.length; k++)
	{
		var variabile = document.getElementById(campi[k]);
		variabile.disabled = true;
	}
}



function svuota(form)
{
	var num = document.getElementById(form).elements.length;

	for (var i = 0; i < num; i++)
	{
		var element = document.getElementById(form).elements[i];
		if(element.type!='submit' && element.type!='button')
			element.value = '';
	}
}

function mostra_div(id) {
	var div = document.getElementById(id);
	div.style.display = (div.style.display == "block") ? "none" : "block";
}