/**
 * @author nissl
 */

var checkControlCode = true

function getSurnameCode(surname) {
    surname = surname.toUpperCase();
    var strSurname = '';
    for (i = 0;i < surname.length; i++) {
        switch (surname.charAt(i)) {
            case 'A' :
            case 'E' :
            case 'I' :
            case 'O' :
            case 'U' :
                break;
            default :
                if ((surname.charAt(i) <= 'Z') && (surname.charAt(i) > 'A')) {
                    strSurname = strSurname + surname.charAt(i);
                }
        }
    }
    if (strSurname.length < 3) {
        for (i = 0;i < surname.length; i++) {
            switch (surname.charAt(i)) {
                case 'A' :
                case 'E' :
                case 'I' :
                case 'O' :
                case 'U' :
                    strSurname = strSurname + surname.charAt(i);
            }
        }
        if (strSurname.length < 3) {
            for (i = strSurname.length;i <= 3; i++) {
                strSurname = strSurname + 'X';
            }
        }
    }
    strSurname = strSurname.substring(0, 3);
    return strSurname;
}

function getNameCode(name) {
    name = name.toUpperCase()
    var strName = '';
    for (i = 0;i < name.length; i++) {
        switch (name.charAt(i)) {
            case 'A' :
            case 'E' :
            case 'I' :
            case 'O' :
            case 'U' :
                break;
            default :
                if ((name.charAt(i) <= 'Z') && (name.charAt(i) > 'A')) {
                    strName = strName + name.charAt(i);
                }
        }
    }
    if (strName.length > 3) {
        strName = strName.substring(0, 1) + strName.substring(2, 4);
    } else {
        if (strName.length < 3) {
            for (i = 0;i < name.length; i++) {
                switch (name.charAt(i)) {
                    case 'A' :
                    case 'E' :
                    case 'I' :
                    case 'O' :
                    case 'U' :
                        strName = strName + name.charAt(i);
                }
            }
            if (strName.length < 3) {
                for (i = strName.length;i <= 3; i++) {
                    strName = strName + 'X';
                }
            }
        }
        strName = strName.substring(0, 3);
    }

    return strName;
}

function getDayAndSexCode(day, sex) {
    var daySex = 0;
    day = parseInt(day, 10);
    var strDaySex = '';
    switch (sex) {
        case 0 :
        case 1 :
            break;
        case "M" :
        case "m" :
            sex = 0;
            break;
        case "F" :
        case "f" :
            sex = 1;
            break;
        default :
            sex = 0;
    }
    daySex = day + (40 * sex);
    if (daySex < 10) {
        strDaySex = "0" + daySex;
    } else {
        strDaySex = daySex;
    }
    return strDaySex;
}

function getCityCode(jsondb, city) {
    if (jsondb[city.toUpperCase()])
        return jsondb[city.toUpperCase()][1];
    else
        return false;
}

function getCityCap(jsondb, city) {
    if (jsondb[city.toUpperCase()])
        return jsondb[city.toUpperCase()][2];
    else
        return false;
}

function getCityProv(jsondb, city) {
    if (jsondb[city.toUpperCase()])
        return jsondb[city.toUpperCase()][0];
    else
        return false;
}

function getYearCode(year) {
    return year.slice(-2, year.length);
}

function getMonthCode(month) {
    month = parseInt(month, 10);
    switch (month) {
        case 1 :
            return 'A';
            break;
        case 2 :
            return 'B';
            break;
        case 3 :
            return 'C';
            break;
        case 4 :
            return 'D';
            break;
        case 5 :
            return 'E';
            break;
        case 6 :
            return 'H';
            break;
        case 7 :
            return 'L';
            break;
        case 8 :
            return 'M';
            break;
        case 9 :
            return 'P';
            break;
        case 10 :
            return 'R';
            break;
        case 11 :
            return 'S';
            break;
        case 12 :
            return 'T';
            break;
    }
}

