$(document).ready(function(){
 
	$("form#question").submit(function() {
        var selectedval  = $('#question option:selected').val(),
        response_text   = $('#ansv');

       response_text.html('<p>Обработка...</p>').show();
		$.post('/phpmailer/statistic.php', {selectedval: selectedval}, function(data){
			response_text.html(data);
		});

		return false;
		
	});
 
    $("#other_rad").click(function(){
        /*alert($('input:radio[name=questioning]:checked').val());*/
        $(this).css('display','none');
        $("#other_text").css('display','');
    })
    
	$("#questioning").submit(function() {
        /**/var checked_item  = $('input:radio[name=questioning]:checked').val();
        if($('#other_text').val()==''){
            $("#other_text").focus();
        }
        if($('#other_text').val()){
            checked_item = $('#other_text').val(); 
        }
        return false;
    })
 
	// Contact form
	$("form#contact_form").submit(function() {
		
		//Setup any needed variables
		var input_name  = $('#name').val(),
		input_phone     = $('#telephone').val(),
		input_message   = $('#msg').val(),
		response_text   = $('#main_content #response_cf');
        
      if (input_name == "") {
      $("span#name_error").show();
      $("input#name").focus();
      return false;
    }
            

	  if (input_phone == "") {
      $("span#telephone_error").show();
      $("input#telephone").focus();
      return false;
    }
	
	  if (input_message == "" || input_message == "Запишите здесь свое сообщение") {
	  $("span#msg_error").show();
	  $("textarea#msg").focus();
	  return false;
    } 
        
		//Hide any previous response text
	//	response_text.hide();

		//Change response text to 'loading...'
	//	response_text.html('<p>Loading...</p>').show();

		//Make AJAX request
		$.post('/phpmailer/sendmail.php', {name: input_name, phone: input_phone, message: input_message}, function(data){
			response_text.html(data);
            $("#contact_form").empty();
            //$("#contact_form").css('background-color','#DBDBDB');
            $("#contact_form").css('text-align','center');
            $("#contact_form").html("<span style=\"color:black;font-size:large;\">"+data+"</span>");
		});

		//Cancel default action
		return false;
		
	});
    
    	// Contact form
	$("form#response").submit(function() {
		
		//Setup any needed variables
		var input_message   = $('#msg').val(),
		response_text   = $('#main_content #response_cf');
        	
	  if (input_message == "") {
	  $("span#msg_error").show();
	  $("textarea#msg").focus();
	  return false;
    } 

		//Make AJAX request
		$.post('/phpmailer/sendresponse.php', {message: input_message}, function(data){
			response_text.html(data);
            $("#response").empty();
            //$("#contact_form").css('background-color','#DBDBDB');
            $("#response").css('text-align','center');
            $("#response").html("<span style=\"color:black;font-size:large;\">"+data+"</span>");
		});

		//Cancel default action
		return false;
		
	});
	    
    	// Contact form
	$("form.questions").submit(function() {
		
		//Setup any needed variables
		var input_message   = $('#msg').val(),
		    input_email   = $('#email').val(),
		response_text   = $('#main_content #response_cf');
        	
	  if (input_message == "") {
	  $("span#msg_error").show();
	  $("textarea#msg").focus();
	  return false;
    }
    
	  if (input_email == "") {
      $("span#email_error").show();
      $("input#email").focus();
      return false;
    }
	
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	if(!emailReg.test(input_email)) {
	$("span#email_error2").show();
    $("input#email").focus();
      return false;
	} 

		//Make AJAX request
		$.post('/phpmailer/sendquestion.php', {message: input_message, email: input_email}, function(data){
			response_text.html(data);/*
            $("#questions").empty();
            $("#questions").css('text-align','center');
            $("#questions").html("<span style=\"color:black;font-size:large;\">"+data+"</span>");*/
		});

		//Cancel default action
		return false;
		
	});
	
	// Contact form
	$("form#contact_request_form").submit(function() {
		
		//Setup any needed variables
		var input_name  = $('#name').val(),
		input_phone     = $('#telephone').val(),
		input_model     = $('#model').val(),
		input_message   = $('#msg').val(),
		response_text   = $('#main_content #response_cf');
        
      if (input_name == "") {
      $("span#name_error").show();
      $("input#name").focus();
      return false;
    }
            

	  if (input_phone == "") {
      $("span#telephone_error").show();
      $("input#telephone").focus();
      return false;
    }
	  if (input_model == "") {
      $("span#model_error").show();
      $("input#model").focus();
      return false;
    }
	
	  if (input_message == "" || input_message == "Запишите здесь свое сообщение") {
	  $("span#msg_error").show();
	  $("textarea#msg").focus();
	  return false;
    } 
        
		//Hide any previous response text
	//	response_text.hide();

		//Change response text to 'loading...'
	//	response_text.html('<p>Loading...</p>').show();

		//Make AJAX request
		$.post('/phpmailer/sendcontresp.php', {name: input_name, phone: input_phone,model:input_model, message: input_message}, function(data){
			response_text.html(data);
            $("#contact_request_form").empty();
            //$("#contact_form").css('background-color','#DBDBDB');
            $("#contact_request_form").css('text-align','center');
            $("#contact_request_form").html("<span style=\"color:black;font-size:large;\">"+data+"</span>");
		});

		//Cancel default action
		return false;
		
	});
	
});
