
var char_nome = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ' \t\r\n\f"; var char_indirizzo = "°()/.,-\ 1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ' \t\r\n\f"; var char_email = "@.-_1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; var char_completo = "\|!£$%&/()=?^*+[]@°#;:§€<>-_.,1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ' \t\r\n\f" + '"'; var char_numeri = "0123456789"; var char_telefono = "()0123456789+-./\ #"; var char_codice_fiscale = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; var char_valuta = "0123456789.,"; var char_password = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; function controlla_combo(nomecampo, descrizionecampo) {
    if (nomecampo.selectedIndex == 0) {
        if (descrizionecampo == null)
            alert("Selezionare un valore per il campo \"" + nomecampo.name + "\".")
        else
            alert("Selezionare un valore per il campo \"" + descrizionecampo + "\".")
        nomecampo.focus(); return false;
    } else return true;
}
function controlla_campo(nomecampo, caratterivalidi, obbligatorio, descrizionecampo) {
    var checkOK = caratterivalidi; var checkStr = nomecampo.value; var allValid = true; if (nomecampo.value == "" && obbligatorio) {
        if (descrizionecampo == null)
            alert("Inserire un valore nel campo \"" + nomecampo.name + "\".")
        else
            alert("Inserire un valore nel campo \"" + descrizionecampo + "\".")
        nomecampo.focus(); return false
    }
    for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i); for (j = 0; j < checkOK.length; j++)
            if (ch == checkOK.charAt(j))
                break; if (j == checkOK.length) { allValid = false; break } 
    }
    if (!allValid) {
        if (descrizionecampo == null)
            alert("Caratteri non ammessi nel campo \"" + nomecampo.name + "\".")
        else
            alert("Caratteri non ammessi nel campo \"" + descrizionecampo + "\".")
        nomecampo.focus(); return false
    }
    return true
}
function controlla_campo_intervallo(nomecampo, valoreminimo, valoremassimo, obbligatorio, descrizionecampo) {
    var checkOK = "0123456789"; var checkStr = sostituiscistringa(nomecampo.value, ".", ""); var allValid = true; if (nomecampo.value == "" && obbligatorio) {
        if (descrizionecampo == null)
            alert("Inserire un valore nel campo \"" + nomecampo.name + "\".")
        else
            alert("Inserire un valore nel campo \"" + descrizionecampo + "\".")
        nomecampo.focus(); return false
    }
    for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i); for (j = 0; j < checkOK.length; j++)
            if (ch == checkOK.charAt(j))
                break; if (j == checkOK.length) { allValid = false; break } 
    }
    if (!allValid) {
        if (descrizionecampo == null)
            alert("Caratteri non ammessi nel campo \"" + nomecampo.name + "\".")
        else
            alert("Caratteri non ammessi nel campo \"" + descrizionecampo + "\".")
        nomecampo.focus(); return false
    }
    if ((checkStr < valoreminimo || checkStr > valoremassimo) && nomecampo.value != '') {
        if (descrizionecampo == null)
            alert("Il valore del campo \"" + nomecampo.name + "\" non è accettabile:\ninserire un valore compreso tra " + afFormatNumber("" + valoreminimo) + " e " + afFormatNumber("" + valoremassimo) + "."); else
            alert("Il valore del campo \"" + descrizionecampo + "\" non è accettabile:\ninserire un valore compreso tra " + afFormatNumber("" + valoreminimo) + " e " + afFormatNumber("" + valoremassimo) + "."); nomecampo.focus(); return false
    }
    return true
}
function controlla_campo_lunghezza(nomecampo, caratterivalidi, lunghezza, obbligatorio, descrizionecampo) {
    var checkOK = caratterivalidi; var checkStr = nomecampo.value; var allValid = true; if (nomecampo.value == "" && obbligatorio) {
        if (descrizionecampo == null)
            alert("Inserire un valore nel campo \"" + nomecampo.name + "\".")
        else
            alert("Inserire un valore nel campo \"" + descrizionecampo + "\".")
        nomecampo.focus(); return false
    }
    for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i); for (j = 0; j < checkOK.length; j++)
            if (ch == checkOK.charAt(j))
                break; if (j == checkOK.length) { allValid = false; break; } 
    }
    if (!allValid) {
        if (descrizionecampo == null)
            alert("Caratteri non ammessi nel campo \"" + nomecampo.name + "\".")
        else
            alert("Caratteri non ammessi nel campo \"" + descrizionecampo + "\".")
        nomecampo.focus(); return false
    }
    if (nomecampo.value.length != lunghezza && nomecampo.value != '') {
        if (descrizionecampo == null)
            alert("La lunghezza del campo \"" + nomecampo.name + "\" non è accettabile.")
        else
            alert("La lunghezza del campo \"" + descrizionecampo + "\" non è accettabile.")
        nomecampo.focus(); return false
    }
    return true
}
function controlla_campo_carattere(nomecampo, caratterivalidi, carattereobbligatorio, obbligatorio, descrizionecampo) {
    var checkOK = caratterivalidi; var checkStr = nomecampo.value; var allValid = true; var allValidO = false; if (nomecampo.value == "" && obbligatorio) {
        if (descrizionecampo == null)
            alert("Inserire un valore nel campo \"" + nomecampo.name + "\".")
        else
            alert("Inserire un valore nel campo \"" + descrizionecampo + "\".")
        nomecampo.focus(); return false
    }
    for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i); for (j = 0; j < checkOK.length; j++)
            if (ch == checkOK.charAt(j))
                break; if (j == checkOK.length) { allValid = false; break } 
    }
    if (!allValid) {
        if (descrizionecampo == null)
            alert("Caratteri non ammessi nel campo \"" + nomecampo.name + "\".")
        else
            alert("Caratteri non ammessi nel campo \"" + descrizionecampo + "\".")
        nomecampo.focus(); return false
    }
    for (i = 0; i < checkStr.length; i++) { if (checkStr.charAt(i) == carattereobbligatorio && nomecampo.value != "") { allValidO = true; break } }
    if (!allValidO && nomecampo.value != "") {
        if (descrizionecampo == null)
            alert("Il campo \"" + nomecampo.name + "\" non sembra essere valido.")
        else
            alert("Il campo \"" + descrizionecampo + "\" non sembra essere valido.")
        nomecampo.focus(); return false
    }
    return true
}
function sostituisci(nomecampo, valoredacercare, valoredasostituire) {
    var checkStr = new String(); checkStr = nomecampo.value; var nvolte; var i; var nuovastr = new String(); var ch = new String(); for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i); if (ch != valoredacercare)
            nuovastr = nuovastr + ch
        else
            nuovastr = nuovastr + valoredasostituire
    }
    return (nuovastr);
}
function sostituiscistringa(stringainput, valoredacercare, valoredasostituire) {
    checkStr = stringainput; var nvolte; var i; var nuovastr = new String(); var ch = new String(); for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i); if (ch != valoredacercare)
            nuovastr = nuovastr + ch
        else
            nuovastr = nuovastr + valoredasostituire
    }
    return (nuovastr);
}
function controlla_valuta(nomecampo, moneta, min, max, obbligatorio, nonzero, descrizionecampo) {
    var checkOK = char_valuta; var checkStr = nomecampo.value; var allValid = true; var decPoints = 0; var allNum = ""; var valore; if (nomecampo.value == "" && obbligatorio) {
        if (descrizionecampo == null)
            alert("Inserire un valore nel campo \"" + nomecampo.name + "\".")
        else
            alert("Inserire un valore nel campo \"" + descrizionecampo + "\".")
        nomecampo.focus(); return false
    }
    if (nomecampo.value == 0 && nonzero) {
        if (descrizionecampo == null)
            alert("Il valore contenuto nel campo \"" + nomecampo.name + "\" deve essere maggiore di zero.")
        else
            alert("Il valore contenuto nel campo \"" + descrizionecampo + "\" deve essere maggiore di zero.")
        nomecampo.focus(); return false
    }
    for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i); for (j = 0; j < checkOK.length; j++) { if (checkStr.charCodeAt(i) - checkOK.charCodeAt(j) == 0) { break; } }
        if (j == checkOK.length) { if ((ch != ".") || (((checkStr.length - i) % 4) == 0)) { allValid = false; break; } }
        allNum += ch;
    }
    if (!allValid) {
        if (descrizionecampo == null)
            alert("Il formato della cifra nel campo \"" + nomecampo.name + "\" non è corretto.")
        else
            alert("Il formato della cifra nel campo \"" + descrizionecampo + "\" non è corretto.")
        nomecampo.focus(); return false
    }
    stringaPulita = togli_punti_e_virgole(nomecampo.value); if ((stringaPulita.length) > 0)
        valore = stringaPulita; else
        valore = 0
    if ((moneta == 2) || (moneta == "euro") || (moneta == "EURO") || (moneta == "'Euro")) { valore = valore * 1936.27 }
    if (!(valore >= min) || !(valore <= max)) {
        if (descrizionecampo == null)
            alert("Il valore specificato nel campo \"" + nomecampo.name + "\" non è ammissibile.")
        else
            alert("Il valore specificato nel campo \"" + descrizionecampo + "\" non è ammissibile.")
        nomecampo.focus(); return false
    }
    return true
}
function controlla_radio(nomecampo, descrizionecampo) {
    var radioSelected = false; for (i = 0; i < nomecampo.length; i++) {
        if (nomecampo[i].checked)
            radioSelected = true
    }
    if (!radioSelected) {
        if (descrizionecampo == null)
            alert("Effettuare una selezione per il campo \"" + nomecampo.name + "\"."); else
            alert("Effettuare una selezione per il campo \"" + descrizionecampo + "\"."); nomecampo[0].focus(); return false
    }
    return true
}
function controlla_separatori_migliaia() {
    var flag = 0; for (var i = 0; i < arguments.length; i += 2) {
        var result = arguments[i].match(/[^.\d]|[.]+\d{4,}[.]+|[.]\d{0,2}[.]|[.]\d{0,2}$|[.]\d{4,}$/gi); if (result != null)
        { alert("PrestitiOnline\n\n Controllare che in " + arguments[i + 1] + ", i valori immessi siano dei numeri consistenti:\n\n" + "- non sono ammessi numeri decimali\n" + "- usare il SOLO punto (facoltativo) per raggruppare le migliaia\n" + "- non inserire caratteri che non siano delle cifre.\n\n"); return false; } 
    }
    return true;
}
function controlla_percentuali() {
    var flag = 0; for (var i = 0; i < arguments.length; i += 2) {
        var result = arguments[i].match(/\d*[.,]{2,}\d*|[%]|\d*[a-zA-z]|\d*[.,]\d{1,}[.,%]\d*/g); if (result != null)
        { alert("Attenzione\n\nControllare che per i tassi di interesse, i valori immessi siano dei numeri consistenti:\n\n" + "- non inserire il simbolo \"%\"\n" + "- usare la virgola o il punto per dividere i decimali\n" + "- non inserire caratteri che non siano delle cifre.\n\n"); return false; } 
    }
    return true;
}
function campo_vuoto(campo) {
    if (campo.value == "" || campo.value == null)
        return true; else
        return false;
}
function togli_punti_e_virgole(str) { str = sostituiscistringa(str, ".", ""); str = sostituiscistringa(str, ",", ""); return str; }
function controlla_email(nomecampo) {
    var emailStr = nomecampo.value; var checkTLD = 1; var knownDomsPat = /^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/; var emailPat = /^(.+)@(.+)$/; var specialChars = "\\(\\)><@',;:\\\\\\\"\\.\\[\\]"; var validChars = "\[^\\s" + specialChars + "\]"; var quotedUser = "(\"[^\"]*\")"; var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/; var atom = validChars + '+'; var word = "(" + atom + "|" + quotedUser + ")"; var userPat = new RegExp("^" + word + "(\\." + word + ")*$"); var domainPat = new RegExp("^" + atom + "(\\." + atom + ")*$"); var matchArray = emailStr.match(emailPat); if (matchArray == null) { nomecampo.focus(); alert("E' necessario inserire un indirizzo e-mail corretto (controllare @ e i punti) per poter procedere."); return false; }
    var user = matchArray[1]; var domain = matchArray[2]; for (i = 0; i < user.length; i++) { if (user.charCodeAt(i) > 127) { nomecampo.focus(); alert("Il nome utente dell'email contiene dei caratteri scorretti."); return false; } }
    for (i = 0; i < domain.length; i++) { if (domain.charCodeAt(i) > 127) { nomecampo.focus(); alert("Il dominio dell'email contiene dei caratteri scorretti."); return false; } }
    if (user.match(userPat) == null) { nomecampo.focus(); alert("Il nome utente dell'email contiene dei caratteri scorretti."); return false; }
    var IPArray = domain.match(ipDomainPat); if (IPArray != null) {
        for (var i = 1; i <= 4; i++) { if (IPArray[i] > 255) { nomecampo.focus(); alert("L'indirizzo IP della email non è corretto."); return false; } }
        return true;
    }
    var atomPat = new RegExp("^" + atom + "$"); var domArr = domain.split("."); var len = domArr.length; for (i = 0; i < len; i++) { if (domArr[i].search(atomPat) == -1) { nomecampo.focus(); alert("Il dominio dell'email non è valido."); return false; } }
    if (checkTLD && domArr[domArr.length - 1].length != 2 && domArr[domArr.length - 1].toLowerCase().search(knownDomsPat) == -1) { nomecampo.focus(); alert("L'indirizzo email deve terminare con un dominio conosciuto o un codice internazionale di 2 lettere."); return false; }
    if (len < 2) { nomecampo.focus(); alert("Nell'email manca la parte tra la @ e il punto."); return false; }
    var domains = "libero.it,tiscali.it,virgilio.it,yahoo.it,alice.it,hotmail.com,hotmail.it,inwind.it,fastwebnet.it,gmail.com,tiscalinet.it,email.it,tele2.it,interfree.it,katamail.com,aliceposta.it,yahoo.com,poste.it,msn.com,live.it,infinito.it,jumpy.it,supereva.it"; domainArray = domains.split(","); if (domain != domainArray[0] && domain != domainArray[1] && domain != domainArray[2] && domain != domainArray[3] && domain != domainArray[4] && domain != domainArray[5] && domain != domainArray[6] && domain != domainArray[7] && domain != domainArray[8] && domain != domainArray[9] && domain != domainArray[10] && domain != domainArray[11] && domain != domainArray[12] && domain != domainArray[13] && domain != domainArray[14] && domain != domainArray[15] && domain != domainArray[16] && domain != domainArray[17] && domain != domainArray[18] && domain != domainArray[19] && domain != domainArray[20] && domain != domainArray[21] && domain != domainArray[23]) {
        suggestThis = suggest_another(domain, domainArray); if (suggestThis != "")
            if (confirm("Hai inserito l' indirizzo email: " + emailStr + " \nDesideri che venga modificato in: " + user + "@" + suggestThis + " ?"))
            { nomecampo.value = user + "@" + suggestThis; } 
    }
    return true;
}
function suggest_another(wrong_domain, array) {
    for (j = 0; j < array.length; j++) {
        if (differences(wrong_domain, array[j]) <= 1)
            return array[j];
    }
    return "";
}
function differences(wrongDomain, item) {
    noOfDiferences = 0; charLengthDif = wrongDomain.length - item.length; switch (charLengthDif) {
        case 0: for (i = 0; i < wrongDomain.length; i++) {
                if (noOfDiferences < 2) {
                    if (wrongDomain.substr(i, 1) != item.substr(i, 1))
                        noOfDiferences++;
                }
                else
                    return noOfDiferences;
            }
            return noOfDiferences; case 1: for (i = 0; i < item.length; i++) {
                if (noOfDiferences < 2)
                { if (item.substr(i, 1) != wrongDomain.substr(i + noOfDiferences, 1)) { noOfDiferences++; i--; } }
                else
                    return noOfDiferences;
            }
            return noOfDiferences; case -1: for (i = 0; i < wrongDomain.length; i++) {
                if (noOfDiferences < 2)
                { if (wrongDomain.substr(i, 1) != item.substr(i + noOfDiferences, 1)) { noOfDiferences++; i--; } }
                else
                    return noOfDiferences;
            }
            return noOfDiferences; default: return 9;
    } 
}
function check_fiscale(campo, obbligatorio, messaggio) {
    caratteri = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z")
    pari = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25)
    dispari = new Array(1, 0, 5, 7, 9, 13, 15, 17, 19, 21, 1, 0, 5, 7, 9, 13, 15, 17, 19, 21, 2, 4, 18, 20, 11, 3, 6, 8, 12, 14, 16, 10, 22, 25, 24, 23)
    cod = campo.value.toLowerCase(); check = true; if (cod.length != 16) { check = false } else {
        lettere = cod.substr(0, 6) + cod.substr(8, 1) + cod.substr(11, 1) + cod.substr(15); numeri = cod.substr(6, 2) + cod.substr(9, 2) + cod.substr(12, 3); for (i = 0; i < 10; i++) { if (lettere.charCodeAt(i) < 97 || lettere.charCodeAt(i) > 122) { check = false; } }
        for (i = 0; i < 8; i++) { if (numeri.charCodeAt(i) < 48 || numeri.charCodeAt(i) > 57) { check = false; } } 
    }
    test = cod.substr(15, 1); var somma = 0
    for (i = 0; i < 16; i = i + 2) {
        carattere = cod.substr(i, 1)
        for (k = 0; k < 36; k++) {
            if (carattere == caratteri[k]) {
                somma = somma + dispari[k]
                break
            } 
        } 
    }
    for (i = 1; i < 15; i = i + 2) {
        carattere = cod.substr(i, 1)
        for (k = 0; k < 36; k++) {
            if (carattere == caratteri[k]) {
                somma = somma + pari[k]
                break
            } 
        } 
    }
    resto = somma % 26; var lettera = String.fromCharCode(97 + resto); if (test != lettera) { check = false; }
    if (check == false) { if (obbligatorio) { alert("Il codice fiscale contenuto nel campo \"" + messaggio + "\" è errato"); campo.focus(); return (false); } else { return (confirm("Il codice fiscale contenuto nel campo \"" + messaggio + "\" è errato; Proseguo?")); } }
    return true;
}
function check_importo_reddito(field, moneta, min, max, obbligatorio, nonzero, messaggio) {
    var checkOK = "0123456789.,"; var checkStr = field.value; var allValid = true; var decPoints = 0; var allNum = ""; var valore; if (field.value == "" && obbligatorio) {
        if (messaggio == null)
            alert("Dati obbligatori nell'importo reddito"); else
            alert(messaggio); field.focus(); return (false);
    }
    if (field.value == 0 && nonzero) {
        if (messaggio == null)
            alert("Il valore contenuto nell'importo reddito deve essere maggiore di zero"); else
            alert(messaggio); field.focus(); return (false);
    }
    for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i); for (j = 0; j < checkOK.length; j++)
            if (ch == checkOK.charAt(j))
                break; if (j == checkOK.length) {
            if ((ch != ".") || (((checkStr.length - i) % 4) != 0))
            { allValid = false; break; } 
        }
        allNum += ch;
    }
    if (!allValid) {
        if (messaggio == null)
            alert("Il formato della cifra nel campo \"Importo reddito\" non è corretto"); else
            alert(messaggio); field.focus(); return (false);
    }
    if ((ReplaceEta(field, ".", "").length) > 0)
        valore = parseFloat(ReplaceEta(field, ".", "")); else
        valore = 0; if (moneta == 2)
        valore = valore * (1936.27 / 1000000); if (!(valore >= min) || !(valore <= max)) {
        if (messaggio == null) {
            if (moneta == 2)
            { alert("Il reddito specificato non può essere convalidato. Controllare l'importo."); }
            else
            { alert("Il reddito specificato non può essere convalidato. Controllare l'importo. Nell'inserire l'importo, fare attenzione all'unità di valuta. Ad esempio, se la richiesta è espressa in \"Milioni di lire\", per inserire un importo di 2.300.000 lire, bisognerà digitare \"2,3\"."); } 
        }
        else
        { alert(messaggio); }
        field.focus(); return (false);
    }
    return (true);
}
function check_importo_residuo(field, moneta, min, max, obbligatorio, nonzero, messaggio) {
    var checkOK = "0123456789.,"; var checkStr = field.value; var allValid = true; var decPoints = 0; var allNum = ""; var valore; if (field.value == "" && obbligatorio) {
        if (messaggio == null)
            alert("Dati obbligatori nell'importo residuo"); else
            alert(messaggio); field.focus(); return (false);
    }
    if (field.value == 0 && nonzero) {
        if (messaggio == null)
            alert("Il valore contenuto nell'importo residuo deve essere maggiore di zero"); else
            alert(messaggio); field.focus(); return (false);
    }
    for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i); for (j = 0; j < checkOK.length; j++)
            if (ch == checkOK.charAt(j))
                break; if (j == checkOK.length) {
            if ((ch != ".") || (((checkStr.length - i) % 4) != 0))
            { allValid = false; break; } 
        }
        allNum += ch;
    }
    if (!allValid) {
        if (messaggio == null)
            alert("Il formato della cifra nel campo \"Importo residuo\" non è corretto"); else
            alert(messaggio); field.focus(); return (false);
    }
    if ((ReplaceEta(field, ".", "").length) > 0)
        valore = parseFloat(ReplaceEta(field, ".", "")); else
        valore = 0; if (moneta == 2)
        valore = valore * (1936.27 / 1000000); if (!(valore >= min) || !(valore <= max)) {
        if (messaggio == null) {
            if (moneta == 2)
            { alert("Il valore residuo del prestito non può essere convalidato. Controllare l'importo."); }
            else
            { alert("Il valore residuo del prestito non può essere convalidato. Controllare l'importo. Nell'inserire l'importo, fare attenzione all'unità di valuta. Ad esempio, se la richiesta è espressa in \"Milioni di lire\", per inserire un importo di 50.000.000 lire, bisognerà digitare \"50\"."); } 
        }
        else
        { alert(messaggio); }
        field.focus(); return (false);
    }
    return (true);
}
function check_rata(field, moneta, min, max, obbligatorio, nonzero, messaggio) {
    var checkOK = "0123456789.,"; var checkStr = field.value; var allValid = true; var decPoints = 0; var allNum = ""; var valore; if (field.value == "" && obbligatorio) {
        if (messaggio == null)
            alert("Dati obbligatori nella rata"); else
            alert(messaggio); field.focus(); return (false);
    }
    if (field.value == 0 && nonzero) {
        if (messaggio == null)
            alert("Il valore contenuto nella rata deve essere maggiore di zero"); else
            alert(messaggio); field.focus(); return (false);
    }
    for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i); for (j = 0; j < checkOK.length; j++)
            if (ch == checkOK.charAt(j))
                break; if (j == checkOK.length) {
            if ((ch != ".") || (((checkStr.length - i) % 4) != 0))
            { allValid = false; break; } 
        }
        allNum += ch;
    }
    if (!allValid) {
        if (messaggio == null)
            alert("Il formato della cifra nel campo \"Rata\" non è corretto"); else
            alert(messaggio); field.focus(); return (false);
    }
    if ((ReplaceEta(field, ".", "").length) > 0)
        valore = parseFloat(ReplaceEta(field, ".", "")); else
        valore = 0; if (moneta == 2)
        valore = valore * (1936.27 / 1000000); if (!(valore >= min) || !(valore <= max)) {
        if (messaggio == null) {
            if (moneta == 2)
            { alert("Il valore della rata non può essere convalidato. Controllare l'importo."); }
            else
            { alert("Il valore della rata non può essere convalidato. Controllare l'importo. Nell'inserire l'importo, fare attenzione all'unità di valuta. Ad esempio, se la richiesta è espressa in \"Milioni di lire\", per inserire un importo di 2.300.000 lire, bisognerà digitare \"2,3\"."); } 
        }
        else
        { alert(messaggio); }
        field.focus(); return (false);
    }
    return (true);
}
function check_curr1(field, moneta, min, max, obbligatorio, nonzero, messaggio) {
    var checkOK = "0123456789.,"; var checkStr = field.value; var allValid = true; var valore; if (field.value == "" && obbligatorio) {
        if (messaggio == null)
            alert("Dati obbligatori nel campo \"" + field.name + "\" "); else
            alert(messaggio); field.focus(); return (false);
    }
    var numvirgole = 0; var numpunti = 0; for (i = 0; i < checkStr.length; i++) {
        if (checkStr.charAt(i) == ',') { ++numvirgole; }
        if (checkStr.charAt(i) == '.') { ++numpunti; } 
    }
    if (numvirgole > 0 && numpunti > 0) { alert("Non sono ammessi virgole e punti contemporaneamente nel campo \"" + field.name + "\"."); field.focus(); return (false); }
    if (moneta == 1 && (numvirgole > 1 || numpunti > 1)) { alert("Ammesso un solo punto o una sola virgola nel campo \"" + field.name + "\"."); field.focus(); return (false); }
    for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i); for (j = 0; j < checkOK.length; j++)
            if (ch == checkOK.charAt(j)) { break; }
        if (j == checkOK.length) { allValid = false; break; }
        if (((ch == ".") || (ch == ',')) && (moneta == 2 || moneta == 3) && ((checkStr.length - i) % 4) != 0) { allValid = false; break; } 
    }
    if (!allValid) {
        if (messaggio == null)
            alert("Il formato della cifra nel campo \"" + field.name + "\" non è corretto"); else
            alert(messaggio); field.focus(); return (false);
    }
    if (field.value == 0 && nonzero) {
        if (messaggio == null)
            alert("Il valore contenuto nel campo \"" + field.name + "\" deve essere maggiore di zero"); else
            alert(messaggio); field.focus(); return (false);
    }
    if ((ReplaceEta(field, ".", "").length) > 0) {
        if (moneta == 1) { valore = parseFloat(ReplaceEta(field, ",", ".")); } else
        { valore = parseFloat(sostituiscistringa(sostituiscistringa(field.value, ".", ""), ",", "")); } 
    } else
        valore = 0; if (moneta == 2)
        valore = valore * (1936.27 / 1000000); if (moneta == 3) { min = min * 1000000; max = max * 1000000; }
    if (!(valore >= min) || !(valore <= max)) {
        if (messaggio == null)
            alert("Il valore del campo \"" + field.name + "\"non è accettabile."); else
            alert(messaggio); field.focus(); return (false);
    }
    return (true);
}
function check_radio(field, messaggio) {
    var radioSelected = false; for (i = 0; i < field.length; i++) {
        if (field[i].checked)
            radioSelected = true;
    }
    if (!radioSelected)
    { alert("Effettuare una selezione " + messaggio + " "); field[0].focus(); return (false); }
    return (true);
}
function check_cap(field, obbligatorio, messaggio) {
    var checkOK = "0123456789"; var checkStr = field.value; var allValid = true; var decPoints = 0; var allNum = ""; if (field.value == "" && obbligatorio) {
        if (messaggio == null)
            alert("Dati obbligatori nel campo \"" + field.name + "\" "); else
            alert(messaggio); field.focus(); return (false);
    }
    if ((checkStr.length != 5) && obbligatorio) {
        if (messaggio == null)
            alert("Il formato del CAP è errato nel campo \"" + field.name + "\" "); else
            alert(messaggio); field.focus(); return (false);
    }
    for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i); for (j = 0; j < checkOK.length; j++)
            if (ch == checkOK.charAt(j))
                break; if (j == checkOK.length)
        { allValid = false; break; }
        allNum += ch;
    }
    if (!allValid) {
        if (messaggio == null)
            alert("Il formato del numero intero nel campo \"" + field.name + "\" non è corretto"); else
            alert(messaggio); field.focus(); return (false);
    }
    return (true);
}
function check_name(field, obbligatorio, messaggio) {
    var checkOK = ",.'- ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ \t\r\n\f"; var checkStr = field.value; var allValid = true; if (field.value == "" && obbligatorio) {
        if (messaggio == null)
            alert("Dati obbligatori nel campo \"" + field.name + "\" "); else
            alert(messaggio); field.focus(); return (false);
    }
    for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i); for (j = 0; j < checkOK.length; j++)
            if (ch == checkOK.charAt(j))
                break; if (j == checkOK.length)
        { allValid = false; break; } 
    }
    if (!allValid) {
        if (messaggio == null)
            alert("Ammesse solo lettere e spazi nel campo \"" + field.name + "\" "); else
            alert(messaggio); field.focus(); return (false);
    }
    return (true);
}
function check_indirizzo(field, obbligatorio, messaggio) {
    var checkOK = char_indirizzo; var checkStr = field.value; var allValid = true; if (field.value == "" && obbligatorio) {
        if (messaggio == null)
            alert("Dati obbligatori nel campo \"" + field.name + "\" "); else
            alert(messaggio); field.focus(); return (false);
    }
    for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i); for (j = 0; j < checkOK.length; j++)
            if (ch == checkOK.charAt(j))
                break; if (j == checkOK.length)
        { allValid = false; break; } 
    }
    if (!allValid) {
        if (messaggio == null)
            alert("Ammesse solo lettere  e spazi nel campo \"" + field.name + "\" "); else
            alert(messaggio); field.focus(); return (false);
    }
    return (true);
}
function check_date(giorno, mese, anno, field, messaggio) {
    var data = new Date(anno, mese, giorno, 0, 0, 0, 0); if (messaggio == null)
        messaggio = "La data non è corretta"; if (data.getDate() != giorno)
    { alert(messaggio); field.focus(); return (false); }
    else
        return (true);
}
function check_string(field, lettere, obbligatorio, messaggio) {
    var checkOK = lettere; var checkStr = field.value; var allValid = true; if (field.value == "" && obbligatorio) {
        if (messaggio == null)
            alert("Dati obbligatori nel campo \"" + field.name + "\" "); else
            alert(messaggio); field.focus(); return (false);
    }
    for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i); for (j = 0; j < checkOK.length; j++)
            if (ch == checkOK.charAt(j))
                break; if (j == checkOK.length)
        { allValid = false; break; } 
    }
    if (!allValid) {
        if (messaggio == null)
            alert("Caratteri non ammessi nel campo \"" + field.name + "\" "); else
            alert(messaggio); field.focus(); return (false);
    }
    return (true);
}
function check_integer(field, obbligatorio, nonzero, messaggio) {
    var checkOK = "0123456789"; var checkStr = field.value; var allValid = true; var decPoints = 0; var allNum = ""; if (field.value == "" && obbligatorio) {
        if (messaggio == null)
            alert("Dati obbligatori nel campo \"" + field.name + "\" "); else
            alert(messaggio); field.focus(); return (false);
    }
    if (field.value == 0 && nonzero) {
        if (messaggio == null)
            alert("Il valore contenuto nel campo \"" + field.name + "\" deve essere maggiore di zero"); else
            alert(messaggio); field.focus(); return (false);
    }
    for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i); for (j = 0; j < checkOK.length; j++)
            if (ch == checkOK.charAt(j))
                break; if (j == checkOK.length)
        { allValid = false; break; }
        allNum += ch;
    }
    if (!allValid) {
        if (messaggio == null)
            alert("Il formato della cifra nel campo \"" + field.name + "\" non è corretto"); else
            alert(messaggio); field.focus(); return (false);
    }
    return (true);
}
function ReplaceEta(field, ValDaCercare, ValDaSost) {
    var checkStr = field.value; var nvolte; var i; var nuovastr = new String(); var ch = new String(); for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i); if (ch != ValDaCercare)
            nuovastr = nuovastr + ch
        else
            nuovastr = nuovastr + ValDaSost
    }
    return (nuovastr);
}
function SelezionaCombo(obj, v) { for (i = 0; i < obj.options.length; i++) { if (obj.options[i].value == v) { obj.selectedIndex = i; break; } } }
function CheckCurrEuro(field, min, max, obbligatorio, nonzero, messaggio) {
    var checkOK = "0123456789.,"; var checkStr = field.value; var allValid = true; var valore; if (field.value == "" && obbligatorio) {
        if (messaggio == null)
            alert("Dati obbligatori nel campo \"" + field.name + "\"."); else
            alert(messaggio); field.focus(); return (false);
    }
    var numvirgole = 0; var numpunti = 0; for (i = 0; i < checkStr.length; i++) {
        if (checkStr.charAt(i) == ',') { ++numvirgole; }
        if (checkStr.charAt(i) == '.') { ++numpunti; } 
    }
    if (numvirgole > 0 && numpunti > 0) { alert("Non sono ammessi virgole e punti contemporaneamente nel campo \"" + field.name + "\"."); field.focus(); return (false); }
    for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i); for (j = 0; j < checkOK.length; j++)
            if (ch == checkOK.charAt(j)) { break; }
        if (j == checkOK.length) { allValid = false; break; }
        if (((ch == ".") || (ch == ',')) && ((checkStr.length - i) % 4) != 0) { allValid = false; break; } 
    }
    if (!allValid) {
        if (messaggio == null)
            alert("Il formato della cifra nel campo \"" + field.name + "\" non è corretto."); else
            alert(messaggio); field.focus(); return (false);
    }
    if (field.value == 0 && nonzero) {
        if (messaggio == null)
            alert("Il valore contenuto nel campo \"" + field.name + "\" deve essere maggiore di zero."); else
            alert(messaggio); field.focus(); return (false);
    }
    if ((ReplaceEta(field, ".", "").length) > 0) { valore = parseFloat(sostituiscistringa(sostituiscistringa(field.value, ".", ""), ",", "")); } else { valore = 0; }
    if (!(valore >= min) || !(valore <= max)) {
        if (messaggio == null)
            alert("Il valore del campo \"" + field.name + "\"non è accettabile."); else
            alert(messaggio); field.focus(); return (false);
    }
    return (true);
}
function controllo_importo_intero(importo, strNomeCampo) {
    var espressione = new RegExp("(^($)|^([1-9]([0-9]*)$))|(^([1-9](|[0-9]|[0-9]{2})([.]([0-9]){3})+)$)"); if (!espressione.test(sostituiscistringa(importo.value, ".", ""))) { alert("Valore non ammesso nel campo " + strNomeCampo + "\nInserire un numero intero."); importo.focus(); return false; }
    return true;
}
function controllo_importo_intero_con_messaggio(importo, strNomeCampo, messaggio) {
    var espressione = new RegExp("(^($)|^([1-9]([0-9]*)$))|(^([1-9](|[0-9]|[0-9]{2})([.]([0-9]){3})+)$)"); if (!espressione.test(sostituiscistringa(importo.value, ".", ""))) {
        if (messaggio != "") { alert(messaggio); } else { alert("Valore non ammesso nel campo " + strNomeCampo + "\nInserire un numero intero."); }
        importo.focus(); return false;
    }
    return true;
}
function non_soloInt(campo, descrizioneCampo, messaggio) {
    if (parseInt(campo.value) == campo.value) {
        if (messaggio != "") { alert(messaggio); } else { alert("Il campo \"" + descrizioneCampo + "\" non può essere formato solo da numeri."); }
        campo.focus(); return true;
    }
    return false;
}
function controlla_campo_intervallo_con_messaggio(nomecampo, valoreminimo, valoremassimo, obbligatorio, descrizionecampo, messaggio) {
    var checkOK = "0123456789"; var checkStr = nomecampo.value.replace(".", ""); var allValid = true; if (nomecampo.value == "" && obbligatorio) {
        if (messaggio != "") { alert(messaggio); } else {
            if (descrizionecampo == null)
                alert("Inserire un valore nel campo \"" + nomecampo.name + "\".")
            else
                alert("Inserire un valore nel campo \"" + descrizionecampo + "\".")
        }
        nomecampo.focus(); return false
    }
    for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i); for (j = 0; j < checkOK.length; j++)
            if (ch == checkOK.charAt(j))
                break; if (j == checkOK.length) { allValid = false; break } 
    }
    if (!allValid) {
        if (messaggio != "") { alert(messaggio); } else {
            if (descrizionecampo == null)
                alert("Caratteri non ammessi nel campo \"" + nomecampo.name + "\".")
            else
                alert("Caratteri non ammessi nel campo \"" + descrizionecampo + "\".")
        }
        nomecampo.focus(); return false
    }
    if ((checkStr < valoreminimo || checkStr > valoremassimo) && nomecampo.value != '') {
        if (messaggio != "") { alert(messaggio); } else {
            if (descrizionecampo == null)
                alert("Il valore del campo \"" + nomecampo.name + "\" non è accettabile:\ninserire un valore compreso tra " + afFormatNumber("" + valoreminimo) + " e " + "" + afFormatNumber(valoremassimo) + " ."); else
                alert("Il valore del campo \"" + descrizionecampo + "\" non è accettabile:\ninserire un valore compreso tra " + afFormatNumber("" + valoreminimo) + " e " + "" + afFormatNumber(valoremassimo) + " .");
        }
        nomecampo.focus(); return false
    }
    return true
}


