var newPortfolioModule = (function() {

	var _titleInput;
	var _descriptionInput;

	var _titleErrorField;
	var _descriptionErrorField;
  var _filesErrorField;

	var _form;

	var _init = function(data) {
		_titleInput = document.querySelector(data["titleInput"]);
		_descriptionInput = document.querySelector(data["descriptionInput"]);

		_titleErrorField = document.querySelector(data["titleErrorField"]);
		_descriptionErrorField = document.querySelector(data["descriptionErrorField"]);
    _filesErrorField = document.querySelector(data["filesErrorField"]);

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

		_setEvents();
	};
	var _setEvents = function() {
		_titleInput.addEventListener("input", _checkTitle);
		_descriptionInput.addEventListener("input", _checkDescription);
		_form.onsubmit = _formSubmited;
	};
	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 _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 "files":
				return _filesErrorField;
		}
	};
	var _checkAll = function() {
		if(!_checkTitle()) {
			_scrollTo(_titleInput);
			return false;
		}
		if(!_checkDescription()) {
			_scrollTo(_descriptionInput);
			return false;
		}
    _unsetError(_filesErrorField);
		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
	}
})();
newPortfolioModule.init({
	"titleInput":".js-title-input",
	"descriptionInput":".js-description-input",

	"titleErrorField":".js-title-error-field",
	"descriptionErrorField":".js-description-error-field",
  "filesErrorField":".js-files-error-field",

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