//------------------------------------------------------------------------------
// C O N T R O L S
// 
// Remember: to set an attribute use setAttribute(name,value), FireFox likes that
//            better and IE seems to be ok whith that. getAttribute should be
//            used to get the value from the attribute.
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// _CTRL = default control namespace
//

var _ctrl=
{
  // We make a control out of the given Dom-object just by extending it with
  // methods and properties
  init: function(strDomNodeName,strFunctions)
  {

    var objControl=document.getElementById(strDomNodeName);
		
 		
	
    objControl.getValue=    function()       {return this.value;};
    objControl.setValue=    function(sValue) {this.value=sValue;};


    objControl.blink=       this.blink;

		objControl.formSubmit = this.formSubmit;
			
    objControl.repaint=     this.repaint;
    objControl.update=      this.update;
    objControl.onfocus=     this.gotFocus;
    objControl.onblur=      this.lostFocus;  
    
    objControl.validaterules=new Array();
    objControl.controls=new Array();

    strParentName=objControl.getAttribute('parent')
    if (strParentName>'')
    {
        objParent=document.getElementById(strParentName);
        objParent.controls.push(objControl.id);
        
        var strDivider=objParent.getAttribute('divider');
        if (strDivider)
        {
          arrDivider=strDivider.split(' ');
          if (arrDivider[0]=='Y')
            if (objControl.offsetTop>arrDivider[1])
              objControl.setAttribute('division','DOWN');
            else
              objControl.setAttribute('division','UP'); 
          else
            if (objControl.offsetLeft>arrDivider[1])
              objControl.setAttribute('division','RIGHT');
            else
              objControl.setAttribute('division','LEFT');                    
        }
        
    } 
      
    var arrFunctions=strFunctions.split('[=]');
    for(i in arrFunctions)
    {
      arrCurrentFunction=arrFunctions[i].split('[+]');
      if (arrCurrentFunction[0]>'') eval("objControl."+arrCurrentFunction[0]+'='+arrCurrentFunction[1]+';');
    }
  
    objControl.repaint(); 
    if (objControl.getAttribute('elem_init')>'') eval(objControl.getAttribute('elem_init'));

    if (objControl.getAttribute('formSubmit') == 'NO') 
      objControl.formSubmit = function(){return ""; };
    else
      objControl.setValue(_decodeHTMLEntities(objControl.getValue()));
      
  },

  update: function()
  {
    //--------------------------------------------------------------------------
    //Update repaints the root object and al it's children
    
    // We zoeken eerst de updateroot, dat is bijvoorbeeld bij een divider de BOX
    // waar de divider toe behoort
    
    objUpdateRoot=document.getElementById(this.getAttribute('updateroot'));
    doUpdate(objUpdateRoot); 
  },
  
  doUpdate: function(objControl)
  {
    objControl.repaint(); 
  
    if (objControl.getAttribute('container'))
    {  
      for (i in objControl.controls)
      { 
        objChild=document.getElementById(objControl.controls[i]);
        if (objChild!=null) this.doUpdate(objChild);
      }    
    }
  
  },

  
  repaint: function ()
  {
    // The real painting of cause is done by the browser but here we set
    // size and location at init and after resizing and moving
    strParentName=this.getAttribute('parent');
    if (strParentName=="" || strParentName==null) 
		  objParent=this.parentNode;
		else
      objParent=document.getElementById(strParentName);
    
//    var strPadding='';
//    if (objParent!=null) strPadding=objParent.style.padding;
//    if (strPadding=='') strPadding='0 0 0 0';  
//    strPadding=strPadding.replace(/px|pt|em/gi,'');
//    var arrPadding=strPadding.split(' ');

    var iContentX=_PaddingL(objParent);
		var iContentW=objParent.clientWidth-_PaddingL(objParent)-_PaddingR(objParent);
    var iContentY=_PaddingT(objParent);
    var iContentH=objParent.clientHeight-_PaddingT(objParent)-_PaddingB(objParent);		

    var iWCompensator= _BorderLW(this)+_BorderRW(this)
		                  +_PaddingL(this)+_PaddingR(this);
    var iHCompensator= _BorderTW(this)+_BorderBW(this)
                      +_PaddingT(this)+_PaddingB(this);
											  
    var intBorderTop    = _BorderTW(this) //,'border-top-width');
  	var intBorderBottom = _BorderBW(this) //,'border-bottom-width');
    var intBorderLeft   = _BorderLW(this) //,'border-left-width');
  	var intBorderRight  = _BorderRW(this) //,'border-right-width');
      
  	var intBorderY=intBorderTop+intBorderBottom;
  	var intBorderX=intBorderLeft+intBorderRight;
  
    strDivider=objParent.getAttribute('divider');
    if (strDivider>'')
    {
      arrDivider=strDivider.split(' ');
      
      if (arrDivider[2]=='DRAG') intDividerSize=4;
      else intDividerSize=0;
      
      if (arrDivider[0]=='Y')
      {
        strDivision=this.getAttribute('division');
        if (strDivision=='DOWN')
        {
          iContentY=(parseInt(arrDivider[1])+intDividerSize);     
          iContentH=(iContentH-parseInt(arrDivider[1])-intDividerSize);					
          intParent_Y=parseInt(arrDivider[1])+intDividerSize;
          intParent_H=objParent.clientHeight-parseInt(arrDivider[1])-intDividerSize;
        }
        else
        {
					iContentH=(arrDivider[1]-_PaddingL(objParent));
          intParent_Y=0;
          intParent_H=arrDivider[1];    
        }
        intParent_X=0;
        intParent_W=objParent.clientWidth;    
      }
      else
      {
        strDivision=this.getAttribute('division');
        if (strDivision=='RIGHT')
        {
					iContentX=(parseInt(arrDivider[1])+intDividerSize);			
					if (this.getAttribute('dock')!='RIGHT')
					  iContentW=(iContentW-parseInt(arrDivider[1])-intDividerSize);
          intParent_X=parseInt(arrDivider[1])+intDividerSize;
          intParent_W=objParent.offsetWidth-parseInt(arrDivider[1])-intDividerSize;
        }
        else
        {
					iContentW=(arrDivider[1]-_PaddingL(objParent));
          intParent_X=0;
          intParent_W=arrDivider[1];  
					  
        }  
        intParent_Y=0;
        intParent_H=objParent.offsetHeight;
      }
    }
    else
    {
      intParent_Y=0;intParent_X=0;
      intParent_W =objParent.offsetWidth;
      intParent_H =objParent.offsetHeight;
      
    }
    if (intParent_H<0) intParent_H=0;
    if (intParent_W<0) intParent_W=0;
		
		intPadding_T=_PaddingT(this);
		intPadding_B=_PaddingB(this);
		intPadding_L=_PaddingL(this);
		intPadding_R=_PaddingR(this);
   
    switch(this.getAttribute('dock'))
    {
      case 'LEFT':
        this.style.left=iContentX+'px'; 
        break;
      case 'TOP':
        this.style.top=iContentY+'px';
        break;
      case 'RIGHT':
        //var MyRight=this.offsetWidth+iWCompensator;					
        this.style.left=(iContentW-this.offsetWidth)+'px';
        break;  
     case 'BOTTOM':
        //var MyHeight=this.offsetHeight+intBorderY;
        this.style.top=(iContentH-this.offsetHeight)+'px';
        break; 
     case 'FILLX':
        this.style.left=iContentX+'px';   
        this.style.width=(iContentW-iWCompensator)+'px';
        break;
     case 'FILL': 
        this.style.left=iContentX+'px';  
        this.style.top =iContentY+'px';  
        this.style.width =(iContentW-iWCompensator)+'px';
        this.style.height=(iContentH-iHCompensator)+'px';       
        break; 
    }
		// if a control has inner contents that need to be repainted, it has a method
		// called repaintContent()
    if (this.repaintContent!=null) this.repaintContent();

  },
  
  formSubmit: function()
  {
    var strPostData='';
    
    if (this.getAttribute('container'))
    {
      var arrChildren=this.controls;
      for (var i in arrChildren)
      {				
        objChild=document.getElementById(arrChildren[i]);						
        strPostData+=objChild.formSubmit();
      }
    }
    else
    { 
      value=''+this.getValue(); 			 
      if (value!=null)
      {			
        //value=value.replace(/&/g,'[AMP]');
				value=_encodeHTMLEntities(value);
        strPostData='&'+this.id+'='+encodeURIComponent(value);    
      }
    }  
    return strPostData;
  },
  
  gotFocus: function ()
  {
    this.style.origClassName=this.style.className;
    this.style.className='_ControlBlink';
    
  },
  lostFocus: function ()
  {
    var Ret='OK';
    this.style.className=this.origClassName;//cfgDesignBorderColor;
    
    if (this.validate) Ret=this.validatefunction();
    
    if (Ret!='OK')
      alert('Waarde in veld niet juist:\n'+$Ret);
  },
   
  blink: function()
  {
    this.blinkStatus=0;
    this.blinkCycles=0;
    this.blinkOrigBgColor=_BGColor(this);
    this.blinkInterval=window.setInterval('blinkExecute("'+this.id+'")',15);  
  },
  blinkExecute: function(ObjName)
  {
    Obj=document.getElementById(ObjName);
    if (Obj==null) return;
    
    Obj.BlinkStatus=Obj.BlinkStatus+1;
    if (Obj.BlinkStatus>20)
    {
      Obj.BlinkCycles=Obj.BlinkCycles+1;
      Obj.BlinkStatus=0;
    }
    if (Obj.BlinkCycles>2) 
    {
        clearInterval(Obj.BlinkInterval);
        Obj.style.backgroundColor=Obj.BlinkOrigBgColor
    }
    else
    {
      Start=hex2num('FFFF00');
      End=hex2num('00FFFF');
      var distR=End[0]-Start[0];
      var distG=End[1]-Start[1];
      var distB=End[2]-Start[2];
      var StepR=Math.round((distR/20)*Obj.BlinkStatus);
      var StepG=Math.round((distG/20)*Obj.BlinkStatus);
      var StepB=Math.round((distB/20)*Obj.BlinkStatus);
      Start[0]=Start[0]+StepR;
      Start[1]=Start[1]+StepG;
      Start[2]=Start[2]+StepB;
      
      Obj.style.backgroundColor='rgb('+Start[0]+','+Start[1]+','+Start[2]+')';//num2hex(Start);
    }
    
  }
};
//------------------------------------------------------------------------------
// BOX
var _box= 
{

  init: function(objBox)
  {
    var strAttr_Divider=objBox.getAttribute('divider');
    
    if (strAttr_Divider>'')
    {
      arrDivider=strAttr_Divider.split(' ');
      objDivider=document.getElementById('divider_'+objBox.id);
      if (objDivider)
        if (arrDivider[0]=='Y')
        { 
          objDivider.update=this.yDivider_Update;
        }
        else
        {
          objDivider.update=this.xDivider_Update;
        }
      if (arrDivider[2]=='DRAG') _mouse.makeDraggable(objDivider);
    
      // if position of divider <0 then it should be taken as a position relative to 
      // bottom or right border.
      if (arrDivider[1]<0)
      {
        if (arrDivider[0]=='Y')
        {
          arrDivider[1]=parseInt(objBox.offsetHeight)+parseInt(arrDivider[1]);
          if (objDivider) objDivider.style.top=arrDivider[1]+'px';
          objBox.setAttribute('divider','Y '+arrDivider[1]+' '+arrDivider[2]);      
        }
        else
        {
          arrDivider[1]=parseInt(objBox.offsetWidth)+parseInt(arrDivider[1]);
          if (objDivider) objDivider.style.left=arrDivider[1]+'px';
          objBox.setAttribute('divider','X '+arrDivider[1]+' '+arrDivider[2]);      
        }
        objBox.repaint();
        
      }
    }
  },
  
  yDivider_Update: function()
  {
    //----------------------------------------------------------------------------
    //Update repaints the root object and al it's children
    
    // We zoeken eerst de updateroot, dat is bijvoorbeeld bij een divider de BOX
    // waar de divider toe behoort
    
    objUpdateRoot=document.getElementById(this.getAttribute('updateroot'));
  
    this.style.left=0;
    this.style.width='100%';
    if (this.offsetTop<0) this.style.top=0;
      else if (this.offsetTop>(objUpdateRoot.offsetHeight-4)) this.style.top=(objUpdateRoot.offsetHeight-4)+'px';
  
    objUpdateRoot.setAttribute('divider','Y '+this.offsetTop+' DRAG');
    _ctrl.doUpdate(objUpdateRoot);
  },
  
  xDivider_Update: function()
  {
    //----------------------------------------------------------------------------
    //Update repaints the root object and al it's children
    
    // We zoeken eerst de updateroot, dat is bijvoorbeeld bij een divider de BOX
    // waar de divider toe behoort
  
    objUpdateRoot=document.getElementById(this.getAttribute('updateroot'));
    
    this.style.top=0;
    this.style.height='100%';
    if (this.offsetLeft<0) this.style.left=0;
      else if (this.offsetLeft>(objUpdateRoot.offsetWidth-20)) this.style.left=(objUpdateRoot.offsetWidth-20);
  
    objUpdateRoot.setAttribute('divider','X '+this.offsetLeft+' DRAG');
    _ctrl.doUpdate(objUpdateRoot);
  }

};

