//=====================================================================//
//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 IframeCommunicator() {
  this.id = ""+(idCount++);
  this.type = "Iframe";
  var pp = this;
  
  ///////////////////////////////////////////////////////////////////
  // Pattern "Template Method" is used here: implement its own concrete
  // "send" here. 
  /////////////////////////////////////////////////////////////////////
  this.send = function () {
    try {
      this.preSend();
      this.state = 2;
      var hid = "hiddeniFrame_"+this.id; 
      var fid = "hiddeniForm_"+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 para = this.params==null?"cId="+this.id+"&trans="+this.type:this.params+"&cId="+this.id+"&trans="+this.type;
	  var url = this.url;
      if (url.indexOf("?")>-1)
		  url += "&"+para;
	  else
		  url += "?"+para;
	  var fHTML = ""   
	  if (this.form) {
	      fHTML = this.form.innerHTML;
	  } 
		  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";
		  
	      form.innerHTML = fHTML+this.convertParaToInputs(url); 
	      
		  form.submit();
		  this.postSend();
	  
	}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);
	}
  }
  

  
	  ///////////////// 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 (w) {
	       // do the preprocess here...
	         pp.handler();
	       // do any clear up here...
	         pp.state = 0;
	         if (w)
	           w.location.replace('about:blank');
	      }
	      return res;
	  })();
}



///////////////////////////////////////////////////////////////
//
// Subclass Communicator
//
//////////////////////////////////////////////////////////////
IframeCommunicator.prototype = new Communicator();


