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.
31 function WebForm_Initialize(webForm) {
33 webForm.__pendingCallbacks = new Array();
35 webForm.WebForm_AutoFocus = function (id)
37 var x = this.WebForm_GetElementById (id);
39 if (x && (!this.WebForm_CanFocus(x))) {
40 x = this.WebForm_FindFirstFocusableChild(x);
45 webForm.WebForm_CanFocus = function (element) {
46 if (!element || !(element.tagName) || element.disabled) {
49 if (element.type && element.type.toLowerCase() == "hidden") {
52 var tagName = element.tagName.toLowerCase();
53 return (tagName == "input" ||
54 tagName == "textarea" ||
55 tagName == "select" ||
56 tagName == "button" ||
60 webForm.WebForm_FindFirstFocusableChild = function (element) {
61 if (!element || !(element.tagName)) {
64 var tagName = element.tagName.toLowerCase();
65 if (tagName == "undefined") {
68 var children = element.childNodes;
70 for (var i = 0; i < children.length; i++) {
72 if (this.WebForm_CanFocus(children[i])) {
76 var focused = this.WebForm_FindFirstFocusableChild(children[i]);
77 if (this.WebForm_CanFocus(focused)) {
88 webForm.WebForm_ReEnableControls = function ()
90 if (typeof(this._form.__enabledControlArray) != 'undefined' && this._form.__enabledControlArray != null)
91 __enabledControlArray = this._form.__enabledControlArray;
93 if (typeof(__enabledControlArray) == 'undefined' || __enabledControlArray == null)
96 this._form.__disabledControlArray = new Array();
97 for (var i = 0; i < __enabledControlArray.length; i++) {
98 var c = this.WebForm_GetElementById (__enabledControlArray[i]);
99 if ((typeof(c) != "undefined") && (c != null) && (c.disabled == true)) {
101 this._form.__disabledControlArray[this._form.__disabledControlArray.length] = c;
104 setTimeout((this._instanceVariableName ? this._instanceVariableName + "." : "") + "WebForm_ReDisableControls ()", 0);
108 webForm.WebForm_ReDisableControls = function ()
110 for (var i = 0; i < this._form.__disabledControlArray.length; i++) {
111 this._form.__disabledControlArray[i].disabled = true;
115 webForm.WebForm_DoPostback = function (id, par, url, apb, pval, tf, csubm, vg)
117 var validationResult = true;
118 if (pval && typeof(this.Page_ClientValidate) == "function")
119 validationResult = this.Page_ClientValidate(vg);
121 if (validationResult) {
122 if ((typeof(url) != "undefined") && (url != null) && (url.length > 0))
123 this._form.action = url;
125 var lastFocus = this._form.elements["__LASTFOCUS"];
126 if ((typeof(lastFocus) != "undefined") && (lastFocus != null))
127 lastFocus.value = id;
131 this.__doPostBack (id, par);
134 webForm.WebForm_DoCallback = function (id, arg, callback, ctx, errorCallback, useAsync)
136 var qs = this.WebForm_getFormData () + "__CALLBACKTARGET=" + id + "&__CALLBACKARGUMENT=" + encodeURIComponent(arg);
138 if (this._form["__EVENTVALIDATION"])
139 qs += "&__EVENTVALIDATION=" + encodeURIComponent(this._form["__EVENTVALIDATION"].value);
142 this.WebForm_httpPost (this._form.serverURL || document.URL, qs, function (httpPost) { This.WebForm_ClientCallback (httpPost, ctx, callback, errorCallback); });
145 webForm.WebForm_ClientCallback = function (httpPost, ctx, callback, errorCallback)
147 var doc = httpPost.responseText;
148 if (doc.charAt(0) == "e") {
149 if ((typeof(errorCallback) != "undefined") && (errorCallback != null))
150 errorCallback(doc.substring(1), ctx);
152 var separatorIndex = doc.indexOf("|");
153 if (separatorIndex != -1) {
154 var validationFieldLength = parseInt(doc.substring(0, separatorIndex));
155 if (!isNaN(validationFieldLength)) {
156 var validationField = doc.substring(separatorIndex + 1, separatorIndex + validationFieldLength + 1);
157 if (validationField != "") {
158 var validationFieldElement = this._form["__EVENTVALIDATION"];
159 if (!validationFieldElement) {
160 validationFieldElement = document.createElement("INPUT");
161 validationFieldElement.type = "hidden";
162 validationFieldElement.name = "__EVENTVALIDATION";
163 validationFieldElement.id = validationFieldElement.name;
164 this._form.appendChild(validationFieldElement);
166 validationFieldElement.value = validationField;
168 if ((typeof(callback) != "undefined") && (callback != null))
169 callback (doc.substring(separatorIndex + validationFieldLength + 1), ctx);
172 if ((typeof(callback) != "undefined") && (callback != null))
178 webForm.WebForm_getFormData = function ()
181 var len = this._form.elements.length;
182 for (n=0; n<len; n++) {
183 var elem = this._form.elements [n];
184 var tagName = elem.tagName.toLowerCase();
185 if (tagName == "input") {
186 var type = elem.type;
187 if ((type == "text" || type == "hidden" || type == "password" ||
188 ((type == "checkbox" || type == "radio") && elem.checked)) &&
189 (elem.id != "__EVENTVALIDATION")) {
190 qs += elem.name + "=" + encodeURIComponent (elem.value) + "&";
192 } else if (tagName == "select") {
193 var selectCount = elem.options.length;
194 for (var j = 0; j < selectCount; j++) {
195 var selectChild = elem.options[j];
196 if (selectChild.selected == true) {
197 qs += elem.name + "=" + encodeURIComponent (elem.value) + "&";
201 else if (tagName == "textarea") {
202 qs += elem.name + "=" + encodeURIComponent (elem.value) + "&";
208 webForm.WebForm_httpPost = function (url, data, callback)
212 if (typeof XMLHttpRequest != "undefined") {
213 httpPost = new XMLHttpRequest ();
215 if (this.axName != null)
216 httpPost = new ActiveXObject (this.axName);
218 var clsnames = new Array ("MSXML", "MSXML2", "MSXML3", "Microsoft");
219 for (n = 0; n < clsnames.length && httpPost == null; n++) {
220 this.axName = clsnames [n] + ".XMLHTTP";
222 httpPost = new ActiveXObject (this.axName);
223 } catch (e) { this.axName = null; }
225 if (httpPost == null)
226 throw new Error ("XMLHTTP object could not be created.");
229 httpPost.onreadystatechange = function () { if (httpPost.readyState == 4) callback (httpPost); };
231 httpPost.open ("POST", url, true); // async
232 httpPost.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
233 setTimeout (function () { httpPost.send (data); }, 10);
236 webForm.WebForm_GetElementById = function (id)
238 return document.getElementById ? document.getElementById (id) :
239 document.all ? document.all [id] :
243 webForm.WebForm_FireDefaultButton = function (event, target)
245 if (event.keyCode != 13) {
248 if(event.srcElement && (event.srcElement.tagName.toLowerCase() == "textarea")) {
251 var defaultButton = this.WebForm_GetElementById(target);
255 if (typeof(defaultButton.click) != "undefined") {
256 defaultButton.click();
257 event.cancelBubble = true;
261 if (defaultButton.href && defaultButton.href.match(/^javascript:/i)) {
262 var jsCode = defaultButton.href.match(/^javascript:(.*)/i)[1];
264 event.cancelBubble = true;
271 webForm.WebForm_SaveScrollPositionSubmit = function ()
273 var pos = this.WebForm_GetElementPosition(this._form);
274 this._form.elements['__SCROLLPOSITIONX'].value = this.WebForm_GetScrollX() - pos.x;
275 this._form.elements['__SCROLLPOSITIONY'].value = this.WebForm_GetScrollY() - pos.y;
276 if ((typeof(this._form.oldSubmit) != "undefined") && (this._form.oldSubmit != null)) {
277 return this._form.oldSubmit();
282 webForm.WebForm_SaveScrollPositionOnSubmit = function ()
284 var pos = this.WebForm_GetElementPosition(this._form);
285 this._form.elements['__SCROLLPOSITIONX'].value = this.WebForm_GetScrollX() - pos.x;
286 this._form.elements['__SCROLLPOSITIONY'].value = this.WebForm_GetScrollY() - pos.y;
287 if ((typeof(this._form.oldOnSubmit) != "undefined") && (this._form.oldOnSubmit != null)) {
288 return this._form.oldOnSubmit();
293 webForm.WebForm_RestoreScrollPosition = function ()
295 var pos = this.WebForm_GetElementPosition(this._form);
296 var ScrollX = parseInt(this._form.elements['__SCROLLPOSITIONX'].value);
297 var ScrollY = parseInt(this._form.elements['__SCROLLPOSITIONY'].value);
298 ScrollX = (isNaN(ScrollX)) ? pos.x : (ScrollX + pos.x);
299 ScrollY = (isNaN(ScrollY)) ? pos.y : (ScrollY + pos.y);
300 window.scrollTo(ScrollX, ScrollY);
301 if ((typeof(this._form.oldOnLoad) != "undefined") && (this._form.oldOnLoad != null)) {
302 return this._form.oldOnLoad();
307 webForm.WebForm_GetScrollX = function () {
308 if (window.pageXOffset) {
309 return window.pageXOffset;
311 else if (document.documentElement && document.documentElement.scrollLeft) {
312 return document.documentElement.scrollLeft;
314 else if (document.body) {
315 return document.body.scrollLeft;
320 webForm.WebForm_GetScrollY = function () {
321 if (window.pageYOffset) {
322 return window.pageYOffset;
324 else if (document.documentElement && document.documentElement.scrollTop) {
325 return document.documentElement.scrollTop;
327 else if (document.body) {
328 return document.body.scrollTop;
333 webForm.WebForm_GetElementPosition = function (element)
335 var result = new Object();
340 result.x = element.offsetLeft;
341 result.y = element.offsetTop;
342 var parent = element.offsetParent;
344 result.x += parent.offsetLeft;
345 result.y += parent.offsetTop;
346 parent = parent.offsetParent;
348 result.width = element.offsetWidth;
349 result.height = element.offsetHeight;