function JTool()
{
	this.width =640;
	this.height =480;
	this.nomePopup ="popup";
	this.nomeCookie ="contaWeb";

	this.ie=document.all;
	this.ns6=document.getElementById && !document.all;
	
	this.vetMesi =new Array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	this.vetDesMesi =new Array ("Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre");
	this.vetGiorni =new Array ("Domenica", "Lunedì", "Martedì", "Mercoledì", "Giovedi", "Venerdì", "Sabato");
	this.vetG =new Array ("Dom.", "Lun.", "Mar.", "Mer.", "Gio.", "Ven.", "Sab.");
	
	var d =new Date();

	this.gd =d.getDate();
	this.m =d.getMonth();

	this.gg =d.getDate();
	this.mm =d.getMonth() +1;
	this.yy =d.getFullYear();

	if (((this.yy % 4 ==0) && !(this.yy % 100 ==0)) || (this.yy % 400 ==0))
		this.vetMesi[1]++;

	this.data=(this.gg<10 ? "0"+this.gg : this.gg)+"/"+(this.mm <10 ? "0"+this.mm : this.mm)+"/"+this.yy;

	this.startData ="01/01/"+this.yy;
	this.endData ="31/12/"+this.yy;

	this.getWidth =function() {return this.width};
	this.getHeight =function() {return this.height};
	
	this.setWidth =function(w) {this.width =w};
	this.setHeight =function(h) {this.height =h};

	this.checkNumero =function(n)
	{
		ret =false;
		if (isNaN(n))
		{
			alert("Il dato introdotto non è un numero ");
			ret =true;
		}
		return ret;
	}

	this.checkDigit =function(e, obj, d)	//evento, oggetto, campo data S=si N=no
	{
		ret =false;
		switch(e.keyCode)
		{
			case 8 :		// back space
			case 9 :		// tasto tab
			case 37 :		// freccia a sinistra
			case 39 :		// freccia a destra
			case 46 :		// Tasto Canc
			case 48 :
			case 49 :
			case 50 :
			case 51 :
			case 52 :
			case 53 :
			case 54 :
			case 55 :
			case 56 :
			case 57 :
			case 96 :
			case 97 :
			case 98 :
			case 99 :
			case 100 :
			case 101 :
			case 102 :
			case 103 :
			case 104 :
			case 16 :			// tasto sfith
			case 105 : ret =true; break;
			case 109 : 			//Trattino del meno
			case 189 : ret =(d !="S" ? true : false);
				   if (obj.value.indexOf("-") >-1)
					ret =false;
					break;
			case 110 : 			//Punto
			//case 190 : ret =(d !="S" ? true : false);
			case 188 : ret =(d !="S" ? true : false);
				   s =obj.value;
				   if (s.indexOf(",") >-1)
					ret =false;
				   break;
			default  : break;
		}
		return ret;
	}

	this.getFocusData = function (s, obj)
	{
		newData =s.substr(0, 2)+s.substr(3, 2)+s.substr(6);
		obj.value=newData;
		obj.focus();
		obj.select();
	}

	this.lostFocusData = function (s, obj)
	{
		if (s.length !=8)
		{
			alert("La data non è corretta! (8 caratteri numerici)");
			obj.focus();
			return false;
		}
		else
		{
			d1 =new Date(parseInt(s.substr(4), 10), parseInt(s.substr(2, 2), 10) -1, parseInt(s.substr(0, 2), 10));
			gg =d1.getDate();
			mm =d1.getMonth() +1;
			yy =d1.getFullYear();
			
			newData=(gg<10 ? "0"+gg : gg)+"/"+(mm <10 ? "0"+mm : mm)+"/"+yy;
			obj.value=newData;
			return true;
		}
	}


	this.setBox =function(obj, testo, w, h, event, colore, filtro)
	{
		oggettoEvento =obj;
		switch(event.button)
		{
			case 0 :
			case 2 : X =event.clientX;
				 Y =event.clientY;
				 break;
			default : X =event.x;
				  Y =event.y;
				 break;
		}

		var box =document.getElementById(this.nomePopup);

		box.style.left=X+"px";
		box.style.top=Y+"px";
		box.style.width =w+"px";
		if (h !=0)
			box.style.height =h+"px";
		box.style.border ="black 1px solid";
		box.style.backgroundColor =(typeof(colore) =="undefined" ? "cyan" : colore);
		box.style.filter =(typeof(filtro) =="undefined" ? "alpha (opacity:80)" : filtro);
		box.innerHTML=testo;
		box.style.visibility="visible";
	}

	this.calcolaOffsetTop =function(obj)
	{
		var offset =0;
		while(obj)
		{
			offset +=obj["offsetTop"];
			obj =obj.offsetParent;
		}
		return offset;
	}
	
	this.closeBox =function()
	{
		var obj =document.getElementById(this.nomePopup);
		obj.style.border ="none";
		obj.style.visibility="hidden";
	}

	this.creaBox =function()
	{
		var box=document.createElement("div");
		box.setAttribute("id", this.nomePopup);
		box.style.position ="absolute";
		box.style.visibility ="hidden";
		document.body.appendChild(box);
	}

	this.verificaInput =function(s, val, obj)
	{
		var ret =false;
		if (s !="")
		{
			for (x =0; x <val.length; x++)
			{
				if (val.charAt(x) ==s)
				{
					ret =true;
					break;
				}
			}
			if (ret ==false)
			{
				alert("Valori non corretti!");
				obj.focus();
			}
		}
		else
		{
			//obj.focus();
			alert("Valore obbligatorio ("+val+")");
		}
		return ret;
	}

	this.arrotondaNumero =function(num, dec, mod)
	{
  		var div = Math.pow(10, dec);
  		switch (mod)
		{
    			case 'e':
    			case 'E': //eccesso
     				return Math.ceil(num * div) / div;
    			break;
    			case 'd':
    			case 'D': //difetto
     				return Math.floor(num * div) / div;
    			break;
    			case 't':
    			case 'T': //troncamento
     				return parseInt(num * div) / div;
    			break;
    			case 'a':
    			case 'A':
    			default: //arrotonda
     				return Math.round(num * div) / div;
	  	}
	}

	this.setCookie =function(azienda)	//evento, oggeto, campo data S=si N=no
	{
		var cd =new Date();
		cd.setDate(cd.getDate() +1); 
		document.cookie=this.nomeCookie+"="+escape(azienda)+";expires="+cd.toGMTString()+";";
	}

	this.getCookie =function()	//evento, oggeto, campo data S=si N=no
	{
		var codiceCookie = new Array();
		codiceCookie[0] ="";
		codiceCookie[1] ="Azienda non selezionata";

		var s_cook =document.cookie.indexOf(this.nomeCookie);
		if (s_cook != -1)
		{
			s_cook +=this.nomeCookie.length +1;
			var e_cook =document.cookie.indexOf(";", s_cook);

			if (e_cook == -1)
			{
				e_cook =document.cookie.length;
				codiceCookie =unescape(document.cookie.substring(s_cook, e_cook)).split("\t");
			}
			else	codiceCookie =unescape(document.cookie.substring(s_cook, e_cook)).split("\t");

			if (e_cook == -1)
				alert("Errore cookie!");
		}
		else	alert("Attenzione attivare i cookie.");
		return codiceCookie;
	}

	this.delCookie =function()	//evento, oggeto, campo data S=si N=no
	{
		document.cookie="contaWeb=;scadenza=Thu, 01-Jan-1970 00:00:01 GMT";
	}

	this.trim =function(snull)
	{
		snull =snull.replace(/^\s+|\s+$/, '');
		return snull;
	}

	this.creaBox();
}

