go.forms = new (function() {

this.cForm = (
	function(form)
	{
		this.form = go.$(form);
		this.props = {};
	}
);

var pForm = {
	
	"checker": (
		function(name, checks, title)
		{
			if (this.baseName) {
				name = this.baseName + "[" + name + "]";
			}
			checks.title = checks.title || title || "поле";
			this.props[name] = checks;
			return true;
		}
	),
	
	"run": (
		function(period)
		{
			this.scanAll();
			this.checkAll();
			if (!period) {
				period = 500;
				go.timers.set({"object": this, "method": "checkAll"}, period);
			}
			go.events.add(this.form, "submit", {"object": this, "method": "onSubmit"});
			return true;
		}
	),
	
	"disable": (
		function()
		{
			this._disable = true;
			if (!this._disDiv) {
				var d = document.createElement("div");
				this._disDiv = d;
				this.form.appendChild(d);				
				d.style.position = "absolute";								
				d.appendChild(document.createTextNode("Loaded ..."));
				d.style.fontWeight = "bold";
				d.style.color      = "black";
				d.style.textAlign  = "center";	
				d.style.verticalAlign  = "middle";	
				d.style.backgroundColor = "#999999";								
				d.style.opacity = 0.5;
				d.style.filter  = "alpha(opacity=50)";
			} else {
				d = this._disDiv;
				d.style.display = "block";
			}
			d.style.marginTop = "-" + this.form.offsetHeight + "px";
			d.style.width  = this.form.offsetWidth  + "px";
			d.style.height = this.form.offsetHeight + "px";							
			return true;
		}
	),
	
	"enable": (
		function() 
		{
			this._disable = false;
			if (this._disDiv) {
				this._disDiv.style.display = "none";
			}
			return true;
		}
	),
	
	"scanAll": (
		function()
		{
			var aels = [
				document.getElementsByTagName("input"), 
				document.getElementsByTagName("textarea"),
				document.getElementsByTagName("select")
			];
			var alen = aels.length;
			for (var i = 0; i < alen; i++) {
				var ael = aels[i];				
				var len = ael.length;
				for (var j = 0; j < len; j++) {
					var el = ael[j];
					name = el.getAttribute("name");						
					if ((name) && (this.props[name])) {
						this.props[name].el = el;
						go.events.add(el, "change", {object: this, "method": this.onChange});
						if (this.props[name].init) {
							me(this.props[name].init, el, [this]);
						}
					}
				}
			}
			return true;
		}
	),
	
	"onChange": (
		function()
		{
			this.checkAll();
			return true;
		}
	),
	
	"checkAll": (
		function() 
		{
			if (this._disable) {
				return true;
			}
			var result = true;
			var messages = [];
			for (var name in this.props) {
				if (!this.checkItem(this.props[name])) {
					result = false;
					messages.push(this._msg);
				}				
			}
			this._messages = messages;
			return result;
		}
	),
	
	"checkItem": (
		function(prop) 
		{
			if (this.checkItemL(prop)) {
				this.drawMessage(prop, true);
				return true;
			} 
			this._msg = this._msg.charAt(0).toUpperCase() + this._msg.substr(1);
			this.drawMessage(prop, false, this._msg);
			return false;
		}
	),
	
	"checkItemL": (
		function(prop) 
		{			
			var el = prop.el;
			var value = el.value;
			if (!prop.notrim) {
				value = value.replace(/^\s+/, "").replace(/\s+$/, "");
			}
			var len = value.length;
			if (prop.min > 0) {
				if (len == 0) {
					this._msg = prop.msgEmpty || ("введите " + prop.title);					
					return false;
				}
				if (len < prop.min) {
					this._msg = prop.msgMin || (prop.title + " короче " + prop.min);
					return false;
				}
			}
			if ((prop.max) && (len > prop.max)) {
				this._msg = prop.msgMax || (prop.title + " длинее " + prop.max);
				return false;
			}			
			if ((prop.reg) && (value.search(prop.reg) == -1)) {
				this._msg = prop.msgReg || (prop.title + " имеет неверный формат");
				return false;
			}
			if ((prop.checked) && (!el.checked)) {
				this._msg = prop.msgChecked || (prop.title + " д.б. выбрано");
				return false;
			}
			if (prop.func) {
				return me(prop.func, this, [prop, value, this]);
			}
			return true;
		}
	),
	
	"drawMessage": (
		function(prop, result, msg)
		{
			if (prop.nodraw) {
				return true;
			}
			if (!prop.img) {
				prop.img = document.createElement("IMG");
				prop.img.setAttribute("alt", "");
				var p = prop.el.parentNode;
				p.appendChild(document.createTextNode(" "));
				p.appendChild(prop.img);
				p.appendChild(document.createTextNode(" "));				
				prop.msg = document.createElement("STRONG");
				prop.msg.appendChild(document.createTextNode(" "));
				prop.msg.className = "red";
				p.appendChild(prop.msg);
			}
			if (result) {
				prop.img.src = IMG_SRC_TRUE;
				prop.msg.firstChild.nodeValue = " ";
			} else {
				prop.img.src = IMG_SRC_FALSE;
				prop.msg.firstChild.nodeValue = msg;
			}
			return true;
		}
	),
	
	"onSubmit": (
		function()
		{
			if (this._disable) {
				return false;
			}
			if (!this.checkAll()) {
				go.events.preventDefault();
				if (this.errorsOL) {
					this.errorsOL.style.display = "";
					go.dom.clear(this.errorsOL);
					var m = this._messages;
					var len = m.length;
					for (var i = 0; i < len; i++) {
						var li = document.createElement("li");
						li.appendChild(document.createTextNode(m[i]));
						this.errorsOL.appendChild(li);
					}
				}
				return true;
			} else {
				if (this.errorsOL) {
					this.errorsOL.style.display = "none";
				}
			}
			this.submit();
			return true;
		}
	),
	
	"submit": (
		function()
		{
			return true;
		}
	),
	
	"getEl": (
		function(name)
		{
			if (this.baseName) {
				name = this.baseName + "[" + name + "]";
			}
			return this.props[name] && this.props[name].el;
		}
	),
	
	"setBaseName": (function(bn) {this.baseName = bn;}),
	"setErrorsOL": (function(ol) {this.errorsOL = go.$(ol);})	
};

this.cForm.prototype = pForm;


//var props = {};
var me = go.methodExec;

var IMG_SRC_TRUE  = "/img/true.gif";
var IMG_SRC_FALSE = "/img/false.gif";

var _t1 = document.createElement("IMG");
_t1.src = IMG_SRC_TRUE;
var _t2 = document.createElement("IMG");
_t2.src = IMG_SRC_FALSE;

})(); 