var _form=
{
	submit: function(strFormName)
	{
		objForm=document.getElementById(strFormName);
		strPostData=objForm.formSubmit();
		
		strAjaxBox=objForm.getAttribute('ajax');		
		if (strAjaxBox>'')
		{		
		  sendRequest(objForm.getAttribute('action'),strPostData,strAjaxBox);	
		}
		else
		{
			//Create realform with hidden fields to submit
			objRealForm=document.createElement('form');
			strUrl=objForm.getAttribute('action');
			objRealForm.action=strUrl.replace('[AMP]','&');
			objRealForm.method='POST';
			
			arrPostData=strPostData.split('&');
			
			for (i=0;i<arrPostData.length;i++)
			{
				strParam=arrPostData[i];
				intPos=strParam.indexOf('=');
				strParamName=strParam.substr(0,intPos);
				strParamValue=strParam.substr(intPos+1);
				objInput=document.createElement('input');
			  objInput.name=strParamName;
				objInput.value=decodeURIComponent(strParamValue);
				objInput.type='hidden';
				objRealForm.appendChild(objInput);
			}
			document.body.appendChild(objRealForm);
			objRealForm.submit();
		}
	}
}

var _checkbox=
{
	init: function(objImage)
	{
		objImage.style.width='16px';
    objImage.value=objImage.getAttribute('value');
	  if (objImage.value==1)
		{
			objImage.src='img/icons/accept.png';
		}	
		
	},
	toggle: function(objImage)
	{
	  if (objImage.value==1)
	  {
	     objImage.value=0;
	     objImage.src='img/icons/cancel.png';
	  }
	  else
	  {
	     objImage.value=1;
	     objImage.src='img/icons/accept.png';
	  }
	  
	},	
	
}

