
var Validators = new Class(
{
   initialize: function()
   {
   	  this.vals = new Array();
   	  this.actionCount = 0;
   },

   add: function(group, cid, vid, type, message, action, unaction, exparam)
   {
      if (!group) group = 'default';
      if ($type(this.vals[group]) != 'array') this.vals[group] = new Array();
      if ($type(this.vals[group][cid]) != 'array') this.vals[group][cid] = new Array();
      this.vals[group][cid][vid] = new Array(type, message, action, unaction, exparam);
   },

   clean: function(group)
   {
   	  if (group == undefined) group = 'default';
          for (cid in this.vals[group])
      {
            if (!$(cid)) continue;
         for (vid in this.vals[group][cid])
         {
              if (!$(vid)) continue;
            $(vid).setHTML('');
               if (this.vals[group][cid][vid][3]) eval(data[3]);
         }
      }
   },

   isValid: function(group, isAll)
   {
      if (isAll == undefined) isAll = true;
   	  if (group == undefined) group = 'default';
   	  this.actionCount = 0;
   	  if (!isAll)
   	  {
   	     for (cid in this.vals[group])
         {
            for (vid in this.vals[group][cid])
            {
            	  if (!this.isValidItem(cid, vid, this.vals[group][cid][vid]))
            	    return false;
            }
         }
   	     return true;
   	  }
   	  var vf = true;
      for (cid in this.vals[group])
      {
         for (vid in this.vals[group][cid])
         {
            if (!this.isValidItem(cid, vid, this.vals[group][cid][vid]))
            {
               vf = false;
               break;
            }
         }
      }
      return vf;
   },

   isValidItem: function(cid, vid, data)
   {
      var vf, cid = $(cid), vid = $(vid);
      if (!cid && $(cid + '_date')) cid = $(cid + '_date');         // for DateControl
      if (!cid && $(cid + '_cityid')) cid = $(cid + '_cityid');     // for Location
      if (!cid || !vid) return true;
      //alert(cid.id + ' - ' + cid.type + ' - ' + cid.nodeName);
      if (data[0] == 'required')
      {
         switch (cid.type)
         {
            case 'text':
            case 'password':
              vf = !(!cid.value);
              break;
            case 'textarea':
              if (typeof(tinyMCE) != 'undefined' && tinyMCE.get(cid.id)) vf = !(!tinyMCE.get(cid.id).getContent());
              else vf = !(!cid.value);
              break;
            case 'select-one':
              vf = (cid.selectedIndex != 0);
              break;
            case 'checkbox':
            case 'radio':
              vf = cid.checked;
              break;
            default:
              var elements = $(cid).getFormElements();
              if (elements.length == 0) vf = true;
              else
              {
                 vf = false;
                 for (var i = 0; i < elements.length; i++)
				   if (elements[i].type == 'radio' || elements[i].type == 'checkbox') vf |= elements[i].checked;
              }
              break;
         }
      }
      else if (data[0] == 'email')
      {
         if (cid.value)
         {
            var re = /[a-z\d-_\.]+@[a-z\d-_]+(\.[a-z\d_-]+)/i;
            vf = re.test(cid.value);
         }
         else vf = true;
      }
      else if (data[0] == 'regularexpression')
      {
         eval("var re = " + data[4] + "; vf = re.test(cid.value);");
      }
      else vf = true;
      if (!vf) vid.setHTML(data[1]);
      else vid.setHTML('');
      if (!vf && data[2]) eval(data[2]);
      if (vf && data[3]) eval(data[3]);
      if (vf) this.actionCount++;
      return vf;
   }

});

var validators = new Validators();