isInteger = function(val) {
  for(var i=0;i<val.length;i++){
    if(!isDigit(val.charAt(i))){return false;}
  }
  return true;
};

isNumeric = function(val) {
  val = this;
  return(parseFloat(val,10)==(val*1));
};

isDigit = function(num) {
  if (num.length>1){return false;}
  var string="1234567890";
  if (string.indexOf(num)!=-1){return true;}
  return false;
};


//-------------------------------------------------------------------
// Define WField module
//-------------------------------------------------------------------

WField = {
  "name":"WField.js",
  "need":"WString.js,WUtility.js,WDate.js"
};

//-------------------------------------------------------------------
// WField.Base (basic inputfield widget)
//-------------------------------------------------------------------
WField.Base = function() {
  this.version = '0.1';
  this.wform = null; 
  this.leaveField = null;  
  this.afterFieldValidated = null;
  this.errorMessage = '';
  bindMethods(this);
};

WField.Base.prototype = {
  setOptions: function(options) {
    update(
      this.options = {
        tab:        null,
        label:      'basefield',
        editable:   1,
        visible:    1,
        required:   0,
        size:       '30',
        maxlength:  0,
        defaultval: 'basefield',
        assistant:  'Assistant content'
      }, options || {});
  },
  init: function(id, frm, options, wform) {
    this.wform = wform;
    this.id = id;
    this.element = (frm==null) ? null : getElement(frm);
    this.assistant = null;
    this.input = new Array(),
    this.setOptions(options || {});
    this.render();
    this.addLabelEvents();
    this.value = null; 
    this.renderedField;
  },
  render: function() {
    this.generateInput();
    this.addInputEvents();
    this.setDefault();
    this.setTabIndex();
    if (this.wform.use_assistant) {
      if (this.renderAssistant()) {
        row.appendChild(this.assistant);      
        // Say it again so IE grasps it.
        this.assistant.style.display = 'none';
      }
    }
    // the actual composition
    if (this.element)
      this.addToForm(row);
    this.renderedField  = row;

    return;
  },
  setTabIndex: function() {
    if (this.options.editable) {
      if (this.options.values) {
        for (i = 0;i < this.options.values.length;i++) {
          this.input[i].setAttribute('tabindex', '1');
        }
      } else {
        this.input[0].setAttribute('tabindex', '1');
      }
    } else {
      if (this.options.values) {
        for (i = 0;i < this.options.values.length;i++) {
          this.input[i].setAttribute('tabindex', '-1');
          this.input[i].setAttribute('disabled', 'true');
        }
      } else {
        this.input[0].setAttribute('tabindex', '-1');
        this.input[0].setAttribute('disabled', 'true');
      }
    }                
  },
  addToForm: function(row) {
    this.element.appendChild(row);
  },
  setDefault: function() {              
    if (this.options.defaultval)
      this.setValue(this.options.defaultval);   
    else
      this.setValue('');   
  },
  setFocus: function() {
    try {
      this.input[0].focus();
      this.input[0].select();
    } catch(e) {}
  },
  generateInput: function() {
    this.input[0] = INPUT(
      {'type':'text',
       'id':this.id,
       'name':this.id+':string',
       'size':this.options.size});
    this.label =  createDOM('label',{'class':'label','id':'label_'+this.id,'for':'fld_'+this.id}, this.options.label);
    row = DIV({'id': 'fld_'+this.id,'class':'inputrow'},
              DIV({'class':(this.options.required) ? 'required' : 'notrequired','id':'reqrd_'+this.id}),
              this.label,
              this.input[0]);
    if (this.options.maxlength > 0)
      this.input[0].setAttribute('maxlength', this. options.maxlength);
    if (!this.options.visible) {
      row.style.display = 'none';
    }
  },
  renderAssistant: function() {               
    if (this.options.assistant) {
      ast_text = DIV({'class':'topassistant'},null);
      ast_text.innerHTML = this.options.assistant;
      this.assistant = DIV({'class':'assistant',
                            'id':'ast_'+this.id,
                            'style':'display:none'},
                            ast_text,
                            DIV({'class':'bottomassistant'}));
      return this.assistant;
    } else {
      this.assistant = null;
      return this.assistant;
    }
  },
  showAssistant: function() {
    if (this.assistant) {
      showElement(this.assistant);
      if (document.all) {
        this.assistant.style.top = this.input[0].parentNode.offsetTop - (this.assistant.offsetHeight - 9)  + 'px';
      } else {
        this.assistant.style.top = this.input[0].parentNode.offsetTop - this.assistant.offsetHeight  + 'px';
      }
      astid = 'ast_'+this.id;               
    }
  },
  hideAssistant: function() {
    if (this.assistant)
      hideElement(this.assistant);
  },
  setValue: function(val) {
    this.input[0].value = val;
    this.syncValue();
  },
  syncValue: function() {
    this.value = this.input[0].value;
  },       
  formatValue: function() {
    if (this.options.format) {
        ;
    }
  },
  labelClicked: function(evt) {
    target = evt.target();
    theid = target.id.slice(6);    
    getElement(theid).focus();
    getElement(theid).select();
  },
  setLabel: function(val) {
   this.options.label = val;
   lbl = getElement('label_'+this.id);
   lbl.removeChild(lbl.firstChild);
   lbl.appendChild(document.createTextNode(val));
  },
  toggleEditable: function() {
    if ((this.options.required) && (strip(this.input[0].value)=='')){
     return;
    }
    if (this.options.editable) {
      this.options.editable = false;     
      for (x = 0; x < this.input.length; x++) {
        this.input[x].setAttribute('disabled', true);
        this.input[x].removeAttribute('tabindex');
      }
    } else {
      this.options.editable = true;
      for (x = 0; x < this.input.length; x++) {
        this.input[x].removeAttribute('disabled');
        this.input[x].setAttribute('tabindex', '1');
      }
    }
  },
  toggleRequired: function(val) {
    if (!this.options.editable) return;

    if (this.options.required) {
      this.options.required = false;
      getElement('reqrd_'+this.id).className='notrequired';
    } else {
      this.options.required = true;
      getElement('reqrd_'+this.id).className='required';
    }
  },
  toggleVisibility: function () {     
    if (this.options.visible) {
      this.options.visible = false;
      $('fld_'+this.id).style.display = 'none';      
    } else {
      this.options.visible = true;
      $('fld_'+this.id).style.display = 'block';      
    }
  },
  setVisible: function () {     
    if (!this.options.visible) {
      this.options.visible = true;
      $('fld_'+this.id).style.display = 'block';      
    }
  },
  setInvisible: function () {     
    if (this.options.visible) {
      this.options.visible = false;
      $('fld_'+this.id).style.display = 'none';      
    }
  },
  addInputEvents: function() {
  	connect(this.input[0], 'onfocus',this.onInputFocus);
  	connect(this.input[0], 'onblur', this.onInputBlur);
  },
  addLabelEvents: function()
  {
    connect(this.label, 'onclick' ,this.labelClicked);
    this.label.style.cursor = (document.all) ? 'hand' : 'pointer';
  },
  onInputFocus: function(evt) {
    evt.target().className='selected';
    try {
      if (this.wform.use_assistant) this.showAssistant();     
    } catch(e) {};
  },
  onInputBlur: function(evt) {
    target = evt.target();
    evt.returnValue = false;
    try {
      this.formatValue();
      this.syncValue();
      if (this.wform.use_assistant) this.hideAssistant();
      evt.returnValue = this.checkValue();
    } catch(e) {alert(e);}                
    
    // Make userchecks possible
    if (this.leaveField != null) {
      evt.returnValue = this.leaveField();
    }
    
    // Reflect the result in the userinterface
    if (evt.returnValue) {
      evt.target().className='';
      if (this.afterFieldValidated != null) {
        this.afterFieldValidated();
      }
    } else {
      target.className='notvalid';
    }
    return evt.returnValue;
  },
  checkValue: function() {

    if (this.value == null) {
      this.value = '';
    }
    if ((this.options.required) && (strip(this.value)=='')) {
      this.setErrorMessage(this.options.label+': Dit is een verplicht veld.');
      return false;
    }
    return true;
  },
  setErrorMessage: function(txt) {
    this.errorMessage = txt;
  }
};

