//=====================================================================//
//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 IframeCommunicator() {
  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;
	  this.hFrame = this.w.document.getElementById(hid);
	  if (this.hFrame==null) {
	    this.hFrame = insertDomNode(this.w.document, this.w.document.body, "iframe", {"id":hid, "name":hid, "style":"display:none"}, "beforeEnd");	    
	  }	  
	  var form = this.w.document.getElementById(fid);
	  if (form==null) {
	    form = insertDomNode(this.w.document, this.w.document.body, "form", {"id":fid, "name":fid, "style":"display:none"}, "beforeEnd");
	  }

	  form.target = hid;
	  form.action = this.url;
	  form.method = "post";
	  if (this.url.indexOf("?")>-1)
    	  form.innerHTML = this.convertParaToInputs(this.url); 
      
	  form.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);
	  if (this.errHandler)
         this.errHandler(e);
	}
  }
  
  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
//
//////////////////////////////////////////////////////////////
IframeCommunicator.prototype = new Communicator();


