//=====================================================================//
//Author: Mark Qian <markqian@hotmail.com>                             //
//WWW: http://www.coolshare.com/                                       //
//All rights reserved (2006)                                           //
//                                                                     //
//You must contact Mark Qian to get a permission of use                //
//in case you want to make any use of the codes besides viewing it     //
//on Mark's site.                                                      //
//=====================================================================//
function FrameCommunicator() {
  this.id = ""+(idCount++);
  var pp = this;
  
  ///////////////////////////////////////////////////////////////////
  // Pattern "Template Method" is used here: implement its own concrete
  // "send" here. 
  /////////////////////////////////////////////////////////////////////
  this.send = function () {
    try {

      this.state = 2;
      var hid = "hiddenFrame_"+this.id; 
      var fid = "hiddenForm_"+this.id;
	  var fs=top.document.getElementsByTagName("frameset")[0];
	  this.hFrame = top.document.getElementById(hid);
	  
	  if (this.hFrame==null) {
	    var row = fs.rows;
	    this.hFrame = insertDomNode(this.w.document, this.w.document.getElementsByTagName('body')[0], "iframe", {"id":hid, "name":hid, "style":"display:none"}, "beforeEnd");    
	    fs.rows = fs.rows+",0";
	  }
	  
	  
	  this.hForm = this.w.document.getElementById(fid);
	  if (this.hForm==null) {
	    this.hForm = insertDomNode(this.w.document, this.w.document.getElementsByTagName('body')[0], "form", {"id":fid, "name":fid, "style":"display:none"}, "beforeEnd");
	  }
	  this.hForm = this.w.document.getElementById(fid);
	  this.hForm.target = hid;
	  this.hForm.action = this.url;
	  this.hForm.method = "post";
	  if (this.url.indexOf("?")>-1)
    	  this.hForm.innerHTML = this.convertParaToInputs(this.url); 
      
	  this.hForm.submit();
	}catch (err) {
	  this.state = 0;
	  var msg = err.description;
	  if (browser && browser=="Internet Explorer") {
	    msg+= "The item \"Navigate sub-frames across different domains\" \nunder your security option is required to be on";
	  }
	  alert(msg);
	}
  }
  
  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;
  }
  
	  ///////////////// Wrapping the concrete callback ///////////
	  //
	  // I don't invoke the real callback function, "handler",
	  // directly when hidden iframe onloading. 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. See the invoking 
	  // in issue_1_select2.jsp
	  //
	  ////////////////////////////////////////////////////////////
  this.process = (function() {
           var res = function () {
	       // do the preprocess here...
	         pp.handler();
	       // do any clear up here...
	         pp.state = 0;
	      }
	      return res;
	  })();
}



///////////////////////////////////////////////////////////////
//
// Subclass Communicator
//
//////////////////////////////////////////////////////////////
FrameCommunicator.prototype = new Communicator();