//-------------------------------------------------------------------
// WField.String (String inputfield widget)
//-------------------------------------------------------------------
WField.String = function() {
  this.version = '0.1';
  bindMethods(this);
};

WField.String.prototype = update (new WField.Base(), {
  test: function() {
    /* Aanroep parent class
    ParentClass.prototype.init.call(this, arg1, arg2);

    or (to call with the same arguments):
    ParentClass.prototype.init.apply(this, arguments);
    */
  },
  init: function(id,frm,options,wform) {
    WField.Base.prototype.init.call(this, id,frm,options,wform);
  }  
});  

//-------------------------------------------------------------------
// WField.Email (Email inputfield widget)
//-------------------------------------------------------------------
WField.Email = function() {
  this.version = '0.1';
  bindMethods(this);
};

WField.Email.prototype = update (new WField.String(), {
  /* Overrule and add methods */
  checkValue: function() {
    if (this.value == null) {
      this.value = '';
    }
    if ((this.options.required) && (strip(this.value)=='')) {
      // Field is required and empty
      this.setErrorMessage(this.options.label+': Dit is een verplicht veld.');
      return false;
    }                     
    this.input[0].value = strip(this.input[0].value);     
    this.syncValue();
    if (this.value.indexOf(' ')>-1) {
      // No spaces allowed
      this.setErrorMessage(this.options.label+': Veld bevat spaties, dit is niet toegestaan.');
      return false;
    }
    spat = this.value.split('@');
    if (spat.length != 2) {
      // more than one @ sign                        
      this.setErrorMessage(this.options.label+': Veld moet een @ bevatten.');
      return false;
    }
    spdom = spat[1].split('.');
    if (spdom.length < 2) {
      // need at least one dot in the domain
      this.setErrorMessage(this.options.label+': De domainnaam is niet correct.');
      return false;
    }                                           
    tlvl = spdom[spdom.length-1].length;
    //alert('toplevel length: '+tlvl+' '+(tlvl != 2) && (tlvl != 3));
    if ((tlvl != 2) && (tlvl != 3)) {
      // toplevel domain is 2 or 3 chars long
      this.setErrorMessage(this.options.label+': De toplevel domainnaam ".'+spdom[spdom.length-1]+'" is niet correct.');
      return false;
    }
    return true;
  },                               
  formatValue: function() {          
    // force to lowercase
    this.input[0].value =  this.input[0].value.toLowerCase();
  }
});

