// JavaScript Document

$(document).ready(function() {
	
	$('#kontakt #submit').click(function() {
		// Fade in the progress bar
		$('#kontakt #formProgress').hide();
		//$('#kontakt #formProgress').html('<img src="images/ajax-loader.gif" /> Sending&hellip;');
		//$('#kontakt #formProgress').fadeIn();
		
		// Disable the submit button
		$('#kontakt #submit').attr("disabled", "disabled");
		
		// Clear and hide any error messages
		$('#kontakt .formError').html('');
		
		// Set temaprary variables for the script
		var isFocus=0;
		var isError=0;
		
		// Get the data from the form
		var name=$('#kontakt #name').val();
		var email=$('#kontakt #email').val();
		var message=$('#kontakt #message').val();
		
		// Validate the data
		if((name=='') || (name=='Imię i nazwisko / firma')) {
			$('#kontakt #errorName').html('Nie bądź anonimowy...');
			//$('#kontakt #name').focus();
			isFocus=1;
			isError=1;
		}
		if((email=='') || (email=='Twój e-mail')) {
			$('#kontakt #errorEmail').html('E-mail potrzebny do odpowiedzi na Twój list.');
			/*if(isFocus==0) {
				$('#kontakt #email').focus();
				isFocus=1;
			}*/
			isError=1;
		} else {
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if(reg.test(email)==false) {
				$('#kontakt #errorEmail').html('Sprawdź proszę poprawność adresu.');
				/*if(isFocus==0) {
					$('#kontakt #email').focus();
					isFocus=1;
				}*/
				isError=1;
			}
		}
		if(message=='') {
			$('#kontakt #errorMessage').html('Nie chcesz nic napisać?');
			/*if(isFocus==0) {
				$('#kontakt #message').focus();
				isFocus=1;
			}*/
			isError=1;
		}
		
		// Terminate the script if an error is found
		if(isError==1) {
			$('#kontakt #formProgress').html('');
			$('#kontakt #formProgress').hide();
			
			// Activate the submit button
			//$('#kontakt #submit').attr("disabled", "");
			
			return false;
		}
		
		$.ajaxSetup ({
			cache: false
		});
		
		var dataString = 'name='+ name + '&email=' + email + '&subject=new message from mediger.net&message=' + message;  
		$.ajax({
			type: "POST",
			url: "php/submit-form-ajax.php",
			data: dataString,
			success: function(msg) {
				
				// Check to see if the mail was successfully sent
				if(msg=='Mail sent') {
					$("#form").fadeOut("fast");
					$("#thanks").fadeIn("fast");
				} else {
					$('#kontakt #formProgress').html('');
					alert('Problem z wysłaniem, spróbuj proszę ponownie.');
				}
				
				// Activate the submit button
				//$('#kontakt #submit').attr("disabled", "");
				$('#kontakt #submit').fadeOut("fast");
			},
			error: function(ob,errStr) {
				$('#kontakt #formProgress').html('');
				alert('Problem z wysłaniem, spróbuj proszę ponownie.');
				
				// Activate the submit button
				//$('#kontakt #submit').attr("disabled", "");
			}
		});
		
		return false;
	});
});