web/support/javascript/form.js
author amit@thunder
Mon, 25 Jan 2010 18:56:45 +0530
changeset 0 8083d21c0020
permissions -rwxr-xr-x
The first commit of all the required files for the review app
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     1
/*
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     2
 * jQuery Form Plugin
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     3
 * @requires jQuery v1.1 or later
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     4
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     5
 * Examples at: http://malsup.com/jquery/form/
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     6
 * Dual licensed under the MIT and GPL licenses:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     7
 *   http://www.opensource.org/licenses/mit-license.php
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     8
 *   http://www.gnu.org/licenses/gpl.html
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     9
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    10
 * Revision: $Id$
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    11
 */
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    12
 (function($) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    13
/**
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    14
 * ajaxSubmit() provides a mechanism for submitting an HTML form using AJAX.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    15
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    16
 * ajaxSubmit accepts a single argument which can be either a success callback function
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    17
 * or an options Object.  If a function is provided it will be invoked upon successful
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    18
 * completion of the submit and will be passed the response from the server.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    19
 * If an options Object is provided, the following attributes are supported:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    20
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    21
 *  target:   Identifies the element(s) in the page to be updated with the server response.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    22
 *            This value may be specified as a jQuery selection string, a jQuery object,
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    23
 *            or a DOM element.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    24
 *            default value: null
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    25
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    26
 *  url:      URL to which the form data will be submitted.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    27
 *            default value: value of form's 'action' attribute
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    28
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    29
 *  type:     The method in which the form data should be submitted, 'GET' or 'POST'.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    30
 *            default value: value of form's 'method' attribute (or 'GET' if none found)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    31
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    32
 *  data:     Additional data to add to the request, specified as key/value pairs (see $.ajax).
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    33
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    34
 *  beforeSubmit:  Callback method to be invoked before the form is submitted.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    35
 *            default value: null
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    36
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    37
 *  success:  Callback method to be invoked after the form has been successfully submitted
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    38
 *            and the response has been returned from the server
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    39
 *            default value: null
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    40
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    41
 *  dataType: Expected dataType of the response.  One of: null, 'xml', 'script', or 'json'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    42
 *            default value: null
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    43
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    44
 *  semantic: Boolean flag indicating whether data must be submitted in semantic order (slower).
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    45
 *            default value: false
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    46
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    47
 *  resetForm: Boolean flag indicating whether the form should be reset if the submit is successful
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    48
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    49
 *  clearForm: Boolean flag indicating whether the form should be cleared if the submit is successful
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    50
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    51
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    52
 * The 'beforeSubmit' callback can be provided as a hook for running pre-submit logic or for
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    53
 * validating the form data.  If the 'beforeSubmit' callback returns false then the form will
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    54
 * not be submitted. The 'beforeSubmit' callback is invoked with three arguments: the form data
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    55
 * in array format, the jQuery object, and the options object passed into ajaxSubmit.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    56
 * The form data array takes the following form:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    57
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    58
 *     [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    59
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    60
 * If a 'success' callback method is provided it is invoked after the response has been returned
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    61
 * from the server.  It is passed the responseText or responseXML value (depending on dataType).
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    62
 * See jQuery.ajax for further details.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    63
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    64
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    65
 * The dataType option provides a means for specifying how the server response should be handled.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    66
 * This maps directly to the jQuery.httpData method.  The following values are supported:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    67
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    68
 *      'xml':    if dataType == 'xml' the server response is treated as XML and the 'success'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    69
 *                   callback method, if specified, will be passed the responseXML value
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    70
 *      'json':   if dataType == 'json' the server response will be evaluted and passed to
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    71
 *                   the 'success' callback, if specified
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    72
 *      'script': if dataType == 'script' the server response is evaluated in the global context
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    73
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    74
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    75
 * Note that it does not make sense to use both the 'target' and 'dataType' options.  If both
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    76
 * are provided the target will be ignored.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    77
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    78
 * The semantic argument can be used to force form serialization in semantic order.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    79
 * This is normally true anyway, unless the form contains input elements of type='image'.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    80
 * If your form must be submitted with name/value pairs in semantic order and your form
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    81
 * contains an input of type='image" then pass true for this arg, otherwise pass false
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    82
 * (or nothing) to avoid the overhead for this logic.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    83
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    84
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    85
 * When used on its own, ajaxSubmit() is typically bound to a form's submit event like this:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    86
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    87
 * $("#form-id").submit(function() {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    88
 *     $(this).ajaxSubmit(options);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    89
 *     return false; // cancel conventional submit
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    90
 * });
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    91
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    92
 * When using ajaxForm(), however, this is done for you.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    93
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    94
 * @example
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    95
 * $('#myForm').ajaxSubmit(function(data) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    96
 *     alert('Form submit succeeded! Server returned: ' + data);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    97
 * });
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    98
 * @desc Submit form and alert server response
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    99
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   100
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   101
 * @example
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   102
 * var options = {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   103
 *     target: '#myTargetDiv'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   104
 * };
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   105
 * $('#myForm').ajaxSubmit(options);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   106
 * @desc Submit form and update page element with server response
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   107
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   108
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   109
 * @example
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   110
 * var options = {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   111
 *     success: function(responseText) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   112
 *         alert(responseText);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   113
 *     }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   114
 * };
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   115
 * $('#myForm').ajaxSubmit(options);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   116
 * @desc Submit form and alert the server response
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   117
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   118
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   119
 * @example
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   120
 * var options = {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   121
 *     beforeSubmit: function(formArray, jqForm) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   122
 *         if (formArray.length == 0) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   123
 *             alert('Please enter data.');
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   124
 *             return false;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   125
 *         }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   126
 *     }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   127
 * };
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   128
 * $('#myForm').ajaxSubmit(options);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   129
 * @desc Pre-submit validation which aborts the submit operation if form data is empty
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   130
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   131
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   132
 * @example
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   133
 * var options = {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   134
 *     url: myJsonUrl.php,
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   135
 *     dataType: 'json',
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   136
 *     success: function(data) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   137
 *        // 'data' is an object representing the the evaluated json data
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   138
 *     }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   139
 * };
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   140
 * $('#myForm').ajaxSubmit(options);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   141
 * @desc json data returned and evaluated
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   142
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   143
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   144
 * @example
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   145
 * var options = {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   146
 *     url: myXmlUrl.php,
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   147
 *     dataType: 'xml',
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   148
 *     success: function(responseXML) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   149
 *        // responseXML is XML document object
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   150
 *        var data = $('myElement', responseXML).text();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   151
 *     }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   152
 * };
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   153
 * $('#myForm').ajaxSubmit(options);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   154
 * @desc XML data returned from server
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   155
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   156
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   157
 * @example
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   158
 * var options = {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   159
 *     resetForm: true
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   160
 * };
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   161
 * $('#myForm').ajaxSubmit(options);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   162
 * @desc submit form and reset it if successful
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   163
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   164
 * @example
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   165
 * $('#myForm).submit(function() {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   166
 *    $(this).ajaxSubmit();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   167
 *    return false;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   168
 * });
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   169
 * @desc Bind form's submit event to use ajaxSubmit
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   170
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   171
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   172
 * @name ajaxSubmit
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   173
 * @type jQuery
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   174
 * @param options  object literal containing options which control the form submission process
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   175
 * @cat Plugins/Form
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   176
 * @return jQuery
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   177
 */
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   178
$.fn.ajaxSubmit = function(options) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   179
    if (typeof options == 'function')
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   180
        options = { success: options };
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   181
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   182
    options = $.extend({
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   183
        url:  this.attr('action') || window.location,
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   184
        type: this.attr('method') || 'GET'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   185
    }, options || {});
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   186
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   187
    // hook for manipulating the form data before it is extracted;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   188
    // convenient for use with rich editors like tinyMCE or FCKEditor
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   189
    var veto = {};
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   190
    $.event.trigger('form.pre.serialize', [this, options, veto]);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   191
    if (veto.veto) return this;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   192
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   193
    var a = this.formToArray(options.semantic);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   194
	if (options.data) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   195
	    for (var n in options.data)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   196
	        a.push( { name: n, value: options.data[n] } );
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   197
	}
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   198
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   199
    // give pre-submit callback an opportunity to abort the submit
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   200
    if (options.beforeSubmit && options.beforeSubmit(a, this, options) === false) return this;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   201
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   202
    // fire vetoable 'validate' event
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   203
    $.event.trigger('form.submit.validate', [a, this, options, veto]);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   204
    if (veto.veto) return this;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   205
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   206
    var q = $.param(a);//.replace(/%20/g,'+');
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   207
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   208
    if (options.type.toUpperCase() == 'GET') {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   209
        options.url += (options.url.indexOf('?') >= 0 ? '&' : '?') + q;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   210
        options.data = null;  // data is null for 'get'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   211
    }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   212
    else
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   213
        options.data = q; // data is the query string for 'post'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   214
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   215
    var $form = this, callbacks = [];
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   216
    if (options.resetForm) callbacks.push(function() { $form.resetForm(); });
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   217
    if (options.clearForm) callbacks.push(function() { $form.clearForm(); });
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   218
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   219
    // perform a load on the target only if dataType is not provided
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   220
    if (!options.dataType && options.target) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   221
        var oldSuccess = options.success || function(){};
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   222
        callbacks.push(function(data) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   223
            if (this.evalScripts)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   224
                $(options.target).attr("innerHTML", data).evalScripts().each(oldSuccess, arguments);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   225
            else // jQuery v1.1.4
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   226
                $(options.target).html(data).each(oldSuccess, arguments);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   227
        });
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   228
    }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   229
    else if (options.success)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   230
        callbacks.push(options.success);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   231
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   232
    options.success = function(data, status) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   233
        for (var i=0, max=callbacks.length; i < max; i++)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   234
            callbacks[i](data, status, $form);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   235
    };
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   236
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   237
    // are there files to upload?
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   238
    var files = $('input:file', this).fieldValue();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   239
    var found = false;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   240
    for (var j=0; j < files.length; j++)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   241
        if (files[j])
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   242
            found = true;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   243
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   244
    if (options.iframe || found) // options.iframe allows user to force iframe mode
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   245
        fileUpload();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   246
    else
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   247
        $.ajax(options);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   248
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   249
    // fire 'notify' event
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   250
    $.event.trigger('form.submit.notify', [this, options]);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   251
    return this;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   252
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   253
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   254
    // private function for handling file uploads (hat tip to YAHOO!)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   255
    function fileUpload() {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   256
        var form = $form[0];
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   257
        var opts = $.extend({}, $.ajaxSettings, options);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   258
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   259
        var id = 'jqFormIO' + $.fn.ajaxSubmit.counter++;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   260
        var $io = $('<iframe id="' + id + '" name="' + id + '" />');
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   261
        var io = $io[0];
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   262
        var op8 = $.browser.opera && window.opera.version() < 9;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   263
        if ($.browser.msie || op8) io.src = 'javascript:false;document.write("");';
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   264
        $io.css({ position: 'absolute', top: '-1000px', left: '-1000px' });
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   265
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   266
        var xhr = { // mock object
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   267
            responseText: null,
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   268
            responseXML: null,
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   269
            status: 0,
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   270
            statusText: 'n/a',
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   271
            getAllResponseHeaders: function() {},
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   272
            getResponseHeader: function() {},
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   273
            setRequestHeader: function() {}
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   274
        };
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   275
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   276
        var g = opts.global;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   277
        // trigger ajax global events so that activity/block indicators work like normal
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   278
        if (g && ! $.active++) $.event.trigger("ajaxStart");
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   279
        if (g) $.event.trigger("ajaxSend", [xhr, opts]);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   280
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   281
        var cbInvoked = 0;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   282
        var timedOut = 0;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   283
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   284
        // take a breath so that pending repaints get some cpu time before the upload starts
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   285
        setTimeout(function() {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   286
            $io.appendTo('body');
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   287
            // jQuery's event binding doesn't work for iframe events in IE
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   288
            io.attachEvent ? io.attachEvent('onload', cb) : io.addEventListener('load', cb, false);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   289
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   290
            // make sure form attrs are set
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   291
            var encAttr = form.encoding ? 'encoding' : 'enctype';
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   292
            var t = $form.attr('target');
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   293
            $form.attr({
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   294
                target:   id,
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   295
                method:  'POST',
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   296
                action:   opts.url
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   297
            });
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   298
            form[encAttr] = 'multipart/form-data';
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   299
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   300
            // support timout
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   301
            if (opts.timeout)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   302
                setTimeout(function() { timedOut = true; cb(); }, opts.timeout);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   303
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   304
            form.submit();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   305
            $form.attr('target', t); // reset target
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   306
        }, 10);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   307
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   308
        function cb() {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   309
            if (cbInvoked++) return;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   310
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   311
            io.detachEvent ? io.detachEvent('onload', cb) : io.removeEventListener('load', cb, false);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   312
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   313
            var ok = true;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   314
            try {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   315
                if (timedOut) throw 'timeout';
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   316
                // extract the server response from the iframe
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   317
                var data, doc;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   318
                doc = io.contentWindow ? io.contentWindow.document : io.contentDocument ? io.contentDocument : io.document;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   319
                xhr.responseText = doc.body ? doc.body.innerHTML : null;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   320
                xhr.responseXML = doc.XMLDocument ? doc.XMLDocument : doc;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   321
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   322
                if (opts.dataType == 'json' || opts.dataType == 'script') {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   323
                    var ta = doc.getElementsByTagName('textarea')[0];
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   324
                    data = ta ? ta.value : xhr.responseText;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   325
                    if (opts.dataType == 'json')
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   326
                        eval("data = " + data);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   327
                    else
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   328
                        $.globalEval(data);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   329
                }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   330
                else if (opts.dataType == 'xml') {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   331
                    data = xhr.responseXML;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   332
                    if (!data && xhr.responseText != null)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   333
                        data = toXml(xhr.responseText);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   334
                }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   335
                else {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   336
                    data = xhr.responseText;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   337
                }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   338
            }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   339
            catch(e){
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   340
                ok = false;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   341
                $.handleError(opts, xhr, 'error', e);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   342
            }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   343
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   344
            // ordering of these callbacks/triggers is odd, but that's how $.ajax does it
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   345
            if (ok) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   346
                opts.success(data, 'success');
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   347
                if (g) $.event.trigger("ajaxSuccess", [xhr, opts]);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   348
            }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   349
            if (g) $.event.trigger("ajaxComplete", [xhr, opts]);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   350
            if (g && ! --$.active) $.event.trigger("ajaxStop");
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   351
            if (opts.complete) opts.complete(xhr, ok ? 'success' : 'error');
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   352
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   353
            // clean up
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   354
            setTimeout(function() {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   355
                $io.remove();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   356
                xhr.responseXML = null;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   357
            }, 100);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   358
        };
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   359
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   360
        function toXml(s, doc) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   361
            if (window.ActiveXObject) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   362
                doc = new ActiveXObject('Microsoft.XMLDOM');
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   363
                doc.async = 'false';
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   364
                doc.loadXML(s);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   365
            }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   366
            else
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   367
                doc = (new DOMParser()).parseFromString(s, 'text/xml');
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   368
            return (doc && doc.documentElement && doc.documentElement.tagName != 'parsererror') ? doc : null;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   369
        };
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   370
    };
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   371
};
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   372
$.fn.ajaxSubmit.counter = 0; // used to create unique iframe ids
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   373
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   374
/**
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   375
 * ajaxForm() provides a mechanism for fully automating form submission.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   376
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   377
 * The advantages of using this method instead of ajaxSubmit() are:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   378
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   379
 * 1: This method will include coordinates for <input type="image" /> elements (if the element
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   380
 *    is used to submit the form).
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   381
 * 2. This method will include the submit element's name/value data (for the element that was
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   382
 *    used to submit the form).
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   383
 * 3. This method binds the submit() method to the form for you.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   384
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   385
 * Note that for accurate x/y coordinates of image submit elements in all browsers
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   386
 * you need to also use the "dimensions" plugin (this method will auto-detect its presence).
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   387
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   388
 * The options argument for ajaxForm works exactly as it does for ajaxSubmit.  ajaxForm merely
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   389
 * passes the options argument along after properly binding events for submit elements and
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   390
 * the form itself.  See ajaxSubmit for a full description of the options argument.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   391
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   392
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   393
 * @example
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   394
 * var options = {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   395
 *     target: '#myTargetDiv'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   396
 * };
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   397
 * $('#myForm').ajaxSForm(options);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   398
 * @desc Bind form's submit event so that 'myTargetDiv' is updated with the server response
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   399
 *       when the form is submitted.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   400
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   401
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   402
 * @example
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   403
 * var options = {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   404
 *     success: function(responseText) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   405
 *         alert(responseText);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   406
 *     }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   407
 * };
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   408
 * $('#myForm').ajaxSubmit(options);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   409
 * @desc Bind form's submit event so that server response is alerted after the form is submitted.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   410
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   411
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   412
 * @example
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   413
 * var options = {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   414
 *     beforeSubmit: function(formArray, jqForm) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   415
 *         if (formArray.length == 0) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   416
 *             alert('Please enter data.');
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   417
 *             return false;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   418
 *         }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   419
 *     }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   420
 * };
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   421
 * $('#myForm').ajaxSubmit(options);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   422
 * @desc Bind form's submit event so that pre-submit callback is invoked before the form
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   423
 *       is submitted.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   424
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   425
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   426
 * @name   ajaxForm
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   427
 * @param  options  object literal containing options which control the form submission process
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   428
 * @return jQuery
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   429
 * @cat    Plugins/Form
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   430
 * @type   jQuery
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   431
 */
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   432
$.fn.ajaxForm = function(options) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   433
    return this.ajaxFormUnbind().submit(submitHandler).each(function() {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   434
        // store options in hash
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   435
        this.formPluginId = $.fn.ajaxForm.counter++;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   436
        $.fn.ajaxForm.optionHash[this.formPluginId] = options;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   437
        $(":submit,input:image", this).click(clickHandler);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   438
    });
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   439
};
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   440
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   441
$.fn.ajaxForm.counter = 1;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   442
$.fn.ajaxForm.optionHash = {};
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   443
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   444
function clickHandler(e) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   445
    var $form = this.form;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   446
    $form.clk = this;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   447
    if (this.type == 'image') {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   448
        if (e.offsetX != undefined) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   449
            $form.clk_x = e.offsetX;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   450
            $form.clk_y = e.offsetY;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   451
        } else if (typeof $.fn.offset == 'function') { // try to use dimensions plugin
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   452
            var offset = $(this).offset();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   453
            $form.clk_x = e.pageX - offset.left;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   454
            $form.clk_y = e.pageY - offset.top;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   455
        } else {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   456
            $form.clk_x = e.pageX - this.offsetLeft;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   457
            $form.clk_y = e.pageY - this.offsetTop;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   458
        }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   459
    }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   460
    // clear form vars
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   461
    setTimeout(function() { $form.clk = $form.clk_x = $form.clk_y = null; }, 10);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   462
};
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   463
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   464
function submitHandler() {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   465
    // retrieve options from hash
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   466
    var id = this.formPluginId;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   467
    var options = $.fn.ajaxForm.optionHash[id];
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   468
    $(this).ajaxSubmit(options);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   469
    return false;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   470
};
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   471
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   472
/**
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   473
 * ajaxFormUnbind unbinds the event handlers that were bound by ajaxForm
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   474
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   475
 * @name   ajaxFormUnbind
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   476
 * @return jQuery
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   477
 * @cat    Plugins/Form
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   478
 * @type   jQuery
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   479
 */
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   480
$.fn.ajaxFormUnbind = function() {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   481
    this.unbind('submit', submitHandler);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   482
    return this.each(function() {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   483
        $(":submit,input:image", this).unbind('click', clickHandler);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   484
    });
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   485
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   486
};
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   487
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   488
/**
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   489
 * formToArray() gathers form element data into an array of objects that can
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   490
 * be passed to any of the following ajax functions: $.get, $.post, or load.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   491
 * Each object in the array has both a 'name' and 'value' property.  An example of
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   492
 * an array for a simple login form might be:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   493
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   494
 * [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   495
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   496
 * It is this array that is passed to pre-submit callback functions provided to the
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   497
 * ajaxSubmit() and ajaxForm() methods.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   498
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   499
 * The semantic argument can be used to force form serialization in semantic order.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   500
 * This is normally true anyway, unless the form contains input elements of type='image'.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   501
 * If your form must be submitted with name/value pairs in semantic order and your form
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   502
 * contains an input of type='image" then pass true for this arg, otherwise pass false
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   503
 * (or nothing) to avoid the overhead for this logic.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   504
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   505
 * @example var data = $("#myForm").formToArray();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   506
 * $.post( "myscript.cgi", data );
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   507
 * @desc Collect all the data from a form and submit it to the server.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   508
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   509
 * @name formToArray
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   510
 * @param semantic true if serialization must maintain strict semantic ordering of elements (slower)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   511
 * @type Array<Object>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   512
 * @cat Plugins/Form
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   513
 */
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   514
$.fn.formToArray = function(semantic) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   515
    var a = [];
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   516
    if (this.length == 0) return a;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   517
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   518
    var form = this[0];
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   519
    var els = semantic ? form.getElementsByTagName('*') : form.elements;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   520
    if (!els) return a;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   521
    for(var i=0, max=els.length; i < max; i++) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   522
        var el = els[i];
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   523
        var n = el.name;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   524
        if (!n) continue;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   525
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   526
        if (semantic && form.clk && el.type == "image") {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   527
            // handle image inputs on the fly when semantic == true
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   528
            if(!el.disabled && form.clk == el)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   529
                a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y});
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   530
            continue;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   531
        }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   532
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   533
        var v = $.fieldValue(el, true);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   534
        if (v && v.constructor == Array) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   535
            for(var j=0, jmax=v.length; j < jmax; j++)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   536
                a.push({name: n, value: v[j]});
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   537
        }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   538
        else if (v !== null && typeof v != 'undefined')
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   539
            a.push({name: n, value: v});
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   540
    }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   541
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   542
    if (!semantic && form.clk) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   543
        // input type=='image' are not found in elements array! handle them here
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   544
        var inputs = form.getElementsByTagName("input");
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   545
        for(var i=0, max=inputs.length; i < max; i++) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   546
            var input = inputs[i];
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   547
            var n = input.name;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   548
            if(n && !input.disabled && input.type == "image" && form.clk == input)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   549
                a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y});
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   550
        }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   551
    }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   552
    return a;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   553
};
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   554
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   555
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   556
/**
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   557
 * Serializes form data into a 'submittable' string. This method will return a string
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   558
 * in the format: name1=value1&amp;name2=value2
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   559
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   560
 * The semantic argument can be used to force form serialization in semantic order.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   561
 * If your form must be submitted with name/value pairs in semantic order then pass
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   562
 * true for this arg, otherwise pass false (or nothing) to avoid the overhead for
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   563
 * this logic (which can be significant for very large forms).
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   564
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   565
 * @example var data = $("#myForm").formSerialize();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   566
 * $.ajax('POST', "myscript.cgi", data);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   567
 * @desc Collect all the data from a form into a single string
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   568
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   569
 * @name formSerialize
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   570
 * @param semantic true if serialization must maintain strict semantic ordering of elements (slower)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   571
 * @type String
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   572
 * @cat Plugins/Form
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   573
 */
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   574
$.fn.formSerialize = function(semantic) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   575
    //hand off to jQuery.param for proper encoding
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   576
    return $.param(this.formToArray(semantic));
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   577
};
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   578
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   579
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   580
/**
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   581
 * Serializes all field elements in the jQuery object into a query string.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   582
 * This method will return a string in the format: name1=value1&amp;name2=value2
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   583
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   584
 * The successful argument controls whether or not serialization is limited to
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   585
 * 'successful' controls (per http://www.w3.org/TR/html4/interact/forms.html#successful-controls).
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   586
 * The default value of the successful argument is true.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   587
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   588
 * @example var data = $("input").formSerialize();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   589
 * @desc Collect the data from all successful input elements into a query string
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   590
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   591
 * @example var data = $(":radio").formSerialize();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   592
 * @desc Collect the data from all successful radio input elements into a query string
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   593
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   594
 * @example var data = $("#myForm :checkbox").formSerialize();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   595
 * @desc Collect the data from all successful checkbox input elements in myForm into a query string
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   596
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   597
 * @example var data = $("#myForm :checkbox").formSerialize(false);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   598
 * @desc Collect the data from all checkbox elements in myForm (even the unchecked ones) into a query string
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   599
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   600
 * @example var data = $(":input").formSerialize();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   601
 * @desc Collect the data from all successful input, select, textarea and button elements into a query string
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   602
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   603
 * @name fieldSerialize
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   604
 * @param successful true if only successful controls should be serialized (default is true)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   605
 * @type String
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   606
 * @cat Plugins/Form
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   607
 */
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   608
$.fn.fieldSerialize = function(successful) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   609
    var a = [];
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   610
    this.each(function() {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   611
        var n = this.name;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   612
        if (!n) return;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   613
        var v = $.fieldValue(this, successful);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   614
        if (v && v.constructor == Array) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   615
            for (var i=0,max=v.length; i < max; i++)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   616
                a.push({name: n, value: v[i]});
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   617
        }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   618
        else if (v !== null && typeof v != 'undefined')
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   619
            a.push({name: this.name, value: v});
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   620
    });
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   621
    //hand off to jQuery.param for proper encoding
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   622
    return $.param(a);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   623
};
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   624
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   625
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   626
/**
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   627
 * Returns the value(s) of the element in the matched set.  For example, consider the following form:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   628
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   629
 *  <form><fieldset>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   630
 *      <input name="A" type="text" />
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   631
 *      <input name="A" type="text" />
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   632
 *      <input name="B" type="checkbox" value="B1" />
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   633
 *      <input name="B" type="checkbox" value="B2"/>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   634
 *      <input name="C" type="radio" value="C1" />
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   635
 *      <input name="C" type="radio" value="C2" />
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   636
 *  </fieldset></form>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   637
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   638
 *  var v = $(':text').fieldValue();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   639
 *  // if no values are entered into the text inputs
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   640
 *  v == ['','']
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   641
 *  // if values entered into the text inputs are 'foo' and 'bar'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   642
 *  v == ['foo','bar']
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   643
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   644
 *  var v = $(':checkbox').fieldValue();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   645
 *  // if neither checkbox is checked
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   646
 *  v === undefined
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   647
 *  // if both checkboxes are checked
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   648
 *  v == ['B1', 'B2']
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   649
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   650
 *  var v = $(':radio').fieldValue();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   651
 *  // if neither radio is checked
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   652
 *  v === undefined
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   653
 *  // if first radio is checked
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   654
 *  v == ['C1']
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   655
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   656
 * The successful argument controls whether or not the field element must be 'successful'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   657
 * (per http://www.w3.org/TR/html4/interact/forms.html#successful-controls).
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   658
 * The default value of the successful argument is true.  If this value is false the value(s)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   659
 * for each element is returned.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   660
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   661
 * Note: This method *always* returns an array.  If no valid value can be determined the
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   662
 *       array will be empty, otherwise it will contain one or more values.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   663
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   664
 * @example var data = $("#myPasswordElement").fieldValue();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   665
 * alert(data[0]);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   666
 * @desc Alerts the current value of the myPasswordElement element
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   667
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   668
 * @example var data = $("#myForm :input").fieldValue();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   669
 * @desc Get the value(s) of the form elements in myForm
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   670
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   671
 * @example var data = $("#myForm :checkbox").fieldValue();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   672
 * @desc Get the value(s) for the successful checkbox element(s) in the jQuery object.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   673
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   674
 * @example var data = $("#mySingleSelect").fieldValue();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   675
 * @desc Get the value(s) of the select control
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   676
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   677
 * @example var data = $(':text').fieldValue();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   678
 * @desc Get the value(s) of the text input or textarea elements
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   679
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   680
 * @example var data = $("#myMultiSelect").fieldValue();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   681
 * @desc Get the values for the select-multiple control
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   682
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   683
 * @name fieldValue
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   684
 * @param Boolean successful true if only the values for successful controls should be returned (default is true)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   685
 * @type Array<String>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   686
 * @cat Plugins/Form
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   687
 */
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   688
$.fn.fieldValue = function(successful) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   689
    for (var val=[], i=0, max=this.length; i < max; i++) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   690
        var el = this[i];
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   691
        var v = $.fieldValue(el, successful);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   692
        if (v === null || typeof v == 'undefined' || (v.constructor == Array && !v.length))
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   693
            continue;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   694
        v.constructor == Array ? $.merge(val, v) : val.push(v);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   695
    }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   696
    return val;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   697
};
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   698
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   699
/**
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   700
 * Returns the value of the field element.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   701
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   702
 * The successful argument controls whether or not the field element must be 'successful'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   703
 * (per http://www.w3.org/TR/html4/interact/forms.html#successful-controls).
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   704
 * The default value of the successful argument is true.  If the given element is not
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   705
 * successful and the successful arg is not false then the returned value will be null.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   706
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   707
 * Note: If the successful flag is true (default) but the element is not successful, the return will be null
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   708
 * Note: The value returned for a successful select-multiple element will always be an array.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   709
 * Note: If the element has no value the return value will be undefined.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   710
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   711
 * @example var data = jQuery.fieldValue($("#myPasswordElement")[0]);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   712
 * @desc Gets the current value of the myPasswordElement element
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   713
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   714
 * @name fieldValue
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   715
 * @param Element el The DOM element for which the value will be returned
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   716
 * @param Boolean successful true if value returned must be for a successful controls (default is true)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   717
 * @type String or Array<String> or null or undefined
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   718
 * @cat Plugins/Form
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   719
 */
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   720
$.fieldValue = function(el, successful) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   721
    var n = el.name, t = el.type, tag = el.tagName.toLowerCase();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   722
    if (typeof successful == 'undefined') successful = true;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   723
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   724
    if (successful && (!n || el.disabled || t == 'reset' || t == 'button' ||
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   725
        (t == 'checkbox' || t == 'radio') && !el.checked ||
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   726
        (t == 'submit' || t == 'image') && el.form && el.form.clk != el ||
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   727
        tag == 'select' && el.selectedIndex == -1))
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   728
            return null;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   729
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   730
    if (tag == 'select') {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   731
        var index = el.selectedIndex;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   732
        if (index < 0) return null;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   733
        var a = [], ops = el.options;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   734
        var one = (t == 'select-one');
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   735
        var max = (one ? index+1 : ops.length);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   736
        for(var i=(one ? index : 0); i < max; i++) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   737
            var op = ops[i];
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   738
            if (op.selected) {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   739
                // extra pain for IE...
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   740
                var v = $.browser.msie && !(op.attributes['value'].specified) ? op.text : op.value;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   741
                if (one) return v;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   742
                a.push(v);
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   743
            }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   744
        }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   745
        return a;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   746
    }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   747
    return el.value;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   748
};
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   749
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   750
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   751
/**
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   752
 * Clears the form data.  Takes the following actions on the form's input fields:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   753
 *  - input text fields will have their 'value' property set to the empty string
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   754
 *  - select elements will have their 'selectedIndex' property set to -1
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   755
 *  - checkbox and radio inputs will have their 'checked' property set to false
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   756
 *  - inputs of type submit, button, reset, and hidden will *not* be effected
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   757
 *  - button elements will *not* be effected
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   758
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   759
 * @example $('form').clearForm();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   760
 * @desc Clears all forms on the page.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   761
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   762
 * @name clearForm
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   763
 * @type jQuery
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   764
 * @cat Plugins/Form
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   765
 */
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   766
$.fn.clearForm = function() {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   767
    return this.each(function() {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   768
        $('input,select,textarea', this).clearFields();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   769
    });
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   770
};
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   771
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   772
/**
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   773
 * Clears the selected form elements.  Takes the following actions on the matched elements:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   774
 *  - input text fields will have their 'value' property set to the empty string
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   775
 *  - select elements will have their 'selectedIndex' property set to -1
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   776
 *  - checkbox and radio inputs will have their 'checked' property set to false
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   777
 *  - inputs of type submit, button, reset, and hidden will *not* be effected
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   778
 *  - button elements will *not* be effected
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   779
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   780
 * @example $('.myInputs').clearFields();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   781
 * @desc Clears all inputs with class myInputs
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   782
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   783
 * @name clearFields
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   784
 * @type jQuery
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   785
 * @cat Plugins/Form
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   786
 */
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   787
$.fn.clearFields = $.fn.clearInputs = function() {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   788
    return this.each(function() {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   789
        var t = this.type, tag = this.tagName.toLowerCase();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   790
        if (t == 'text' || t == 'password' || tag == 'textarea')
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   791
            this.value = '';
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   792
        else if (t == 'checkbox' || t == 'radio')
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   793
            this.checked = false;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   794
        else if (tag == 'select')
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   795
            this.selectedIndex = -1;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   796
    });
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   797
};
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   798
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   799
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   800
/**
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   801
 * Resets the form data.  Causes all form elements to be reset to their original value.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   802
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   803
 * @example $('form').resetForm();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   804
 * @desc Resets all forms on the page.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   805
 *
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   806
 * @name resetForm
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   807
 * @type jQuery
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   808
 * @cat Plugins/Form
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   809
 */
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   810
$.fn.resetForm = function() {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   811
    return this.each(function() {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   812
        // guard against an input with the name of 'reset'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   813
        // note that IE reports the reset function as an 'object'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   814
        if (typeof this.reset == 'function' || (typeof this.reset == 'object' && !this.reset.nodeType))
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   815
            this.reset();
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   816
    });
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   817
};
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   818
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   819
})(jQuery);