    //funzioni comuni a piu' pagine

    var classeCampoFocus = 'focus';
    var classeCampoBlur = 'input';
    var classeCampoReadOnly = 'readonly';

    var rigaTabella = 'rigtb1';
    var rigaTabellaSelezionata = 'rigtbsel';

    var NOME_FINESTRA_POPUP = 'popUpWindow';

    var listaCharOKIntPos = new Array(8,9,13,16,17,18,20,27,33,34,35,36,37,38,39,40,46,48,49,50,51,52,53,54,55,56,57);

    // le pagine che importano questo file possono stare nella pop up
    var POP_UP_PAGE = true;

    var idForm = null;

    function trim(a){
        return a.replace(/^\s+/,'').replace(/\s+$/,'')
    }

    function campoInputOnFocus(obj){
        if (!obj)
            var obj = this;
        obj.className = classeCampoFocus;
    }

    function campoInputOnBlur(obj){
        if (!obj)
            var obj = this;
        obj.className = classeCampoBlur;
    }

    function focusOnSelectedIndex(obj){
        if (!obj)
            obj = this;
        for(var i=0;i<obj.options.length;i++){
            if(obj.options[i].selected){
    //				obj.options[i].style.background = '#ffff99';
                obj.options[i].className=classeCampoFocus;
            } else {
    //				obj.options[i].style.background = '#ffffff';
                obj.options[i].className=classeCampoBlur;
            }
        }

    }

    function blurOnSelectedIndex(obj){
        if (!obj)
            obj = this;
        for(var i=0;i<obj.options.length;i++){
    //			obj.options[i].style.background = '#ffffff';
            obj.options[i].className=classeCampoBlur;
        }
    }

    function initBody(){
        verificaPopUp();
        setFormFieldColor();
        if(idForm){
            var form = document.getElementById(idForm);
            if(form){
                if ((browserUtente.name == BROWSER_EXPLORER)){
                    form.onsubmit=validaForm;
                } else {
                    form.setAttribute('onsubmit', 'return validaForm(this);');
                }
            }
        }
        disabilitaLink();
    }

    function initBodyNoJS(){
    verificaPopUp();
    //setFormFieldColor();
    if(idForm){
        var form = document.getElementById(idForm);
        if(form){
            if ((browserUtente.name == BROWSER_EXPLORER)){
                //alert(idForm);
                form.onsubmit=validaForm;
            } else {
                form.setAttribute('onsubmit', 'return validaForm(this);');
            }
        }
    }
    disabilitaLink();
    }

    function setFormFieldColor(){
        for(var j=0;j<document.forms.length;j++){
            var inputs = document.forms[j].elements;
            for(var i=0;i<inputs.length;i++){
                if ((inputs[i].type == 'text') || (inputs[i].type == 'password')){
                    inputs[i].className=classeCampoBlur;
                    inputs[i].size=inputs[i].size+2;
                    if(inputs[i].readOnly){
                        inputs[i].className=classeCampoReadOnly;
                    }else{
                        if ((browserUtente.name == BROWSER_EXPLORER)){
                            inputs[i].onblur = campoInputOnBlur;
                            inputs[i].onfocus = campoInputOnFocus;
                        } else{
                            inputs[i].setAttribute('onfocus','campoInputOnFocus(this);');
                            inputs[i].setAttribute('onblur','campoInputOnBlur(this);');
                        }
                    }
                } else if (inputs[i].type == 'select-one'){
                    inputs[i].className=classeCampoBlur;
                    if ((browserUtente.name == BROWSER_EXPLORER)){
                        inputs[i].onfocus = focusOnSelectedIndex;
                        inputs[i].onblur = blurOnSelectedIndex;
    //						inputs[i].onchange = focusOnSelectedIndex;
                    } else{
                        inputs[i].setAttribute('onfocus','focusOnSelectedIndex(this);');
                        inputs[i].setAttribute('onblur','blurOnSelectedIndex(this);');
    //						inputs[i].setAttribute('onchange','campoInputOnFocus(this);');
                    }
                }
            }
        }
    }

    function verificaPopUp(){
        var popUp = false;
        try{
            popUp = POP_UP_PAGE;
        }catch(e){
            popUp = false;
        }
        if(window.name==NOME_FINESTRA_POPUP && !popUp){
            self.close();
            // forse non e' il caso di fare il refresh
            if(opener) opener.document.location.reload();
        }
    }

    function selectRow(id){
        var selectedRow = document.getElementById(id);
        if(!selectedRow) return false;
        if(lastRowSelected==null){
            selectedRow.className = rigaTabellaSelezionata;
            lastRowSelected = selectedRow;
        }else if(lastRowSelected != selectedRow){
            selectedRow.className = rigaTabellaSelezionata;
            lastRowSelected.className = rigaTabella;
            lastRowSelected = selectedRow;
        }
        return false;
    }

    //funzione che apre una finestra di dimensioni parametrizzabili
    function openmac(url,win,width,height,scroll) {
        window.open(url,NOME_FINESTRA_POPUP,'width='+width+',height='+height+',toolbar=0,directories=0,menubar=0,status=0,location=0,scrollbars='+scroll+',resizable=0 left=100 top=50');
        return false;
    }

    function openAppEst(url,win,width,height,scroll,resizable,left,top,menubar,toolbar) {
        var ls_win = 'titanedi';
        if (win != null)
            ls_win = win;

        var li_width = 800;
        if (width != null)
            li_width = width;

        var li_height = 600;
        if (height != null)
            li_height = height;

        var li_scroll = 1;
        if (scroll != null)
            li_scroll = scroll;

        var li_resizable = 1;
        if (resizable != null)
            li_resizable = resizable;

        var li_left = 100;
        if (left != null)
            li_left = left;

        var li_top = 50;
        if (top != null)
            li_top = top;

        var li_menubar = 1;
        if (menubar != null)
            li_menubar = menubar;

        var li_toolbar = 0;
        if (toolbar != null)
            li_toolbar = toolbar;

        var rif = window.open(url,ls_win,'width='+li_width+',height='+li_height+',toolbar='+li_toolbar+',directories=0,menubar='+li_menubar+',status=1,location=0,scrollbars='+li_scroll+',resizable='+li_resizable+' left='+li_left+' top='+li_top);
        return rif;
    }

    //funzione che controlla i caratteri consentiti
    function caratteriCons(str,check){
        var ris = true;
        str = str.toLowerCase();
        check = check.toLowerCase();
        for (i=0;i<str.length;i++){
            if (check.indexOf(str.substr(i,1))<0){
              ris = false
              break;
            }
        }
        return ris;
    }

    //valida che l'indirizzo inserito sia un indirizzo email valido
    function isEmail(campo){
        var email = campo.value;
        var errore = true;
        var i;
        var checkstruser = 'abcdefghilmnopqrstuvzxwykj0123456789.-_';
        var checkstrhost = 'abcdefghilmnopqrstuvzxwykj0123456789.-';
        var host;
        var username;
        var temp;

        email = email.toLowerCase();
        if (email == '')
            errore = true;
        if (email.search('@') < 0)
            return false;

        email =	email.split('@')
        if (email.length > 2)
            return false;

        username = email[0];
        host = email[1];

        if (username.length < 2)
            return false;

        if (host.length < 6)
            return false;

        if (!caratteriCons(username,checkstruser))
            return false;

        if (!caratteriCons(host,checkstrhost))
            return false;

        temp = host.split('.');
        if ((temp[temp.length-1].length < 2)||(temp[temp.length-1].length > 4))
            return false;

        if ((host.substr(0,1) == '.')||(host.substr((host.length-1),1) == '.'))
            return false;

        errore = false;
        for (i = 0; i < temp.length; i++){
            if ((temp[i].substr(0,1) == '-')||(temp[i].substr((temp[i].length-1),1) == '-')){
                errore = true;
                break;
            }
        }
        if (errore)
            return false;
        return true;
    }

    //controlla la validita' dell'email
    function validaEmail(input, label){
        if(!isEmail(input)){
            alert("Il campo " + label + " non e' un indirizzo e-mail valido.");
            input.focus();
            input.select();
            return false;
        }
    }

     /**
     * Verifica se un testo e' una partita IVA ammissibile
     */
    function checkPIva(value) {
        if (value.length != 11)
            return 1; // errore su lunghezza
        if (isNaN(value))
            return 2; // errore su non numerico
        if (value.substring(0,7)=='0000000')
            return 3; // errore su troppi zeri
        //algoritmo di validazione
        somma = 0;
        checkDigit = value.substring(10,11);
        for (i = 0; i < 10 ;i++){
            car = value.substring(i,i+1);
            if (((i+1) % 2) == 0){
                //posizione pari
                car = car * 2;
                if (car>9) car = (car+'').substring(0,1)*1 + (car+'').substring(1,2)*1;
            }
            somma += car*1;
        }
        somma = somma + '';
        if (somma.length >1 )
            digit = somma.substring((somma.length) - 1,(somma.length))
        else
            digit =somma;
        if (digit != 0)
          digit = 10 - digit;
        if (checkDigit==digit)
            return 0
        else
            return 4
    }

    function isPartitaIva(input, label){
        switch(checkPIva(input.value)) {
            case (1): // lunghezza sbagliata (compreso lo 0)
            case (2): // non ci sono solo numeri
                alert("Il campo " + label + " non e' valido: la Partita Iva deve essere composta da 11 numeri");
                input.focus();
                input.select();
                return false;
                break;
            case (3): // troppi zeri nel codice
            case (4): // errore nella sequenza di numeri
                alert("Il campo " + label + " non e' valido: il numero di Partita Iva non e' valido");
                input.focus();
                input.select();
                return false;
                break;
        }
        return true;
    }

    function validaForm(form){
        alert('test');
        if(!form) form=this;
        return true;
    }

    function alertDate(obj, hdObj, askConfirm){
        var msg = "Proseguendo verra' impostata la data seguente:\n\nGiorno: %g%\nMese: %m%\nAnno: %a%\n\nContinuare?";
        if(obj.value==''){
            hdObj.value = '';
            return true;
        }
        var data = parseDate(obj);
        if(!data) return false;
        var ok = true;
        if(askConfirm){
            var g = data.getDate();
            var m = (data.getMonth()+1);
            var a = data.getFullYear();

            msg = msg.replace("%g%", g);
            msg = msg.replace("%m%", m);
            msg = msg.replace("%a%", a);
            msg = msg.replace(/\\n/g, "\n");
            ok = confirm(msg);
            if(!ok){
                obj.select();
            }
        }
        // setto il valore della data nell'Hidden
        var strDate = data.getFullYear() + '/' + (data.getMonth()+1) + '/' + data.getDate();
        hdObj.value = strDate;
        return ok;
    }

    //controlla l'inserimento corretto delle date (dd/MM/yyyy)
    function parseDate(obj) {
        var msg1 = "Data non riconosciuta: %data%";
        var msg2 = "Errore nel parsing della data:\ngiorni non validi per il mese.";
        var msg3 = "Errore nel parsing della data:\nmese non valido.";
        var msg4 = "Errore nel parsing della data:\nanno non bisestile.";
        if(!obj) return false;
        var value;
        if((value=obj.value) == '') return false;

        var sep = value.indexOf('/');
        if(sep == -1){
            alert(msg1.replace("%data%", obj.value));
            obj.select();
            return false;
        }
        var ok = true;
        var day = trim(value.substring(0, sep));
        value = value.substring(sep+1);
        sep = value.indexOf('/');
        var month = trim(value.substring(0, sep));
        value = value.substring(sep+1);
        var year = trim(value);
        if((day.length>2 || month.length>2 || (year.length>4 || year.length<2)) ||
                 (isNaN(year) || isNaN(month) || isNaN(day))){
            alert(msg1.replace("%data%", obj.value));
            obj.select();
            return false;
        }
        var now = new Date();
        if(year.length < 4){
            var indx = 4-year.length;
            year = now.getFullYear().toString().substr(0,indx) + year;
        }
        var data = day + '/' + month + '/' + year;
        var error = checkDate(day, month, year);
        if(error>0){
            switch (error){
                case(1):
                    // Giorni non corretti per il mese
                    alert(msg2.replace(/\\n/g, '\n'));
                    break;
                case(2):
                    // Mese non valido
                    alert(msg3.replace(/\\n/g, '\n'));
                    break;
                case(3):
                    // Anno non bisestile
                    alert(msg4.replace(/\\n/g, '\n'));
                    break;
            }
            obj.select();
            return false;
        }
        data = new Date(year, month-1, day);
        return data;
    }

    function checkDate(day, month, year){
        var error = 0;
        switch (month){
        // Febbraio
            case ("2") :case ("02") :
                var bisestile = ((year % 4 == 0) && (year % 400 != 0));
                if((bisestile && (day>29)) || (!bisestile && (day>28)))
                    if(day == 29)
                        error = 3; // l'anno considerato non e' bisestile
                    else
                        error = 1; // giorni non corretti per il mese
                break;
            // mesi da trentuno gg
            case ("1") : case ("01") :
            case ("3") : case ("03") :
            case ("5") : case ("05") :
            case ("7") : case ("07") :
            case ("8") : case ("08") :
            case ("10"): case ("12") :
                if(day>31)
                    error = 1; // giorni non corretti per il mese
                break;
            // mesi da trenta gg
            case ("4") : case ("04") :
            case ("6") : case ("06") :
            case ("9") : case ("09") :
            case ("11"):
                if(day>30)
                    error = 1; // giorni non corretti per il mese
                break;
            default : error = 2; // mese non corretto
        }
        return error;
    }

    //******************************************************************************
    //verifica se il contenuto del campo e' un numero intero positivo.
    //ritorna true se e' un numero o se e' null, altrimenti ritorna false
    function isInt(campo)
    {
        if (campo.value.length == 0)
            return true;

        var numero = trim(campo.value);

        ncar = numero.length;

        //vengono ammessi solo digit
        var cifre = '0123456789';
        var ok = true;
        for (var i = 0; i < ncar; i++)
        {
            if (cifre.indexOf(numero.charAt(i))<0)
            {
                //non e' un numero
                ok = false;
            }
        }
        if (ok)
        {
            campo.value = numero;
            return true;
        }
        else
            return false;
    }// fine di isInt()
    //******************************************************************************

    //******************************************************************************
    //verifica se il contenuto del campo e' un numero decimale positivo o negativo.
    //il formato e' del tipo [+,-]ii[,ddddd]
    //dopo il controllo restituisce sempre nel formato [-]ii,dd
    //ritorna true se e' un numero decimale o se e' null, altrimenti ritorna false
    function isNum(form, campo, nome_campo) {

        if (campo.value.length == 0)
        {
            return true;
        }

        var numero = trim(campo.value);
        var cifre1 = '+-0123456789,';
        var cifre2 = '0123456789,';
        //vengono tolti gli spazi in testa ed in coda

        ncar = numero.length;

        var num_virgole = 0;  // mi dice quante virgole ci sono

        // verifico che il 1^ carattere sia un digit oppure "-" oppure "+" oppure ","
        var ok_primo = true;
        if (cifre1.indexOf(numero.charAt(0))<0)
            ok_primo = false;

        if (numero.charAt(0) == ',')
            num_virgole += 1;


        //verifico che gli altri caratteri siano o digit o ","
        var ok = true;
        for (var i = 1; i < ncar; i++)
        {
            //window.alert('cifra '+i+'^ :'+numero2.charAt(i));
            if (cifre2.indexOf(numero.charAt(i)) < 0)
                ok = false;
            if (numero.charAt(i) == ',')
                num_virgole += 1;
        }

        if (ok && ok_primo && (num_virgole <= 1))
        {
            // se c'e' tolgo il leading "+"
            if (numero.charAt(0) == '+')
            {
                var numero3 = numero.substr(1);
            }
            else
            {
                var numero3 = numero;
            }

            pos_virgola = numero3.indexOf(',');
            var len = numero3.length;
            var numero_formattato;
            /*
            if (pos_virgola == -1) {
                // e' un numero intero; aggiungo ",00" alla fine
                numero_formattato=numero3.concat(",00");
            } else {
                if ((len - pos_virgola -1) > 2) {
                    // ci sono piu' di 2 decimali; tolgo quelli in eccesso
                    var ndec = len - pos_virgola -1;	// numero di posizioni decimali
                    var dec_da_togliere = ndec -2;		// numero di posizioni decimali da tagliare da destra
                    var numero4 = numero3.substr(0, (len - dec_da_togliere));	//ho tolto i decimali di troppo
                    numero3 = numero4;
                }
                if (pos_virgola == 0) {
                    //la virgola e' il 1^ carattere; aggiungo uno 0 in testa
                    var numero5 = "0".concat(numero3);
                    numero3 = numero5;
                }
                numero_formattato = numero3;
            }

            form.elements[campo].value = numero_formattato;
            */
            return true;
        }
        else
            return false;
    }// fine di isNum()
    //******************************************************************************

    function selTd(tdId){
        var tdh = document.getElementById(tdId);
        tdh.className="Sel";
    }
    function notSelTd(tdId){
        var tdh = document.getElementById(tdId);
        tdh.className="NotSel";
    }

    function cambiaImg(immagine, percNuovaImm){
            var immag = document.getElementById(immagine);
            if (immag){
                immag.src = percNuovaImm;
                }
            return;
        }


    function charOK(evt) {
        evt = (evt) ? evt : event;
        var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
            ((evt.which) ? evt.which : 0));
        //alert(charCode);
        if ((charCode < 31 || charCode > 122) && (charCode != 13)) {
            alert("Carattere NON valido!");
            return false;
        }
        return true;
        }

    function charOK_TEXTAREA(evt) {
        evt = (evt) ? evt : event;
        var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
            ((evt.which) ? evt.which : 0));
        //alert(charCode);
        if (charCode < 31 || charCode > 122) {
            alert("Carattere NON valido!");
            return false;
        }
        return true;
        }

    function charOKIntPos(myfield, e, dec) {
      var key;
      var keychar;

      if (window.event)
        key = window.event.keyCode;
      else if (e)
        key = e.which;
      else
        return true;
      keychar = String.fromCharCode(key);

      // control keys
      if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
        return true;

      // numbers
      else if ((("0123456789").indexOf(keychar) > -1))
        return true;

      // decimal point jump
      else if (dec && (keychar == ".")) {
        myfield.form.elements[dec].focus();
        return false;
      } else
        return false;
    }


     function checkLen(fld,maxlen,keypress){
        var maxcar;
        if (keypress)
            maxcar=maxlen;
        else
            maxcar=maxlen+1;
        if (fld.value.length >= maxcar){
            alert("Ci sono " + fld.value.length + " caratteri. Il massimo e' " + maxlen + " caratteri!");
            return false;
        }
        else{
            return true;
        }
     }

    //funzione utilizzata da addModCorpoOrdine.jsp e addModCorpoPreventivo.jsp
    //posiziona il cursore all'ultima riga inserita (anche ad ogni refresh)
    function scrollOggettoFinoInFondo(Id){
        var oggetto = document.getElementById(Id);
        if (oggetto){
            oggetto.scrollTop = oggetto.scrollHeight - oggetto.clientHeight;
        }
        return;
    }

    //seleziona tutte le checkbox presenti nella lista con un doppio click
    function selAllCheckbox(formName, fieldName, helpFieldName){
        var frm = document.forms(formName);
        var helpField = document.getElementById(helpFieldName);
        var selTutti = true;
        if (helpField){
            if (helpField.value == "s"){
                selTutti = false;
            } else {
                selTutti = true;
            }
            if(selTutti){
                helpField.value = "s";
            } else{
                helpField.value = "n";
            }
        }
        if (frm) {
            var flds = document.getElementsByName(fieldName);
            if (flds){
                for(var i=0;i<flds.length;i++){
                    if (!flds[i].disabled){
                        flds[i].checked = selTutti;
                    }
                }
            }
        }
        return;
    }

    //seleziona tutte le checkbox presenti nella lista con un doppio click
    function copiaValori(formName, fieldNameFrom, fieldNameTo){
    var frm = document.forms(formName);
    if (frm) {
        var fldsFrom = document.getElementsByName(fieldNameFrom);
        var fldsTo = document.getElementsByName(fieldNameTo);
        if (fldsFrom && fldsTo){
            for(var i=0;i<fldsFrom.length;i++){
                if (fldsFrom[i] && fldsTo[i]){
                    fldsTo[i].value = trim(fldsFrom[i].value);
                }
            }
        }
    }
    return;
    }



    //funzione che riabilita un oggetto
    function riabilita(objId){
        //alert(bottone);
        var oggetto = document.getElementById(objId)
        if (oggetto){
            oggetto.disabled=false;
        }
        return true;
    }

    //funzione che disabilita un oggetto per tot millisecondi
    function disabilTemp(objId, milliSecondi){
        var oggetto = document.getElementById(objId)
        if (oggetto){
            oggetto.disabled=true;
            setTimeout('riabilita("' + objId + '")',milliSecondi);
        }
        //alert (objId);
        return true;
    }

    function isInteroPositivo(input, label){
        var valido = isInt(input);
        if (!valido) {
            alert("Il campo " + label + " deve essere numerico");
        };
        return valido;
    }

    function hideObj(objName){
        var oObj = document.getElementById(objName);
        if(oObj){
        oObj.style.display='none';
        }
    }


    function disabilitaLink(){
        if(window.name == 'VendutoPopUp') {
            var aLink = document.body.all.tags("a");
            //alert(aLink.length);
            for(var i=0;i<aLink.length;i++){
                if(aLink[i]){
                    //alert(i + " - " + aLink[i].href + " - " + aLink[i].name);
                    aLink[i].href="#";
                }
            }
        }
        return false;
    }

    function trasfNumero(str){
        var espressione = /[-+]?[0-9]*\.?[0-9]+/;
        var ls = str;
        ls = trim(ls.replace(',', '.'));
        ls = espressione.exec(ls);
        return ls;
    }