﻿$(document).ready(function(){
	var box_name;
	$('a.infobox').click(function(){
		box_name = $(this).attr('data-type');
		$('div.'+box_name).show('fast', function(){
			$('#infobox').fadeIn('slow');
		});
		return false;
	});
	
	$('a.infobox-close').click(function(){
		$('#infobox').hide('slow', function(){
			$('div.'+box_name).hide();
		});
		return false;
	});
	
	$('#submit').click(function(){
		sendMail();
		return false;
	});
});

function sendMail(){
	var validate = true;
	
	if($('#name').val() == ''){
		$('#name').css('background', '#f77b7b');
		validate = false;
	}else{
		$('#name').removeAttr('style');
	}
	
	if($('#email').val() == ''){
		$('#email').css('background', '#f77b7b');
		validate = false;
	}else{
		$('#email').removeAttr('style');
	}
	
	if($('#title').val() == ''){
		$('#title').css('background', '#f77b7b');
		validate = false;
	}else{
		$('#title').removeAttr('style');
	}
	
	if($('#content').val() == ''){
		$('#content').css('background', '#f77b7b');
		validate = false;
	}else{
		$('#content').removeAttr('style');
	}
	
	if(validate){
		$.ajax({
			type: "POST",
			url: 'class/mail.php',
			data: {
				name: $('#name').val(), 
				email: $('#email').val(), 
				title: $('#title').val(), 
				content: $('#content').val(),
				recaptcha_response_field: $('#recaptcha_response_field').val(),
				recaptcha_challenge_field: $('#recaptcha_challenge_field').val()				
			},
			success: function(e){
				alert(e);
				if(e != 'incorrect captcha'){
					$('#infobox').hide('slow', function(){					
						$('div.contact').hide();				
					});
				};
			}
		});
	}
}
