2 * Portions of this file are based on pieces of Yahoo User Interface Library
3 * Copyright (c) 2007, Yahoo! Inc. All rights reserved.
4 * YUI licensed under the BSD License:
5 * http://developer.yahoo.net/yui/license.txt
6 * <script type="text/javascript">
13 request : function(method, uri, cb, data, options) {
15 var hs = options.headers;
18 if(hs.hasOwnProperty(h)){
19 this.initHeader(h, hs[h], false);
24 this.initHeader('Content-Type', 'text/xml', false);
26 data = options.xmlData;
30 return this.asyncRequest(method, uri, cb, data);
33 serializeForm : function(form) {
34 if(typeof form == 'string') {
35 form = (document.getElementById(form) || document.forms[form]);
38 var el, name, val, disabled, data = '', hasSubmit = false;
39 for (var i = 0; i < form.elements.length; i++) {
40 el = form.elements[i];
41 disabled = form.elements[i].disabled;
42 name = form.elements[i].name;
43 val = form.elements[i].value;
45 if (!disabled && name){
49 case 'select-multiple':
50 for (var j = 0; j < el.options.length; j++) {
51 if (el.options[j].selected) {
53 data += encodeURIComponent(name) + '=' + encodeURIComponent(el.options[j].attributes['value'].specified ? el.options[j].value : el.options[j].text) + '&';
56 data += encodeURIComponent(name) + '=' + encodeURIComponent(el.options[j].hasAttribute('value') ? el.options[j].value : el.options[j].text) + '&';
64 data += encodeURIComponent(name) + '=' + encodeURIComponent(val) + '&';
77 if(hasSubmit == false) {
78 data += encodeURIComponent(name) + '=' + encodeURIComponent(val) + '&';
83 data += encodeURIComponent(name) + '=' + encodeURIComponent(val) + '&';
88 data = data.substr(0, data.length - 1);
96 useDefaultHeader:true,
98 defaultPostHeader:'application/x-www-form-urlencoded',
100 useDefaultXhrHeader:true,
102 defaultXhrHeader:'XMLHttpRequest',
104 hasDefaultHeaders:true,
116 setProgId:function(id)
118 this.activeX.unshift(id);
121 setDefaultPostHeader:function(b)
123 this.useDefaultHeader = b;
126 setDefaultXhrHeader:function(b)
128 this.useDefaultXhrHeader = b;
131 setPollingInterval:function(i)
133 if (typeof i == 'number' && isFinite(i)) {
134 this.pollInterval = i;
138 createXhrObject:function(transactionId)
144 http = new XMLHttpRequest();
146 obj = { conn:http, tId:transactionId };
150 for (var i = 0; i < this.activeX.length; ++i) {
154 http = new ActiveXObject(this.activeX[i]);
156 obj = { conn:http, tId:transactionId };
169 getConnectionObject:function()
172 var tId = this.transactionId;
176 o = this.createXhrObject(tId);
178 this.transactionId++;
189 asyncRequest:function(method, uri, callback, postData)
191 var o = this.getConnectionObject();
197 o.conn.open(method, uri, true);
199 if (this.useDefaultXhrHeader) {
200 if (!this.defaultHeaders['X-Requested-With']) {
201 this.initHeader('X-Requested-With', this.defaultXhrHeader, true);
205 if(postData && this.useDefaultHeader){
206 this.initHeader('Content-Type', this.defaultPostHeader);
209 if (this.hasDefaultHeaders || this.hasHeaders) {
213 this.handleReadyState(o, callback);
214 o.conn.send(postData || null);
220 handleReadyState:function(o, callback)
224 if (callback && callback.timeout) {
225 this.timeout[o.tId] = window.setTimeout(function() {
226 oConn.abort(o, callback, true);
227 }, callback.timeout);
230 this.poll[o.tId] = window.setInterval(
232 if (o.conn && o.conn.readyState == 4) {
233 window.clearInterval(oConn.poll[o.tId]);
234 delete oConn.poll[o.tId];
236 if(callback && callback.timeout) {
237 window.clearTimeout(oConn.timeout[o.tId]);
238 delete oConn.timeout[o.tId];
241 oConn.handleTransactionResponse(o, callback);
244 , this.pollInterval);
247 handleTransactionResponse:function(o, callback, isAbort)
251 this.releaseObject(o);
255 var httpStatus, responseObject;
259 if (o.conn.status !== undefined && o.conn.status != 0) {
260 httpStatus = o.conn.status;
272 if (httpStatus >= 200 && httpStatus < 300) {
273 responseObject = this.createResponseObject(o, callback.argument);
274 if (callback.success) {
275 if (!callback.scope) {
276 callback.success(responseObject);
281 callback.success.apply(callback.scope, [responseObject]);
286 switch (httpStatus) {
294 responseObject = this.createExceptionObject(o.tId, callback.argument, (isAbort ? isAbort : false));
295 if (callback.failure) {
296 if (!callback.scope) {
297 callback.failure(responseObject);
300 callback.failure.apply(callback.scope, [responseObject]);
305 responseObject = this.createResponseObject(o, callback.argument);
306 if (callback.failure) {
307 if (!callback.scope) {
308 callback.failure(responseObject);
311 callback.failure.apply(callback.scope, [responseObject]);
317 this.releaseObject(o);
318 responseObject = null;
321 createResponseObject:function(o, callbackArg)
328 var headerStr = o.conn.getAllResponseHeaders();
329 var header = headerStr.split('\n');
330 for (var i = 0; i < header.length; i++) {
331 var delimitPos = header[i].indexOf(':');
332 if (delimitPos != -1) {
333 headerObj[header[i].substring(0, delimitPos)] = header[i].substring(delimitPos + 2);
341 obj.status = o.conn.status;
342 obj.statusText = o.conn.statusText;
343 obj.getResponseHeader = headerObj;
344 obj.getAllResponseHeaders = headerStr;
345 obj.responseText = o.conn.responseText;
346 obj.responseXML = o.conn.responseXML;
348 if (typeof callbackArg !== undefined) {
349 obj.argument = callbackArg;
355 createExceptionObject:function(tId, callbackArg, isAbort)
358 var COMM_ERROR = 'communication failure';
360 var ABORT_ERROR = 'transaction aborted';
366 obj.status = ABORT_CODE;
367 obj.statusText = ABORT_ERROR;
370 obj.status = COMM_CODE;
371 obj.statusText = COMM_ERROR;
375 obj.argument = callbackArg;
381 initHeader:function(label, value, isDefault)
383 var headerObj = (isDefault) ? this.defaultHeaders : this.headers;
385 if (headerObj[label] === undefined) {
386 headerObj[label] = value;
391 headerObj[label] = value + "," + headerObj[label];
395 this.hasDefaultHeaders = true;
398 this.hasHeaders = true;
403 setHeader:function(o)
405 if (this.hasDefaultHeaders) {
406 for (var prop in this.defaultHeaders) {
407 if (this.defaultHeaders.hasOwnProperty(prop)) {
408 o.conn.setRequestHeader(prop, this.defaultHeaders[prop]);
413 if (this.hasHeaders) {
414 for (var prop in this.headers) {
415 if (this.headers.hasOwnProperty(prop)) {
416 o.conn.setRequestHeader(prop, this.headers[prop]);
420 this.hasHeaders = false;
424 resetDefaultHeaders:function() {
425 delete this.defaultHeaders;
426 this.defaultHeaders = {};
427 this.hasDefaultHeaders = false;
430 abort:function(o, callback, isTimeout)
432 if(this.isCallInProgress(o)) {
434 window.clearInterval(this.poll[o.tId]);
435 delete this.poll[o.tId];
437 delete this.timeout[o.tId];
440 this.handleTransactionResponse(o, callback, true);
450 isCallInProgress:function(o)
453 return o.conn.readyState != 4 && o.conn.readyState != 0;
462 releaseObject:function(o)
471 'MSXML2.XMLHTTP.3.0',