]> ToastFreeware Gitweb - philipp/winterrodeln/wradmin.git/blob - wradmin/public/yui/connection/connection-debug.js
Additional cleanup.
[philipp/winterrodeln/wradmin.git] / wradmin / public / yui / connection / connection-debug.js
1 /*
2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
5 version: 2.7.0
6 */
7 /**
8  * The Connection Manager provides a simplified interface to the XMLHttpRequest
9  * object.  It handles cross-browser instantiantion of XMLHttpRequest, negotiates the
10  * interactive states and server response, returning the results to a pre-defined
11  * callback you create.
12  *
13  * @namespace YAHOO.util
14  * @module connection
15  * @requires yahoo
16  * @requires event
17  */
18
19 /**
20  * The Connection Manager singleton provides methods for creating and managing
21  * asynchronous transactions.
22  *
23  * @class Connect
24  */
25
26 YAHOO.util.Connect =
27 {
28   /**
29    * @description Array of MSFT ActiveX ids for XMLHttpRequest.
30    * @property _msxml_progid
31    * @private
32    * @static
33    * @type array
34    */
35         _msxml_progid:[
36                 'Microsoft.XMLHTTP',
37                 'MSXML2.XMLHTTP.3.0',
38                 'MSXML2.XMLHTTP'
39                 ],
40
41   /**
42    * @description Object literal of HTTP header(s)
43    * @property _http_header
44    * @private
45    * @static
46    * @type object
47    */
48         _http_headers:{},
49
50   /**
51    * @description Determines if HTTP headers are set.
52    * @property _has_http_headers
53    * @private
54    * @static
55    * @type boolean
56    */
57         _has_http_headers:false,
58
59  /**
60   * @description Determines if a default header of
61   * Content-Type of 'application/x-www-form-urlencoded'
62   * will be added to any client HTTP headers sent for POST
63   * transactions.
64   * @property _use_default_post_header
65   * @private
66   * @static
67   * @type boolean
68   */
69     _use_default_post_header:true,
70
71  /**
72   * @description The default header used for POST transactions.
73   * @property _default_post_header
74   * @private
75   * @static
76   * @type boolean
77   */
78     _default_post_header:'application/x-www-form-urlencoded; charset=UTF-8',
79
80  /**
81   * @description The default header used for transactions involving the
82   * use of HTML forms.
83   * @property _default_form_header
84   * @private
85   * @static
86   * @type boolean
87   */
88     _default_form_header:'application/x-www-form-urlencoded',
89
90  /**
91   * @description Determines if a default header of
92   * 'X-Requested-With: XMLHttpRequest'
93   * will be added to each transaction.
94   * @property _use_default_xhr_header
95   * @private
96   * @static
97   * @type boolean
98   */
99     _use_default_xhr_header:true,
100
101  /**
102   * @description The default header value for the label
103   * "X-Requested-With".  This is sent with each
104   * transaction, by default, to identify the
105   * request as being made by YUI Connection Manager.
106   * @property _default_xhr_header
107   * @private
108   * @static
109   * @type boolean
110   */
111     _default_xhr_header:'XMLHttpRequest',
112
113  /**
114   * @description Determines if custom, default headers
115   * are set for each transaction.
116   * @property _has_default_header
117   * @private
118   * @static
119   * @type boolean
120   */
121     _has_default_headers:true,
122
123  /**
124   * @description Determines if custom, default headers
125   * are set for each transaction.
126   * @property _has_default_header
127   * @private
128   * @static
129   * @type boolean
130   */
131     _default_headers:{},
132
133  /**
134   * @description Property modified by setForm() to determine if the data
135   * should be submitted as an HTML form.
136   * @property _isFormSubmit
137   * @private
138   * @static
139   * @type boolean
140   */
141     _isFormSubmit:false,
142
143  /**
144   * @description Property modified by setForm() to determine if a file(s)
145   * upload is expected.
146   * @property _isFileUpload
147   * @private
148   * @static
149   * @type boolean
150   */
151     _isFileUpload:false,
152
153  /**
154   * @description Property modified by setForm() to set a reference to the HTML
155   * form node if the desired action is file upload.
156   * @property _formNode
157   * @private
158   * @static
159   * @type object
160   */
161     _formNode:null,
162
163  /**
164   * @description Property modified by setForm() to set the HTML form data
165   * for each transaction.
166   * @property _sFormData
167   * @private
168   * @static
169   * @type string
170   */
171     _sFormData:null,
172
173  /**
174   * @description Collection of polling references to the polling mechanism in handleReadyState.
175   * @property _poll
176   * @private
177   * @static
178   * @type object
179   */
180     _poll:{},
181
182  /**
183   * @description Queue of timeout values for each transaction callback with a defined timeout value.
184   * @property _timeOut
185   * @private
186   * @static
187   * @type object
188   */
189     _timeOut:{},
190
191   /**
192    * @description The polling frequency, in milliseconds, for HandleReadyState.
193    * when attempting to determine a transaction's XHR readyState.
194    * The default is 50 milliseconds.
195    * @property _polling_interval
196    * @private
197    * @static
198    * @type int
199    */
200      _polling_interval:50,
201
202   /**
203    * @description A transaction counter that increments the transaction id for each transaction.
204    * @property _transaction_id
205    * @private
206    * @static
207    * @type int
208    */
209      _transaction_id:0,
210
211   /**
212    * @description Tracks the name-value pair of the "clicked" submit button if multiple submit
213    * buttons are present in an HTML form; and, if YAHOO.util.Event is available.
214    * @property _submitElementValue
215    * @private
216    * @static
217    * @type string
218    */
219          _submitElementValue:null,
220
221   /**
222    * @description Determines whether YAHOO.util.Event is available and returns true or false.
223    * If true, an event listener is bound at the document level to trap click events that
224    * resolve to a target type of "Submit".  This listener will enable setForm() to determine
225    * the clicked "Submit" value in a multi-Submit button, HTML form.
226    * @property _hasSubmitListener
227    * @private
228    * @static
229    */
230          _hasSubmitListener:(function()
231          {
232                 if(YAHOO.util.Event){
233                         YAHOO.util.Event.addListener(
234                                 document,
235                                 'click',
236                                 function(e){
237                                         var obj = YAHOO.util.Event.getTarget(e),
238                                                 name = obj.nodeName.toLowerCase();
239                                         if((name === 'input' || name === 'button') && (obj.type && obj.type.toLowerCase() == 'submit')){
240                                                 YAHOO.util.Connect._submitElementValue = encodeURIComponent(obj.name) + "=" + encodeURIComponent(obj.value);
241                                         }
242                                 });
243                         return true;
244             }
245             return false;
246          })(),
247
248   /**
249    * @description Custom event that fires at the start of a transaction
250    * @property startEvent
251    * @private
252    * @static
253    * @type CustomEvent
254    */
255         startEvent: new YAHOO.util.CustomEvent('start'),
256
257   /**
258    * @description Custom event that fires when a transaction response has completed.
259    * @property completeEvent
260    * @private
261    * @static
262    * @type CustomEvent
263    */
264         completeEvent: new YAHOO.util.CustomEvent('complete'),
265
266   /**
267    * @description Custom event that fires when handleTransactionResponse() determines a
268    * response in the HTTP 2xx range.
269    * @property successEvent
270    * @private
271    * @static
272    * @type CustomEvent
273    */
274         successEvent: new YAHOO.util.CustomEvent('success'),
275
276   /**
277    * @description Custom event that fires when handleTransactionResponse() determines a
278    * response in the HTTP 4xx/5xx range.
279    * @property failureEvent
280    * @private
281    * @static
282    * @type CustomEvent
283    */
284         failureEvent: new YAHOO.util.CustomEvent('failure'),
285
286   /**
287    * @description Custom event that fires when handleTransactionResponse() determines a
288    * response in the HTTP 4xx/5xx range.
289    * @property failureEvent
290    * @private
291    * @static
292    * @type CustomEvent
293    */
294         uploadEvent: new YAHOO.util.CustomEvent('upload'),
295
296   /**
297    * @description Custom event that fires when a transaction is successfully aborted.
298    * @property abortEvent
299    * @private
300    * @static
301    * @type CustomEvent
302    */
303         abortEvent: new YAHOO.util.CustomEvent('abort'),
304
305   /**
306    * @description A reference table that maps callback custom events members to its specific
307    * event name.
308    * @property _customEvents
309    * @private
310    * @static
311    * @type object
312    */
313         _customEvents:
314         {
315                 onStart:['startEvent', 'start'],
316                 onComplete:['completeEvent', 'complete'],
317                 onSuccess:['successEvent', 'success'],
318                 onFailure:['failureEvent', 'failure'],
319                 onUpload:['uploadEvent', 'upload'],
320                 onAbort:['abortEvent', 'abort']
321         },
322
323   /**
324    * @description Member to add an ActiveX id to the existing xml_progid array.
325    * In the event(unlikely) a new ActiveX id is introduced, it can be added
326    * without internal code modifications.
327    * @method setProgId
328    * @public
329    * @static
330    * @param {string} id The ActiveX id to be added to initialize the XHR object.
331    * @return void
332    */
333         setProgId:function(id)
334         {
335                 this._msxml_progid.unshift(id);
336                 YAHOO.log('ActiveX Program Id  ' + id + ' added to _msxml_progid.', 'info', 'Connection');
337         },
338
339   /**
340    * @description Member to override the default POST header.
341    * @method setDefaultPostHeader
342    * @public
343    * @static
344    * @param {boolean} b Set and use default header - true or false .
345    * @return void
346    */
347         setDefaultPostHeader:function(b)
348         {
349                 if(typeof b == 'string'){
350                         this._default_post_header = b;
351                         YAHOO.log('Default POST header set to  ' + b, 'info', 'Connection');
352                 }
353                 else if(typeof b == 'boolean'){
354                         this._use_default_post_header = b;
355                 }
356         },
357
358   /**
359    * @description Member to override the default transaction header..
360    * @method setDefaultXhrHeader
361    * @public
362    * @static
363    * @param {boolean} b Set and use default header - true or false .
364    * @return void
365    */
366         setDefaultXhrHeader:function(b)
367         {
368                 if(typeof b == 'string'){
369                         this._default_xhr_header = b;
370                         YAHOO.log('Default XHR header set to  ' + b, 'info', 'Connection');
371                 }
372                 else{
373                         this._use_default_xhr_header = b;
374                 }
375         },
376
377   /**
378    * @description Member to modify the default polling interval.
379    * @method setPollingInterval
380    * @public
381    * @static
382    * @param {int} i The polling interval in milliseconds.
383    * @return void
384    */
385         setPollingInterval:function(i)
386         {
387                 if(typeof i == 'number' && isFinite(i)){
388                         this._polling_interval = i;
389                         YAHOO.log('Default polling interval set to ' + i +'ms', 'info', 'Connection');
390                 }
391         },
392
393   /**
394    * @description Instantiates a XMLHttpRequest object and returns an object with two properties:
395    * the XMLHttpRequest instance and the transaction id.
396    * @method createXhrObject
397    * @private
398    * @static
399    * @param {int} transactionId Property containing the transaction id for this transaction.
400    * @return object
401    */
402         createXhrObject:function(transactionId)
403         {
404                 var obj,http;
405                 try
406                 {
407                         // Instantiates XMLHttpRequest in non-IE browsers and assigns to http.
408                         http = new XMLHttpRequest();
409                         //  Object literal with http and tId properties
410                         obj = { conn:http, tId:transactionId };
411                         YAHOO.log('XHR object created for transaction ' + transactionId, 'info', 'Connection');
412                 }
413                 catch(e)
414                 {
415                         for(var i=0; i<this._msxml_progid.length; ++i){
416                                 try
417                                 {
418                                         // Instantiates XMLHttpRequest for IE and assign to http
419                                         http = new ActiveXObject(this._msxml_progid[i]);
420                                         //  Object literal with conn and tId properties
421                                         obj = { conn:http, tId:transactionId };
422                                         YAHOO.log('ActiveX XHR object created for transaction ' + transactionId, 'info', 'Connection');
423                                         break;
424                                 }
425                                 catch(e2){}
426                         }
427                 }
428                 finally
429                 {
430                         return obj;
431                 }
432         },
433
434   /**
435    * @description This method is called by asyncRequest to create a
436    * valid connection object for the transaction.  It also passes a
437    * transaction id and increments the transaction id counter.
438    * @method getConnectionObject
439    * @private
440    * @static
441    * @return {object}
442    */
443         getConnectionObject:function(isFileUpload)
444         {
445                 var o;
446                 var tId = this._transaction_id;
447
448                 try
449                 {
450                         if(!isFileUpload){
451                                 o = this.createXhrObject(tId);
452                         }
453                         else{
454                                 o = {};
455                                 o.tId = tId;
456                                 o.isUpload = true;
457                         }
458
459                         if(o){
460                                 this._transaction_id++;
461                         }
462                 }
463                 catch(e){}
464                 finally
465                 {
466                         return o;
467                 }
468         },
469
470   /**
471    * @description Method for initiating an asynchronous request via the XHR object.
472    * @method asyncRequest
473    * @public
474    * @static
475    * @param {string} method HTTP transaction method
476    * @param {string} uri Fully qualified path of resource
477    * @param {callback} callback User-defined callback function or object
478    * @param {string} postData POST body
479    * @return {object} Returns the connection object
480    */
481         asyncRequest:function(method, uri, callback, postData)
482         {
483                 var o = (this._isFileUpload)?this.getConnectionObject(true):this.getConnectionObject();
484                 var args = (callback && callback.argument)?callback.argument:null;
485
486                 if(!o){
487                         YAHOO.log('Unable to create connection object.', 'error', 'Connection');
488                         return null;
489                 }
490                 else{
491
492                         // Intialize any transaction-specific custom events, if provided.
493                         if(callback && callback.customevents){
494                                 this.initCustomEvents(o, callback);
495                         }
496
497                         if(this._isFormSubmit){
498                                 if(this._isFileUpload){
499                                         this.uploadFile(o, callback, uri, postData);
500                                         return o;
501                                 }
502
503                                 // If the specified HTTP method is GET, setForm() will return an
504                                 // encoded string that is concatenated to the uri to
505                                 // create a querystring.
506                                 if(method.toUpperCase() == 'GET'){
507                                         if(this._sFormData.length !== 0){
508                                                 // If the URI already contains a querystring, append an ampersand
509                                                 // and then concatenate _sFormData to the URI.
510                                                 uri += ((uri.indexOf('?') == -1)?'?':'&') + this._sFormData;
511                                         }
512                                 }
513                                 else if(method.toUpperCase() == 'POST'){
514                                         // If POST data exist in addition to the HTML form data,
515                                         // it will be concatenated to the form data.
516                                         postData = postData?this._sFormData + "&" + postData:this._sFormData;
517                                 }
518                         }
519
520                         if(method.toUpperCase() == 'GET' && (callback && callback.cache === false)){
521                                 // If callback.cache is defined and set to false, a
522                                 // timestamp value will be added to the querystring.
523                                 uri += ((uri.indexOf('?') == -1)?'?':'&') + "rnd=" + new Date().valueOf().toString();
524                         }
525
526                         o.conn.open(method, uri, true);
527
528                         // Each transaction will automatically include a custom header of
529                         // "X-Requested-With: XMLHttpRequest" to identify the request as
530                         // having originated from Connection Manager.
531                         if(this._use_default_xhr_header){
532                                 if(!this._default_headers['X-Requested-With']){
533                                         this.initHeader('X-Requested-With', this._default_xhr_header, true);
534                                         YAHOO.log('Initialize transaction header X-Request-Header to XMLHttpRequest.', 'info', 'Connection');
535                                 }
536                         }
537
538                         //If the transaction method is POST and the POST header value is set to true
539                         //or a custom value, initalize the Content-Type header to this value.
540                         if((method.toUpperCase() === 'POST' && this._use_default_post_header) && this._isFormSubmit === false){
541                                 this.initHeader('Content-Type', this._default_post_header);
542                                 YAHOO.log('Initialize header Content-Type to application/x-www-form-urlencoded; UTF-8 for POST transaction.', 'info', 'Connection');
543                         }
544
545                         //Initialize all default and custom HTTP headers,
546                         if(this._has_default_headers || this._has_http_headers){
547                                 this.setHeader(o);
548                         }
549
550                         this.handleReadyState(o, callback);
551                         o.conn.send(postData || '');
552                         YAHOO.log('Transaction ' + o.tId + ' sent.', 'info', 'Connection');
553
554
555                         // Reset the HTML form data and state properties as
556                         // soon as the data are submitted.
557                         if(this._isFormSubmit === true){
558                                 this.resetFormState();
559                         }
560
561                         // Fire global custom event -- startEvent
562                         this.startEvent.fire(o, args);
563
564                         if(o.startEvent){
565                                 // Fire transaction custom event -- startEvent
566                                 o.startEvent.fire(o, args);
567                         }
568
569                         return o;
570                 }
571         },
572
573   /**
574    * @description This method creates and subscribes custom events,
575    * specific to each transaction
576    * @method initCustomEvents
577    * @private
578    * @static
579    * @param {object} o The connection object
580    * @param {callback} callback The user-defined callback object
581    * @return {void}
582    */
583         initCustomEvents:function(o, callback)
584         {
585                 var prop;
586                 // Enumerate through callback.customevents members and bind/subscribe
587                 // events that match in the _customEvents table.
588                 for(prop in callback.customevents){
589                         if(this._customEvents[prop][0]){
590                                 // Create the custom event
591                                 o[this._customEvents[prop][0]] = new YAHOO.util.CustomEvent(this._customEvents[prop][1], (callback.scope)?callback.scope:null);
592                                 YAHOO.log('Transaction-specific Custom Event ' + o[this._customEvents[prop][1]] + ' created.', 'info', 'Connection');
593
594                                 // Subscribe the custom event
595                                 o[this._customEvents[prop][0]].subscribe(callback.customevents[prop]);
596                                 YAHOO.log('Transaction-specific Custom Event ' + o[this._customEvents[prop][1]] + ' subscribed.', 'info', 'Connection');
597                         }
598                 }
599         },
600
601   /**
602    * @description This method serves as a timer that polls the XHR object's readyState
603    * property during a transaction, instead of binding a callback to the
604    * onreadystatechange event.  Upon readyState 4, handleTransactionResponse
605    * will process the response, and the timer will be cleared.
606    * @method handleReadyState
607    * @private
608    * @static
609    * @param {object} o The connection object
610    * @param {callback} callback The user-defined callback object
611    * @return {void}
612    */
613
614     handleReadyState:function(o, callback)
615
616     {
617                 var oConn = this;
618                 var args = (callback && callback.argument)?callback.argument:null;
619
620                 if(callback && callback.timeout){
621                         this._timeOut[o.tId] = window.setTimeout(function(){ oConn.abort(o, callback, true); }, callback.timeout);
622                 }
623
624                 this._poll[o.tId] = window.setInterval(
625                         function(){
626                                 if(o.conn && o.conn.readyState === 4){
627
628                                         // Clear the polling interval for the transaction
629                                         // and remove the reference from _poll.
630                                         window.clearInterval(oConn._poll[o.tId]);
631                                         delete oConn._poll[o.tId];
632
633                                         if(callback && callback.timeout){
634                                                 window.clearTimeout(oConn._timeOut[o.tId]);
635                                                 delete oConn._timeOut[o.tId];
636                                         }
637
638                                         // Fire global custom event -- completeEvent
639                                         oConn.completeEvent.fire(o, args);
640
641                                         if(o.completeEvent){
642                                                 // Fire transaction custom event -- completeEvent
643                                                 o.completeEvent.fire(o, args);
644                                         }
645
646                                         oConn.handleTransactionResponse(o, callback);
647                                 }
648                         }
649                 ,this._polling_interval);
650     },
651
652   /**
653    * @description This method attempts to interpret the server response and
654    * determine whether the transaction was successful, or if an error or
655    * exception was encountered.
656    * @method handleTransactionResponse
657    * @private
658    * @static
659    * @param {object} o The connection object
660    * @param {object} callback The user-defined callback object
661    * @param {boolean} isAbort Determines if the transaction was terminated via abort().
662    * @return {void}
663    */
664     handleTransactionResponse:function(o, callback, isAbort)
665     {
666                 var httpStatus, responseObject;
667                 var args = (callback && callback.argument)?callback.argument:null;
668
669                 try
670                 {
671                         if(o.conn.status !== undefined && o.conn.status !== 0){
672                                 httpStatus = o.conn.status;
673                         }
674                         else{
675                                 httpStatus = 13030;
676                         }
677                 }
678                 catch(e){
679
680                          // 13030 is a custom code to indicate the condition -- in Mozilla/FF --
681                          // when the XHR object's status and statusText properties are
682                          // unavailable, and a query attempt throws an exception.
683                         httpStatus = 13030;
684                 }
685
686                 if(httpStatus >= 200 && httpStatus < 300 || httpStatus === 1223){
687                         responseObject = this.createResponseObject(o, args);
688                         if(callback && callback.success){
689                                 if(!callback.scope){
690                                         callback.success(responseObject);
691                                         YAHOO.log('Success callback. HTTP code is ' + httpStatus, 'info', 'Connection');
692                                 }
693                                 else{
694                                         // If a scope property is defined, the callback will be fired from
695                                         // the context of the object.
696                                         callback.success.apply(callback.scope, [responseObject]);
697                                         YAHOO.log('Success callback with scope. HTTP code is ' + httpStatus, 'info', 'Connection');
698                                 }
699                         }
700
701                         // Fire global custom event -- successEvent
702                         this.successEvent.fire(responseObject);
703
704                         if(o.successEvent){
705                                 // Fire transaction custom event -- successEvent
706                                 o.successEvent.fire(responseObject);
707                         }
708                 }
709                 else{
710                         switch(httpStatus){
711                                 // The following cases are wininet.dll error codes that may be encountered.
712                                 case 12002: // Server timeout
713                                 case 12029: // 12029 to 12031 correspond to dropped connections.
714                                 case 12030:
715                                 case 12031:
716                                 case 12152: // Connection closed by server.
717                                 case 13030: // See above comments for variable status.
718                                         responseObject = this.createExceptionObject(o.tId, args, (isAbort?isAbort:false));
719                                         if(callback && callback.failure){
720                                                 if(!callback.scope){
721                                                         callback.failure(responseObject);
722                                                         YAHOO.log('Failure callback. Exception detected. Status code is ' + httpStatus, 'warn', 'Connection');
723                                                 }
724                                                 else{
725                                                         callback.failure.apply(callback.scope, [responseObject]);
726                                                         YAHOO.log('Failure callback with scope. Exception detected. Status code is ' + httpStatus, 'warn', 'Connection');
727                                                 }
728                                         }
729
730                                         break;
731                                 default:
732                                         responseObject = this.createResponseObject(o, args);
733                                         if(callback && callback.failure){
734                                                 if(!callback.scope){
735                                                         callback.failure(responseObject);
736                                                         YAHOO.log('Failure callback. HTTP status code is ' + httpStatus, 'warn', 'Connection');
737                                                 }
738                                                 else{
739                                                         callback.failure.apply(callback.scope, [responseObject]);
740                                                         YAHOO.log('Failure callback with scope. HTTP status code is ' + httpStatus, 'warn', 'Connection');
741                                                 }
742                                         }
743                         }
744
745                         // Fire global custom event -- failureEvent
746                         this.failureEvent.fire(responseObject);
747
748                         if(o.failureEvent){
749                                 // Fire transaction custom event -- failureEvent
750                                 o.failureEvent.fire(responseObject);
751                         }
752
753                 }
754
755                 this.releaseObject(o);
756                 responseObject = null;
757     },
758
759   /**
760    * @description This method evaluates the server response, creates and returns the results via
761    * its properties.  Success and failure cases will differ in the response
762    * object's property values.
763    * @method createResponseObject
764    * @private
765    * @static
766    * @param {object} o The connection object
767    * @param {callbackArg} callbackArg The user-defined argument or arguments to be passed to the callback
768    * @return {object}
769    */
770     createResponseObject:function(o, callbackArg)
771     {
772                 var obj = {};
773                 var headerObj = {};
774
775                 try
776                 {
777                         var headerStr = o.conn.getAllResponseHeaders();
778                         var header = headerStr.split('\n');
779                         for(var i=0; i<header.length; i++){
780                                 var delimitPos = header[i].indexOf(':');
781                                 if(delimitPos != -1){
782                                         headerObj[header[i].substring(0,delimitPos)] = header[i].substring(delimitPos+2);
783                                 }
784                         }
785                 }
786                 catch(e){}
787
788                 obj.tId = o.tId;
789                 // Normalize IE's response to HTTP 204 when Win error 1223.
790                 obj.status = (o.conn.status == 1223)?204:o.conn.status;
791                 // Normalize IE's statusText to "No Content" instead of "Unknown".
792                 obj.statusText = (o.conn.status == 1223)?"No Content":o.conn.statusText;
793                 obj.getResponseHeader = headerObj;
794                 obj.getAllResponseHeaders = headerStr;
795                 obj.responseText = o.conn.responseText;
796                 obj.responseXML = o.conn.responseXML;
797
798                 if(callbackArg){
799                         obj.argument = callbackArg;
800                 }
801
802                 return obj;
803     },
804
805   /**
806    * @description If a transaction cannot be completed due to dropped or closed connections,
807    * there may be not be enough information to build a full response object.
808    * The failure callback will be fired and this specific condition can be identified
809    * by a status property value of 0.
810    *
811    * If an abort was successful, the status property will report a value of -1.
812    *
813    * @method createExceptionObject
814    * @private
815    * @static
816    * @param {int} tId The Transaction Id
817    * @param {callbackArg} callbackArg The user-defined argument or arguments to be passed to the callback
818    * @param {boolean} isAbort Determines if the exception case is caused by a transaction abort
819    * @return {object}
820    */
821     createExceptionObject:function(tId, callbackArg, isAbort)
822     {
823                 var COMM_CODE = 0;
824                 var COMM_ERROR = 'communication failure';
825                 var ABORT_CODE = -1;
826                 var ABORT_ERROR = 'transaction aborted';
827
828                 var obj = {};
829
830                 obj.tId = tId;
831                 if(isAbort){
832                         obj.status = ABORT_CODE;
833                         obj.statusText = ABORT_ERROR;
834                 }
835                 else{
836                         obj.status = COMM_CODE;
837                         obj.statusText = COMM_ERROR;
838                 }
839
840                 if(callbackArg){
841                         obj.argument = callbackArg;
842                 }
843
844                 return obj;
845     },
846
847   /**
848    * @description Method that initializes the custom HTTP headers for the each transaction.
849    * @method initHeader
850    * @public
851    * @static
852    * @param {string} label The HTTP header label
853    * @param {string} value The HTTP header value
854    * @param {string} isDefault Determines if the specific header is a default header
855    * automatically sent with each transaction.
856    * @return {void}
857    */
858         initHeader:function(label, value, isDefault)
859         {
860                 var headerObj = (isDefault)?this._default_headers:this._http_headers;
861                 headerObj[label] = value;
862
863                 if(isDefault){
864                         this._has_default_headers = true;
865                 }
866                 else{
867                         this._has_http_headers = true;
868                 }
869         },
870
871
872   /**
873    * @description Accessor that sets the HTTP headers for each transaction.
874    * @method setHeader
875    * @private
876    * @static
877    * @param {object} o The connection object for the transaction.
878    * @return {void}
879    */
880         setHeader:function(o)
881         {
882                 var prop;
883                 if(this._has_default_headers){
884                         for(prop in this._default_headers){
885                                 if(YAHOO.lang.hasOwnProperty(this._default_headers, prop)){
886                                         o.conn.setRequestHeader(prop, this._default_headers[prop]);
887                                         YAHOO.log('Default HTTP header ' + prop + ' set with value of ' + this._default_headers[prop], 'info', 'Connection');
888                                 }
889                         }
890                 }
891
892                 if(this._has_http_headers){
893                         for(prop in this._http_headers){
894                                 if(YAHOO.lang.hasOwnProperty(this._http_headers, prop)){
895                                         o.conn.setRequestHeader(prop, this._http_headers[prop]);
896                                         YAHOO.log('HTTP header ' + prop + ' set with value of ' + this._http_headers[prop], 'info', 'Connection');
897                                 }
898                         }
899                         delete this._http_headers;
900
901                         this._http_headers = {};
902                         this._has_http_headers = false;
903                 }
904         },
905
906   /**
907    * @description Resets the default HTTP headers object
908    * @method resetDefaultHeaders
909    * @public
910    * @static
911    * @return {void}
912    */
913         resetDefaultHeaders:function(){
914                 delete this._default_headers;
915                 this._default_headers = {};
916                 this._has_default_headers = false;
917         },
918
919   /**
920    * @description This method assembles the form label and value pairs and
921    * constructs an encoded string.
922    * asyncRequest() will automatically initialize the transaction with a
923    * a HTTP header Content-Type of application/x-www-form-urlencoded.
924    * @method setForm
925    * @public
926    * @static
927    * @param {string || object} form id or name attribute, or form object.
928    * @param {boolean} optional enable file upload.
929    * @param {boolean} optional enable file upload over SSL in IE only.
930    * @return {string} string of the HTML form field name and value pairs..
931    */
932         setForm:function(formId, isUpload, secureUri)
933         {
934         var oForm, oElement, oName, oValue, oDisabled,
935             hasSubmit = false,
936             data = [], item = 0,
937             i,len,j,jlen,opt;
938
939                 this.resetFormState();
940
941                 if(typeof formId == 'string'){
942                         // Determine if the argument is a form id or a form name.
943                         // Note form name usage is deprecated by supported
944                         // here for legacy reasons.
945                         oForm = (document.getElementById(formId) || document.forms[formId]);
946                 }
947                 else if(typeof formId == 'object'){
948                         // Treat argument as an HTML form object.
949                         oForm = formId;
950                 }
951                 else{
952                         YAHOO.log('Unable to create form object ' + formId, 'warn', 'Connection');
953                         return;
954                 }
955
956                 // If the isUpload argument is true, setForm will call createFrame to initialize
957                 // an iframe as the form target.
958                 //
959                 // The argument secureURI is also required by IE in SSL environments
960                 // where the secureURI string is a fully qualified HTTP path, used to set the source
961                 // of the iframe, to a stub resource in the same domain.
962                 if(isUpload){
963
964                         // Create iframe in preparation for file upload.
965                         this.createFrame(secureUri?secureUri:null);
966
967                         // Set form reference and file upload properties to true.
968                         this._isFormSubmit = true;
969                         this._isFileUpload = true;
970                         this._formNode = oForm;
971
972                         return;
973
974                 }
975
976                 // Iterate over the form elements collection to construct the
977                 // label-value pairs.
978                 for (i=0,len=oForm.elements.length; i<len; ++i){
979                         oElement  = oForm.elements[i];
980                         oDisabled = oElement.disabled;
981             oName     = oElement.name;
982
983                         // Do not submit fields that are disabled or
984                         // do not have a name attribute value.
985                         if(!oDisabled && oName)
986                         {
987                 oName  = encodeURIComponent(oName)+'=';
988                 oValue = encodeURIComponent(oElement.value);
989
990                                 switch(oElement.type)
991                                 {
992                     // Safari, Opera, FF all default opt.value from .text if
993                     // value attribute not specified in markup
994                                         case 'select-one':
995                         if (oElement.selectedIndex > -1) {
996                             opt = oElement.options[oElement.selectedIndex];
997                             data[item++] = oName + encodeURIComponent(
998                                 (opt.attributes.value && opt.attributes.value.specified) ? opt.value : opt.text);
999                         }
1000                         break;
1001                                         case 'select-multiple':
1002                         if (oElement.selectedIndex > -1) {
1003                             for(j=oElement.selectedIndex, jlen=oElement.options.length; j<jlen; ++j){
1004                                 opt = oElement.options[j];
1005                                 if (opt.selected) {
1006                                     data[item++] = oName + encodeURIComponent(
1007                                         (opt.attributes.value && opt.attributes.value.specified) ? opt.value : opt.text);
1008                                 }
1009                             }
1010                         }
1011                                                 break;
1012                                         case 'radio':
1013                                         case 'checkbox':
1014                                                 if(oElement.checked){
1015                             data[item++] = oName + oValue;
1016                                                 }
1017                                                 break;
1018                                         case 'file':
1019                                                 // stub case as XMLHttpRequest will only send the file path as a string.
1020                                         case undefined:
1021                                                 // stub case for fieldset element which returns undefined.
1022                                         case 'reset':
1023                                                 // stub case for input type reset button.
1024                                         case 'button':
1025                                                 // stub case for input type button elements.
1026                                                 break;
1027                                         case 'submit':
1028                                                 if(hasSubmit === false){
1029                                                         if(this._hasSubmitListener && this._submitElementValue){
1030                                 data[item++] = this._submitElementValue;
1031                                                         }
1032                                                         hasSubmit = true;
1033                                                 }
1034                                                 break;
1035                                         default:
1036                         data[item++] = oName + oValue;
1037                                 }
1038                         }
1039                 }
1040
1041                 this._isFormSubmit = true;
1042                 this._sFormData = data.join('&');
1043
1044                 YAHOO.log('Form initialized for transaction. HTML form POST message is: ' + this._sFormData, 'info', 'Connection');
1045
1046                 this.initHeader('Content-Type', this._default_form_header);
1047                 YAHOO.log('Initialize header Content-Type to application/x-www-form-urlencoded for setForm() transaction.', 'info', 'Connection');
1048
1049                 return this._sFormData;
1050         },
1051
1052   /**
1053    * @description Resets HTML form properties when an HTML form or HTML form
1054    * with file upload transaction is sent.
1055    * @method resetFormState
1056    * @private
1057    * @static
1058    * @return {void}
1059    */
1060         resetFormState:function(){
1061                 this._isFormSubmit = false;
1062                 this._isFileUpload = false;
1063                 this._formNode = null;
1064                 this._sFormData = "";
1065         },
1066
1067   /**
1068    * @description Creates an iframe to be used for form file uploads.  It is remove from the
1069    * document upon completion of the upload transaction.
1070    * @method createFrame
1071    * @private
1072    * @static
1073    * @param {string} optional qualified path of iframe resource for SSL in IE.
1074    * @return {void}
1075    */
1076         createFrame:function(secureUri){
1077
1078                 // IE does not allow the setting of id and name attributes as object
1079                 // properties via createElement().  A different iframe creation
1080                 // pattern is required for IE.
1081                 var frameId = 'yuiIO' + this._transaction_id;
1082                 var io;
1083                 if(YAHOO.env.ua.ie){
1084                         io = document.createElement('<iframe id="' + frameId + '" name="' + frameId + '" />');
1085
1086                         // IE will throw a security exception in an SSL environment if the
1087                         // iframe source is undefined.
1088                         if(typeof secureUri == 'boolean'){
1089                                 io.src = 'javascript:false';
1090                         }
1091                 }
1092                 else{
1093                         io = document.createElement('iframe');
1094                         io.id = frameId;
1095                         io.name = frameId;
1096                 }
1097
1098                 io.style.position = 'absolute';
1099                 io.style.top = '-1000px';
1100                 io.style.left = '-1000px';
1101
1102                 document.body.appendChild(io);
1103                 YAHOO.log('File upload iframe created. Id is:' + frameId, 'info', 'Connection');
1104         },
1105
1106   /**
1107    * @description Parses the POST data and creates hidden form elements
1108    * for each key-value, and appends them to the HTML form object.
1109    * @method appendPostData
1110    * @private
1111    * @static
1112    * @param {string} postData The HTTP POST data
1113    * @return {array} formElements Collection of hidden fields.
1114    */
1115         appendPostData:function(postData)
1116         {
1117                 var formElements = [],
1118                         postMessage = postData.split('&'),
1119                         i, delimitPos;
1120                 for(i=0; i < postMessage.length; i++){
1121                         delimitPos = postMessage[i].indexOf('=');
1122                         if(delimitPos != -1){
1123                                 formElements[i] = document.createElement('input');
1124                                 formElements[i].type = 'hidden';
1125                                 formElements[i].name = decodeURIComponent(postMessage[i].substring(0,delimitPos));
1126                                 formElements[i].value = decodeURIComponent(postMessage[i].substring(delimitPos+1));
1127                                 this._formNode.appendChild(formElements[i]);
1128                         }
1129                 }
1130
1131                 return formElements;
1132         },
1133
1134   /**
1135    * @description Uploads HTML form, inclusive of files/attachments, using the
1136    * iframe created in createFrame to facilitate the transaction.
1137    * @method uploadFile
1138    * @private
1139    * @static
1140    * @param {int} id The transaction id.
1141    * @param {object} callback User-defined callback object.
1142    * @param {string} uri Fully qualified path of resource.
1143    * @param {string} postData POST data to be submitted in addition to HTML form.
1144    * @return {void}
1145    */
1146         uploadFile:function(o, callback, uri, postData){
1147
1148                 // Each iframe has an id prefix of "yuiIO" followed
1149                 // by the unique transaction id.
1150                 var frameId = 'yuiIO' + o.tId,
1151                     uploadEncoding = 'multipart/form-data',
1152                     io = document.getElementById(frameId),
1153                     oConn = this,
1154                         args = (callback && callback.argument)?callback.argument:null,
1155             oElements,i,prop,obj;
1156
1157                 // Track original HTML form attribute values.
1158                 var rawFormAttributes =
1159                 {
1160                         action:this._formNode.getAttribute('action'),
1161                         method:this._formNode.getAttribute('method'),
1162                         target:this._formNode.getAttribute('target')
1163                 };
1164
1165                 // Initialize the HTML form properties in case they are
1166                 // not defined in the HTML form.
1167                 this._formNode.setAttribute('action', uri);
1168                 this._formNode.setAttribute('method', 'POST');
1169                 this._formNode.setAttribute('target', frameId);
1170
1171                 if(YAHOO.env.ua.ie){
1172                         // IE does not respect property enctype for HTML forms.
1173                         // Instead it uses the property - "encoding".
1174                         this._formNode.setAttribute('encoding', uploadEncoding);
1175                 }
1176                 else{
1177                         this._formNode.setAttribute('enctype', uploadEncoding);
1178                 }
1179
1180                 if(postData){
1181                         oElements = this.appendPostData(postData);
1182                 }
1183
1184                 // Start file upload.
1185                 this._formNode.submit();
1186
1187                 // Fire global custom event -- startEvent
1188                 this.startEvent.fire(o, args);
1189
1190                 if(o.startEvent){
1191                         // Fire transaction custom event -- startEvent
1192                         o.startEvent.fire(o, args);
1193                 }
1194
1195                 // Start polling if a callback is present and the timeout
1196                 // property has been defined.
1197                 if(callback && callback.timeout){
1198                         this._timeOut[o.tId] = window.setTimeout(function(){ oConn.abort(o, callback, true); }, callback.timeout);
1199                 }
1200
1201                 // Remove HTML elements created by appendPostData
1202                 if(oElements && oElements.length > 0){
1203                         for(i=0; i < oElements.length; i++){
1204                                 this._formNode.removeChild(oElements[i]);
1205                         }
1206                 }
1207
1208                 // Restore HTML form attributes to their original
1209                 // values prior to file upload.
1210                 for(prop in rawFormAttributes){
1211                         if(YAHOO.lang.hasOwnProperty(rawFormAttributes, prop)){
1212                                 if(rawFormAttributes[prop]){
1213                                         this._formNode.setAttribute(prop, rawFormAttributes[prop]);
1214                                 }
1215                                 else{
1216                                         this._formNode.removeAttribute(prop);
1217                                 }
1218                         }
1219                 }
1220
1221                 // Reset HTML form state properties.
1222                 this.resetFormState();
1223
1224                 // Create the upload callback handler that fires when the iframe
1225                 // receives the load event.  Subsequently, the event handler is detached
1226                 // and the iframe removed from the document.
1227                 var uploadCallback = function()
1228                 {
1229                         if(callback && callback.timeout){
1230                                 window.clearTimeout(oConn._timeOut[o.tId]);
1231                                 delete oConn._timeOut[o.tId];
1232                         }
1233
1234                         // Fire global custom event -- completeEvent
1235                         oConn.completeEvent.fire(o, args);
1236
1237                         if(o.completeEvent){
1238                                 // Fire transaction custom event -- completeEvent
1239                                 o.completeEvent.fire(o, args);
1240                         }
1241
1242                         obj = {
1243                             tId : o.tId,
1244                             argument : callback.argument
1245             };
1246
1247                         try
1248                         {
1249                                 // responseText and responseXML will be populated with the same data from the iframe.
1250                                 // Since the HTTP headers cannot be read from the iframe
1251                                 obj.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:io.contentWindow.document.documentElement.textContent;
1252                                 obj.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
1253                         }
1254                         catch(e){}
1255
1256                         if(callback && callback.upload){
1257                                 if(!callback.scope){
1258                                         callback.upload(obj);
1259                                         YAHOO.log('Upload callback.', 'info', 'Connection');
1260                                 }
1261                                 else{
1262                                         callback.upload.apply(callback.scope, [obj]);
1263                                         YAHOO.log('Upload callback with scope.', 'info', 'Connection');
1264                                 }
1265                         }
1266
1267                         // Fire global custom event -- uploadEvent
1268                         oConn.uploadEvent.fire(obj);
1269
1270                         if(o.uploadEvent){
1271                                 // Fire transaction custom event -- uploadEvent
1272                                 o.uploadEvent.fire(obj);
1273                         }
1274
1275                         YAHOO.util.Event.removeListener(io, "load", uploadCallback);
1276
1277                         setTimeout(
1278                                 function(){
1279                                         document.body.removeChild(io);
1280                                         oConn.releaseObject(o);
1281                                         YAHOO.log('File upload iframe destroyed. Id is:' + frameId, 'info', 'Connection');
1282                                 }, 100);
1283                 };
1284
1285                 // Bind the onload handler to the iframe to detect the file upload response.
1286                 YAHOO.util.Event.addListener(io, "load", uploadCallback);
1287         },
1288
1289   /**
1290    * @description Method to terminate a transaction, if it has not reached readyState 4.
1291    * @method abort
1292    * @public
1293    * @static
1294    * @param {object} o The connection object returned by asyncRequest.
1295    * @param {object} callback  User-defined callback object.
1296    * @param {string} isTimeout boolean to indicate if abort resulted from a callback timeout.
1297    * @return {boolean}
1298    */
1299         abort:function(o, callback, isTimeout)
1300         {
1301                 var abortStatus;
1302                 var args = (callback && callback.argument)?callback.argument:null;
1303
1304
1305                 if(o && o.conn){
1306                         if(this.isCallInProgress(o)){
1307                                 // Issue abort request
1308                                 o.conn.abort();
1309
1310                                 window.clearInterval(this._poll[o.tId]);
1311                                 delete this._poll[o.tId];
1312
1313                                 if(isTimeout){
1314                                         window.clearTimeout(this._timeOut[o.tId]);
1315                                         delete this._timeOut[o.tId];
1316                                 }
1317
1318                                 abortStatus = true;
1319                         }
1320                 }
1321                 else if(o && o.isUpload === true){
1322                         var frameId = 'yuiIO' + o.tId;
1323                         var io = document.getElementById(frameId);
1324
1325                         if(io){
1326                                 // Remove all listeners on the iframe prior to
1327                                 // its destruction.
1328                                 YAHOO.util.Event.removeListener(io, "load");
1329                                 // Destroy the iframe facilitating the transaction.
1330                                 document.body.removeChild(io);
1331                                 YAHOO.log('File upload iframe destroyed. Id is:' + frameId, 'info', 'Connection');
1332
1333                                 if(isTimeout){
1334                                         window.clearTimeout(this._timeOut[o.tId]);
1335                                         delete this._timeOut[o.tId];
1336                                 }
1337
1338                                 abortStatus = true;
1339                         }
1340                 }
1341                 else{
1342                         abortStatus = false;
1343                 }
1344
1345                 if(abortStatus === true){
1346                         // Fire global custom event -- abortEvent
1347                         this.abortEvent.fire(o, args);
1348
1349                         if(o.abortEvent){
1350                                 // Fire transaction custom event -- abortEvent
1351                                 o.abortEvent.fire(o, args);
1352                         }
1353
1354                         this.handleTransactionResponse(o, callback, true);
1355                         YAHOO.log('Transaction ' + o.tId + ' aborted.', 'info', 'Connection');
1356                 }
1357
1358                 return abortStatus;
1359         },
1360
1361   /**
1362    * @description Determines if the transaction is still being processed.
1363    * @method isCallInProgress
1364    * @public
1365    * @static
1366    * @param {object} o The connection object returned by asyncRequest
1367    * @return {boolean}
1368    */
1369         isCallInProgress:function(o)
1370         {
1371                 // if the XHR object assigned to the transaction has not been dereferenced,
1372                 // then check its readyState status.  Otherwise, return false.
1373                 if(o && o.conn){
1374                         return o.conn.readyState !== 4 && o.conn.readyState !== 0;
1375                 }
1376                 else if(o && o.isUpload === true){
1377                         var frameId = 'yuiIO' + o.tId;
1378                         return document.getElementById(frameId)?true:false;
1379                 }
1380                 else{
1381                         return false;
1382                 }
1383         },
1384
1385   /**
1386    * @description Dereference the XHR instance and the connection object after the transaction is completed.
1387    * @method releaseObject
1388    * @private
1389    * @static
1390    * @param {object} o The connection object
1391    * @return {void}
1392    */
1393         releaseObject:function(o)
1394         {
1395                 if(o && o.conn){
1396                         //dereference the XHR instance.
1397                         o.conn = null;
1398
1399                         YAHOO.log('Connection object for transaction ' + o.tId + ' destroyed.', 'info', 'Connection');
1400
1401                         //dereference the connection object.
1402                         o = null;
1403                 }
1404         }
1405 };
1406 YAHOO.register("connection", YAHOO.util.Connect, {version: "2.7.0", build: "1799"});