function formValidator(){};
formValidator.prototype = {
	formulario: "",
	rules: {},
	sendForm: true,
	error: {},
	init: function(formu, opts){
		this.formulario = $(formu);
		this.rules = opts;
		for(f in this.rules.required)
			this.error[this.rules.required[f]] = [];
	},
	whitespace: function(value){
		return /^\S+$/i.test(value);
	},
	email: function(value){
		var response = false; /*Ko*/
		if(this.rules.email != "undefined"){
			response = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(value);
		}
		return response;
	},
	minium: function(minlength, value){
		return (value.length >= minlength) ? true : false;
	},
	isNumber: function(value){
		return !isNaN(value);
	},
	validate: function(){
			if(this.rules.required){
				for(field in this.rules.required){
					name = this.rules.required[field];
					if(!this.whitespace(document.getElementById(name).value)) this.throwError(name,"required");
					else this.removeError(name,"required");
				}
			}
			if(this.rules.email){
				for(field in this.rules.email){
					name = this.rules.email[field];
					if(!this.email(document.getElementById(name).value)) this.throwError(name,"email");
					else this.removeError(name,"email");
				}
			}
			if(this.rules.number){
				for(field in this.rules.number){
					name = this.rules.number[field];
					if(this.isNumber(document.getElementById(name).value) && document.getElementById(name).value != "") this.removeError(name,"number");
					else this.throwError(name,"number");
				}
			}
			if(this.rules.length){
				var minium = this.rules.length.min;
				for(field in this.rules.length.values){
					name = this.rules.length.values[field];
					if(!this.minium(minium,document.getElementById(name).value)) this.throwError(name,"length");
					else this.removeError(name,"length");
				}
			}
			

			return !this.hasErrors(); /*Si hay error devovlerá true, de lo cotnrario false. Lo negamos para que sea devuelto y actúe como deba en el formulario*/
	},
	hasErrors: function(){
		var hasError = false;
		for(var err in this.error){
			if(hasError == false){
				if(this.error[err].length > 0) hasError=true;
			}
		}
		return hasError;
	},
	throwError: function(field,err){
		if(!isIn(this.error[field], err)){
			this.error[field].push(err);
			/*document.getElementById(field).style.border = "1px solid red";*/
			$("#"+field).addClass("error");	
		}
	},
	removeError: function(field, err){
		if(isIn(this.error[field], err))
			this.error[field] = remove(this.error[field], err);
		/*if(this.error[field].length < 1)
			$("#"+field).removeClass("error");*/
	}
};



function isIn(array,value){
	var response = false;
	var total = array.length;
	for(var i=0;i<total;i++){
		if(array[i] == value) response = true;
	}
	return response;
}

function remove(array, s){
	var response = array;
	for (i=0; i < response.length; i++){
  		if (s == response[i]) response.splice(i, 1);
 	}
	return response;
}


$(document).ready(function() {
	validar = new formValidator();
	validar.init("contacto",{ "required": ["nombre","apellidos","email","telefono","poblacion","comentario","cp"], 
							  "email": { "email": ["email"] },
							  "number": ["cp","telefono"],
							  "length": { "min": 5, "values": ["cp"]} 
							});
	
	$("#contacto").submit(function(){ return validar.validate(); });
});

/*$(document).ready(function() {
	// validate signup form on keyup and submit
		jQuery.validator.addMethod("nowhitespace", function(value, element) {
		return /^\S+$/i.test(value);
	}
	,"");
		
	var validator = $("#contacto").validate({		
		rules: {
			nombre: "required",
			apellidos: "required",
			telefono: "required",
			poblacion:"required",
			cp: {required:true,number:true, maxlength:5},
			comentario: "required",
			email: {required:true,email:true}

		},
		
		messages: {
			nombre: "",
			apellidos: "",
			telefono: "",
			poblacion: "",
			cp: {required:"",number:"", maxlength:""},
			comentario: "",
			email: {required:"",email:""}
		},

		// the errorPlacement has to take the table layout into account

		errorPlacement: function(error, element) {

			if ( element.is(":radio") )

				error.appendTo( element.parent().next().next() );

			else if ( element.is(":checkbox") )

				error.appendTo ( element.next() );

			else {

				error.appendTo( element.parent().next() );

				}

		},

		// set this class to error-labels to indicate valid fields

		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		}		
	
	});
});*/