//Examples and documentation at: http://malsup.com/jquery/form/
(function($) { $.fn.ajaxSubmit = function(u) { if (!this.length) { log('ajaxSubmit: skipping submit process - no element selected'); return this } if (typeof u == 'function') u = { success: u }; u = $.extend({ url: this.attr('action') || window.location.toString(), type: this.attr('method') || 'GET' }, u || {}); var v = {}; this.trigger('form-pre-serialize', [this, u, v]); if (v.veto) { log('ajaxSubmit: submit vetoed via form-pre-serialize trigger'); return this } var a = this.formToArray(u.semantic); if (u.data) { u.extraData = u.data; for (var n in u.data) a.push({ name: n, value: u.data[n] }) } if (u.beforeSubmit && u.beforeSubmit(a, this, u) === false) { log('ajaxSubmit: submit aborted via beforeSubmit callback'); return this } this.trigger('form-submit-validate', [a, this, u, v]); if (v.veto) { log('ajaxSubmit: submit vetoed via form-submit-validate trigger'); return this } var q = $.param(a); if (u.type.toUpperCase() == 'GET') { u.url += (u.url.indexOf('?') >= 0 ? '&' : '?') + q; u.data = null } else u.data = q; var w = this, callbacks = []; if (u.resetForm) callbacks.push(function() { w.resetForm() }); if (u.clearForm) callbacks.push(function() { w.clearForm() }); if (!u.dataType && u.target) { var x = u.success || function() { }; callbacks.push(function(a) { $(u.target).html(a).each(x, arguments) }) } else if (u.success) callbacks.push(u.success); u.success = function(a, b) { for (var i = 0, max = callbacks.length; i < max; i++) callbacks[i](a, b, w) }; var y = $('input:file', this).fieldValue(); var z = false; for (var j = 0; j < y.length; j++) if (y[j]) z = true; if (u.iframe || z) { if ($.browser.safari && u.closeKeepAlive) $.get(u.closeKeepAlive, fileUpload); else fileUpload() } else $.ajax(u); this.trigger('form-submit-notify', [this, u]); return this; function fileUpload() { var i = w[0]; if ($(':input[@name=submit]', i).length) { alert('Error: Form elements must not be named "submit".'); return } var j = $.extend({}, $.ajaxSettings, u); var k = 'jqFormIO' + (new Date().getTime()); var l = $('<iframe id="' + k + '" name="' + k + '" />'); var m = l[0]; if ($.browser.msie || $.browser.opera) m.src = 'javascript:false;document.write("");'; l.css({ position: 'absolute', top: '-1000px', left: '-1000px' }); var o = { responseText: null, responseXML: null, status: 0, statusText: 'n/a', getAllResponseHeaders: function() { }, getResponseHeader: function() { }, setRequestHeader: function() { } }; var g = j.global; if (g && !$.active++) $.event.trigger("ajaxStart"); if (g) $.event.trigger("ajaxSend", [o, j]); var p = 0; var q = 0; var r = i.clk; if (r) { var n = r.name; if (n && !r.disabled) { u.extraData = u.extraData || {}; u.extraData[n] = r.value; if (r.type == "image") { u.extraData[name + '.x'] = i.clk_x; u.extraData[name + '.y'] = i.clk_y } } } setTimeout(function() { var t = w.attr('target'), a = w.attr('action'); w.attr({ target: k, encoding: 'multipart/form-data', enctype: 'multipart/form-data', method: 'POST', action: j.url }); if (j.timeout) setTimeout(function() { q = true; cb() }, j.timeout); var b = []; try { if (u.extraData) for (var n in u.extraData) b.push($('<input type="hidden" name="' + n + '" value="' + u.extraData[n] + '" />').appendTo(i)[0]); l.appendTo('body'); m.attachEvent ? m.attachEvent('onload', cb) : m.addEventListener('load', cb, false); i.submit() } finally { w.attr('action', a); t ? w.attr('target', t) : w.removeAttr('target'); $(b).remove() } }, 10); function cb() { if (p++) return; m.detachEvent ? m.detachEvent('onload', cb) : m.removeEventListener('load', cb, false); var c = 0; var d = true; try { if (q) throw 'timeout'; var f, doc; doc = m.contentWindow ? m.contentWindow.document : m.contentDocument ? m.contentDocument : m.document; if (doc.body == null && !c && $.browser.opera) { c = 1; p--; setTimeout(cb, 100); return } o.responseText = doc.body ? doc.body.innerHTML : null; o.responseXML = doc.XMLDocument ? doc.XMLDocument : doc; o.getResponseHeader = function(a) { var b = { 'content-type': j.dataType }; return b[a] }; if (j.dataType == 'json' || j.dataType == 'script') { var h = doc.getElementsByTagName('textarea')[0]; o.responseText = h ? h.value : o.responseText } else if (j.dataType == 'xml' && !o.responseXML && o.responseText != null) { o.responseXML = toXml(o.responseText) } f = $.httpData(o, j.dataType) } catch (e) { d = false; $.handleError(j, o, 'error', e) } if (d) { j.success(f, 'success'); if (g) $.event.trigger("ajaxSuccess", [o, j]) } if (g) $.event.trigger("ajaxComplete", [o, j]); if (g && ! --$.active) $.event.trigger("ajaxStop"); if (j.complete) j.complete(o, d ? 'success' : 'error'); setTimeout(function() { l.remove(); o.responseXML = null }, 100) }; function toXml(s, a) { if (window.ActiveXObject) { a = new ActiveXObject('Microsoft.XMLDOM'); a.async = 'false'; a.loadXML(s) } else a = (new DOMParser()).parseFromString(s, 'text/xml'); return (a && a.documentElement && a.documentElement.tagName != 'parsererror') ? a : null } } }; $.fn.ajaxForm = function(c) { return this.ajaxFormUnbind().bind('submit.form-plugin', function() { $(this).ajaxSubmit(c); return false }).each(function() { $(":submit,input:image", this).bind('click.form-plugin', function(e) { var a = this.form; a.clk = this; if (this.type == 'image') { if (e.offsetX != undefined) { a.clk_x = e.offsetX; a.clk_y = e.offsetY } else if (typeof $.fn.offset == 'function') { var b = $(this).offset(); a.clk_x = e.pageX - b.left; a.clk_y = e.pageY - b.top } else { a.clk_x = e.pageX - this.offsetLeft; a.clk_y = e.pageY - this.offsetTop } } setTimeout(function() { a.clk = a.clk_x = a.clk_y = null }, 10) }) }) }; $.fn.ajaxFormUnbind = function() { this.unbind('submit.form-plugin'); return this.each(function() { $(":submit,input:image", this).unbind('click.form-plugin') }) }; $.fn.formToArray = function(b) { var a = []; if (this.length == 0) return a; var c = this[0]; var d = b ? c.getElementsByTagName('*') : c.elements; if (!d) return a; for (var i = 0, max = d.length; i < max; i++) { var e = d[i]; var n = e.name; if (!n) continue; if (b && c.clk && e.type == "image") { if (!e.disabled && c.clk == e) a.push({ name: n + '.x', value: c.clk_x }, { name: n + '.y', value: c.clk_y }); continue } var v = $.fieldValue(e, true); if (v && v.constructor == Array) { for (var j = 0, jmax = v.length; j < jmax; j++) a.push({ name: n, value: v[j] }) } else if (v !== null && typeof v != 'undefined') a.push({ name: n, value: v }) } if (!b && c.clk) { var f = c.getElementsByTagName("input"); for (var i = 0, max = f.length; i < max; i++) { var g = f[i]; var n = g.name; if (n && !g.disabled && g.type == "image" && c.clk == g) a.push({ name: n + '.x', value: c.clk_x }, { name: n + '.y', value: c.clk_y }) } } return a }; $.fn.formSerialize = function(a) { return $.param(this.formToArray(a)) }; $.fn.fieldSerialize = function(b) { var a = []; this.each(function() { var n = this.name; if (!n) return; var v = $.fieldValue(this, b); if (v && v.constructor == Array) { for (var i = 0, max = v.length; i < max; i++) a.push({ name: n, value: v[i] }) } else if (v !== null && typeof v != 'undefined') a.push({ name: this.name, value: v }) }); return $.param(a) }; $.fn.fieldValue = function(a) { for (var b = [], i = 0, max = this.length; i < max; i++) { var c = this[i]; var v = $.fieldValue(c, a); if (v === null || typeof v == 'undefined' || (v.constructor == Array && !v.length)) continue; v.constructor == Array ? $.merge(b, v) : b.push(v) } return b }; $.fieldValue = function(b, c) { var n = b.name, t = b.type, tag = b.tagName.toLowerCase(); if (typeof c == 'undefined') c = true; if (c && (!n || b.disabled || t == 'reset' || t == 'button' || (t == 'checkbox' || t == 'radio') && !b.checked || (t == 'submit' || t == 'image') && b.form && b.form.clk != b || tag == 'select' && b.selectedIndex == -1)) return null; if (tag == 'select') { var d = b.selectedIndex; if (d < 0) return null; var a = [], ops = b.options; var e = (t == 'select-one'); var f = (e ? d + 1 : ops.length); for (var i = (e ? d : 0); i < f; i++) { var g = ops[i]; if (g.selected) { var v = $.browser.msie && !(g.attributes['value'].specified) ? g.text : g.value; if (e) return v; a.push(v) } } return a } return b.value }; $.fn.clearForm = function() { return this.each(function() { $('input,select,textarea', this).clearFields() }) }; $.fn.clearFields = $.fn.clearInputs = function() { return this.each(function() { var t = this.type, tag = this.tagName.toLowerCase(); if (t == 'text' || t == 'password' || tag == 'textarea') this.value = ''; else if (t == 'checkbox' || t == 'radio') this.checked = false; else if (tag == 'select') this.selectedIndex = -1 }) }; $.fn.resetForm = function() { return this.each(function() { if (typeof this.reset == 'function' || (typeof this.reset == 'object' && !this.reset.nodeType)) this.reset() }) }; $.fn.enable = function(b) { if (b == undefined) b = true; return this.each(function() { this.disabled = !b }) }; $.fn.select = function(b) { if (b == undefined) b = true; return this.each(function() { var t = this.type; if (t == 'checkbox' || t == 'radio') this.checked = b; else if (this.tagName.toLowerCase() == 'option') { var a = $(this).parent('select'); if (b && a[0] && a[0].type == 'select-one') { a.find('option').select(false) } this.selected = b } }) }; function log() { if ($.fn.ajaxSubmit.debug && window.console && window.console.log) window.console.log('[jquery.form] ' + Array.prototype.join.call(arguments, '')) } })(jQuery);