function getControlCode(partialCodFis) {
    var value = 0
    partialCodFis = partialCodFis.toUpperCase();
    for (i = 0;i < 15; i++) {
        if (((i + 1) % 2) != 0) // dispari
        {
            switch (partialCodFis.charAt(i)) {
                case '0' :
                case 'A' : {
                    value += 1;
                    break;
                }
                case '1' :
                case 'B' : {
                    value += 0;
                    break;
                }
                case '2' :
                case 'C' : {
                    value += 5;
                    break;
                }
                case '3' :
                case 'D' : {
                    value += 7;
                    break;
                }
                case '4' :
                case 'E' : {
                    value += 9;
                    break;
                }
                case '5' :
                case 'F' : {
                    value += 13;
                    break;
                }
                case '6' :
                case 'G' : {
                    value += 15;
                    break;
                }
                case '7' :
                case 'H' : {
                    value += 17;
                    break;
                }
                case '8' :
                case 'I' : {
                    value += 19;
                    break;
                }
                case '9' :
                case 'J' : {
                    value += 21;
                    break;
                }
                case 'K' : {
                    value += 2;
                    break;
                }
                case 'L' : {
                    value += 4;
                    break;
                }
                case 'M' : {
                    value += 18;
                    break;
                }
                case 'N' : {
                    value += 20;
                    break;
                }
                case 'O' : {
                    value += 11;
                    break;
                }
                case 'P' : {
                    value += 3;
                    break;
                }
                case 'Q' : {
                    value += 6;
                    break;
                }
                case 'R' : {
                    value += 8;
                    break;
                }
                case 'S' : {
                    value += 12;
                    break;
                }
                case 'T' : {
                    value += 14;
                    break;
                }
                case 'U' : {
                    value += 16;
                    break;
                }
                case 'V' : {
                    value += 10;
                    break;
                }
                case 'W' : {
                    value += 22;
                    break;
                }
                case 'X' : {
                    value += 25;
                    break;
                }
                case 'Y' : {
                    value += 24;
                    break;
                }
                case 'Z' : {
                    value += 23;
                    break;
                }
            }
        } else // pari
        {
            switch (partialCodFis.charAt(i)) {
                case '0' :
                case 'A' : {
                    value += 0;
                    break;
                }
                case '1' :
                case 'B' : {
                    value += 1;
                    break;
                }
                case '2' :
                case 'C' : {
                    value += 2;
                    break;
                }
                case '3' :
                case 'D' : {
                    value += 3;
                    break;
                }
                case '4' :
                case 'E' : {
                    value += 4;
                    break;
                }
                case '5' :
                case 'F' : {
                    value += 5;
                    break;
                }
                case '6' :
                case 'G' : {
                    value += 6;
                    break;
                }
                case '7' :
                case 'H' : {
                    value += 7;
                    break;
                }
                case '8' :
                case 'I' : {
                    value += 8;
                    break;
                }
                case '9' :
                case 'J' : {
                    value += 9;
                    break;
                }
                case 'K' : {
                    value += 10;
                    break;
                }
                case 'L' : {
                    value += 11;
                    break;
                }
                case 'M' : {
                    value += 12;
                    break;
                }
                case 'N' : {
                    value += 13;
                    break;
                }
                case 'O' : {
                    value += 14;
                    break;
                }
                case 'P' : {
                    value += 15;
                    break;
                }
                case 'Q' : {
                    value += 16;
                    break;
                }
                case 'R' : {
                    value += 17;
                    break;
                }
                case 'S' : {
                    value += 18;
                    break;
                }
                case 'T' : {
                    value += 19;
                    break;
                }
                case 'U' : {
                    value += 20;
                    break;
                }
                case 'V' : {
                    value += 21;
                    break;
                }
                case 'W' : {
                    value += 22;
                    break;
                }
                case 'X' : {
                    value += 23;
                    break;
                }
                case 'Y' : {
                    value += 24;
                    break;
                }
                case 'Z' : {
                    value += 25;
                    break;
                }
            }
        }
    }
    value %= 26;
    switch (value) {
        case 0 : {
            controlChar = 'A';
            break;
        }
        case 1 : {
            controlChar = 'B';
            break;
        }
        case 2 : {
            controlChar = 'C';
            break;
        }
        case 3 : {
            controlChar = 'D';
            break;
        }
        case 4 : {
            controlChar = 'E';
            break;
        }
        case 5 : {
            controlChar = 'F';
            break;
        }
        case 6 : {
            controlChar = 'G';
            break;
        }
        case 7 : {
            controlChar = 'H';
            break;
        }
        case 8 : {
            controlChar = 'I';
            break;
        }
        case 9 : {
            controlChar = 'J';
            break;
        }
        case 10 : {
            controlChar = 'K';
            break;
        }
        case 11 : {
            controlChar = 'L';
            break;
        }
        case 12 : {
            controlChar = 'M';
            break;
        }
        case 13 : {
            controlChar = 'N';
            break;
        }
        case 14 : {
            controlChar = 'O';
            break;
        }
        case 15 : {
            controlChar = 'P';
            break;
        }
        case 16 : {
            controlChar = 'Q';
            break;
        }
        case 17 : {
            controlChar = 'R';
            break;
        }
        case 18 : {
            controlChar = 'S';
            break;
        }
        case 19 : {
            controlChar = 'T';
            break;
        }
        case 20 : {
            controlChar = 'U';
            break;
        }
        case 21 : {
            controlChar = 'V';
            break;
        }
        case 22 : {
            controlChar = 'W';
            break;
        }
        case 23 : {
            controlChar = 'X';
            break;
        }
        case 24 : {
            controlChar = 'Y';
            break;
        }
        case 25 : {
            controlChar = 'Z';
            break;
        }
    }
    return controlChar;
}