//-------------------------------------------------------------------
// WField.Integer (Integer inputfield widget)
//-------------------------------------------------------------------
WField.Integer = function() {
  this.version = '0.1'; 
  bindMethods(this);
};
/* Take all behaviors from Base */
WField.Integer.prototype = update (new WField.String(), {
  /* Overrule methods */
  checkValue: function() {
    if (this.value == null) {
      this.value = '0';
    }        
    if ((this.options.required) && (strip(this.input[0].value)=='')) {
      this.input[0].select();
      this.setErrorMessage(this.options.label+' invoer is verplicht');
      return false;
    }
    if (isInteger(this.value))
      return true;
    else {
      this.setErrorMessage(this.options.label+': Alleen gehele getallen toegestaan');
//      alert('Alleen gehele getallen')
      return false;
    }
    return true;
  },                             
  formatValue: function() {
     if (this.options.format) {  
       this.input[0].value = numberFormatter(this.options.format)((this.input[0].value=="")? 0 :this.input[0].value);
     }
  }
});
//-------------------------------------------------------------------
// WField.Date (Date inputfield widget)
//-------------------------------------------------------------------

WField.Date = function() {
  this.version = '0.1';
  bindMethods(this);
};
WField.Date.prototype = update(new WField.Base(),{
/* Overrule and add methods */
                  
  init: function(id,frm,options,wform) {
    WField.Base.prototype.init.call(this, id,frm,options,wform);
    if (!this.options.format)
      this.options.format=('Y,M,D');
    if (!this.options.dateseparator)
      this.options.dateseparator = '-'
    if (!this.options.timeseparator)
      this.options.timeseparator = ':'
  },
  generateInput: function() {            
    // find out the format
    this.dateorder = this.options.format.split(',');
    for (a=0;a<this.dateorder.length;a++) {
      switch(this.dateorder[a]) {
        case 'Y':
          this.dateorder[a] = 0;
          break;
        case 'M':
          this.dateorder[a] = 1;
          break;
        case 'D':
        this.dateorder[a] = 2;
          break;
      }
    }
    this.checkOn = this.dateorder[2];
    this.input[0] = INPUT(
      {'type':'text',
       'style':'margin-right:3px',
       'id':this.id,
       'name':this.id+'_year',
       'tabindex':1,
       'maxlength':'4',
       'size':'4'});
     this.input[1] = INPUT(
       {'type':'text',
        'style':'margin-right:3px',
        'id':this.id,
        'name':this.id+'_month',
        'tabindex':1,
        'maxlength':'2',
        'size':'2'});
      this.input[2] = INPUT(
        {'type':'text',
         'style':'margin-right:3px',
         'id':this.id,
         'name':this.id+'_day',
         'tabindex':1, 
         'maxlength':'2',
         'size':'2'});
      this.label = createDOM('label',{'class':'label','id':'label_'+this.id,'for':'fld_'+this.id},this.options.label);
      row = DIV({'id': 'fld_'+this.id,'class':'inputrow'},
                DIV({'class':(this.options.required) ? 'required' : 'notrequired','id':'reqrd_'+this.id}),
                  this.label,
                  this.input[this.dateorder[0]],
                  this.input[this.dateorder[1]],
                  this.input[this.dateorder[2]]);
      if (!this.options.visible) {
        row.style.display = 'none';
      }
  },

  checkValue: function() {
    return true;
  },
  setDefault: function() {
    if (!this.options.defaultval) {
      this.options.defaultval = 'today';
    }
    switch (this.options.defaultval) {
    case 'today':
      this.setValue(toISOTimestamp(new Date()));
      break;
    default:
      this.setValue(this.options.defaultval);     
      break;
    }                             
  },
  setValue: function(val) {
      if (val==null) {
        this.value = new Date();
      } else {
        this.value = isoDate(val);
      }                               
      this.input[0].value = numberFormatter("0000")(this.value.getFullYear());
      this.input[1].value = numberFormatter("00")(this.value.getMonth()+1);
      this.input[2].value = numberFormatter("00")(this.value.getDate());
  },
  syncValue: function() {
    this.value = new Date(parseInt(this.input[0].value),parseInt(this.input[1].value),parseInt(this.input[2].value))
  },
  addInputEvents: function() {
      connect(this.input[0], 'onfocus', this.onInputFocus);
      connect(this.input[1], 'onfocus', this.onInputFocus);
      connect(this.input[2], 'onfocus', this.onInputFocus);

      connect(this.input[0], 'onblur', this.onInputBlur);
      connect(this.input[1], 'onblur', this.onInputBlur);
      connect(this.input[2], 'onblur', this.onInputBlur);
  },
  onInputBlur: function(evt) {
		target = evt.target();
    target.className='';
    try {
      this.formatValue();
      this.syncValue();
      if (this.wform.use_assistant) this.hideAssistant();
      if (target.name == this.input[2].name) { // Day          
        if (strip(target.value)=='') {
          target.value = new Date().getDate();
        }
        if (parseInt(target.value) > 31) {
          target.value = 31;
        }
        if (parseInt(target.value) < 1) {
          target.value = 1;
        }
      }  
      if (target.name == this.input[1].name) { // Month
        if (parseInt(target.value) > 12) {
          target.value = 12;
        } else if (parseInt(target.value) < 1) {
          target.value = 1;
        } else if (strip(target.value)=='') {
          target.value = new Date().getMonth()+1;
        }
      }  
      if (target.name == this.input[0].name) { // Year      
        if (parseInt(target.value)<50) {
          target.value = parseInt(target.value)+2000;
        } else if (parseInt(target.value)>=50 && parseInt(target.value)<100) {
          target.value = parseInt(target.value)+1900;        
        } else if (strip(target.value)=='') {
          target.value = new Date().getFullYear();
        }
        target.value = numberFormatter("0000")(target.value);
      }  
      if (target.name == this.input[this.checkOn].name) {
        return this.checkValue();
      }    
      return true;
    } catch(e) {alert(e);}
  }  
});                           
//-------------------------------------------------------------------
// WField.File (File uploadfield widget)
//-------------------------------------------------------------------