// ********** Funzioni per SmartPhone *************

function detectAndroid() {
    if (navigator.userAgent.toLowerCase().search("android") > -1)
        return true;
    return false;
}

function detectIPhone() {
    if (navigator.userAgent.toLowerCase().search("iphone") > -1)
        return true;
    return false;
}

function detectIPod() {
    if (navigator.userAgent.toLowerCase().search("ipod") > -1)
        return true;
    return false;
}

function detectSeries60() {
    if (navigator.userAgent.toLowerCase().search("series60") > -1)
        return true;
    return false;
}

function detectSymbian() {
    if (navigator.userAgent.toLowerCase().search("symbian") > -1)
        return true;
    return false;
}

function detectWebkit() {
    if (navigator.userAgent.toLowerCase().search("webkit") > -1)
        return true;
    return false;
}

function detectWindowMobile() {
    if (navigator.userAgent.toLowerCase().search("windows phone") > -1)
        return true;
    return false;
}

function detectBlackBerry() {
    if (navigator.userAgent.toLowerCase().search("blackberry") > -1)
        return true;
    return false;
}

function detectPalm() {
    if (navigator.userAgent.toLowerCase().search("palm") > -1)
        return true;
    return false;
}

// ********** Termine Funzioni per SmartPhone *************


// ********** Funzioni per la punteggiatura dinamica *************

