function query(URI, callback) {

    var head = document.getElementsByTagName('HEAD')[0];
    if (!head)
        return;

    var scr = document.createElement('SCRIPT');
        scr.type = 'text/javascript';

    head.appendChild(scr);
    scr.src  = URI;

    scr.onload = callback;
    scr.onreadystatechange = function() {
        if ((this.readyState == 'loaded') || (this.readyState == 'complete')) {
            this.onreadystatechange = null;
            callback();
        }
    }
}
(function($) {
	// --------------------
	// Форма обратной связи
	var $feedback = $('#feedback'),
		$fb = $feedback.find('fieldset'),
		
		// Сворачивание
		$fb_button = $feedback.find('h2 a').click(function(event) {
			var $this = $(this);
			if ($fb.is(':hidden')) $feedback.addClass('opened');
			$fb.css('overflow', 'hidden');
			$fb.slideToggle(function() {
				if ($fb.is(':hidden')) $feedback.removeClass('opened');
			});
			event.preventDefault();
		});

		// Проверка заполненности полей
		// Если у поля установлено data('re'), проверяется соответствие этому регулярному выражению
		field_re = /^[\s ]|[\s ]$/,
		field_empty = function($el) {
			return !$el.attr('value').replace(field_re, '').length ||
				($el.attr('value') == $el.data('placeholder'));
		},
		field_filled = function($el) {
			return !field_empty($el) && (!$el.data('re') || $el.attr('value').match($el.data('re')));
		},

		// Поля формы с плейсхолдерами
		$feedback_fields = $feedback.find('textarea, input.text').add('#header input.text').each(function(index, item) {
			var $item = $(item);
			$item.data('placeholder', $item.attr('value'));
		}).focus(function(event) {
			var $this = $(this);
			if (field_empty($this)) $this.attr('value', '');
		}).blur(function(event) {
			var $this = $(this);
			if (field_empty($this)) $this.attr('value', $this.data('placeholder'));
		}).not('#header input.text'),

		// Выборочная очистка
		feedback_clear = function() {
			$feedback.find('textarea').attr('value', '');
		},

		// Отправка сообщения
		feedback_send = function() {
		       var data='?fio='+$('#fio').attr('value')+'&phone='+$('#phone').attr('value')+'&email='+$('#email').attr('value')+'&message='+$('#message').attr('value');
		       var URI = '/work/MOD/send.php';
			 query(  
                		URI + ((URI.indexOf('?') == -1) ? '?' + data.substr(1) : data),
	            function() {                                       
                    }                    
                );
			feedback_clear();
			alert('Ваше сообщение отправлено.');
		},

		// Сообщение об ошибке
		// @param array errors — имена полей с ошибками
		feedback_error = function(errors) {
			alert('Заполните все поля');
			$feedback.find('*[name=' + errors[0] + ']').focus().select();
		};

	// Шаблон для email
	$feedback.find('input[name=email]').data('re', new RegExp('^[a-z0-9_.-]+@([a-z0-9-]+\\.)+[a-z]{2,6}$', 'i'));

	// Проверка полей при отправке
	$feedback.submit(function(event) {
		var errors = [];
		$feedback_fields.each(function(index, item) {
			var $item = $(item);
			if (!field_filled($item)) errors.push($item.attr('name'));
		});
		errors.length ? feedback_error(errors) : feedback_send();
		return event.preventDefault() && false;
	});	
}(jQuery));
