function FormErrors(err_msgs,obj_form) { this._construct(err_msgs,obj_form) }
(function() {
    FormErrors.prototype = {
        // Internals.
        _obj_form:		null,
        _objects:		null,
        _err_msgs:		null,
        _errors:		null,
        _internal_errs:	null,
		_defmsg		  : "This field is incorrect",
        // Constructor.
        _construct: function(err_msgs,obj_form) {
			this._objects = new Array();
			this._errors = new Array();
        	if(err_msgs != null) {
	        	this._err_msgs = err_msgs;
			} else {
			    _internal_errs = {'_construct' : 'Texts of the messages for errors are absent.'};
			}
        	if(obj_form != null) {
			    var form = this._getElementById(obj_form);
				if(!form) {
					form = this._getElementsByName(obj_form);
				}
        		this._obj_form = form;
        	}
        },

		_getElementById: function(id) {
			try {
				if (document.getElementById != null)
				    return document.getElementById(id);
				if (document.all != null)
				    return document.all[id];
			} catch (e) {}
			return null;
		},

		_getElementsByName: function(name) {
			if (document.getElementsByName != null) {
			    var _obj = document.getElementsByName(name);
			    return _obj[0];
			}
			return null;
		},

        add: function(field,types) {
		    var _obj = this._getElementById(field);
			if(_obj == null) {
				_obj = this._getElementsByName(field);
			}
			if(_obj != null)
				this._objects.push({'object': _obj,'types': types});
        },

        _trim: function(str) {
			str = str.replace(/(^\s+)|(\s+$)/g,"");
		    return str;
        },

        checkErrors: function() {
		    for(var i=0;i<this._objects.length;i++) {
		    	this._checkError(this._objects[i]['object'],this._objects[i]['types']);
			}
			var _errPrint = this._getElementById("errPrint");
			if(this._errors.length > 0) {
				if(_errPrint != null) {
					_errPrint.style.display = 'block';
					_errPrint.style.fontWeight = 'bold';
					_div = this._getElementById(_errPrint.id+"Node");
					if(_div != null) {
						_errPrint.removeChild(_div);
					}
					_div = document.createElement("DIV");
					_div.id = _errPrint.id+"Node";
					_div.style.fontWeight = 'lighter';
					_errPrint.appendChild(_div);
				}
			    for(var i=0;i<this._errors.length;i++) {
					if(_errPrint != null) {
						_a = document.createElement("A");
						_a.id = "a"+this._errors[i]['id'];
						_a.value = this._errors[i]['id'];
						_a.href = "javascript:void(0)";
						_a.onclick = function() { document.getElementById(this.value).focus(); }
						_a.style.color = "red";
						_a.style.marginTop = 5;
						_a.innerHTML = this._errors[i]['msg'];
						_div.appendChild(_a);
						_div.appendChild(document.createElement("P"));
					}
					_a = this._getElementById("aer"+this._errors[i]['id']);
					if(_a == null) {
						_a = document.createElement("A");
						_a.id = "aer"+this._errors[i]['id'];
						_a.style.color = "red";
						if(_field = this._getElementById(this._errors[i]['id'])) {
							if(_errdiv = this._getElementById("err"+this._errors[i]['id'])) {
								_errdiv.appendChild(_a);
							} else {
					            if (_field.nextSibling) 
    		        			    _field.parentNode.insertBefore(_a,_field.nextSibling);
					            else _field.parentNode.appendChild(_a);
								_field.parentNode.insertBefore(document.createElement("BR"),_a);
							}
							if(i == 0) _field.focus();
						}
					}
//					_a.innerHTML = this._errors[i]['msg'];
//					_a.style.display = 'block';
				}
				return true;
			} else {
				if(_errPrint != null) {
					_errPrint.style.display = 'none';
				}
			}
			return false;
        },

        _checkError: function(obj,types) {
			this._delMsg(obj);
        	switch (obj.tagName) {
				case "INPUT":
				    if(obj.type == "text" || obj.type == "password")
						if(typeof(types) == "undefined") this._isEmpty(obj);
						else this._checkTypes(obj,types);
					else if(obj.type == "checkbox" && !obj.checked) {
						this._errors.push({'id': obj.id,'msg': this._setMsg(obj.id,'empty')});
					}
				break;
				case "TEXTAREA":
					if(typeof(types) == "undefined") this._isEmpty(obj);
					else this._checkTypes(obj,types);
				break;
				case "SELECT":
					if(typeof(types) == "undefined") this._isEmpty(obj);
					else this._checkTypes(obj,types);
				break;
        	}
        },

		_checkTypes: function(obj,types) {
			for (key in types)
				if(key == 'empty' && this._isEmpty(obj,types[key])) return;
				else if(key == 'length' && this._isEqual(obj,types[key],1)) break;
				else if(key == 'equal' && this._isEqual(obj,types[key])) break;
				else if(key == 'function' && this._isFunction(obj,types[key])) return;
		},

        _isEmpty: function(obj, value) {
            value = (value != null)?value:"";
        	if(this._trim(obj.value) == value) {
				this._errors.push({'id': obj.id,'msg': this._setMsg(obj.id,'empty')});
				return true;
			}
			return false;
        },

        _isEqual: function(obj, types, option) {
            if(typeof(types) != "undefined") {
                var err = false;
				var val;
				try {
				    if(typeof(option) != "undefined")
		                val = (new String(obj.value)).length;
	        	    else
		                val = obj.value;
				} catch (e) {}
				for (key in types) {
				    if(key == '<' && val < this._getValueById(types[key],option)) { err = key; break; }
					else if(key == '>'  && val >  this._getValueById(types[key],option)) { err = key; break; }
					else if(key == '<=' && val <= this._getValueById(types[key],option)) { err = key; break; }
					else if(key == '>=' && val >= this._getValueById(types[key],option)) { err = key; break; }
					else if(key == '==' && val == this._getValueById(types[key],option)) { err = key; break; }
					else if(key == '!=' && val != this._getValueById(types[key],option)) { err = key; break; }
				}
				if(err) {
					this._errors.push({'id': obj.id,'msg': this._setMsg(obj.id,'equal',err)});
					return true;
				}
			}
        	return false;
        },

        _isFunction: function(obj, types) {
            if(typeof(types) != "undefined") {
				var funcName = types.shift();
				var func = funcName;
				if(types[0] == 'this') types[0] = obj.value;
				for(var i=0;i<types.length;i++)
					types[i] = (new String(types[i])).replace(/\'+/g,"\\\'");
				func += "('"+types.join("','")+"')";
				try {
				    if(!eval(func)) {
						this._errors.push({'id': obj.id,'msg': this._setMsg(obj.id,'function',funcName)});
						return true;
					}
				} catch(e){}
			}
        	return false;
        },

        _delMsg: function(obj) {
            var _a = this._getElementById("aer" + obj.id);
            try {
	       		if (typeof(_a) == "object") {
					_a.style.display = 'none';	       			
//    	    	    if(_a.previousSibling != null && _a.previousSibling.tagName == "BR")
//						_a.parentNode.removeChild(_a.previousSibling);
//					_a.parentNode.removeChild(_a);
	        	}
	        } catch (e) {}
        },

        _setMsg: function(id,type,type2) {
			if(typeof(this._err_msgs) == "object") {
				if(typeof(this._err_msgs['err_per_msgs']) == "object" && typeof(this._err_msgs['err_per_msgs'][id]) == "object") {
					if(typeof(this._err_msgs['err_per_msgs'][id][type]) == "object" && type2 != "undefined" && typeof(this._err_msgs['err_per_msgs'][id][type][type2]) != "undefined") {
						return this._err_msgs['err_per_msgs'][id][type][type2];
					} else if (typeof(this._err_msgs['err_per_msgs'][id][type]) != "undefined") {
						return this._err_msgs['err_per_msgs'][id][type];
					}
				} else if (typeof(this._err_msgs[type]) != "undefined") {
					return this._err_msgs[type];
				}
			}
			return this._defmsg;
        },

		_getValueById: function(id,type) {
			var _obj = this._getElementById(id);
			try {
				if(_obj != null && typeof(type) != "undefined")
		            return (new String(_obj.value)).length;
	    	    else if(_obj != null)
				    return _obj.value;
			} catch(e) {}
			return id;
		}
	}
})();

function email_is_valid(_email) {
    email_re1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(\.$)/
    email_re2 = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
    if (_email == "" || email_re1.exec(_email) || !email_re2.exec(_email))
        return false
    return true
}