var _grid=
{
	init: function(objGrid)
	{
		objGrid.repaintContent=_grid.repaintContent;
	  _grid.load(objGrid,'');	
	},
	load: function (objGrid,data)
	{
		data+='&TABLE='+encodeURIComponent(objGrid.getAttribute('table'));
		data+='&FIELDS='+encodeURIComponent(objGrid.getAttribute('fields'));
		data+='&RULES='+encodeURIComponent(objGrid.getAttribute('rules'));
		data+='&FILTER='+encodeURIComponent(objGrid.getAttribute('filter'));
		
		var strLink=objGrid.getAttribute('link');
		strLink=strLink.replace(/&/g,'[AMP]');
		data+='&LINK='+encodeURIComponent(strLink);
	
		sendRequest('ajax/axGridList.php',data,objGrid.id);
	},
	loadName: function(strGridName,data)
	{
		objGrid=document.getElementById(strGridName);
		_grid.load(objGrid,data);
	},
	setWhenEnter: function(strGridName,data,evt)
	{
		//Set filters when ENTER is pressed
    evt = (evt); //? evt : event;
    var charCode = (evt.keyCode); //? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
    if (charCode == 13 || charCode == 3) 
      _grid.loadName(strGridName,data);
		else 
		  return true;
             		
	},
	
	scroll: function (strGridName)
  {
    labelDiv =document.getElementById("Labels_"+strGridName);
    dataDiv  =document.getElementById("Data_"+strGridName);
    //scrTop=dataDiv.scrollTop;
    scrLeft=dataDiv.scrollLeft;

    labelDiv.scrollLeft=scrLeft;
    //createCookie('GridScrollTop_'+GridName,scrTop,1);
    //createCookie('GridScrollLeft_'+GridName,scrLeft,1); 
  },

	repaintContent: function(objGrid)
	{
		var objGrid=this;
	  strGridName=objGrid.id;
    labelDiv =document.getElementById("Labels_"+strGridName);
    dataDiv  =document.getElementById("Data_"+strGridName);	
		infoDiv  =document.getElementById("Info_"+strGridName);
		
		intMasterW=objGrid.offsetWidth-2;		
		intLabelW=intMasterW-_BorderLW(labelDiv)-_BorderRW(labelDiv);
		intDataW =intMasterW-_BorderLW(dataDiv)-_BorderRW(dataDiv);
		
		labelDiv.style.width=intLabelW+'px';
		dataDiv.style.width=intDataW+'px';
		
		iInfoH=infoDiv.offsetHeight+_BorderTW(infoDiv)+_BorderBW(infoDiv);
		
		intMasterH=objGrid.offsetHeight-_PaddingT(objGrid)-_PaddingB(objGrid);
		intDataY=labelDiv.offsetHeight+_BorderTW(labelDiv)+_BorderBW(labelDiv);
		intDataH=intMasterH-intDataY-_BorderTW(dataDiv)-_BorderBW(dataDiv)-iInfoH;
		dataDiv.style.top=intDataY+'px';
		dataDiv.style.height=intDataH+'px';
		
		infoDiv.style.top=(intDataY+intDataH)+'px';
    infoDiv.style.width=(intMasterW-_BorderLW(infoDiv)-_BorderRW(infoDiv))+'px';
	},
}

