
var dataBlank = '  /  /';
var oraBlank = '  :';

function apriCalendario(gfPop,idElemento,posizione,pageMode){
  if(posizione=="b")
		var pos=new Array(0,0,false);
  else if(posizione=="t")
  	var pos=new Array(0,-180,false);
  else if(posizione=="l")
  	var pos=new Array(-100,0,false);
  else if(posizione=="r")
  	var pos=new Array(120,-20,false);
  else if(posizione=="rt")
  	var pos=new Array(120,-100,false);
  else
  	var pos=new Array(0,0,false);

	if(document.getElementById(idElemento).disabled)return false;
	if(gfPop) gfPop.fPopCalendar(document.getElementById(idElemento),null,null,null,pos);
	return false;
}

function apriCalendario2(idElemento){
    var mioitem = document.getElementById(idElemento);
    if (mioitem.readOnly!=true&&mioitem.disabled!=true){
	    if (idElemento.indexOf("[")>-1&&idElemento.indexOf("[")>-1){
	      var elemento = idElemento.substring(0,idElemento.indexOf("["));
	      var indice = idElemento.substring(idElemento.indexOf("["));
	      cal.select(document.getElementById(idElemento),elemento+"_a_cal"+indice,'dd/MM/yyyy');
	    } else {
	      cal.select(document.getElementById(idElemento),idElemento+"_a_cal",'dd/MM/yyyy');
	    }
	}
    return false;
}

function chiudiCalendario(e){
      if (!self.gfPop) return;
      var n=!e?self.event.srcElement.name:e.target.name;
      if (document.layers) {
        with (gfPop) var l=pageX, t=pageY, r=l+clip.width, b=t+clip.height;
      	if (n!="popcal"&&(e.pageX>r||e.pageX<l||e.pageY>b||e.pageY<t))
      	gfPop.fHideCal();
      	return routeEvent(e);
      } else if (n!="popcal") gfPop.fHideCal();
}

function postback(idForm) {

	var form;
	//form = document.getElementById(idForm);
	form = document.all(idForm);
	if (form!=null)	form.submit();

}

function postbackAct(idForm, idAction) {

	var form;
	//form = document.getElementById(idForm);
	form = document.all(idForm);
	if (form!=null)	{
		form.action = idAction;
		form.submit();
	}

}



  var req;
  var which;
  var idCmb;

  function getClasseElemento(url,id,livello) {

	//alert('uei');

	var cmb
	var selLiv1
	var selLiv2
	var selLiv3

	//alert('getClasseElemento - START');

	selLiv1 = '';
	selLiv2 = '';
	selLiv3 = '';

	cmb = document.getElementById('cmbLivello1');
	selLiv1 = cmb.value;
	cmb = document.getElementById('cmbLivello2');
	selLiv2 = cmb.value;
	cmb = document.getElementById('cmbLivello3');
	selLiv3 = cmb.value;

	//alert('Liv1 : ' + selLiv1);
	//alert('Liv2 : ' + selLiv2);
	//alert('Liv3 : ' + selLiv3);

	url = url + "&selLiv1=" + selLiv1 + "&selLiv2=" + selLiv2 + "&selLiv3=" + selLiv3

	//alert('url : ' + url);
	//alert('id : ' + id);

	var span

	if (livello=="1") {
		span = document.getElementById('cmbLivello2_span');
		span.innerHTML = "<select id='cmbLivello2' name='cmbLivello2' class='w100'><option/></select>";
		span = document.getElementById('cmbLivello3_span');
		span.innerHTML = "<select id='cmbLivello3' name='cmbLivello3' class='w100'><option/></select>";
		span = document.getElementById('cmbLivello4_span');
		span.innerHTML = "<select id='cmbLivello4' name='cmbLivello4' class='w100'><option/></select>";
	}
	if (livello=="2") {
		span = document.getElementById('cmbLivello3_span');
		span.innerHTML = "<select id='cmbLivello3' name='cmbLivello3' class='w100'><option/></select>";
		span = document.getElementById('cmbLivello4_span');
		span.innerHTML = "<select id='cmbLivello4' name='cmbLivello4' class='w100'><option/></select>";
	}
	if (livello=="3") {
		span = document.getElementById('cmbLivello4_span');
		span.innerHTML = "<select id='cmbLivello4' name='cmbLivello4' class='w100'><option/></select>";
	}

	retrieveURL(url,id);

  }

  function retrieveURL(url,id) {

  	//alert('retrieveURL - START');
  	//alert('url : ' + url);
  	//alert('id : ' + id);

	idCmb = id;

    if (window.XMLHttpRequest) { // Non-IE browsers
      req = new XMLHttpRequest();
      req.onreadystatechange = processStateChange;
      //req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=ISO-8859-1");
      try {
        req.open("GET", url, true);
      } catch (e) {
        alert(e);
      }
      req.send(null);
    } else if (window.ActiveXObject) { // IE
      req = new ActiveXObject("Microsoft.XMLHTTP");
        req.onreadystatechange = processStateChange;
		//req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=ISO-8859-1");
	  if (req) {
        req.open("GET", url, true);
        req.send();
      }
    }
  }

  function processStateChange() {

  	//alert('processStateChange - START');
  	//alert('id : ' + idCmb);

  	//alert('req.readyState : ' + req.readyState);
  	//alert('req.responseText : ' + req.responseText);

    if (req.readyState == 4) { // Complete
      if (req.status == 200) { // OK response
      	var sResponse
      	var span
      	sResponse = req.responseText;
      	//alert('sResponse : ' + sResponse);
      	span = document.getElementById(idCmb + "_span");
      	//if (span==null) alert('fallito!!!');
      	//alert(sResponse);
        span.innerHTML = sResponse;
      } else {
        alert("Problem: " + req.statusText);
      }
    }
  }

