$(document).ready(function() {
						   
	sendMessage = function() {
	
		var name = $('#contact-n').val();
		var email = $('#contact-e').val();
		var comments = $('#contact-c').val();
		$('#contact-result').html("<img src='/images/loading.gif' alt=''/>");
		
		if (name == ""){
			$('#contact-result').html("<span class='error'>Please provide a name. All fields are required.</span>");
		} 
		
		else if (email == ""){
	  			$('#contact-result').html("<span class='error'>Please enter an e-mail.</span>");
	  		}
			
		else if ((jQuery.inArray("@", email) == -1) || (jQuery.inArray(".", email) == -1)) {
			$('#contact-result').html("<span class='error'>Please enter a valid e-mail.</span>");
		}
		
		else if (comments == "") {
			$('#contact-result').html("<span class='error'>Please provide a comment. All fields are required.</span>");
		}
		//not a bot
		else if ($('#contact-b').val() == "") {
			
			$.ajax({
			   type: "POST",
			   url: "/js/response.php?name="+name+"&email="+email+"&comments="+comments,
			   
			   success: function(msg){ 
					$('#contact-result').html(msg);
					/*
					if (msg.indexOf("Thank you for your message") != -1){
						//clear all the fields
						$('#contact-n').val("");
						$('#contact-e').val("");
						$('#contact-c').val("");
				   }
				   */
			   },
			   error: function(){
					$('#contact-result').html("<span class='error'>Error!</span>");
				   }
			});
			/*
			var url = "/js/response.php?name="+name+"&email="+email+"&comments="+comments;
			var obj = {};
			$.post(url, obj, 
				   function(data) 
				   {
					   $('#contact-result').html(data);
					   if (data == "Thank you for your message!") {
						//clear all the fields
						$('#contact-n').val("");
						$('#contact-e').val("");
						$('#contact-c').val("");
				   }
				   }
			);*/
		} 
		else {
			$('#contact-result').html("<span class='error'>I think you're a bot!</span>");
		}
		
	}
	
	
	
});

function updateRating(id, value) {
	var url = '/js/vote.php?id='+id+'&rating='+value;
	var obj = {};
	
	$.post(url, obj, function(data) {
		var rating = data.split("=");
		
		$("#result").html((rating[0]/rating[1]).toFixed(2) + "<span style='font-size: 0.7em;'> (" + rating[1] + " votes)</span>");
		$("#numOfVotes").html(rating[1]);
		$("#thank_you_for_rating").html("Thank you!");
		$("#your_rating").html("Your rating: " +  value);
		$("#your_rating").css('display', 'inline');	 
		$(".vote-btn").each(function() { $(this).attr('checked',false);});
		$("#rate_"+value).attr('checked',true);
		
							 }
    );
}