function sistemaPunteggiatura(field, event) {

    // Per alcuni casi non sistemo la punteggiatura: Windows Mobile - Android 
    if (!(detectAndroid() || detectWindowMobile())) {

        // Memorizzo posizione cursore
        GetPosition(field);

        // Se uso le frecce direzionali la text non viene modificata
        if (event.keyCode != "37" && event.keyCode != "38" && event.keyCode != "39" && event.keyCode != "40") {
            var v1 = field.value;
            var v2;
            v2 = afFormatNumber(field.value);
            if (v1 != v2) {
                field.value = v2;
            }

            // Calcolo prossima posizione
            switch (event.keyCode) {
                case 46: // Seleziono Canc 
                    if (v2.length * 1 + 1 == v1.length * 1)
                        posizioneCursore = posizioneCursore - 1;
                    break;
                case 8: // Seleziono la freccia che cancella
                    if (v2.length * 1 + 1 == v1.length * 1)
                        posizioneCursore = posizioneCursore - 1;
                    break;
                default:
                    if (v2.length > v1.length)
                        posizioneCursore = posizioneCursore + 1;
                    break;
            }

            // Se inserisco un carattere non numero resetto la posizione
            if (!((event.keyCode >= "48" && event.keyCode <= "57") || (event.keyCode >= "96" && event.keyCode <= "105") || event.keyCode == "8" || event.keyCode == "46" || field.value.length == 10)) {
                posizioneCursore = posizioneCursore - 1;
            }

            // Metto il cursore nella sua posizione 
            if (document.selection) {
                // IE
                var FieldRange = field.createTextRange();
                FieldRange.move("character", posizioneCursore);
                FieldRange.select();
            } else {
                // No IE
                field.selectionEnd = posizioneCursore;
                field.selectionStart = posizioneCursore;
            }
        }

        return v1 != v2;
    }
}

