//Copyright 2008 www.liteweb.info
//last modified friday 29 aughust 2008
//Version 1.2

var liteweb_Val =
{
	ValEmail: function(str) {
		var pattern = /^[a-z]([\.\-\_]{0,}([a-z_0-9]|[a-z_0-9]\.[a-z_0-9])*)+@([a-z0-9]|[a-z0-9][\.\-][a-z0-9])+\.[a-z]{2,}$/i;
		return pattern.test(str);
	},
	ValUserName: function(str) {
		var pat = /^[a-z]{1,}([a-z_0-9]|-)*$/i;
		return pat.test(str);
	},
	ValNoNumbers: function(str) {
		var pat = /\d/;
		return !pat.test(str);
	},
	Mendatory: function(str, min, max) {
		return str.length > 0;
	},
	CheckLength: function(str, min, max) {
		var l = str.length;
		return (defined(min) ? l >= min : true) && (max ? l <= max : true);
	},
	ValNumbers: function(str, min, max) {
		var pat = /^\d*$/;
		return pat.test(str) && (defined(min) ? parseFloat(str) >= min : true) && (max ? parseFloat(str) <= max : true);
	},
	ValFloat: function(str, min, max) {
		if (str.length == 0 && (!defined(min) || min == 0))
			return true;
		var pat = /^-?\d*(\.\d+)?$/;
		return pat.test(str) && (defined(min) ? parseFloat(str) >= min : true) && (max ? parseFloat(str) <= max : true);
	},
	ValDate: function(str) {
		var pat = /^\d{1,2}(\/|-)\d{1,2}(\/|-)\d{2,4}$/;
		if (pat.test(str)) {
			var sep = "-";
			var i = str.indexOf("/");
			if (i > 0)
				sep = "/";
			var ar = str.split(sep);
			if (ar.length != 3)
				return false;
			if (ar[0] > 12 && ar[1] > 12)
				return false;
			if (ar[2].length == 3 || ar[2].length > 4)
				return false;
			return true;
		}
		return false;
	},
	ValPassword: function(s) {
		return true;
	},
	ValidateImage: function(str) {
		if (!str)
			return true;
		var pat = /\.((jpg)|(jpeg)|(bmp)|(gif)|(png))$/i;
		return pat.test(str);
	},
	Messages:
	{
		Mendatory: "Enter a value in the \"{0}\" field.",
		MendatoryLimited: "{0} must be between {1} and {2} characters long.",
		ValNumbers: "Enter only numbers in the \"{0}\" field.",
		ValNumbersLimited: "{0} must be between {1} and {2}.",
		ValEmail: "Enter a correct email address in the \"{0}\" field.",
		ValImage: "Enter only images in the \"{0}\" field",
		ValPassword: "Password must contain at least one capital letter and one number."
	},
	Errors: [],
	Validator: function(_Mendatory, min, max, _Function, _Message, dep) {
		this.Validate = true;
		this.CheckMendatory = _Mendatory && _Function != "Mendatory";
		this._Mendatory = _Mendatory;
		this.min = min;
		this.max = max;
		if (_Function && typeof (_Function) == "string" && liteweb_Val[_Function])
			this._Function = liteweb_Val[_Function];
		else if (!_Function && _Mendatory)
			this._Function = liteweb_Val["Mendatory"];
		else if (typeof (_Function) == "function")
			this._Function = _Function;
		else
			this.Validate = false;
		this.Depends = dep;
		this.Message = liteweb_Val.Messages[_Message] ? liteweb_Val.Messages[_Message] : _Message;
		this.Exec = function(Form, Field, FieldName) {
			FieldName = FieldName ? FieldName : Field.name;
			var _ = liteweb_Val;
			if (this.Validate) {
				_.DownLight(Field);
				if (this.CheckMendatory) {
					if (!_.Mendatory(Field.value)) {
						var m = _.Messages["Mendatory"].Format(FieldName);
						_.Hilight(Field, m);
						Form.Errors.push(m);
						return;
					}
					if (min || max)
						if (!_.CheckLength(Field.value, min, max)) {
						var m = _.Messages["MendatoryLimited"].Format(FieldName, min, max);
						Form.Errors.push(m);
						_.Hilight(Field, m);
						return;
					}
				}
				if (typeof this._Function == "function") {
					if (!this._Function(Field.value, min, max)) {
						var m = this.Message.Format(FieldName, min, max);
						_.Hilight(Field, m);
						Form.Errors.push(m);
						return;
					}
				}
			}
			if (typeof this.Depends == "function")
				if (!this.Depends())
				return false;
		}
	},
	OldBorderColors: {},
	OldTitles: {},
	Hilight: function(Field, Message) {
		try {
			var _ = liteweb_Val;
			_.OldBorderColors[Field.form.name + "_" + Field.name] = Field.style.borderColor;
			Field.style.borderColor = "red";
			if (Field.document.getElementById(_.Forms[Field.form.name].patern + Field.name))
				Field.document.getElementById(_.Forms[Field.form.name].patern + Field.name).innerHTML = Message;
			_.OldTitles[Field.form.name + "_" + Field.name] = Field.title;
			Field.title = Message;
		} catch (e) { }
	},
	DownLight: function(Field) {
		try {
			var _ = liteweb_Val;
			Field.style.borderColor = _.OldBorderColors[Field.form.name + "_" + Field.name];
			if (Field.document.getElementById(_.Forms[Field.form.name].patern + Field.name))
				Field.document.getElementById(_.Forms[Field.form.name].patern + Field.name).innerHTML = "";
			Field.title = _.OldTitles[Field.form.name + "_" + Field.name];
		} catch (e) { }
	},
	Form: function(f, fields, patern, _alert, box, serverContainer) {
		this.form = f;
		this.fields = fields;
		this.patern = patern;
		this._alert = _alert;
		this.box = box ? document.getElementById(box) : null;
		this.serverContainer = serverContainer ? serverContainer : "";
		$(f).bind("submit", function() {
			return liteweb_Val.Forms[f.id].Validate();
		});
		this.Errors = [];
		if (this.serverContainer) {
			var _fields = {};
			for (i in fields) {
				var temp = this.serverContainer + i;
				_fields[temp] = fields[i];
				_fields[temp].field = f[temp];
			}
			fields = null;
			this.fields = _fields;
		}
		for (i in this.fields) {
			this.fields[i].field.onfocus = new Function("liteweb_Val.FocusIn(this)");
			this.fields[i].field.onblur = new Function("liteweb_Val.FocusOut(this)");
		}
		this.Validate = function() {
			this.Errors = [];
			if (this.box)
				this.box.style.display = "none";
			for (i in this.fields)
				this.fields[i].Validate(i);
			if (this.Errors.length > 0) {
				if (this.box) {
					box.innerHTML = "Please correct the following before submitting: <ul><li>" + this.Errors.join("</li><li>") + "</li></ul>";
					box.style.display = "block";
				}
				if (this._alert)
					alert("Please correct the following before submitting:\n\n\n\t" + this.Errors.join("\t\t\n\t") + "\n\n");
				return false;
			}
			return true;
		};
		this.Submit = function() {
			this.form.submit();
		};
	},
	Field: function(f, name, Name, val) {
		this.f = f;
		this.name = name;
		this.Name = Name ? Name : name;
		this.vals = val;
		this.field = f[name];
		this.Validate = function() {
			for (var i = 0; i < this.vals.length; i++) {
				try {
					this.vals[i].Exec(liteweb_Val.Forms[this.f.id], this.field, this.Name);
				}
				catch (e) {
					alert(e.description);
				}
			}
		};
		this.Submit = function() {
			this.f.submit();
		};
	},
	FocusIn: function(f) {
	},
	FocusOut: function(f) {
		var o = liteweb_Val.Forms[f.form.id];
		if (o)
			if (o.fields[f.id])
			o.fields[f.id].Validate();
	},
	Forms: {},
	V: function(f) {
		if (liteweb_Val.Forms[f.id])
			return liteweb_Val.Forms[f.id].Validate();
		else if (liteweb_Val.Forms[f.id])
			return liteweb_Val.Forms[f.id].Validate();
		return true;
	}
};
var Ready_Vals = 
{
	Mendatory: new liteweb_Val.Validator(true),
	Email: new liteweb_Val.Validator(true, 5, 150, "ValEmail", "ValEmail"),
	UserName: new liteweb_Val.Validator(true, 5, 25, "ValUserName", "Enter a correct username in the \"{0}\" field."),
	Number: new liteweb_Val.Validator(false, null, null, "ValNumbers", "ValNumbers"),
	Image: new liteweb_Val.Validator(false, null, null, "ValidateImage", "ValImage"),
	Money: new liteweb_Val.Validator(false, 0, null, "ValFloat", "Please enter a money value in the \"{0}\" field."),
	Password: new liteweb_Val.Validator(true, 6, 20, "ValPassword", "ValPassword")
};
try
{
	if(liteweb)
		liteweb.v = liteweb_Val;
}
catch(E)
{
}