function getFiscalCode(name, surname, year, month, day, sex, city) {
    codfis = "" + getSurnameCode(surname) + getNameCode(name)
            + getYearCode(year) + getMonthCode(month)
            + getDayAndSexCode(day, sex) + getCityCode(CITIES, city);
    codfis += getControlCode(codfis);
    return codfis.toUpperCase();
}

function checkFiscalCode(fiscalCode, checkControlCode, name, surname, year,
        month, day, sex, city) {
    var codfis = getFiscalCode(name, surname, year, month, day, sex, city);

    if (checkControlCode) {
        codfis = codfis.slice(0, -1)
        fiscalCode = fiscalCode.slice(0, -1)
    }

    if (cf == fiscalCode) {
        return true;
    } else {
        return false;
    }
}

var LASTOUTMESSAGE = ""

function outMessage(message) {
    LASTOUTMESSAGE = message;
}

function isValidPiva(piva) {
    if (piva.length != 11) {
        outMessage("La lunghezza della partita IVA non è corretta.");
        return false;
    }
    validi = "0123456789";
    for (i = 0;i < 11; i++) {
        if (validi.indexOf(piva.charAt(i)) == -1) {
            outMessage("La partita IVA contiene un carattere non valido: "
                    + piva.charAt(i));
            return false;
        }
    }
    s = 0;
    for (i = 0;i <= 9; i += 2)
        s += piva.charCodeAt(i) - '0'.charCodeAt(0);
    for (i = 1;i <= 9; i += 2) {
        c = 2 * (piva.charCodeAt(i) - '0'.charCodeAt(0));
        if (c > 9)
            c = c - 9;
        s += c;
    }
    if ((10 - s % 10) % 10 != piva.charCodeAt(10) - '0'.charCodeAt(0)) {
        outMessage("La partita IVA non è valida");
        return false;
    }
    return true;
}

function isValidCodFis(codfis) {

    var validi;
    var i;
    var s;
    var set1;
    var set2;
    var setpari;
    var setdisp;
    codfis = codfis.toUpperCase();

    if (codfis.length != 16) {
        outMessage("La lunghezza del codice fiscale non è corretta");
        return false;
    }

    validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    for (i = 0;i < 16; i++) {
        if (validi.indexOf(codfis.charAt(i)) == -1) {
            outMessage("Il codice fiscale contiene un carattere non valido: "
                    + codfis.charAt(i));
            return false;
        }
    }
    set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
    setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
    s = 0;
    for (i = 1;i <= 13; i += 2)
        s += setpari.indexOf(set2.charAt(set1.indexOf(codfis.charAt(i))));
    for (i = 0;i <= 14; i += 2)
        s += setdisp.indexOf(set2.charAt(set1.indexOf(codfis.charAt(i))));
    if (s % 26 != codfis.charCodeAt(15) - 'A'.charCodeAt(0)) {
        outMessage("Il codice fiscale non è corretto");
        return false;
    }
    return true;
}



function maxCodFis() {
    cod = getFiscalCode("massimo", "ghisalberti", "1963", "06", "24", "M",
            "cecina")
    alert(cod);
}

function nicCodFis() {
    cod = getFiscalCode("nicola", "gori", "1977", "10", "09", "M", "perugia")
    alert(cod);
}

