$(function() {

	// load the modal window
	$('a.modal').click(function(){

		// scroll to top
		$('html, body').animate({scrollTop:0}, 'fast');

		// before showing the modal window, reset the form incase of previous use.
		$('.success, .errors, .bekle, #bekle').hide();
		$('form#contactForm').show();
		
		// Reset all the default values in the form fields
		$('#name').val('');
		$('#email').val('');
		$('#tel').val('');
		$('#yas').val('');
		$('#ilce').val('');
		$('#meslek').val('');

		//show the mask and contact divs
		$('#mask').show().fadeTo('', 0.7);
		$('div#contact').fadeIn();

		// stop the modal link from doing its default action
		return false;
	});

	// close the modal window is close div or mask div are clicked.
	$('div#close, div#mask').click(function() {
		$('div#contact, div#mask').stop().fadeOut('slow');

	});

	$('#contactForm input').focus(function() {
		$(this).val('');
	});
	
	$('#contactForm textarea').focus(function() {
        $(this).val('');
    });

	// when the Submit button is clicked...
	$('input#submit').click(function() {
	$('.errors').hide().remove();
	$('#bekle img').remove();
		//Inputed Strings
		var name = $('#name').val();
		var	email = $('#email').val();
		var	tel = $('#tel').val();
		var	yas = $('#yas').val();
		var	ilce = $('#ilce').val();
		var	meslek = $('#meslek').val();

		//Error Count
		var error_count = 0;
		
		//Regex Strings
		var name_regex = /^[a-z0-9_-]{3,16}$/;
		// email_regex = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
		//var	email_regex =/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		//var	email_regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; 
		var email_regex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		//
		/*var paraTag = jQuery('input#submit').parent('p');
    jQuery(paraTag).children('input').remove();
    jQuery(paraTag).append('<input type="submit" name="submit" id="submit" value="Gönder"/>');*/
			//Test Username
			/*if(!username_regex.test(username)) {
				$('#contact_header').after('<p class=errors>Geçersiz Ad Soyad!</p>');
				error_count += 1;
			}*/
			//Test Email
			
			/*if(email_regex.test(email) == false) {
				$('#contact_header').after('<p class=errors>Geçersiz email!</p>');
				error_count += 1;
			}*/
			//Blank ad?
			if(name == '') {
				$('#contact_header').after('<p class=errors>Lütfen Ad Soyad yazınız!</p>');
				error_count += 1;
			}
			if(email_regex.test(email) == false) {
				$('#contact_header').after('<p class=errors>Geçersiz email!</p>');
				error_count += 1;
			}
			//Blank email?
			/*if(email == '') {
				$('#contact_header').after('<p class=errors>Lütfen E-Mailinizi yazınız!</p>');
				error_count += 1;
			}*/
			//Blank tel?
			if(tel == '') {
				$('#contact_header').after('<p class=errors>Lütfen telefon yazınız!</p>');
				error_count += 1;
			}
			//Blank yas?
			if(yas == '') {
				$('#contact_header').after('<p class=errors>Lütfen yaşınızı yazınız!</p>');
				error_count += 1;
			}
			//Blank ilce?
			if(ilce == '') {
				$('#contact_header').after('<p class=errors>Lütfen ilçenizi yazınız!</p>');
				error_count += 1;
			}
			//Blank meslek?
			if(meslek == '') {
				$('#contact_header').after('<p class=errors>Lütfen meslek yazınız!</p>');
				error_count += 1;
			}
			
			
			
			//No Errors?
			if(error_count === 0) {
		$('html, body').animate({scrollTop:0}, 'fast');
		$('#bekle').slideDown('slow');
		$('#bekle').append('<img src="css/img/ajax-loader.gif" class="loaderIcon" alt="Loading..." />');
		$('form#contactForm').fadeOut('slow');
				$.ajax({
					type: "POST",
					url: "./send.php",
					data: "name="+name+"&email="+email+"&tel="+tel+"&yas="+yas+"&meslek="+meslek+"&ilce="+ilce,
					error: function() {
						$('.errors').hide();
						$('#bekle img.loaderIcon').fadeOut('fast');
						$('#bekle').hide();
						$('#sendError').slideDown('slow');
					},
					success: function () {
						$('.errors').hide();
						$('#bekle').hide();
						$('#bekle img.loaderIcon').fadeOut(2000);
						$('.success').slideDown('slow');
						$('form#contactForm').fadeOut('slow');
						$('div#contact, div#mask').stop().fadeOut(6000);
					}				
				});	
			}
			
			else {
                $('.errors').show();
            }
			
		return false;
	});
	
});