function cancellaElementiInizialiErrati(field) {
    //Per un numero intero non è possibile iniziare con zero o punto
    while (field.value.substr(0, 1) == "0" || field.value.substr(0, 1) == ".") {
        if (field.value.length > 1) {
            field.value = field.value.substr(1, field.value.length - 1)
        } else {
            field.value = "";
        }
    }
}


function afFormatNumber(str) {
    var separator = '.';
    var i, char, out = '', decimalPosition = -1, isNegative = false, isValid = true;

    for (i = 0; i < str.length; i++) {
        char = str.substr(i, 1);
        if ((char >= '0') && (char <= '9')) {
            out += char;
        }
        if ((char == '-') && (i == 0)) {
            out += char;
            isNegative = true;
        }
    }

    if (separator) {
        if (decimalPosition == -1) {
            decimalPosition = out.length;
        } else {
            decimalPosition -= 1;
        }
        for (i = decimalPosition - 3; i > 0; i -= 3) {
            if (((i == 1) && (isNegative)) || (i == decimalPosition)) break;
            out = out.substr(0, i) + separator + out.substr(i);
        }
    }

    if (!out) {
        out = "";
    }

    return out;
}

var os = -1
var oe = -1
var posizioneCursore;
function GetPosition(o) {
    var t = o.value, s = getSelectionStart(o), e = getSelectionEnd(o)
    if (s == os && e == oe) return
    posizioneCursore = s;
}

function getSelectionStart(o) {
    if (o.createTextRange) {
        var r = document.selection.createRange().duplicate()
        r.moveEnd('character', o.value.length)
        if (r.text == '') return o.value.length
        return o.value.lastIndexOf(r.text)
    } else return o.selectionStart
}

function getSelectionEnd(o) {
    if (o.createTextRange) {
        var r = document.selection.createRange().duplicate()
        r.moveStart('character', -o.value.length)
        return r.text.length
    } else return o.selectionEnd
}



// ********** Termine Funzioni per la punteggiatura dinamica *************
