function onContactFormSubmit () {
	try {
		var subject = $('inp_subject').value;
		if (!subject) {
			$('inp_subject').focus();
			showErrorForInput('inp_subject', 'Please, enter subject of your enquiry');
			return false;
		}

		var text = $('inp_message').value;
		if (!text) {
			$('inp_message').focus();
			showErrorForInput('inp_message', 'Please, enter message contents');
			return false;
		}

		var email = $('inp_email').value;
		if (!email) {
			$('inp_email').focus();
			showErrorForInput('inp_email', 'Please, enter your email address');
			return false;
		}
		if (!email_filter.test(email)) {
			$('inp_email').focus();
			showErrorForInput('inp_email', 'Please, enter a valid email address');
			return false;
		}

		var code = $('inp_code').value;
		if (!code) {
			$('inp_code').focus();
			showErrorForInput('inp_code', 'Please enter the code from<br /> the image', 0, ($('contact_us_page')? 3: 160));
			return false;
		}

		new Ajax.Request ('/contact_us.php', {
			method: 'post',
			onSuccess: onContactFormSuccess,
			onFailure: onContactFormError,
			parameters: $H({text: text, subject: subject, mail: email, code: code, browser: navigator.userAgent }).toQueryString()
		});
		$('ajax_form_loading').style.display = $('ajax_form_loading2').style.display = 'block';
	} catch (e) { };
	return false;
}

function onContactFormSuccess (ajaxRequest) {
	$('ajax_form_loading').style.display = $('ajax_form_loading2').style.display = 'none';
	var jsonObj = null;
	try {
		eval ('jsonObj = ' + ajaxRequest.responseText);
	} catch (e) { }
	if (!jsonObj) {
		alert ("There was error sending your message.\nPlease try again later!");
		return false;
	}
	if (jsonObj.error || jsonObj.errorid) {
		if (jsonObj.errorid == 2) {
			$('img_code').src = '/image_protection_' + (new Date()).getTime() + '.png';
			$('inp_code').focus();
			$('inp_code').value = '';
			showErrorForInput('inp_code', 'Wrong code! Please try again', 0, ($('contact_us_page')? 3: 160));
		}
	} else {
		alert ("Thank you for contacting us!\nWe will get back to you shortly!");
		$('inp_code').value = '';
		$('img_code').src = '/image_protection_' + (new Date()).getTime() + '.png';
	}
}
function onContactFormError () {
	alert ("There was a problem sending your enquiry\nPlease, check your internet connection and try again!");
	$('ajax_form_loading').style.display = $('ajax_form_loading2').style.display = 'none';
}

function onInputChange (inp) {
	if (!window.lastErrorInputShown) return;
	if (inp.id == window.lastErrorInputShown || inp.id == window.lastErrorInputShown.id)
		hideInputError();
}

function emptyContactForm () {
	$('inp_subject').value = $('inp_message').value = $('inp_email').value = '';
}

Event.observe(window, 'load', function () {
	if (!window.nofocus) {
		if (!$('inp_subject').value)
			$('inp_subject').focus();
		else
			$('inp_message').focus();
	}
});
