personSearch1.js
4.47 KB
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
/*
* requires fittingStrings.js to be loaded first
*/
/*
* provide alternative Names already existent in DB to given names in
* input text fields:
*
* Arguments:
* fname / ID of the input text field with firstname
* sname / ID of the input text field with surname
* fnameAlt / ID of the list that recieves the alternative firstnames
* snameAlt / ID of the list that receives the alternative surnames
* names / Array containing the already existing names and persons.
*
* Methods:
* updField / exact match(no-case) found => update input field
* genAlternativesList / generate List of alternative Strings
* altSurnames / fill the list of alternative surnames
* altFirstnames / fill the list of alternative firstnames
*/
function c_personSearch1 (fname, sname, fnameAlt, snameAlt, names, person)
{
this.firstname = fname;
this.surname = sname;
this.fNameAlt = fnameAlt;
this.sNameAlt = snameAlt;
this.surnames = new Array ();
this.firstnames = new Array ();
this.persons = new Array ();
for (sn in names)
{
this.surnames.push (sn);
this.firstnames[sn] = new Array ();
this.persons[sn] = new Array ();
for (d in names[sn])
{
this.firstnames[sn].push (names[sn][d][0]);
this.persons[sn][names[sn][d][0]] = names[sn][d][1];
}
}
this.person = person;
this.sNameBest = new Array (-1, -1, null);
this.fNameBest = new Array (-1, -1, null);
this.show = function (id)
{
var node = document.getElementById (id);
node.parentNode.style.visibility = 'visible';
return false;
}
this.hide = function (id)
{
var node = document.getElementById (id);
node.parentNode.style.visibility = 'hidden';
return false;
}
this.updField = function (field, alternative, altId)
{
if (alternative[1] == 0)
field.value = alternative[2];
else
if (alternative[1] >= 0)
return this.show (altId);
return this.hide (altId);
}
this.genAlternativesList = function (bestFit, dists)
{
if (dists.length > 0)
// there are caculated distances for surnames
{
var i = 0;
var bestMatch = dists[0].split ('::'); // infos for best match
// reset matches select
while ((child = bestFit.firstChild) != null)
bestFit.removeChild (child);
for (match in dists)
// walk through all matches
{
// get infos for match
var sMatch = dists[match].split ('::');
// create list entries
newLi = document.createElement ('li');
liTxt = document.createTextNode (sMatch[2]);
newLi.appendChild (liTxt);
bestFit.appendChild (newLi);
i=i+1;
if (i >= bestMatch[1])
// dont show more than best match distance matches.
break;
}
}
else
// there are no caculated distances for surnames
{
// reset matches select
while ((child = bestFit.firstChild) != null)
bestFit.removeChild (child);
return new Array (-1, -1, '');
}
return bestMatch;
}
this.altSurnames = function ()
{
var surname = document.getElementById (this.surname);
var sNameAlt = document.getElementById (this.sNameAlt);
var dists = fittingStrings (surname.value, this.surnames, false);
this.sNameBest = this.genAlternativesList (sNameAlt, dists);
this.updField (surname, this.sNameBest, this.sNameAlt)
if (this.sNameBest[1] == 0)
{
var firstname = document.getElementById (this.firstname);
var dists = fittingStrings (
firstname.value, this.firstnames[this.sNameBest[2]], false);
if (dists.length > 0)
this.fNameBest = dists[0].split ('::');
if (this.person != null)
if (this.fNameBest[1] == 0)
this.person.update (
this.persons[this.sNameBest[2]][this.fNameBest[2]]);
else
this.person.reset ();
}
else
{
this.fNameBest = new Array (-1, -1, null);
if (this.person != null)
this.person.reset ();
}
return false;
}
this.altFirstnames = function (person)
{
var firstname = document.getElementById (this.firstname);
var fNameAlt = document.getElementById (this.fNameAlt);
if (this.sNameBest[1] == 0)
{
var surname = document.getElementById (this.surname);
var dists = fittingStrings (
firstname.value, this.firstnames[this.sNameBest[2]], false);
}
else
dists = new Array ();
this.fNameBest = this.genAlternativesList (fNameAlt, dists);
this.updField (firstname, this.fNameBest, this.fNameAlt)
if (this.person != null)
if (this.sNameBest[1] == 0 && this.fNameBest[1] == 0)
this.person.update (
this.persons[this.sNameBest[2]][this.fNameBest[2]]);
else
this.person.reset ();
return false;
}
}