
//****************************************************************************
// PopControl: 		the base of pop up control(eg:a calendar on it)
// Author: 		Lucas zheng
// Date:		2004-6-9
// Param: 		dataStr=the html code of what you want to show
// Using:
//      first.  new a popControl object,use obj.show(popCtrl,textCtrl,valueCtrl,distance) to show the control
//		  popCtrl=the object to snap with;
//                textCtrl=the object to set the return text;
//                valueCtrl=the object to set the return value;
//                distance=the object to move left or right a distance;
//	second. use obj.setReturn() / obj.hide() / obj.popControlKeyDown(event,index) / document.onclick() to hide the control
//	third.  you can add event at beforeShow/afterShow/beforeHide/afterHide/beforeReture()/afterReturn()
//                like this: obj.beforeShow="alert('before show')";
//	fourth. use obj.setReturn(text,val) to get you return and hide control
//                i.e.: var popCtrl=new popControl("<table border=1><tr><td onClick='hideControls();'>asdf,fdsa</td></tr></table>", "left");
//****************************************************************************
var popControls=[];
function popControl(dataStr,align){
	this.n_id=popControls.length;
	popControls[this.n_id]=this;
    this.parameter="";
    this.rootDir="";
    this.align=align;
	this.popCtrl=null;			//the object which popControl to popup		(mandatory)
	this.textCtrl=null;			//the object which the return text to set	(optional)
	this.valueCtrl=null;			//the object which the return value to set	(optional)
	this.goSelectTag = new Array();
    this.visible=false;
    //add by chenliang
    this.posX = null;
    this.posY = null;
	document.write("<div id='iPopControl_"+ this.n_id +"' onclick='event.cancelBubble=true' onKeydown='popControlKeyDown(event,"+this.n_id+")' style='POSITION:absolute;top:-1000;left:-1000;visibility:hidden;border:1px outset;width:auto;height:auto;z-index:1000;'> ");
	if(dataStr==null || dataStr=="")
       	document.write("&nbsp;</div>");
    else if(dataStr instanceof Array)
		document.write(dataStr.join("\r\n")+"</div>");
	else
		document.write(dataStr+"</div>");
	this.vicPopCtrl=eval("document.all.iPopControl_"+this.n_id);
	this.Point=function(iX, iY){
		this.x = iX;
		this.y = iY;
	}
	this.getParameter=function(sParam,sSeperator,sEqual){
		if(this.parameter==null || this.parameter=="") return "";
		if(sParam==null || sParam=="") return this.parameter;
		if(sSeperator==null) sSeperator=";";
		if(sEqual==null) sEqual="=";
		var tStr=this.parameter.split(sSeperator);
		var tmpStr;
		for(var i=0;i<tStr.length;i++)
		{
	 		tmpStr=tStr[i].split(sEqual);
 			if(tmpStr.length>1)
    			if(tmpStr[0]==sParam)
     				return tmpStr[1];
		}
		return "";
    }
	this.fGetXY=function(aTag){
  		var oTmp = aTag;
  		var pt = new this.Point(0,0);
  		do {
  			pt.x += oTmp.offsetLeft;
  			pt.y += oTmp.offsetTop;
  			oTmp = oTmp.offsetParent;
  		} while(oTmp.tagName!="BODY");
  		return pt;
	}
	this.fToggleTags=function(){
  		with (document.all.tags("SELECT")){
 			for (i=0; i<length; i++)
 			if ((item(i).mark!="popControl")&& this.fTagInBound(item(i))){
 				item(i).style.visibility = "hidden";
 				this.goSelectTag[this.goSelectTag.length] = item(i);
 			}
  		}
	}
	this.fTagInBound=function(aTag){
  		with (this.vicPopCtrl.style){
  			var l = parseInt(left);
  			var t = parseInt(top);
  			var r = l+parseInt(this.vicPopCtrl.offsetWidth);
  			var b = t+parseInt(this.vicPopCtrl.offsetHeight);
			var ptLT = this.fGetXY(aTag);
			return !((ptLT.x>r)||(ptLT.x+aTag.offsetWidth<l)||(ptLT.y>b)||(ptLT.y+aTag.offsetHeight<t));
  		}
	}
	this.beforeShow=null;
	this.afterShow=null;
	this.beforeHide=null;
	this.afterHide=null;
    this.beforeReturn=null;
    this.afterReturn=null;
	//you must atleast supply popCtrl(here or obj.popCtrl=XX)
	//you can define event beforeShow/afterShow by you own;
	this.show=function(pCtrl,tCtrl,vCtrl,relative){//edit by tom, 2005-08-29.
    	this.popCtrl=null;
        this.textCtrl=null;
        this.valueCtrl=null;
		//the popup object
		if(pCtrl!=null){
			if(typeof(pCtrl)=="string")
				this.popCtrl=eval((pCtrl.indexOf("document.all.")>=0?"":"document.all.")+pCtrl);
			else if(typeof(pCtrl)=="object")
				this.popCtrl=pCtrl;
		}
		//the return text object
		if(tCtrl!=null){
			if(typeof(tCtrl)=="string")
				this.textCtrl=(tCtrl.indexOf("document.all.")>=0?"":"document.all.")+tCtrl;
			else if(typeof(tCtrl)=="object")
				this.textCtrl=tCtrl;
		}
		//the return value object(can be null)
		if(vCtrl!=null){
			if(typeof(vCtrl)=="string")
				this.valueCtrl=(vCtrl.indexOf("document.all.")>=0?"":"document.all.")+vCtrl;
			else if(typeof(vCtrl)=="object")
				this.valueCtrl=vCtrl;
		}
//alter by chenliang 		
//		if(this.popCtrl==null){
//			alert("config Error!");
//			return false;
//		}

		if(this.beforeShow!=null)
                {
                  try{eval(this.beforeShow);}catch(e){}
                }
		event.cancelBubble=true;
		//hide other popControl
		for(var i=0;i<popControls.length;i++)
        	if(popControls[i].visible==true) popControls[i].hide();

		var point= null ;
		//add by chenliang 
		if(this.popCtrl!=null){
		 point = this.fGetXY(this.popCtrl);
		 }
		else{
		 point = new this.Point(this.posX,this.posY);
		 }
		 
		with (this.vicPopCtrl.style) {
        	if(this.align!=null && this.align.toLowerCase()=="right")
            	left=point.x-(this.vicPopCtrl.offsetWidth-(this.popCtrl==null?0:this.popCtrl.offsetWidth));
            else if(relative==undefined)
                left = point.x;//add by tom,at 2005-08-29.
            else
                left = point.x + relative;
            top  = point.y+(this.popCtrl==null?0:this.popCtrl.offsetHeight);
			this.fToggleTags(point);
			visibility = 'visible';
  		}
        this.visible=true;
  		if(this.afterShow!=null)
        {
          try{eval(this.afterShow);}catch(e){}
        }
        
	}

	this.hide=function(){
		if(this.beforeHide!=null)
		{
                 try{eval(this.beforeHide);}catch(e){}
		}
		this.vicPopCtrl.style.visibility = "hidden";
        this.vicPopCtrl.style.left="-1000";
        this.vicPopCtrl.style.top="-1000";

  		this.goSelectTag.length = 0;
        this.visible=false;
  		if(this.afterHide!=null)
        {
        	try{eval(this.afterHide);}catch(e){}
        }
	}
	//set the return text to textCtrl
	this.setText=function(tmpTxt){
		if(this.textCtrl!=null&&tmpTxt!=null)
		{
			if(typeof(this.textCtrl)=="object")
				this.textCtrl.value=tmpTxt;
			else
				eval(this.textCtrl+"='"+tmpTxt+"'");
		}
	}
	//set the return value to valueCtrl
	this.setValue=function(tmpVal){
		if(this.valueCtrl!=null&&tmpVal!=null)
		{
			if(typeof(this.valueCtrl)=="object")
				this.valueCtrl.value=tmpVal;
			else
				eval(this.valueCtrl+"='"+tmpVal+"'");
		}
	}
	//set text and value,and hide the control
	this.setReturn=function(tmpTxt,tmpVal){
          	if(this.beforeReturn!=null)
                  try{eval(this.beforeReturn);}catch(e){}
		this.setText(tmpTxt);
		this.setValue(tmpVal);
                if(this.afterReturn!=null)
              	  try{eval(this.afterReturn);}catch(e){}
		this.hide();
	}
    this.setPopContent=function(contentStr){
    	if(contentStr==null)
            	this.vicPopCtrl.innerHTML="&nbsp;";
      	else if(contentStr instanceof Array)
            	this.vicPopCtrl.innerHTML=(contentStr.join("\r\n"));
            else
            	this.vicPopCtrl.innerHTML=contentStr;
    }
}

