$(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#e4f0f7"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });
    $('textarea').focus(function(){
    $(this).css({backgroundColor:"#e4f0f7"});
  });
  $('textarea').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });
  
  
  function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
	}
  
function isValidPhone(emailAddress) {
	var pattern = new RegExp(/^[0-9-()]+$/);
	return pattern.test(emailAddress);
	}
  
  
  
  
  
  $(".button_kon").click(function() {

    $('.error').hide();
	

	
	
	
		
	var error = false;	
		
	  var nazwisko = $("input#nazwisko").val();
		if (nazwisko == "") {
      $("label#nazwisko_error").show();
      $("input#nazwisko").css({backgroundColor:"white"});
      error= true;
    }
		var imie = $("input#imie").val();
		if (imie == "") {
      $("label#imie_error").show();
      $("input#imie").css({backgroundColor:"white"});
      error= true;
    }
		var miejscowosc = $("input#miejscowosc").val();
		if (miejscowosc == "") {
      $("label#miejscowosc_error").show();
      $("input#miejscowosc").css({backgroundColor:"white"});
      error= true;
    }
    	var telefon = $("input#telefon").val();
    	if (telefon!="" && !isValidPhone(telefon)) {
    $("label#telefon_error").show();
      $("input#telefon").css({backgroundColor:"white"});
      $("label#telefon_error").text("To nie jest prawidłowy numer telefonu");
     error= true;
    }
    
    
    var telnip = $("input#telnip").val();
    if (telnip == "") {
      $("label#telnip_error").show();
      $("input#telnip").css({backgroundColor:"white"});
      $("label#telnip_error").text("To pole jest wymagane");
      error= true;
    } else	if (!isValidPhone(telnip)) {
    $("label#telnip_error").show();
      $("input#telnip").css({backgroundColor:"white"});
      $("label#telnip_error").text("To nie jest prawidłowy numer NIP / telefonu");
     error= true;
    }
    

    var email = $("input#email").val();
		if (email == "") {
      $("label#email_error").show();
      $("input#email").css({backgroundColor:"white"});
      $("label#email_error").text("To pole jest wymagane");
      error= true;
    } else if (!isValidEmailAddress(email)) {
    $("label#email_error").show();
      $("input#email").css({backgroundColor:"white"});
      $("label#email_error").text("To nie jest prawidłowy adres e-mail");
     error= true;
    }
    var pytanie = $("textarea#pytanie").val();
		if (pytanie == "") {
      $("label#pytanie_error").show();
      $("textarea#pytanie").css({backgroundColor:"white"});
      error= true;
    }
		if (error == true) return false;
		var dataString = 'nazwisko='+ nazwisko + '&imie=' + imie + '&miejscowosc=' + miejscowosc + '&telefon=' + telefon + '&telnip=' + telnip+ '&email=' + email+ '&pytanie=' + pytanie;
		//alert (dataString);return false;
		
		
		
		
        $('#contact_form').html("<div id='message'></div>");
        $('#message').html("<img id='checkmark' src='img/loading.gif' />")

       
      
		
		$.ajax({
      type: "POST",
      url: "includes/ajax/process_kontakt.php",
      data: dataString,
      success: function() {
        $('#contact_form').html("<div id='message'></div>");
        $('#message').html("<h2>Formularz kontaktowy został przesłany pomyślnie</h2>")
        .append("<p>Dziękujemy!</p>")
        .hide()
        .fadeIn(1500, function() {
          $('#message').append("<img id='checkmark' src='img/check.png' />");
        });
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#nazwisko").select().focus();
});

