www.usr.com/support/gpl/USR9113_release1.0.tar.gz
[bcm963xx.git] / userapps / broadcom / cfm / html / usr_device.js
1 /* --------------------------------------------------------------------------
2
3         (c) 2005-2006 U.S. Robotics Corporation
4
5         -------------------------------------------------------------------------- */
6
7
8 /* --------------------------------------------------------------------------
9
10         SETUP PAGE HEADER
11
12         This outputs the header for a Setup page.
13         The passed number indicates which step this is and how many steps there are.
14
15         <img id="idImgHeaderSetup" src="logo.gif">
16
17         If there are three steps, there will be three different headers:
18
19         <div class="idHeader">
20                 <span class="clsStepDone">Setup</span>
21                 <span class="clsStepCurrent">1</span>
22                 <span class="clsStepNotDone">&hellip; 2 &hellip; 3</span>
23         </div>
24
25         <div class="idHeader">
26                 <span class="clsStepDone">Setup 1 &hellip;</span>
27                 <span class="clsStepCurrent">2</span>
28                 <span class="clsStepNotDone">&hellip; 3</span>
29         </div>
30
31         <div class="idHeader">
32                 <span class="clsStepDone">Setup 1 &hellip; 2 &hellip;</span>
33                 <span class="clsStepCurrent">3</span>
34         </div>
35 */
36 function writeHeaderSetup(iStep, iSteps)
37 {
38         var strSteps = "<span class=\"clsStepDone\">Setup";
39         for (var i = 1; i <= iSteps; ++i)
40         {
41                 if (i == iStep)
42                         strSteps += "<\/span> <span class=\"clsStepCurrent\">" + i + "<\/span><span class=\"clsStepNotDone\">";
43                 else
44                         strSteps += " " + i;
45
46                 if (i < iSteps)
47                         strSteps += " &hellip;";
48         }
49         strSteps += "<\/span>";
50
51         setPageTitle(document.title);
52         _writeHeader(strSteps);
53 }
54
55
56
57 /* --------------------------------------------------------------------------
58
59         GENERAL HELPER FUNCTIONS
60
61 */
62
63 /*
64         This function prompts the user to add a bookmark if the browser's not IE.
65         Example:
66                 <script type="text/javascript">
67                         document.write('<a href="' + document.location.href + '" title="USRobotics Router" rel="sidebar" onclick="bookmark(this); return false;">Bookmark the router</a>');
68                 </script>
69 */
70 function bookmark(anchor)
71 {
72         if ((navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0))
73        window.external.AddFavorite(anchor.getAttribute('href'), anchor.getAttribute('title'));
74 }
75
76
77 function toggleVisibility(id)
78 {
79         var elt = document.getElementById(id);
80         if ((elt == null) || !elt.style || (elt.style.display == null))
81                 return;
82
83         if (elt.style.display == "none")
84                 elt.style.display = "";
85         else
86                 elt.style.display = "none";
87 }
88
89
90 function setVisibility(id, state)
91 {
92         var elt = document.getElementById(id);
93         if ((elt == null) || !elt.style || (elt.style.display == null))
94                 return;
95
96         if (state)
97                 elt.style.display = "";
98         else
99                 elt.style.display = "none";
100 }
101
102
103 function setEnabled(id, state)
104 {
105         var elt = document.getElementById(id);
106         if ((elt == null) || !elt.style || (elt.style.display == null))
107                 return;
108
109         elt.disabled = !state;
110 }
111
112
113
114 /* --------------------------------------------------------------------------
115
116         FUNCTIONS TO HIDE/SHOW AN HTML TAG WHEN A CHECK BOX IS IN A CERTAIN STATE.
117
118         Example:
119
120         initializePage()
121         {
122                 mapChkToRegions.push(new MapChkToRegion(document.forms[0].chkRelay, "idDHCPrelay", true));
123                 setRegionVisibility(null);
124         }
125
126         HTML:
127         <input type="checkbox" name="chkRelay" id="chkRelay" onclick="setRegionVisibility(this);">
128 */
129
130 function MapChkToRegion(eltChk, idRegion, bVisibleState)
131 {
132         this._eltChk = eltChk;
133         this._eltRegion = document.getElementById(idRegion);
134         this._bVisibleState = bVisibleState;
135 }
136
137 var mapChkToRegions = new Array();      // of MapChkToRegion objects
138
139 /*
140         Pass null as the element if you want to set every region.
141         Pass an element if you only want to set the visibility of
142         the region(s) associated with that element.
143 */
144 function setRegionVisibility(eltChk)
145 {
146         for (var i = 0; i < mapChkToRegions.length; ++i)
147         {
148                 if ((eltChk == null) || (eltChk == mapChkToRegions[i]._eltChk))
149                 {
150                         if ((mapChkToRegions[i]._bVisibleState && mapChkToRegions[i]._eltChk.checked) ||
151                                  (!mapChkToRegions[i]._bVisibleState && !mapChkToRegions[i]._eltChk.checked))
152                                 mapChkToRegions[i]._eltRegion.style.display = "";
153                         else
154                                 mapChkToRegions[i]._eltRegion.style.display = "none";
155                 }
156         }
157 }
158
159
160
161 /* --------------------------------------------------------------------------
162
163         DEVICE FUNCTIONS
164
165 */
166
167 /*
168         This function validates that the passed string is a valid format for a
169         MAC address. The format is six hexadecimal bytes delimited by
170         punctuation marks. The hex bytes can be lower or upper case.
171
172         This function returns true if the passed value is a valid MAC format.
173         It returns false if it's invalid and displays an error message.
174
175         Valid examples:
176                 00-AA,BB;CC/DD:EE
177                 00.aa.bb.cc.dd.ee
178 */
179 function validateMAC(mac)
180 {
181         var re = new RegExp("^[\\dA-F]{2}([:\\.\\-,;/][\\dA-F]{2}){5}$", "i");
182         var bOK = (mac.match(re) != null);
183         if (!bOK)
184                 alert("Please enter a valid MAC address.\r\rA MAC address is a set of six bytes separated by colons, periods, dashes, commas, semicolons, or slashes. (A byte consists of two hexadecimal digits: 0-9 and A-F.) For example: \"00:C0:49:AA:22:43.\"");
185
186         return bOK;
187 }
188
189 /*
190         This function converts the MAC address byte delimiters to colons.
191 */
192 function normalizeMAC(mac)
193 {
194         mac = mac.toUpperCase();
195         return mac.replace(/\W/g, ":");
196 }
197
198
199 function validateUsername(elt)
200 {
201         if (elt.value.length == 0)
202         {
203                 alert("Please enter a user name.");
204                 elt.focus();
205                 return false;
206         }
207
208         return isPrintable("User name", elt);
209 }
210
211 function validatePassword(elt)
212 {
213         if (elt.value.length == 0)
214         {
215                 alert("Please enter a password.");
216                 elt.focus();
217                 return false;
218         }
219
220         return isPrintable("Password", elt);
221 }
222
223 /*
224         This function determines if the passed string contains only printable characters.
225         It returns true if the string is okay. It displays an error and returns false
226         if the string contains an invalid character.
227
228         95 "printable" characters:
229         <space>!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
230
231         We support ALL "printable" characters in the ASCII and Extended ASCII table.
232         These are character codes       32-126, 128-254, inclusive (decimal), or
233                                                                                 0x20-0x7E, 0x80-0xFE, inclusive (hex).
234         www.lookuptables.com
235 */
236 function isPrintable(strFieldName, elt)
237 {
238         /*
239                 Note: We have to escape characters both for Javascript and for RegExp.
240                                 Thus, " must be escaped ONLY for Javascript: \"
241                                                 \ and [ and ] must be escaped for RegExp and then the
242                                                 escape characters have to be escaped for Javascript:
243                                                 \\\\ and \\[ and \\]
244         */
245         // Kludge to fix Safari regex bug with Unicode escape sequences
246         var re = new RegExp("[^\\x20-\\x7E\\x80-\\xFE]", "g");
247
248         var arMatch = elt.value.match(re);
249         // Most browsers return null; Safari returns an empty match object
250         if ((arMatch == null) || (arMatch.length == 0))
251                 return true;
252
253         var strIllegalChars = "";
254         for (var i = 0; i < arMatch.length; ++i)        //> This comment is required for some reason.
255                 strIllegalChars += arMatch[i];
256
257         // There should be some illegal chars at this point, but just in case...
258         if (strIllegalChars == "")
259                 return true;
260
261         // handle KWM Bug
262         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.";
263         if (arMatch.length == 1)
264                 alert("The " + strFieldName + " entry contains an invalid character: " + strIllegalChars + strMsg2);
265         else
266                 alert("The " + strFieldName + " entry contains invalid characters: " + strIllegalChars + strMsg2);
267
268         elt.focus();
269         return false;
270 }
271
272
273
274 /* --------------------------------------------------------------------------
275
276         WIRELESS SECURITY FUNCTIONS
277
278 */
279
280 /*
281                                 ASCII           Hex
282         64-bits          5              10
283         128-bits                13              26
284         256-bit         29              58
285
286         Arguments:
287                 bits  the number of bits in the key being generated
288                 hex     flag indicating if the key is hex
289 */
290 function getKeyLength(bits, bHex)
291 {
292         if (bits == 64)
293                 return bHex ? 10 : 5;
294         else if (bits == 128)
295                 return bHex ? 26 : 13;
296         else // 256
297                 return bHex ? 58: 29;
298 }
299
300
301 /*
302         Functions to determine key type from the key length
303 */
304
305 function isKeyASCII(strKey)
306 {
307         var len = strKey.length;
308         return ((len == 5) || (len == 13) || (len == 29));
309 }
310 function isKeyHex(strKey)
311 {
312         var len = strKey.length;
313         return ((len == 10) || (len == 26) || (len == 58));
314 }
315
316
317 /*
318         Returns the number of bits in the passed key.
319 */
320 function getKeySize(strKey)
321 {
322         var len = strKey.length;
323         if ((len == 5) || (len == 10))
324                 return 64;
325         if ((len == 13) || (len == 26))
326                 return 128;
327         if ((len == 29) || (len == 58))
328                 return 256;
329
330         return 0;
331 }