1/* xajax Javascript library :: version 0.5 (Beta 1) */ 2var xajax={callOptions:{method:'post'}, 3workId:'xajaxWork'+new Date().getTime(), 4depth:0, 5eventFunctions:{globalRequestDelay:null, 6globalRequestComplete:null 7}, 8delayEventTime:400, 9delayTimer:null, 10responseErrorsForAlert:["400","401","402","403","404","500","501","502","503"], 11DebugMessage:function(text){if(text.length > 1000)text=text.substr(0,1000)+"...\n[long response]\n...";try{if(this.debugWindow==undefined||this.debugWindow.closed==true){this.debugWindow=window.open('about:blank','xajax_debug_'+this.workId,'width=800,height=600,scrollbars=yes,resizable=yes,status=yes');this.debugWindow.document.write('<html><head><title>xajax debug output</title></head><body><h2>xajax debug output</h2><div id="debugTag"></div></body></html>');} 12text=text.replace(/&/g,"&") 13text=text.replace(/</g,"<") 14text=text.replace(/>/g,">") 15debugTag=this.debugWindow.document.getElementById('debugTag');debugTag.innerHTML=('<b>'+(new Date()).toString()+'</b>: '+text+'<hr/>')+debugTag.innerHTML;}catch(e){alert("xajax Debug:\n "+text);} 16}, 17isEventFunction:function(mFunction){if(mFunction&&typeof mFunction=="function")return true;return false;}, 18getRequestObject:function(){if(xajaxConfig.debug)this.DebugMessage("Initializing Request Object..");var req=null;if(typeof XMLHttpRequest!="undefined") 19req=new XMLHttpRequest();if(!req&&typeof ActiveXObject!="undefined"){try{req=new ActiveXObject("Msxml2.XMLHTTP");} 20catch(e){try{req=new ActiveXObject("Microsoft.XMLHTTP");} 21catch(e2){try{req=new ActiveXObject("Msxml2.XMLHTTP.4.0");} 22catch(e3){req=null;} 23} 24} 25} 26if(!req&&window.createRequest) 27req=window.createRequest();if(!req)this.DebugMessage("Request Object Instantiation failed.");return req;}, 28$:function(sId){if(!sId){return null;} 29var returnObj=document.getElementById(sId);if(!returnObj&&document.all){returnObj=document.all[sId];} 30if(xajaxConfig.debug&&!returnObj){this.DebugMessage("Element with the id \""+sId+"\" not found.");} 31return returnObj;}, 32include:function(sFileName){var objHead=document.getElementsByTagName('head');var objScript=document.createElement('script');objScript.type='text/javascript';objScript.src=sFileName;objHead[0].appendChild(objScript);}, 33includeOnce:function(sFileName){var loadedScripts=document.getElementsByTagName('script');for(var i=0;i < loadedScripts.length;i++){if(loadedScripts[i].src&&loadedScripts[i].src.indexOf(sFileName)==0) 34return;} 35return this.include(sFileName);}, 36addCSS:function(sFileName){var objHead=document.getElementsByTagName('head');var objCSS=document.createElement('link');objCSS.rel='stylesheet';objCSS.type='text/css';objCSS.href=sFileName;objHead[0].appendChild(objCSS);}, 37stripOnPrefix:function(sEventName){sEventName=sEventName.toLowerCase();if(sEventName.indexOf('on')==0){sEventName=sEventName.replace(/on/,'');} 38return sEventName;}, 39addOnPrefix:function(sEventName){sEventName=sEventName.toLowerCase();if(sEventName.indexOf('on')!=0){sEventName='on'+sEventName;} 40return sEventName;}, 41addHandler:function(sElementId,sEvent,sFunctionName){if(window.addEventListener){sEvent=this.stripOnPrefix(sEvent);eval("this.$('"+sElementId+"').addEventListener('"+sEvent+"',"+sFunctionName+",false);");} 42else{sAltEvent=this.addOnPrefix(sEvent);eval("this.$('"+sElementId+"').attachEvent('"+sAltEvent+"',"+sFunctionName+",false);");} 43}, 44removeHandler:function(sElementId,sEvent,sFunctionName){if(window.addEventListener){sEvent=this.stripOnPrefix(sEvent);eval("this.$('"+sElementId+"').removeEventListener('"+sEvent+"',"+sFunctionName+",false);");} 45else{sAltEvent=this.addOnPrefix(sEvent);eval("this.$('"+sElementId+"').detachEvent('"+sAltEvent+"',"+sFunctionName+",false);");} 46}, 47create:function(sParentId,sTag,sId){var objParent=this.$(sParentId);objElement=document.createElement(sTag);objElement.setAttribute('id',sId);if(objParent) 48objParent.appendChild(objElement);}, 49insert:function(sBeforeId,sTag,sId){var objSibling=this.$(sBeforeId);objElement=document.createElement(sTag);objElement.setAttribute('id',sId);objSibling.parentNode.insertBefore(objElement,objSibling);}, 50insertAfter:function(sAfterId,sTag,sId){var objSibling=this.$(sAfterId);objElement=document.createElement(sTag);objElement.setAttribute('id',sId);objSibling.parentNode.insertBefore(objElement,objSibling.nextSibling);}, 51getInput:function(sType,sName,sId){var Obj;if(!window.addEventListener){Obj=document.createElement('<input type="'+sType+'" id="'+sId+'" name="'+sName+'">');} 52else{Obj=document.createElement('input');Obj.setAttribute('type',sType);Obj.setAttribute('name',sName);Obj.setAttribute('id',sId);} 53return Obj;}, 54createInput:function(sParentId,sType,sName,sId){var objParent=this.$(sParentId);var objElement=this.getInput(sType,sName,sId);if(objParent&&objElement) 55objParent.appendChild(objElement);}, 56insertInput:function(sBeforeId,sType,sName,sId){var objSibling=this.$(sBeforeId);var objElement=this.getInput(sType,sName,sId);if(objElement&&objSibling&&objSibling.parentNode) 57objSibling.parentNode.insertBefore(objElement,objSibling);}, 58insertInputAfter:function(sAfterId,sType,sName,sId){var objSibling=this.$(sAfterId);var objElement=this.getInput(sType,sName,sId);if(objElement&&objSibling&&objSibling.parentNode){objSibling.parentNode.insertBefore(objElement,objSibling.nextSibling);} 59}, 60remove:function(sId){objElement=this.$(sId);if(objElement&&objElement.parentNode&&objElement.parentNode.removeChild){objElement.parentNode.removeChild(objElement);} 61}, 62replace:function(sId,sAttribute,sSearch,sReplace){var bFunction=false;if(sAttribute=="innerHTML") 63sSearch=this.getBrowserHTML(sSearch);eval("var txt=this.$('"+sId+"')."+sAttribute);if(typeof txt=="function"){txt=txt.toString();bFunction=true;} 64if(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);} 65newTxt+=txt;if(bFunction){eval('this.$("'+sId+'").'+sAttribute+'=newTxt;');} 66else if(this.willChange(sId,sAttribute,newTxt)){eval('this.$("'+sId+'").'+sAttribute+'=newTxt;');} 67} 68}, 69getFormValues:function(frm){var objForm;var submitDisabledElements=false;if(arguments.length > 1&&arguments[1]==true) 70submitDisabledElements=true;var prefix="";if(arguments.length > 2) 71prefix=arguments[2];if(typeof(frm)=="string") 72objForm=this.$(frm);else 73objForm=frm;var sXml="<xjxquery><q>";if(objForm&&objForm.tagName&&objForm.tagName.toUpperCase()=='FORM'){var formElements=objForm.elements;for(var i=0;i < formElements.length;i++){if(!formElements[i].name) 74continue;if(formElements[i].name.substring(0,prefix.length)!=prefix) 75continue;if(formElements[i].type&&(formElements[i].type=='radio'||formElements[i].type=='checkbox')&&formElements[i].checked==false) 76continue;if(formElements[i].disabled&&formElements[i].disabled==true&&submitDisabledElements==false) 77continue;var name=formElements[i].name;if(name){if(sXml!='<xjxquery><q>') 78sXml+='&';if(formElements[i].type=='select-multiple'){if(name.substr(name.length-2,2)!='[]') 79name+='[]';for(var j=0;j < formElements[i].length;j++){if(formElements[i].options[j].selected==true) 80sXml+=name+"="+encodeURIComponent(formElements[i].options[j].value)+"&";} 81} 82else{sXml+=name+"="+encodeURIComponent(formElements[i].value);} 83} 84} 85} 86sXml+="</q></xjxquery>";return sXml;}, 87objectToXML:function(obj){var sXml="<xjxobj>";for(i in obj){try{if(i=='constructor') 88continue;if(obj[i]&&typeof(obj[i])=='function') 89continue;var key=i;var value=obj[i];if(value&&typeof(value)=="object"&&this.depth <=50){this.depth++;value=this.objectToXML(value);this.depth--;} 90key=key.replace(/]]>/g,']]]]><![CDATA[>');value=value.replace(/]]>/g,']]]]><![CDATA[>');sXml+="<e><k><![CDATA["+key+"]]></k><v><![CDATA["+value+"]]></v></e>";} 91catch(e){if(xajaxConfig.debug)this.DebugMessage(e.name+": "+e.message);} 92} 93sXml+="</xjxobj>";return sXml;}, 94_nodeToObject:function(node){if(node.nodeName=='#cdata-section'){var data=node.data;while(node=node.nextSibling){data+=node.data;} 95return data;} 96else if(node.nodeName=='xjxobj'){var data=new Array();for(var j=0;j<node.childNodes.length;j++){var child=node.childNodes[j];var key;var value;if(child.nodeName=='e'){for(var k=0;k<child.childNodes.length;k++){if(child.childNodes[k].nodeName=='k'){key=child.childNodes[k].firstChild.data;} 97else if(child.childNodes[k].nodeName=='v'){if(child.childNodes[k].firstChild==null) 98value='';else 99value=this._nodeToObject(child.childNodes[k].firstChild);} 100} 101if(key!=null&&value!=null){data[key]=value;key=value=null;} 102} 103} 104return data;} 105}, 106runDelayEvents:function(){if(this.isEventFunction(this.eventFunctions.globalRequestDelay)){this.eventFunctions.globalRequestDelay();} 107if(this.isEventFunction(this.callOptions.onRequestDelay)){this.callOptions.onRequestDelay();} 108}, 109setCallOptions:function(aOptions){this.callOptions={URI:"", 110parameters:null, 111onRequestDelay:null, 112beforeResponse:null, 113onResponse:null 114};for(optionKey in aOptions){this.callOptions[optionKey]=aOptions[optionKey];} 115}, 116call:function(sFunction,aOptions){var i,r,postData;this.setCallOptions(aOptions);if(document.body&&xajaxConfig.waitCursor) 117document.body.style.cursor='wait';if(xajaxConfig.statusMessages==true)window.status='Sending Request...';if(xajax.loadingFunction!=undefined){xajax.eventFunctions.globalRequestDelay=xajax.loadingFunction;} 118if(xajax.doneLoadingFunction!=undefined){xajax.eventFunctions.globalRequestComplete=xajax.doneLoadingFunction;} 119clearTimeout(this.delayTimer);this.delayTimer=setTimeout("xajax.runDelayEvents()",this.delayEventTime);if(xajaxConfig.debug)this.DebugMessage("Starting xajax...");if(!this.callOptions.method){var requestType="post";} 120else{var requestType=this.callOptions.method;if(requestType!=("get"||"post")){requestType="post";} 121} 122if(this.callOptions.URI){var uri=this.callOptions.URI;} 123else{var uri=xajaxConfig.requestURI;} 124var value;var aArgs=this.callOptions.parameters;postData="xajax="+encodeURIComponent(sFunction);postData+="&xajaxr="+new Date().getTime();if(aArgs){for(i=0;i <aArgs.length;i++){value=aArgs[i];if(typeof(value)=="object") 125value=this.objectToXML(value);postData+="&xajaxargs[]="+encodeURIComponent(value);} 126} 127switch(requestType){case "get":{var uriGet=uri.indexOf("?")==-1?"?":"&";uriGet+=postData;uri+=uriGet;postData=null;}break;case "post":{}break;default: 128alert("Illegal request type: "+requestType);return false;break;} 129r=this.getRequestObject();if(!r)return false;r.open(requestType,uri,true);if(requestType=="post"){try{r.setRequestHeader("Method","POST "+uri+" HTTP/1.1");r.setRequestHeader("Content-Type","application/x-www-form-urlencoded");} 130catch(e){delete r;r=null;aOptions.method='get';return this.call(sFunction,aOptions);} 131}else{r.setRequestHeader("If-Modified-Since","Sat, 1 Jan 2000 00:00:00 GMT");} 132r.onreadystatechange=function(){if(r.readyState!=4) 133return;xajax.readyStateChange(r);delete r;r=null;} 134if(xajaxConfig.debug)this.DebugMessage("Calling "+sFunction+" uri="+uri+" (post:"+postData+")");r.send(postData);if(xajaxConfig.statusMessages==true)window.status='Waiting for data...';delete r;return true;}, 135readyStateChange:function(r){try{if(r.status==0||r.status==200){if(xajaxConfig.debug)xajax.DebugMessage("Received:\n"+r.responseText);if(r.responseXML&&r.responseXML.documentElement) 136this.processResponse(r.responseXML);else if(r.responseText=="") 137this.completeResponse();else{var errorString="Error: the XML response that was returned from the server is invalid.";errorString+="\nReceived:\n"+r.responseText;trimmedResponseText=r.responseText.replace(/^\s+/g,"");if(trimmedResponseText!=r.responseText) 138errorString+="\nYou have whitespace at the beginning of your response.";trimmedResponseText=r.responseText.replace(/\s+$/g,"");if(trimmedResponseText!=r.responseText) 139errorString+="\nYou have whitespace at the end of your response.";alert(errorString);this.completeResponse();if(xajaxConfig.statusMessages==true)window.status='Invalid XML response error';} 140} 141else{if(this.arrayContainsValue(this.responseErrorsForAlert,r.status)){var errorString="Error: the server returned the following HTTP status: "+r.status;errorString+="\nReceived:\n"+r.responseText;alert(errorString);} 142this.completeResponse();if(this.statusMessages==true)window.status='Invalid XML response error';} 143}catch(e){} 144}, 145processResponse:function(xml){clearTimeout(this.delayTimer);if(this.isEventFunction(this.eventFunctions.globalRequestComplete)){this.eventFunctions.globalRequestComplete();} 146if(this.isEventFunction(this.callOptions.beforeResponse)){var eventReturn=this.callOptions.beforeResponse(xml);if(eventReturn==false){this.completeResponse();return;} 147} 148if(xajaxConfig.statusMessages==true)window.status='Processing...';var tmpXajax=null;xml=xml.documentElement;if(xml==null){this.completeResponse();return;} 149var skipCommands=0;for(var i=0;i<xml.childNodes.length;i++){if(skipCommands > 0){skipCommands--;continue;} 150if(xml.childNodes[i].nodeName=="cmd"){var cmd;var id;var property;var data;var search;var type;var before;var objElement=null;for(var j=0;j<xml.childNodes[i].attributes.length;j++){if(xml.childNodes[i].attributes[j].name=="n"){cmd=xml.childNodes[i].attributes[j].value;} 151else if(xml.childNodes[i].attributes[j].name=="t"){id=xml.childNodes[i].attributes[j].value;} 152else if(xml.childNodes[i].attributes[j].name=="p"){property=xml.childNodes[i].attributes[j].value;} 153else if(xml.childNodes[i].attributes[j].name=="c"){type=xml.childNodes[i].attributes[j].value;} 154} 155if(xml.childNodes[i].childNodes.length > 1&&xml.childNodes[i].firstChild.nodeName=="#cdata-section"){data="";for(var j=0;j<xml.childNodes[i].childNodes.length;j++){data+=xml.childNodes[i].childNodes[j].data;} 156} 157else if(xml.childNodes[i].firstChild&&xml.childNodes[i].firstChild.nodeName=='xjxobj'){data=this._nodeToObject(xml.childNodes[i].firstChild);} 158else if(xml.childNodes[i].childNodes.length > 1){for(var j=0;j<xml.childNodes[i].childNodes.length;j++){if(xml.childNodes[i].childNodes[j].childNodes.length > 1&&xml.childNodes[i].childNodes[j].firstChild.nodeName=="#cdata-section"){var internalData="";for(var k=0;k<xml.childNodes[i].childNodes[j].childNodes.length;k++){internalData+=xml.childNodes[i].childNodes[j].childNodes[k].nodeValue;} 159}else{var internalData=xml.childNodes[i].childNodes[j].firstChild.nodeValue;} 160if(xml.childNodes[i].childNodes[j].nodeName=="s"){search=internalData;} 161if(xml.childNodes[i].childNodes[j].nodeName=="r"){data=internalData;} 162} 163} 164else if(xml.childNodes[i].firstChild) 165data=xml.childNodes[i].firstChild.nodeValue;else 166data="";if(cmd!="jc")objElement=this.$(id);var cmdFullname;try{if(cmd=="cc"){cmdFullname="confirmCommands";var confirmResult=confirm(data);if(!confirmResult){skipCommands=id;} 167} 168if(cmd=="al"){cmdFullname="alert";alert(data);} 169else if(cmd=="jc"){cmdFullname="call";var scr=id+'(';if(data[0]!=null){scr+='data[0]';for(var l=1;l<data.length;l++){scr+=',data['+l+']';} 170} 171scr+=');';eval(scr);} 172else if(cmd=="js"){cmdFullname="script/redirect";eval(data);} 173else if(cmd=="in"){cmdFullname="includeScript";this.include(data);} 174else if(cmd=="ino"){cmdFullname="includeScriptOnce";this.includeOnce(data);} 175else if(cmd=="css"){cmdFullname="includeCSS";this.addCSS(data);} 176else if(cmd=="as"){cmdFullname="assign/clear";if(this.willChange(id,property,data)){eval("objElement."+property+"=data;");} 177} 178else if(cmd=="ap"){cmdFullname="append";eval("objElement."+property+"+=data;");} 179else if(cmd=="pp"){cmdFullname="prepend";eval("objElement."+property+"=data+objElement."+property);} 180else if(cmd=="rp"){cmdFullname="replace";this.replace(id,property,search,data) 181} 182else if(cmd=="rm"){cmdFullname="remove";this.remove(id);} 183else if(cmd=="ce"){cmdFullname="create";this.create(id,data,property);} 184else if(cmd=="ie"){cmdFullname="insert";this.insert(id,data,property);} 185else if(cmd=="ia"){cmdFullname="insertAfter";this.insertAfter(id,data,property);} 186else if(cmd=="ci"){cmdFullname="createInput";this.createInput(id,type,data,property);} 187else if(cmd=="ii"){cmdFullname="insertInput";this.insertInput(id,type,data,property);} 188else if(cmd=="iia"){cmdFullname="insertInputAfter";this.insertInputAfter(id,type,data,property);} 189else if(cmd=="ev"){cmdFullname="addEvent";property=this.addOnPrefix(property);eval("this.$('"+id+"')."+property+"= function(){"+data+";}");} 190else if(cmd=="ah"){cmdFullname="addHandler";this.addHandler(id,property,data);} 191else if(cmd=="rh"){cmdFullname="removeHandler";this.removeHandler(id,property,data);} 192} 193catch(e){if(xajaxConfig.debug) 194alert("While trying to '"+cmdFullname+"' (command number "+i+"), the following error occured:\n" 195+e.name+": "+e.message+"\n" 196+(id&&!objElement?"Object with id='"+id+"' wasn't found.\n":""));} 197delete objElement;delete cmd;delete cmdFullname;delete id;delete property;delete search;delete data;delete type;delete before;delete internalData;delete j;delete k;delete l;} 198} 199delete xml;delete i;delete skipCommands;this.completeResponse();if(this.isEventFunction(this.callOptions.onResponse)){this.callOptions.onResponse();} 200}, 201completeResponse:function(){document.body.style.cursor='default';if(xajaxConfig.statusMessages==true)window.status='Done';}, 202getBrowserHTML:function(html){tmpXajax=document.getElementById(this.workId);if(!tmpXajax){tmpXajax=document.createElement("div");tmpXajax.setAttribute('id',this.workId);tmpXajax.style.display="none";tmpXajax.style.visibility="hidden";document.body.appendChild(tmpXajax);} 203tmpXajax.innerHTML=html;var browserHTML=tmpXajax.innerHTML;tmpXajax.innerHTML='';return browserHTML;}, 204willChange:function(element,attribute,newData){if(!document.body){return true;} 205if(attribute=="innerHTML"){newData=this.getBrowserHTML(newData);} 206elementObject=this.$(element);if(elementObject){var oldData;eval("oldData=this.$('"+element+"')."+attribute);if(newData!==oldData) 207return true;} 208return false;}, 209viewSource:function(){return "<html>"+document.getElementsByTagName("HTML")[0].innerHTML+"</html>";}, 210arrayContainsValue:function(array,valueToCheck){for(i=0;i<array.length;i++){if(array[i]==valueToCheck)return true;} 211return false;} 212} 213if(xajaxConfig.legacy){xajax.advancedCall=xajax.call;xajax.call=function(sFunction,aArgs,sRequestType){var options={};if(aArgs!=null){options['parameters']=aArgs;} 214if(sRequestType!=null){options['method']=sRequestType;} 215xajax.advancedCall(sFunction,options);} 216} 217xajaxLoaded=true;