// Javascript RMI // (c) copyright 2008 designagape, inc Common = function(inRMIURL, inRMIMode, inUTimeout, inDebug) { this.RMIURL = inRMIURL; this.RMIMode = inRMIMode; this.RMIPrefix = "rmi_"; this.uTimeout = parseInt(inUTimeout); this.debugMode = inDebug; this.requests = {}; this.requestData = {}; this.callbackHandler = false; // m is a table of character substitutions. this.m = { '\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"' : '\\"', '\\': '\\\\' }; } Common.prototype.debug = function(inString) { if(this.debugMode) { alert(inString); this.log(inString); } } Common.prototype.initConnect = function() { this.debug("initConnect() called.."); var A; var msxmlhttp = new Array( 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'); for (var i = 0; i < msxmlhttp.length; i++) { try { A = new ActiveXObject(msxmlhttp[i]); } catch (e) { A = null; } } if(!A && typeof XMLHttpRequest != "undefined") A = new XMLHttpRequest(); if (!A) this.debug("Could not create connection object."); return A; } Common.prototype.doCall = function(inFunc, args) { this.debug("in x.doCall.. mode:" + this.RMIMode); var calluuTime = new Date().getTime(); var i; var post_data; var serialData = "func=" + escape(inFunc) + "&rsrnd=" + calluuTime; var target_id; var uri = this.RMIURL; //serialize data for (i = 0; i < args.length-1; i++) serialData += "&rsargs[]=" + escape(this.serialize(args[i])); if(args.length > 0 && typeof(args[args.length-1]) != "function") { serialData += "&rsargs[]=" + escape(this.serialize(args[i])); } if (this.RMIMode == "GET") { (uri.indexOf("?") == -1) ? uri += "?" : uri += "&"; uri += serialData; post_data = null; } else if (this.RMIMode == "POST") { post_data = serialData; } else { alert("Illegal request type: " + this.RMIMode); } var connection = this.initConnect(); if (connection == null) { this.debug("NULL sajax object for user agent:\n" + navigator.userAgent); return false; } else { connection.open(this.RMIMode, uri, true); //window.open(uri); this.requests[calluuTime] = connection; if (this.RMIMode == "POST") { connection.setRequestHeader("Method", "POST " + uri + " HTTP/1.1"); connection.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); } connection.onreadystatechange = function() { //alert("ready state = " + connection.readyState); if (connection.readyState != 4) return; //alert("received"); //alert(typeof(x)); x.debug("received " + connection.responseText); var status; var data; var txt = connection.responseText.replace(/^\s*|\s*$/g,""); status = txt.charAt(0); data = txt.substring(2); if (status == "") { // let's just assume this is a pre-response bailout and let it slide for now } else if (status == "-") { alert("Error: " + data); } else { var prefix = ""; var callback = inFunc.substring(prefix.length) + "_cb"; try { //alert(typeof(args[args.length-1])); //alert(inFunc + "_cb " + typeof(x[inFunc + "_cb"])); //console.log(typeof(args[args.length-1])); if(typeof(args[args.length-1]) == "function") { args[args.length-1](eval(data)); } else if(x.callbackHandler != false) { x.callbackHandler[callback](eval(data)); } else if(typeof(x[callback]) == "function") { eval("x." + callback + "(eval(data))"); } } catch (e) { if(typeof(console) != "undefined") { console.log("Caught error '" + e + "': Could not eval " + data ); } else { alert("Caught error " + e + ": Could not eval " + data ); } } } } this.debug(inFunc + " uri = " + uri + " /post = " + post_data); connection.send(post_data); this.debug(inFunc + " waiting.."); delete connection; return true; } } Common.prototype.defaultFilter = function(baseObj, key) { var result; if (typeof key === 'string') { if (!Object.prototype.hasOwnProperty.call(baseObj, key)) { return undefined; } } else if (typeof key === 'number') { if (!(baseObj instanceof Array)) { return undefined; } } else { return undefined; } result = baseObj[key]; if(typeof(result) == 'undefined') { return undefined; } if (typeof result.toJSON === 'function') { return result.toJSON(); } else { return result; } } Common.prototype.serialize = function(value, optFilter) { var out = []; // array holding partial texts // var stack = []; // for diagnosing cycles var filter = optFilter || this.defaultFilter; // // The internal recursive serialization function. // function serializer(value) { var i,j; // loop counters var len; // array lengths; var needComma = false; var k,v; // property key and value // stack.push(value); switch (typeof value) { case 'object': if (value === null) { out.push('null'); } else if (value instanceof Array) { len = value.length; out.push('['); //for (i = 1; i < len; i += 1) { for (i = 0; i < len; i++) { v = filter(value, i); if (v !== undefined) { if (needComma) { out.push(','); } else { needComma = true; } serializer(v); } } out.push(']'); } else { out.push('{'); for (k in value) { v = filter(value, k); if (v !== undefined) { if (needComma) { out.push(','); } else { needComma = true; } serializer(k); out.push(':'); serializer(v); } } out.push('}'); } break; case 'string': // If the string contains no control characters, no quote // characters, and no backslash characters, then we can // simply slap some quotes around it. Otherwise we must // also replace the offending characters with safe // sequences. if ((/["\\\x00-\x1f]/).test(value)) { //"])){ out.push('"' + value.replace((/[\x00-\x1f\\"]/g), //"]), function (a) { var c = x.m[a]; if (c) { return c; } c = a.charCodeAt(); return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16); }) + '"'); } else { out.push('"' + value + '"'); } break; case 'number': // JSON numbers must be finite. Encode non-finite numbers // as null. //out.push(isFinite(this) ? String(this) : 'null'); out.push(isFinite(value) ? String(value) : 'null'); break; case 'boolean': out.push(String(value)); break; default: out.push('null'); } // stack.pop(); } var fakeRoot = [value]; serializer(filter(fakeRoot, 0)); return out.join(''); } Common.prototype.log = function(inObj) { if(typeof(console) != "undefined") { if(typeof(console.log) != "underfined") { console.log(inObj); } } } //stub Common.prototype.disableListing = function () { return(this.doCall("disableListing", this.disableListing.arguments)); } //stub Common.prototype.editListing = function () { return(this.doCall("editListing", this.editListing.arguments)); } //stub Common.prototype.emailListing = function () { return(this.doCall("emailListing", this.emailListing.arguments)); } //stub Common.prototype.getListingToken = function () { return(this.doCall("getListingToken", this.getListingToken.arguments)); } //stub Common.prototype.loadJSTemplates = function () { return(this.doCall("loadJSTemplates", this.loadJSTemplates.arguments)); } //stub Common.prototype.getImageURLs = function () { return(this.doCall("getImageURLs", this.getImageURLs.arguments)); } //stub Common.prototype.newListing = function () { return(this.doCall("newListing", this.newListing.arguments)); } //stub Common.prototype.saveListing = function () { return(this.doCall("saveListing", this.saveListing.arguments)); } //stub Common.prototype.searchListings = function () { return(this.doCall("searchListings", this.searchListings.arguments)); } var x = new Common('/js/common.ajax.js.php', 'POST', 500, false); tempDateObj = new Date(); if(typeof(tempDateObj.toJSON) == "undefined") { Date.prototype.toJSON = function () { function f(n) { return n < 10 ? '0' + n : n; } return (this.getUTCFullYear() + '-' + f(this.getUTCMonth() + 1) + '-' + f(this.getUTCDate()) + 'T' + f(this.getUTCHours()) + ':' + f(this.getUTCMinutes()) + ':' + f(this.getUTCSeconds()) + 'Z'); }; } delete tempDateObj;