$(document).ready(function() {  
      
    //if submit button is clicked  
    $('button#contact_submit').click(function () {          
          
        //Get the data from all the fields  
        var name = $('input[name=name]');
  		var nameLabel = $('label[id=name_label]')
        var email = $('input[name=email]');  
		var emailLabel = $('label[id=email_label]')
        var projectname = $('input[name=projectname]');
        var when = $('input[name=when]');
        var budget = $('input[name=budget]');  
        var message = $('textarea[name=message]');
		var messageLabel = $('label[id=message_label]')
        

     	//Simple validation to make sure user entered something  
        //If error found, add hightlight class to the text field  
        if (name.val()=='') {
            nameLabel.addClass('highlight');
            return false;  
        } else nameLabel.removeClass('highlight');  

        if (email.val()=='') {  
            emailLabel.addClass('highlight');  
            return false;  
        } else emailLabel.removeClass('highlight');  

        if (message.val()=='') {  
            messageLabel.addClass('highlight');  
            return false;  
        } else messageLabel.removeClass('highlight');

        //organize the data properly  
        var data = 'name=' + name.val() + '&email=' + email.val() + '&projectname='  
        + projectname.val() + '&when=' + when.val() + '&budget=' + budget.val() + '&message='  + encodeURIComponent(message.val());  

        //disabled all the text fields  
        $('.text').attr('disabled','true');

        //show the loading sign  
        // $('.loading').show();  

        //start the ajax
        $.ajax({
            //this is the php file that processes the data and send mail  
            url: "../contact_email_process.php",

            //GET method is used
            type: "GET",

            //pass the data
            data: data,

            //Do not cache the page  
            cache: false,  

            //success  
            success: function (html) {                
                //if process.php returned 1/true (send mail success)  
                if (html==1) {                    
                    //hide the form  

					var myTimer = {};

					$('#contact_body').fadeOut(100, function(){                   
						$('.done').fadeIn(100, function(){
							myTimer = $.timer(1000,function(){
								$('div#contact_popup').fadeOut(1000, function(){
									$('form#contact')[0].reset()
									$('.done').hide();
									$('#contact_body').show();
								});		
							});
						});
			        });  		

                //if process.php returned 0/false (send mail failed)  
                } else alert("Sorry, my contact form seems to be broken. I would ask you to contact me and let me know, but ... how?");                 
            }         
        });  

        //cancel the submit button default behaviours  
        return false;  
    });   
});   
