/*

  13.06.06 v1.0
  muhneer.nl
  form-validate

  UPDATES
  -------------------------------------------------
  30.06.06 V1.3, rik hartman
  New ruleset is_numeric.
  This rule checks if an input is numeric and returns false if it isn't, but only when a field is filled!
  is_numeric syntax : rule('Preferred display name', 'Field ID', 'is_numeric', 'Optional arguments');
  -------------------------------------------------

  28.06.06 v1.2, muhneer.nl
  New ruleset Password.
  Password syntax : rule('Preferred display name', 'Password ID', 'password', '&fields=ID to check&min_val=Nr chars');
  -------------------------------------------------

  27.06.06 v1.1, muhneer.nl
  New syntax : rule('Preferred display name', 'Field ID', 'Action');
  -------------------------------------------------

*/

/* FORM VALIDATION */
function form_validation(form_name, error_message_id)
{
                /* init vars */
                this.css_invalid = "invalid_field"; /* CSS class containing a markup for a invalid field */
                this.css_valid = "valid_field"; /* CSS class containing a markup for a valid field */
                this.error_div = error_message_id;  /* ID of the error_msg div */

                this.name = form_name;

                this.error_field = new Array();
                this.error_code = new Array();

                this.field_name = new Array();
                this.field_error_name = new Array();
                this.field_action = new Array();
                this.field_arguments = new Array();


                this.element = 0;
                this.error_element = 0;

                /* create check rule */
                this.rule = function(error_name, name, action, argument)
                {

                    this.field_name[this.element] = name;
                    this.field_error_name[this.element] = error_name;
                    this.field_action[this.element] = action;
                    this.field_arguments[this.element] = argument;
                    this.element++;
                }

}

/* FORM CHECK */
function form_check(validate_object)
{
				/* reset the arrays, refresh the checked fields */
                validate_object.error_field = new Array();
                validate_object.error_field_name = new Array();
                validate_object.error_code = new Array();
                validate_object.error_element = 0;

                /* cycle through the rules that are set */
                for (var i = 0; i < validate_object.element; i++)
                {

                   switch (validate_object.field_action[i])
                   {

                      case "password":
                         valid_password(validate_object, i);
                      break;
                      case "email":
                         valid_email(validate_object, i);
                      break;
                      case "is_nummer":
                         is_numeric(validate_object, i);
                      break;
                      case "nummer":
                         valid_numeric(validate_object, i);
                      break;
                      default:
                         filled(validate_object, i);
                      break;
                    }
                }

                /* errors found */
                if (validate_object.error_field.length > 0)
                {
                    error_print(validate_object);
                    return false;
                }

}


/* RULES - expandable */
/* -filled----------------------------------- */
function filled(validate_object, element)
{

                if (!document.getElementById(validate_object.field_name[element]).value) {
                   validate_object.error_field[validate_object.error_element] = validate_object.field_name[element];
                   validate_object.error_field_name[validate_object.error_element] = validate_object.field_error_name[element];
                   validate_object.error_code[validate_object.error_element] = 1;
                   validate_object.error_element++;
                   return false;
                }
                return true;

}

/* -Is numeric ------------------------------------ */
function is_numeric(validate_object, element)
{

	if (document.getElementById(validate_object.field_name[element]).value) {

		isnumeric_value = document.getElementById(validate_object.field_name[element]).value;
		valid_filter  = "0123456789.-";
		numerieke_waarde = true;


		for (i=0; i<isnumeric_value.length && numerieke_waarde == true; i++) {

			karakter = isnumeric_value.charAt(i);

			if (valid_filter.indexOf(karakter) == -1) {
				numerieke_waarde = false;

				validate_object.error_field[validate_object.error_element] = validate_object.field_name[element];
				validate_object.error_field_name[validate_object.error_element] = validate_object.field_error_name[element];
				validate_object.error_code[validate_object.error_element] = 3;
				validate_object.error_element++;
			}
		}


		return numerieke_waarde;

	} else {
		return true;
	}



}

