var ValidateForm = Class.create();

/**
 *  validate some specified fields of a form
 *  
 * @author: lwielink<lwielink@refdag.nl>
 * @date: 30/08/2007
 */	
ValidateForm.prototype = {   
	
	requiredFields		  : new Array(),
	requiredEmptyFields	 : new Array(),
	options				 : new Array(),
		
	/**
	 *  Initialize the input fields en check if the required
	 *  Whenn argument 2 exists, the constructor will not executed	  
	 *			
	 * @param string ElementId
	 * @return void	
	 */			   
	initialize : function(elementId)
	{		 
		this.clear();  
		this.options.controlFields = {
			'email'		 : '^[A-Za-z0-9_-]+([\.]{1}[A-Za-z0-9_-]+)*@[A-Za-z0-9-]+([\.]{1}[A-Za-z0-9-]+)$',
			'zipcode'	   : '^[0-9]{4}\s*[a-zA-Z]{2,3}$',
			'street_number' : '^[0-9]+$'
		};
		this.options.warnings = {
			'default'	   : 'Verplicht veld',
			'email'		 : 'Onjuist formaat: info@rdtref.nl',
			'zipcode'	   : 'Onjuist formaat: 8071KM',
			'street_number' : 'Onjuist formaat: alleen cijfers toegestaan'
		};
		
		if ((!arguments) || arguments.length < 2) {
			var table = $(elementId);
			var inputs = table.getElementsByTagName('INPUT');
			for (var i=0; i < inputs.length; i++) {
				try {
					if ((inputs[i].nextSibling.nextSibling.className == 'required') && inputs[i].nextSibling.nextSibling.nodeName.match(/^span$/i)) { 
						this.requiredFields.push(inputs[i]);
						if (inputs[i].value.empty()) {
						   this.requiredEmptyFields.push(new Array([inputs[i].id],[1], [0]));
						} else {
							found = false;
							for(value in this.options.controlFields){
								if (value == inputs[i].name) {
									if (!inputs[i].value.match(new RegExp(this.options.controlFields[value]))) {
										this.requiredEmptyFields.push(new Array([inputs[i].id],[1], [1]));
										found = true;
									}
								}
							}
							if (!found)
								this.defineWarning(inputs[i], 0);
						}
					}
				} catch (exception) {}
			}
		}
	},
	
	getWarning : function(type)
	{
		return this.finalWarning[type];
	},
	
	/**
	 *  Check the form on the base of the filled requiredEmptyFields array
	 *  If no errors the form will be hide and the finisch text will be show	 
	 *  
	 * @return boolean	 
	 */
	checkForm : function()
	{  
		/* walk through the required en empty required fields and show a warning if needed */
		if (this.requiredFields.length > 0) {
			if (this.requiredEmptyFields.length > 0) {
				for (var i=0; i < this.requiredEmptyFields.length; i++) {
					this.defineWarning(String(this.requiredEmptyFields[i][0]), this.requiredEmptyFields[i][1], this.requiredEmptyFields[i][2]);
				}
				/* set height */
				if (obj = $('outerImageContainer')) {
					obj.style.height = $('lightboxData').getHeight()+50 + 'px';
				}
				return false;
			}
		}
		if ((arguments) && arguments.length > 0) {
			if (typeof(arguments[0]) == 'object') {
				var config = arguments[0];
				if (config.onSuccessInnerScript)
				    return eval(config.onSuccessInnerScript);
				if (config.sendPostDataByAjaxCall == true) {
    				new Ajax.Request(config.requestUri,
    				{
    					method:(!config.method) ? 'post' : config.method,
    					parameters:$(config.formId).serialize(true),
    					onSuccess: function(transport){
    					   var response = transport.responseText || "no response text";
    					   if (response) 
    						  $('lightboxData').update(config.onSuccessWarning);
    					},
    					onFailure: function() {
    					   $('lightboxData').update(config.onFailureWarning);
    					}
    				});
    				return false;
    			} else {
                    $('lightboxData').style.display = 'none';
                    var div = document.createElement('div');
                    div.style.fontSize = '12px';
                    div.id = 'result_div_id';
                    div.appendChild(document.createTextNode('Een moment geduld astublieft....'));
                    $('imageContainer').appendChild(div);
                    $('loading').style.display = 'block';
                    return true;
                }
			} 
		} 
	},
	
	/**
	 *  Define the warning of the input element.
	 *  Two options:
	 *   - a red border
	 *   - red warning defined in the top of this class			   
	 *  
	 * @return boolean	 
	 */
	 
	defineWarning : function(fieldId, error, showWarning)
	{
		if (parseInt(error) == 1) {
			/* create a div for append the error warning */
			var divItem = document.createElement('DIV');
			divItem.className = 'form_input_error';
			var inputFieldName = $(fieldId).name;
			if (($(fieldId).parentNode.firstChild.className != 'form_input_error') || parseInt(showWarning) == 1)  {
				if (parseInt(showWarning) == 1) {
					if ($(fieldId).parentNode.firstChild.className == 'form_input_error') 
						$(fieldId).parentNode.removeChild($(fieldId).parentNode.firstChild);
				}
				/* set error warning */
				var typeOfWarning = (parseInt(showWarning) == 1) ? inputFieldName : 'default';
				divItem.appendChild(document.createTextNode(this.options.warnings[typeOfWarning]));
				$(fieldId).parentNode.insertBefore(divItem, $(fieldId).parentNode.firstChild);
			}
			$(fieldId).setStyle({'border' : '1px solid red'});
		} else {
			$(fieldId).setStyle({'border' : '0px none'});
			/* removing the error warning */
			if ($(fieldId).parentNode.firstChild.className == 'form_input_error') 
				$(fieldId).parentNode.removeChild($(fieldId).parentNode.firstChild);
		}
	},
	
	
	
	/**
	 * Check the profile form.
	 * 
	 * @param object form
	 * @param int currentItem
	 * @param array fields
	 * @return boolean	 
	 */							  
	checkProfileForm : function(form, currentItem, fields)
	{   
		try {
			returnValue = true;
			for (var i =0; i < fields.length; i++) {
				var toGetField = eval("form." + fields[i] + "_" + parseInt(currentItem-1));
				/* error handling */
				var error = document.createElement('SPAN');
				error.innerHTML = this.options.warnings['default'] + '<br />';
				error.className = 'required';
				if (toGetField.value == ''){
					returnValue = false;
					if (!toGetField.parentNode.firstChild.nodeName.match(/span/i)) {
						if (fields[i] != 'education_to') {
							toGetField.parentNode.insertBefore(error, toGetField);
						} else {
							var error = document.createElement('DIV');
							error.setStyle({'position':'absolute','left':'305px', 'marginTop':'-35px', 'color':'#EB541F'});
							error.innerHTML = 'Verplicht veld<br />';
							toGetField.parentNode.insertBefore(error, toGetField);
						}
					}
				} else {
					if (toGetField.parentNode.firstChild.nodeName.match(/span/i))
						toGetField.parentNode.removeChild(toGetField.parentNode.firstChild);
				}
			}			
			return returnValue;
		} catch (exception) {
			return false;
		}
	},
	
	
	/**
	 *  clear all defined data
	 *  
	 * @return void		  
	 */		 
	clear		   : function()
	{
		this.requiredEmptyFields.clear();
		this.requiredFields.clear();
		this.options.clear();
	}
};
