// =====================================================================//
// 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.                                                      //
//======================================================================//

/////////////////////////// Closure 2 //////////////////////////////
//
// Communicator is contained in a closure since we
// need to preserved the reference of Communicator
// so that we can refer it in method wrapper, "process", that 
// will be assigned to onreadystatechange.
// When the call return, we need the "handler", the real callback 
// processor wrapped by "process", to carry out the real tasks on 
// the return text. The callback will be executed in the "process"'s
// execution context where we can't refer to the Communicator that 
// appears as a local var of doKeyUp and may be garbage collected
// if we don't wrap it with a closure.
//
///////////////////////////////////////////////////////////////
var Communicator = (function(){
	function construct() {	    
	  this.state = 0;
	  this.type = null;

	  this.init = function (w, url, params, handler, form) {
		    this.state = 1;
		    this.w = w;
		    this.url = url;
		    this.params = params;
		    this.handler = handler;
		    this.form = form;
		    this.postInit();
            var u = this.url.split("?");
            this.url = u[0];
            if (u.length>1) {
              this.params = this.params==null?u[1]:this.params+"&"+u[1];
            }
            var tt = 'nocache=' +(new Date()).getTime();
            this.params = this.params==null?tt:this.params+"&"+tt;
	  }
	  this.preSend = function () {
	  
	  }
	  
	  this.postSend = function () {
	    window.status = "Request sent using "+ this.type;
	  }
	  this.postInit = function () {
	  
	  }
	  var pp = this;
	
	  
	  
	  ////////////////////////////////////////////////////////////
	  // Process nothing in case the sub class does not
	  // implement "send" instead of throwing an "undefine" error
	  //////////////////////////////////////////////////////////
	  this.send = function() {
	    
	  }
	  
	  this.convertParaToInputs = function(url) {
	    var u = url.split("?");
	    var pp = u[1].split("&");
	    var res = "";
	    for (var i=0; i<pp.length; i++) {
	      var ppp = pp[i].split("=");
	      res += "<input type=hidden name=\""+ppp[0]+"\" value=\""+ppp[1]+"\">";
	    }
	    return res;
      }
      
      this.buildParaForForm = function(f) {
        var inputs = f.elements;

        var res = "";
        if (inputs.length>0) {
            var j = 0;
		    for (var i=0; i<inputs.length; i++) {
		      if (!inputs[i].name) {
		        continue;
		      }
		      if (j==0)
		      	res += escape(inputs[i].name)+"="+escape(inputs[i].value);
		      else
 		      	res += "&"+escape(inputs[i].name)+"="+escape(inputs[i].value);
 		      j++;
		    }
	    }
	    return res;
      }
    }
	return construct;
})();



