// JavaScript Document

$("#submit").click(function(){
	$('.msgContact').html('');
	//
	var name 			= $('#name').val();
	var email 			= $('#email').val();
	var organization 	= $('#organization').val();
	var country	 		= $('#country').val();
	var city 			= $('#city').val();
	var message 		= $('#message').val();
	var humanInput 		= $('#humanInput').val();
	
	var fuente 			= location.href;//$('#fuente').val();
	
	if( name != '' && email != '' && message != '' && humanInput != '' && name != 'Name *' && email != 'Email *' && message != 'Message *'){
		
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email)){
		  		   
		   $.ajax({
				   type: "POST",
				   url: "contacto-ajax-home.php",
				   data: "name=pp"+name+"&email="+email+"&country="+country+"&message="+message+"&city="+city+"&organization="+organization+"&humanInput="+humanInput+"&fuente="+fuente,
				   dataType: "json",
				   beforeSend: function(objeto){
						$('.msgContact').html('Sending...');
					},
				   error: function(objeto, quepaso, otroobj){
						$('.msgContact').html('An error occurred. Try again.');
				    },
				   success: function(data){
					 
					 if(data.err){
							$('.msgContact').html(data.msg);
					   }else{
							$('#name').val('');
							$('#email').val('');
							$('#organization').val('');
							$('#country').val('');
							$('#city').val('');
							$('#message').val('');
							$('#humanInput').val('');
							//
							$('.msgContact').html(data.msg);
					   }
					 					 
				   }
				 });
		   
		}else{
			$('.msgContact').html('Please enter a valid email.');
		}
		  
	  }else{
		  if(humanInput == ''){
			  $('.msgContact').html('Please verify the human field');
		  }else{
		  	$('.msgContact').html('Name, Email & Message are required. Please verify.');
		  }
	  }
	  //
	  $('.msgContact').fadeIn();
	  setTimeout(function(){
		   $('.msgContact').fadeOut();
	  }, 3000);
      //return false;
});