var _tree=
{
  init: function(objTree)
  {
    //objTree.repaintContent=_tree.repaintContent;
    _tree.load(objTree,''); 
  },	
  load: function (objTree,data)
  {
    data+='&TABLE='+objTree.getAttribute('table');
    data+='&FIELD='+objTree.getAttribute('field');
    
    var strLink=objTree.getAttribute('link');
    strLink=strLink.replace(/&/g,'[AMP]');
    data+='&LINK='+strLink;
  
    sendRequest('ajax/axTreeView.php',data,objTree.id);
  },	
	
  checkMouse: function (event,iTreeIdx,sTreeName)
  { 
//	      var event=window.event;
        var btn=event.button;
				
        //document.all['QMenu'].style.visibility = 'hidden';

        if (btn == 2)		
		{				 
    oTree=document.getElementById(sTreeName);
    sMenuDef=oTree.getAttribute('quickmenu');
		if (!sMenuDef) return false;
    sMenu=_quickmenu.createMenu(sMenuDef,oTree.nodes[iTreeIdx]); 
    

            //if (tree[id][2]=='folder')
            //    for (itm=0;itm<ttidx;itm++)
            //        QMenu+="<A HREF='editor.php?Action=NewRec&Tbl="+ttype[itm][4]+"&Parent="+tree[id][1]+"' TARGET="+guiTreeTargetFrame+"> <IMG SRC=../img/icons/"+ttype[itm][2]+"_add.png> New "+ttype[itm][1]+"</A><BR>";


            //event.cancelBubble = true;
						//event.returnValue = false;
            _quickmenu.show(sMenu);

            return false;
        }
				// On IE left button=1, on Firefox left button=0
  },


  DrawLevel: function(strTree,ThisLevel)
  {
    var objTree=document.getElementById(strTree);

 
    var Lvl="<TABLE CLASS=tree CELLPADDING=0 CELLSPACING=0>";
    var iNode=0;
	
    for (var i=0;i<objTree.nodes.length;i++)
    {	
		  var aThisNode=objTree.nodes[i];
			var sParent=aThisNode[objTree.sTvwField];	
      if (sParent==ThisLevel)
      {
				var sChildren=_tree.DrawLevel(strTree,aThisNode.ID);

        //if (objTree.nodes[i][1]==Blk)
        //{
        //  Lvl+="<TR><TD BACKGROUND="+window._cfgBaseUrl+"/img/sys/trbg.gif><IMG SRC="+root+"/img/sys/trchild.gif></TD><TD>";
        //  Lvl+="<IMG CLASS=tree SRC="+window._cfgBaseUrl+"/img/icons/folder_x.png ALIGN=top> <FONT COLOR=AAAAAA>"+objTree.nodes[i][3]+"</FONT></TD></TR>";
        //}
        //else
        //{
				
          if (sChildren>'')
          {

            if (aThisNode.open)
            {
              iNode++;
              Lvl+="<TR><TD BACKGROUND="+window._cfgBaseUrl+"/img/sys/trbg.gif  onclick=_tree.nodeClose('"+strTree+"',"+i+")>";
              Lvl+="<IMG SRC="+window._cfgBaseUrl+"/img/sys/trmin.gif>";
              Lvl+="</TD><TD   onDblClick=_tree.pupChoose('"+strTree+"',"+i+");>";
//              Lvl+="<DIV onMouseDown=_tree.checkMouse(event,"+i+",'"+strTree+"')>";
              Lvl+="<IMG CLASS=tree SRC="+window._cfgBaseUrl+"/img/sys/trfldr_o.gif ALIGN=top> "+aThisNode.Name+"</DIV>";
              Lvl+=sChildren;
              Lvl+="</TD></TR>";
            }
            else
            {
							iNode++;
              Lvl+="<TR><TD BACKGROUND="+window._cfgBaseUrl+"/img/sys/trbg.gif  onclick=_tree.nodeOpen('"+strTree+"',"+i+")>";
              Lvl+="<IMG SRC="+window._cfgBaseUrl+"/img/sys/trplus.gif>";
              Lvl+="</TD><TD onDblClick=_tree.pupChoose('"+strTree+"',"+i+") onMouseDown=_tree.checkMouse(event,"+i+",'"+strTree+"')>";
              Lvl+="<IMG CLASS=tree SRC="+window._cfgBaseUrl+"/img/sys/trfldr.gif ALIGN=top> "+aThisNode.Name+"</TD></TR>";
            }
          }
          else
          {

						iNode++;
            Lvl+="<TR><TD BACKGROUND="+window._cfgBaseUrl+"/img/sys/trbg.gif><IMG SRC="+window._cfgBaseUrl+"/img/sys/trchild.gif></TD><TD>";
            Lvl+="<IMG CLASS=tree SRC="+window._cfgBaseUrl+"/img/sys/trfldr.gif onDblClick=_tree.pupChoose('"+strTree+"',"+i+") onMouseDown=_tree.checkMouse(event,"+i+",'"+strTree+"') ALIGN=top> "+aThisNode.Name+"</TD></TR>";
          }
        //}
      }
    }
    if (iNode>0)
		{
      Lvl+="<TR><TD><IMG SRC="+window._cfgBaseUrl+"/img/sys/trchild_end.gif></TD><TD> ... </TD></TR>";
      Lvl+="</TABLE>";			
		}
		else
		  Lvl='';

    return Lvl;
  },



  nodeClose: function (strTree,node)
  {
		var objTree=document.getElementById(strTree);
    objTree.nodes[node].open=false;
    Lvl="";
		Blk=-1;
    if (Blk<0) Lvl+="<TABLE CLASS=edit><TR><TD ALIGN=Right><A onmousedown=_tree.pupChoose('"+strTree+"',-1)>Root</A></TD></TR></TABLE>";

    Lvl+=_tree.DrawLevel(strTree,0);
    objTree.innerHTML=Lvl;

    //document.selection.empty();
  },

  nodeOpen: function (strTree,node)
  {
		var objTree=document.getElementById(strTree);
    objTree.nodes[node].open=true;
    Lvl="";
		Blk=-1;
    if (Blk<0) Lvl+="<TABLE CLASS=edit><TR><TD ALIGN=Right><A onmousedown=_tree.pupChoose('"+strTree+"',-1)>Root</A></TD></TR></TABLE>";
    Lvl+=_tree.DrawLevel(strTree,0);
    objTree.innerHTML=Lvl;

    //document.selection.empty();

  },
  
  OpenID: function (recID)
  {
      // Open tree-item based on record ID
    found=-1;
    for(t=0;t<idx;t++)
    {
       if (objTree.nodes[t][1]==recID)
       {
          found=t;t=idx;
       }
    }

    if (found>-1)
    {
       objTree.nodes[found][5]=1;
       OpenID(objTree.nodes[found][0]);
    }

  },
	
  pupTreeDialog: function (sTable,sField,oClient)
	{
    _dialog.open('pupTreeDialog',true,oClient);
    _dialog.reSize('pupTreeDialog','400px','400px');
    _dialog.position('pupTreeDialog','25%','25%');
		_dialog.setTitle('pupTreeDialog','Browse tree')

    //The callback-function of the application
    //_dialog.currDialog.onReturn=onReturn;    

    pupObj=document.getElementById('dialog_pupTreeDialog');
    pupW=pupObj.clientWidth;
    pupH=pupObj.clientHeight;

    var sDialog='';  

	  sDialog+='<DIV ID=tvwDialogTree STYLE="position:absolute;top:44px;width:100%;height:'+(pupH-45)+'px" ';
    sDialog+=' field='+sField+' table='+sTable+' link="function(){return false;}"';
    sDialog+=' ';
	  sDialog+=' ELEM_init=_tree.init(objControl)>';
  

  
    pupObj.innerHTML=sDialog;
    _ctrl.init('tvwDialogTree','');		
		
	},
	
  pupChoose: function(sTreeName,iNodeIdx)
	{
	
		oTree=document.getElementById(sTreeName);
		if (iNodeIdx<0)
		{
			
			var aNode={ID:0,Name:"root"};
			
		
		}
		else
		{
		  var aNode=oTree.nodes[iNodeIdx];	
		  
		  
		}
	
		_dialog.client.onReturn(aNode);
		_dialog.close();
	},
	
	//----------------------------------------------------------------------------
	// TVW
	tvwInit: function (objTreeField)
	{
		
		objTreeField.onReturn=_tree.returnValue;	

		objTreeField.value=objTreeField.getAttribute('value');	
	},
	
	returnValue: function(aNode)
	{
		this.value=aNode.ID;
		this.innerHTML='<IMG SRC=./img/icons/tree.png  ALIGN=absmiddle> '+aNode.Name;
		
	},	
	
}