WField.File = function() {
  this.version = '0.1';            
  bindMethods(this);  
};

WField.File.prototype = update (new WField.Base(), {
  generateInput: function() {
     this.input[0] = INPUT(
       {'type':'file',
        'id':this.id,
        'name':this.id+':file',
        'size':this.options.size});
     this.input[0].setAttribute('name',this.id+':file')

     this.label = createDOM('label',{'class':'label','id':'label_'+this.id,'for':'fld_'+this.id},this.options.label);
     row = DIV({'id': 'fld_'+this.id,'class':'inputrow'},
               DIV({'class':(this.options.required) ? 'required' : 'notrequired','id':'reqrd_'+this.id}), 
               this.label,
               this.input[0]);
     if (this.options.maxlength > 0)
       this.input[0].setAttribute('maxlength', this. options.maxlength);
     if (!this.options.visible) {
       row.style.display = 'none';
     }    
   }  
});
//-------------------------------------------------------------------
// WField.Image (Image uploadfield widget)
//-------------------------------------------------------------------

WField.Image = function() {
  this.version = '0.1';            
  bindMethods(this);  
};

WField.Image.prototype = update (new WField.Base(), {
    generateInput: function() {
       this.input[0] = INPUT(
         {'type':'file',
          'id':this.id,
          'name':this.id+':file',
          'size':this.options.size});
       this.input[0].setAttribute('name',this.id+':file')
       this.label = createDOM('label',{'class':'label','id':'label_'+this.id,'for':'fld_'+this.id},this.options.label);
       row = DIV({'id': 'fld_'+this.id,'class':'inputrow'},
                 DIV({'class':(this.options.required) ? 'required' : 'notrequired','id':'reqrd_'+this.id}), 
                 this.label,
                 this.input[0]);
       if (this.options.maxlength > 0)
         this.input[0].setAttribute('maxlength', this. options.maxlength);
       if (!this.options.visible) {
         row.style.display = 'none';
       }    
     }  
});

