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 = webForm.WebForm_getFormData () + "__CALLBACKTARGET=" + id + "&__CALLBACKARGUMENT=" + encodeURIComponent(arg);
138 if (webForm._form["__EVENTVALIDATION"])
139 qs += "&__EVENTVALIDATION=" + encodeURIComponent(webForm._form["__EVENTVALIDATION"].value);
141 webForm.WebForm_httpPost (webForm._form.serverURL || document.URL, qs, function (httpPost) { webForm.WebForm_ClientCallback (httpPost, ctx, callback, errorCallback); });
144 webForm.WebForm_ClientCallback = function (httpPost, ctx, callback, errorCallback)
146 var doc = httpPost.responseText;
147 if (doc.charAt(0) == "e") {
148 if ((typeof(errorCallback) != "undefined") && (errorCallback != null))
149 errorCallback(doc.substring(1), ctx);
151 var separatorIndex = doc.indexOf("|");
152 if (separatorIndex != -1) {
153 var validationFieldLength = parseInt(doc.substring(0, separatorIndex));
154 if (!isNaN(validationFieldLength)) {
155 var validationField = doc.substring(separatorIndex + 1, separatorIndex + validationFieldLength + 1);
156 if (validationField != "") {
157 var validationFieldElement = this._form["__EVENTVALIDATION"];
158 if (!validationFieldElement) {
159 validationFieldElement = document.createElement("INPUT");
160 validationFieldElement.type = "hidden";
161 validationFieldElement.name = "__EVENTVALIDATION";
162 validationFieldElement.id = validationFieldElement.name;
163 this._form.appendChild(validationFieldElement);
165 validationFieldElement.value = validationField;
167 if ((typeof(callback) != "undefined") && (callback != null))
168 callback (doc.substring(separatorIndex + validationFieldLength + 1), ctx);
171 if ((typeof(callback) != "undefined") && (callback != null))
177 webForm.WebForm_getFormData = function ()
180 var len = this._form.elements.length;
181 for (n=0; n<len; n++) {
182 var elem = this._form.elements [n];
183 var tagName = elem.tagName.toLowerCase();
184 if (tagName == "input") {
185 var type = elem.type;
186 if ((type == "text" || type == "hidden" || type == "password" ||
187 ((type == "checkbox" || type == "radio") && elem.checked)) &&
188 (elem.id != "__EVENTVALIDATION")) {
189 qs += elem.name + "=" + encodeURIComponent (elem.value) + "&";
191 } else if (tagName == "select") {
192 var selectCount = elem.options.length;
193 for (var j = 0; j < selectCount; j++) {
194 var selectChild = elem.options[j];
195 if (selectChild.selected == true) {
196 qs += elem.name + "=" + encodeURIComponent (elem.value) + "&";
200 else if (tagName == "textarea") {
201 qs += elem.name + "=" + encodeURIComponent (elem.value) + "&";
207 webForm.WebForm_httpPost = function (url, data, callback)
211 if (typeof XMLHttpRequest != "undefined") {
212 httpPost = new XMLHttpRequest ();
214 if (this.axName != null)
215 httpPost = new ActiveXObject (this.axName);
217 var clsnames = new Array ("MSXML", "MSXML2", "MSXML3", "Microsoft");
218 for (n = 0; n < clsnames.length && httpPost == null; n++) {
219 this.axName = clsnames [n] + ".XMLHTTP";
221 httpPost = new ActiveXObject (this.axName);
222 } catch (e) { this.axName = null; }
224 if (httpPost == null)
225 throw new Error ("XMLHTTP object could not be created.");
228 httpPost.onreadystatechange = function () { if (httpPost.readyState == 4) callback (httpPost); };
230 httpPost.open ("POST", url, true); // async
231 httpPost.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
232 setTimeout (function () { httpPost.send (data); }, 10);
235 webForm.WebForm_GetElementById = function (id)
237 return document.getElementById ? document.getElementById (id) :
238 document.all ? document.all [id] :
242 webForm.WebForm_FireDefaultButton = function (event, target)
244 if (event.keyCode != 13) {
247 if(event.srcElement && (event.srcElement.tagName.toLowerCase() == "textarea")) {
250 var defaultButton = this.WebForm_GetElementById(target);
254 if (typeof(defaultButton.click) != "undefined") {
255 defaultButton.click();
256 event.cancelBubble = true;
260 if (defaultButton.href && defaultButton.href.match(/^javascript:/i)) {
261 var jsCode = defaultButton.href.match(/^javascript:(.*)/i)[1];
263 event.cancelBubble = true;
270 webForm.WebForm_SaveScrollPositionSubmit = function ()
272 var pos = this.WebForm_GetElementPosition(this._form);
273 this._form.elements['__SCROLLPOSITIONX'].value = this.WebForm_GetScrollX() - pos.x;
274 this._form.elements['__SCROLLPOSITIONY'].value = this.WebForm_GetScrollY() - pos.y;
275 if ((typeof(this._form.oldSubmit) != "undefined") && (this._form.oldSubmit != null)) {
276 return this._form.oldSubmit();
281 webForm.WebForm_SaveScrollPositionOnSubmit = function ()
283 var pos = this.WebForm_GetElementPosition(this._form);
284 this._form.elements['__SCROLLPOSITIONX'].value = this.WebForm_GetScrollX() - pos.x;
285 this._form.elements['__SCROLLPOSITIONY'].value = this.WebForm_GetScrollY() - pos.y;
286 if ((typeof(this._form.oldOnSubmit) != "undefined") && (this._form.oldOnSubmit != null)) {
287 return this._form.oldOnSubmit();
292 webForm.WebForm_RestoreScrollPosition = function ()
294 var pos = this.WebForm_GetElementPosition(this._form);
295 var ScrollX = parseInt(this._form.elements['__SCROLLPOSITIONX'].value);
296 var ScrollY = parseInt(this._form.elements['__SCROLLPOSITIONY'].value);
297 ScrollX = (isNaN(ScrollX)) ? pos.x : (ScrollX + pos.x);
298 ScrollY = (isNaN(ScrollY)) ? pos.y : (ScrollY + pos.y);
299 window.scrollTo(ScrollX, ScrollY);
300 if ((typeof(this._form.oldOnLoad) != "undefined") && (this._form.oldOnLoad != null)) {
301 return this._form.oldOnLoad();
306 webForm.WebForm_GetScrollX = function () {
307 if (window.pageXOffset) {
308 return window.pageXOffset;
310 else if (document.documentElement && document.documentElement.scrollLeft) {
311 return document.documentElement.scrollLeft;
313 else if (document.body) {
314 return document.body.scrollLeft;
319 webForm.WebForm_GetScrollY = function () {
320 if (window.pageYOffset) {
321 return window.pageYOffset;
323 else if (document.documentElement && document.documentElement.scrollTop) {
324 return document.documentElement.scrollTop;
326 else if (document.body) {
327 return document.body.scrollTop;
332 webForm.WebForm_GetElementPosition = function (element)
334 var result = new Object();
339 result.x = element.offsetLeft;
340 result.y = element.offsetTop;
341 var parent = element.offsetParent;
343 result.x += parent.offsetLeft;
344 result.y += parent.offsetTop;
345 parent = parent.offsetParent;
347 result.width = element.offsetWidth;
348 result.height = element.offsetHeight;