var FormValidation =
{
  init: function()
  {
		$$('form').each( function(f) {
			Event.observe(f, 'submit', FormValidation.submitListener);
		});
  },

  rules:
  {
    required: /./,
    requiredNotWhitespace: /\S/,
    positiveInteger: /^\d*[1-9]\d*$/,
    positiveOrZeroInteger: /^\d+$/,
    integer: /^-?\d*$/,
    potinteger: /(^$|^-?\d+$)/,
    decimal: /^-?\d+(\.\d+)?$/,
    potdecimal: /(^$|^-?\d+(\.\d+)?$)/,
    email: /^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/,
	date: /^[0-1]?\d\/[0-3]?\d\/\d{2}$/,
	potdate: /(^$|^[0-1]?\d\/[0-3]?\d\/\d{2}$)/,
	time: /^[0-1]?\d\:[0-5]\d (am|AM|pm|PM)$/,
	pottime: /(^$|^[0-1]?\d\:[0-5]\d (am|AM|pm|PM)$)/,
	zip: /^[1-9]\d{4}$/,
	potzip: /(^$|^[1-9]\d{4}$)/,
	ssn: /^[1-9]\d{2}-\d{2}-\d{4}$/,
	potssn: /(^$|^[1-9]\d{2}-\d{2}-\d{4}$)/,
	radio: /^$/,
	checkbox: /^$/,
	weight: /(^$|^\d{1,2}(\.\d{1,2})?$)/,
    telephone: /^(\+\d+)?( |\-)?(\(?\d+\)?)?( |\-)?(\d+( |\-)?)*\d+$/,
	pottelephone: /(^$|^(\+\d+)?( |\-)?(\(?\d+\)?)?( |\-)?(\d+( |\-)?)*\d+$)/
  },

  errors:
  {
    required: "Please fill in this required field.",
    requiredNotWhitespace: "Please fill in this required field.",
    positiveInteger: "This field may only contain a positive whole number.",
    positiveOrZeroInteger: "This field may only contain a non-negative whole number.",
    integer: "This field may only contain a whole number.",
    potinteger: "This field may only contain a whole number.",
    decimal: "This field may only contain a number.",
    potdecimal: "This field may only contain a number.",
    email: "Please enter a valid email address into this field.",
	date: "Please enter your date in the format mm/dd/yy.",
	potdate: "Please enter your date in the format mm/dd/yy.",
	time: "Please enter your time 12 hour (xx:xx am/pm) format.",
	pottime: "Please enter your time 12 hour (xx:xx am/pm) format.",
	zip: "Please enter a valid zip code into this field.",
	potzip: "Please enter a valid zip code into this field.",
	ssn: "Please enter a valid social security number into this field.",
	potssn: "Please enter a valid social security number into this field.",
	radio: "Please choose an option.",
	checkbox: "Please select the checkbox.",
	weight: "Please enter a valid baby weight.",
    telephone: "Please enter a valid telephone number into this field.",
    pottelephone: "Please enter a valid telephone number into this field."
  },

  submitListener: function(event)
  {
    var fields = this.elements;
    
    for (var i = 0; i < fields.length; i++)
    {
      var className = fields[i].className;
      var classRegExp = /(^| )(\S+)( |$)/g;
      var classResult;
      
      while (classResult = classRegExp.exec(className))
      {
        var oneClass = classResult[2];
        var rule = FormValidation.rules[oneClass];
        if (typeof rule != "undefined")
        {
		  if (oneClass == 'radio' || oneClass == 'checkbox')
		  {
			var inputParent = fields[i].up();
			var sub_oktogo = false;
			inputParent.select('input').each( function (i) {
				if (i.checked)
					sub_oktogo = true;
				})
			if (!sub_oktogo)
			{
				fields[i].focus();
				alert(FormValidation.errors[oneClass]);
				new Effect.Highlight(inputParent, {startcolor:'#ff9999', endcolor:'#ffffff'});
				Event.stop(event);
				while (classResult = classRegExp.exec(className))
					continue;
				return false;
			}
		  }
          else if (!rule.test(fields[i].value))
          {
            fields[i].focus();
            alert(FormValidation.errors[oneClass]);
			new Effect.Highlight(fields[i].up(), {startcolor:'#ff9999', endcolor:'#ffffff'});
            Event.stop(event);
			while (classResult = classRegExp.exec(className))
				continue;
            return false;
          }
        }
      }
    }
	return true;
  }
};

Event.observe(window, 'load', FormValidation.init);

