added files
[bcm963xx.git] / userapps / broadcom / cfm / html / usr_common.js
1 /* -------------------------------------------------------------------------- */
2 /*
3         (c) 2005-2006 U.S. Robotics Corporation
4 */
5 /* -------------------------------------------------------------------------- */
6
7
8 /* --------------------------------------------------------------------------
9
10         Return appropriate product name, based on wireless feature.
11
12 */
13 function getProductName(strWireless)
14 {
15         if (strWireless == "1")
16                 return "U.S. Robotics Wireless MAXg ADSL Gateway";
17         else
18                 return "U.S. Robotics ADSL 4-Port Router";
19 }
20
21 // version for HTML
22 function getProductNameHTML(strWireless)
23 {
24         if (strWireless == "1")
25                 return "U.S.&nbsp;Robotics Wireless <i>MAX</i>g ADSL Gateway";
26         else
27                 return "U.S.&nbsp;Robotics ADSL 4-Port Router";
28 }
29
30
31 /* --------------------------------------------------------------------------
32
33         Return <title> string for the page.
34
35 */
36 function setPageTitle(strWireless)
37 {
38         if (document.title == "")
39                 document.title = getProductName(strWireless);
40         else
41                 document.title = getProductName(strWireless) + " ยป " + document.title;
42 }
43
44
45 /* --------------------------------------------------------------------------
46
47         Page header and footer functions
48
49 */
50
51 /*
52         This function returns the filename of the current page.
53         It looks for both slash and backward slash.
54 */
55 function getPageFilename()
56 {
57         var strFilename = document.URL;
58         var ixSeparator = strFilename.lastIndexOf("/");
59         var ixSeparator2 = strFilename.lastIndexOf("\\");
60         if (ixSeparator2 > ixSeparator)
61                 ixSeparator = ixSeparator2;
62         if (ixSeparator != -1)
63                 strFilename = strFilename.substr(ixSeparator + 1);
64         return strFilename;
65 }
66
67 /*
68         This writes out a page's header, including the optional menu bar.
69         The title parameter is optional. If passed, it's written out
70         as the header text.
71
72         <ul id="idMenu">
73                 <li><label>Sunfishes</label>
74                         <ul>
75                                 <li><a href="#">Blackbanded sunfish</a></li>
76                                 <li><a href="#">Shadow bass</a></li>
77                                 <li><a href="#">Ozark bass</a></li>
78                                 <li><a href="#">White crappie</a></li>
79                         </ul>
80                 </li>
81
82                 <li><label>Grunts</label>
83                         <ul>
84                                 <li><a href="#">Smallmouth grunt</a></li>
85                                 <li><a href="#">Burrito</a></li>
86                                 <li><a href="#">Pigfish</a></li>
87                         </ul>
88                 </li>
89
90                 <li><label>Remoras</label>
91                         <ul>
92                                 <li><a href="#">Whalesucker</a></li>
93                                 <li><a href="#">Marlinsucker</a></li>
94                                 <li><a href="#">Ceylonese remora</a></li>
95                                 <li><a href="#">Spearfish remora</a></li>
96                                 <li><a href="#">Slender suckerfish</a></li>
97                         </ul>
98                 </li>
99         </ul>
100 */
101 function writeHeader(strTitle)
102 {
103 //      var strFilename = getPageFilename();              // get filename of current page
104
105         document.write("<img id=\"idImgHeader\" src=\"usr_banner.gif\">\n");
106
107         document.write("<div id=\"idHeader\">" + strTitle + "<\/div>\n");
108
109         var menuMain = createMenu();
110         menuMain.write();
111
112         document.write("<div id=\"idContent\">");
113 }
114
115
116 /*
117         This outputs the header for a Setup page.
118         The passed number indicates which step this is and how many steps there are.
119
120         <img id="idImgHeader" src="banner.gif">
121
122         <h2 class="clsSteps">Setup
123                 <span class="clsStepCurrent">1</span>
124                 <span class="clsStepNotDone">&hellip; 2 &hellip; 3</span>
125         </h2>
126
127         <h2 class="clsSteps">Setup
128                 <span class="clsStepDone">1 &hellip;</span>
129                 <span class="clsStepCurrent">2</span>
130                 <span class="clsStepNotDone">&hellip; 3</span>
131         </h2>
132
133
134         <h2 class="clsSteps">Setup
135                 <span class="clsStepDone">1 &hellip; 2 &hellip;</span>
136                 <span class="clsStepCurrent">3</span>
137         </h2>
138 */
139 function writeHeaderSetup(iStep, iSteps)
140 {
141         document.write("<img id=\"idImgHeader\" src=\"usr_banner.gif\">");
142
143         document.write("<div id=\"idSteps\">Setup ");
144
145         for (var i = 1; i <= iSteps; ++i)
146         {
147                 if (i == iStep)
148                         document.write("<span class=\"clsStepCurrent\">" + i + "<\/span> ");
149                 else
150                         document.write("<span class=\"clsStepNotDone\">" + i + "<\/span> ");
151
152                 if (i < iSteps)
153                         document.write("<span class=\"clsStepNotDone\">&hellip;<\/span> ");
154         }
155         document.write("<\/div>");
156
157         document.write("<div id=\"idContent\">");
158 }
159
160
161 /*
162         This writes the header for an "information" page.
163 */
164 function writeHeaderInfo(strName)
165 {
166         document.write("<img id=\"idImgHeader\" src=\"usr_banner.gif\">");
167         document.write("<div id=\"idHeader\">" + strName + "<\/div>");
168         document.write("<div id=\"idContent\">");
169 }
170
171
172 function writeFooter()
173 {
174         document.write("<\/div>");      // close "idContent"
175
176         document.write("<p class=\"clsCopyright\">&copy; 2004-2006 <a href=\"http://www.usr.com\">U.S. Robotics Corporation<\/a>.<\/p>");
177 }
178
179
180
181 /* --------------------------------------------------------------------------
182
183         Wireless info functions
184
185 */
186
187 /*
188         This is called to set the visibility of fields on
189         the Status and Tutorial pages.
190 */
191 // initializeSecurity("<%ejGetWl(wlAuthMode)%>", "<%ejGetWl(wlWep)%>")
192 function initializeSecurity(strAuthMode, strWEPmode)
193 {
194         var method = getSecurityMethod(strAuthMode, strWEPmode);
195         if (method.indexOf("RADIUS") != -1)
196         {
197                 // only show Encryption if also WPA
198                 if (method.indexOf("WPA") != -1)
199                         setVisibility("idEncrypt", true);
200
201                 setVisibility("idRADIUSkey", true);
202
203                 // Tutorial
204                 setVisibility("idKey8021x1", true);
205                 setVisibility("idKey8021x2", true);
206                 setVisibility("idKey8021x3", true);
207         }
208         else if (method.indexOf("WPA") != -1)
209         {
210                 setVisibility("idEncrypt", true);
211                 setVisibility("idKeyWPA", true);
212         }
213         else if (method.indexOf("WEP") != -1)
214         {
215                 setVisibility("idKeyWEP", true);
216         }
217 }
218
219 // getSecurityMethod("<%ejGetWl(wlAuthMode)%>", "<%ejGetWl(wlWep)%>")
220 function getSecurityMethod(strAuthMode, strWEPmode)
221 {
222         switch (strAuthMode)
223         {
224                 case "open":
225                         if (strWEPmode == "enabled")
226                                 return "WEP open";
227                         return "None";
228
229                 case "shared":                  return "WEP shared";
230
231                 case "psk":                             return "WPA";
232                 case "psk2":                    return "WPA2";
233                 case "psk2mix":         return "WPA2 and WPA";
234
235                 case "wpa":                             return "WPA with 802.1x (RADIUS)";
236                 case "wpa2":                    return "WPA2 with 802.1x (RADIUS)";
237                 case "wpa2mix":         return "WPA2 and WPA with 802.1x (RADIUS)";
238                 case "radius":                  return "802.1x (RADIUS)";
239
240                 default:                                        return "None";
241         }
242
243         return "";
244 }
245
246
247 /*
248         Return encryption method used for WPA or WEP
249 */
250 function getEncryption(strEncrypt)
251 {
252         switch (strEncrypt)
253         {
254                 case "aes":                             return "AES";
255                 case "tkip":                    return "TKIP";
256                 case "tkip+aes":                return "AES and TKIP";
257                 default:                                        return "";
258         }
259 }
260
261
262 /*
263         Return the WEP key specified by the current index.
264 */
265 function getWEPkey(strWEPkeyIx, strKey1, strKey2, strKey3, strKey4)
266 {
267         if (strWEPkeyIx == "")
268                 return "";
269
270         var ixKey = parseInt(strWEPkeyIx);
271         if ((ixKey >= 1) && (ixKey <= 4))
272         {
273                 var keys = new Array(strKey1, strKey2, strKey3, strKey4);
274                 return keys[ixKey - 1];
275         }
276
277         return "";
278 }
279
280 function getWEPkeyType(strKey)
281 {
282         return _getKeySize(strKey) + "-" + (_isKeyASCII(strKey) ? "ASCII" : "hex");
283 }
284
285 /*
286         Functions to determine key type from the key length
287 */
288
289 function _isKeyASCII(strKey)
290 {
291         var len = strKey.length;
292         return ((len == 5) || (len == 13));
293 }
294 function _isKeyHex(strKey)
295 {
296         var len = strKey.length;
297         return ((len == 10) || (len == 26));
298 }
299
300 function _getKeySize(strKey)
301 {
302         var len = strKey.length;
303         if ((len == 5) || (len == 10))
304                 return 64;
305         if ((len == 13) || (len == 26))
306                 return 128;
307
308         return 0;
309 }
310
311
312
313 /*
314         Only call if WEP.
315
316         This is called when the user changes a sub-type of
317         encryption (WEP ASCII 128-bit, WEP hex 64-bit, ...).
318
319         Type values:
320                 type128ASCII
321                 type128hex
322                 type64ASCII
323                 type64hex
324 */
325 function setMaxLength(bClearValues, strType, eltKeys)
326 {
327         var bHex = ((strType == "type128hex") || (strType == "type64hex"));
328
329         var iBits = 128;
330         if ((strType == "type64ASCII") || (strType == "type64hex"))
331                 iBits = 64;
332
333         var iMaxLength = getKeyLength(iBits, bHex);
334
335         document.getElementById("idKeyNumChars").innerHTML = iMaxLength;
336
337         /*
338                 some page(s) might have only one key field,
339                 instead of an array of them, so check for that
340         */
341         if (eltKeys.length == null)
342         {
343                 eltKeys.maxLength = iMaxLength;
344                 if (bClearValues)
345                         eltKeys.value = "";
346         }
347         else
348         {
349                 for (var i = 0; i < eltKeys.length; ++i)
350                 {
351                         eltKeys[i].maxLength = iMaxLength;
352                         if (bClearValues)
353                                 eltKeys[i].value = "";
354                 }
355         }
356 }
357
358 /*
359                                 ASCII           Hex
360         64-bits          5              10
361         128-bits                13              26
362 */
363 function getKeyLength(bits, bHex)
364 {
365         var len = 0;
366
367         if (bHex)
368         {
369                 // hex
370                 switch (bits)
371                 {
372                         case 64:                len = 10;       break;
373                         case 128:
374                         default:                len = 26;       break;
375                 }
376         }
377         else
378         {
379                 // ASCII
380                 switch (bits)
381                 {
382                         case 64:                len = 5;        break;
383                         case 128:
384                         default:                len = 13;       break;
385                 }
386         }
387
388         return len;
389 }
390
391
392
393 /* --------------------------------------------------------------------------
394
395         General helper functions
396
397 */
398
399 /*
400         This function tests the passed element's value for being inside
401         the passed range--inclusive. If it's not, it displays an error
402         and sets focus to the field...and returns false.
403 */
404 function inRange(strName, elt, min, max)
405 {
406         if ((elt.value == "") || (elt.value < min) || (elt.value > max))
407         {
408                 alert("The " + strName + " setting must be between " + min + " and " + max + ", inclusive.");
409                 elt.focus();
410                 return false;
411         }
412         return true;
413 }
414
415 /*
416         This function tests the passed element's length for being at least the
417         passed number of characters. If it's not, it displays an error and sets
418         focus to the field...and returns false.
419 */
420 function minLength(strName, elt, min)
421 {
422         if (elt.value.length < min)
423         {
424                 alert("The " + strName + " field must have at least " + min + " characters.");
425                 elt.focus();
426                 return false;
427         }
428
429         return true;
430 }
431
432
433 function toggleVisibility(id)
434 {
435         var elt = document.getElementById(id);
436         if ((elt == null) || !elt.style || (elt.style.display == null))
437                 return;
438
439         if (elt.style.display == "none")
440                 elt.style.display = "";
441         else
442                 elt.style.display = "none";
443 }
444
445
446 function setVisibility(id, state)
447 {
448         var elt = document.getElementById(id);
449         if ((elt == null) || !elt.style || (elt.style.display == null))
450                 return;
451
452         if (state)
453                 elt.style.display = "";
454         else
455                 elt.style.display = "none";
456 }
457
458
459 function setEnabled(id, state)
460 {
461         var elt = document.getElementById(id);
462         if ((elt == null) || !elt.style || (elt.style.display == null))
463                 return;
464
465         elt.disabled = !state;
466 }
467
468
469 function validateUsername(elt)
470 {
471         if (elt.value.length == 0)
472         {
473                 alert("Please enter a user name.");
474                 elt.focus();
475                 return false;
476         }
477
478         return isPrintable("User name", elt);
479 }
480
481 function validatePassword(elt)
482 {
483         if (elt.value.length == 0)
484         {
485                 alert("Please enter a password.");
486                 elt.focus();
487                 return false;
488         }
489
490         return isPrintable("Password", elt);
491 }
492
493
494 /*
495         This function determines if the passed string contains only printable characters.
496         It returns true if the string is okay. It displays an error and returns false
497         if the string contains an invalid character.
498
499         95 "printable" characters:
500         <space>!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
501
502         We support ALL "printable" characters in the ASCII and Extended ASCII table.
503         These are character codes       32-126, 128-254, inclusive (decimal), or
504                                                                                 0x20-0x7E, 0x80-0xFE, inclusive (hex).
505         www.lookuptables.com
506 */
507 function isPrintable(strFieldName, elt)
508 {
509         /*
510                 Note: We have to escape characters both for Javascript and for RegExp.
511                                 Thus, " must be escaped ONLY for Javascript: \"
512                                                 \ and [ and ] must be escaped for RegExp and then the
513                                                 escape characters have to be escaped for Javascript:
514                                                 \\\\ and \\[ and \\]
515         */
516         // Kludge to fix Safari regex bug with Unicode escape sequences
517         var re = new RegExp("[^\\x20-\\x7E\\x80-\\xFE]", "g");
518
519         var arMatch = elt.value.match(re);
520         // Most browsers return null; Safari returns an empty match object
521         if ((arMatch == null) || (arMatch.length == 0))
522                 return true;
523
524         var strIllegalChars = "";
525         for (var i = 0; i < arMatch.length; ++i)
526                 strIllegalChars += arMatch[i];
527
528         // There should be some illegal chars at this point, but just in case...
529         if (strIllegalChars == "")
530                 return true;
531
532         // handle KWM Bug
533         var strMsg2 = "\n\nPlease use only a-z, A-Z, 0-9, space, punctuation, or charcters in the extended ASCII set, such as accented characters.";
534         if (arMatch.length == 1)
535                 alert("The " + strFieldName + " entry contains an invalid character: " + strIllegalChars + strMsg2);
536         else
537                 alert("The " + strFieldName + " entry contains invalid characters: " + strIllegalChars + strMsg2);
538
539         elt.focus();
540         return false;
541 }