var xmlDoc;

var classeLi ="";
var linkT ="";
var tipoMenu ="P";
var descAzi="";

function preparaMenu(codAzi, obj, tipo, gruppoMenu)
{  
      var pathFile =""; 
      xmlDoc =null;
      tipoMenu =tipo;
	
      if (tipo =="P")
      {
	  descAzi =gruppoMenu;
	  classeLi ="menuLi";
	  linkT ="linkT";
	  pathFile ="/contabilita/tabellaMenu.php";
	  
      }
      else
      {
	 classeLi ="";
	 linkT ="";
	 pathFile ="/contabilita/tabellaMenuLaterali.php?gru="+gruppoMenu;
      }

      if (window.ActiveXObject)
      {        
	  xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	  xmlDoc.onreadystatechange = function ()
          {
		if (xmlDoc.readyState == 4)
                     creaMenu(codAzi, obj);
	  };
      }
      else	if (document.implementation && document.implementation.createDocument)
      		{
			xmlDoc = document.implementation.createDocument("", "", null);
		  	xmlDoc.onload = function() {creaMenu(codAzi, obj); }
      		}
      		else
		{
	  		alert('Attenzione: Il tuo browser non supporta questo java_script');
	  		return;
      		}
	
	xmlDoc.async = false;
	xmlDoc.load(pathFile);
}

function creaMenu(codAzi, obj)
{
	//var param ="?azi="+codAzi+"&desc="+escape(descAzi);
	var param ="";

	//if (tipoMenu =="P")
    	//	param =param +"&desc="+escape(descAzi);
	
	if (xmlDoc ==null)
	{
		alert("L'oggetto xmlDoc non è stato creato");
        	return;
	}

	//if (xmlDoc.parseError.errorCode !=0)
	//   return;
    
	var xmlR =xmlDoc.getElementsByTagName("menu");
	if (xmlDoc ==null || xmlR ==null)
        	alert("Attenzione errore di programmazione");

	var nodo =null;
	var str ="";
	var link ="";

	for (x =0; x <xmlR.length; x++)
	{
		for (idx =0; idx <xmlR[x].childNodes.length; idx++)
		{
			nodo =xmlR[x].childNodes[idx];
                         
                        for (j =0; j <nodo.childNodes.length; j++)
			{
				switch(nodo.childNodes[j].nodeName)
                        	{	
					case "linkRef" : link =nodo.childNodes[j].firstChild.nodeValue;
				      			 break;
			      		case "nomeMenu" : s =nodo.childNodes[j].firstChild.nodeValue;
						  	  str =str+'<li class="'+classeLi+'"><a href="/contabilita/'+link+param+'" class="'+linkT+'">'+s+'</a></li>';
					          	if (tipoMenu =="L")
								str =str+"<br>";
							  break;
				}
			}
		}
        }
	document.getElementById(obj).innerHTML=str;
}


