



function getResult( mail_result )
{	
    
    if( !checkNullQuestions() ) return; //выход если не отмечены все вопросы
    
    if( mail_result == 'mail')
    {
        if( !checkTestForm() ) return; //выход если не заполненны обязательные поля
    }
    
    $('label').css({'background-color':'#fff','color':'#000'}); //восстановление стилей ответов при повторной отправке
    
	var formOption = {
		success: function getServerQuestion( anser_ar )
				{
					$(function(){
						$('.result').html( anser_ar['text'] );
						$('.result').css('backgroundImage','none');	
					});
                                        
                                        if( mail_result == 'mail' )
                                            showPopUpMesage( anser_ar['popup_text'], 110 ); 
                                        
                                        if( anser_ar['question_err_ar'].length > 0 )
                                        {
                                            cnt_err = anser_ar['question_err_ar'].length;
                                            
                                            for(i=0; i<cnt_err; i++)
                                            {
                                                $('label[for=id_'+anser_ar['question_err_ar'][i]+']').css({'background-color':'#ff0000','color':'#fff'});
                                            }
                                        }
				},
                dataType:  'json'                
	};
	
	$(function(){
		$('#result_or_mail').attr('value', mail_result );
		$('.result').html('&nbsp;');
		$('.result').css('backgroundImage','url(/images/ajax-loader.gif)');
		$('#test_form').ajaxSubmit(formOption);
	});
}



function checkTestForm()
{
    tel         = $('#tel').attr('value');
    fio         = $('#fio').attr('value');
//    email       = $('#email').attr('value');
//    birthday    = $('#birthday').attr('value');
    
    if( tel.length < 7 || fio.length < 4 )
    {
        showPopUpMesage( '<h2><font color="#f00000">Заполните пожалуйста поля «Имя» и «Телефон»</font></h2>', 150 ); 
        $("html:not(:animated), body:not(:animated)").animate({scrollTop: $('h1').position().top}, 300);
        return false;
    }
    else
        return true;
    
}


function checkNullQuestions()
{
    $('.area-holder strong').css({'background':'none'});
    question_ar = $('.area-holder');
    lock = false; //для блокировки отправки данных
    questionStr = ''; //номера не отмеченных вопросов 
    cnt_questions = question_ar.length;
    for(i=0; i<cnt_questions; i++ )
    {
        if( $('input:checked', question_ar[i]).attr('value') == undefined ) 
        {
            $('strong', question_ar[i]).css({'background':'#c4e7ff'});
            lock = true;
            questionStr += i+1+', ';
        }
    }
    
    if( !lock )
        return true;
    else
    {
        message = '<h2><font color="#f00000">Вы не отметили ни одного варианта ответа в следующих вопросах:</font></h2> <p style="font-size:14px;"><b>'+rtrim(questionStr)+'</b></p>';
        showPopUpMesage( message, 150 ); 
        return false;
    }
        
}

function showPopUpMesage( message, height )
{
    html_faq_form = '<div id="faq_send_main_block"><div id="faq_send_main_block_bg" onclick="closeFAQform()"></div><center><div id="faq_send_form_block"><a href="javascript:void(0)" id="faq_send_close" onclick="closeFAQform()"></a>'+message+'</div></center></div>';
    $('body').prepend(html_faq_form);
    popupCenter();
    
    if( height > 0 )
        $('#faq_send_form_block').css({'height':height+'px'});
}


// Убирает пробельные символы справа
function rtrim( str )
{
	var ptrn = /((\s*\S+)*),/;
	return str.replace(ptrn, "$1");
}

