//------------------------------------------------------------------------------
// DEFAULTCONTROL
// We are making a controlobject by extending DOM objects with our own methods 
// and attributes

var StyleAttr=' |border|left|top|height|width|background|visibility|font|border|overflow|clip|';

function CreateDefaultControl(Name,Functions)
{
  var DomObj=document.getElementById(Name);
//alert(Name+':'+DomObj);  
  DomObj.getValue=_default_getValue;
  DomObj.setValue=_default_setValue;
  DomObj.Blink=_default_Blink;
  DomObj.onfocus=_default_gotFocus;
  DomObj.onblur=_default_lostFocus;
  
  DomObj.FormSubmit=_default_FormSubmit;
  DomObj.Repaint=_default_Repaint;
  DomObj.validaterules=new Array();
  DomObj.controls=new Array();
   
  ParentName=DomObj.getAttribute('parent')
  if (ParentName>'')
  {
      Parent=document.getElementById(ParentName);
      Parent.controls.push(DomObj.id);
      
      var Divd=Parent.getAttribute('divider');
      if (Divd)
      {
        Divd=Divd.split(' ');
        if (Divd[0]=='Y')
          if (DomObj.offsetTop>Divd[1])
            DomObj.setAttribute('division','DOWN');
          else
            DomObj.setAttribute('division','UP'); 
        else
          if (DomObj.offsetLeft>Divd[1])
            DomObj.setAttribute('division','RIGHT');
          else
            DomObj.setAttribute('division','LEFT');                    
      }
      
  } 
     
  if (DomObj.getAttribute('elem_init')>'') eval(DomObj.getAttribute('elem_init'));

  Functions=Functions.split('[=]');
  for(i in Functions)
  {
    Funcs=Functions[i].split('[+]');
    if (Funcs[0]>'') eval("this."+Funcs[0]+'='+Funcs[1]+';');
  }

  

 
  DomObj.Repaint(); 
}
function _default_getValue()
{
  return this.value;
}  
function _default_setValue(Value)
{
  this.value=Value;
}

function _default_Update()
{
  //----------------------------------------------------------------------------
  //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
  
  UpdRoot=document.getElementById(this.getAttribute('updateroot'));
  DoUpdate(UpdRoot);
}

function DoUpdate(Obj)
{
  Obj.Repaint(); 
  
  if (Obj.getAttribute('container'))
  {  
  
    children=Obj.controls;
    for (i in Obj.controls)
    { 
      child=document.getElementById(Obj.controls[i]);
      if (child!=null) DoUpdate(child);
    }    
  }

}

function _default_Repaint()
{
  // The real painting of cause is done by the browser but here we set
  // size and location at init and after resizing and moving
  ParentName=this.getAttribute('parent');
  if (ParentName=="") return;

  Parent=document.getElementById(ParentName);
  
  Padding='';
  if (Parent!=null) Padding=Parent.style.padding;
  if (Padding=='') Padding='0 0 0 0';
  
  Padding=Padding.replace(/px|pt|em/gi,'');
  Padding=Padding.split(' ');
  
  Divider=Parent.getAttribute('divider');
  if (Divider>'')
  {
    Divider=Divider.split(' ');
    
    if (Divider[0]=='Y')
    {
      MyDiv=this.getAttribute('division');
      if (MyDiv=='DOWN')
      {
        ParentTop=parseInt(Divider[1])+4;
        ParentHeight=Parent.offsetHeight-parseInt(Divider[1])-4;
      }
      else
      {
        ParentTop=0;
        ParentHeight=Divider[1];    
      }
      ParentLeft=0;
      ParentWidth=Parent.offsetWidth;    
    }
    else
    {
      MyDiv=this.getAttribute('division');
      if (MyDiv=='RIGHT')
      {
        ParentLeft=parseInt(Divider[1])+4;
        ParentWidth=Parent.offsetWidth-parseInt(Divider[1])-4;
      }
      else
      {
        ParentLeft=0;
        ParentWidth=Divider[1];    
      }  
      ParentTop=0;
      ParentHeight=Parent.offsetHeight;
    }
  }
  else
  {
    ParentTop=0;ParentLeft=0;
    ParentWidth=Parent.offsetWidth;
    ParentHeight=Parent.offsetHeight;
    
  }
  if (ParentHeight<0) ParentHeight=0;
  if (ParentWidth<0) ParentWidth=0;
 
  switch(this.getAttribute('dock'))
  {
    case 'LEFT':
      this.style.left=ParentLeft+Padding[3];
      break;
    case 'TOP':
      this.style.top=ParentTop+Padding[0];
      break;
    case 'RIGHT':
      //var ParentRight=Parent.offsetWidth;
      var MyRight=this.offsetWidth;
      this.style.left=ParentWidth-MyRight-Padding[1];
      break;  
   case 'BOTTOM':
      //var ParentHeight=Parent.offsetHeight;
      var MyHeight=this.offsetHeight;
      this.style.top=ParentHeight-MyHeight-Padding[2];
      break; 
   case 'FILLX':
      this.style.left=(parseInt(ParentLeft)+parseInt(Padding[3]))+'px';   
      this.style.width=(parseInt(ParentWidth)-parseInt(Padding[1])-parseInt(Padding[3]))+'px';
      break;
   case 'FILL':
//alert(this.id+'::'+ParentWidth+'-'+parseInt(Padding[1])+'-'+parseInt(Padding[3])+'px');   
      this.style.left=(parseInt(ParentLeft)+parseInt(Padding[3]))+'px'; 
      this.style.top=(parseInt(ParentTop)+parseInt(Padding[0]))+'px';  
      this.style.width=(parseInt(ParentWidth)-parseInt(Padding[1])-parseInt(Padding[3]))+'px';
      this.style.height=(parseInt(ParentHeight)-parseInt(Padding[0])-parseInt(Padding[2]))+'px';  
      break; 
  }
  
}

function _default_FormSubmit()
{
  var Ret='';
  
  if (this.getAttribute('container'))
  {
    children=this.controls;
    for (i in children)
    {
      child=document.getElementById(children[i]);
      Ret+=child.FormSubmit();
    }
  }
  else
  { 
    value=''+this.getValue();
    if (value!=null)
    {
      value=value.replace(/&/g,'[AMP]');
      Ret='&'+this.id+'='+encodeURI(value);      
    }
  }  
  return Ret;
}

function _default_gotFocus()
{
  this.style.borderColor=cfgDesignAlertColor;
  
}
function _default_lostFocus()
{
  var Ret='OK';
  this.style.borderColor=cfgDesignBorderColor;
  
  if (this.validate) Ret=this.validatefunction();
  
  if (Ret!='OK')
    alert('Waarde in veld niet juist:\n'+$Ret);
}

function _default_doNothing()
{
  // Standaardfunctie zonder actie
}

function _default_Blink()
{
  this.BlinkStatus=0;
  this.BlinkCycles=0;
  this.BlinkOrigBgColor=this.style.backgroundColor;
  this.BlinkInterval=window.setInterval('BlinkExecute("'+this.id+'")',15);  
}
function BlinkExecute(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(cfgDesignBackGroundColor);
    End=hex2num(cfgDesignNormalColor);
    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=num2hex(Start);
  }
  
}