var _quickmenu=
{
	oMenu:null,
	
	show: function (sMenu)
  {
		
		
    if (_quickmenu.oMenu!=null) _quickmenu.hide();

    _quickmenu.oMenu=document.createElement('DIV');
    _quickmenu.oMenu.style.width='150px';
    _quickmenu.oMenu.style.height='200px';		
		
    _quickmenu.oMenu.style.left=_mouse.x+'px';
    _quickmenu.oMenu.style.top=_mouse.y+'px';
		
		_SetStyleClass(_quickmenu.oMenu,'_QMenu');


    _quickmenu.oMenu.innerHTML=sMenu;
		document.body.appendChild(_quickmenu.oMenu);
    
		oItemsTbl=document.getElementById('tblQMenuItems');
		_quickmenu.oMenu.style.height=oItemsTbl.offsetHeight+'px';
  
    setTimeout("_quickmenu.hide()",10000);
		return false;
  },
	
	hide: function()
	{
		if (_quickmenu.oMenu != null) 
		{
		  document.body.removeChild(_quickmenu.oMenu);
			_quickmenu.oMenu=null;
	  }
	},
	
	createMenu: function (sMenuDef,oData)
	{
    // Definition is in JSON, lets eval it to a array
		sMenuCode='var aItems=['+unescape(sMenuDef)+'];';
		eval(sMenuCode);

		sMenu='<TABLE WIDTH=100% ID=tblQMenuItems>';
	
		for (var i in aItems)
		{

			sLink=_parseString(aItems[i].command,oData);
		  sLink=sLink.replace(/\[AMP\]/g,'&');
			sMenu+='<TR><TD CLASS=_QMenuItem onmouseover=_SetStyleClass(this,"_QMenuItemHover") onmouseout=_SetStyleClass(this,"_QMenuItem") onclick='+sLink+'>';
			sMenu+='<IMG SRC='+window._cfgBaseUrl+'/img/icons/'+aItems[i].icon+'.png ALIGN=absmiddle> '+aItems[i].title;
			sMenu+='</TD></TR>';
			
		}	
		sMenu+='</TABLE>';
		return sMenu;
	}
	
}