function leggiFileXml(pathFile, strFunction)
{
      if (window.ActiveXObject)
      {        
	  xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	  xmlDoc.onreadystatechange = function ()
          {
		if (xmlDoc.readyState == 4)
                     strFunction;
	  };
      }
      else	if (document.implementation && document.implementation.createDocument)
      		{
			xmlDoc = document.implementation.createDocument("", "", null);
		  	xmlDoc.onload = function(){strFunction;};
      		}
	
      		else
		{
	  		alert('Attenzione: Il tuo browser non supporta questo java_script');
	  		return;
      		}
	
	xmlDoc.async = false;
	xmlDoc.load(pathFile);
}

function setQueryString(docForm)
{
	var strSubmitContent = '';
	var formElem;
	var strLastElemName = '';

	for (i = 0; i < docForm.elements.length; i++)
        {
		formElem = docForm.elements[i];
		switch (formElem.type)
                {
			// campo Text, hidden elementi form
			case 'text':
			case 'hidden':
			case 'password':
			case 'textarea':
			case 'select-one':
				strSubmitContent += formElem.name + '=' + escape(formElem.value) + '&'
				break;

			// pulsante Radio
			case 'radio':
				if (formElem.checked)
                                {
					strSubmitContent += formElem.name + '=' + escape(formElem.value) + '&'
				}
				break;

			// Checkboxes
			case 'checkbox':
				if (formElem.checked) 
                                {
					// Scorro la check-box, con lo stesso nome
					if (formElem.name == strLastElemName)
                                        {
						// Strip alla fine ampersand se c'è nè uno
						if (strSubmitContent.lastIndexOf('&') == strSubmitContent.length-1)
                                                {
							strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
						}
						// Appendo i valori come virgola-delimitati alla stringa
						strSubmitContent += ',' + escape(formElem.value);
					}
					else
                                        {
						strSubmitContent += formElem.name + '=' + escape(formElem.value);
					}
					strSubmitContent += '&';
					strLastElemName = formElem.name;
				}
				break;
		}
	}
	strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
        return strSubmitContent;
}

var xmlHttpReq = null;

function creaXMLHttpRequest()
{
	// IE
	if (window.ActiveXObject)
        {
                xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
	else if (window.XMLHttpRequest)
        {
		// Mozilla/Safari
                xmlHttpReq = new XMLHttpRequest();
                //xmlHttpReq.overrideMimeType('text/xml');
        }
	if (xmlHttpReq ==null)
		alert("Errore creazione Request");
}

function xmlhttpPost(strURL, strSubmit, strFunction, method) 
{
	creaXMLHttpRequest()
        xmlHttpReq.open(method, strURL, true);

        xmlHttpReq.setRequestHeader('Content-Type',
		     'application/x-www-form-urlencoded');

        xmlHttpReq.onreadystatechange = function() 
        {
           if (xmlHttpReq.readyState == 4)
           {
                strResponse = xmlHttpReq.responseText;
                switch (xmlHttpReq.status) 
                {
                   // Pagina non trovata errore
                   case 404:
                           alert('Errore: Non trovato. La URL richiesta ' +
                                   strURL + ' non è stata trovata.');
                           break;
                   // Visualizza il risultato in una finestra per l'errore server-side
                   case 500:
                           handleErrFullPage(strResponse);
                           break;
                   case 200:
                           // Chiama JS alert per errori dedicati o messaggi di debug
                           if (strResponse.indexOf('Errore:') > -1 || strResponse.indexOf('Debug:') > -1) 
                           {
                                   alert(strResponse);
                           }
                           // Attiva la funzione passata come argomento per vedere i risultati
                           else
                           {
                                  strFunction(strResponse);                                                                    
                           }
			   break;
		   default:
			   alert('Errore: Non trovato. La URL richiesta ' +
                                   strURL + ' non è stata trovata.');
                           break;
                }
            }
        }
	if (method =='POST')
	        xmlHttpReq.send(strSubmit);
	else	xmlHttpReq.send(null);
}


function handleErrFullPage(strIn) 
{
        var errorWin;

        // Crea una nuova finestra e visualizza l'errore
        try 
        {
                errorWin = window.open('', 'errorWin');
                errorWin.document.body.innerHTML = strIn;
        }
        // Se i pop-up sono bloccati, si informa l'utente
        catch(e)
        {
                alert('Errore, ma il messaggio di errore non può essere' +
                        ' visualizzato perchè il programma blocca i pop-up.\n' +
                        'Abilita i pop-up per questo sito.');
        }
}

