var DocumentURL = decodeURI(document.location.search);

// ajax captcha reload
function _myReloadCaptcha(captchaNumber) {
	if (typeof captchaNumber == "undefined") {
		FadeIn('blue');
		ShowLoader();
		$.ajax({
			type: "post",
			url: "/ajax/captcha.html",
			beforeSend: function() {
				$("#CaptchaImg").css("visibility", "hidden");
			},
			success: function(res) {
				FadeOut();
				HideLoader();
				var _newCaptchaId = res.getElementsByTagName('Number')[0].textContent;
				$("#CaptchaImg").attr("src", "/ajax/captcha.html?Picture="+_newCaptchaId);
				$("#Number").attr("value", _newCaptchaId);
				$("#Approval").attr("value", "");
				$("#CaptchaImg").css("visibility", "visible");
			},
			error: function() {
				FadeOut();
				HideLoader();
				alert(MO_str_ajax_noResponse);
			}
		});
	} else {
		FadeOut();
		HideLoader();
		var _newCaptchaId = captchaNumber;
		$("#CaptchaImg").attr("src", "/ajax/captcha.html?Picture="+_newCaptchaId);
		$("#Number").attr("value", _newCaptchaId);
		$("#Approval").attr("value", "");
		$("#CaptchaImg").css("visibility", "visible");
	}
}
//ENDF _myReloadCaptcha()



function FormClass(objForm) {
	var self = this;
	this.Form = objForm;
	this.Error = false;
	this.Select = [];

	$(this.Form).submit(function() { self.CheckForm(); return false; });
}
FormClass.prototype.CheckEmpty = function(input) {
	if (input.value == '') {
		this.Error = true;
		$(input).addClass('error');
		$("#fieldsFillError").show();
		this.Select.push(input);
	}
}
FormClass.prototype.CheckForm = function() {
	var self = this;
	this.Error = false;
	this.Select = [];

	$("#fieldsFillError").hide();

	// next fields should be filled: PersonName, Contacts, Comments, Approval (captcha)
	this.CheckEmpty(this.Form.PersonName);
//    this.CheckEmpty(this.Form.EMail);
	this.CheckEmpty(this.Form.Contacts);
	this.CheckEmpty(this.Form.Comments);
//    this.CheckEmpty(this.Form.Approval);


	if (this.Error == true) {
		this.Select[0].select();
		return false;
	} else {
		$.ajax({
			type: "post",
			url: "/feedback/formpost.html",
			data: $(this.Form).serialize(),
			dataType: "xml",
			beforeSend: function() {
				// $("#captchaError").hide();
				FadeIn('blue');
				ShowLoader();
			},
			success: function(xml) {
				//var _captchaResult    = xml.getElementsByTagName('CaptchaResult')[0].textContent;
				var _controllerResult = xml.getElementsByTagName('ControllerResult')[0].textContent;
				//var _captchaNumber    = xml.getElementsByTagName('CaptchaNumber')[0].textContent;
				if ($.browser.msie) {
					//_captchaResult    = xml.getElementsByTagName('CaptchaResult')[0].firstChild.nodeValue;
					_controllerResult = xml.getElementsByTagName('ControllerResult')[0].firstChild.nodeValue;
					//_captchaNumber    = xml.getElementsByTagName('CaptchaNumber')[0].firstChild.nodeValue;
				}
				//if (_captchaResult=="CaptchaOk") {
					if (_controllerResult == "OK") {
						FadeOut();
						HideLoader();
						//alert($("#reqResponse").attr("value"));
						$("#d_feedback_form").hide();
						$("#d_feedback_form_reply").show();
						// nulling the form
						$("#PersonName").attr("value", "");
						//$("#EMail"     ).attr("value", "");
						$("#Contacts"  ).attr("value", "");
						$("#Comments"  ).attr("value", "");
						//$("#Approval"  ).attr("value", "");
						// reload captcha
						// _myReloadCaptcha();
						// hide the form
						//$("#partnership_request_hidden").slideToggleAlt(1200);
					} else {
						FadeOut();
						HideLoader();
						alert($("#reqResponseErr").attr("value"));
					}
				/*} else { // _captchaResult == "CaptchaError";
					// captcha error, it should be refreshed
					$("#captchaError").show();
					$("#Approval").addClass("error");
					document.getElementById("Approval").select();
					_myReloadCaptcha(_captchaNumber);
				}*/
			},
			error: function() {
				FadeOut();
				HideLoader();
				alert(MO_str_ajax_noResponse);
			}
		});
	}
}




function SmileHandler (SmileType) {
//alert(SmileType)
	$("#t_smiles_form div.Smile").removeClass("clicked");
	$("#t_smiles_form div.Smile span.Smile_bg").removeClass("Smile_hover");
	$("#t_feedback_form td.selectedSmile div.Smile").hide();

	$("#t_smiles_form div.Smile_"+SmileType).addClass("clicked");
	$("#t_smiles_form div.Smile_"+SmileType+" span.Smile_bg").addClass("Smile_hover");
	$("#t_feedback_form td.selectedSmile div.Smile_"+SmileType).show();

	$("#SmileType").attr("value", SmileType);
}




$(document).ready(function () {
	CreateLoader();

	$('#TopNavigation').removeClass('invisible');
	$('#TopNavigation_cm').remove();
	$('#TopNavigation_cp').remove();

	//var elFeedbackContacts = document.getElementById("Contacts");
	//if (elFeedbackContacts != null) new FieldClass(elFeedbackContacts, { email: true });



	SmileHandler('normal');

	$("#t_smiles_form div.Smile").mouseover(function() {
		if (!($(this).hasClass("clicked"))) {
			$("span.Smile_bg", this).addClass("Smile_hover");
		}
	}).mouseleave(function() {
		if (!($(this).hasClass("clicked"))) {
			$("span.Smile_bg", this).removeClass("Smile_hover");
		}
	});


	var objForm = document.getElementById('f_feedback_form');
	new FormClass(objForm);

	// обработчик инпутов для формы
	var formPersonName = document.getElementById("PersonName");
//	var formPersonCity = document.getElementById("PersonCity");
//	var formPersonPhone = document.getElementById("PersonPhone");
//	var formEmail       = document.getElementById("EMail");
	var formContacts	= document.getElementById("Contacts");
	var formComments	= document.getElementById("Comments");
//	var formCaptchaValue = document.getElementById("Approval");

	if (formPersonName	!= null) new FieldClass(formPersonName, { latinAndCyrillic: true });
//	if (formPersonCity	!= null) new FieldClass(formPersonCity);
//	if (formPersonPhone	!= null) new FieldClass(formPersonPhone);
//	if (formEmail		!= null) new FieldClass(formEmail, { email: true });
	if (formContacts	!= null) new FieldClass(formContacts);
	if (formComments	!= null) new FieldClass(formComments);
//	if (formCaptchaValue != null) new FieldClass(formCaptchaValue);


	// refresh captcha when it's clicked
	// $("#CaptchaImg").click (function() { _myReloadCaptcha(); })

});