//-------------------------------------------------------------------
// WField.Password (Password inputfield widget)
//-------------------------------------------------------------------
WField.Password = function() {
  this.version = '0.1';            
  bindMethods(this);
};                          

WField.Password.prototype = update (new WField.String(), {
  /* Overrule methods */
  generateInput: function() {
     this.input[0] = INPUT(
       {'type':'password',
        'id':this.id,
        'name':this.id+':string',
        'size':this.options.size});
     this.label = createDOM('label',{'class':'label','id':'label_'+this.id,'for':'fld_'+this.id},this.options.label);
     row = DIV({'id': 'fld_'+this.id,'class':'inputrow'},
               DIV({'class':(this.options.required) ? 'required' : 'notrequired','id':'reqrd_'+this.id}), 
               this.label,
               this.input[0]);
     if (this.options.maxlength > 0)
       this.input[0].setAttribute('maxlength', this. options.maxlength);
     if (!this.options.visible) {
       row.style.display = 'none';
     }    
   },
  checkValue: function() {
    if (this.value == null) {
      this.value = '';
    }        
    if ((this.options.required) && (strip(this.value)=='')) {
      this.setErrorMessage(this.options.label+': Invoer is verplicht');
      return false;
    }
    if (this.options.minlength > this.value.length ) {
      this.setErrorMessage(this.options.label+': Dit veld moet minimaal '+this.options.minlength+' karakters bevatten');      
      return false;
    }                           
    return true;
  }
});

//-------------------------------------------------------------------
// WField.Checkbox (Checkbox inputfield widget)
//-------------------------------------------------------------------
WField.Checkbox = function() {
  this.version = '0.1'; 
  bindMethods();
};
/* Take all behaviors from Base */
WField.Checkbox.prototype = update (new WField.Base(), {
  generateInput: function() {
     this.input[0] = INPUT(
       {'type':'checkbox',
        'class':'wcheckbox',
        'id':this.id,
        'name':this.id+':checkbox',
        'size':this.options.size});
     this.label = createDOM('label',{'class':'label','id':'label_'+this.id,'for':'fld_'+this.id},this.options.label);
     row = DIV({'id': 'fld_'+this.id,'class':'inputrow'},
               DIV({'class':(this.options.required) ? 'required' : 'notrequired','id':'reqrd_'+this.id}),
               this.label,
//               DIV({'class':'label','id':'label_'+this.id},this.options.label),
               this.input[0]);
     if (this.options.maxlength > 0)
       this.input[0].setAttribute('maxlength', this. options.maxlength);
     if (!this.options.visible) {
       row.style.display = 'none';
     }    
   },
  setDefault: function() {
    if (!this.options.defaultval) {
      this.options.defaultval = false;
    }
    switch (this.options.defaultval) {
    case true:
      this.input[0].checked = true;
      break;
    case false:
      this.input[0].checked = false;
      break;
    }                             
  },
  setValue: function(val) {
    this.input[0].checked = val;
    this.syncValue();
  },
  syncValue: function() {
    this.value = this.input[0].checked;
  }                                                     
});
//-------------------------------------------------------------------
// WField.RadioGroup (RadioGroup inputfield widget)
//-------------------------------------------------------------------
WField.RadioGroup = function() {
  this.version = '0.1';
  bindMethods(this);
};

