$(function()
{
	$('a#offres').click(showOffres);
	$('img#close').click(hideOffres);
	
	
	
	$('a.rad').click(setRad);
	$('a.check').click(checkBox);
	
	
	
	
	//Pour récupérer si pro ou part : $('a.sel').text();
	
	//if submit button is clicked
	    $('#submit').click(function () {       

	        //Get the data from all the fields
	        var name = $('input[name=nom]');
	        var prenom = $('input[name=prenom]');
	 		var email = $('input[name=email]');
	 		var telephone = $('input[name=telephone]');
	        var comment = $('textarea[name=message]');
			var categorie = $('a.sel').text();
			

			if ($('a.check').hasClass('checked') && categorie == 'Pro'){
				var covering = true;
			} else covering = false;
	        //Simple validation to make sure user entered something
	        //If error found, add hightlight class to the text field
	        if (name.val()=='') {
	            name.addClass('hightlight');
	            return false;
	        } else name.removeClass('hightlight');

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

	        if (comment.val()=='') {
	            comment.addClass('hightlight');
	            return false;
	        } else comment.removeClass('hightlight');

	        //organize the data properly
	        var data = 'nom=' + name.val() + '&prenom=' + prenom.val()+ '&email=' + email.val() + '&telephone=' + telephone.val() + '&categorie=' + categorie + '&covering=' + covering + '&message='  + encodeURIComponent(comment.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.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) {

					} else alert('Votre message a bien été envoyé.');             
	            }      
	        });

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

	
	function setRad()
	{
		e = $(this);
		$('a.rad').removeClass('sel');
		if(!e.hasClass('sel'))
		{
			e.addClass('sel');
		}
		if(e.text() == 'Pro')
		{
			$('a.check').fadeIn();
		}
		else
		{
			$('a.check').fadeOut();
		}
		
		return false;
	}
	
	function checkBox()
	{
		e = $(this);
		
		if(!e.hasClass('checked'))
		{
			e.addClass('checked');
		}
		else
		{
			e.removeClass('checked');
		}
		
		return false;
	}
	
	function showOffres()
	{		
		$('div.plan').fadeIn();
		
		return false;
	}
	
	function hideOffres()
	{		
		$('div.plan').fadeOut();
		
		return false;
	}
})
