var newProjectModule = (function() {

	var _titleInput;
	var _descriptionInput;
	var _categorySelect;
	var _subCategoriesSelect;
	var _subCategorySelect;
	var _emailInput;

	var _titleErrorField;
	var _descriptionErrorField;
	var _categoryErrorField;
	var _emailErrorField;

	var _form;

	var _init = function(data) {
		_titleInput = document.querySelector(data["titleInput"]);
		_descriptionInput = document.querySelector(data["descriptionInput"]);
		_categorySelect = document.querySelector(data["categorySelect"]);
		_subCategoriesSelect = document.querySelectorAll(data["subCategoriesSelect"]);
		_emailInput = document.querySelector(data["emailInput"]);

		_titleErrorField = document.querySelector(data["titleErrorField"]);
		_descriptionErrorField = document.querySelector(data["descriptionErrorField"]);
		_categoryErrorField = document.querySelector(data["categoryErrorField"]);
		_emailErrorField = document.querySelector(data["emailErrorField"]);

		_form = document.querySelector(data["form"]);

		_setEvents();
	};
	var _setEvents = function() {
		_categorySelect.addEventListener("change", _categoryChanged);
		_titleInput.addEventListener("input", _checkTitle);
		_descriptionInput.addEventListener("input", _checkDescription);
		_form.onsubmit = _formSubmited;
	};
	var _categoryChanged = function(e) {
		_subCategorySelect = null;
		for(var i = 0; i < _subCategoriesSelect.length; i++) {
			if(e.target.value == _subCategoriesSelect[i].dataset.catid) {
				_subCategoriesSelect[i].classList.remove("hidden");
				_subCategoriesSelect[i].name = "category";
				_subCategoriesSelect[i].required = true;
				_subCategorySelect = _subCategoriesSelect[i];
			}
			else {
				_subCategoriesSelect[i].classList.add("hidden");
				_subCategoriesSelect[i].name = "";
				_subCategoriesSelect[i].required = false;
			}
			_subCategoriesSelect[i].selectedIndex = 0;
		}
		_checkCategory();
	};
	var _checkTitle = function() {
		var text = _titleInput.value;
		if(!text) {
			_setError(_titleErrorField, "Введите название запроса");
			return false;
		}
		if(text.length > 70) {
			_setError(_titleErrorField, "Максимальная длина названия 70 символов");
			return false;
		}
		_unsetError(_titleErrorField);
		return true;
	};
	var _checkDescription = function() {
		var text = _descriptionInput.value;
		if(!text) {
			_setError(_descriptionErrorField, "Введите описание запроса");
			return false;
		}
		if(text.length > 500) {
			_setError(_descriptionErrorField, "Максимальная длина описания 500 символов");
			return false;
		}
		_unsetError(_descriptionErrorField);
		return true;
	};
	var _checkCategory = function() {
		if(_categorySelect.value == 0) {
			_setError(_categoryErrorField, "Выберите категорию");
			return false;
		}
		_unsetError(_categoryErrorField);
		return true;
	};
	var _formSubmited = function() {
		if(!_checkAll()) return false;
		var formData = new FormData(document.sendDodjiRequest);
		formData = formDataFilter(formData);
		var xhr = new XMLHttpRequest();
		xhr.open("post", $('#sendDodjiRequest').attr('action'));
		xhr.send(formData);

		xhr.onreadystatechange = function () {
			if (this.readyState != 4) return;
			try {
				var response = JSON.parse(this.responseText);
				if(response.result === 'success') {
					document.location.href = response.redirectUrl;
				} else {
					if (response.errors !== undefined) {
						for (var field in response.errors) {
							_setError(_getErrorFieldByName(field), response.errors[field]);
						}
					}
				}
			}
			catch (e) {
				console.error('Некорректный ответ json');
				return false;
			}
		};
		return false;
	};
	var _getErrorFieldByName = function(fieldName) {
		switch(fieldName) {
			case "title":
				return _titleErrorField;
			case "description":
				return _descriptionErrorField;
			case "category":
				return _categoryErrorField;
			case "email":
				show_login(true, function() {
					_formSubmited();
				}, _emailInput.value);
				return _emailErrorField;
		}
	};
	var _checkAll = function() {
		if(!_checkTitle()) {
			_scrollTo(_titleInput);
			return false;
		}
		if(!_checkDescription()) {
			_scrollTo(_descriptionInput);
			return false;
		}
		if(!_checkCategory()) {
			_scrollTo(_categorySelect);
			return false;
		}
		return true;
	};
	var _setError = function(errorField, text) {
		errorField.innerText = text;
		errorField.classList.remove("hidden");
	};
	var _unsetError = function(errorField) {
		errorField.innerText = "";
		errorField.classList.add("hidden");
	};
	var _scrollTo = function(field) {
		$('html, body').animate({
			scrollTop: field.offsetTop - 120
		}, 200);
	};
	return {
		init:_init
	}
})();
newProjectModule.init({
	"titleInput":".js-title-input",
	"descriptionInput":".js-description-input",
	"categorySelect":".js-category-select",
	"subCategoriesSelect":".js-sub-category-select",
	"emailInput":".js-email-input",

	"titleErrorField":".js-title-error-field",
	"descriptionErrorField":".js-description-error-field",
	"categoryErrorField":".js-category-error-field",
	"emailErrorField":".js-email-error-field",

	"form":".js-form"
});