var _select=
{
	init: function(oSelectBox)
	{

	  oSelectBox.setValue=_select.setValue;
		oSelectBox.getValue=_select.getValue;

		oSelectBox.setValue(oSelectBox.getAttribute('value')); 	
	},
	
	setValue: function (sValue)
	{

		var oOptions=this.options;
    for (i=0;i<oOptions.length;i++)
    {
      if (oOptions[i].value==sValue)
        oOptions[i].selected=true;
      else
        oOptions[i].selected=false;
    }		
	},
	
	getValue: function()
	{
		return this.options[this.selectedIndex].value;
	}
	
}

var _img=
{
  //----------------------------------------------------------------------------
  // IMG = Image field control
  init: function(oImageField)
  {
    oImageField.onReturn=_img.returnValue; 
  },
  
  returnValue: function(sImgName)
  {
    this.value=sImgName;
    this.innerHTML='<IMG SRC=./img/icons/image.png  ALIGN=absmiddle> '+sImgName;  
  },
}

var _menu=
{
  dropdown: function(oItem,bLeft)
  {
    oParent=oItem.mnuparent;
    
    if (oParent.oMenu!=null) _menu.close(oParent.oMenu);

    oParent.oMenu=document.createElement('DIV');
    //oParent.oMenu.style.width='150px';
    oParent.oMenu.style.height='200px';    
    
    oParent.oMenu.style.left=oItem.offsetLeft+'px';
    oParent.oMenu.style.top= (oItem.offsetTop+24)+'px';
    
    _SetStyleClass(oParent.oMenu,'_dropdownList');

    sTableName='ddmenu_'+oItem.id;
		sMenu='<TABLE ID='+sTableName+' WIDTH=100%>';
    for (var i in window.aPageMainMenu)
		{
			
			oChild=window.aPageMainMenu[i];
		
			if (oChild.tvwsys_Pages==oItem.id)
			   sMenu+='<TR><TD STYLE="overflow:hidden;" NOWRAP onClick=_goURL("?PAGE='+oChild.ID+'");>'+oChild.Name+'</TR></TD>';
			
		}
		sMenu+='</TABLE>';

    oParent.appendChild(oParent.oMenu);
    
		oParent.oMenu.innerHTML=sMenu;
    oItemsTbl=document.getElementById(sTableName);
    oParent.oMenu.style.height=oItemsTbl.offsetHeight+'px';
  
    setTimeout("_menu.close(oParent.oMenu)",10000);
    return false;      
  },
	
	close: function(oMenu)
	{
		if (oMenu.oMenu!=null) _menu.close(oMenu.oMenu);
		if (oMenu.parentNode!=null) oMenu.parentNode.removeChild(oMenu);
	}
}

