
var Validators = new Class(
{
   initialize: function()
   {
      this.validators = new Array();
      this.controls = new Array();
      this.results = new Array();
   },

   add: function(vid, groups, cids, type, mode, message, action, unaction, exparam)
   {
      cids = cids.split(',');
      this.controls.combine(cids);
      this.validators[vid] = {'groups': groups.split(','), 'cids': cids, 'type': type, 'mode': mode, 'message': message, 'action': action, 'unaction': unaction, 'exparam': exparam};
   },

   remove: function(vid)
   {
      this.validators.erase(vid);
   },

   clean: function(group)
   {
      if (group == undefined) group = 'default';
      this.results = new Array();
      for (vid in this.validators)
      {
         if (!$(vid)) continue;
         if (group == '' || this.validators[vid].groups.contains(group))
         $(vid).style.display = 'none';
         if (this.validators[vid].unaction) eval(this.validators[vid].unaction);
      }
   },

   isValid: function(group, isAll)
   {
      if (isAll == undefined) isAll = true;
      if (group == undefined) group = 'default';
      for (var i = 0; i < this.controls.length; i++) this.results[this.controls[i]] = true;
      if (!isAll)
      {
         for (vid in this.validators)
         {
            if (!$(vid)) continue;
            if ((group == '' || this.validators[vid].groups.contains(group)) && !this.validate(vid)) return false;
         }
         return true;
      }
      var flag = true;
      for (vid in this.validators)
      {
         if (!$(vid)) continue;
         if ((group == '' || this.validators[vid].groups.contains(group)) && !this.validate(vid)) flag = false;
      }
      return flag;
   },

   validate: function(vid)
   {
      if (!$(vid)) return true;
      var val = this.validators[vid];
      var flag = true, valtype = val.type.toLowerCase();
      switch (valtype)
      {
         case 'required':
         case 'email':
         case 'regularexpression':
           switch (val.mode.toUpperCase())
           {
              case 'AND':
                for (cid in val.cids) flag &= this.check(val.cids[cid], valtype, vid);
                break;
              case 'OR':
                for (cid in val.cids) flag |= this.check(val.cids[cid], valtype, vid);
                break;
              case 'XOR':
                var n = 0;
                for (cid in val.cids) if (this.check(val.cids[cid], valtype, vid)) n++;
                flag = (n == 1);
                break;
           }
           break;
         case 'compare':
           break;
      }
      if (!flag) $(vid).style.display = '';
      else $(vid).style.display = 'none';
      if (!flag && val.action) eval(val.action);
      if (flag && val.unaction) eval(val.unaction);
      return flag;
   },

   check: function(cid, type, vid)
   {
      var cid = $(cid);
      if (!cid) return true;
      var flag = true;
      switch (type.toLowerCase())
      {
         case 'required':
           switch (cid.type)
           {
              case 'text':
              case 'file':
              case 'password':
                flag = !(!cid.value);
                break;
              case 'textarea':
                if (typeof(easyEditor) != 'undefined' && easyEditor.isExists(cid.id)) flag = (easyEditor.getContent(cid.id).length > 0);
                else if (typeof(tinyMCE) != 'undefined' && tinyMCE.get(cid.id)) flag = !(!tinyMCE.get(cid.id).getContent());
                else flag = !(!cid.value);
                break;
              case 'select-one':
                flag = (cid.value != '');
                break;
              case 'checkbox':
              case 'radio':
                flag = cid.checked;
                break;
              default:
                var elements = cid.getFormElements();
                if (elements.length == 0) flag = true;
                else
                {
                   flag = false;
                   for (var i = 0; i < elements.length; i++)
                     if (elements[i].type == 'radio' || elements[i].type == 'checkbox') flag |= elements[i].checked;
                }
                break;
           }
           break;
         case 'email':
           if (cid.value)
           {
              var re = /^[0-9a-z_\.\-]+@[0-9a-z_^\.\-]+\.[a-z]{2,6}$/i;
              flag = re.test(cid.value);
           }
           else flag = true;
           break;
         case 'regularexpression':
           if (cid.value.length > 0)
           {
              if (this.validators[vid].exparam.expression.charAt(0) == 'i') eval("var re = " + this.validators[vid].exparam.expression.substr(1) + "; flag = !re.test(cid.value);");
              else eval("var re = " + this.validators[vid].exparam.expression + "; flag = re.test(cid.value);");
           }
           else flag = true;
           break;
      }
      this.results[cid.id] &= flag;
      return flag;
   }
});

var validators = new Validators();

