$(document).ready(function() {
	
	$('.error').hide();
	
	var cs = $(this).find(':selected').val()-1;

	var currentTextareaLine	= new Array(
				'...sucks a big time!'+"\n"+'...is cool dude!'+"\n"+"\n"+'These are just examples :)',
				"...but I don't think that you got the skills for the job!"+"\n"+"...and you're just the right person to do it!",
				'...and do all the shitty work?'+"\n"+'...and create something magical?',
				"...are from the place where sun doesn't shine!"+"\n"+'...are the best in every way!',
				"...but can't say it 'cause that's not appropriate."+"\n"+'...can be something else. So write something else!'
	);
	
	$("textarea#comment").addClass('examples').val(currentTextareaLine[cs]);
	
	$('select').change(function(){
		cs = $(this).find(':selected').val()-1;
		
		$("textarea#comment").addClass('examples').val(currentTextareaLine[cs]);
		
	});
	
	$("textarea#comment").focus(function()
	{
		if ($("textarea#comment").hasClass('examples'))
		{
			$("textarea#comment").removeClass('examples').val('');
		}
	});
	
	$("textarea#comment").blur(function()
	{
		if ($("textarea#comment").val() == '')
		{
			$("textarea#comment").addClass('examples').val(currentTextareaLine[cs]);
		}
	});
	
	$(".button").click(function()
	{
		
		var phrase = $('#phrase :selected').text();
	
    	$('.error').hide();
  	  	var name = $("input#name").val();
	  	if (name == "")
  		{
        	$("#name_error").show();
        	$("input#name").focus();
        	//$('input#name').css('background-position', '0 -62px');
        	return false;
      	}
      	
      	if (name.length < 2)
      	{
      		$("#name_error").text("Your name can't be less than 2 characters? Or can it?").show();
        	$("input#name").focus();
        	//$('input#name').css('background-position', '0 -62px');
        	return false;
      	}
      	
      	var email = $("input#email").val();
  		if (email == "")
  		{
  			$('#email_error').text('Please fill the field with correct email address.').show();
        	$("input#email").focus();
        	//$('input#email').css('background-position', '0 -62px');
        	return false;
      	}
      	
      	if (!isValidEmailAddress(email))
      	{
      		$('#email_error').text('Check your email address.').show();
        	$("input#email").focus();
        	//$('input#email').css('background-position', '0 -62px');
        	return false;
      	}
  		var comment = $("textarea#comment").val();
  		
  		if (comment == "")
  		{
	        $("#comment_error").show();
	        $("textarea#comment").focus();
	        return false;
      	}
      	
      	if ($("textarea#comment").hasClass('examples'))
  		{
	        $("#comment_error").text("Please fill the box with your own comment.").show();
	        $("textarea#comment").focus();
	        return false;
      	}
      
      var dataString = 'name='+ name + '&email=' + email + '&comment=' + comment + '&phrase=' + phrase;
		//alert (dataString);return false;
		
		$.ajax({
      			type: "POST",
      			url: "inc/sendemail.php",
      			data: dataString,
      			success: function() {
      				$('#contactMe').html("<div id='message'></div>");
        $('#message').html("<h2 class='pinkFont'>Feedback sent!</h2><p>Thanks a lot! I'll be in touch!</p>")
        .hide()
        .fadeIn(1500);
       }
     });
    	return false;
	});
	
});

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);
}