jQuery(document).ready(function($){


$('.orderform_control_short').click(function(){
$(this).addClass('orderform_control_short_active');
$('.orderform_control_full').removeClass('orderform_control_full_active');
$('.orderform_short').css('display', 'block');
$('.orderform_sended').css('display', 'none');
$('.orderform_full').css('display', 'none');
});

$('.orderform_control_full').click(function(){
$(this).addClass('orderform_control_full_active');
$('.orderform_control_short').removeClass('orderform_control_short_active');
$('.orderform_short').css('display', 'none');
$('.orderform_sended').css('display', 'none');
$('.orderform_full').css('display', 'block');
});


$('.orderform_short_button').click(function(){

formnotready = 0;

if (!$('.orderform_short_message').val())
{
	formnotready = 1;
	count_message = 0;
	var intervalID_message = setInterval( function() {if (count_message == 0 || count_message == 2)
	{
	$('.orderform_short_message').css('background-color','#EEEEEE');
	$('.orderform_short_message').text('Необходимо заполнить поле');
	} else
	{$('.orderform_short_message').css('background-color','#FFFFFF');};
	if (count_message == 3) {
	clearInterval(intervalID_message);
	$('.orderform_short_message').val('');
	};
	count_message++;} , 250);
};

if (!$('.orderform_short_contacts').val())
{
	formnotready = 1;
	count_contacts = 0;
	var intervalID_contacts = setInterval( function() {if (count_contacts == 0 || count_contacts == 2)
	{$('.orderform_short_contacts').css('background-color','#EEEEEE');
	$('.orderform_short_contacts').text('Необходимо заполнить поле');
	} else
	{$('.orderform_short_contacts').css('background-color','#FFFFFF');};
	if (count_contacts == 3) {
	clearInterval(intervalID_contacts);
	$('.orderform_short_contacts').val('');
	};
	count_contacts++;} , 250);
};

var checkmailregexp = /.*\@.*\..{0,4}/i;
if ($('.orderform_short_email').val() && !checkmailregexp.test($('.orderform_short_email').val()))
{
	formnotready = 1;
	count_subscribe = 0;
	var intervalID_subscribe = setInterval( function() {if (count_subscribe == 0 || count_subscribe == 2)
	{$('.orderform_short_email').css('background-color','#EEEEEE');
	$('.orderform_short_email').text('Необходимо заполнить поле');
	} else
	{$('.orderform_short_email').css('background-color','#FFFFFF');};
	if (count_subscribe == 3) {clearInterval(intervalID_subscribe);
	$('.orderform_short_email').val('');
	};
	count_subscribe++;} , 250);
};

if (formnotready == 0)
{
return true;
} else
{
return false;	
};
});



$('.orderform_full_button').click(function(){

formnotready = 0;
if (!$('.orderform_full_stone').val())
{
	formnotready = 1;
	count_stone = 0;
	var intervalID_stone = setInterval( function() {if (count_stone == 0 || count_stone == 2)
	{$('.orderform_full_stone').css('background-color','#EEEEEE');
	$('.orderform_full_stone').text('Необходимо заполнить поле');
	} else
	{$('.orderform_full_stone').css('background-color','#FFFFFF');};
	if (count_stone == 3) {clearInterval(intervalID_stone);
	$('.orderform_full_stone').val('');
	};
	count_stone++;} , 250);
};

if (!$('.orderform_full_size').val())
{
	formnotready = 1;
	count_size = 0;
	var intervalID_size = setInterval( function() {if (count_size == 0 || count_size == 2)
	{$('.orderform_full_size').css('background-color','#EEEEEE');
	$('.orderform_full_size').text('Необходимо заполнить поле');
	} else
	{$('.orderform_full_size').css('background-color','#FFFFFF');};
	if (count_size == 3) {clearInterval(intervalID_size);
	$('.orderform_full_size').val('');};
	count_size++;} , 250);
};

if (!$('.orderform_full_count').val())
{
	formnotready = 1;
	count_count = 0;
	var intervalID_count = setInterval( function() {if (count_count == 0 || count_count == 2)
	{$('.orderform_full_count').css('background-color','#EEEEEE');
	$('.orderform_full_count').text('Необходимо заполнить поле');
	} else
	{$('.orderform_full_count').css('background-color','#FFFFFF');};
	if (count_count == 3) {clearInterval(intervalID_count);
	$('.orderform_full_count').val('');};
	count_count++;} , 250);
};

if (!$('.orderform_full_date').val())
{
	formnotready = 1;
	count_date = 0;
	var intervalID_date = setInterval( function() {if (count_date == 0 || count_date == 2)
	{$('.orderform_full_date').css('background-color','#EEEEEE');
	$('.orderform_full_date').text('Необходимо заполнить поле');
	} else
	{$('.orderform_full_date').css('background-color','#FFFFFF');};
	if (count_date == 3) {clearInterval(intervalID_date);
	$('.orderform_full_date').val('');};
	count_date++;} , 250);
};

if (!$('.orderform_full_contacts').val())
{
	formnotready = 1;
	count_contacts = 0;
	var intervalID_contacts = setInterval( function() {if (count_contacts == 0 || count_contacts == 2)
	{$('.orderform_full_contacts').css('background-color','#EEEEEE');
	$('.orderform_full_contacts').text('Необходимо заполнить поле');
	} else
	{$('.orderform_full_contacts').css('background-color','#FFFFFF');};
	if (count_contacts == 3) {clearInterval(intervalID_contacts);
	$('.orderform_full_contacts').val('');};
	count_contacts++;} , 250);
};

var checkmailregexp = /.*\@.*\..{0,4}/i;
if ($('.orderform_full_email').val() && !checkmailregexp.test($('.orderform_full_email').val()))
{
	formnotready = 1;
	count_subscribe = 0;
	var intervalID_subscribe = setInterval( function() {if (count_subscribe == 0 || count_subscribe == 2)
	{$('.orderform_full_email').css('background-color','#EEEEEE');
	$('.orderform_full_email').text('Необходимо заполнить поле');
	} else
	{$('.orderform_full_email').css('background-color','#FFFFFF');};
	if (count_subscribe == 3) {clearInterval(intervalID_subscribe);
	$('.orderform_full_email').val('');};
	count_subscribe++;} , 250);
};

if (formnotready == 0)
{
return true;
} else
{
return false;	
};
});


$('#orderform_full_form').ajaxForm(function(data) { 
$('.orderform_full').slideUp(1000,function(){
$('.orderform_sended').slideDown(1000);});
}); 

$('#orderform_short_form').ajaxForm(function(data) { 
$('.orderform_short').slideUp(1000,function(){
$('.orderform_sended').slideDown(1000);});
}); 

$('.orderform_help').toggle(function() {
  $(this).find('.orderform_help_data').fadeIn('slow');
}, function() {
  $(this).find('.orderform_help_data').fadeOut('slow');
});

function activate_expand_stone_block()
{
$('.stone_pick_block > .title').click(function(){
if ($(this).parent().find('.stones').css('display') == 'none')
{
	$(this).parent().find('.stones').slideDown(500);
} else
{
	$(this).parent().find('.stones').slideUp(500);
};
});
};

activate_expand_stone_block();

$('.orderform_full_stone_pick').click(function(){

	if ($('.orderform').find('.stone_pick_wrapper').length == 1){
	$('.stone_pick_wrapper').fadeIn('slow');
	} else
	{
	
		$.get("/ajax/production_list.php", function(data){
		
		$('.orderform').prepend(data);
		$.get('/ajax/production_autocomplete_list.php', function(data){
			data = data.split(';');
			$('.search_stone_by_name').autocomplete(data);
			$('.stone_pick_wrapper').fadeIn('slow');
				$('.stone_pick_close').click(function(){
					$('.stone_pick_wrapper').fadeOut('fast');
				});
			activate_stones_unit_click();

			$('#search_stone_id').ajaxForm(function(data) { 
			$('.stone_pick_block.default').remove();
			$('.stone_pick_block.found').remove();
			$('.s_p').append(data);
			$('.stone_pick').append(data);
			if ($('.search_stone_by_name').val() != '') {$('.search_stone_by_name_close').fadeIn(300);};
			if ($('.search_stone_by_article').val() != '') {$('.search_stone_by_article_close').fadeIn(300);};
			activate_expand_stone_block();
			activate_stones_unit_click();
			
				$('.search_stone_by_name_close').unbind('click');
				$('.search_stone_by_name_close').click(function(){
				$('.search_stone_by_name').val('');
				$('#search_stone_id').submit();
				$(this).fadeOut(300);
				});
				
				$('.search_stone_by_article_close').unbind('click');			
				$('.search_stone_by_article_close').click(function(){
				$('.search_stone_by_article').val('');
				$('#search_stone_id').submit();
				$(this).fadeOut(300);
				});
			
			}); 
			
			$('#search_stone_id').submit(function(){
			});

		});
		
		});
		
	};

});

function activate_stones_unit_click()
{
$('.stones_unit_name, .stones_unit_poroda').unbind('click');
$('.stones_unit_name, .stones_unit_poroda').click(function(){
$('#stone').val($(this).parent().find('.stones_unit_poroda').text()+' '+$(this).parent().find('.stones_unit_artikul').text()+' '+$(this).parent().find('.stones_unit_name').text());
$('.stone_pick_wrapper').fadeOut('fast');
});
};




if ($('#new_advert_form').length){
$('#new_advert_form').submit(function(){
	
	if ($(this).find('[name="f_Name"]').val() == '' || $(this).find('[name="f_Phone"]').val() == '' || $(this).find('[name="f_Message"]').val() == '')
	{
		$(this).find('.warn').text('Все поля обязательны для заполнения!');
	} else
	{
		$.post('/ajax/new_advert_post.php', 
		{
			name : $(this).find('[name="f_Name"]').val(),
			phone : $(this).find('[name="f_Phone"]').val(),
			message : $(this).find('[name="f_Message"]').val()		
		}, 
		function(){
			$('#new_advert_form').find('.warn').text('Ваше сообщение успешно отправлено!');
			$('#new_advert_form').find('[name="f_Name"]').val('');
			$('#new_advert_form').find('[name="f_Phone"]').val('');
			$('#new_advert_form').find('[name="f_Message"]').val('');
		});
	};
	
	return false;
});
};

});