/* -valid_password---------------------------- */
function valid_password(validate_object, element) {

               if (filled(validate_object, element)) {

                       var arg_name=new Array();
                       var arg_value=new Array();

                       base_field = document.getElementById(validate_object.field_name[element]).value;

                       var arg_array = validate_object.field_arguments[element].split("&");
                       arg_array.splice(0,1);



                       /* Create the array for handeling the different arguments */
                       for (x in arg_array) /* walk through the arguments array */
                       {
                            var temp_array = arg_array[x].split("=");
                            arg_name[x] = temp_array[0];
                            arg_value[x] = temp_array[1];
                       }

                       /* Walk through the arg_name array and activatie checks */
                       for (y in arg_name)
                       {

                            /* ARGUMENTS - expandable */
                            switch (arg_name[y])
                            {

                            case "fields":

                                  var arg_fields_array = arg_value[y].split(",");
                                  for (z in arg_fields_array)
                                  {
                                     compare_field = document.getElementById(arg_fields_array[z]).value;
                                     if (base_field != compare_field) {
                                            validate_object.error_field[validate_object.error_element] = validate_object.field_name[element];
                                            validate_object.error_field_name[validate_object.error_element] = validate_object.field_error_name[element];
                                            validate_object.error_code[validate_object.error_element] = 4;
                                            validate_object.error_element++;
                                     }
                                  }

                            break;

                            case "min_val":

                                  validate_object.min_val = arg_value[y];

                                  if (base_field.length < validate_object.min_val) {
                                     validate_object.error_field[validate_object.error_element] = validate_object.field_name[element];
                                     validate_object.error_field_name[validate_object.error_element] = validate_object.field_error_name[element];
                                     validate_object.error_code[validate_object.error_element] = 5;
                                     validate_object.error_element++;
                                  }

                            break;

                            }

                       }

               }

}

/* -valid_email----------------------------- */
function valid_email(validate_object, element)
{

                 if (filled(validate_object, element)) {

                     email_value = document.getElementById(validate_object.field_name[element]).value;
                     valid_filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

                     if (!valid_filter.test(email_value))
                     {
                        validate_object.error_field[validate_object.error_element] = validate_object.field_name[element];
                        validate_object.error_field_name[validate_object.error_element] = validate_object.field_error_name[element];
                        validate_object.error_code[validate_object.error_element] = 2;
                        validate_object.error_element++;
                        return false;
                     }
                 }

}

/* -valid_numeric----------------------------- */
function valid_numeric(validate_object, element)
{

                 if (filled(validate_object, element)) {

                     isnumeric_value = document.getElementById(validate_object.field_name[element]).value;
                     valid_filter  = "0123456789.-";
                                         numerieke_waarde = true;

                                         for (i=0; i<isnumeric_value.length && numerieke_waarde == true; i++) {
                                                 karakter = isnumeric_value.charAt(i);
                                                if (valid_filter.indexOf(karakter) == -1) {
                                                        numerieke_waarde = false;
                                                                             validate_object.error_field[validate_object.error_element] = validate_object.field_name[element];
                     validate_object.error_field_name[validate_object.error_element] = validate_object.field_error_name[element];
                     validate_object.error_code[validate_object.error_element] = 3;
                     validate_object.error_element++;
                                                }
                                         }



                                         return numerieke_waarde;
                 }

}
/* ------------------------------------------ */


/* ERROR - handling */
function error_print(validate_object)
{

                /* STYLE - give al the borders their original color */
                for (var j = 0; j < validate_object.element; j++)
                {
                   document.getElementById(validate_object.field_name[j]).className=validate_object.css_valid;
                }

                /* ERROR - messages */
                error_desc = new Array();
                error_desc[1] = "is een verplicht veld";
                error_desc[2] = "is geen bestaand email-adres";
                error_desc[3] = "is geen numerieke waarde";
                error_desc[4] = "wachtwoorden komen niet overeen";
                error_desc[5] = "wachtwoord moet uit meer dan "+validate_object.min_val+ " tekens bestaan";

                error_msg = "<span><strong>Er hebben zich de volgende fouten voorgedaan :</strong></span>";
                error_msg += "<ul>";

                /* ERROR - compose the error message */
                for (var i = 0; i < validate_object.error_field.length; i++)
                {

                   error_msg += "<li>" + validate_object.error_field_name[i] + " : " + error_desc[validate_object.error_code[i]] + "</li>";

                   /* STYLE - give invalid field a different border */
                   document.getElementById(validate_object.error_field[i]).className=validate_object.css_invalid;

                }

                error_msg += "</ul>";

                /* ERROR - print the error message */
                document.getElementById(validate_object.error_div).innerHTML = error_msg;
                document.getElementById(validate_object.error_div).style.display = "block";

}

