Add support for multiple fomrs on the client side in case of TARGET_J2EE portal support.
[mono.git] / mcs / class / System.Web / resources / webform.js
1 /*
2  * WebForm.js
3  *
4  * Authors:
5  *   Chris Toshok (toshok@ximian.com)
6  *   Lluis Sanchez Gual (lluis@novell.com)
7  *
8  * (c) 2005 Novell, Inc. (http://www.novell.com)
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining
11  * a copy of this software and associated documentation files (the
12  * "Software"), to deal in the Software without restriction, including
13  * without limitation the rights to use, copy, modify, merge, publish,
14  * distribute, sublicense, and/or sell copies of the Software, and to
15  * permit persons to whom the Software is furnished to do so, subject to
16  * the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28  */
29
30
31 function WebForm_AutoFocus (id)
32 {
33         var x = WebForm_GetElementById (id);
34
35         if (x && (!WebForm_CanFocus(x))) {
36                 x = WebForm_FindFirstFocusableChild(x);
37         }
38         if (x) { x.focus(); }
39 }
40
41 function WebForm_CanFocus(element) {
42         if (!element || !(element.tagName) || element.disabled) {
43                 return false;
44         }
45         if (element.type && element.type.toLowerCase() == "hidden") {
46                 return false;
47         }
48         var tagName = element.tagName.toLowerCase();
49         return (tagName == "input" ||
50                         tagName == "textarea" ||
51                         tagName == "select" ||
52                         tagName == "button" ||
53                         tagName == "a");
54 }
55
56 function WebForm_FindFirstFocusableChild(element) {
57         if (!element || !(element.tagName)) {
58                 return null;
59         }
60         var tagName = element.tagName.toLowerCase();
61         if (tagName == "undefined") {
62                 return null;
63         }
64         var children = element.childNodes;
65         if (children) {
66                 for (var i = 0; i < children.length; i++) {
67                         try {
68                                 if (WebForm_CanFocus(children[i])) {
69                                         return children[i];
70                                 }
71                                 else {
72                                         var focused = WebForm_FindFirstFocusableChild(children[i]);
73                                         if (WebForm_CanFocus(focused)) {
74                                                 return focused;
75                                         }
76                                 }
77                         } catch (e) {
78                         }
79                 }
80         }
81         return null;
82 }
83
84 function wasControlEnabled (id)
85 {
86         if (typeof (__enabledControlArray) == 'undefined')
87                 return false;
88
89         for (var i = 0; i < __enabledControlArray.length; i ++) {
90                 if (id == __enabledControlArray[i])
91                         return true;
92         }
93
94         return false;
95 }
96
97 function WebForm_ReEnableControls (currForm)
98 {
99         currForm = currForm || theForm;
100         if (typeof (currForm) == 'undefined')
101                 return;
102
103         for (var i = 0; i < currForm.childNodes.length; i ++) {
104                 var node = currForm.childNodes[i];
105                 if (node.disabled && wasControlEnabled (node.id))
106                         node.disabled = false;
107         }
108 }
109
110 function WebForm_DoPostback (ctrl, par, url, apb, pval, tf, csubm, vg)
111 {
112         if (pval && typeof(Page_ClientValidate) == "function" && !Page_ClientValidate(vg))
113                 return;
114
115         var form = WebForm_GetFormFromCtrl (ctrl);
116         if (url != null)
117                 form.action = url;
118                 
119         if (csubm)
120                 __doPostBack (ctrl, par);
121 }
122
123 function WebForm_GetFormFromCtrl (id)
124 {
125         // We need to translate the id from ASPX UniqueID to its ClientID.
126         var ctrl = WebForm_GetElementById (id.replace(/:/g, "_"));
127         while (ctrl != null) {
128                 if (ctrl.isAspForm)
129                         return ctrl;
130                 ctrl = ctrl.parentNode;
131         }
132         return theForm;
133 }
134
135 function WebForm_GetElementById (id)
136 {
137         return document.getElementById ? document.getElementById (id) :
138                document.all ? document.all [id] :
139                    document [id];
140 }
141
142 function WebForm_FireDefaultButton(event, target)
143 {
144         if (event.keyCode != 13) {
145                 return true;
146         }
147         if(event.srcElement && (event.srcElement.tagName.toLowerCase() == "textarea")) {
148                 return true;
149         }
150         var defaultButton = WebForm_GetElementById(target);
151         if (defaultButton && typeof(defaultButton.click) != "undefined") {
152                 defaultButton.click();
153                 event.cancelBubble = true;
154                 return false;
155         }
156         return true;
157 }
158
159