-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
280 lines (230 loc) · 7.08 KB
/
main.js
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
var codicecat;
function calcola(surname,name,sex,birthplace,provincia,birthday){
//if is empty
if(surname==""||
name==""||
sex==""||
birthplace==""||
provincia==""){
errore();
return 0;
}
name=name.replace(/ /g,'')
surname=surname.replace(/ /g,'')
birthplace=adapt(birthplace);
console.log(name+" " +surname+" "+ birthplace+ provincia)
codicecatastale(birthplace,provincia);
let codicefiscale= inizio(surname,name)+data(birthday,sex)+codicecat;
codicefiscale =codiceverifica(codicefiscale);
//if is invalid
if(codicefiscale.includes("undefined")){
errore();
return 0;
}
document.getElementById("cf").innerHTML=codicefiscale;
document.getElementById("form").style.display = "none";
document.getElementById("result").style.display = "block";
}
function inizio(cognome,nome){
let consonanti=[];
let vocali=[];
//cognome
for(let i=0;i<cognome.length;i++){
if(cognome.charAt(i)=="A"||
cognome.charAt(i)=="E"||
cognome.charAt(i)=="I"||
cognome.charAt(i)=="O"||
cognome.charAt(i)=="U"){
vocali.push(cognome.charAt(i));
}
else{
consonanti.push(cognome.charAt(i));
}
}
if(consonanti.length>=3){
cognome = consonanti[0]+consonanti[1]+consonanti[2];
}
else if(consonanti.length>0&&consonanti.length<3){
cognome="";
for(let i=0;i<consonanti.length;i++){
cognome+=consonanti[i];
}
for(let i=0;cognome.length<3&&vocali[i]!=='undefined';i++){
if(typeof vocali[i]!=="undefined"){
cognome+=vocali[i];
}
else{
cognome+="X";
}
}
}
//nome
consonanti=[];
vocali=[];
for(let i=0;i<nome.length;i++){
if(nome.charAt(i)=="A"||
nome.charAt(i)=="E"||
nome.charAt(i)=="I"||
nome.charAt(i)=="O"||
nome.charAt(i)=="U"){
vocali.push(nome.charAt(i));
}
else{
consonanti.push(nome.charAt(i));
}
}
if(consonanti.length>=4){
nome = consonanti[0]+consonanti[2]+consonanti[3];
}
else if(consonanti.length==3){
nome = consonanti[0]+consonanti[1]+consonanti[2];
}
else if(consonanti.length<3){
if(consonanti.length>0){
nome="";
for(let i=0;i<consonanti.length;i++){
nome+=consonanti[i];
}
for(let i=0;nome.length<3&&vocali[i]!=='undefined';i++){
if(typeof vocali[i]!=="undefined"){
nome+=vocali[i];
}
else{
nome+="X";
}
}
}
}
return cognome+nome;
}
function data(date,sex){
let year = date.slice(2,4);
let m=date.slice(5,7);
let mesi={"01":"A","02":"B","03":"C","04":"D","05":"E","06":"H","07":"L","08":"M","09":"P","10":"R","11":"S","12":"T"};
m= mesi[m];
let day=date.slice(8);
if(sex=="Femmina"){
day=Number(day)+40;
}
return year+m+day;
}
function codicecatastale(value,district){
var result
if(district=="EE"){
$.ajax({
url: "./data/estero.json",
dataType: "json",
type: "get",
async: false, // this doesn't allow to start the next function without having completed this one
cache: false,
success: function (obj){
const keyone= "Denominazione IT";
result= obj.stati.find((x => x[keyone] ==value));
codicecat =result["Codice AT"];
console.log(codicecat);
}
});
}
else{
$.ajax({
url: "./data/italia.json",
dataType: "json",
type: "get",
async: false, // this doesn't allow to start the next function without having completed this one
cache: false,
success: function (obj){
const keyone= "nome";
const keytwo = "sigla";
result= obj.comuni.find((x => x[keyone] ==value&& x[keytwo]==district));
codicecat =result["codiceCatastale"];
}
});
}
}
function codiceverifica(codicefiscale){
let dispari=0;
let pari=0;
const legdisp = {"0":"1",
"1":"0",
"2":"5",
"3":"7",
"4":"9",
"5":"13",
"6":"15",
"7":"17",
"8":"19",
"9":"21",
"A":"1",
"B":"0",
"C":"5",
"D":"7",
"E":"9",
"F":"13",
"G":"15",
"H":"17",
"I":"19",
"J":"21",
"K":"2",
"L":"4",
"M":"18",
"N":"20",
"O":"11",
"P":"3",
"Q":"6",
"R":"8",
"S":"12",
"T":"14",
"U":"16",
"V":"10",
"W":"22",
"X":"25",
"Y":"24",
"Z":"23"
}
for(let i=0;i<codicefiscale.length;i++){
//IF IS EVEN
if(i%2==1){
//IF IS NAN
if(isNaN(codicefiscale.charAt(i))){
pari+=(codicefiscale.charCodeAt(i))-65;
}
//ELSE
else{
pari+=Number(codicefiscale.charAt(i));
}
}
//IF IS UNEVEN
else if(i%2==0){
dispari+=Number(legdisp[codicefiscale.charAt(i)]);
}
}
return codicefiscale+ String.fromCharCode((pari+dispari)%26+65);
}
function adapt(value){
value.toLowerCase();
value= value.charAt(0).toUpperCase()+value.slice(1);
for(let i=0;i<value.length;i++){
if(value.charAt(i-1)==" "){
value=value.slice(0,i) +value.charAt(i).toUpperCase()+value.slice(i+1);
}
}
return value;
}
function ritenta(){
svuotaform();
document.getElementById("form").style.display = "block";
document.getElementById("result").style.display = "none";
document.getElementById("invalid").style.display = "none";
}
function errore(){
document.getElementById("form").style.display = "none";
document.getElementById("invalid").style.display = "block";
}
function svuotaform(){
document.getElementById('surname').value="";
document.getElementById('name').value="";
document.getElementById('sex').value="Maschio";
document.getElementById('birthplace').value="";
document.getElementById('provincia').value="";
document.getElementById('birthday').value="";
}