/* Take all behaviors from Checkbox */
WField.RadioGroup.prototype = merge (new WField.Base(), {
  setDefault: function() {
    if (this.options.defaultval) {
      for (rb = 0;rb < this.options.values.length;rb++) {
        if (this.options.values[rb].code == this.options.defaultval) {
          this.input[rb].setAttribute("checked","checked");
        }
      }
    }
  },
  setValue: function(val) {
    this.value = val;
    for (rb=0;rb<this.input.length;rb++) {
      if (this.input[rb].value == val) {
        this.input[rb].checked = true;
      } else {
        this.input[rb].checked = false;        
      }
    }
    this.syncValue();
  },
  syncValue: function() { 
    for (rb=0;rb<this.input.length;rb++) {
      if (this.input[rb].checked) {
        this.value = this.input[rb].value;
      }
    }
  },           
  checkValue: function() {
    return true;
  },
  generateInput: function() {
    this.label =  createDOM('label',{'class':'label','id':'label_'+this.id,'for': 'fld_'+this.id});
    this.label.innerHTML = this.options.label;
    row = DIV({'id': 'fld_'+this.id,'class':'inputrow'},
              DIV({'class':(this.options.required) ? 'required' : 'notrequired','id':'reqrd_'+this.id}),
              this.label);
    rg = DIV({'class':'radiogroup'});

    for (rb = 0;rb < this.options.values.length;rb++) {
      this.input[rb] = INPUT(
        {'type':'radio',
         'style':'margin-right:3px',
         'id':this.id+this.options.values[rb].code,
         'name':this.id+':radio',                                
         'value': this.options.values[rb].code,
         'tabindex':1
        });                  

      st = {'style':'float:none;zoom:1;display:block;','class':'radiolabel'};
      if (this.options.format) {
        switch (this.options.format) {
          case 'inline':
            st = {'style':'float:left','class':'radiolabel'};
            break;
          case 'inrow':
            break;
          default:
            break;
        }
      }
      
      lbl = DIV(st, this.input[rb],
                    this.options.values[rb].descr
               );
      rg.appendChild(lbl)
    }
    row.appendChild(rg);   
    row.style.display = 'block';
    if (!this.options.visible) {
      row.style.display = 'none';
    }
  },
  addLabelEvents: function() {
  }
});

//-------------------------------------------------------------------
// WField.Text (Text inputfield widget)
//-------------------------------------------------------------------
WField.Text = function() {
  this.version = '0.1';
  bindMethods(this);
};

WField.Text.prototype = update(new WField.Base(), {
  /* Overrule methods */
 generateInput: function() {                           
    this.input[0] = TEXTAREA({'id':this.id,'name':this.id+':lines','tabindex':1},this.options.defaultval);
    this.input[0].style.height = this.options.size[0]+'px';
    this.input[0].style.width = this.options.size[1]+'px';
    this.label = createDOM('label',{'class':'label','id':'label_'+this.id,'for':'fld_'+this.id},this.options.label);
    row = DIV({'id': 'fld_'+this.id,'class':'inputrow'},
              DIV({'class':(this.options.required) ? 'required' : 'notrequired','id':'reqrd_'+this.id}),
              this.label,
              //DIV({'class':'label','id':'label_'+this.id},this.options.label),
              this.input[0]); 
    if (!this.options.visible) {
      row.style.display = 'none';
    }
  }
});                     

//-------------------------------------------------------------------
// WField.HtmlText (HtmlText inputfield widget)
//-------------------------------------------------------------------

var editor = null;
xinha_editors = null;
xinha_config = null;
xinha_plugins = null;

