home *** CD-ROM | disk | FTP | other *** search
/ Joystick Magazine 2003 November / CD1_JOY_153.iso / demos / NHL2004Demo.exe / fe / COMMON / js / Remote_Request.js < prev    next >
Text File  |  2003-08-20  |  9KB  |  269 lines

  1. Remote_Request.cacheFileDir        = "fe/common/data/"        
  2. Remote_Request.cachedRequests    = new JAX_Object();        
  3. Remote_Request.lkeyTimeouts        = 0;                        
  4. Remote_Request.requestsMade        = 0;                        
  5. Remote_Request.domainId         = "&domain=EASO&subdomain=NHL_2003&targetSubdomain=NHL_2003&";
  6. Remote_Request.clearCache        =  function() {};
  7.  
  8. function Remote_Request(serverURL, paramStr, isAsync)
  9. {
  10.     this.url            = (serverURL.match(/[http|https|file]:\/\//gi)) ? serverURL : System.getHostURL() + serverURL;
  11.     this.queryString    = (paramStr) ? paramStr : "";
  12.     this.isPogo            = true;
  13.     this.isAsyncProp    = (isAsync) ? true : false;
  14.     this.isPost            = true;
  15.     this.returnXMLDom    = true;
  16.     this.error            = "";
  17.     this.status            = "-1";
  18.     this.statusText        = "-1";
  19.     this.badHTTPStatus    = false;
  20.     this.httpRequest    = null;
  21.     this.isComplete        = false;
  22.     this.isSending        = false;
  23.     this.isAborted        = false;
  24.     this.value            = null;
  25.     this.oncomplete        = function() {};
  26.     this.onstatuschange    = function() {};
  27.     this.onerror        = function() {};
  28.     serverURL = paramStr = isAsync = null;
  29. };
  30. Remote_Request.prototype.ID            = -100;
  31. Remote_Request.prototype.parseStatus      = function()
  32. {
  33.     if (this.status != "-1") {
  34.         if (!this.status.match(/200/gi)) { this.badHTTPStatus = true; };
  35.     };
  36.     return ("::" + this.status + "::" + this.statusText);
  37. };
  38. Remote_Request.prototype.isResponseValid = function()
  39. {
  40.     var bIsValid        = false;
  41.     if (this.isComplete)  {
  42.         if (this.value) {
  43.             if (this.value.match(/(?:\:\:\d+\:\:|\:\:\-\d+\:\:)/g)) {
  44.                 this.error        = this.value.replace(/<SERVER_ERROR>/gi, "");
  45.                 this.error        = this.error.replace(/<\/SERVER_ERROR>/gi, "");
  46.                 this.error        = this.error.replace(/(^\s*)|(\s*$)/g, "");
  47.             } else if (this.value.match(/not found.*404/gi)) {
  48.                 this.error        = "::99::" + scMsg("SC_REGERROR_99");
  49.             } else {
  50.                 bIsValid        = true;
  51.             };
  52.         } else {
  53.             this.error            ="::501::Server not responding";
  54.         };
  55.     };
  56.     return bIsValid;
  57. };
  58. Remote_Request.prototype.cacheRequest            = function() {};
  59. Remote_Request.prototype.setIsPogoRequest        = function(bool) { this.isPogo = (bool) ? true : false; bool=null;};
  60. Remote_Request.prototype.setIsPostRequest        = function(bool) { this.isPost  = (bool) ? true : false; bool=null; };
  61. Remote_Request.prototype.setReturnXML_DOM        = function(bool) { this.returnXMLDom = (bool) ? true : false; bool=null; };
  62. Remote_Request.prototype.setOnStatusChange        = function(fHandler) { this.onstatuschange = fHandler;fHandler=null; };
  63. Remote_Request.prototype.setOnComplete            = function(fHandler) { this.oncomplete    = fHandler; fHandler=null; };
  64. Remote_Request.prototype.setID                = function(id) { this.ID = id; id=null };
  65. Remote_Request.prototype.getURL                = function() { return this.url; };
  66. Remote_Request.prototype.isAsync                = function() { return this.isAsyncProp; };
  67. Remote_Request.prototype.isError                = function() { return this.error; };
  68. Remote_Request.prototype.getValue                = function() { return this.value; };
  69. Remote_Request.prototype.getID                = function() { return this.ID; };
  70. Remote_Request.prototype.parseErrorCode        = function()
  71. {
  72.     var returnValue    = this.error.match(/\:\:\d+\:\:/g);
  73.     if (returnValue) {
  74.         returnValue    = returnValue.toString();
  75.         returnValue    = returnValue.replace(/\:\:/g, "");
  76.         returnValue    = parseInt(returnValue);
  77.     };
  78.     return returnValue;
  79. };
  80. Remote_Request.prototype.send                    = function()
  81. {
  82.     if (gblRRManager.isAborting || this.isAborted) { 
  83.         gblRRManager.unregister(this);
  84.         CollectGarbage();
  85.         return;
  86.     };
  87.     this.httpRequest    = new ActiveXObject("MSXML2.XMLHTTP");
  88.     if (this.isAsyncProp) {
  89.         var ti    = this;
  90.         var nRequestID    = gblRRManager.register(ti); 
  91.         if (nRequestID    == -500) { return; };
  92.         this.httpRequest.onreadystatechange = function()
  93.         {
  94.             if (gblRRManager.isAborting || ti.isAborted) { 
  95.                 gblRRManager.unregister(ti);
  96.                 ti = ti.httpRequest = ti.oncomplete = ti.onerror = ti.onstatuschange = null;
  97.                 CollectGarbage();
  98.                 return;
  99.              };
  100.             if (ti.httpRequest.readyState != 4) {
  101.                 if (ti.onstatuschange) {
  102.                     ti.onstatuschange();
  103.                 };
  104.                 try {
  105.                     ti.status        = ti.httpRequest.status;
  106.                     ti.statusText    = ti.httpRequest.statusCode;
  107.                 } catch (e) {}
  108.                     
  109.             } else {    
  110.                 ti.isSending    = false;
  111.                 ti.isComplete    = true;
  112.                 try {
  113.                     ti.status        = ti.httpRequest.status;
  114.                     ti.statusText    = ti.httpRequest.statusCode;
  115.                 } catch (e) {}
  116.                 if (!ti.isImage) {
  117.                     ti.value        = ti.httpRequest.responseText;
  118.                     ti.isvalid        = ti.isResponseValid();
  119.                 } else {
  120.                     ti.value        = ti.httpRequest.responseStream;
  121.                     try { 
  122.                         if (ti.oncomplete && !gblRRManager.isAborting) {
  123.                             ti.oncomplete();
  124.                         };
  125.                     } catch (e) {
  126.                         if (e.name == "Error" && e.number == -2147467259) {
  127.                             //freed script error;
  128.                         } else {
  129.                             throw e;
  130.                         };
  131.                     } finally {
  132.                         gblRRManager.unregister(ti);
  133.                           ti = ti.httpRequest = ti.oncomplete = ti.onerror = ti.onstatuschange = null;
  134.                           CollectGarbage();
  135.                         return;
  136.                     };
  137.                 };
  138.                 if (ti.isvalid) {
  139.                     if (ti.returnXMLDom) {
  140.                         if (ti.httpRequest.responseXML.xml) {
  141.                             ti.value = ti.httpRequest.responseXML;
  142.                         } else {
  143.                             ti.value        = new ActiveXObject("MSXML2.DOMDocument");
  144.                             ti.value.async    = false;
  145.                             ti.loaded        = ti.value.loadXML(ti.httpRequest.responseText);
  146.                             if (!ti.loaded) {
  147.                                 ti.value    = ti.httpRequest.responseText;
  148.                                 ti.error    = "::99::XML DOM NOT RETURNED";
  149.                             } else {
  150.                             };
  151.                             delete ti.loaded;                        
  152.                         };
  153.                     };
  154.                 } else {
  155.                     if (ti.onerror) {
  156.                         ti.onerror();
  157.                     };
  158.                 };
  159.                 delete ti.isvalid;
  160.                 if (ti.error.match(/\:\:-1\:\:/g)) {
  161.                     gblRRManager.doCancel();
  162.                     ti = ti.httpRequest = ti.oncomplete = ti.onerror = ti.onstatuschange = null;
  163.                     CollectGarbage();
  164.                     return;
  165.                 };
  166.                 try { 
  167.                     if (ti.oncomplete && !gblRRManager.isAborting) {
  168.                         ti.oncomplete();
  169.                     };
  170.                 } catch (e) {
  171.                     if (e.name == "Error" && e.number == -2147467259) {
  172.                         //freed script error;
  173.                     } else {
  174.                         throw e;
  175.                     };
  176.                 } finally {
  177.                     gblRRManager.unregister(ti);
  178.                     ti = ti.httpRequest = ti.oncomplete = ti.onerror = ti.onstatuschange = null;
  179.                     CollectGarbage();
  180.                 };
  181.             };
  182.         };
  183.     };
  184.     if (this.isPogo) {
  185.         this.url        = this.url.replace(/\/sc\//i, "/easo/nhl03/");
  186.         this.url        += "?site=pogo&lkey=" + System.Session.getUserLKey() + Remote_Request.domainId;
  187.         if (!this.isPost) {  this.url    += this.queryString;    };
  188.     } else {
  189.         this.url += "?" + this.queryString;
  190.     };
  191.     if (!this.isAborted && !gblRRManager.isAborting) {
  192.         this.isSending = true;
  193.         if (this.isPost) {
  194.             this.httpRequest.Open("POST", this.url, this.isAsyncProp);
  195.             this.httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
  196.             this.httpRequest.send(this.queryString);
  197.         } else {
  198.             if (!this.isImage) {
  199.                 this.httpRequest.Open("GET", this.url + "&easo_date=" + escape(new Date()) + "&", this.isAsyncProp);
  200.                 this.httpRequest.send();
  201.             } else {
  202.                 this.httpRequest.Open("GET", this.url + "&easo_date=" + escape(new Date()) , this.isAsyncProp);
  203.                 this.httpRequest.setRequestHeader("Content-Type", "image/gif");
  204.                 //this.httpRequest.setRequestHeader("Cache-Control", "no-cache");
  205.                 this.httpRequest.send();
  206.             };
  207.         };
  208.     };
  209.     if (!this.isAsyncProp) {
  210.         try {
  211.             ti.status        = ti.httpRequest.status;
  212.             ti.statusText    = ti.httpRequest.statusCode;
  213.         } catch (e) {}
  214.         this.isSending    = false;
  215.         this.value        = this.httpRequest.responseText; 
  216.         this.isComplete    = true;
  217.         this.isvalid    = this.isResponseValid();
  218.         if (this.isvalid) {
  219.             if (this.returnXMLDom) {
  220.                 if (this.httpRequest.responseXML.xml) {
  221.                     this.value = this.httpRequest.responseXML;
  222.                 } else {
  223.                 
  224.                     this.value        = new ActiveXObject("MSXML2.DOMDocument");
  225.                     this.value.async    = false;
  226.                     this.loaded    = this.value.loadXML(this.httpRequest.responseText);
  227.                     if (!this.loaded) {
  228.                         this.value    = this.httpRequest.repsonseText;
  229.                         ti.error    = "::99::XML DOM NOT RETURNED";
  230.                     };
  231.                     delete this.loaded;
  232.                 };
  233.             };
  234.         } else {
  235.             this.onerror();
  236.         };    
  237.         this.oncomplete();
  238.         this.isvalid = this.httpRequest = this.oncomplete = this.onerror = this.onstatuschange = null;
  239.         delete this.isvalid; delete this.httpRequest; delete this.oncomplete; delete this.onerror; delete this.onstatuschange;
  240.         CollectGarbage();
  241.     };
  242. };
  243. Remote_Request.prototype.isValueDOM            = function()
  244. {
  245.     if (this.value.xml) {
  246.         return true;
  247.     };
  248.     return false;
  249. };
  250. Remote_Request.prototype.abort                = function()
  251. {
  252.     this._killFunctions();
  253.     this.isSending = false; 
  254.     this.isAborted    = true;
  255.     this.httpRequest.onreadystatechange = function() { return false; };
  256.     this.httpRequest.abort(); 
  257.     this.httpRequest =null;
  258.     gblRRManager.unregister(this);
  259. };
  260. Remote_Request.prototype._killFunctions            = function() 
  261. {
  262.     this.isReponseValid    = Remote_Request.temp_function;
  263.     this.isError        = Remote_Request.temp_function;
  264.     this.parseErrorCode    = Remote_Request.temp_function;
  265.     this.send            = Remote_Request.temp_function;
  266.     this.oncomplete        = Remote_Request.temp_function;
  267.     this.getValue        = Remote_Request.temp_function;
  268. };
  269. Remote_Request.temp_function            = function() {};