function listValidateCode( code )
{
	//alert(code);

	if( code >= 32 && code <= 122 ) return true;		//tutti i caratteri visibili
/*
	if( code >= 48 && code <= 57 ) return true;			//0 .. 9
	if( code >= 65 && code <= 90 ) return true;			//A .. Z
	if( code >= 97 && code <= 122 ) return true;		//a .. z
	if( code == 32 ) return true;										//space
*/
	return false;
}

function listFindText( ctl, code )
{
	if( !ctl ) return false;
	if( !listValidateCode( code ) ) return false;

	var text = String.fromCharCode( code );

	if( !document.LIST_TEXT ) document.LIST_TEXT = '';
	document.LIST_TEXT += text.toLowerCase();

	var opt;
	for (i=0; i<=ctl.options.length-1; i++)
	{
		if( ctl.options( i ).text.substr(0, document.LIST_TEXT.length).toLowerCase() == document.LIST_TEXT )
		{
			ctl.options( i ).selected = true;
			ctl.selectedIndex = i;
			return true;
		}
	}
	return false;
}

function listKeyDown( ctl, interval )
{
  if( !ctl ) return false;

	if( !interval ) interval = 800;
	if( interval <= 0 ) interval = 800;

	var code = (document.layers) ? window.event.which : window.event.keyCode;
	if( code == 0 ) return false;

	//alert( code );
	window.event.returnValue = false;

	if( code == 13 || code == 9 ) { if( !listBlur( ctl ) ) window.event.returnValue = true; return false; }

	//up & down + page up & down + home + end
	if( code == 38 || code == 40 || code == 33 || code == 34 || code == 35 || code == 36 )
	{
		document.LIST_TEXT = "";
		var i = ctl.selectedIndex;
		var last = ctl.options.length-1;

		if( code == 33 ) i = i-8;		//page up
		if( code == 34 ) i = i+8;		//page down
		if( code == 38 ) i--;				//up
		if( code == 40 ) i++;				//down
		if( code == 35 ) i = last;	//end
		if( code == 36 ) i = 0;			//home

		if( i < 0 ) i = 0;
		if( i > last ) i = last;

		ctl.selectedIndex = i;
		return true;
	}

	if( document.LIST_TMID ) window.clearTimeout( document.LIST_TMID );

	listFindText( ctl, code )

	document.LIST_TMID = window.setTimeout( 'document.LIST_TEXT = "";', interval );

}

function listFocus( ctl )
{
  if( !ctl ) return false;

	try { document.LIST_SELX = ctl.selectedIndex; } catch( e ) { }

	return true;
}

function listBlur( ctl )
{
  if( !ctl ) return false;

	if( ctl.selectedIndex == document.LIST_SELX ) return false;

	try { ctl.onchange(); } catch( e ) { return false; }

	return true;
}
//---------------------------------------------------------------

function mostra(pDiv) {
	var miodiv;
	miodiv = document.getElementById(pDiv);
	if (miodiv!=null) miodiv.style.display = '';
	if (miodiv!=null) miodiv.style.visibility='visible';
}

function nascondi(pDiv) {
	var miodiv
	miodiv = document.getElementById(pDiv);
	if (miodiv!=null) miodiv.style.display = 'none';
}

