/*-- ------------------------------------------------------ oxsolution JAVASCRIPT functions http://www.oxsolution.com ------------------------------------------------------ --*/ function oxc_RPC(returnType) { this.returnType = returnType || "text"; //header, text, json, xml, body, status, statusText this.utf8Sign = true; //force get method using encodeURI with "&rpcCharset=utf8" this.timeout = 5000; this.busy = false; this.httpConnect = function() { var http; try { http = new XMLHttpRequest(); }catch(e) { try { http = new ActiveXObject("Microsoft.XMLHTTP"); }catch(e) { try { http = new ActiveXObject("Msxml2.XMLHTTP"); }catch(e) { http = false; } } } return http; } this.http = this.httpConnect(); this.returnValue = function(type) { if (type == "header") return this.http.getAllResponseHeaders(); //header as string if (type == "text") return this.http.responseText; //string if (type == "json") return this.code2obj(this.http.responseText); //JSON object if (type == "xml") return this.http.responseXML; //XML DOM object if (type == "body") return this.http.responseBody; //unsined bytes array if (type == "status") return this.http.status; //200 is ok if (type == "statusText") return this.http.statusText; //'OK' is ok return ""; } this.abort = function() { if (this.http) { this.http.abort(); } this.http = this.httpConnect(); this.httpFinish(); } this.httpGet = function(url,func) { if (this.busy) return; if (this.utf8Sign) { url += ((url.indexOf("?") == -1)? "?" : "&")+"rpcCharset=utf8"; url = encodeURI(url); } var aSync = true; if (func == false || func == null) { aSync = false; }else if (typeof func == "function") { this.httpStart(); var that = this; var timer = setTimeout(function() {that.abort()}, this.timeout); this.http.onreadystatechange = function() { if (that.http.readyState == 4 && that.http.status == 200) { if (timer) { clearTimeout(timer); timer = null; } that.httpFinish(); func(that.returnValue(that.returnType)); } }; } this.http.open("GET",url,aSync); this.http.setRequestHeader("Content-Type", "text/xml"); this.http.send(null); if (!aSync) return this.returnValue(this.returnType); } this.httpPost = function(url,obj,func) { if (this.busy) return; if (this.utf8Sign) { url += ((url.indexOf("?") == -1)? "?" : "&")+"rpcCharset=utf8"; url = encodeURI(url); } var aSync = true; if (func == false || func == null) { aSync = false; }else if (typeof func == "function") { this.httpStart(); var that = this; var timer = setTimeout(function() {that.abort()}, this.timeout); this.http.onreadystatechange = function() { if (that.http.readyState == 4 && that.http.status == 200) { if (timer) { clearTimeout(timer); timer = null; } that.httpFinish(); func(that.returnValue(that.returnType)); } }; } this.http.open("POST",url,aSync); this.http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); var buf = ""; if (typeof obj == "object") { for (var key in obj) { buf += "&"+encodeURIComponent(key)+"="+encodeURIComponent(obj[key]); } if (buf != "") buf = buf.substr(1); }else{ buf = encodeURI(obj); } this.http.send(buf); if (!aSync) return this.returnValue(this.returnType); } this.httpStart = function() { this.busy = true; showLoading(); } this.httpFinish = function() { this.busy = false; hideLoading(); } this.code2obj = function(code) { var obj; try { eval("obj = "+code+";"); return obj; }catch(e) { return false; } } this.obj2code = function(obj) { var buf = "", type; for (var key in obj) { type = (obj[key] == null)? "null" : typeof obj[key]; if (type == "function") continue; buf += ',"'+key+'":'; if (type == "string") buf += '"'+this.escapeChar(obj[key])+'"'; else if (type == "number" || type == "boolean") buf += obj[key].toString(); else if (type == "null") buf += "null"; else buf += this.obj2code(obj[key]); } if (buf != "") buf = buf.substr(1); return "{"+buf+"}"; } this.escapeChar = function(v) { return v.replace(/\\/g,"\\\\") .replace(/\"/g,'\\"') .replace(/\n/g,"\\n") .replace(/\r/g,"\\r") .replace(/\t/g,"\\t"); } }