/**
 * @author jochen
 * 
 * formValidate is a class that validates forms *duh*
 * You can have different types of fields, each passed through by the input's classname
 * e.g:
 * 		<input type="phone" id="phone" name="phone" class="required phone_be" />
 * 
 * In order to have the input checked, it must be a "required" field, the, we have different types:
 * ~ numeric		:		only numeric chars are alowed
 * ~ zip_be			:		Belgian zip code '8560', '9000', ...
 * ~ phone_be		:		Belgian phone number '056123456', all other tags will be stripped
 * ~ email			:		Valid email address
 *  
 */

// validation is triggerd by the user, later I look for custom actions (onfocus, onblur, onchange, ...)

function formValidate(oForm, eMatch, oSubmit){
	
	this.jah = new joinkJAH();
	
	this.formSubmit = oSubmit;	
	this.error = false;
	this.errTxt = [];
	
	this.form = oForm;
	var inputs  = this.form.getElementsByTagName('input');
	var selects = this.form.getElementsByTagName('select');
		
	this.fields = [];
	
	var i = 0;
	for (;i<inputs.length;i++){
		var o = new fieldValidate(inputs[i]);
		this.fields.push(o);
	}


	this.alert = false;
	if (eMatch){
		if (eMatch.alert){
			this.alert = true;
		}
		if (eMatch.css){
			this.css = eMatch.css;
		}
	}
		
}

formValidate.prototype = {
	
	validate:function(){
		this.resetErrors();
		var j = 0;
		for (;j<this.fields.length;j++){
			this.fields[j].validate(this);	
		}
		if (this.errTxt){
			this.showError();
		}else{
			this.jah.setParams(this.form);
			this.jah.setHtmlObj('formsubmit');
			this.jah.doRequest(this.formSubmit, 'POST');
		}
	}
	,
	resetErrors:function(){
		this.errTxt = '';
		var j = 0;
		for (;j<this.fields.length;j++){
			this.fields[j].resetErrors(this);	
		}
	}
	,
	triggerError:function(oField, eText){
		this.errTxt += eText + '\n';
		if (this.css){
			// check if it's not already an error field
			var c = oField.className.split(' ');
			if (c[c.length-1] != 'error'){
				oField.className += ' ' + this.css;
			}
		}		
	}	
	,
	showError:function(){
		if (this.alert){
				window.alert(this.errTxt);			
		}		

	}
}

function fieldValidate(i){
	this.field = i;
	this.classes = this.field.className.split(' ');
	this.regex = [];
	this.regex['numeric'] = /^\d+$/;
	this.regex['zip_be']  = /^\d{4}$/;
	this.regex['email']   = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	this.regex['phone_be'] = /^[0-9]/;
	this.regex['shakespeare'] = /(bb|[^b]{2})/;
}

fieldValidate.prototype = {
	
	validate:function(parent){
		// check for the classNames and do some math ;)
		// is it required?
		if (this.classes.inArray('required')){
			if (this.field.value != ''){
			}else{
				parent.triggerError(this.field, this.field.id + ' is een verplicht veld');
			}
		}
		
		// is it numeric?
		if (this.classes.inArray('numeric')){
			if (this.regex['numeric'].test(this.field.value)){
			}else{
				parent.triggerError(this.field, this.field.id + ' moet een numerieke waarde zijn');
			}
		}

		// is it a postal code (BE)
		if (this.classes.inArray('zip_be')){
			if (this.regex['zip_be'].test(this.field.value)){
			}else{
				parent.triggerError(this.field, this.field.id + ' moet een postcode zijn');	
			}
		}

		// is it a valid email?
		if (this.classes.inArray('email')){
			if(this.regex['email'].test(this.field.value)){
			}else{
				parent.triggerError(this.field, this.field.id + ' is geen geldig emailadres');
			}
		}

	}	
	,
	resetErrors:function(parent){
		var j = 0;
		var c = this.field.className.split(' ');
		var nc = '';
		for (;j<c.length;j++){
			if (c[j] != parent.css){
				nc += c[j];
			}
		}
		this.field.className = nc;
	}
}
/*
Array.prototype.inArray = function (value)
// Returns true if the passed value is found in the
// array.  Returns false if it is not.
{
    var i;
    for (i=0; i < this.length; i++) {
        // Matches identical (===), not just similar (==).
        if (this[i] === value) {
            return true;
        }
    }
    return false;
};
*/