function mostra_nascondi(pDiv,pImgNascosto,pImgVisibile) {
  var miodiv
  miodiv = document.getElementById(pDiv);
  if (mostra_nascondi.arguments.length>1) {
    var miaimg
    miaimg = document.getElementById(pDiv+"img");
    if (miodiv.style.display==''){
      miaimg.src=pImgNascosto;
    } else {
      miaimg.src=pImgVisibile;
    }
  }
  if (miodiv.style.display==''){
    nascondi(pDiv);
  } else {
    mostra(pDiv);
  }
}
function mostra_tab(pTab, pListTab, pImgOn, pImgOff,pItem) {
  var miodiv
  var miolink
  var miaimg
  miodiv = document.getElementById(pTab);
  if (miodiv!=null){
	  if (mostra_tab.arguments.length>1) {
	    var tabs = new Array();
	    tabs = pListTab.split(",");
	    for (var indice=0;indice<tabs.length;indice++){
		  if (tabs[indice]!=pTab){
		    nascondi(tabs[indice]);
		    miolink = document.getElementById("a_mostra_tab_"+tabs[indice]);
		    if (miolink!=null){
		      miolink.className="tabHeaderOff";
		    }
	        if (mostra_tab.arguments.length>3) {
	          miaimg = document.getElementById("img_mostra_tab_"+tabs[indice]);
	           if (miaimg!=null){
	             miaimg.src=pImgOff;
	           }
	        }
		  }
		}
	  }
	  mostra(pTab);
	  miolink = document.getElementById("a_mostra_tab_"+pTab);
	  if (miolink!=null){
	    miolink.className="tabHeader";
	  }
	  if (mostra_tab.arguments.length>3) {
	    miaimg = document.getElementById("img_mostra_tab_"+pTab);
	    if (miaimg!=null){
	      miaimg.src=pImgOn;
	    }
	  }
	  if (mostra_tab.arguments.length>4) {
	      mioitem = document.getElementById(pItem);
	      if (mioitem!=null){
	        mioitem.focus();
	      }
	  }
  }
  return false;
}
function verificaitem_gourl(pItem, pText, pUrl){
// la funzione cerca un item, se lo trova ed e' valorizzato
// prima di spostarsi all'url dichiarato chiede conferma
    if (document.getElementById(pItem)!=null){
      if (document.getElementById(pItem).value!=''){
		if (!window.confirm(pText)){
	      return;
	    }
	  }
	}
    window.location.href=pUrl;
}

function Righello(IDtabella){
trs=document.getElementById(IDtabella).tBodies[0].rows;
for(i=0;i<trs.length;i++){
      tds=trs[i].cells;
      if (tds.length>0){
        if (tds[0].getElementsByTagName("a").length>0){
          trs[i].link=tds[0].getElementsByTagName("a")[0].href;
          if (trs[i].style.cursor) {
            trs[i].style.cursor="pointer";
          }
          var st;
          st = ""+tds[0].getElementsByTagName("a")[0].href;
          if (st.toUpperCase().indexOf("JAVASCRIPT:")!=-1){
            trs[i].onclick=function(){eval(replaceAll(this.link,"%20"," "))}
          } else {
            trs[i].onclick=function(){document.location.href=this.link}
          }
          trs[i].onmouseover = function(){this.className="corrente "+replaceAll(this.className,"corrente","");return false;}
          trs[i].onmouseout = function(){this.className=replaceAll(this.className,"corrente","");return false;}
        }
      }
    }
}

