/******************************************** * Author : G. Jongerius Version: 0.1 * Filename: ajax.js * Package : com.jsoft * * Description: * This Javascript file is designed for use in the com.jsoft web applications. * Copyright (C) 2007 Jong-Soft */ function Ajax(url) { this.url = url; this.isCallback = false; this.isConnected = false; this.isPending = false; this.isAsynchronize = false; this.useCallback = false; this.xmlHttp = false; this.protocal = "GET"; } Ajax.prototype.oncalldone = function (responseText) { this.response = responseText; }; Ajax.prototype.setUrl = function(url) { this.url = url; }; Ajax.prototype.getResponse = function () { if (this.isConnected) return this.xmlHttp.responseText; return ""; }; Ajax.prototype.setCallbackFunction = function(callBackFunction, callBackParams) { this.callBackFunction = callBackFunction; this.callBackParams = callBackParams; this.useCallback = true; }; Ajax.prototype.setAsynchronize = function (flag) { if (flag==true) this.isAsynchronize = true; else this.isAsynchronize = false; }; /** * Create a call to the preset url with all the settings known to the object * pending on if the call is synchronize or not it will wait in this function. */ Ajax.prototype.callUrl = function () { if (!this.isPending) this.createAjaxObject(); var self = this; this.xmlHttp.open(this.protocal, this.url, this.isAsynchronize); this.xmlHttp.onreadystatechange = function() { if (self.xmlHttp.readyState == 4) { self.callDone(); } } this.isConnected = true; this.xmlHttp.send(null); if (this.isAsynchronize) this.callDone(); }; /** * Make a call to the webserver to preload information needed later on * in the website. * * This is always asynchronize. */ Ajax.prototype.preLoad = function (storeIn) { var storeObject = null; try { storeObject = (storeIn instanceof HTMLElement) ? storeIn : document.getElementById(storeIn); } catch (err) { storeObject = (storeIn.nodeType && storeIn.nodeType === 1) ? storeIn : document.getElementById(storeIn); } this.oncalldone = function (responseContent) { storeObject.innerHTML = responseContent; } this.setAsynchronize(true); this.callUrl(); }; /** * Function that is called automatically when the call to the web server * is done. This function will either call the callback function set * or raise the internal event. */ Ajax.prototype.callDone = function() { if (!this.isConnected) return; if (this.xmlHttp.status != 200) this.xmlHttp.responseText = 'An internal server error occured. Please contact the administrator.'; if (this.useCallback) eval(this.callBackFunction +"('"+ escape(this.xmlHttp.responseText) +"')"); else this.oncalldone(this.xmlHttp.responseText); }; /** * Create the ajax object needed to make the Ajax calls to the webserver. * If this function fails no Ajax will be supported for the website. */ Ajax.prototype.createAjaxObject = function() { if (!this.xmlHttp) { try { this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { this.xmlHttp = false; } } if (!this.xmlHttp && typeof XMLHttpRequest!='undefined') { this.xmlHttp = new XMLHttpRequest(); } } if (this.xmlHttp) this.isPending = true; }; /** * Submit the given form to the given url */ function doAjaxFormSubmit(formName, url, callBackFunction, callBackParams) { var oForm = document.getElementById(formName); for (i = 0; i < oForm.elements.length; i++) { if (url.indexOf('?') == -1) url += "?"; else url += "&"; url += oForm.elements[i].name + "=" + oForm.elements[i].value; } var response = ajaxCall(url); if (response.length > 0) { if (response.indexOf('http') == 0) { document.location.href = response; } else { eval(callBackFunction +"(" + callBackParams +",'"+ escape(response) +"')"); } } }