
// =====================================================================//
// Author: Mark Qian <markqian@hotmail.com>                             //
// WWW: http://www.coolshare.com/                                       //
// Copyright (c) 2006, Mark Qian                                        //
//                                                                      //
// You must contact Mark Qian to get a permission of use                //
// in case you want to make any use of the codes except viewing it     //
// on Mark's site.                                                      //
//======================================================================//



function AJAXCommunicator() {
  this.id = ""+(idCount++);
  this.type = "AJAX";
  ///////////////////////////////////////////////////////////////////
  //
  // Pattern "Template Method" is used here: implement its own concrete
  // "send" here. 
  //
  /////////////////////////////////////////////////////////////////////
  this.send = function () {
    try {
        this.preSend();
	    this.http = createRequestObject();
	    var para = this.params==null?"cId="+this.id+"&trans="+this.type:this.params+"&cId="+this.id+"&trans="+this.type;
	    var parent = this;
	    
	    
	      ///////////////// Wrapping the concrete callback ///////////
		  //
		  // I don't assign the real callback function, "handler",
		  // directly to the onreadystatechange. Instead, I wrapped
		  // the "handler" with a wrapper, "process" so
		  // that I can do some system level tasks before and
		  // after the "handler" is carried out. For example,
		  // I can update the state of the Communicator here
		  // when the Communicator is in my pool. I can't wrap it
		  // in the super class but only here since the handler is 
		  // not available until now.
		  //
		  ////////////////////////////////////////////////////////////	  
	    this.http.onreadystatechange = (function() {
	           var process = function () {
		       // do the preprocess here...
		       
		       if(parent.http.readyState == 4){	    
		          parent.res = parent.http.responseText;
		          parent.handler();
		       }
		       // do any clear up here...
		        parent.state = 0;
		      }
		      return process;
		  })();
		  
	    this.http.open('POST', this.url, true); 
	    this.http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
	    this.http.setRequestHeader("Content-length", para.length); 
	    this.http.setRequestHeader("Connection", "close"); 
	    this.http.send(para);    
	    this.state = 2;
	    this.postSend();
     } catch (e) {
       if (this.errHandler)
         this.errHandler(e);
     }
  }
  
  this.postInit = function () {
    if (this.form)
      this.params = this.buildParaForForm(this.form);
  }
}

///////////////////////////////////////////////////////////////
//
// Subclass Communicator
//
//////////////////////////////////////////////////////////////
AJAXCommunicator.prototype = new Communicator();

