3 // http://jquery.com/docs/ajax/
5 if ( typeof XMLHttpRequest == 'undefined' && typeof window.ActiveXObject == 'function') {
6 XMLHttpRequest = function() {
7 return new ActiveXObject((navigator.userAgent.toLowerCase().indexOf('msie 5') >= 0) ?
8 "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP");
12 // Counter for holding the active query's
15 $.xml = function( type, url, data, ret ) {
17 ret = type.onComplete;
18 var onSuccess = type.onSuccess;
19 var onError = type.onError;
25 var xml = new XMLHttpRequest();
29 xml.open(type || "GET", url, true);
31 xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
33 // Set header so calling script knows that it's an XMLHttpRequest
34 xml.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
36 /* Borrowed from Prototype:
37 * Force "Connection: close" for Mozilla browsers to work around
38 * a bug where XMLHttpReqeuest sends an incorrect Content-length
39 * header. See Mozilla Bugzilla #246651.
41 if ( xml.overrideMimeType )
42 xml.setRequestHeader('Connection', 'close');
44 xml.onreadystatechange = function() {
46 if ( xml.readyState == 1 ) {
50 // Show loader if needed
51 if ( ($.xmlActive >= 1) && ($.xmlCreate) )
55 // Socket is closed and data is available
56 if ( xml.readyState == 4 ) {
60 // Hide loader if needed
61 if ( ($.xmlActive <= 0) && ($.xmlDestroy) ) {
66 if ( xml.status && xml.status >= 200 && xml.status < 300 ) {
69 } else if ( onError ) {
83 $.httpData = function(r,type) {
84 return r.getResponseHeader("content-type").indexOf("xml") > 0 || type == "xml" ?
85 r.responseXML : r.responseText;
88 $.get = function( url, ret, type ) {
89 $.xml( "GET", url, null, function(r) {
90 if ( ret ) { ret( $.httpData(r,type) ); }
94 $.getXML = function( url, ret ) {
95 $.get( url, ret, "xml" );
98 $.post = function( url, data, ret, type ) {
99 $.xml( "POST", url, $.param(data), function(r) {
100 if ( ret ) { ret( $.httpData(r,type) ); }
104 $.postXML = function( url, data, ret ) {
105 $.post( url, data, ret, "xml" );
108 $.param = function(a) {
110 if (a && typeof a == 'object' && a.constructor == Array) {
111 for ( var i=0; i < a.length; i++ ) {
112 s[s.length] = a[i].name + "=" + encodeURIComponent( a[i].value );
116 s[s.length] = j + "=" + encodeURIComponent( a[j] );
122 $.fn.load = function(a,o,f) {
124 // I overwrote the event plugin's .load
125 // this won't happen again, I hope -John
126 if ( a && a.constructor == Function ) {
127 return this.bind("load", a);
131 if ( o && o.constructor == Function ) {
135 if (typeof o !== 'undefined') {
140 $.xml(t,a,o,function(res){
141 // Assign it and execute all scripts
142 self.html(res.responseText).find("script").each(function(){
143 try { eval( this.text || this.textContent || this.innerHTML || ""); } catch(e){}
147 if (f && f.constructor == Function)
154 * Initial frontend function to submit form variables. This function
155 * is for registering coordinates, in the case of an image being used
156 * as the submit element, and sets up an event to listen and wait for
157 * a form submit click. It then calls any following chained functions
158 * to actually gather the variables and submit them.
160 * Usage examples, when used with getForm().putForm():
162 * 1. Just eval the results returned from the backend.
163 * $('#form-id').form();
165 * 2. Render backend results directly to target ID (expects (x)HTML).
166 * $('#form-id').form('#target-id');
168 * 3. Submit to backend URL (form action) then call this function.
169 * $('#form-id').form(post_callback);
171 * 4. Load target ID with backend results then call a function.
172 * $('#form-id').form('#target-id', null, post_callback);
174 * 5. Call a browser function (for validation) and then (optionally)
175 * load server results to target ID.
176 * $('#form-id').form('#target-id', pre_callback);
178 * 6. Call validation function first then load server results to
179 * target ID and then also call a browser function.
180 * $('#form-id').form('#target-id', pre_callback, post_callback);
182 * @param target arg for the target id element to render
183 * @param pre_cb callback function before submission
184 * @param post_cb callback after any results are returned
185 * @return "this" object
186 * @see getForm(), putForm()
187 * @author Mark Constable (markc@renta.net)
188 * @author G. vd Hoven, Mike Alsup, Sam Collett
191 $.fn.form = function(target, pre_cb, post_cb) {
192 $('input[@type="submit"],input[@type="image"]', this).click(function(ev){
193 this.form.clicked = this;
194 if (ev.offsetX != undefined) {
195 this.form.clicked_x = ev.offsetX;
196 this.form.clicked_y = ev.offsetY;
198 this.form.clicked_x = ev.pageX - this.offsetLeft;
199 this.form.clicked_y = ev.pageY - this.offsetTop;
202 this.submit(function(e){
204 $(this).getForm().putForm(target, pre_cb, post_cb);
210 * This function gathers form element variables into an array that
211 * is embedded into the current "this" variable as "this.vars". It
212 * is normally used in conjunction with form() and putForm() but can
213 * be used standalone as long as an image is not used for submission.
215 * Standalone usage examples:
217 * 1. Gather form vars and return array to LHS variable.
218 * var myform = $('#form-id').getForm();
220 * 2. Provide a serialized URL-ready string (after 1. above).
221 * var mystring = $.param(myform.vars);
223 * 3. Gather form vars and send to RHS plugin via "this.vars".
224 * $('#form-id').getForm().some_other_plugin();
226 * @return "this" object
227 * @see form(), putForm()
228 * @author Mark Constable (markc@renta.net)
229 * @author G. vd Hoven, Mike Alsup, Sam Collett
232 $.fn.getForm = function() {
234 var ok = {INPUT:true, TEXTAREA:true, OPTION:true};
235 $('*', this).each(function() {
236 if (this.disabled || this.type == 'reset' || (this.type == 'checkbox' && !this.checked) || (this.type == 'radio' && !this.checked))
239 if (this.type == 'submit' || this.type == 'image') {
240 if (this.form.clicked != this)
243 if (this.type == 'image') {
244 if (this.form.clicked_x) {
245 a.push({name: this.name+'_x', value: this.form.clicked_x});
246 a.push({name: this.name+'_y', value: this.form.clicked_y});
252 if (!ok[this.nodeName.toUpperCase()])
255 var par = this.parentNode;
256 var p = par.nodeName.toUpperCase();
257 if ((p == 'SELECT' || p == 'OPTGROUP') && !this.selected)
260 var n = this.name || par.name;
261 if (!n && p == 'OPTGROUP' && (par = par.parentNode))
267 a.push({name: n, value: this.value});
276 * Final form submission plugin usually used in conjunction with
277 * form() and getForm(). If a second argument is a valid function
278 * then it will be called before the form vars are sent to the
279 * backend. If this pre-submit function returns exactly "false"
280 * then it will abort further processing otherwise the process
281 * will continue according to the first and third arguments.
283 * If the first argument is a function, and it exists, then the form
284 * values will be submitted and that callback function called. If
285 * the first argument is a string value then the "load()" plugin
286 * will be called which will populate the innerHTML of the indicated
287 * element and a callback will be called if there is third argument.
288 * If there are no arguments then the form values are submitted with
289 * an additional variable (evaljs=1) which indicates to the backend
290 * to to prepare the returned results for evaluation, ie; the result
291 * needs to be valid javascript all on a single line.
295 * $.fn.myvars = function() {
297 * for (var i in this) {
298 * if (this[i] instanceof Function || this[i] == null) continue;
299 * this.vars.push({name: i, value: this[i].length});
304 * precb = function(vars) {
305 * return confirm('Submit these values?\n\n'+$.param(vars));
308 * $('*').myvars().putForm('#mytarget',precb,null,'myhandler.php');
310 * @param target arg for the target id element to render
311 * @param pre_cb callback function before submission
312 * @param post_cb callback after any results are returned
313 * @param url form action override
314 * @param mth form method override
315 * @return "this" object
316 * @see form(), getForm(), load(), xml()
317 * @author Mark Constable (markc@renta.net)
318 * @author G. vd Hoven, Mike Alsup, Sam Collett
321 $.fn.putForm = function(target, pre_cb, post_cb, url, mth) {
322 if (pre_cb && pre_cb.constructor == Function)
323 if (pre_cb(this.vars) === false)
327 var url = url || f.action || '';
328 var mth = mth || f.method || 'POST';
330 if (target && target.constructor == Function) {
331 $.xml(mth, url, $.param(this.vars), target);
332 } else if (target && target.constructor == String) {
333 $(target).load(url, this.vars, post_cb);
335 this.vars.push({name: 'evaljs', value: 1});
336 $.xml(mth, url, $.param(this.vars), function(r) { eval(r.responseText); });