5 * Chris Toshok (toshok@ximian.com)
6 * Lluis Sanchez Gual (lluis@novell.com)
7 * Igor Zelmanovich (igorz@mainsoft.com)
9 * (c) 2005 Novell, Inc. (http://www.novell.com)
11 * Permission is hereby granted, free of charge, to any person obtaining
12 * a copy of this software and associated documentation files (the
13 * "Software"), to deal in the Software without restriction, including
14 * without limitation the rights to use, copy, modify, merge, publish,
15 * distribute, sublicense, and/or sell copies of the Software, and to
16 * permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
19 * The above copyright notice and this permission notice shall be
20 * included in all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 function WebForm_AutoFocus (id)
34 var x = WebForm_GetElementById (id);
36 if (x && (!WebForm_CanFocus(x))) {
37 x = WebForm_FindFirstFocusableChild(x);
42 function WebForm_CanFocus(element) {
43 if (!element || !(element.tagName) || element.disabled) {
46 if (element.type && element.type.toLowerCase() == "hidden") {
49 var tagName = element.tagName.toLowerCase();
50 return (tagName == "input" ||
51 tagName == "textarea" ||
52 tagName == "select" ||
53 tagName == "button" ||
57 function WebForm_FindFirstFocusableChild(element) {
58 if (!element || !(element.tagName)) {
61 var tagName = element.tagName.toLowerCase();
62 if (tagName == "undefined") {
65 var children = element.childNodes;
67 for (var i = 0; i < children.length; i++) {
69 if (WebForm_CanFocus(children[i])) {
73 var focused = WebForm_FindFirstFocusableChild(children[i]);
74 if (WebForm_CanFocus(focused)) {
85 function WebForm_ReEnableControls (currForm)
87 if (typeof(currForm.__enabledControlArray) != 'undefined' && currForm.__enabledControlArray != null)
88 __enabledControlArray = currForm.__enabledControlArray;
90 if (typeof(__enabledControlArray) == 'undefined' || __enabledControlArray == null)
93 currForm.__disabledControlArray = new Array();
94 for (var i = 0; i < __enabledControlArray.length; i++) {
95 var c = WebForm_GetElementById (__enabledControlArray[i]);
96 if ((typeof(c) != "undefined") && (c != null) && (c.disabled == true)) {
98 currForm.__disabledControlArray[currForm.__disabledControlArray.length] = c;
101 __currForm = currForm
102 setTimeout("WebForm_ReDisableControls (__currForm)", 0);
106 function WebForm_ReDisableControls (currForm)
108 for (var i = 0; i < currForm.__disabledControlArray.length; i++) {
109 currForm.__disabledControlArray[i].disabled = true;
113 // This function is only used in the context of TARGET_J2EE for portlets
114 function PortalWebForm_DoPostback (id, par, url, apb, pval, tf, csubm, vg, currForm)
117 currForm = WebForm_GetFormFromCtrl (id, currForm);
118 if (url.indexOf ("vmw.action.page=") == 0) {
119 currForm.__NEXTVMWACTIONPAGE.value = url.substring ("vmw.action.page=".length);
120 url = currForm.action;
122 else if (url.indexOf ("vmw.render.page=") == 0)
124 currForm.__NEXTVMWRENDERPAGE.value = url.substring ("vmw.render.page=".length);
129 return WebForm_DoPostback (id, par, url, apb, pval, tf, csubm, vg, currForm);
131 function WebForm_DoPostback (id, par, url, apb, pval, tf, csubm, vg, currForm)
133 currForm = WebForm_GetFormFromCtrl (id, currForm);
135 if (typeof(SetValidatorContext) == "function")
136 SetValidatorContext (currForm);
138 var validationResult = true;
139 if (pval && typeof(Page_ClientValidate) == "function")
140 validationResult = Page_ClientValidate(vg);
142 if (validationResult) {
143 if ((typeof(url) != "undefined") && (url != null) && (url.length > 0))
144 currForm.action = url;
146 var lastFocus = currForm.elements["__LASTFOCUS"];
147 if ((typeof(lastFocus) != "undefined") && (lastFocus != null))
148 lastFocus.value = id;
152 currForm.__doPostBack (id, par);
155 function WebForm_DoCallback (id, arg, callback, ctx, errorCallback, useAsync, currForm)
157 currForm = WebForm_GetFormFromCtrl (id, currForm);
158 var qs = WebForm_getFormData (currForm) + "__CALLBACKTARGET=" + id + "&__CALLBACKARGUMENT=" + encodeURIComponent(arg);
160 if (currForm["__EVENTVALIDATION"])
161 qs += "&__EVENTVALIDATION=" + encodeURIComponent(currForm["__EVENTVALIDATION"].value);
163 WebForm_httpPost (currForm.serverURL || document.URL, qs, function (httpPost) { WebForm_ClientCallback (httpPost, ctx, callback, errorCallback, currForm); });
166 function WebForm_ClientCallback (httpPost, ctx, callback, errorCallback, currForm)
168 var doc = httpPost.responseText;
169 if (doc.charAt(0) == "e") {
170 if ((typeof(errorCallback) != "undefined") && (errorCallback != null))
171 errorCallback(doc.substring(1), ctx);
173 var separatorIndex = doc.indexOf("|");
174 if (separatorIndex != -1) {
175 var validationFieldLength = parseInt(doc.substring(0, separatorIndex));
176 if (!isNaN(validationFieldLength)) {
177 var validationField = doc.substring(separatorIndex + 1, separatorIndex + validationFieldLength + 1);
178 if (validationField != "") {
179 var validationFieldElement = currForm["__EVENTVALIDATION"];
180 if (!validationFieldElement) {
181 validationFieldElement = document.createElement("INPUT");
182 validationFieldElement.type = "hidden";
183 validationFieldElement.name = "__EVENTVALIDATION";
184 validationFieldElement.id = validationFieldElement.name;
185 currForm.appendChild(validationFieldElement);
187 validationFieldElement.value = validationField;
189 if ((typeof(callback) != "undefined") && (callback != null))
190 callback (doc.substring(separatorIndex + validationFieldLength + 1), ctx);
193 if ((typeof(callback) != "undefined") && (callback != null))
199 function WebForm_getFormData (currForm)
202 var len = currForm.elements.length;
203 for (n=0; n<len; n++) {
204 var elem = currForm.elements [n];
205 var tagName = elem.tagName.toLowerCase();
206 if (tagName == "input") {
207 var type = elem.type;
208 if ((type == "text" || type == "hidden" || type == "password" ||
209 ((type == "checkbox" || type == "radio") && elem.checked)) &&
210 (elem.id != "__EVENTVALIDATION")) {
211 qs += elem.name + "=" + encodeURIComponent (elem.value) + "&";
213 } else if (tagName == "select") {
214 var selectCount = elem.options.length;
215 for (var j = 0; j < selectCount; j++) {
216 var selectChild = elem.options[j];
217 if (selectChild.selected == true) {
218 qs += elem.name + "=" + encodeURIComponent (elem.value) + "&";
222 else if (tagName == "textarea") {
223 qs += elem.name + "=" + encodeURIComponent (elem.value) + "&";
230 function WebForm_httpPost (url, data, callback)
234 if (typeof XMLHttpRequest != "undefined") {
235 httpPost = new XMLHttpRequest ();
238 httpPost = new ActiveXObject (axName);
240 var clsnames = new Array ("MSXML", "MSXML2", "MSXML3", "Microsoft");
241 for (n = 0; n < clsnames.length && httpPost == null; n++) {
242 axName = clsnames [n] + ".XMLHTTP";
244 httpPost = new ActiveXObject (axName);
245 } catch (e) { axName = null; }
247 if (httpPost == null)
248 throw new Error ("XMLHTTP object could not be created.");
251 httpPost.onreadystatechange = function () { if (httpPost.readyState == 4) callback (httpPost); };
253 httpPost.open ("POST", url, true); // async
254 httpPost.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
255 setTimeout (function () { httpPost.send (data); }, 10);
258 function WebForm_GetFormFromCtrl (id, currForm)
263 // We need to translate the id from ASPX UniqueID to its ClientID.
264 var ctrl = WebForm_GetElementById (id.replace(/\$/g, "_"));
265 while (ctrl != null) {
268 ctrl = ctrl.parentNode;
273 function WebForm_GetElementById (id)
275 return document.getElementById ? document.getElementById (id) :
276 document.all ? document.all [id] :
280 function WebForm_FireDefaultButton(event, target)
282 if (event.keyCode != 13) {
285 if(event.srcElement && (event.srcElement.tagName.toLowerCase() == "textarea")) {
288 var defaultButton = WebForm_GetElementById(target);
289 if (defaultButton && typeof(defaultButton.click) != "undefined") {
290 defaultButton.click();
291 event.cancelBubble = true;
297 function WebForm_SaveScrollPositionSubmit()
299 var pos = WebForm_GetElementPosition(this);
300 this.elements['__SCROLLPOSITIONX'].value = WebForm_GetScrollX() - pos.x;
301 this.elements['__SCROLLPOSITIONY'].value = WebForm_GetScrollY() - pos.y;
302 if ((typeof(this.oldSubmit) != "undefined") && (this.oldSubmit != null)) {
303 return this.oldSubmit();
308 function WebForm_SaveScrollPositionOnSubmit()
310 var pos = WebForm_GetElementPosition(this);
311 this.elements['__SCROLLPOSITIONX'].value = WebForm_GetScrollX() - pos.x;
312 this.elements['__SCROLLPOSITIONY'].value = WebForm_GetScrollY() - pos.y;
313 if ((typeof(this.oldOnSubmit) != "undefined") && (this.oldOnSubmit != null)) {
314 return this.oldOnSubmit();
319 function WebForm_RestoreScrollPosition(currForm)
321 currForm = currForm || theForm;
322 var pos = WebForm_GetElementPosition(currForm);
323 var ScrollX = parseInt(currForm.elements['__SCROLLPOSITIONX'].value);
324 var ScrollY = parseInt(currForm.elements['__SCROLLPOSITIONY'].value);
325 ScrollX = (isNaN(ScrollX)) ? pos.x : (ScrollX + pos.x);
326 ScrollY = (isNaN(ScrollY)) ? pos.y : (ScrollY + pos.y);
327 window.scrollTo(ScrollX, ScrollY);
328 if ((typeof(currForm.oldOnLoad) != "undefined") && (currForm.oldOnLoad != null)) {
329 return currForm.oldOnLoad();
334 function WebForm_GetScrollX() {
335 if (window.pageXOffset) {
336 return window.pageXOffset;
338 else if (document.documentElement && document.documentElement.scrollLeft) {
339 return document.documentElement.scrollLeft;
341 else if (document.body) {
342 return document.body.scrollLeft;
347 function WebForm_GetScrollY() {
348 if (window.pageYOffset) {
349 return window.pageYOffset;
351 else if (document.documentElement && document.documentElement.scrollTop) {
352 return document.documentElement.scrollTop;
354 else if (document.body) {
355 return document.body.scrollTop;
360 function WebForm_GetElementPosition(element)
362 var result = new Object();
367 result.x = element.offsetLeft;
368 result.y = element.offsetTop;
369 var parent = element.offsetParent;
371 result.x += parent.offsetLeft;
372 result.y += parent.offsetTop;
373 parent = parent.offsetParent;
375 result.width = element.offsetWidth;
376 result.height = element.offsetHeight;