// JavaScript Document

/* class for handling all form-steps in javascript */

var JS_Forms = Class.create(
{
   initialize: function()
   {
	  this.requiredFields = new Array();
      //alert('test');
   },
   
   
   /* to focus a field (usability) */
   
   focusField: function(field)
   {
      if(this.focussedField)
      {
         $(this.focussedField).removeClassName('focus');
      }
      //alert(field);
      $(field).addClassName('focus');
      $(field).focus()
      this.focussedField = field;
   },
   
   addRequiredField: function(field)
   {	   
	   this.requiredFields.push(field);	   
	  // var_dump(this.requiredFields);
   },
   
   /* some steps in the form need validation */
   
   validate: function()
   {
	   
      	validator = true;     
		this.messages = '';
      
      	fields   = this.requiredFields; //,'geslacht'   		
            
		fields.each(function(fieldname){
		   //fieldnameTotal = fieldname+'_'+i;
		   if(!$(fieldname).value)
		   {
			  this.highlight(fieldname+'_lb');		  
			  
			  if(!this.fieldToFocus)
			  {
				 this.fieldToFocus = fieldname;
			  }
			  
			  if(fieldname == 'email')
			  {
				 fieldname = 'e-mail';
			  }
			  this.messages = this.messages + '- '+ fieldname + '<br/>';
			  
			  validator = false;
		   }
		   else
		   {
			  this.unHighlight(fieldname+'_lb');
		   }
		   
		}.bind(this));     // bind the complete object to the each  function to be able to use its functions    
      
		if(!validator)
		{
			//$('messages_'+this.step).innerHTML = '<img src="/images/warning.gif" alt="waarschuwing" id="warning" />';
			$('messages').innerHTML = 'Vul a.u.b. alle verplichte velden in: <br/>' + this.messages;
			this.messages = '';
			this.focusField(this.fieldToFocus);
			this.fieldToFocus = '';
		}
		else
		{
			$('form').submit();
		}
      
      //return validator;
   },
   
   
   highlight: function(id)
   {
      $(id).style.color = 'red';//'#751012';
   },
   unHighlight: function(id)
   {
      $(id).style.color = '#1F1F1F';
   }
});

js_forms = new JS_Forms();

