
var Validators = function()
{
   this.vals = new Array();

   this.add = function(group, cid, vid, type, message, action, unaction, exparam)
   {
      if (!group) group = 'default';
      if (jQuery.type(this.vals[group]) != 'array') this.vals[group] = new Array();
      if (jQuery.type(this.vals[group][cid]) != 'array') this.vals[group][cid] = new Array();
      this.vals[group][cid][vid] = new Array(type, message, action, unaction, exparam);
   },

   this.clean = function(group)
   {
      if (group == undefined)
      {
         for (group in this.vals) this.clean(group);
         return;
      }
      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(this.vals[group][cid][vid][3]);}
         }
      }
   },

   this.isValid = function(group, isAll)
   {
      if (isAll == undefined) isAll = true;
     if (group == undefined) group = 'default';
     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;
   },

   this.isValidItem = function(cid, vid, data)
   {
      var cidname = cid;
      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')
      {
         if (cid.is('input')) {
             switch (cid.attr('type'))
             {
                case 'text':
                case 'password':
                  vf = !(!cid.val());
                  break;
                case 'checkbox':
                case 'radio':
                  vf = cid.is(':checked');
                  break;
             }
         } else if (cid.is('textarea')) {
             if (typeof(tinyMCE) != 'undefined' && tinyMCE.get(cid.id)) vf = !(!tinyMCE.get(cid.id).getContent());
             else vf = !(!cid.val());
         } else if (cid.is('select')) {
             vf = !(!cid.val());
         } else {
             var elements = $('#'+cidname+' :input');
             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;
             }
         }
      }
      else if (data[0] == 'email')
      {
         if (cid.val())
         {
            var re = /[a-z\d-_\.]+@[a-z\d-_]+(\.[a-z\d_-]+)/i;
            vf = re.test(cid.val());
         }
         else vf = true;
      }
      else if (data[0] == 'regularexpression')
      {
         eval("var re = " + data[4] + "; vf = re.test(cid.val());");
      }
      else vf = true;
      if (!vf) vid.html(data[1]);
      else vid.html('');
      if (!vf && data[2]) eval(data[2]);
      if (vf && data[3]) eval(data[3]);
      return vf;
   }

};

var validators = new Validators();
