/*
	Validates every form that has a validate-class on it.

	The following classes are inspected by the script after document is loaded.


	require: 
		The input (text, radio, textarea) is required, and the script will alert the rel attribute on form submit, if empty.

	forceFloat: 
		The input (text) is forced to be a float, on keyup the input will delete everything 
		right to left that is not a float.
	
	forcePFloat: 
		The input (text) is forced to be a positiv float, on keyup the input will delete everything 
		right to left that is not a positive float.

	forceInt:
		The input (text) is forced to be a integer, on keyup the input will delete everything
		right to left that is not a integer.

	forceInt:
		The input (text) is forced to be a positive integer, on keyup the input will delete everything
		right to left that is not a positive integer.

	validEmail:
		The input (text) must be a valid email, the script will alert the rel attribute on form submit if invalid.


*/

var FLOAT_REG = /^[-]?[\d\.]*\,?\d*$/
var PFLOAT_REG = /^[\d\.]*\,?\d*$/
var INT_REG = /^[-]?[\d]*$/
var PINT_REG = /^[\d]*$/
var EMAIL_REG = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/

$(document).ready(function(){
	$("form").each(function(i){
		if($(this).hasClass("validate")){
			$(this).bind("submit", function(){
				return validate(this);
			});
		}
	});

	$(":text").each(function(){
		if($(this).hasClass("forceFloat")){
			$(this).bind("keyup", function(){
				forceValue(this, FLOAT_REG);
			});
		}

		if($(this).hasClass("forcePFloat")){
			$(this).bind("keyup", function(){
				forceValue(this, PFLOAT_REG);
			});
		}

		if($(this).hasClass("forceInt")){
			$(this).bind("keyup", function(){
				forceValue(this, INT_REG);
			});
		}
		
		if($(this).hasClass("forcePInt")){
			$(this).bind("keyup", function(){
				forceValue(this, PINT_REG);
			});
		}
	});
});

function validate(form){
	var validForm = true;
	$(form).find(":input").each(function(i){
		
		//require
		if($(this).hasClass("required")){
			if($(this).attr("type") == "text"){
				validForm = requireText(this);
			} else if($(this).attr("type") == "textarea"){
				validForm = requireText(this);
			} else if($(this).attr("type") == "radio"){
				validForm = requireRadio(this, form);
			}

			if(!validForm){
				return false;
			}		
		} 
		
		//validate special cases
		if($(this).hasClass("validEmail") && $(this).attr("type") == "text"){
			validForm = validateMail(this);
			if(!validForm){
				return false;
			}
		}
	});
	return validForm;
}


function requireText(input){
	if (input.value == "") {
		displayError(input);
		input.focus();
		return false;
	} else {
		return true;
	}
}

function validateMail(input){
	if(input.value.toString().match(EMAIL_REG) == null){
		displayError(input);
		return false;
	} else {
		return true;
	}
}

function requireRadio(input, form){
	var other = $(form).find(":radio[name="+input.name+"]");
	var checked = false;
	$(other).each(function(){
		checked = checked || this.checked;
	});
	
	if (!checked) {
		displayError(input);
		return false;
	} else {
		return true;
	}	
}

function forceValue(input, reg){
	while(!input.value.toString().match(reg)){
		input.value = input.value.toString().substring(0, input.value.length-1);	
	}		
}

function displayError(input){
	alert($(input).attr("rel"));
}