var FormValidator = new Class({
  form: null,
  fields: null,
  initialize: function(el, def) {
	this.form = $(el);
	this.fields = def;
	this.form.addEvent('submit',this.validate.bind(this));
  },
  validate: function(event) {
    response = "";
	this.fields.each(function(item, index, array) {
	  switch (item[1]) {
	    case 'text':
		case 'textarea':
		  var value = String($(item[0]).value);
		  switch(item[2]) {
			case 'VALID_NOT_ZERO':
			  valid = value.test(/[^0]/);
			  break;
			case 'VALID_EMAIL':
			  valid = value.test(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/);
			  break;
			case 'VALID_CONFIRM_VALUE':
			  valid = (value == String($(item[4]).value));
			  break;
			case 'VALID_MAX_WORDS':
			  words = (!value.length) ? 0 : value.match(/[A-Za-z\'\-]+/g).length;
			  valid = (words <= item[4]);
			  break;
		  }
		  break;
		case 'select':
		  switch (item[2]) {
		    case 'VALID_SELECT_NOT_TOP':
		      valid =  ($(item[0]).options[$(item[0]).selectedIndex].value != item[4]);
			  break;
			case 'VALID_SELECT_MULTI_LIMIT':
			  valid = ($(item[0]).getSelected().length <= item[4]);
			  break;
		  }
		  break;
	  }
	  if (!valid && response != '') response += '|'; 
	  if (!valid) response += item[3];
	});

	if (response != '') {
      errormsg = $('error_' + this.form.id);
      errorheading = $('error_heading_' + this.form.id);
	  
	  errorlist = $('error_list_' + this.form.id);
	  if (errorlist != null) errorlist.dispose();

      var ul = new Element('ul');
	  ul.id = 'error_list_' + this.form.id;
	  
	  response.split('|').each(function(item, index, array) {
	    var li = new Element('li', { text: item }).inject(ul, 'bottom');
	  });
	  
	  ul.inject(errorheading, 'after');
	  errormsg.setStyle('display', 'block');
	  event.stop();
	  return false;
	}
	return true;
  }
  
});