// 
// 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.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.OnFocus = function() {
	this.Input.className = (this.DefaultCss ? this.DefaultCss + ' ' : '') + 'focus' + (this.LenCss ? this.LenCss + ' ' : '');
}

InputController.prototype.OnBlur = function() {
	this.Input.className = this.DefaultCss + " " + this.LenCss;
}

InputController.prototype.OnKeyUp = function() {
	this.CheckLength();
}

InputController.prototype.OnChange = function() {
	this.CheckLength();
}
// end event methods

InputController.prototype.CheckLength = function() {
	if (('textarea' == this.Input.type) && (this.MaxLength) && (this.Input.value.length > this.MaxLength)) {
		this.Input.value = this.Input.value.substring(0, this.MaxLength);
	}
	if (true == this.Required && null != this.RequiredMarker) this.CheckRequiredMarker();
}

InputController.prototype.Validate = function() {
	var error = '';
	this.CheckLength();
	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);
		this.Input.focus();
		return false;
	}
	return true;
}

InputController.prototype.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;
}

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

InputController.prototype.SetRequired = function(requiredText, marker) {
	this.Required = true;
	this.RequiredText = requiredText;
	this.RequiredMarker = marker;
	if (this.RequiredMarker) this.CheckRequiredMarker();
}

InputController.prototype.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.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;
	if (false == this.Validate()) {
		if (null != submit) submit.disabled = false;
		return false;
	}
	return true;
}

FormController.prototype.Add = function(input) {
	this.List[this.List.length] = input;
	return true;
}

FormController.prototype.AddRange = function() {
	var i = 0;
	for (i = 0; i < arguments.length; i++) {
		if (false == this.Add(arguments[i])) return false;
	}
	return true;
}

FormController.prototype.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;
}

FormController.prototype.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);
		}
	}
}

FormController.prototype.Validate = function() {
	var i = 0;
	for (i = 0; i < this.List.length; i++) {
		if (false == this.List[i].Validate()) return false;
	}
	return true;
}

FormController.prototype.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";
		}
	}
}