var BorderColorNormal = "#c7c7c7";
var BorderColorError = "#ff7301";
var intImageCounter = 1;
var regexpMail = /^[\w\.-]+@[\w\.-]+\.[a-z]{2,4}$/i;
var regexpСode = /^\d{5}$/;
var intMaxTextLength = 2000;

function MakeRequest(request) {
	var http_request = false;
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {}
		}
	}

	if (!http_request) {
		alert("Для корректной работы скрипта, интернет-обозреватель должен поддерживать XMLHTTP-запросы.");
		return false;
	} else {
		http_request.open("POST", "request.php", false);
		http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http_request.send(request);
		return http_request.responseText;
	}
}

function ResetBorderColor(element) {
	element.style.borderColor = BorderColorNormal;
}

function ShowAlert(element, text_alert) {
	var oInfo = document.getElementById("info");
	oInfo.innerHTML = text_alert;
	element.style.borderColor = BorderColorError;
	element.focus();
	alert(text_alert);
}

function ShowSmallAlert(text_alert) {
	var oInfo = document.getElementById("info");
	oInfo.innerHTML = text_alert;
	alert(text_alert);
}

function OnSubmitClick() {
	var oName = document.getElementById("name");
	var oMail = document.getElementById("email");
	var oText = document.getElementById("text");
	var oCode = document.getElementById("code");
	var oButton = document.getElementById("submitButton");
	var boolButtonEnable = true;
	oButton.disabled = true;
	oName.style.borderColor = BorderColorNormal;
	oMail.style.borderColor = BorderColorNormal;
	oText.style.borderColor = BorderColorNormal;
	oCode.style.borderColor = BorderColorNormal;
	if (oName.value.length < 1) {
		ShowAlert(oName, "Введите ваше имя.");
	} else if (!regexpMail.test(oMail.value)) {
		ShowAlert(oMail, "Введите корректный адрес электронной почты.");
	} else if (oText.value.length < 1) {
		ShowAlert(oText, "Напишите текст письма.");
	} else if (!regexpСode.test(oCode.value)) {
		ShowAlert(oCode, "Введите корректный код подтверждения.");
	} else {
		var request = "page=contact&name=" + encodeURIComponent(oName.value) +
			"&mail=" + encodeURIComponent(oMail.value) +
			"&text=" + encodeURIComponent(oText.value) +
			"&code=" + encodeURIComponent(oCode.value) +
			"&order_from=" + encodeURIComponent(GetCookie("user_from"));
		switch(MakeRequest(request)) {
		case "cookie" :
			ShowSmallAlert("Для отправки письма, в интернет-обозревателе должны быть включены Куки (Cookie).");
			break;
		case "code" :
			ShowAlert(oCode, "Введите корректный код подтверждения.");
			break;
		case "done" :
			boolButtonEnable = false;
			oText.value = "";
			oCode.value = "";
			RefreshImage(false);
			oButton.disabled = true;
			ShowSmallAlert("Ваше письмо отправлено. Спасибо.");
			DisabledButton(9);
			break;
		default :
			ShowSmallAlert("Во время отправки письма произошла ошибка. Попробуйте повторить попытку.");
		}
	}
	if (boolButtonEnable != false) oButton.disabled = false;
}

function DisabledButton(counter) {
	var oButton = document.getElementById("submitButton");
	if (counter > 0) {
		oButton.value = "Подождите (" + counter-- + ")";
		window.setTimeout("DisabledButton(" + counter + ")", 1000);
	} else {
		oButton.disabled = false;
		oButton.value = "Отправить";
	}
}

function RefreshImage(CodeFieldFocus) {
	var oCode = document.getElementById("code");
	document.getElementById("image").src = "image.php?" + intImageCounter++;
	if (CodeFieldFocus == true) oCode.focus();
}

function TextLength() {
	var oText = document.getElementById("text");
	var oInfo = document.getElementById("info");
	oInfo.innerHTML = "Осталось символов: " + (intMaxTextLength - oText.value.length);

	if (oText.value.length > intMaxTextLength) {
		oText.value = oText.value.substring(0, intMaxTextLength);
		ShowAlert(oText, "Количество символов в тексте письма не может быть больше " + intMaxTextLength + "!");
	}
}