/* -------------------- HtmlText field -------------------- */
WField.HtmlText = function() {
  this.version = '0.1';
  bindMethods(this);
};

WField.HtmlText.prototype = update(new WField.Base(), {
  /* Overrule methods */
 "generateInput": function() {                           
    this.input[0] = TEXTAREA({'id':this.id,'name':this.id+':lines','tabindex':1},this.options.defaultval);
    this.input[0].style.height = this.options.size[0]+'px';
    this.input[0].style.width = this.options.size[1]+'px';
    row = DIV({'id': 'fld_'+this.id,'class':'inputrow'},
              this.input[0]); 
    if (!this.options.visible) {
      row.style.display = 'none';
    }

    try {
      this.initEditor();
    } catch (e) {
      alert(e.message);
    }

  },
  "addToForm": function(row) {
    this.element.appendChild(row);
    this.initEditor();
  },
  "addLabelEvents": function()
  {
    return;
  },   
  "initEditor": function() {
    xinha_plugins = xinha_plugins ? xinha_plugins :
    [
         'CharacterMap',
         'ContextMenu',
         'ListType',
         'FindReplace',
         'Stylist',
         'TableOperations',
         'InsertAnchor',
         'DoubleClick',
         'PasteText',
         'HorizontalRule'
    ];
    if(!HTMLArea.loadPlugins(xinha_plugins, this.initEditor)) return;

    xinha_editors = xinha_editors ? xinha_editors :
    [
      this.id
    ];

    xinha_config = new HTMLArea.Config();
    try {
    xinha_config.pageStyle = "@import url(DesignerStyles/editorstyles.css);";
    xinha_config.stylistLoadStylesheet('DesignerStyles/editorstyles.css');
    } catch(e) {}
    xinha_config.flowToolbars = false;
    xinha_config.toolbar = [
      ["popupeditor"],
      ["separator","formatblock","bold","italic","underline","strikethrough"],
      ["separator","forecolor","hilitecolor","textindicator"],
      ["separator","subscript","superscript"],
      ["separator","justifyleft","justifycenter","justifyright","justifyfull"],
      ["separator","killword","clearfonts","removeformat","linebreak"],
      (HTMLArea.is_gecko ? ["cut","copy","paste"] : ["cut","copy","paste","overwrite"]),
      ["separator","insertorderedlist","insertunorderedlist","outdent","indent"],
      ["separator","inserthorizontalrule","createlink","insertimage","inserttable"],
      ["separator","undo","redo","selectall","print"],["separator","htmlmode"], 
      ["toggleborders","lefttoright", "righttoleft", "separator","showhelp","linebreak"],
    ];
    xinha_config.formatblock =  {
      "&mdash; Formaat &mdash;"  : "",
      "Kop 1": "h1",
      "Kop 2": "h2",
      "Kop 3": "h3",
      "Kop 4": "h4",
      "Kop 5": "h5",
      "Kop 6": "h6",
      "Paragraaf":"p",
      "Adres":"address",
      "Layer":"div",
      "Geformateerd": "pre"
    };
    xinha_config.ListType.mode = 'panel';
    xinha_config.mozParaHandler = 'best';
    xinha_config.panel_dimensions = 
    {
      left:   '180px', // Width
      right:  '160px',
      top:    '100px', // Height
      bottom: '100px'
    };
    xinha_config.height = 'intraxxioncms';
    xinha_config.width = getElement('sheet0').offsetWidth - 8 +'px';
    xinha_config.killWordOnPaste = false;

    xinha_editors   = HTMLArea.makeEditors(xinha_editors, xinha_config, xinha_plugins);
    editor=xinha_editors[this.id];//.htmledit;
    HTMLArea.startEditors(xinha_editors);    
  }
});                     

//-------------------------------------------------------------------
// WField.Select (Select widget)
//-------------------------------------------------------------------
WField.Select = function() {
  this.version = '0.2';
  bindMethods(this);
};

