1 | var URL = 0; |
---|
2 | var FILENAME = 1; |
---|
3 | var DESCRIPTION = 2; |
---|
4 | var CONTENTS = 3; |
---|
5 | var MATCH_TYPE = 4; |
---|
6 | var N_MATCHES = 5; |
---|
7 | var SCORE = 6; |
---|
8 | var i = 7; |
---|
9 | var MATCHES = 8; |
---|
10 | |
---|
11 | var html; |
---|
12 | |
---|
13 | var searchString; |
---|
14 | var wildcard; |
---|
15 | var invalidSearchString; |
---|
16 | var origSearchString; |
---|
17 | |
---|
18 | FILENAME_SCORE_ORDER = 0; |
---|
19 | URL_SCORE_ORDER = 1; |
---|
20 | DESCRIPTION_SCORE_ORDER = 2; |
---|
21 | CONTENT_SCORE_ORDER = 3; |
---|
22 | |
---|
23 | SCORE_PER_TYPE = 15000; |
---|
24 | |
---|
25 | var sortResultsByType = true; |
---|
26 | var omitDescriptions = false; |
---|
27 | var addMatchSummary = true; |
---|
28 | |
---|
29 | var searchTitles = true; |
---|
30 | var searchDescriptions = true; |
---|
31 | var searchContent = true; |
---|
32 | |
---|
33 | var footer = ""; |
---|
34 | |
---|
35 | function isAlnumAmp(ch) { |
---|
36 | if ((ch >= "a" && ch <= "z") || (ch == "&") ||(ch >= "A" && ch <= "Z") || (ch >= "0" && ch <="9")) { |
---|
37 | return true; |
---|
38 | } else { |
---|
39 | return false; |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | function searchElement(fileNumber, matchType, upperSearchString) { |
---|
44 | var element = a[fileNumber][matchType].toUpperCase(); |
---|
45 | var w, x, y; |
---|
46 | var z = 0; |
---|
47 | |
---|
48 | a[fileNumber][N_MATCHES] = 0; |
---|
49 | w = element.indexOf(upperSearchString); |
---|
50 | while (w >= 0){ |
---|
51 | z = z + w + 1; |
---|
52 | if ((wildcard == -2) || (wildcard == -5)) { |
---|
53 | x = false; |
---|
54 | } else { |
---|
55 | if (w == 0) { |
---|
56 | x = false; |
---|
57 | } else { |
---|
58 | x = isAlnumAmp(element.charAt(w - 1)); |
---|
59 | } |
---|
60 | } |
---|
61 | if ((wildcard == -3) || (wildcard == -5)) { |
---|
62 | y = false; |
---|
63 | } else { |
---|
64 | if (element.length - w == upperSearchString.length) { |
---|
65 | y = false; |
---|
66 | } else { |
---|
67 | y = isAlnumAmp(element.charAt(w + upperSearchString.length)); |
---|
68 | } |
---|
69 | } |
---|
70 | if (!x && !y) { |
---|
71 | a[fileNumber][MATCHES + a[fileNumber][N_MATCHES]] = z - 1; |
---|
72 | a[fileNumber][N_MATCHES]++; |
---|
73 | } |
---|
74 | element = element.substring(w + 1, element.length); |
---|
75 | while (isAlnumAmp(element.charAt(0)) && element.length > 0) { |
---|
76 | element = element.substring(1, element.length); |
---|
77 | z++; |
---|
78 | } |
---|
79 | w = element.indexOf(upperSearchString); |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | function searchFile(fileNumber, upperSearchString) { |
---|
84 | var matchIndex = -1, matchType; |
---|
85 | |
---|
86 | a[fileNumber][MATCH_TYPE] = -1; |
---|
87 | |
---|
88 | while (++matchIndex <= CONTENTS && a[fileNumber][MATCH_TYPE] == -1) { |
---|
89 | matchType = matchIndex == 0 ? FILENAME : (matchIndex == 1 ? DESCRIPTION : (matchIndex == 2 ? URL : CONTENTS)); |
---|
90 | if ((matchType == FILENAME || matchType == URL) && !searchTitles) { |
---|
91 | continue; |
---|
92 | } |
---|
93 | |
---|
94 | if (matchType == DESCRIPTION && !searchDescriptions) { |
---|
95 | continue; |
---|
96 | } |
---|
97 | |
---|
98 | if (matchType == CONTENTS && !searchContent) { |
---|
99 | continue; |
---|
100 | } |
---|
101 | |
---|
102 | searchElement(fileNumber, matchType, upperSearchString); |
---|
103 | if (a[fileNumber][N_MATCHES] > 0) { |
---|
104 | a[fileNumber][MATCH_TYPE] = matchType; |
---|
105 | } |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | function sortResults() { |
---|
110 | var fileNumber, t, tempScore, E; |
---|
111 | |
---|
112 | for (fileNumber = 1; fileNumber < a.length; fileNumber++) { |
---|
113 | a[fileNumber][i] = fileNumber; |
---|
114 | } |
---|
115 | |
---|
116 | if (sortResultsByType) { |
---|
117 | for (fileNumber = 1; fileNumber < a.length; fileNumber++) { |
---|
118 | if (a[fileNumber][MATCH_TYPE] == FILENAME) { |
---|
119 | a[fileNumber][SCORE] = (4 - FILENAME_SCORE_ORDER) * SCORE_PER_TYPE; |
---|
120 | } else if (a[fileNumber][MATCH_TYPE] == DESCRIPTION) { |
---|
121 | a[fileNumber][SCORE] = (4 - DESCRIPTION_SCORE_ORDER) * SCORE_PER_TYPE; |
---|
122 | } else if (a[fileNumber][MATCH_TYPE] == URL) { |
---|
123 | a[fileNumber][SCORE] = (4 - URL_SCORE_ORDER) * SCORE_PER_TYPE; |
---|
124 | } else { |
---|
125 | a[fileNumber][SCORE] = (4 - CONTENT_SCORE_ORDER) * SCORE_PER_TYPE + a[fileNumber][N_MATCHES]; |
---|
126 | } |
---|
127 | } |
---|
128 | for (fileNumber = 2; fileNumber < a.length; fileNumber++) { |
---|
129 | tempScore = a[fileNumber][SCORE]; |
---|
130 | E = a[fileNumber][i]; |
---|
131 | for (t = fileNumber; t > 1 && tempScore > a[t-1][SCORE]; t--) { |
---|
132 | a[t][SCORE] = a[t-1][SCORE]; |
---|
133 | a[t][i] = a[t-1][i]; |
---|
134 | } |
---|
135 | a[t][SCORE] = tempScore; |
---|
136 | a[t][i] = E; |
---|
137 | } |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | function putMatchSummary(fileNumber) { |
---|
142 | var pluralSuffix = a[fileNumber][N_MATCHES]==1 ? "" : "es"; |
---|
143 | |
---|
144 | html += ""; |
---|
145 | html += "<font color=gray>"; |
---|
146 | if (a[fileNumber][MATCH_TYPE] == FILENAME) { |
---|
147 | html += " - matched title"; |
---|
148 | } else { |
---|
149 | if (a[fileNumber][MATCH_TYPE] == DESCRIPTION) { |
---|
150 | html += " - matched description"; |
---|
151 | } else { |
---|
152 | if (a[fileNumber][MATCH_TYPE] == URL) { |
---|
153 | html += " - matched URL"; |
---|
154 | } else { |
---|
155 | html += " - " + a[fileNumber][N_MATCHES] + " match" + pluralSuffix + ""; |
---|
156 | } |
---|
157 | } |
---|
158 | } |
---|
159 | html += "</font>"; |
---|
160 | html += ""; |
---|
161 | } |
---|
162 | |
---|
163 | function putMatchDescription(fileNumber, curMatch) { |
---|
164 | var matchLocation = a[fileNumber][MATCHES + curMatch - 1]; |
---|
165 | var matchStart = matchLocation < 35 ? 0 : matchLocation - 35; |
---|
166 | var matchEnd = (matchLocation + 35 > a[fileNumber][CONTENTS].length) ? a[fileNumber][CONTENTS].length : matchLocation + 35; |
---|
167 | |
---|
168 | var Q = false; |
---|
169 | while ((matchStart >= 0) && !Q) { |
---|
170 | if (isAlnumAmp(a[fileNumber][CONTENTS].charAt(matchStart))) { |
---|
171 | matchStart--; |
---|
172 | } else { |
---|
173 | Q = true; |
---|
174 | } |
---|
175 | } |
---|
176 | matchStart++; |
---|
177 | |
---|
178 | Q = false; |
---|
179 | while ((matchEnd > matchLocation) && !Q) { |
---|
180 | if (isAlnumAmp(a[fileNumber][CONTENTS].charAt(matchEnd))) { |
---|
181 | matchEnd--; |
---|
182 | } else { |
---|
183 | Q = true; |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | html += "<BR>\".. " + a[fileNumber][CONTENTS].substring(matchStart, matchLocation); |
---|
188 | html += "<B>" + a[fileNumber][CONTENTS].substring(matchLocation, matchLocation + searchString.length) + "</B>"; |
---|
189 | html += a[fileNumber][CONTENTS].substring(matchLocation + searchString.length, matchEnd) + " ..\""; |
---|
190 | } |
---|
191 | |
---|
192 | function putAllMatchDescriptions(fileNumber) { |
---|
193 | if (omitDescriptions == false) { |
---|
194 | var curMatch = 1; |
---|
195 | while ((curMatch < 4) && (curMatch <= a[fileNumber][N_MATCHES])) { |
---|
196 | putMatchDescription(fileNumber, curMatch); |
---|
197 | curMatch++; |
---|
198 | } |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | function putFoundString(v, R) { |
---|
203 | html += v.substring(0, R); |
---|
204 | html += "<B>" + v.substring(R, R + searchString.length) + "</B>"; |
---|
205 | html += v.substring(R + searchString.length, v.length); |
---|
206 | } |
---|
207 | |
---|
208 | function putItem(fileNumber, itemNumber) { |
---|
209 | html += "<p>" + itemNumber + ". "; |
---|
210 | html += "<A href=\"" + a[fileNumber][URL] + "\" target=\"file_frame\">" + a[fileNumber][FILENAME] + "</a>"; |
---|
211 | |
---|
212 | if (a[fileNumber][MATCH_TYPE] == CONTENTS) { |
---|
213 | putAllMatchDescriptions(fileNumber); |
---|
214 | } else { |
---|
215 | html += ""; |
---|
216 | } |
---|
217 | |
---|
218 | if (a[fileNumber][MATCH_TYPE] == DESCRIPTION) { |
---|
219 | html += "<font color=gray>"; |
---|
220 | html += "<br>Description: "; |
---|
221 | html += "</font>"; |
---|
222 | html += ""; |
---|
223 | putFoundString(a[fileNumber][DESCRIPTION], a[fileNumber][MATCHES]); |
---|
224 | } else { |
---|
225 | if( a[fileNumber][DESCRIPTION].length > 0 ) { |
---|
226 | html += "<font color=gray>"; |
---|
227 | html += "<br>Description: "; |
---|
228 | html += "</font>"; |
---|
229 | html += "" + a[fileNumber][DESCRIPTION]; |
---|
230 | } else { |
---|
231 | } |
---|
232 | } |
---|
233 | html += "<br>"; |
---|
234 | html += "<font color=#005500>"; |
---|
235 | if (a[fileNumber][MATCH_TYPE] == URL) { |
---|
236 | putFoundString(a[fileNumber][URL], a[fileNumber][MATCHES]); |
---|
237 | } else { |
---|
238 | html += a[fileNumber][URL]; |
---|
239 | } |
---|
240 | html += "</font>"; |
---|
241 | if (addMatchSummary) { |
---|
242 | putMatchSummary(fileNumber); |
---|
243 | } |
---|
244 | html += "<br>"; |
---|
245 | } |
---|
246 | |
---|
247 | function putResults() { |
---|
248 | var itemNumber = 0; |
---|
249 | |
---|
250 | if (!(invalidSearchString)) { |
---|
251 | for (var fileNumber = 1; fileNumber < a.length; fileNumber++) { |
---|
252 | if (a[a[fileNumber][i]][N_MATCHES] > 0) { |
---|
253 | putItem(a[fileNumber][i], ++itemNumber); |
---|
254 | } |
---|
255 | } |
---|
256 | } else { |
---|
257 | if (wildcard == -4) { |
---|
258 | html += "<P><B>ERROR:</B> The wildcard chararcter (*) must be at the beginning or end of the text."; |
---|
259 | } |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | function putHeader() { |
---|
264 | html += "<html><head><title>Search results for \"" + origSearchString + "\"</title>" |
---|
265 | html += getStyles(); |
---|
266 | html += "</head><body>"; |
---|
267 | html += "<table CELLPADDING=\"3\" CELLSPACING=\"0\" CLASS=\"listing\">" |
---|
268 | html += "<tr><td CLASS=\"title\">Search Results</td></tr>" |
---|
269 | html += "</table>" |
---|
270 | html += "<p>You searched for <b>" + origSearchString +".</b>"; |
---|
271 | } |
---|
272 | |
---|
273 | function putFooter() { |
---|
274 | var nMatches = 0; |
---|
275 | |
---|
276 | if (!(invalidSearchString)) { |
---|
277 | for (var fileNumber = 1; fileNumber < a.length; fileNumber++) { |
---|
278 | if (a[fileNumber][N_MATCHES] > 0) { |
---|
279 | nMatches++; |
---|
280 | } |
---|
281 | } |
---|
282 | } |
---|
283 | |
---|
284 | if (nMatches == 0) { |
---|
285 | html += "<P>No pages matched your search. "; |
---|
286 | } else { |
---|
287 | var G = nMatches == 1 ? "" : "s"; |
---|
288 | html += "<P><FONT CLASS=\"list_tagline\">" + nMatches + " page" + G + " listed.</FONT> "; |
---|
289 | } |
---|
290 | html += footer; |
---|
291 | html += "</p></body></html>"; |
---|
292 | } |
---|
293 | |
---|
294 | function launchBrowser() { |
---|
295 | var htmlCode = html; |
---|
296 | |
---|
297 | iu = open("", "Object", "resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,width=475,height=600"); |
---|
298 | |
---|
299 | if ((navigator.appName.indexOf("Microsoft")!=-1) && (navigator.appVersion.indexOf("3.0") != -1)) { |
---|
300 | alert("Click to see results"); |
---|
301 | } |
---|
302 | |
---|
303 | iu.document.open(); |
---|
304 | iu.document.write(htmlCode); |
---|
305 | iu.document.close(); |
---|
306 | } |
---|
307 | |
---|
308 | function replaceSpecialChars(str) { |
---|
309 | var returnStr = ""; |
---|
310 | |
---|
311 | for (var index = 0; index < str.length; index++) { |
---|
312 | if (str.charAt(index) == "<") { |
---|
313 | returnStr += "<"; |
---|
314 | } else if (str.charAt(index) == ">") { |
---|
315 | returnStr += ">"; |
---|
316 | } else if (str.charAt(index) == "\"") { |
---|
317 | returnStr += """; |
---|
318 | } else { |
---|
319 | returnStr += str.charAt(index); |
---|
320 | } |
---|
321 | } |
---|
322 | return(returnStr); |
---|
323 | } |
---|
324 | |
---|
325 | function checkSearchString() { |
---|
326 | wildcard = searchString.indexOf("*"); |
---|
327 | if (wildcard == 0) { |
---|
328 | wildcard = -2; |
---|
329 | invalidSearchString = false; |
---|
330 | } else if (wildcard == searchString.length -1) { |
---|
331 | wildcard = -3; |
---|
332 | invalidSearchString = false; |
---|
333 | } else if (wildcard > 0 ) { |
---|
334 | wildcard = -4; |
---|
335 | invalidSearchString = true; |
---|
336 | } else { |
---|
337 | invalidSearchString = false; |
---|
338 | } |
---|
339 | |
---|
340 | if (searchString.indexOf("*") != searchString.lastIndexOf("*")) { |
---|
341 | if (wildcard == -2) { |
---|
342 | if (searchString.lastIndexOf("*") == searchString.length - 1) { |
---|
343 | wildcard = -5; |
---|
344 | } else { |
---|
345 | wildcard = -4; |
---|
346 | invalidSearchString = true; |
---|
347 | } |
---|
348 | } |
---|
349 | } |
---|
350 | |
---|
351 | if ((wildcard == -2) || (wildcard == -5)) { |
---|
352 | searchString = searchString.substring(1, searchString.length); |
---|
353 | } |
---|
354 | |
---|
355 | if ((wildcard == -3) || (wildcard == -5)) { |
---|
356 | searchString = searchString.substring(0, searchString.length - 1); |
---|
357 | } |
---|
358 | } |
---|
359 | |
---|
360 | function toggleOmitDescriptions() { |
---|
361 | omitDescriptions = !omitDescriptions; |
---|
362 | } |
---|
363 | |
---|
364 | function toggleSortResults() { |
---|
365 | sortResultsByType = !sortResultsByType; |
---|
366 | } |
---|
367 | |
---|
368 | function toggleMatchSummary() { |
---|
369 | addMatchSummary = !addMatchSummary; |
---|
370 | } |
---|
371 | |
---|
372 | function toggleSearchTitles() { |
---|
373 | searchTitles = !searchTitles; |
---|
374 | } |
---|
375 | |
---|
376 | function toggleSearchDescriptions() { |
---|
377 | searchDescriptions = !searchDescriptions; |
---|
378 | } |
---|
379 | |
---|
380 | function toggleSearchContent() { |
---|
381 | searchContent = !searchContent; |
---|
382 | } |
---|
383 | |
---|
384 | function startsearch() { |
---|
385 | var upperSearchString; |
---|
386 | searchString = document.formSearch.txtSearch.value; |
---|
387 | |
---|
388 | if ((searchString.length > 0) && (searchString != "*")) { |
---|
389 | html = ""; |
---|
390 | origSearchString = searchString; |
---|
391 | searchString = replaceSpecialChars(searchString); |
---|
392 | checkSearchString(); |
---|
393 | upperSearchString = searchString.toUpperCase(); |
---|
394 | if (!(invalidSearchString)) { |
---|
395 | for (var fileNumber = 1; fileNumber < a.length; fileNumber++) { |
---|
396 | searchFile(fileNumber, upperSearchString); |
---|
397 | } |
---|
398 | sortResults(); |
---|
399 | } |
---|
400 | putHeader(); |
---|
401 | putResults(); |
---|
402 | putFooter(); |
---|
403 | launchBrowser(); |
---|
404 | } |
---|
405 | } |
---|