
Forms = {
	Fields: {
		Contains: { 
			number: function(){
				if (isNaN( (this.value+'').replace(",", ".").replace(/\s+/g, "") )) return false;
				return true;
			},
			between: function(num1, num2, inc){
				num1 = num1 || -Infinity;
				num2 = num2 || Infinity;
				var v = this.value;
				if(inc) return (this.Contains.number() && (v >= num1 && v <= num2));
				return (this.Contains.number() && (v > num1 && v < num2));
			},
			length: function(num1, num2, inc){
				num1 = num1 || -Infinity;
				num2 = num2 || Infinity;
				var l = this.value.trim().length;
				if(inc) return (l >= num1 && l <= num2);
				return (l > num1 && l < num2);
			},
			email: function(){
				var re = new RegExp("\\w+([-+.\']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
				alert(re.exec(this.value)); // return true;
				return false;
				//var matches = rx.exec(this.value);
				//return (matches != null && this.value == matches[0])
			}
		},
		//empty: function(){ },
		//length: function(){ },
		equals: function(to){
			return (this.value == to);
		},
		
		selected: function(){
			console.log(arguments);
			console.log(this);
		},
		checked: function(){ }
	},
	Prompt: function(msg, el){
		alert(msg);
		if(el) el.focus();
		return false;
	},
	Validate: function(options){
		for(var x in options){
			for(var i in options[x]){
				if(x != 'radio' && x != 'checkbox'){
					var el = $(x);
					switch(i){
						case "empty":
							if(!el.Contains.length(1)) return Forms.Prompt(options[x][i], el);
							break;
						case "smaller":
							if(!el.Contains.length(null,options[x][i][0])) return Forms.Prompt(options[x][i][1], el);
							break;
						case "longer":
							if(!el.Contains.length(options[x][i][0])) return Forms.Prompt(options[x][i][1], el);
							break;
						case "numeric":
							if(!el.Contains.number()) return Forms.Prompt(options[x][i], el);
							break;
						case "between":
							if(!el.Contains.between(options[x][i][0][0], options[x][i][0][1])) return Forms.Prompt(options[x][i][1], el);
							break;
						case "length":
							var params = options[x][i][0];
							if(params.length < 3) for(var y = params.length; y < 3; y++) params[y] = null;
							if(!el.Contains.length(params[0], params[1], params[2])) return Forms.Prompt(options[x][i][1], el);
							break;
						case "email":
							if(!el.Contains.email()) return Forms.Prompt(options[x][i], el);
							break;
						default:
							break;
					}
				} else {
					console.log(i);
				}
			}
		}
		return true;
	}
}

/*
function notEmpty(field, msg){
	if(field.value == ""){
		alert(msg);
		field.focus();
		return false;
	}
	return true;
}
function longEnough(field, length, msg){
	console.log(field.value.length < length);
	if(field.value.length < length){
		console.log('return false');
		alert(msg);
		field.focus();
		return false;
	}
	console.log('return true');
	return true;
}
function smallerThan(field, length, msg){
	if(field.value.length > length){
		alert(msg);
		field.focus();
		return false;
	}
	return true;
}
function lengthBetween(field, range, msg){
	if(field.value.length < range[0] || field.value.length > range[1]){
		alert(msg);
		field.focus();
		return false;
	}
	return true;
}
function isNumber(field, msg){
	value = (field.value+'').replace(",", ".").replace(/\s+/g, "");
	if (isNaN(value)){
		alert(msg);
		field.select();
		field.focus();
		return false;
	}
	return true;
}
function validateForm(form, options){
	for(var x in options){
		for(var i in options[x]){
			if(x != 'radio' && x != 'checkbox'){
				switch(i){
					case "empty":
						//if(!notEmpty($(x), )) return false;
						if(smallerThan($(x), 0, options[x][i])) return false;
						break;
					case "smaller":
						if(smallerThan($(x), options[x][i][0], options[x][i][1])) return false;
						break;
					case "longer":
						if(longEnough($(x), options[x][i][0], options[x][i][1])) return false;
						break;
					default:
						break;
				}
			} else {
				console.log(i);
			}
		}
	}
	return true;
}
*/