var _relation=
{
	init: function(oRelationObj)
	{
		oRelationObj.getValue=_relation.getValue;
	},
	
  moveIn: function(sControlName)
	{
    oIn =document.getElementById('in_'+sControlName);	
		oOut=document.getElementById('out_'+sControlName);	
		_moveSelected(oOut,oIn);
	},
  moveOut: function(sControlName)
  {
    oIn =document.getElementById('in_'+sControlName); 
    oOut=document.getElementById('out_'+sControlName);  
    _moveSelected(oIn,oOut);
  },
	
	getValue: function()
	{
		var sInName='in_'+this.id;
		var oIn =document.getElementById(sInName); 
		var sNewInList='';
		var sNext='';
		
    for (var i=0, l=oIn.options.length;i<l;i++) 
		{
			  sOptionValue=oIn.options[i].value;
				if (sOptionValue.substring(0, 1) != '_') 
				{
					//If not starting with _ then it wasn't already there
			    sNewInList += sNext + sOptionValue;
			    sNext = '|';
		    }
    }	
    var sOutName='out_'+this.id;
    var oOut =document.getElementById(sOutName); 
    var sNewOutList='';
    var sNext='';		
    for (var i=0, l=oOut.options.length;i<l;i++) 
    {
        sOptionValue=oOut.options[i].value;
        if (sOptionValue.substring(0, 1) == '_') 
        {
          //If not starting with _ then it wasn't already there
          sNewOutList += sNext + sOptionValue.substring(1);
          sNext = '|';
        }
    }		
		
		  	
		sRet='{';
		sRet+='"relationtable":"'+this.getAttribute('relationtable')+'",';
		sRet+='"childfield":"'+this.getAttribute('childfield')+'",';
		sRet+='"parentfield":"'+this.getAttribute('parentfield')+'",';
		sRet+='"parentid":"'+this.getAttribute('parentid')+'",';
		sRet+='"in":"'+sNewInList+'",';
		sRet+='"out":"'+sNewOutList+'"}';
		return sRet;
	},	
	
	
	
}

