$(function(){	jQuery.fn.exists = function(){ return jQuery(this).length > 0; }
	jQuery.fn.isValidEmail = function(){
		return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]*\.)+[a-z]{2,6}$/i).test(jQuery(this).val());
	}

	// last item
	$('#footer .partners img:last-child, .sidebar_menu li:last-child, .pub_list div:last-child, .team_list div:last-child, #details .contact div:last-child, .services_menu li:last-child').addClass('last');

	$(".full_link").fancybox({		'titlePosition' : 'inside',
		'transitionIn'  : 'none',
		'transitionOut' : 'none'
	});

	$(".link_question").fancybox({
		'titlePosition' : 'inside',
		'transitionIn'  : 'none',
		'transitionOut' : 'none'
	});
});


$(document).ready(function(){	$("#question_form").bind("submit", function() {
		var isError = false;

		if ($("#question_name").val().length < 2 || $("#question_contact").val().length < 6 || $("#question_text").val().length < 10) {
			isError = true; $("#err_question_fields").show();
		} else {
			$("#err_question_fields").hide();
		}

		/*if ($("#question_email").val().length < 1 || !$("#question_email").isValidEmail()) {
			isError = true; $("#err_question_email").show();
		} else {
			$("#err_question_email").hide();
		}*/
		$("#err_question_email").hide();

		if (isError) {
			$("#err_question").show();
			$.fancybox.resize();
			return false;
		} else {			$("#err_question").hide();
			$.fancybox.resize();
		}

		$.fancybox.showActivity();

		$.ajax({
			type	: "POST",
			cache	: false,
			url		: "/ajax/question/",
			data	: $(this).serializeArray(),
			success: function(data) {
				$.fancybox(data, {
				//	'padding' : 0,
					'showCloseButton' : false,
				//	'hideOnContentClick' : true
				});
			}
		});

		return false;
	});

	$("#feedback_form").bind("submit", function() {
		var isError = false;

		if ($("#feedback_name").val().length < 2 || $("#feedback_phone").val().length < 7 || $("#feedback_text").val().length < 10) {
			isError = true; $("#err_feedback_fields").show();
		} else {
			$("#err_feedback_fields").hide();
		}

		if ($("#feedback_email").val().length < 1 || !$("#feedback_email").isValidEmail()) {
			isError = true; $("#err_feedback_email").show();
		} else {
			$("#err_feedback_email").hide();
		}

		if (isError) {
			$("#msg_feedback").show();
			return false;
		} else {
			$("#msg_feedback").hide();
		}

		$.ajax({
			type	: "POST",
			cache	: false,
			url		: "/ajax/feedback/",
			data	: $(this).serializeArray(),
			success	: function(data) {
				$("#msg_feedback div").hide();

				if (data == 'OK') {					$("#feedback_form div").hide();
					$("#msg_feedback .success").show();
				} else {
					$("#msg_feedback .server").html(data);
					$("#msg_feedback .server").show();
				}

				$("#msg_feedback").show();
			}
		});

		return false;
	});
});


function selectRegion(region) {
	if (typeof(region) != "undefined") {
		document.getElementById('region').value = region;
	}

	var region = document.getElementById('region').value;

	$.ajax({
		type	: "POST",
		cache	: false,
		url		: "/ajax/office/",
		data	: '&region=' + region,
		success	: function(data) {
			$('#region_info').html(data);

			var to = $('#region_info h5').html();
			$('.c_form_to span').html(to);

			var office_id = $('#cur_office_id').html();
			$('#office_id').val(office_id);
		}
	});
}