function gestUtente(idForm, idAction, act) {

	var ctl;
	var ctl1;
	ctl1 = document.getElementById('userName');
	ctl = document.getElementById('user');
	if (ctl!=null) ctl.value = ctl1.innerHTML;
	ctl = document.getElementById('act');
	if (ctl!=null) ctl.value = act;

	postbackAct(idForm, idAction);

}
//var winModalWindow
//function IgnoreEvents(e)
//{
//  return false
//}
//function HandleFocus()
//{
//  if (winModalWindow)
//  {
//    if (!winModalWindow.closed)
//    {
//      winModalWindow.focus()
//    }
//    else
//    {
//      window.top.releaseEvents (Event.CLICK|Event.FOCUS)
//      window.top.onclick = ""
//    }
//  }
//  return false
//}
function showLOV(link,button,id) {
  //w=450;
  //h=350;
  w=screen.width*0.8;
  h=screen.height*0.8;
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;
  winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',resizable=yes,scrollbars=yes'
  var urlfinale;
  urlfinale = link;
  if (urlfinale.indexOf("?")>-1){
    urlfinale=urlfinale+"&"
  } else {
    urlfinale=urlfinale+"?"
  }
  urlfinale=urlfinale+"returnfield="+id
//  if (window.showModalDialog)
//  {
//    window.showModalDialog(urlfinale,null,
//    "dialogWidth="+w+"px;dialogHeight="+h+"px;top="+wint+";left="+winl+";resizable=yes;scrollbars=yes")
//  }
//  else
//  {
//    window.top.captureEvents (Event.CLICK|Event.FOCUS)
//    window.top.onclick=IgnoreEvents
//    window.top.onfocus=HandleFocus
//    winModalWindow =
//    window.open (urlfinale,"lov
//     "dependent=yes,width="+w+",height="+h+",top="+wint+",left="+winl+",resizable=yes,scrollbars=yes")
//  winModalWindow.focus()
//  }
//  {
//  winprops=winprops+",modal=yes";
//  alert(winprops);
  winModalWindow = window.open(urlfinale, "lov", winprops);
//  }
  if (window.focus) {winModalWindow.focus()}
  return false;
}
function showPopupForm(link,button,id) {
  w=screen.width*0.8;
  h=screen.height*0.8;
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;
  winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',resizable=yes,scrollbars=yes'
  var urlfinale;
  urlfinale = link;
  if (id!=null&&id!=""){
  if (urlfinale.indexOf("?")>-1){
    urlfinale=urlfinale+"&"
  } else {
    urlfinale=urlfinale+"?"
  }
  urlfinale=urlfinale+"returnfield="+id
  }
  winModalWindow = window.open(urlfinale, "popupform", winprops);
  if (window.focus) {winModalWindow.focus()}
  return false;
}

function remLink() {
  if (window.sList && window.sList.open && !window.sList.closed)
    window.sList.opener = null;
}

