// JavaScript Document

$(document).ready(function(){
	//Hide all elements being shown to non-Javascript users
	$('.noscript').hide();
	
	//Add inline labels to form fields
	$("#name").DefaultValue("Name");
	$("#email").DefaultValue("Email");
	$("#message").DefaultValue("Message");
	
	//Toggles between open and closed contact form
	function contact() {
		if ($("#contactForm").is(":hidden")){
			$("#contactForm").slideDown("slow");
			$("#backgroundPopup").css({"opacity": "0.7"});
			$("#backgroundPopup").fadeIn("slow"); 
		}
		else{
			$("#contactForm").slideUp("slow");
			$("#backgroundPopup").fadeOut("slow");  
		}
	}
	 
	//When any object with class "contact" is clicked, toggle the contact form
	$(".contact").click(function(){contact()});
	
	
    //Handles form submission
  	$('.contactForm').submit( function(){
		//Validation	
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var email = document.getElementById('email');
		if (!filter.test(email.value)) {
			$('.email-missing').show();
		} else {$('.email-missing').hide();}
		if (document.cform.name.value == "") {
			$('.name-missing').show();
		} else {$('.name-missing').hide();}	
		if (document.cform.message.value == "") {
			$('.message-missing').show();
		} else {$('.message-missing').hide();}		
		if ((document.cform.name.value == "") || (!filter.test(email.value)) || (document.cform.message.value == "")){
			return false;
		} 
		
		if ((document.cform.name.value != "") && (filter.test(email.value)) && (document.cform.message.value != "")) {
			//Hide the form
			$('.contactForm').hide();
		
			//Display loading bar
			$('.loader').append($('.bar'));
			$('.bar').css({display:'block'});
		
			//Send the ajax request
			$.get('mail.php',{name:$('#name').val(),
							  email:$('#email').val(),
							  message:$('#message').val()},
		
			//Return the data
			function(data){
			  //Hide the loading graphic
			  $('.bar').css({display:'none'});
			  $('.loader').append(data);
			  
			  setTimeout('$("#backgroundPopup").fadeOut("slow"); $("#contactForm").slideUp("slow")', 2000);
	
	});
			
			//stay on the page
			return false;
		} 
  });
	//Required for IE6  
	$("#backgroundPopup").css({  
		"height": document.documentElement.clientHeight 
	});  
});