/* Take all behaviors from Checkbox */
WField.Select.prototype = update (new WField.Base(), {
  generateInput: function() { 
    this.input[0] = SELECT({'id':'select_'+this.id,'style':'width:'+this.options.size+'px'}, 
                            map(this.addOption, this.options.values));
    this.label = createDOM('label',{'class':'label','id':'label_'+this.id,'for':'fld_'+this.id},this.options.label);
    row = DIV({'id': 'fld_'+this.id,'class':'inputrow'},
              DIV({'class':(this.options.required) ? 'required' : 'notrequired','id':'reqrd_'+this.id}),
              this.label, this.input[0]);
    if (!this.options.visible) {
      row.style.display = 'none';
    }
    connect(this.input[0],'onchange', this,
      function(e) {
        this.syncValue();
      }
    );
  },
  addOption: function(ob) {
    return OPTION({'id':this.id+ob.code, 'value': ob.code}, ob.descr);
  },
  addLabelEvents: function() {
  },
  setValue: function(val) {
    this.input[0].value = val;
    this.syncValue();
  },
  setTabIndex: function() {
    if (this.options.editable) {
      this.input[0].setAttribute('tabindex', '1');
    } else {
      this.input[0].setAttribute('tabindex', '-1');
      this.input[0].setAttribute('disabled', 'true');
    }                
  }
});

WField.String.Inspector = function() {   
  this.inspectorform = null;
};           

WField.String.Inspector.prototype = {
  "init": function() { 
    label = new WField.String('label',null, {
                              label:      'Label',
                              editable:   true,
                              visible:    true,
                              required:   true,
                              unique:     false,
                              size:       '40',
                              maxlength:  0,
                              defaultval: 'Label van dit veld',
                              assistant:  'Vul hier het label van dit veld in' }
    );
    editable = new WField.Checkbox('editable',null, {
                              label:      'Wijzigbaar',
                              editable:   true,
                              visible:    true,
                              required:   true,
                              unique:     false,
                              size:       '',
                              maxlength:  0,
                              defaultval: true,
                              assistant:  'Is dit veld wijzigbaar?' }
    );
    visible = new WField.Checkbox('visible',null, {
                              label:      'Zichtbaar',
                              editable:   true,
                              visible:    true,
                              required:   true,
                              unique:     false,
                              size:       '',
                              maxlength:  0,
                              defaultval: true,
                              assistant:  'Is dit veld zichtbaar?' }
    );
    required = new WField.Checkbox('required',null, {
                              label:      'Verplicht',
                              editable:   true,
                              visible:    true,
                              required:   true,
                              unique:     false,
                              size:       '',
                              maxlength:  0,
                              defaultval: false,
                              assistant:  'Is invoer in dit veld verplicht?' }
    );
    unique = new WField.Checkbox('unique',null, {
                              label:      'Uniek',
                              editable:   true,
                              visible:    true,
                              required:   true,
                              unique:     false,
                              size:       '',
                              maxlength:  0,
                              defaultval: false,
                              assistant:  'Dienen waardes in dit veld uniek te zijn?' }
    );
    size = new WField.Integer('size',null, {
                              label:      'Veldbreedte',
                              editable:   true,
                              visible:    true,
                              required:   true,
                              unique:     false,
                              size:       '',
                              maxlength:  3,
                              defaultval: '200',
                              assistant:  'Veldbreedte in pixels' }
    );
    minlength = new WField.Integer('minlength',null, {
                              label:      'Minimale lengte',
                              editable:   true,
                              visible:    true,
                              required:   true,
                              unique:     false,
                              size:       '',
                              maxlength:  3,
                              defaultval: '0',
                              assistant:  'Minimale invoer in karakters, 0 = geen restrictie' }
    );
    maxlength = new WField.Integer('maxlength',null, {
                              label:      'Maximale lengte',
                              editable:   true,
                              visible:    true,
                              required:   true,
                              unique:     false,
                              size:       '',
                              maxlength:  3,
                              defaultval: '0',
                              assistant:  'Maximale invoer in karakters, 0 = geen restrictie' }
    );
    defaultval = new WField.String('defaultval',null, {
                              label:      'Standaard waarde',
                              editable:   true,
                              visible:    true,
                              required:   false,
                              unique:     false,
                              size:       '30',
                              maxlength:  3,
                              defaultval: '',
                              assistant:  'Vul de standaardwaarde van dit veld in' }
    );
    assistant = new WField.Text('assistant',null, {
                              label:      'Hulptekst',
                              editable:   true,
                              visible:    true,
                              required:   false,
                              unique:     false,
                              size:       [300,70],
                              maxlength:  0,
                              defaultval: '',
                              assistant:  'Vul hier evt. hulptekst in.' }
    );
    
  }
};