function aggiungiClasse(pItem, pClassName){
  var classe = pItem.className;
  if (classe==null){
    classe="";
  }
  if (classe == ""){
    classe = pClassName;
  } else {
    classe = classe + " " + pClassName;
  }
  pItem.className = classe;
}
function togliClasse(pItem, pClassName){
  var classe = pItem.className;
  if (classe==null){
    classe="";
  } else {
    classe=classe+" ";
  }
  if (classe.indexOf(pClassName)!=-1){
    classe = classe.substr(0,classe.indexOf(pClassName+" ")-1)+classe.substr(classe.indexOf(pClassName)+pClassName.length+1);
    classe = Trim(classe);
  }
  pItem.className = classe;
}
function valida(miaform)
{
// funzione di validazione items INPUT, SELECT, TEXTAREA
// in base agli attributi di validazione
// prima scorre gli INPUT poi i SELECT ed infine i TEXTAREA
	var required = "";
	var label    = "";
	var index    = 0;
	var valore   = "";
	var mask     = "";
	var testoerrore = "";
	var errore   = false;
	var elfocus;
	var i;
    var coll_nodes=document.getElementById(miaform).getElementsByTagName("INPUT");
    var item=null;
	for(i=0;i<coll_nodes.length;i++)
	{
	    item = coll_nodes[i];
		if ( item.type != "button" )
		{
			togliClasse(item,"error");
			required = item.getAttribute("_REQUIRED");
			if ( required != null && required != "" ) {
                valore = item.value;
                if (item.getAttribute("_MASK")!=null){
                  mask = item.getAttribute("_MASK");
                  if (mask.substring(0,1)=="D"){
				 	valore = valore.replace(/\//g,"");
				 	valore = Trim(valore);
                  }
                }
				if ( valore == "") {
					label = item.getAttribute("_LABEL");
					if (label == null){
					  label = document.getElementById(miaform).id;
					}
					aggiungiClasse(item,"error");
					testoerrore = testoerrore + "\nCampo '"+label+"' obbligatorio";
					if (!errore){
						elfocus = item;
						errore = true;
					}
				}
			}
			if (item.getAttribute("_MASK")!=null){
			  if (item.getAttribute("_MASK")!=""){
  			    if (!CheckField(item)){
					aggiungiClasse(item,"error");
					if (!errore){
					  elfocus = item;
					  errore = true;
					}
			    }
			  }
			}
		}
	}
	coll_nodes=document.getElementById(miaform).getElementsByTagName("SELECT");
	for(i=0;i<coll_nodes.length;i++)
	{
		item=coll_nodes[i];
		togliClasse(item,"error");
		required = item.getAttribute("_REQUIRED");
		if ( required != null && required != "" ) {
			if ( item.value == "") {
				label = item.getAttribute("_LABEL");
				if (label == null){
				  label = document.getElementById(miaform).id;
				}
				testoerrore = testoerrore + "\nCampo '"+label+"' obbligatorio";
				aggiungiClasse(item,"error");
				if (!errore){
					elfocus = item;
					errore = true;
				}
			}
		}
		if (item.getAttribute("_MASK")!=null){
 		 if (item.getAttribute("_MASK")!=""){
		  if (!CheckField(item)){
			aggiungiClasse(item,"error");
               if (!errore){
 			  elfocus=item;
                 errore = true;
			}
		  }
  		 }
		}
	}
	coll_nodes=document.getElementById(miaform).getElementsByTagName("TEXTAREA");
	for(i=0;i<coll_nodes.length;i++)
	{
		item = coll_nodes[i];
		togliClasse(item,"error");
		required = item.getAttribute("_REQUIRED");
		if ( required != null && required != "" ) {
			if ( item.value == "") {
				label = item.getAttribute("_LABEL");
				if (label == null){
				  label = document.getElementById(miaform).id;
				}
				testoerrore = testoerrore + "\nCampo '"+label+"' obbligatorio";
				aggiungiClasse(item,"error");
                if (!errore){
					elfocus=item;
					errore = true;
				}
			}
		}
		if (item.getAttribute("_MASK")!=null){
		 if (item.getAttribute("_MASK")!=""){
		  if (!CheckField(item)){
			aggiungiClasse(item,"error");
            if (!errore){
            	elfocus=item;
            	errore = true;
 			}
		  }
		 }
		}
	}
	if (errore) {
	  if (testoerrore!=""){
	  	window.alert("Segnalazioni"+testoerrore);
	  }
	  try {
		  elfocus.focus();
	  } catch (e) { var nulla;}
	  return false;
	}
	return true;
}
var statoSubmit="0";
function modificaLoading(messaggio){
  var loading = document.getElementById('loading');
  if (loading==null){
    alert(messaggio);
  } else {
    if (loading.style.display=='none'){
      alert(messaggio);
    } else {
      if (document.getElementById('loadinginner')!=null){
        var loading = document.getElementById('loadinginner');
      }
      loading.innerHTML = loading.innerHTML+"<br/>"+messaggio;
    }
  }
}
function sottometti(miaform, miostato) {
  if (statoSubmit=="1"){
    modificaLoading("E' in corso un'altra operazione, attendere prego.");
  } else {
    if (miostato=="") miostato="ricerca";
    if (valida(miaform)&&validazioneForm(miaform,miostato)){
      if (document.getElementById("statoaction")!=null){
        document.forms[miaform].statoaction.value=miostato;
      }
      document.getElementById(miaform).submit();
      if (document.forms[miaform].target!='_blank'){
        var t=setTimeout("mostra('loading');",500);
        statoSubmit="1";
      }
      nascondi("boxMessaggi");
    }
  }
  return false;
}
function cancella(miaform, miostato) {
  if (statoSubmit=="1"){
    modificaLoading("E' in corso un'altra operazione, attendere prego.");
  } else {
    if (miostato=="") miostato="ricerca";
    if (confirm("Confermi la cancellazione dell'elemento?")){
      document.forms[miaform].statoaction.value=miostato;
      document.getElementById(miaform).submit()
      if (document.forms[miaform].target!='_blank'){
        var t=setTimeout("mostra('loading');",500);
        statoSubmit="1";
      }
    }
  }
  return false;
}
function vai(miaform, miostato) {
  if (statoSubmit=="1"){
    modificaLoading("E' in corso un'altra operazione, attendere prego.");
  } else {
    if (miostato=="") miostato="ricerca";
    window.location=document.getElementById(miaform).action+"?statoaction="+miostato
    var t=setTimeout("mostra('loading');",300);
    statoSubmit="1";
  }
  return false;
}
function pulisci(miaform, miostato) {
  if (statoSubmit=="1"){
    modificaLoading("E' in corso un'altra operazione, attendere prego.");
  } else {
    if (miostato=="") miostato="ricerca";
    window.location=document.getElementById(miaform).action+"?statoaction="+miostato
    var t=setTimeout("mostra('loading');",300);
    statoSubmit="1";
  }
  return false;
}
function help(miaform, miostato) {
  w=screen.width*0.9;
  h=screen.height*0.9;
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;
  winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',resizable=yes,scrollbars=yes'
  var urlfinale="help/toolbar.html";
  winModalWindow = window.open(urlfinale, "help", winprops);
  if (window.focus) {winModalWindow.focus()}
  return false;
}
function setFocus(){
    // terminate if we hit a non-compliant DOM implementation
    var xre = new RegExp(/\berror\b/);
    // Search only forms to avoid spending time on regular text
    for (var f = 0; (formnode = document.getElementsByTagName('form').item(f)); f++){
        // Search for errors first, focus on first error if found
         for (var i = 0; (node = formnode.getElementsByTagName('div').item(i)); i++) {
             var innerEls =node.childNodes;
             for (var j = 0; j < innerEls.length; j++) {
                 try {
                     inputnode=innerEls[j];
                     if (inputnode.tagName=="INPUT"||inputnode.tagName=="TEXTAREA"){
                     if (inputnode.type.toUpperCase()!="HIDDEN"&&inputnode.type.toUpperCase()!="BUTTON") { // check availability first
                       if (inputnode.focus) { // check availability first
                           if (inputnode.readOnly!=true&&inputnode.disabled!=true){
                             inputnode.focus();
                             return;
                           }
                       }
                     }
                     }
                 } catch(e) {
                      // try next one, this can happen with a hidden or
                      // invisible input field
                 }
             }
         }
     }
 }
function fnFormatNumber(number,decimals) {
  var i,d;

  // set default values
//  if(number=="") number=parseInt("0",10);
//  if(decimals=="") decimals=parseInt("2",10);

  // round number to specified number of decimals
  number=""+Math.round(number*Math.pow(10,decimals))*Math.pow(10,-decimals);

  // find index of decimal point
  d=number.indexOf(".");

  // if no decimal point, number is an integer
  // pad number with trailing zeros
  if(d==-1) {
    if (decimals>0){
      number=number+".";
      for(i=0;i<decimals;i++)
        number=number+"0";
    }
    return number;
  }

  // if decimal point is first index
  // pad number with leading zero
  if(d==0) {
    number="0"+number;
    d++;
  }

  // if first index is negative sign
  // pad number with leading zero
  if(d==1 && number.substring(0,1)=="-") {
    number="-0" + number.substring(1,number.length);
    d++;
  }

  // truncate number to desired length
  number=number.substring(0,d+decimals+1);

  // if number is shorter than desired length
  // pad number with trailing zeros
  while(number.length<=d+decimals)
    number=number+"0";

  if (decimals==0){
    number=number.substring(0,d);
  }

  return number;
}
function fnImporto(valore){
  var stringa;
  stringa = fnFormatNumber(valore,2);
  stringa=stringa.replace(/\,/g,"");
  stringa=stringa.replace(/\./g,",");
  return stringa;
}
function fnIntero(valore){
  var stringa;
  stringa = fnFormatNumber(valore,0);
  stringa=stringa.replace(/\,/g,"");
  stringa=stringa.replace(/\./g,",");
  return stringa;
}
function fnParseImporto(valore){
  var stringa;
  stringa=valore;
  stringa=stringa.replace(/\./g,"");
  stringa=stringa.replace(/\,/g,".");
  return Number(stringa);
}
function fnParseData(datastringa,pdifanni,pdifmesi,pdifgiorni){
	var year = Number("0");
	var month = Number("0");
	var date = Number("0");
	var hh = Number("0");
	var mm = Number("0");
	var ss = Number("0");
	if (datastringa.length == 8) {
	    year = Number(datastringa.substring(4));
	    month = Number(datastringa.substring(2, 4))-1;
	    date = Number(datastringa.substring(0, 2));
	    hh = Number("00");
	    mm = Number("00");
	    ss = Number("00");
	} else if (datastringa.length == 10) {
	    year = Number(datastringa.substring(6));
	    month = Number(datastringa.substring(3, 5))-1;
	    date = Number(datastringa.substring(0, 2));
	    hh = Number("00");
	    mm = Number("00");
	    ss = Number("00");
	}
	return new Date(year+pdifanni, month+pdifmesi, date+pdifgiorni, hh, mm, ss);
}
function fnFormatData(pdata){
  dan = pdata.getMonth()+1;
  return changeDate(pdata.getDate().toString()+"/"+dan.toString()+"/"+pdata.getFullYear().toString());
}
function nvlDato(pnode,pstringa){
  if (pnode==null){
    return pstringa;
  }
  if (pnode.childNodes.length==0){
    return pstringa;
  }
  if (pnode.firstChild.data==null){
    return pstringa;
  }
  return pnode.firstChild.data;
}
if ( !window.encodeURIComponent ) window.encodeURIComponent = function ( s ) {
	// thanks to http://www.worldtimzone.com/res/encode/
	// test string for cyrillic: '`~!@#$%^&*()-_=+\\|/[]{};:,.?\'" abcdefghigklmnopqrstuvwxyz ABCDEFGHIGKLMNOPQRSTUVWXYZ ????????????????????????????????? ?????????????????????????????????'
	var okURIchars = /[a-z\d\.!~\*'\(\)_-]/i;
	var hexchars = "0123456789ABCDEF";

	var toHex = function ( n ) { return hexchars.charAt( n >> 4 ) + hexchars.charAt( n & 0xF ); }

	var utf8 = function ( wide ) {
		var c, s;
		var enc = "";
		var i = 0;
		while ( i < wide.length ) {
			c = wide.charCodeAt( i++ );
			// handle UTF-16 surrogates
			if ( c >= 0xDC00 && c < 0xE000 ) continue;
			if ( c >= 0xD800 && c < 0xDC00 ) {
				if ( i >= wide.length ) continue;
				s = wide.charCodeAt( i++ );
				if ( s < 0xDC00 || c >= 0xDE00 ) continue;
				c = ( ( c - 0xD800 ) << 10 ) + ( s - 0xDC00 ) + 0x10000;
			}
			// output value
			if ( c < 0x80 ) { enc += String.fromCharCode( c );
			} else if ( c < 0x800 ) { enc += String.fromCharCode( 0xC0 + ( c >> 6 ), 0x80 + ( c & 0x3F ) );
			} else if ( c < 0x10000 ) { enc += String.fromCharCode( 0xE0 + ( c >> 12 ), 0x80 + ( c >> 6 & 0x3F ), 0x80 + ( c & 0x3F ) );
			} else { enc += String.fromCharCode( 0xF0 + ( c >> 18 ), 0x80 + ( c >> 12 & 0x3F ), 0x80+ ( c >> 6 & 0x3F ), 0x80 + ( c & 0x3F ) ); }
		}
		return enc;
	}

	var s = utf8( s );
	var enc = "";
	for ( var i = 0; i < s.length; i++ ) {
		if ( !( s.charAt( i ).match( okURIchars ) ) ) {
			enc += "%" + toHex( s.charCodeAt( i ) );
		} else {
			enc += s.charAt( i );
		}
	}
	return enc;
}
function apriReport(link,target) {
  var winprops = ''
  var urlfinale;
  var miawin;
  urlfinale = link;
  var oggi = new Date();
  oggi=oggi.toString().replace(/ /g,"_").replace(/\+/g,"_").replace(/:/g,"_");
  miawin = window.open(urlfinale, "stampa"+oggi, winprops);
  if (window.focus) {miawin.focus()}
  return false;
}
function replaceAll(s, t, u) {
  i = s.indexOf(t);
  r = "";
  if (i == -1) return s;
  r += s.substring(0,i) + u;
  if ( i + t.length < s.length)
    r += replaceAll(s.substring(i + t.length, s.length), t, u);
  return r;
}
function apriFile(codappl,idcnt,idpkg,file) {
  w=screen.width*0.8;
  h=screen.height*0.8;
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;
  winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',resizable=yes,scrollbars=yes'
  var urlfinale="ACT0201GetFileArchiviatore.do?codappl="+codappl+"&idcnt="+idcnt+"&idpkg="+idpkg+"&file="+file;
  winMia = window.open(urlfinale, "aprifile", winprops);
  if (window.focus) {winMia.focus()}
  return false;
}
function createElementFast(pelement, ptype, pvalue, pname, pid, psize, pmaxlength, penabled, pclass ){
var item;
item = document.createElement(pelement);
if (ptype!=null){
    item.type=ptype;
}
if (pvalue!=null){
    item.value = pvalue;
}
if (pname!=null){
    item.name = pname;
}
if (pid!=null){
    item.id = pid;
}
if (psize!=null){
    item.size = psize;
}
if (pmaxlength!=null){
    item.maxLength = pmaxlength;
}
if (penabled==false){
    if (ptype.toUpperCase()=="CHECKBOX"){
      item.disabled = true;
    } else {
      item.readOnly = true;
    }
    item.className = "readonly";
} else {
    item.tabIndex="2";
}
var wclassName="";
if (pclass!=null){
  wclassName=pclass;
} else {
  wclassName="formtext";
}
if (wclassName!=null){
    if (penabled==false){
      item.className = wclassName+" readonly";
    } else {
      item.className = wclassName;
    }
}
return item;
}
function apriFileWebDav(tipo, dati, codappl,idcnt,idpkg,file,filetmp) {
  w=screen.width*0.8;
  h=screen.height*0.8;
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;
  winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',resizable=yes,scrollbars=yes'
  var urlfinale="ACT0203GetFileWebDav.do?tipo="+tipo+"&dati="+dati+"&codappl="+codappl+"&idcnt="+idcnt+"&idpkg="+idpkg+"&file="+file+"&filetmp="+filetmp;
  winMia = window.open(urlfinale, "aprifile", winprops);
  if (window.focus) {winMia.focus()}
  return false;
}
function apriFileUpload(tipo, dati, label,nomefileserver,estensioni) {
  w=screen.width*0.8;
  h=screen.height*0.8;
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;
  winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',resizable=yes,scrollbars=yes'
  var urlfinale="ACT0108UploadFile.do?statoaction=nuovo&tipo="+tipo+"&dati="+dati+"&labelfile="+label+"&nomefileserver="+nomefileserver+"&estensioni="+estensioni;
  winMia = window.open(urlfinale, "uploadfile", winprops);
  if (window.focus) {winMia.focus()}
  return false;
}
function firmaFileWebDav(tipo, dati, codappl,idcnt,idpkg,file,filetmp) {
  w=screen.width*0.8;
  h=screen.height*0.8;
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;
  winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',resizable=yes,scrollbars=yes'
  var urlfinale="ACT0205FirmaFileWebDav.do?tipo="+tipo+"&dati="+dati+"&codappl="+codappl+"&idcnt="+idcnt+"&idpkg="+idpkg+"&file="+file+"&filetmp="+filetmp;
  winMia = window.open(urlfinale, "aprifile", winprops);
  if (window.focus) {winMia.focus()}
  return false;
}
function apriFileModello(tipo, dati) {
  w=screen.width*0.8;
  h=screen.height*0.8;
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;
  winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',resizable=yes,scrollbars=yes'
  var urlfinale="ACT0204GetModello.do?tipo="+tipo+"&dati="+dati;
  winMia = window.open(urlfinale, "aprifile", winprops);
  if (window.focus) {winMia.focus()}
  return false;
}
function generaStampa(idcartella,idmodulo,from,username,idnormativa) {
  w=screen.width*0.8;
  h=screen.height*0.8;
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;
  winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',resizable=yes,scrollbars=yes'
  var urlfinale="ACT1201GeneraStampa.do?&idcartella="+idcartella+"&idmodulo="+idmodulo+"&from="+from+"&username="+username+"&idnormativa="+idnormativa;
  winMia = window.open(urlfinale, "generaStampa", winprops);
  if (window.focus) {winMia.focus()}
  return false;
}
function generaAnteprima(idcartella,idmodulo) {
  w=screen.width*0.8;
  h=screen.height*0.8;
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;
  winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',resizable=yes,scrollbars=yes'
  var urlfinale="ACT1205GeneraAnteprima.do?&idcartella="+idcartella+"&idmodulo="+idmodulo;
  winMia = window.open(urlfinale, "generaAnteprima", winprops);
  if (window.focus) {winMia.focus()}
  return false;
}
function valoreCampo(pid){
  var item = document.getElementById(pid);
  if (item==null){
    return null;
  } else {
    var valore = item.value;
    return valore;
  }
}
function valoreCampoNvl(pid){
  var item = document.getElementById(pid);
  if (item==null){
    return '';
  } else {
    var valore = item.value;
    return valore;
  }
}
function verificaForm(pNomeform){
// verifica se una form ha i dati modificati rispetto a quelli iniziali
	var i;
	var el;
	for(i=0;i<document.getElementById(pNomeform).getElementsByTagName("INPUT").length;i++)
	{
	  el = document.getElementById(pNomeform).getElementsByTagName("INPUT")[i];
 	  if (el.type=="button"||el.type=="hidden"){
 	    //nulla
 	  } else if (el.type=="checkbox"||el.type=="radio"){
	    if (el.checked!=el.defaultChecked){
	    alert(el.name);
	    return false;
	    }
	  } else {
	    if (el.value!=el.defaultValue){
	      if (replaceAll(replaceAll(el.value,"/","")," ","")!=""||el.defaultValue!=""){
		    return false;
		  }
	    }
	  }
	}
	
	for(i=0;i<document.getElementById(pNomeform).getElementsByTagName("TEXTAREA").length;i++)
	{
	  el = document.getElementById(pNomeform).getElementsByTagName("TEXTAREA")[i];
 	  if (el.value!=el.defaultValue){
	    return false;
	  }
	}
 return true;
}