var _date=
{
	pupDateDialog: function(oClient,sTitle)
	{
    _dialog.open('pupDateDialog',true,oClient);
    _dialog.reSize('pupDateDialog','300px','250px');
    _dialog.position('pupDateDialog','25%','25%');
    _dialog.setTitle('pupDateDialog',sTitle);
		
		oClient.onReturn=_date.pupReturn;
		
    oPup=document.getElementById('dialog_pupDateDialog');
    pupW=oPup.clientWidth;
    pupH=oPup.clientHeight;		
	
		sDate=oClient.value;
		//sDate=sDate.trim();
		aDateTime=sDate.split(' ');
		aDateParts=aDateTime[0].split('-');
		_date.nDay=parseInt(aDateParts[0]);
		_date.nMonth=parseInt(aDateParts[1]);
		_date.nYear=parseInt(aDateParts[2]);
		
		oPup.innerHTML=_date.makeCalender();		
	},

  MaxDays:new Array(99,31,28,31,30,31,30,31,31,30,31,30,31),
	MNames:new Array("","januari","februari","maart","april","mei","juni","juli","augustus","september","oktober","november","december"),
  aWeekDays:new Array("ma","di","wo","do","vr","za","zo"),
 
  nYear:2009,
	nMonth:10,
	nDay:26,

  makeCalender:function()
  {
    var FirstDay = new Date(_date.nYear,_date.nMonth-1,1);
    var Day=FirstDay.getDay()-2;
    if (Day<0) Day=7+Day;
    Day=Day*-1;
    //if (Day<-5) Day+=7;


    var Max=_date.MaxDays[_date.nMonth];
    if (_date.nMonth==2) Max=29;

    var bdy="<CENTER>";
    bdy+="<H3>"+_date.MNames[_date.nMonth]+" "+_date.nYear+"</H3>";


    var nxt ="<A onClick=javascript:PrevMonth()> <IMG SRC=img/sys/icBack16.gif ALIGN=top> </A>";
    nxt+="<SELECT onChange=_date.otherDay(this)>";
    for (t=1;t<=Max;t++)
    {
        if (t==_date.nDay)
            nxt+="<OPTION SELECTED>"+t+"</OPTION>";
        else
            nxt+="<OPTION>"+t+"</OPTION>";

    }
    nxt+="</SELECT> ";

    nxt+="<SELECT onChange=_date.otherMonth(this)>";
    for (t=1;t<13;t++)
    {
        if (t==_date.nMonth)
            nxt+="<OPTION SELECTED VALUE="+t+">"+_date.MNames[t]+"</OPTION>";
        else
            nxt+="<OPTION VALUE="+t+">"+_date.MNames[t]+"</OPTION>";
    }
    nxt+="</SELECT> ";

    nxt+="<SELECT onChange=_date.otherYear(this)>";
    for (t=_date.nYear-25;t<_date.nYear+25;t++)
    {
        if (t==_date.nYear)
            nxt+="<OPTION VALUE="+t+" SELECTED>"+t+"</OPTION>";
        else
            nxt+="<OPTION VALUE="+t+">"+t+"</OPTION>";

    }
    nxt+="</SELECT> ";


    //nxt+="<IMG SRC=img/nodot.gif WIDTH=200 HEIGHT=1>";
    nxt+="<A onClick=javascript:NextMonth()> <IMG SRC=img/sys/icForward16.gif ALIGN=top> </A>";


    bdy+=nxt;

    bdy+="<TABLE STYLE='width:100%;text-align:center;'><TR CLASS=_Label>";
    for (d=0;d<7;d++)
		  bdy+="<TD>"+_date.aWeekDays[d]+"</TD>";
		bdy+="</TR>";	

    for (w=0;w<5;w++)
    {
        bdy+="<TR HEIGHT=20>";

        for (d=0;d<7;d++)
        {
            if (Day>0)
                if (Day>Max)
                    bdy+="<TD CLASS=_Label></TD>";
                else
                    if (Day==_date.nDay)
                        bdy+="<TD CLASS=_Control><A onDblClick='_date.selectDate("+Day+");'>"+Day+"</A></TD>";
                    else
                        bdy+="<TD CLASS=day><A onDblClick='_date.selectDate("+Day+");'>"+Day+"</A></TD>";
            else
                bdy+="<TD CLASS=_Label></TD>";

            Day++;
        }
        bdy+="</TR>";
    }

    bdy+="</TABLE>";

    //bdy+="<BUTTON onClick=Okay()>Ok</BUTTON>";
    //bdy+=nxt;
    return bdy;
  },
	
	pupReturn: function()
	{
		_dialog.client.value=_date.nDay+"-"+_date.nMonth+"-"+_date.nYear;
	},
		
  otherMonth: function(ObjRef)
  {
    var Idx=ObjRef.selectedIndex;
    var Val=ObjRef.options[Idx].text;
    var Inf=ObjRef.options[Idx].value;

    _date.nMonth=Inf;
    oPup=document.getElementById('dialog_pupDateDialog');
		oPup.innerHTML=_date.makeCalender();
  },
  otherYear:function (ObjRef)
  {
    var Idx=ObjRef.selectedIndex;
    var Val=ObjRef.options[Idx].text;
    var Inf=ObjRef.options[Idx].value;
    _date.nYear=parseInt(Inf);
		
    oPup=document.getElementById('dialog_pupDateDialog');
    oPup.innerHTML=_date.makeCalender();
  },
  otherDay:function(ObjRef)
  {
    var Idx=ObjRef.selectedIndex;
    var Val=ObjRef.options[Idx].text;
    //var Inf=ObjRef.options[Idx].value;

    _date.nDay=Val-0;
    oPup=document.getElementById('dialog_pupDateDialog');
    oPup.innerHTML=_date.makeCalender();
  },
  selectDate: function(iSelectDay)
  {
    _date.nDay=iSelectDay;
    _dialog.submit();
  }	
}
