function Xajax() { if (xajaxDebug) this.DebugMessage = function(text) { alert("Xajax Debug:\n " + text) }; this.workId = 'xajaxWork'+ new Date().getTime(); this.depth = 0; //Get the XMLHttpRequest Object this.getRequestObject = function() { if (xajaxDebug) this.DebugMessage("Initializing Request Object.."); var req; try { req=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { req=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e2) { req=null; } } if(!req && typeof XMLHttpRequest != "undefined") req = new XMLHttpRequest(); if (xajaxDebug) { if (!req) this.DebugMessage("Request Object Instantiation failed."); } return req; } // xajax.$() is shorthand for document.getElementById() this.$ = function(sId) { if (!sId) { return null; } var returnObj = document.getElementById(sId); if (xajaxDebug && !returnObj && sId != this.workId) { this.DebugMessage("Element with the id \"" + sId + "\" not found."); } return returnObj; } // xajax.include(sFileName) dynamically includes an external javascript file this.include = function(sFileName) { var objHead = document.getElementsByTagName('head'); var objScript = document.createElement('script'); objScript.type = 'text/javascript'; objScript.src = sFileName; objHead[0].appendChild(objScript); } // xajax.addHandler adds an event handler to an element this.addHandler = function(sElementId, sEvent, sFunctionName) { if (window.addEventListener) { eval("this.$('"+sElementId+"').addEventListener('"+sEvent+"',"+sFunctionName+",false);"); } else { eval("this.$('"+sElementId+"').attachEvent('on"+sEvent+"',"+sFunctionName+",false);"); } } // xajax.removeHandler removes an event handler from an element this.removeHandler = function(sElementId, sEvent, sFunctionName) { if (window.addEventListener) { eval("this.$('"+sElementId+"').removeEventListener('"+sEvent+"',"+sFunctionName+",false);"); } else { eval("this.$('"+sElementId+"').detachEvent('on"+sEvent+"',"+sFunctionName+",false);"); } } // xajax.create creates a new child node under a parent this.create = function(sParentId, sTag, sId) { var objParent = this.$(sParentId); objElement = document.createElement(sTag); objElement.setAttribute('id',sId); objParent.appendChild(objElement); } // xajax.insert inserts a new node before another node this.insert = function(sBeforeId, sTag, sId) { var objSibling = this.$(sBeforeId); objElement = document.createElement(sTag); objElement.setAttribute('id',sId); objSibling.parentNode.insertBefore(objElement, objSibling); } this.getInput = function(sType, sName, sId) { var Obj; if (sType == "radio" && !window.addEventListener) { Obj = document.createElement(''); } else { Obj = document.createElement('input'); Obj.setAttribute('type',sType); Obj.setAttribute('name',sName); Obj.setAttribute('id',sId); } return Obj; } // xajax.createInput creates a new input node under a parent this.createInput = function(sParentId, sType, sName, sId) { var objParent = this.$(sParentId); var objElement = this.getInput(sType, sName, sId); objParent.appendChild(objElement); } // xajax.insertInput creates a new input node before another node this.insertInput = function(sBeforeId, sType, sName, sId) { var objSibling = this.$(sBeforeId); var objElement = this.getInput(sType, sName, sId); objSibling.parentNode.insertBefore(objElement, objSibling); } // xajax.remove deletes an element this.remove = function(sId) { objElement = this.$(sId); if (objElement.parentNode && objElement.parentNode.removeChild) { objElement.parentNode.removeChild(objElement); } } //xajax.replace searches for text in an attribute of an element and replaces it //with a different text this.replace = function(sId,sAttribute,sSearch,sReplace) { var bFunction = false; if (sAttribute == "innerHTML") sSearch = this.getBrowserHTML(sSearch); eval("var txt=document.getElementById('"+sId+"')."+sAttribute); if (typeof txt == "function") { txt = txt.toString(); bFunction = true; } if (txt.indexOf(sSearch)>-1) { var newTxt = ''; while (txt.indexOf(sSearch) > -1) { x = txt.indexOf(sSearch)+sSearch.length+1; newTxt += txt.substr(0,x).replace(sSearch,sReplace); txt = txt.substr(x,txt.length-x); } newTxt += txt; if (bFunction) { eval("newTxt =" + newTxt); eval('this.$("'+sId+'").'+sAttribute+'=newTxt;'); } else if (this.willChange(sId,sAttribute,newTxt)) { eval('this.$("'+sId+'").'+sAttribute+'=newTxt;'); } } } // xajax.getFormValues() builds a query string XML message from the elements of a form object this.getFormValues = function(frm) { var objForm; var submitDisabledElements = false; if (arguments.length > 1 && arguments[1] == true) submitDisabledElements = true; if (typeof(frm) == "string") objForm = this.$(frm); else objForm = frm; var sXml = ""; if (objForm && objForm.tagName == 'FORM') { var formElements = objForm.elements; for( var i=0; i < formElements.length; i++) { if (formElements[i].type && (formElements[i].type == 'radio' || formElements[i].type == 'checkbox') && formElements[i].checked == false) continue; if (formElements[i].disabled && formElements[i].disabled == true && submitDisabledElements == false) continue; var name = formElements[i].name; if (name) { if (sXml != '') sXml += '&'; if(formElements[i].type=='select-multiple') { for (var j = 0; j < formElements[i].length; j++) { if (formElements[i].options[j].selected == true) sXml += name+"="+encodeURIComponent(formElements[i].options[j].value)+"&"; } } else { sXml += name+"="+encodeURIComponent(formElements[i].value); } } } } sXml +=""; return sXml; } // Generates an XML message that xajax can understand from a javascript object this.objectToXML = function(obj) { var sXml = ""; for (i in obj) { try { if (i == 'constructor') continue; if (obj[i] && typeof(obj[i]) == 'function') continue; var key = i; var value = obj[i]; if (value && typeof(value)=="object" && (value.constructor == Array ) && this.depth <= 50) { this.depth++; value = this.objectToXML(value); this.depth--; } sXml += ""+key+""+value+""; } catch(e) { if (xajaxDebug) this.DebugMessage(e); } } sXml += ""; return sXml; } // Sends a XMLHttpRequest to call the specified PHP function on the server // * sRequestType is optional -- defaults to POST this.call = function(sFunction, aArgs, sRequestType) { var i,r,postData; if (document.body && xajaxWaitCursor) document.body.style.cursor = 'wait'; if (xajaxStatusMessages == true) window.status = 'Sending Request...'; if (xajaxDebug) this.DebugMessage("Starting xajax..."); if (sRequestType == null) { var xajaxRequestType = xajaxDefinedPost; } else { var xajaxRequestType = sRequestType; } var uri = xajaxRequestUri; var value; switch(xajaxRequestType) { case xajaxDefinedGet:{ var uriGet = uri.indexOf("?")==-1?"?xajax="+encodeURIComponent(sFunction):"&xajax="+encodeURIComponent(sFunction); if (aArgs) { for (i = 0; i"); if (r.responseXML) xajax.processResponse(r.responseXML); else { alert("Error: the XML response that was returned from the server is invalid."); document.body.style.cursor = 'default'; if (xajaxStatusMessages == true) window.status = 'Invalid XML response error'; } } delete r; } if (xajaxDebug) this.DebugMessage("Calling "+sFunction +" uri="+uri+" (post:"+ postData +")"); r.send(postData); if (xajaxStatusMessages == true) window.status = 'Waiting for data...'; delete r; return true; } //Gets the text as it would be if it were being retrieved from //the innerHTML property in the current browser this.getBrowserHTML = function(html) { tmpXajax = this.$(this.workId); if (tmpXajax == null) { tmpXajax = document.createElement("div"); tmpXajax.setAttribute('id',this.workId); tmpXajax.style.display = "none"; tmpXajax.style.visibility = "hidden"; document.body.appendChild(tmpXajax); } tmpXajax.innerHTML = html; var browserHTML = tmpXajax.innerHTML; tmpXajax.innerHTML = ''; return browserHTML; } // Tests if the new Data is the same as the extant data this.willChange = function(element, attribute, newData) { if (!document.body) { return true; } var oldData; if (attribute == "innerHTML") { newData = this.getBrowserHTML(newData); } eval("oldData=document.getElementById('"+element+"')."+attribute); if (newData != oldData) return true; return false; } //Process XML xajaxResponses returned from the request this.processResponse = function(xml) { if (xajaxStatusMessages == true) window.status = 'Processing...'; var tmpXajax = null; xml = xml.documentElement; if (xml == null) { alert("Error: the XML response that was returned from the server cannot be processed."); document.body.style.cursor = 'default'; if (xajaxStatusMessages == true) window.status = 'XML response processing error'; return; } for (i=0; i 1) { for (j=0; j