/** (C) HTML.IT - insieme di funzioni ed oggetti utili per interagire con ajax */

/** FUNZIONI */

	// funzione per prendere un elemento con id univoco
		function prendiElementoDaId(id_elemento) {
			var elemento;
			if(document.getElementById)
				elemento = document.getElementById(id_elemento);
			else
				elemento = document.all[id_elemento];
			return elemento;
		};
	
	// funzione per assegnare un oggetto XMLHttpRequest
		function assegnaXMLHttpRequest() {
			var
				XHR = null,
				browserUtente = navigator.userAgent.toUpperCase();
			if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
				XHR = new XMLHttpRequest();
			else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0) {
				if(browserUtente.indexOf("MSIE 5") < 0)
					XHR = new ActiveXObject("Msxml2.XMLHTTP");
				else
					XHR = new ActiveXObject("Microsoft.XMLHTTP");
			}
			return XHR;
		};

/** OGGETTI / ARRAY */

	// oggetto di verifica stato
		var readyState = {
			INATTIVO:	0,
			INIZIALIZZATO:	1,
			RICHIESTA:	2,
			RISPOSTA:	3,
			COMPLETATO:	4
		};

/* SCRIPT AJAX */

// funzione di caricamento testo,
// accetta una stringa contenente
// il nome di un file da leggere

function caricaTool(basefile) {

  // variabili di funzione
  var
    // assegnazione oggetto XMLHttpRequest
    ajax = assegnaXMLHttpRequest(),
    // assegnazione elemento del documento
    elemento = prendiElementoDaId('tabz'),
    // risultato booleano di funzione
    usaLink = true;
  
  // se l'oggetto XMLHttpRequest non e' nullo
  if(ajax) {
    // il link al file non deve essere usato
    usaLink = false;

    // impostazione richiesta asincrona in GET
    // del file specificato
	
    ajax.open("get", '/includes/tools/tools_' + basefile + ".php", true);
	
    // rimozione dell'header "connection" come "keep alive"
    ajax.setRequestHeader("connection", "close");

    // impostazione controllo e stato della richiesta
    ajax.onreadystatechange = function() {
      
      // verifica dello stato
      if(ajax.readyState === readyState.COMPLETATO) {
        // verifica della risposta da parte del server
        if(ajax.status == 200)
        {
          // operazione avvenuta con successo
          elemento.innerHTML = ajax.responseText;
        }
        else {
          // errore di caricamento
          elemento.innerHTML = "Impossibile effettuare l'operazione richiesta.<br />";
          elemento.innerHTML += "Errore riscontrato: " + ajax.status;
        }
      } 
    }

    // invio richiesta
    ajax.send(null);
  }
   
  return usaLink;
} 

/** fine (C) HTML.IT - insieme di funzioni ed oggetti utili per interagire con ajax */

function ricerca_sitesearch() {
	var argomento = (self.document.forms['cerca'].termine.value);
	var url_ricerca = "";
	var returnString = "";
	for (var i = 0; i < argomento.length; i++) {
		c = argomento.charAt(i);
		if (c == " ") c = "+";
		returnString += c;
	}
	if (self.document.forms['cerca'].elements[1].checked)
	{
		document.siteSearch1.keyWords.value=argomento;
		if(document.siteSearch1.keyWords.value.length!=0){
			document.siteSearch1.submit();
		}else{
			alert("Inserire un termine di ricerca.");
		}
	} 
	else if (self.document.forms['cerca'].elements[2].checked)
	{
		document.googleSearch.q.value = argomento;
		if(document.googleSearch.q.value.length!=0){
			document.googleSearch.submit();
		}else{
			alert("Inserire un termine di ricerca.");
		}
	}
}

	function help(campo) {
		var msg;
		switch (campo) {
			case "data":
				msg = "Data dell'ultimo aumento: Indicare mese e anno\nin cui si e' ottenuto per l&#8217;ultima volta\nun aumento di stipendio (adeguamento contrattuale,\nscatto di anzianita', aumento di merito).\nNel caso in cui non siano mai stati riconosciuti aumenti,\nindicare la data corrispondente all&#8217;ingresso nel ruolo \/ posizione.";
			break;
		}
		alert(msg);
	}
	function calcRal() {
		var thisForm = this.document.CALC;
		if (thisForm.NMENS.options[thisForm.NMENS.options.selectedIndex].value != '')
			thisForm.RAL_ATT.value = parseInt(thisForm.RETR_MENS.value) * parseInt(thisForm.NMENS.options[thisForm.NMENS.options.selectedIndex].value);
		else
			thisForm.RAL_ATT.value = '';
	}
	function check() {
		var thisForm = this.document.CALC;
		if (checknumber(thisForm.RAL_ATT.value,'Totale annuo 2003')) {
			if (thisForm.ULT_AUM_M.options[thisForm.ULT_AUM_M.options.selectedIndex].value != '' && thisForm.ULT_AUM_A.options[thisForm.ULT_AUM_A.options.selectedIndex].value)
				return true;
			else {
				alert("Attenzione, compilare il campo 'Data ultimo aumento'");
				return false;
			}
		}
		else 
			return false;	 
	}
	function checknumber(val,nomeCampo){
		var x=val;
		var anum=/(^\d+$)/;
		if (anum.test(x))
			testresult=true;
		else{
			alert("Inserire un numero intero valido per il campo " + nomeCampo);
			testresult=false;
		}
		return (testresult);
	}
	
	
