// 
// InputController class
// 

function InputController(input, defaultCss, lenCss, maxlength) {
	var thisCopy = this;

	this.input = input;
	this.defaultCss = defaultCss;
	this.lenCss = lenCss;
	this.maxLength = maxlength;
	this.email = false;
	this.url = false;
	this.isNumeric = false;

	this.required = false;
	this.requiredText = null;
	this.requiredMarker = null;
	this.requiredFileFormats = null;

	this.input.onfocus = function() {return thisCopy.onFocus()};
	this.input.onblur = function() {return thisCopy.onBlur()};
	this.input.onkeyup = function() {return thisCopy.onKeyUp()};
	this.input.onchange = function() {return thisCopy.onChange()};

	this.onBlur();

	return this;
}

// event methods
InputController.prototype = {
	// event methods
	onFocus: function() {
		var newClassName = '';
		if (this.defaultCss) newClassName += this.defaultCss;
		if (this.lenCss) newClassName += ' ' + this.lenCss;
		if (newClassName) this.input.className = newClassName;
	},
	onBlur: function() {
		var newClassName = '';
		if (this.defaultCss) newClassName += this.defaultCss;
		if (this.lenCss) newClassName += ' ' + this.lenCss;
		if (newClassName) this.input.className = newClassName;
	},
	onKeyUp: function() {
		this.checkFieldValueLength();
		if (true == this.required && null != this.requiredMarker) this.checkRequiredMarker();
	},
	onChange: function() {
		if (true == this.required && null != this.requiredMarker) this.checkRequiredMarker();
		this.checkFieldValueLength();
		this.checkFieldValue();
	},
	// end event methods

	validate: function() {
		var error = '';
		if (true == this.required && true == this.isEmpty()) {
			switch (this.input.type) {
				case 'text':
				case 'password':
					error = "Пожалуйста, заполните поле '" + this.requiredText + "'";
					break;
				case 'textarea':
					error = "Введите, пожалуйста, текст в поле '" + this.requiredText + "'";
					break;
				case 'select-one':
					error = "Пожалуйста, выберите из выпадающего списка '" + this.requiredText + "'";
					break;
				case 'file':
					error = "Пожалуйста, определите файл в поле '" + this.requiredText + "'";
					break;
			}
		}
		if ((true == this.email) && (false == this.isEmpty()) && (false == this.isEmail(this.input.value))) error = 'Неправильно введен E-mail';
		if ('' != error) {
			alert(error);
			try {
				if (!this.input.readonly) this.input.focus();
			} catch (err) {
				alert(err.message);
			}
			return false;
		}
		return true;
	},

	isEmpty: function() {
		var empty = false;
		var reEmptyStr = new RegExp("\\s+");
		switch (this.input.type) {
			case 'text':
			case 'password':
			case 'textarea':
			case 'file':
				if ('' == str_replace(' ', '', str_replace('&nbsp;', '', this.input.value))) empty = true;
				break;
			case 'select-one':
				var value = str_replace(' ', '', str_replace('&nbsp;', '', this.input.options[this.input.selectedIndex].value));
				if (('' == value) || (0 == value)) empty = true;
				break;
		}
		return empty;
	},

	isEmail: function(value) {
		var re=/([\w,\-,\.]+\@[\w,\-,\.]+\.\w{2,4})/i;
		if (value.match(re)) return true;
		else return false;
	},

	checkFieldValue: function() {
		if (true == this.isNumeric) {
			value = this.input.value;
			value = str_replace(',', '.', value);
			value = parseFloat(value);
			value = pround(value, 2);
			this.input.value = value;
		}
	},

	checkFieldValueLength: function() {
		if (('textarea' == this.input.type) && (this.maxLength) && (this.input.value.length > this.maxLength)) {
			this.input.value = this.input.value.substring(0, this.maxLength);
		}
	},

	setRequired: function(requiredText, marker) {
		this.required = true;
		this.requiredText = requiredText;
		this.requiredMarker = marker;
		if (this.requiredMarker) this.checkRequiredMarker();
	},

	checkRequiredMarker: function() {
		this.requiredMarker.style.visibility = (true == this.isEmpty()) ? "visible" : "hidden";
	}
}

// 
// FormController class
// 

function FormController(form) {
	var thisCopy = this;

	this.form = form;
	this.list = new Array();
	this.beforeSubmitHandlers = new Array();
	this.afterSubmitHandlers = new Array();

	this.form.onsubmit = function() {return thisCopy.onSubmit()};

	return this;
}

FormController.prototype = {
	onSubmit: function() {
		var submit = get_form_submit(this.form);
		if (null != submit) submit.disabled = true;
		for (var i = 0; i < this.beforeSubmitHandlers.length; i++) {
			var func = this.beforeSubmitHandlers[i];
			if (false == func()) {
				if (null != submit) submit.disabled = false;
				return false;
			}
		}
		if (false == this.validate()) {
			if (null != submit) submit.disabled = false;
			return false;
		}
		for (var i = 0; i < this.afterSubmitHandlers.length; i++) {
			var func = this.afterSubmitHandlers[i];
			if (false == func()) {
				if (null != submit) submit.disabled = false;
				return false;
			}
		}
		return true;
	},

	add: function(input) {
		this.list[this.list.length] = input;
		return true;
	},

	addBeforeSubmitHandler: function(func) {
		this.beforeSubmitHandlers.push(func);
	},

	addAfterSubmitHandler: function(func) {
		this.afterSubmitHandlers.push(func);
	},

	// for compatible
	addSubmitHandler: function(func) {
		this.afterSubmitHandlers.push(func);
	},

	addRange: function() {
		var i = 0;
		for (i = 0; i < arguments.length; i++) {
			if (false == this.add(arguments[i])) return false;
		}
		return true;
	},

	remove: function(input) {
		var i = 0;
		for (i = 0; i < this.list.length; i++) {
			if (input == this.list[i]) this.list.splice(i, 1);
		}
		return true;
	},

	removeByName: function(name, is_prefix) {
		id_prefix = (undefined == is_prefix) ? is_prefix : false;
		var i = 0;
		for (i = 0; i < this.list.length; i++) {
			if ((true == is_prefix) && (name == this.list[i].input.name.substr(0, name.length))) {
				this.list.splice(i, 1);
			} else if ((false == is_prefix) && (name == this.list[i].input.name)) {
				this.list.splice(i, 1);
			}
		}
	},

	validate: function() {
		var i = 0;
		for (i = 0; i < this.list.length; i++) {
			if (false == this.list[i].validate()) return false;
		}
		return true;
	},

	raiseError: function(errorText) {
		if (!errorText) return;
		var i = 0;
		for (i = 0; i < this.list.length; i++) {
			if ((this.list[i].input.name.length <= errorText.length) && (this.list[i].input.name == errorText.substring(0, this.list[i].input.name.length))) {
				this.list[i].input.className += ' inpAlert';
			}
		}
	}
}