// Pie Chart init
window.onload = function(){
	//If you want you can set your chart colors here. If no colors are set random colors will be used
	CanvasChart.setColors(['#fffce9','#fde663','#fff2a5', '#fff8cb', '#ffd800']);
	
	//Just pass an array of data to the plotData function
	CanvasChart.plotData([15,20,25,15,35]);
}

$(function (){

	$(window).scroll(function () {
		var offset = $(window).scrollTop();
		if(offset <= 323) {
			$('.home').addClass('active');
			$('.contact').removeClass('active');
			$('.work').removeClass('active');
			$('.about').removeClass('active');
		}
		if(offset >= 323 && offset <= 770) {
			$('.home').removeClass('active');
			$('.contact').removeClass('active');
			$('.work').removeClass('active');
			$('.about').addClass('active');
		}
		if(offset >=770 && offset <= 1600) {
			$('.home').removeClass('active');
			$('.about').removeClass('active');
			$('.contact').removeClass('active');
			$('.work').addClass('active');
		}
		if(offset >= 1600) {
			$('.home').removeClass('active');
			$('.about').removeClass('active');
			$('.work').removeClass('active');
			$('.contact').addClass('active');
		}
	});
	
	// Contact form validation
	required = ["yourName", "email", "message"];
	
	email = $("#email");
	errornotice = $("#error");
	successnotice = $("#response");
	
	emptyerror = "Please fill out this field.";
	emailerror = "Please enter a valid e-mail.";

	$("#contactForm #submit").click(function(){	
		for (i=0;i<required.length;i++) {
			var input = $('#'+required[i]);
			if ((input.val() == "") || (input.val() == emptyerror)) {
				input.addClass("needsfilled");
				input.val(emptyerror);
				errornotice.fadeIn(750);
			} else {
				input.removeClass("needsfilled");
			}
		}
		
		if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
			email.addClass("needsfilled");
			email.val(emailerror);
		}

		if ($(":input").hasClass("needsfilled")) {
			return false;
		} else {
			errornotice.hide();
			sendMessage();
			successnotice.fadeIn(750);
			$(":input").attr("disabled", true);
			$(":input").addClass("disabled");
		}
	});
	
	$(":input").focus(function(){		
	   if ($(this).hasClass("needsfilled")) {
			$(this).val("");
			$(this).removeClass("needsfilled");
	   }
	});
	
	function sendMessage() {
		$.ajax({
			type: "POST",
			url: "mail.php",
			data: "yourName=" + document.getElementById("yourName").value + 
			"&company=" + document.getElementById("company").value +
			"&email=" + document.getElementById("email").value +
			"&phone=" + document.getElementById("phone").value +
			"&message=" + document.getElementById("message").value,
			success: function(html){
				$("#response").html(html);
			}
		});
	}
});


