/* AJFORM 1.1 - World's Easiest JavaScript AJAX ToolKit ajform.js http://redredmusic.com/brendon/ajform/ Brendon Crawford ** BSD LICENSE ********************************************************* Copyright (c) 2005 Brendon Crawford All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ************************************************************************ */ DOM = new Object; DOM.setEventListener = function( eventName , functionName ) { if( document.addEventListener ) { this.addEventListener( eventName , functionName , false ); } else if( document.attachEvent ) { this.attachEvent( "on" + eventName , functionName ); } } DOM.getSourceElement = function() { if( window.event != undefined ) { return window.event.srcElement; } else { return this; } } window.setEventListener = DOM.setEventListener; AJForm = new Object; AJForm.STATUS = { 'SUCCESS' : 0 , 'HTTP_OBJECT_FAILED' : 1 , 'FILE_UPLOAD_FAILED' : 2 , 'SERVER_ERROR' : 3 }; AJForm.getHTTPRequest = function() { if( typeof window.ActiveXObject != 'undefined' ) { try { doc = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { return false; } } else if( typeof XMLHttpRequest != 'undefined' ) { try { doc = new XMLHttpRequest(); } catch(e) { return false; } } return doc; } AJForm.activateForm = function() { this.getSourceElement = DOM.getSourceElement; thisForm = this.getSourceElement(); activeRetVal = AJForm.submitForm( thisForm ); return activeRetVal; } /* FORM SUBMISSION: submitForm() SCRIPTED SUBMISSION: submitForm( [formObject [, callbackFunction]] ) */ AJForm.submitForm = function( thisElm ){ //if a second argument was specified if( AJForm.submitForm.arguments.length > 1 ) { userFuncName = AJForm.submitForm.arguments[1]; } else { userFuncName = thisElm.getAttribute( 'ajform-callback' ); } if( userFuncName == null ) { return true; } //userFunc is the callback reference userFunc = eval(userFuncName); debugIt = thisElm.getAttribute('ajform-debug'); if( debugIt != null ) { if( debugIt == "1" || debugIt.toLowerCase() == "true" || debugIt.toLowerCase() == "yes" ) { debugIt = true; } } else { debugIt = false; } //no action specified if( (file = thisElm.getAttribute('action')) == null ) { file = new String( window.location ); } //intiialize httpRequest object and see if it failed if( !(doc = AJForm.getHTTPRequest()) || !(docLoader = AJForm.getHTTPRequest()) ) { if( debugIt ) { alert( "Error: Could not initiate HTTPRequest." ); } userFuncVal = userFunc( null , AJForm.STATUS['HTTP_OBJECT_FAILED'] ); return userFuncVal; } dataStr = ""; //construct values for( var e = 0; e < thisElm.elements.length; e++ ) { //we canot yet handle if( thisElm.elements[e].name == '' || thisElm.elements[e].name == 'undefined' ) { continue; } //file upload if( (thisElmType = thisElm.elements[e].getAttribute('type')) != null ) { if( thisElmType.toLowerCase() == "file" ) { //if we encounter a file upload, then bail out if( debugIt ) { alert( "Error: Cannot handle file uploads." ); } userFuncVal = userFunc( null , AJForm.STATUS['FILE_UPLOAD_FAILED'] ); return userFuncVal; } } if( e ) { dataStr += "&"; } dataStr += thisElm.elements[e].name + "=" + escape(thisElm.elements[e].value); } //identify this as being an ajform set dataStr += "&ajform:submit=1"; doc.onreadystatechange = function() { // only if req shows "loaded" if (doc.readyState == 4) { // only if "OK" if (doc.status == 200) { userFunc( doc.responseText , AJForm.STATUS['SUCCESS'] ); } else { if( debugIt ) { alert("Error: " + doc.statusText); } userFunc( doc.responseText , AJForm.STATUS['SERVER_ERROR'] ); } } } requestType = thisElm.getAttribute('method'); requestType = ( requestType == null || requestType == 'undefined' ) ? 'get' : requestType; //METHOD if( requestType.toLowerCase() == "get" ) { file += (file.match(/\?/)) ? ("&" + dataStr) : ("?" + dataStr); doc.open( "GET", file, true ); doc.setRequestHeader( "Content-Type" , "application/x-www-form-urlencoded; charset=UTF-8" ); doc.send(''); } else if( requestType.toLowerCase() == "post" ) { doc.open( "POST", file, true ); doc.setRequestHeader( "Content-Type" , "application/x-www-form-urlencoded; charset=UTF-8" ); doc.send(dataStr); } return false; } AJForm.init = function() { for( var i = 0; i < document.forms.length; i++ ) { document.forms[i].onsubmit = AJForm.activateForm; } } //SET THE LISTENER TO INITIALIZE THE ACTIONS window.setEventListener( 'load' , AJForm.init );