  //definizione di variabili globali 
  replugua = eval("/" + "=" + "/g"); 
  
  function CustomizeWindow(lingua) {                                                                                                                                                                                                
    if (lingua == 'IT') {
      window.open('/cgigrp/vw00rh0.cgi?lng=IT','_blank',
       'scrollbars=yes,resizable=yes,status=yes,location=no,toolbar=no');
    }
    if (lingua == 'EN') {
      window.open('/cgigrp/vw00rh0.cgi?lng=EN','_blank',
       'scrollbars=yes,resizable=yes,status=yes,location=no,toolbar=no');
    }
  }
                                                                                                                                                                                                                                    
  function infopgm() {
   // visualizzazione nome programma che ha creato la pagina
    alert(infopgmname);
  }

  function textfocus(field) {
    if (!field.readOnly) {
      field.style.backgroundColor = colorfocus;
    }
  }

  function textblur(field) {                                                                                                                                                                                                        
    field.style.backgroundColor = 'transparent';                                                                                                                                                                                    
  }                                                                                                                                                                                                                                 

  function loading() {
    if (document.images.imggif) {
      document.images.imggif.src="/img/loading.gif";
      document.images.imggif.className="displayimg";
    }
  }

  function unloading() {
    if (document.images.imggif) {
      document.images.imggif.src="/img/tpy.gif";
      document.images.imggif.className="hideimg";
    }
  }

  function closeall(lingua)
    {
    var testo="";
    if (lingua == 'IT') {
       testo='Conferma chiusura?';
    }
    if (lingua == 'EN') {
       testo='Do you confirm closing?';
    }
    if (confirm(testo)) {
       if (window.sblocco && typeof window.sblocco == "function") {
         sblocco();
       }
     window.close();
    }
  }

  function operendform() {
    // alla chiusura del browser eseguo chiusura controllata di ciò che è aperto
    clowinctrl();
  }

  function clowinctrl() {
    // gestione chiusura controllata della finestra, comune a tutte le form
    if (window.chiudiwin && typeof window.chiudiwin=="function") {
      chiudiwin();
    }
    // esegue il programma di sblocco entità bloccate
    // i pgm che bloccano sono quelli che hanno nella form il campo funzchoice
    if (document.getElementById("funzchoice")) {
      if (document.getElementById("funzchoice").value == " " ||
          document.getElementById("funzchoice").value == "") {
        if (window.sblocco && typeof window.sblocco == "function") {
          sblocco();
        }
      }
    }
  }

  function impostatitle(numform) {                                                                                                                                                                                                  
      // reperimento titolo del campo dalla costante identificata da nomecampo + 'label'                                                                                                                                            
      // i campi per i quali reperire il titolo sono quelli per i quali esiste il label e                                                                                                                                           
      // hanno il titolo vuoto oppure non hanno titolo                                                                                                                                                                              
      var ximptit = document.forms[numform].elements.length;               // quanti sono i campi della form                                                                                                                        
      for (iimptit=0;iimptit<ximptit;iimptit++) {                                                                                                                                                                                   
          box = document.forms[numform].elements[iimptit];                                                                                                                                                                          
          if (box.id == "") {                     // se il campo non ha id                                                                                                                                                          
            continue;                                                                                                                                                                                                               
          }                                                                                                                                                                                                                         
          if  (document.getElementById(box.id).getAttribute("title") == "" ||     //titolo vuoto                                                                                                                                    
               document.getElementById(box.id).getAttribute("title") == null) {   //oppure senza titolo                                                                                                                             
            if (document.getElementById(box.id + "label")) {                      // purchè esista label                                                                                                                            
              document.getElementById(box.id).setAttribute("title",           // titolo da label del campo                                                                                                                          
                    document.getElementById(box.id + "label").innerHTML);                                                                                                                                                           
            }                                                                                                                                                                                                                       
          }                                                                                                                                                                                                                         
      }                                                                                                                                                                                                                             
  }                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                    
  function replcarspec(stringa) {                                                                                                                                                                                                   
    //nella chiamata wiindow.open i caratteri speciali vanno convertiti                                                                                                                                                              
    // variabili per sostituzione dei caratteri speciali con l'equivalente ascii esadecimale
    stringaout = "";                                                                              
    arraychar = stringa.split("");                             //transform in array                                                             
    for (is=0; is<arraychar.length; is++) {                                                       
      stringaout = stringaout + decToHexChar(arraychar[is]);   //receive character converted to hex
    }   
    // firefox e netscape passano solo lf, explorer e opera invece crlf quindi la sostituzione va fatta per gradi                                                                                                                   
    stringaout = stringaout.replace(/\r\n/g,"%0D%0A");       // CR LF sostituiti con equvalente ascii esadec                                                                                                                              
    stringaout = stringaout.replace(/\n/g,"%0D%0A");         // LF sostituiti con equvalente ascii esadec crlf                                                                                                                            
                                                                                          
    return stringaout;                                                                            
  }    

  function decToHexChar(carattere)  { //convert character to hex                  
    var hexVals = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",     
      "A", "B", "C", "D", "E", "F");                                              
    var hexString = "";                                                           
    var radix = 16;                                                               
    num = carattere.charCodeAt(0);                                                
    if (num > 255) {      //non-ISO-8859-1 character                                            
      return carattere;                                                           
    }                                                                             
    if (isUnsafe(carattere) == false) {   //no need conversion                    
      return carattere;                                                           
    }                                                                             
    while (num >= radix) {                                                        
      temp = num % radix;                                                         
      num = Math.floor(num / radix);                                              
      hexString += hexVals[temp];                                                 
    }                                                                             
    hexString += hexVals[num];                                                    
    return reversal(hexString + '%');                                             
  }                                                                               

  function reversal(s) { // part of the hex-ifying functionality     
    var len = s.length;                                              
    var trans = "";                                                  
    for (ic=0; ic<len; ic++) {                                       
      trans = trans + s.substring(len-ic-1, len-ic);                 
    }                                                                
    s = trans;                                                       
    return s;                                                        
  }                                                                  

  // this function checks to see if a char is URL unsafe.                                 
  // Returns bool result. True = unsafe (convertito), False = safe (non convertito)
  function isUnsafe(compareChar) {                                                        
    var unsafeString = "\"<>%\\^[]`\+\$\,#-";    //questi vanno convertiti anche se nel gruppo dei caratteri normali               
    // aggiungere alla lista sopra questi caratteri sei si vogliono convertiti ";", "/", "?", ":", "@", "=", "&"  
    if ((unsafeString.indexOf(compareChar) == -1 && compareChar.charCodeAt(0) > 32
       && compareChar.charCodeAt(0) < 123)
       || compareChar.charCodeAt(0) == 10 || compareChar.charCodeAt(0) == 13) {    //CR e LF non vanno converititi, lo fa dopo
      return false; } // found no unsafe chars, return false  (no conversion)
    else {                                                                                
      return true;                                                                        
    }                                                                                     
  }                                                                                        
                                                                                                                                                                                                                                    
  function replcarspecajax(stringa) {                                                                                                                                                                                               
    //nella chiamata ajax, con firefox dal 3 in poi, i caratteri speciali non vanno convertiti         
    stringaout = stringa;                                                                                                                                     
    if (ffversion < 3) { //con firefox < 3 occorre convertire i caratteri speciali in  esadecimale ascii
      stringaout = replcarspec(stringa);
    } 
    else {
      replperc = eval("/" + "%" + "/g");    //con firefox >= 3, solo il carattere %          
      stringaout = stringaout.replace(replperc,"%25");      
      // firefox e netscape passano solo lf, explorer e opera invece crlf quindi la sostituzione va fatta per gradi                                                                                                                 
      stringaout = stringaout.replace(/\r\n/g,"%0D%0A");       // CR LF sostituiti con equvalente ascii esadec                                                                                                                            
      stringaout = stringaout.replace(/\n/g,"%0D%0A");         // LF sostituiti con equvalente ascii esadec crlf                                                                                                                          

    } 
    return stringaout;    
  }                                                                                                                                                                                                                                 

  function replcarampe(stringa) {                                                                                                                                                                                                   
    // variabili per sostituzione del carattere speciale & (delle descrizioni) in E                                                                                                                                                 
    replampe = eval("/" + "&" + "/g");                                                                                                                                                                                              
  //sostiuisce carattere speciale con E                                                                                                                                                                                             
    stringa = stringa.replace(replampe,"E");                                                                                                                                                                                        
    return stringa;                                                                                                                                                                                                                 
  }                                                                                                                                                                                                                                 

  function cvtdatnum(idfielddat,idfieldnum) {                                                                                                                                                                                       
    // verifica esistenza campo numerico da riempire che ha come identificativo quello ricevuto                                                                                                                                     
    if (!document.getElementById(idfieldnum)) {                                                                                                                                                                                     
      return true;                                                                                                                                                                                                                  
    }                                                                                                                                                                                                                               
    // verifica esistenza campo data da elaborare che ha come identificativo quello ricevuto                                                                                                                                        
    if (!document.getElementById(idfielddat)) {                                                                                                                                                                                     
      return true;                                                                                                                                                                                                                  
    }                                                                                                                                                                                                                               
    // se la data è vuota, imposto 0 nel campo numerico ed esco                                                                                                                                                                     
    if (document.getElementById(idfielddat).value == "") {                                                                                                                                                                          
      document.getElementById(idfieldnum).value = "0";                                                                                                                                                                              
      return true;                                                                                                                                                                                                                  
    }                                                                                                                                                                                                                               
    datacvtid = idfielddat + 'cvt';         //nome del campo hidden che dice se è data da convertire                                                                                                                                
    if (!document.getElementById(datacvtid)) {        // se non esiste imposto non è data                                                                                                                                           
      datacvt = 'n';                                                                                                                                                                                                                
    }                                                                                                                                                                                                                               
    else {                                                                                                                                                                                                                          
      datacvt = document.getElementById(datacvtid).value; //nel campo hidden il flag tipo data da convert                                                                                                                           
    }                                                                                                                                                                                                                               
    // se la data da convertire è un periodo mese o settimana deve eseguire una routine diversa                                                                                                                                     
    if (datacvt == 'm') {                                                                                                                                                                                                           
      return cvtmesnum(idfielddat,idfieldnum);                                                                                                                                                                                      
    }                                                                                                                                                                                                                               
    box = document.getElementById(idfielddat);
    stringa = document.getElementById(idfielddat).value;                                                                                                                                                                            
    // trasformo in minuscolo per facilitare il compito di trasformazione dei mesi espliciti                                                                                                                                        
    stringa = stringa.toLowerCase();                                                                                                                                                                                                
    mese1 = ["january", "february", "march", "april", "may", "june",                                                                                                                                                                
            "july", "august", "september", "october", "november", "december"];                                                                                                                                                      
    mese2 = ["jan", "feb", "mar", "apr", "may", "jun",                                                                                                                                                                              
            "jul", "aug", "sep", "oct", "nov", "dec"];                                                                                                                                                                              
    mese3 = ["gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno",                                                                                                                                                          
            "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre"];                                                                                                                                                    
    mese4 = ["gen", "feb", "mar", "apr", "mag", "giu",                                                                                                                                                                              
            "lug", "ago", "set", "ott", "nov", "dic"];                                                                                                                                                                              
    for (icvtdatnum=0;icvtdatnum<12;icvtdatnum++) {                                                                                                                                                                                 
      nummes = icvtdatnum + 1;                                                                                                                                                                                                      
      if (nummes < 10) {                                                                                                                                                                                                            
        stringa = stringa.replace(mese1[icvtdatnum],'0' + nummes.toString());                                                                                                                                                       
        stringa = stringa.replace(mese3[icvtdatnum],'0' + nummes.toString());                                                                                                                                                       
        stringa = stringa.replace(mese2[icvtdatnum],'0' + nummes.toString());                                                                                                                                                       
        stringa = stringa.replace(mese4[icvtdatnum],'0' + nummes.toString());                                                                                                                                                       
      }                                                                                                                                                                                                                             
      else {                                                                                                                                                                                                                        
        stringa = stringa.replace(mese1[icvtdatnum],nummes);                                                                                                                                                                        
        stringa = stringa.replace(mese3[icvtdatnum],nummes);                                                                                                                                                                        
        stringa = stringa.replace(mese2[icvtdatnum],nummes);                                                                                                                                                                        
        stringa = stringa.replace(mese4[icvtdatnum],nummes);                                                                                                                                                                        
      }                                                                                                                                                                                                                             
    }                                                                                                                                                                                                                               
    // elimino gli eventuali spazi esistenti tra giorno mese ed anno                                                                                                                                                                
    for (;stringa.indexOf(' ') >= 0;) {                                                                                                                                                                                             
      stringa = stringa.replace(" ","");                                                                                                                                                                                            
    }                                                                                                                                                                                                                               
    // se ci sono separatori devo verificare che siano almeno 2 e uguali                                                                                                                                                            
    if (stringa.indexOf('/') >= 0 || stringa.indexOf('-') >= 0                                                                                                                                                                      
        || stringa.indexOf('.') >= 0 || stringa.indexOf(',') >= 0) {                                                                                                                                                                
      esistesep = ' ';                                                                                                                                                                                                              
      if (stringa.indexOf('/') >= 0) {                                                                                                                                                                                              
        esistesep = '*';                                                                                                                                                                                                            
      }                                                                                                                                                                                                                             
      if (stringa.indexOf('-') >= 0) {                                                                                                                                                                                              
        if (esistesep == '*') {                                                                                                                                                                                                     
          box.focus();                                                                                                                                                                                                              
          erroresep2();                                                                                                                                                                                                             
          return false;                                                                                                                                                                                                             
        }                                                                                                                                                                                                                           
        esistesep = '*';                                                                                                                                                                                                            
      }                                                                                                                                                                                                                             
      if (stringa.indexOf('.') >= 0) {                                                                                                                                                                                              
        if (esistesep == '*') {                                                                                                                                                                                                     
          box.focus();                                                                                                                                                                                                              
          erroresep2();                                                                                                                                                                                                             
          return false;                                                                                                                                                                                                             
        }                                                                                                                                                                                                                           
        esistesep = '*';                                                                                                                                                                                                            
      }                                                                                                                                                                                                                             
      if (stringa.indexOf(',') >= 0) {                                                                                                                                                                                              
        if (esistesep == '*') {                                                                                                                                                                                                     
          box.focus();                                                                                                                                                                                                              
          erroresep2();                                                                                                                                                                                                             
          return false;                                                                                                                                                                                                             
        }                                                                                                                                                                                                                           
        esistesep = '*';                                                                                                                                                                                                            
      }                                                                                                                                                                                                                             
      if (stringa.indexOf('/') == stringa.lastIndexOf('/') &&                                                                                                                                                                       
          stringa.indexOf('-') == stringa.lastIndexOf('-') &&                                                                                                                                                                       
          stringa.indexOf('.') == stringa.lastIndexOf('.') &&                                                                                                                                                                       
          stringa.indexOf(',') == stringa.lastIndexOf(',')) {                                                                                                                                                                       
        box.focus();                                                                                                                                                                                                                
        erroresep1();                                                                                                                                                                                                               
        return false;                                                                                                                                                                                                               
      }                                                                                                                                                                                                                             
    }                                                                                                                                                                                                                               
    // se ci sono separatori devo verificare che ci siano almeno 2 cifre tra uno e l'altro                                                                                                                                          
    if (stringa.indexOf('/') >= 0) {                                                                                                                                                                                                
      stringa = correggidata('/',stringa);                                                                                                                                                                                          
    }                                                                                                                                                                                                                               
    if (stringa.indexOf('-') >= 0) {                                                                                                                                                                                                
      stringa = correggidata('-',stringa);                                                                                                                                                                                          
    }                                                                                                                                                                                                                               
    if (stringa.indexOf('.') >= 0) {                                                                                                                                                                                                
      stringa = correggidata('.',stringa);                                                                                                                                                                                          
    }                                                                                                                                                                                                                               
    if (stringa.indexOf('.') >= 0) {                                                                                                                                                                                                
      stringa = correggidata(',',stringa);                                                                                                                                                                                          
    }                                                                                                                                                                                                                               
    // se ci sono separatori li levo                                                                                                                                                                                                
    for (;stringa.indexOf('/') >= 0;) {                                                                                                                                                                                             
      stringa = stringa.replace("/","");                                                                                                                                                                                            
    }                                                                                                                                                                                                                               
    for (;stringa.indexOf('-') >= 0;) {                                                                                                                                                                                             
      stringa = stringa.replace("-","");                                                                                                                                                                                            
    }                                                                                                                                                                                                                               
    for (;stringa.indexOf('.') >= 0;) {                                                                                                                                                                                             
      stringa = stringa.replace(".","");                                                                                                                                                                                            
    }                                                                                                                                                                                                                               
    for (;stringa.indexOf(',') >= 0;) {                                                                                                                                                                                             
      stringa = stringa.replace(",","");                                                                                                                                                                                            
    }                                                                                                                                                                                                                               
    document.getElementById(idfieldnum).value = stringa;                                                                                                                                                                            
    return true;                                                                                                                                                                                                                    
  }                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                    
  function correggidata(separatore,strdata) {                                                                                                                                                                                       
    // se il primo separatore si trova in posizione < 4 assumo che siamo in formato gg-mm-aaaa                                                                                                                                      
    if (strdata.indexOf(separatore,0) < 4) {                                                                                                                                                                                        
      // verifico che il separatore non si trovi in posizione 1 e 4                                                                                                                                                                 
      if (strdata.indexOf(separatore,0) == 1) {      //manca lo zero iniziale nel giorno                                                                                                                                            
        strdata = '0' + strdata;                                                                                                                                                                                                    
      }                                                                                                                                                                                                                             
      if (strdata.indexOf(separatore,3) == 4) {      //manca lo zero iniziale nel mese                                                                                                                                              
        strdata = strdata.substr(0,3) + '0' + strdata.substr(3);                                                                                                                                                                    
      }                                                                                                                                                                                                                             
    }                                                                                                                                                                                                                               
    // se il primo separatore si trova in posizione = 4 assumo che siamo in formato aaaa-mm-gg                                                                                                                                      
    if (strdata.indexOf(separatore,0) == 4) {                                                                                                                                                                                       
      // verifico che il separatore non si trovi in posizione 6 e che l'ultima cifra non sia in pos 8                                                                                                                               
      if (strdata.indexOf(separatore,5) == 6) {      //manca lo zero iniziale nel mese                                                                                                                                              
        strdata = strdata.substr(0,5) + '0' + strdata.substr(5);                                                                                                                                                                    
      }                                                                                                                                                                                                                             
      if (strdata.length == 9) {      //manca lo zero iniziale nel giorno                                                                                                                                                           
        strdata = strdata.substr(0,7) + '0' + strdata.substr(8);                                                                                                                                                                    
      }                                                                                                                                                                                                                             
    }                                                                                                                                                                                                                               
    return strdata;                                                                                                                                                                                                                 
  }                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                    
  function erroresep1() {                                                                                                                                                                                                           
    var testo="";                                                                                                                                                                                                                   
    if (linguamsg == 'IT') {                                                                                                                                                                                                        
      testo='Manca un carattere separatore';                                                                                                                                                                                        
    }                                                                                                                                                                                                                               
    if (linguamsg == 'EN') {                                                                                                                                                                                                        
      testo='Lost one separator character ';                                                                                                                                                                                        
    }                                                                                                                                                                                                                               
    alert(testo);                                                                                                                                                                                                                   
  }                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                    
  function erroresep2() {                                                                                                                                                                                                           
    if (linguamsg == 'IT') {                                                                                                                                                                                                        
      testo='Non utilizzare caratteri separatori misti';                                                                                                                                                                            
    }                                                                                                                                                                                                                               
    if (linguamsg == 'EN') {                                                                                                                                                                                                        
      testo='Mixed separator character not available';                                                                                                                                                                              
    }                                                                                                                                                                                                                               
    alert(testo);                                                                                                                                                                                                                   
  }                                                                                                                                                                                                                                 

  function cvtmesnum(idfielddat,idfieldnum) {                                                                                                                                                                                       
    // verifica e convere io periodo nel formato mese/anno e settimana/anno                                                                                                                                                         
    box = document.getElementById(idfielddat);                                                                                                                                                                                      
    stringa = document.getElementById(idfielddat).value;                                                                                                                                                                            
    // trasformo in minuscolo per facilitare il compito di trasformazione dei mesi espliciti                                                                                                                                        
    stringa = stringa.toLowerCase();                                                                                                                                                                                                
    mese1 = ["january", "february", "march", "april", "may", "june",                                                                                                                                                                
            "july", "august", "september", "october", "november", "december"];                                                                                                                                                      
    mese2 = ["jan", "feb", "mar", "apr", "may", "jun",                                                                                                                                                                              
            "jul", "aug", "sep", "oct", "nov", "dec"];                                                                                                                                                                              
    mese3 = ["gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno",                                                                                                                                                          
            "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre"];                                                                                                                                                    
    mese4 = ["gen", "feb", "mar", "apr", "mag", "giu",                                                                                                                                                                              
            "lug", "ago", "set", "ott", "nov", "dic"];                                                                                                                                                                              
    for (icvtdatnum=0;icvtdatnum<12;icvtdatnum++) {                                                                                                                                                                                 
      nummes = icvtdatnum + 1;                                                                                                                                                                                                      
      if (nummes < 10) {                                                                                                                                                                                                            
        stringa = stringa.replace(mese1[icvtdatnum],'0' + nummes.toString());                                                                                                                                                       
        stringa = stringa.replace(mese3[icvtdatnum],'0' + nummes.toString());                                                                                                                                                       
        stringa = stringa.replace(mese2[icvtdatnum],'0' + nummes.toString());                                                                                                                                                       
        stringa = stringa.replace(mese4[icvtdatnum],'0' + nummes.toString());                                                                                                                                                       
      }                                                                                                                                                                                                                             
      else {                                                                                                                                                                                                                        
        stringa = stringa.replace(mese1[icvtdatnum],nummes);                                                                                                                                                                        
        stringa = stringa.replace(mese3[icvtdatnum],nummes);                                                                                                                                                                        
        stringa = stringa.replace(mese2[icvtdatnum],nummes);                                                                                                                                                                        
        stringa = stringa.replace(mese4[icvtdatnum],nummes);                                                                                                                                                                        
      }                                                                                                                                                                                                                             
    }                                                                                                                                                                                                                               
    // elimino gli eventuali spazi esistenti tra giorno mese ed anno                                                                                                                                                                
    for (;stringa.indexOf(' ') >= 0;) {                                                                                                                                                                                             
      stringa = stringa.replace(" ","");                                                                                                                                                                                            
    }                                                                                                                                                                                                                               
    // se ci sono separatori devo verificare che sia solo 1 e nella posizione giusta                                                                                                                                                
    if (stringa.indexOf('/') >= 0 || stringa.indexOf('-') >= 0                                                                                                                                                                      
        || stringa.indexOf('.') >= 0 || stringa.indexOf(',') >= 0) {                                                                                                                                                                
      esistesep = ' ';                                                                                                                                                                                                              
      if (stringa.indexOf('/') >= 0) {                                                                                                                                                                                              
        if (stringa.indexOf('/') != stringa.lastIndexOf('/')) {                                                                                                                                                                     
          erroresepm1();                                                                                                                                                                                                            
          box.focus();                                                                                                                                                                                                              
          return false;                                                                                                                                                                                                             
        }                                                                                                                                                                                                                           
      if (stringa.indexOf('/') > 2) {                                                                                                                                                                                               
          erroresepm2();                                                                                                                                                                                                            
          box.focus();                                                                                                                                                                                                              
          return false;                                                                                                                                                                                                             
      }                                                                                                                                                                                                                             
        esistesep = '*';                                                                                                                                                                                                            
      }                                                                                                                                                                                                                             
      if (stringa.indexOf('-') >= 0) {                                                                                                                                                                                              
        if (esistesep == '*') {                                                                                                                                                                                                     
          erroresepm1();                                                                                                                                                                                                            
          box.focus();                                                                                                                                                                                                              
          return false;                                                                                                                                                                                                             
        }                                                                                                                                                                                                                           
        if (stringa.indexOf('-') != stringa.lastIndexOf('-')) {                                                                                                                                                                     
          erroresepm1();                                                                                                                                                                                                            
          box.focus();                                                                                                                                                                                                              
          return false;                                                                                                                                                                                                             
        }                                                                                                                                                                                                                           
        if (stringa.indexOf('-') > 2) {                                                                                                                                                                                             
          erroresepm2();                                                                                                                                                                                                            
          box.focus();                                                                                                                                                                                                              
          return false;                                                                                                                                                                                                             
        }                                                                                                                                                                                                                           
        esistesep = '*';                                                                                                                                                                                                            
      }                                                                                                                                                                                                                             
      if (stringa.indexOf('.') >= 0) {                                                                                                                                                                                              
        if (esistesep == '*') {                                                                                                                                                                                                     
          erroresepm1();                                                                                                                                                                                                            
          box.focus();                                                                                                                                                                                                              
          return false;                                                                                                                                                                                                             
        }                                                                                                                                                                                                                           
        if (stringa.indexOf('.') != stringa.lastIndexOf('.')) {                                                                                                                                                                     
          erroresepm1();                                                                                                                                                                                                            
          box.focus();                                                                                                                                                                                                              
          return false;                                                                                                                                                                                                             
        }                                                                                                                                                                                                                           
        if (stringa.indexOf('.') > 2) {                                                                                                                                                                                             
          erroresepm2();                                                                                                                                                                                                            
          box.focus();                                                                                                                                                                                                              
          return false;                                                                                                                                                                                                             
        }                                                                                                                                                                                                                           
        esistesep = '*';                                                                                                                                                                                                            
      }                                                                                                                                                                                                                             
      if (stringa.indexOf(',') >= 0) {                                                                                                                                                                                              
        if (esistesep == '*') {                                                                                                                                                                                                     
          erroresepm1();                                                                                                                                                                                                            
          box.focus();                                                                                                                                                                                                              
          return false;                                                                                                                                                                                                             
        }                                                                                                                                                                                                                           
        if (stringa.indexOf(',') != stringa.lastIndexOf(',')) {                                                                                                                                                                     
          erroresepm1();                                                                                                                                                                                                            
          box.focus();                                                                                                                                                                                                              
          return false;                                                                                                                                                                                                             
        }                                                                                                                                                                                                                           
        if (stringa.indexOf(',') > 2) {                                                                                                                                                                                             
          erroresepm2();                                                                                                                                                                                                            
          box.focus();                                                                                                                                                                                                              
          return false;                                                                                                                                                                                                             
        }                                                                                                                                                                                                                           
        esistesep = '*';                                                                                                                                                                                                            
      }                                                                                                                                                                                                                             
    }                                                                                                                                                                                                                               
    // se c'è il separatore devo verificare che ci siano almeno 2 cifre prima                                                                                                                                                       
    if (stringa.indexOf('/') >= 0) {                                                                                                                                                                                                
      stringa = correggimese('/',stringa);                                                                                                                                                                                          
    }                                                                                                                                                                                                                               
    if (stringa.indexOf('-') >= 0) {                                                                                                                                                                                                
      stringa = correggimese('-',stringa);                                                                                                                                                                                          
    }                                                                                                                                                                                                                               
    if (stringa.indexOf('.') >= 0) {                                                                                                                                                                                                
      stringa = correggimese('.',stringa);                                                                                                                                                                                          
    }                                                                                                                                                                                                                               
    if (stringa.indexOf('.') >= 0) {                                                                                                                                                                                                
      stringa = correggimese(',',stringa);                                                                                                                                                                                          
    }                                                                                                                                                                                                                               
    // se c'è il separatore lo levo                                                                                                                                                                                                 
    for (;stringa.indexOf('/') >= 0;) {                                                                                                                                                                                             
      stringa = stringa.replace("/","");                                                                                                                                                                                            
    }                                                                                                                                                                                                                               
    for (;stringa.indexOf('-') >= 0;) {                                                                                                                                                                                             
      stringa = stringa.replace("-","");                                                                                                                                                                                            
    }                                                                                                                                                                                                                               
    for (;stringa.indexOf('.') >= 0;) {                                                                                                                                                                                             
      stringa = stringa.replace(".","");                                                                                                                                                                                            
    }                                                                                                                                                                                                                               
    for (;stringa.indexOf(',') >= 0;) {                                                                                                                                                                                             
      stringa = stringa.replace(",","");                                                                                                                                                                                            
    }                                                                                                                                                                                                                               
    if (stringa.length != 6) {  //verifico che alla fin fine siano stati inseriti 6 caratteri
      erroresepm2();                                                                                                                                                                                                                
      box.focus();                                                                                                                                                                                                                  
      return false;                                                                                                                                                                                                                 
    }                                                                                                                                                                                                                               
    document.getElementById(idfieldnum).value = stringa;                                                                                                                                                                            
    return true;                                                                                                                                                                                                                    
  }                                                                                                                                                                                                                                 
  function correggimese(separatore,strdata) {                                                                                                                                                                                       
    // verifico che il separatore non si trovi in posizione 1                                                                                                                                                                       
    if (strdata.indexOf(separatore,0) == 1) {      //manca lo zero iniziale nel mese o settimana                                                                                                                                    
      strdata = '0' + strdata;                                                                                                                                                                                                      
    }                                                                                                                                                                                                                               
    return strdata;                                                                                                                                                                                                                 
  }                                                                                                                                                                                                                                 
  function erroresepm1() {                                                                                                                                                                                                          
    var testo="";                                                                                                                                                                                                                   
    if (linguamsg == 'IT') {                                                                                                                                                                                                        
      testo='Indicare un solo carattere separatore';                                                                                                                                                                                
    }                                                                                                                                                                                                                               
    if (linguamsg == 'EN') {                                                                                                                                                                                                        
      testo='Enter only one separator character ';                                                                                                                                                                                  
    }                                                                                                                                                                                                                               
    alert(testo);                                                                                                                                                                                                                   
  }                                                                                                                                                                                                                                 
  function erroresepm2() {                                                                                                                                                                                                          
    var testo="";                                                                                                                                                                                                                   
    if (linguamsg == 'IT') {                                                                                                                                                                                                        
      testo='Formato periodo errato, utilizzare mm/aaaa';                                                                                                                                                                           
    }                                                                                                                                                                                                                               
    if (linguamsg == 'EN') {                                                                                                                                                                                                        
      testo='Period layout wrong, use mm/yyyy)';                                                                                                                                                                                    
    }                                                                                                                                                                                                                               
    alert(testo);                                                                                                                                                                                                                   
  }                                                                                                                                                                                                                                 

  /* ========================================================================= */
  /* Apre e inizializza la finestra del calendario */
  /* fieldid = id del campo data                   */
  /* buttonid = id del bottone che apre il calendario */
  /* showtime = true x visualizzare l'ora else false */
  /* ========================================================================= */
  function apricalendar(fieldid,buttonid,showtime,Format,iniday) {
    if (!Format) {
      Format="%d/%m/%Y";
    }
    if (!iniday) {
      iniday=1;      //lunedì
    }
    // ¥ viene sostituito con / xchè non posso mettere direttamente /% nel html altrimenti lo interpreta
    // come una variabile che deve essere riempita dal programma cgi e fa casino
    for (;Format.indexOf('¥') >= 0;) {
      Format = Format.replace('¥','/');
    }
    if (showtime) {
    Format=Format+" %H:%M";
    }
    Calendar.setup({
         inputField     :    fieldid,               // id of the input field
         ifFormat       :    Format,                // format of the input field
         daFormat       :    Format,                // format of the display data
         button         :    buttonid,              // trigger for the calendar (button ID)
         firstDay       :    iniday,                // 1 = monday
         align          :    "Bl",                  // alignment (defaults to "Bl")
         singleClick    :    true,
         electric       :    false,
         showsTime      :    showtime
    });
  }

  function impostaavanti() {
    // se premuto avanti, imposto il campo funzione con la dicitura "avanti" (usato nei bottoni)
    if (document.getElementById("funzchoice")) {                     // se esiste l'id
        document.getElementById("funzchoice").setAttribute("value",
                  "avanti");
    }
  }

  function impostavuoto() {
    // per vuotare il campo funzione in modo che venga eseguito lo sblocco in caso di chiusura con x della finestra
    if (document.getElementById("funzchoice")) {                     // se esiste l'id
        document.getElementById("funzchoice").setAttribute("value",
                  "");
    }
  }

  function impostagestione() {                                                                                                                                                                                                      
    // se premuto bottone gestione imposto il campo funzione con la dicitura "gestione"                                                                                                                                             
    if (document.getElementById("funzchoice")) {                     // se esiste l'id                                                                                                                                              
        document.getElementById("funzchoice").setAttribute("value",                                                                                                                                                                 
                  "gestione");                                                                                                                                                                                                      
    }                                                                                                                                                                                                                               
  }                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                    
  function impostasalva() {                                                                                                                                                                                                         
    // se premuto bottone salva imposto il campo funzione con la dicitura "salva"                                                                                                                                                   
    if (document.getElementById("funzchoice")) {                     // se esiste l'id                                                                                                                                              
        document.getElementById("funzchoice").setAttribute("value",                                                                                                                                                                 
                  "salva");                                                                                                                                                                                                         
    }                                                                                                                                                                                                                               
  }                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                    
  function impostacancella() {                                                                                                                                                                                                      
    // se premuto bottone cancellazione imposto il campo funzione con la dicitura "cancella"                                                                                                                                        
    if (document.getElementById("funzchoice")) {                     // se esiste l'id                                                                                                                                              
        document.getElementById("funzchoice").setAttribute("value",                                                                                                                                                                 
                  "cancella");                                                                                                                                                                                                      
    }                                                                                                                                                                                                                               
  }                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                    
  function impostaassocia() {                                                                                                                                                                                                       
    // se premuto bottone conferma associazione imposto il campo funzione con la dicitura "associa"                                                                                                                                 
    if (document.getElementById("funzchoice")) {                     // se esiste l'id                                                                                                                                              
        document.getElementById("funzchoice").setAttribute("value",                                                                                                                                                                 
                  "associa");                                                                                                                                                                                                       
    }                                                                                                                                                                                                                               
  }                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                    
  function impostavisualizza() {                                                                                                                                                                                                    
    // se premuto bottone visualizzazione imposto il campo funzione con la dicitura "visualizza"                                                                                                                                    
    if (document.getElementById("funzchoice")) {                     // se esiste l'id                                                                                                                                              
        document.getElementById("funzchoice").setAttribute("value",                                                                                                                                                                 
                  "visualizza");                                                                                                                                                                                                    
    }                                                                                                                                                                                                                               
  }                                

  function testcampi(numform,atleastone,impavanti) {                                                                                                                                                                                
  //------------------------------------------------------------------------------------------                                                                                                                                      
  // questo gruppo di statement serve per l'inclusione dei controlli standard delle form                                                                                                                                            
  // presuppone l'utilizzo di sngfield, txtfield, selfield ... per la definizione dei campi                                                                                                                                         
  //                                                                                                                                                                                                                                
  // variabili utilizzate in richiamo                                                                                                                                                                                               
  //   atleastone=true        se deve essere controllato che ce ne sia almeno uno non obbligatorio pieno                                                                                                                            
  //----------------------------------------------------------------------------------------------------                                                                                                                            
    loading();                                                                                                                                                                                                               
    var xtestcampi = document.forms[numform].elements.length;               // quanti sono i campi della form  
    var almenouno = " ";                                                                                                                                                                                                           
    for (itestcampi=0;itestcampi<xtestcampi;itestcampi++) {                                                                                                                                                                         
        box = document.forms[numform].elements[itestcampi];                                                                                                                                                                         
        if (box.type == "submit" || box.type == "button") {                                                                                                                                                                         
           continue;                                                                                                                                                                                                                
        }                                                                                                                                                                                                                             
        mandatoryid = box.id + 'obb';         //nome del campo hidden mandatory                                                                                                                                                     
        if (!document.getElementById(mandatoryid)) {        // se non esiste imposto non obbligatorio                                                                                                                               
          mandatory = 'n';                                                                                                                                                                                                          
        }                                                                                                                                                                                                                           
        else {                                                                                                                                                                                                                      
          mandatory = document.getElementById(mandatoryid).value; //nel campo hidden il flag obbligatorio                                                                                                                           
          if (box.value != "") {                                                                                                                                                                                                    
            if (atleastone  && mandatory != "y") {                                                                                                                                                                                  
              almenouno = "*";                                                                                                                                                                                                      
            }                                                                                                                                                                                                                       
          }                                                                                                                                                                                                                         
        }                                                                                                                                                                                                                           
        datacvtid = box.id + 'cvt';         //nome del campo hidden che dice se è data da convertire                                                                                                                                
        if (!document.getElementById(datacvtid)) {        // se non esiste imposto non è data                                                                                                                                       
          datacvt = 'n';                                                                                                                                                                                                            
        }                                                                                                                                                                                                                           
        else {                                                                                                                                                                                                                      
          datacvt = document.getElementById(datacvtid).value; //nel campo hidden il flag data da convert                                                                                                                            
        }                                                                                                                                                                                                                           
        if (box.type != "hidden") {                                                                                                                                                                                                
          var valore = box.value;                                                                                                                                                                                                   
          if (valore.indexOf('+') >= 0) {    //non si può usare il "+"                                                                                                                                                              
            if (linguamsg == 'IT') {                                                                                                                                                                                                
              testo='Carattere + non utilizzabile ' + box.title;                                                                                                                                                                    
            }                                                                                                                                                                                                                       
            if (linguamsg == 'EN') {                                                                                                                                                                                                
             testo='Character + not available' + box.title;                                                                                                                                                                         
            }                                                                                                                                                                                                                       
            alert(testo);                                                                                                                                                                                                           
            box.focus();                                                                                                                                                                                                            
            unloading();                                                                                                                                                                                                            
            return false;                                                                                                                                                                                                           
          }                                                                                                                                                                                                                         
          if (valore.indexOf('&') >= 0) {    //non si può usare il "&"                                                                                                                                                              
            if (linguamsg == 'IT') {                                                                                                                                                                                                
              testo='Carattere & non utilizzabile ' + box.title;                                                                                                                                                                    
            }                                                                                                                                                                                                                       
            if (linguamsg == 'EN') {                                                                                                                                                                                                
              testo='Character & not available' + box.title;                                                                                                                                                                        
            }                                                                                                                                                                                                                       
            alert(testo);                                                                                                                                                                                                           
            box.focus();                                                                                                                                                                                                            
            unloading();                                                                                                                                                                                                            
            return false;                                                                                                                                                                                                           
          }                                                                                                                                                                                                                         
        }                                                                                                                                                                                                                           
        // conversione delle date in numero e controlli                                                                                                                                                                             
        if (datacvt == "y") {                         //data da convertire/controllare                                                                                                                                              
          test = cvtdatnum(box.id,box.id + "num");                                                                                                                                                                                  
          if (!test) {                                                                                                                                                                                                              
            box.focus();                                                                                                                                                                                                            
            unloading();                                                                                                                                                                                                            
            return false;                                                                                                                                                                                                           
          }                                                                                                                                                                                                                         
        }                                                                                                                                                                                                                           
        if (datacvt == "m") {                         //mese o settimana da convertire/controllare                                                                                                                                  
          test = cvtmesnum(box.id,box.id + "num");                                                                                                                                                                                  
          if (!test) {                                                                                                                                                                                                              
            box.focus();                                                                                                                                                                                                            
            unloading();                                                                                                                                                                                                            
            return false;                                                                                                                                                                                                           
          }                                                                                                                                                                                                                         
        }                                                                                                                                                                                                                           
        if (mandatory != "y") {                                                                                                                                                                                                     
           continue;                                                                                                                                                                                                                
        }                                                                                                                                                                                                                           
        if (box.value == "") {                                                                                                                                                                                                      
          if (linguamsg == 'IT') {                                                                                                                                                                                                  
            testo='Inserire il dato mancante  in ' + box.title;                                                                                                                                                                     
          }                                                                                                                                                                                                                         
          if (linguamsg == 'EN') {                                                                                                                                                                                                  
            testo='Enter missing data in ' + box.title;                                                                                                                                                                             
          }                                                                                                                                                                                                                         
          alert(testo);                                                                                                                                                                                                             
          box.focus();                                                                                                                                                                                                              
          unloading();                                                                                                                                                                                                              
          return false;                                                                                                                                                                                                             
        }                                                                                                                                                                                                                           
    }                                                                                                                                                                                                                               
    if (atleastone && almenouno != "*") {                                                                                                                                                                                           
      if (linguamsg == 'IT') {                                                                                                                                                                                                      
        testo='Inserire almeno una richiesta';                                                                                                                                                                                      
      }                                                                                                                                                                                                                             
      if (linguamsg == 'EN') {                                                                                                                                                                                                      
        testo='Enter at least one request';                                                                                                                                                                                         
      }                                                                                                                                                                                                                             
      alert(testo);                                                                                                                                                                                                                 
      box.focus();                                                                                                                                                                                                                  
      unloading();                                                                                                                                                                                                                  
      return false;                                                                                                                                                                                                                 
    }                                                                                                                                                                                                                               
    else {                                                                                                                                                                                                                          
      if (impavanti) {                                                                                                                                                                                                              
        impostaavanti();
      }                                                                                                                                                                                                                             
      return true;                                                                                                                                                                                                                  
    }                                                                                                                                                                                                                               
  }                                                                                                                                                                                                                                 

  function testcampiconf(numform,atleastone,impavantic) {                                                                                                                                                                           
//----------------------------------------------------------------------------------------------------**                                                                                                                            
// questo gruppo di statement serve per l'inclusione dei controlli standard delle form                **                                                                                                                            
// e in più richiede la conferma dell'aggiornamento                                                   **                                                                                                                            
// presuppone l'utilizzo di sngfield, txtfield, selfield ... per la definizione dei campi             **                                                                                                                            
//                                                                                                    **                                                                                                                            
// variabili utilizzate in richiamo                                                                                                                                                                                                 
//   testoconfit          testo da emettere nella richiesta di conferma in italiano                                                                                                                                                 
//   testoconfen          testo da emettere nella richiesta di conferma in inglese                                                                                                                                                  
//   atleastone=true      se deve essere controllato che ce ne sia almeno uno non obbligatorio pieno**                                                                                                                              
//----------------------------------------------------------------------------------------------------**                                                                                                                            
    loading();                                                                                                                                                                                                                      
    if (!testcampi(numform,atleastone,false)) {
      unloading();                                                                                                                                                                                                                  
      return false;                                                                                                                                                                                                                
    }                                                                                                                                                                                                                   
    if (linguamsg == 'IT') {                                                                                                                                                                                                        
      testo=testoconfit;                                                                                                                                                                                                            
    }                                                                                                                                                                                                                               
    if (linguamsg == 'EN') {                                                                                                                                                                                                        
      testo=testoconfen;                                                                                                                                                                                                            
    }                                                                                                                                                                                                                               
    if (confirm(testo)) {                                                                                                                                                                                                           
      if (impavantic) {                                                                                                                                                                                                             
        impostaavanti();
      }                                                                                                                                                                                                                             
      return true;                                                                                                                                                                                                                  
    }                                                                                                                                                                                                                               
    else {
      unloading();                                                                                                                                                                                                                  
      return false;                                                                                                                                                                                                                 
    }                                                                                                                                                                                                                               
  }                                                                                                                                                                                                                                 

  function winstilestd() {                                                                                                                                                                                                          
//----------------------------------------------------------------------------------------------------**                                                                                                                            
// questa funzione prepara posizionamento e stile delle finestre di interrogazione                    **                                                                                                                            
//----------------------------------------------------------------------------------------------------**                                                                                                                            
    valheight = 500;
    valwidth = 800;                                                                                                                                                                                                                 
    posleft = Math.floor((screen.width-valwidth));                                                                                                                                                                                  
    postop = Math.floor((screen.height-valheight)/3);                                                                                                                                                                               
    return stile = 'height=' + valheight + ',width=' + valwidth + ',top=' + postop + ',left=' + posleft                                                                                                                             
            + ',scrollbars=yes,resizable=yes,status=yes,location=no,toolbar=no';                                                                                                                                                    
  }                                                                                                                                                                                                                                 

  function winstilepers(divisore,posright) {                                                                                                                                                                                        
//----------------------------------------------------------------------------------------------------**                                                                                                                            
// questa funzione prepara posizionamento e stile delle finestre di interrogazione personalizzato     **                                                                                                                            
//----------------------------------------------------------------------------------------------------**                                                                                                                            
    if (divisore == 0) {
      divisore = 1;
    }                                                                                                                                                                                                                               
    valheight = 500;
    valwidth = 800;                                                                                                                                                                                                                 
    if (posright == 'y') {            //posizionamento tutto a dx
      posleft = 1;                                                                                                                                                                                                                  
    }                                                                                                                                                                                                                               
    else  {
      posleft = Math.floor((screen.width-valwidth));                                                                                                                                                                                
    }                                                                                                                                                                                                                               
    postop = Math.floor((screen.height-valheight)/divisore);                                                                                                                                                                        
    return stile = 'height=' + valheight + ',width=' + valwidth + ',top=' + postop + ',left=' + posleft                                                                                                                             
            + ',scrollbars=yes,resizable=yes,status=yes,location=no,toolbar=no';                                                                                                                                                    
  }                                                                                                                                                                                                                                 

  function winstilesearch() {                                                                                                                                                                                                       
//----------------------------------------------------------------------------------------------------**                                                                                                                            
// questa funzione prepara posizionamento e stile delle finestre di ricerca con autocompletamento     **                                                                                                                            
//----------------------------------------------------------------------------------------------------**                                                                                                                            
    valheight = 350;
    valwidth = 700;                                                                                                                                                                                                                 
    posleft = Math.floor((screen.width-valwidth));                                                                                                                                                                                  
    postop = Math.floor((screen.height-valheight)/3);                                                                                                                                                                               
    return stile = 'height=' + valheight + ',width=' + valwidth + ',top=' + postop + ',left=' + posleft                                                                                                                             
            + ',scrollbars=yes,resizable=yes,status=yes,location=no,toolbar=no';                                                                                                                                                    
  }                                                                                                                                                                                                                                 

  function callajaxsync(pgmtocall,stringpar,metodo) {                                                                                                                                                                               
//----------------------------------------------------------------------------------------------------**                                                                                                                            
// funzione che esegue la chiamata ajax XMLHttpRequest() in modalità sincrona                                                                                                                                                       
// ritorna true/false e riempie variabile rsptexthttprequest col contenuto di responseText                                                                                                                                          
// metodo può assumere i valori get o post                                                                                                                                                                                          
//----------------------------------------------------------------------------------------------------**                                                                                                                            
    if (!metodo) {
      metodo = 'post';
    }
    // se contiene caratteri speciali vanno sostituiti con l'equivalente ascii esadecimale
    // con il metodo GET i parametri sono nell'url
    if (metodo == 'get') {
      pgmtocall = replcarspecajax(pgmtocall);
      application = replcarspec(application);
      application = application.replace(replugua,"%2D");
    }else{
    // con il metodo POST i parametri sono in una variabile a parte
      stringpar = replcarspecajax(stringpar);
      application = pgmtocall;
    }
    var client = false;
    if (window.XMLHttpRequest){
      var client = new XMLHttpRequest();
    }else{
      var client = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (!client) {
      ermsg = 'Errore in inizializzazione XMLHttpRequest';
      unloading();
      alert(ermsg);
      return false;
    }
    // metodo GET
    if (metodo == 'get') {
      client.open("GET",pgmtocall,false);
      client.send(null);
    }else{
    // metodo POST
      client.open("POST",pgmtocall,false);
      //verifica se Firefox 3.xx o maggiore per correggere problema caratteri speciali in utf-8
      if (ffversion>=3) {
        client.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        client.overrideMimeType("text/html; charset=UTF-8");
      }else{
        client.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      }
      client.setRequestHeader("Content-Length", stringpar.length);
      client.send(stringpar);
    }
    if (client.status != 200) {          //ricevuto errore
      ermsg = application + ' ' + client.status + ': ' + client.statusText;
      unloading();
      alert(ermsg);
      return false;
    }
    //riempio le variabili globali con i valori che possono servire ed esco
    statushttprequest = client.status;
    ststexthttprequest = client.statusText;
    rsptexthttprequest = client.responseText;
    return true;
  }                                                                                                                                                                                                                                 

  function callajaxAsync(pgmtocall,stringpar,metodo,funzione) {                                                                                                                                                                     
//----------------------------------------------------------------------------------------------------**                                                                                                                            
// funzione che esegue la chiamata ajax XMLHttpRequest() in modalità asincrona                                                                                                                                                      
// ritorna true/false e indica quale funzione deve essere eseguita al termine                                                                                                                                                       
// la funzione che verrà eseguita al termine deve contenere il checkchiamataAsync(clientAs)                                                                                                                                         
// metodo può assumere i valori get o post                                                                                                                                                                                          
// funzione = stringa che contiene la funzione da eseguire al termine della chiamata
//----------------------------------------------------------------------------------------------------**                                                                                                                            
    if (!metodo) {
      metodo = 'post';
    }
    // se contiene caratteri speciali vanno sostituiti con l'equivalente ascii esadecimale
    // con il metodo GET i parametri sono nell'url
    if (metodo == 'get') {
      pgmtocall = replcarspecajax(pgmtocall);
      application = replcarspec(application);
      application = application.replace(replugua,"%2D");
    }else{
    // con il metodo POST i parametri sono in una variabile a parte
      stringpar = replcarspecajax(stringpar);
      application = pgmtocall;
    }
    var clientAs = false;
    if (window.XMLHttpRequest){
      var clientAs =  new XMLHttpRequest();
    }else{
      var clientAs = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (!clientAs) {
      ermsg = 'Errore in inizializzazione XMLHttpRequest';
      unloading();
      alert(ermsg);
      return false;
    }
    // metodo GET
    if (metodo == 'get') {
      clientAs.open("GET",pgmtocall,true);
      clientAs.onreadystatechange = function() { eval(funzione); }
      flaghttprequest = '1';       //in esecuzione
      clientAs.send(null);
    }else{
    // metodo POST
      clientAs.open("POST",pgmtocall,true);
      clientAs.onreadystatechange = function() { eval(funzione); }
      //verifica se Firefox 3.xx o maggiore per correggere problema caratteri speciali in utf-8
      if (ffversion>=3) {
        clientAs.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        clientAs.overrideMimeType("text/html; charset=UTF-8");
      }else{
        clientAs.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      }
      clientAs.setRequestHeader("Content-Length", stringpar.length);
      flaghttprequest = '1';       //in esecuzione
      clientAs.send(stringpar);
    }
    return true;
  }                                                                                                                                                                                                                                 

  function checkrisposta(varrisposta,tipochiamata) {                                                                                                                                                                                
//----------------------------------------------------------------------------------------------------**                                                                                                                            
// funzione che controlla se la chiamata xmlhttprequest ha reso rtcd=0                                                                                                                                                              
// emette una segnalazione di errore se si riceve un rtcd<>0 dal programma richiamato                                                                                                                                               
// gestisce anche il messaggio warning, se riceve rtcd=W emette errore ma prosegue ritornando true                                                                                                                                  
// se rtcd=0 ritorna true, se rtcd<>0 rtcd <> W ritorna false                                                                                                                                                                       
// se tipochiamata=A è chiamata asincrona e testa le sue variabili globali specifiche                                                                                                                                               
//----------------------------------------------------------------------------------------------------**                                                                                                                            
    var webapplication;                                                                                                                                                                                                             
    var ermsg;                                                                                                                                                                                                                      
    if (!tipochiamata) {                                                                                                                                                                                                            
      tipochiamata = '';
    }                                                                                                                                                                                                                               
    if (varrisposta != "") {                                                                                                                                                                                                        
      if (varrisposta.indexOf('<body>') >= 0) {    //vecchio metodo - la risposta conteneva la struttura html
        var startparam = parseInt(varrisposta.indexOf('<body>') + 13);                                                                                                                                                              
        var endparam = parseInt(varrisposta.indexOf('</body>') - 8);                                                                                                                                                                
        var lenparam = endparam - startparam;                                                                                                                                                                                       
        var newstringa = varrisposta.substr(startparam,lenparam);                                                                                                                                                                   
      }                                                                                                                                                                                                                             
      else {                                //nuovo metodo - la risposta contiene solo i parametri di ritorno                                                                                                                       
        var newstringa = varrisposta;       // separati da ¶                                                                                                                                                                        
      }                                                                                                                                                                                                                             
      if (tipochiamata == 'A') {          //chiamata asicrona
        rtnparamhttprequestAsync = newstringa.split('¶');   //trasformo in array l'elenco dei parametri                                                                                                                             
        numparamhttprequestAsync = rtnparamhttprequestAsync.length;         //quanti parametri ho ricevuto                                                                                                                          
      }else{                              //chiamata sicrona
        rtnparamhttprequest = newstringa.split('¶');   //trasformo in array l'elenco dei parametri                                                                                                                                  
        numparamhttprequest = rtnparamhttprequest.length;         //quanti parametri ho ricevuto                                                                                                                                    
      }                                                                                                                                                                                                                             
    }                                                                                                                                                                                                                               
    else {                                                                                                                                                                                                                          
      if (tipochiamata == 'A') {          //chiamata asicrona
        numparamhttprequestAsync = 0;                                                                                                                                                                                               
      }else{                              //chiamata sicrona
        numparamhttprequest = 0;                                                                                                                                                                                                    
      }                                                                                                                                                                                                                             
    }                                                                                                                                                                                                                               
    //carico variabili locali
    if (tipochiamata == 'A') {          //chiamata asicrona
      numeroparam = numparamhttprequestAsync;
      arrayparam = rtnparamhttprequestAsync;
    }else{                              //chiamata sicrona
      numeroparam = numparamhttprequest;
      arrayparam = rtnparamhttprequest;
    }                                                                                                                                                                                                                               
    if (numeroparam == 0) {             //non ha ricevuto parametri                                                                                                                                                                 
      return false;                                                                                                                                                                                                                 
    }                                                                                                                                                                                                                               
    var rtnrtcd='0';                                                                                                                                                                                                                
    var pgmdsperror;                                                                                                                                                                                                                
    // reperimento parametri relativi ai messaggi di errore                                                                                                                                                                         
    for(var f=0;f<numeroparam;f++){                                                                                                                                                                                                 
      if (arrayparam[f].indexOf('webapplication=') >= 0)  {                                                                                                                                                                         
        startvalue = parseInt(arrayparam[f].indexOf('webapplication=') + 15);                                                                                                                                                       
        webapplication = arrayparam[f].substr(startvalue);                                                                                                                                                                          
      }                                                                                                                                                                                                                             
      if (arrayparam[f].indexOf('ermsg=') >= 0)  {                                                                                                                                                                                  
        startvalue = parseInt(arrayparam[f].indexOf('ermsg=') + 6);                                                                                                                                                                 
        ermsg = arrayparam[f].substr(startvalue);                                                                                                                                                                                   
      }                                                                                                                                                                                                                             
      if (arrayparam[f].indexOf('rtnrtcd=') >= 0)  {                                                                                                                                                                                
        startvalue = parseInt(arrayparam[f].indexOf('rtnrtcd=') + 8);                                                                                                                                                               
        rtnrtcd = arrayparam[f].substr(startvalue);                                                                                                                                                                                 
      }                                                                                                                                                                                                                             
    }                                                                                                                                                                                                                               
    // se ricevuto errore bloccante emetto segnalazione e blocco                                                                                                                                                                    
    if (rtnrtcd != '0' && rtnrtcd != 0 && rtnrtcd != 'W')  {                                                                                                                                                                        
      unloading();                                                                                                                                                                                                                  
      alert(ermsg);
      return false;                                                                                                                                                                                                                 
    }                                                                                                                                                                                                                               
    else {                                                                                                                                                                                                                          
    // se ricevuto errore warning emetto segnalazione, e richiedo conferma per proseguire                                                                                                                                           
      if (rtnrtcd == 'W')  {                                                                                                                                                                                                        
        conferma = confirm(ermsg);
        if (!conferma) {
          unloading();                                                                                                                                                                                                              
          return false;                                                                                                                                                                                                             
        }                                                                                                                                                                                                                           
      }                                                                                                                                                                                                                             
      return true;                                                                                                                                                                                                                  
    }                                                                                                                                                                                                                               
  }                                                                                                                                                                                                                                 

  function checkrispostaasync(chiamata) {                                                                                                                                                                                           
//----------------------------------------------------------------------------------------------------**                                                                                                                            
// funzione che controlla se la chiamata xmlhttprequest è terminata e ha reso rtcd=0                                                                                                                                                
// emette una segnalazione di errore se si riceve un rtcd<>0 dal programma richiamato                                                                                                                                               
// gestisce anche il messaggio warning, se riceve rtcd=W emette errore ma prosegue ritornando true                                                                                                                                  
// quindi se rtcd=0 ritorna true, se rtcd<>0 rtcd <> W ritorna false
//----------------------------------------------------------------------------------------------------**
    if (chiamata.readyState != 4) {          //non ancora terminato                                                                                                                                                                 
      return false;                                                                                                                                                                                                                 
    }                                                                                                                                                                                                                               
    flaghttprequest = '';       //terminato                                                                                                                                                                                         
    if (chiamata.status != 200) {          //ricevuto errore                                                                                                                                                                        
      ermsg = application + ' ' + chiamata.status + ': ' + chiamata.statusText;
      unloading();                                                                                                                                                                                                                  
      alert(ermsg);
      return false;                                                                                                                                                                                                                 
    }                                                                                                                                                                                                                               
    if (!checkrisposta(chiamata.responseText)) {
      return false;
    }
    else {
      return true;
    }
  }                                                                                                                                                                                                                                 

  function checkchiamataAsync(chiamata) {                                                                                                                                                                                           
//----------------------------------------------------------------------------------------------------**                                                                                                                            
// funzione che controlla se la chiamata xmlhttprequest è terminata                                                                                                                                                                 
// dopo di questa occorre testare i parametri ritornati dal programma richiamato                                                                                                                                                    
// con checkrisposta(rsptexthttprequestAsync,'A')                                                                                                                                                                                   
//----------------------------------------------------------------------------------------------------**                                                                                                                            
    if (chiamata.readyState != 4) {          //non ancora terminato                                                                                                                                                                 
      return false;                                                                                                                                                                                                                 
    }                                                                                                                                                                                                                               
    flaghttprequest = '';       //terminato                                                                                                                                                                                         
    if (chiamata.status != 200             //ricevuto errore                                                                                                                                                                        
       && chiamata.status != 0) {          //se 0 è perchè si è chiuso il pgm prima del termine della chiamata                                                                                                                      
      ermsg = application + ' ' + chiamata.status + ': ' + chiamata.statusText;
      unloading();                                                                                                                                                                                                                  
      alert(ermsg);
      return false;                                                                                                                                                                                                                 
    }                                                                                                                                                                                                                               
    //riempio le variabili globali con i valori che servono ed esco
    statushttprequestAsync = chiamata.status;
    ststexthttprequestAsync = chiamata.statusText;
    rsptexthttprequestAsync = chiamata.responseText;
    return true;                                                                                                                                                                                                                    
  }                                                                                                                                                                                                                                 

  function roundTo(valtoround, posdecimali) {
//----------------------------------------------------------------------------------------------------**                                                                                                                            
// funzione che ritorna la cifra arrotondata al numero di decimali indicato                                                                                                                                                         
// è nel formato javascritp, cioè con il punto come separatore dei decimali                                                                                                                                                         
//----------------------------------------------------------------------------------------------------**                                                                                                                            
    var cifra = valtoround * Math.pow(10,posdecimali);
    cifra = Math.round(cifra);
    return cifra / Math.pow(10,posdecimali);
  }                                                                                                                                                                                                                                 

  function stringatocifra(valtocifra) {
//----------------------------------------------------------------------------------------------------**                                                                                                                            
// funzione che ritorna la cifra senza separatori di migliaia e con punto al posto della                                                                                                                                            
// virgola decimale                                                                                                                                                                                                                 
//----------------------------------------------------------------------------------------------------**                                                                                                                            
    valtocifra = valtocifra.replace(/\./g, "");  //tolgo i punti delle migliaia
    valtocifra = valtocifra.replace(/\,/g, "."); //sostituisco la virgola decimale col punto
    if (isNaN(valtocifra) || valtocifra == '') {
      valtocifra = 0;
     }
    return valtocifra;
  }                                                                                                                                                                                                                                 

  function cifratostringa(valtostringa, emettizero, sepmigliaia, sepdecimali) {
//----------------------------------------------------------------------------------------------------**                                                                                                                            
// funzione che ritorna una stringa contenente la cifra con la virgola decimale                                                                                                                                                     
//----------------------------------------------------------------------------------------------------**                                                                                                                            
    if (!sepdecimali || sepdecimali == '') {      //default = ,
      sepdecimali = ',';
    }                                                                                                                                                                                                                               
    if (!sepmigliaia) {                           //default = .
      sepmigliaia = '.';
    }                                                                                                                                                                                                                               
    if (!emettizero || emettizero == '') {        //default = zero visualizzato
      emettizero = 's';
    }                                                                                                                                                                                                                               
    // se la cifra è zero e non si vuole l'emissione dello zero, pulisco la stringa ed esco
    if (emettizero != 's' && valtostringa == 0) {
      stringa = '';
      return stringa;
    }                                                                                                                                                                                                                               
    stringa = new String(valtostringa);  // trasformo la cifra in stringa
    stringa = stringa.replace(/\./g, sepdecimali); //sostituisco il punto con separatore decimale
    // se richiesti separatori di migliaia, li inserisco
    if (sepmigliaia != '') {
       dp=stringa.indexOf(sepdecimali)!=-1?stringa.substring(0,stringa.indexOf(sepdecimali)).length:stringa.length;
       for (imig=dp-3;imig>0;imig-=3)
         stringa=stringa.substring(0,imig)+sepmigliaia+stringa.substr(imig);
       //numero=numero.replace(/-./,"-");
       stringa=stringa.replace("-"+sepmigliaia,"-");
     }                                                                                                                                                                                                                              
    return stringa;
  }                                                                                                                                                                                                                                 

  function validasessione() {
//----------------------------------------------------------------------------------------------------**                                                                                                                            
// funzione che controlla il timeout del login da extranet, chiudendo la finestra e riportando alla pagina di login                                                                                                                 
//----------------------------------------------------------------------------------------------------**                                                                                                                            
    if (!document.getElementById("vq87sid")) {
      return true;
    }
    if (document.getElementById("vq87sid").value == "") {
      return true;
    }
    pgmtocall = '/cgisrv/vq87rh0c.cgi';
    stringpar = 'lng=' + linguamsg
              + '&vq87sid=' + document.getElementById("vq87sid").value;
    chkajax = callajaxsync(pgmtocall,stringpar,'post');
    if (!chkajax) {
      return false;
    }
    //verifico i parametri ritornati dal programma chiamato
    if (!checkrisposta(rsptexthttprequest)) {
      return false;
    }
    if (numparamhttprequest > 1) {  //se ricevuti parametri
      var rilogin = "";
      var messaggio = "";
      for(var f=0;f<numparamhttprequest;f++){
        if (rtnparamhttprequest[f].indexOf('vq87log=') >= 0)  {
          startvalue = parseInt(rtnparamhttprequest[f].indexOf('vq87log=') + 8);
          rilogin = rtnparamhttprequest[f].substr(startvalue);
        }
        if (rtnparamhttprequest[f].indexOf('vq87msg=') >= 0)  {
          startvalue = parseInt(rtnparamhttprequest[f].indexOf('vq87msg=') + 8);
          messaggio = rtnparamhttprequest[f].substr(startvalue);
        }
      }
      if (rilogin == 'y') {  //occorre rifare il login
        alert(messaggio);
        if (window.sblocco && typeof window.sblocco == "function") {      //eventuale sblocco di entità
          sblocco();
        }
        document.forms[0].action = '/cgifree/vq87rh0.cgi';
        document.forms[0].target = '_self';
        document.forms[0].submit();
        return false;
      }
      if (rilogin == 'c') {  //occorre chiudere la finestra
        alert(messaggio);
        if (window.sblocco && typeof window.sblocco == "function") {      //eventuale sblocco di entità
          sblocco();
        }
        window.close();
        return false;
      }
      return true;
    }
    return true;
  }

  function disabbottoni(elencobottoni) {
    // disabilita i bottoni con id indicati
    var reqbottoniv = elencobottoni.split(',');   //trasformo in array l'elenco degli id
    var numbottoniv = reqbottoniv.length;
    for(var fvariabot=0;fvariabot<numbottoniv;fvariabot++){
      if (document.getElementById(reqbottoniv[fvariabot])) {             //verifico esistenza id bottone
        document.getElementById(reqbottoniv[fvariabot]).disabled = true;
        document.getElementById(reqbottoniv[fvariabot]).style.visibility = "hidden";
      }
    }
  }

  function sblocco() {
    // esegue sblocco prendendo il programma da chiamare dal campo sbloccapgm
    if (!document.getElementById("sbloccopgm")) {           //se non c'è non è da sbloccare
      return true;
    }
    pgmtocallsblo = document.getElementById("sbloccopgm").value;
    identificsblo = document.getElementById("sbloccopar").value;
    if (pgmtocallsblo == "") {                                  //se non è pieno non è da sbloccare
      return true;
    }
    var clientsblo = false;
    if (window.XMLHttpRequest){
      var clientsblo = new XMLHttpRequest();
    }
    else {
      var clientsblo = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (!clientsblo) {                                           //inutile dare errore
      return true;
    }
    identificsblo = replcarspecajax(identificsblo);
    application = pgmtocallsblo;
    clientsblo.open("POST",pgmtocallsblo,false);
    if (ffversion>=3) {
      clientsblo.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
      clientsblo.overrideMimeType("text/html; charset=UTF-8");
    }
    else {
      clientsblo.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    }
    clientsblo.setRequestHeader("Content-Length", identificsblo.length);
    clientsblo.send(identificsblo);
    if (clientsblo.status != 200) {          //ricevuto errore
       ermsg = application + ' ' + clientsblo.status + ': ' + clientsblo.statusText;
       unloading();
       alert(ermsg);
       return false;
     }
     if (!checkrisposta(clientsblo.responseText)) {
       return false;
     }
     return true;
   }

  function checkchar(fieldtexta) {
    // calcola numero caratteri ancora disponibili in textarea durante la digitazione
    originalchars=fieldtexta.value;
    x=originalchars.length;
    chars=fieldtexta.value;
    usacrlf='no';
    maxlunghezza = 0;
    if  (fieldtexta.getAttribute("maxlength") != "" &&     //lunghezza  dichiarata
        fieldtexta.getAttribute("maxlength") != null) {
       maxlunghezza = parseInt(fieldtexta.getAttribute("maxlength"),10);
    }
    if (originalchars.indexOf('\r\n') >= 0)  {
      usacrlf='si';
    }
    // il browser non conta i caratteri "a capo" quindi intercetto LF (per firefox e netscape) e CRLF
    // (per explorer e opera) li sostituisco con 2 caratteri blank
    // perchè quando viene scritto nel file dell'iSeries questo diventa 0D25 (quindi 2 spazi occupati)
    // se usa i soli LF devo però sapere quanti lf ci sono nella textarea x calcolare l'eccedenza
    quantilf=0;
    if (usacrlf == 'no')  {           //se firefox o netscape devo contare quanti lf ci sono
      for (i=0;i<x;i++) {
        if (originalchars[i] == '\n') {       //conto quanti ce ne sono
          quantilf += 1;
        }
      }
    }
    chars=chars.replace(/\r\n/g,"  ");          // non conta gli acapo (CRLF) quindi li sostituisco con 2 blk
    chars=chars.replace(/\n/g,"  ");          // non conta gli acapo (LF x firefox)  quindi li sostituisco con 2 blk
    if (chars.length>maxlunghezza) {           // se arrivato al numero max caratteri, esco
      maxutilizzabile = maxlunghezza - quantilf;
      // tronco l'eccedenza sul campo originale tenendo conto che ogni lf vale due caratteri
      fieldtexta.value=originalchars.substr(0,maxutilizzabile);     //tronco l'eccedenza
      chars=fieldtexta.value;
      chars=chars.replace(/\r\n/g,"  ");        // rifaccio i calcoli sui dati troncati
      chars=chars.replace(/\n/g,"  ");          // rifaccio i calcoli sui dati troncati
      fieldtexta.blur();
    }
    document.getElementById(fieldtexta.id + "charsleft").setAttribute("value", maxlunghezza - chars.length);
  }

  function checkcharini(fieldtextaid) {
    // calcolo numero caratteri disponibili nella textarea in apertura iniziale della pagina
    maxlunghezza = document.getElementById(fieldtextaid).getAttribute("maxlength");
    chars = document.getElementById(fieldtextaid).value;
    // il browser non conta i caratteri "a capo" quindi intercetto LF (per firefox e netscape) e CRLF
    // (per explorer e opera) li sostituisco con 2 caratteri blank
    // perchè quando viene scritto nel file dell'iSeries questo diventa 0D25 (quindi 2 spazi occupati)
    chars=chars.replace(/\r\n/g,"  ");          // non conta gli acapo (CRLF) quindi li sostituisco con 2 blanks
    chars=chars.replace(/\n/g,"  ");          // non conta gli acapo (LF x firefox)  quindi li sostituisco con 2 blanks
    document.getElementById(fieldtextaid + "charsleft").setAttribute("value", maxlunghezza - chars.length);
  }

