/*

RSLite - Author: Brent Ashley
Simple non-concurrent remote scripting calls.
send one string, receive one string

1) use this include
2) in body_onload(), set the RSLite var:
   RSLite = new RSLiteObject();
3) before using, set callback and failure functions if you want more than an alert
4) set interval and attempts for retries if defaults not suitable
5) call it, passing single parm:
   RSLite.call( rsPage, parm )
6) your callback receives string, or failure function called.

rsPage simply takes input parm "p" and sets session-expiry cookie called "RSLite".

Modified by Mark Qian - added parent and self fields.
*/
function RSLiteObject(p){
  this.interval = 500;
  this.attempts = 30;
  this.parent = p;
  
  ////////////////////////////////////////////////////
  //
  // use "self" to perserve "this" in a clousure
  //
  ///////////////////////////////////////////////////
  var self = this;
  this.call = function ( page, parm ){
    parm = (parm != null)? parm : "";
    var i = new Image();
    var d = new Date();
    document.cookie = 'RSLite=x; expires=Fri, 31 Dec 1999 23:59:59 GMT;';
    if (page.indexOf("?")>-1)
      i.src = page + '&u=' + d.getTime() + '&pre=' + parm;
    else
      i.src = page + '?u=' + d.getTime() + '&pre=' + parm;

    setTimeout( self.receive, this.interval );
  }  
  this.receive = function ( attempt ){  
                   var att = attempt || 1;
                   var response = null;
                   var aCookie = document.cookie.split("; ");
                   for (var i=0; i < aCookie.length; i++){
                     var aCrumb = aCookie[i].split("=");
                     if (aCrumb[0] == 'RSLite') response = aCrumb[1];
                   }
                   if ( response != null ){
                     self.callback( unescape(response.replace(/\+/g,' ')) );
                   } else {
                     if (att < self.attempts){
                       setTimeout( "cm.findCommunicator("+self.parent.id+").RSLite.receive( " + (att+1) +" );",self.interval);
                     } else {
                       self.failure();
                     }
                   }    
                 }
  this.callback = function( response ){ 
                    alert(response); 
                  }
  this.failure = function(){ 
                   if (this.parent.errHandler)
                      this.parent.errHandler("Communication failed with following possible reasons:\n  1). your browser cookie is off (this page requires you turn on browser cookie);\n  2). Server access failure\n  3). You violate the Same Origin rule trying to send requests crossing domain.\n  4). the size of return content exceeds the size limitation of a cookie");                                                                                  
                 }
}
var RSLite;
