Subversion Repositories cryptochat

Rev

View as "text/javascript" | Blame | Last modification | View Log | RSS feed

  1. // remote scripting library
  2. // (c) copyright 2005 modernmethod, inc
  3. var sajax_debug_mode = false;
  4. var sajax_failure_redirect = "";
  5. var sajax_remote_uri = "";
  6. var sajax_request_type = "";
  7. var sajax_target_id = "";
  8.  
  9. function sajax_debug(text) {
  10.         if (sajax_debug_mode) {
  11.                 alert(text);
  12.         }
  13.         return true;
  14. }
  15.  
  16. function sajax_failure(text) {
  17.         if(sajax_failure_redirect != "" && !sajax_debug_mode) {
  18.                 window.location.href = sajax_failure_redirect;
  19.         } else {
  20.                 sajax_debug(text);
  21.         }
  22.         return false;
  23. }
  24.  
  25. var sajax_requests = [];
  26.  
  27. function sajax_cancel(id) {
  28.          if(arguments.length === 0) {
  29.                 for (var i = 0; i < sajax_requests.length; i++) {
  30.                         if(sajax_requests[i]) {
  31.                                 sajax_requests[i].abort();
  32.                                 sajax_requests.splice(i, 1, null);
  33.                         }
  34.                 }
  35.         } else if(sajax_requests[id]) {
  36.                 sajax_requests[id].abort();
  37.                 sajax_requests.splice(id, 1, null);
  38.         }
  39. }
  40.  
  41. //Support IE 5
  42. if (typeof(encodeURIComponent) == "undefined") {
  43.         encodeURIComponent = function(string) {
  44.                 this.encodeChar = function(c) {
  45.                         c = c.charCodeAt(0);
  46.                         var utf8 = "";
  47.                         if (c < 128) {
  48.                                 utf8 += String.fromCharCode(c);
  49.                         } else if((c > 127) && (c < 2048)) {
  50.                                 utf8 += String.fromCharCode((c >> 6) | 192);
  51.                                 utf8 += String.fromCharCode((c & 63) | 128);
  52.                         } else {
  53.                                 utf8 += String.fromCharCode((c >> 12) | 224);
  54.                                 utf8 += String.fromCharCode(((c >> 6) & 63) | 128);
  55.                                 utf8 += String.fromCharCode((c & 63) | 128);
  56.                         }
  57.                         var encoded = "";
  58.                         for(var i = 0; i < utf8.length; i++) {
  59.                                 encoded += "%"+utf8.charCodeAt(i).toString(16).toUpperCase();
  60.                         }
  61.                         return encoded;
  62.                 };
  63.                
  64.                 string = string.replace(/\r\n/g,"\n");
  65.                 var encoded = "";
  66.                 for (var n = 0; n < string.length; n++) {
  67.                         if(string.charAt(n).match(/[~!*()'a-z0-9]/i) === null) {
  68.                                 encoded += encodeChar(string.charAt(n));
  69.                         } else {
  70.                                 encoded += string.charAt(n);
  71.                         }
  72.                 }
  73.                
  74.                 return encoded;
  75.         };
  76. }
  77.  
  78. //Support IE 5, 5.5 and 6
  79. if (typeof(window.XMLHttpRequest) == "undefined") {
  80.         window.XMLHttpRequest = function() {
  81.                 var msxmlhttp = Array(
  82.                         'Msxml2.XMLHTTP.6.0',
  83.                         'Msxml2.XMLHTTP.5.0',
  84.                         'Msxml2.XMLHTTP.4.0',
  85.                         'Msxml2.XMLHTTP.3.0',
  86.                         'Msxml2.XMLHTTP',
  87.                         'Microsoft.XMLHTTP');
  88.                 for (var i = 0; i < msxmlhttp.length; i++) {
  89.                         try { return new window.ActiveXObject(msxmlhttp[i]); }
  90.                         catch(e) {}
  91.                 }
  92.                 return null;
  93.         };
  94. }
  95.  
  96. function sajax_do_call(func_name, args, method, asynchronous, uri) {
  97.        
  98.         //Handle old code calls
  99.         switch(arguments.length) {
  100.                 case 0:
  101.                         return false;
  102.                 case 1:
  103.                         var args = [];
  104.                 case 2:
  105.                         var method = "GET";
  106.                 case 3:
  107.                         var asynchronous = true;
  108.                 case 4:
  109.                         var uri = "";
  110.         }
  111.        
  112.         if(sajax_request_type != "") {
  113.                 method = sajax_request_type;
  114.         }
  115.        
  116.         if(method !== "POST") {
  117.                 method = "GET";
  118.         }
  119.        
  120.         if(sajax_remote_uri != "") {
  121.                 uri = sajax_remote_uri;
  122.         }
  123.        
  124.         if(uri == "") {
  125.                 uri = window.location.href.replace(/#.*$/, "");
  126.         }
  127.        
  128.         var i, x;
  129.         var geturi = "";
  130.         var data;
  131.         var target_id = sajax_target_id;
  132.         var argsarray = Array();
  133.        
  134.         sajax_debug("in sajax_do_call().." + method + "/" + sajax_target_id);
  135.        
  136.         for(i = 0; i < args.length-1; i++) {
  137.                 argsarray[i] = args[i];
  138.         }
  139.        
  140.         data = "rs=" + encodeURIComponent(func_name);
  141.         if(argsarray.length > 0) {
  142.                 try {
  143.                         //the ending & is here to avoide issues with safari 1.2 appending junk on POST
  144.                         data += "&rsargs=" + encodeURIComponent(JSON.stringify(argsarray)) + '&';
  145.                 } catch(e) {
  146.                         return sajax_failure("JSON.stringify() failed for user agent:\n" + navigator.userAgent);
  147.                 }
  148.         }
  149.        
  150.         try {
  151.                 x = new window.XMLHttpRequest();
  152.         } catch(e) {}
  153.         if(x === null || typeof x.readyState !== "number") {
  154.                 //TODO support iframe ajaxing
  155.                 //document.getElementsByTagName("pre")[0].innerHTML
  156.                 return sajax_failure("NULL sajax object for user agent:\n" + navigator.userAgent);
  157.         }
  158.        
  159.         if(method == "POST" && typeof x.setRequestHeader == "undefined") {
  160.                 //TODO convert uri to absolute uri
  161.                 if((uri+data).length < 512) {
  162.                         sajax_debug("Browser did not support POST, switching to GET");
  163.                         method = "GET";
  164.                 } else {
  165.                         return sajax_failure("Request failed for user agent:\n" + navigator.userAgent);
  166.                 }
  167.         }
  168.        
  169.         if (method == "GET") {
  170.                 geturi = uri;
  171.                 if (geturi.indexOf("?") == -1) {
  172.                         geturi += "?" + data;
  173.                 } else {
  174.                         geturi += "&" + data;
  175.                 }
  176.  
  177.                 if(geturi.length > 512){
  178.                         method = "POST";
  179.                         sajax_debug("Data to long for GET switching to POST");
  180.                 } else {
  181.                         uri = geturi;
  182.                         data = null;
  183.                 }
  184.         }
  185.        
  186.         x.open(method, uri, asynchronous);
  187.        
  188.         if (method == "POST") {
  189.                 x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
  190.                 x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  191.         }
  192.        
  193.         //Prevent Opera from executing the callback multiple times.
  194.         var alreadydone = false;
  195.         var responcefunc = function() {
  196.                 if (alreadydone === true)  {
  197.                         return false;
  198.                 }
  199.                
  200.                 if (x.readyState != 4) {
  201.                         return false;
  202.                 }
  203.                
  204.                 var status;
  205.                 var data;
  206.                 var txt = x.responseText.replace(/^\s*|\s*$/g,"");
  207.                 status = txt.charAt(0);
  208.                 if(status == "-" || status == "+") {
  209.                         data = txt.substring(2);
  210.                 } else {
  211.                         data = txt;
  212.                 }
  213.  
  214.                 if(status == "" && (x.status == 200 || x.status == "" || x.status == "12019")) {
  215.                         // let's just assume this is a pre-response bailout and let it slide for now
  216.                         return false;
  217.                 } else if(status != "+" || x.status != 200) {
  218.                         alert("Error " + x.status + ": " + data);
  219.                         return false;
  220.                 } else {
  221.                         alreadydone = true;
  222.                         var callback;
  223.                         var extra_data = false;
  224.                         if (typeof args[args.length-1] == "object") {
  225.                                 callback = args[args.length-1].callback;
  226.                                 extra_data = args[args.length-1].extra_data;
  227.                         } else {
  228.                                 callback = args[args.length-1];
  229.                         }
  230.                         try {
  231.                                 if(typeof(JSON) != "undefined" && typeof(JSON.parse) != "undefined") {
  232.                                         try {
  233.                                                 var res = JSON.parse(data);
  234.                                         } catch(e) {
  235.                                                 return sajax_failure("JSON.parse failed for user agent:\n" + navigator.userAgent);
  236.                                         }
  237.                                 } else {
  238.                                         sajax_debug("Warning: JSON is being directly executed via eval()!");
  239.                                         eval("var res = ("+data+"); res;");
  240.                                 }
  241.                                 if(target_id) {
  242.                                         document.getElementById(target_id).innerHTML = res;
  243.                                 } else {
  244.                                         callback(res, extra_data);
  245.                                 }
  246.                                 sajax_requests.splice(id, 1, null);
  247.                         } catch(e) {
  248.                                 sajax_debug("Caught error " + e + ": Could not parse " + data );
  249.                                 return false;
  250.                         }
  251.                 }
  252.                 return true;
  253.         };
  254.        
  255.         if(asynchronous) {
  256.                 x.onreadystatechange = responcefunc;
  257.         }
  258.        
  259.         sajax_debug(func_name + " uri = " + uri + "/post = " + data);
  260.         try {
  261.                 x.send(data);
  262.         }
  263.         catch(e) {
  264.                 if(method === "POST" && geturi === "") {
  265.                         sajax_debug("Browser did not support POST, tyring GET instead");
  266.                         sajax_request_type = "";
  267.                         return sajax_do_call(func_name, args, "GET", asynchronous);
  268.                 } else {
  269.                         return sajax_failure("Request failed for user agent:\n" + navigator.userAgent);
  270.                 }
  271.         }
  272.         sajax_debug(func_name + " waiting..");
  273.        
  274.         if(asynchronous) {
  275.                 var id = sajax_requests.length;
  276.                 sajax_requests[id] = x;
  277.                 return id;
  278.         } else {
  279.                 return responcefunc();
  280.         }
  281. }