function popControlKeyDown(evnt,index){
  try{
    if(evnt.keyCode==27)
    	popControls[index].hide();
  }
  catch(e){}
}
//hide all popControls
document.write("<SCRIPT event=onclick() for=document language=JavaScript>");
document.write("  for(var i=0;i<popControls.length;i++)");
document.write("    if(popControls[i].visible==true) popControls[i].hide();");
document.write("</SCRIPT>");

//******************** Some utility here *********************\
//get a iFrame with src/id/width/height
//the tObj is the name of popControl,we can use in src page
function getIFrame(tObj,tSrc,tName,tWidth,tHeight){
  var result="<iframe frameborder=\"0\" scrolling=\"auto\" controlObj=\""+ tObj+"\" src=\"" + tSrc +"\"";
  if(tName!=null)
  	result+=" name=\""+ tName + "\"";
  else
  	result+=" name=\"_"+ tObj + "Frame\"";
  if(tWidth!=null)
  	result+=" width=\"" + tWidth + "\"";
  else
  	result+=" width=\"250\"";
  if(tHeight!=null)
  	result+=" height=\""+ tHeight + "\"";
  else
  	result+=" height=\"180\"";
  result+="></iframe>";
  return result;
}

//get a parameter from a string
//eg: "type=object;id=12;text=ok",
//     in this string ';' is seperator(default), '=' is equal(default)
//     so invoke getParameter("type",";","=") will get string  "object".
function getParameter(sText,sParam,sSeperator,sEqual)
{
  if(sText==null || sText=="") return "";
  if(sParam==null || sParam=="") return "";
  if(sSeperator==null) sSeperator=";";
  if(sEqual==null) sEqual="=";
  var tStr=sText.split(sSeperator);
  var tmpStr;
  for(var i=0;i<tStr.length;i++)
  {
    tmpStr=tStr[i].split(sEqual);
    if(tmpStr.length>1)
       if(tmpStr[0]==sParam)
         return tmpStr[1];
  }
  return "";
}
//*********************************************************************
function resizeIframe(defaultHeight) {
    try{
  	  var i = parent.document.getElementById(window.name)
  	  var iHeight = document.body.scrollHeight
  	  
  	  if((typeof defaultHeight)=='undefined')
  	 	 i.style.height = iHeight + 5 + "px" ;
  	  else if(Number(iHeight) < Number(defaultHeight))
  	  	 i.style.height = 	defaultHeight + "px" ; 
  	}
  	catch(e){}
  }	
