$(document).ready(function(){
    $("#contact_msg").hide();
    
    $("#contact_button").click(function ()
    {
	if ($("#contact_slide").css('margin-top') === "-435px")
	{
	    $("#contact_slide").animate({"marginTop": "+=435px"}, "slow").animate({"marginTop": "-=5px"}, "fast");
	}
	else
	{
	    $("#contact_slide").animate({"marginTop": "-=435px"}, "slow").animate({"marginTop": "+=5px"}, "fast");
	}
    });
    
    //inputNumbers($("input[name='phone']"));
    //inputNumbers($("input[name='postcode']"));

    $("#sendform").click(function(){
	var errorMsg = '';
	
	var civ = $("select[name='civ']");
	var surname = $("input[name='surname']");
	var name = $("input[name='name']");
	var job = $("select[name='job']");
	var postcode = $("input[name='postcode']");
	var city = $("input[name='city']");
	var email = $("input[name='email']");
	var phone = $("input[name='phone']");
	var subject = $("select[name='subject']");
	var message = $("textarea[name='message']");
	
	var allFields = $([]).add(civ)
			     .add(surname)
			     .add(name)
			     .add(job)
			     .add(postcode)
			     .add(city)
			     .add(email)
			     .add(phone)
			     .add(subject)
			     .add(message);
			     
	allFields.removeClass("ui-state-error");
	
	var isInputData = true;
	
	isInputData = isInputData && checkEmptyInput(civ);
	isInputData = isInputData && checkEmptyInput(surname);
	isInputData = isInputData && checkEmptyInput(name);
	isInputData = isInputData && checkEmptyInput(job);
	isInputData = isInputData && checkEmptyInput(postcode);
	isInputData = isInputData && checkEmptyInput(city);
	isInputData = isInputData && checkEmptyInput(email);
	isInputData = isInputData && checkEmptyInput(phone);
	isInputData = isInputData && checkEmptyInput(subject);
	isInputData = isInputData && checkEmptyInput(message);
	
	if (!isInputData)
	{
	    errorMsg = "Vous devez renseigner tous les champs.";
	}
	else
	{
	    if (postcode.val().length < 5)
	    {
		errorMsg = "Merci d'indiquer un code postal correct.";
		postcode.addClass("ui-state-error");
	    }
	    
	    if (!postcode.val().match(/^[0-9]+$/))
	    {
		errorMsg = "Merci d'indiquer un code postal numérique.";
		postcode.addClass("ui-state-error");
	    }
	    
	    if (!email.val().match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i))
	    {
		errorMsg = "Merci d'indiquer une adresse email correcte.";
		email.addClass("ui-state-error");
	    }
	    
	    if (phone.val().length < 10)
	    {
		errorMsg = "Merci d'indiquer un num&eacute;ro de t&eacute;l&eacute;phone correct.";
		phone.addClass("ui-state-error");
	    }
	    
	    if (!phone.val().match(/^[0-9]+$/))
	    {
		errorMsg = "Merci d'indiquer un num&eacute;ro de t&eacute;l&eacute;phone numérique.";
		phone.addClass("ui-state-error");
	    }
	}
 
	if (errorMsg != '')
	{
	    $("#text_msg").html(errorMsg);
	    $("#action_msg").html('<a id="close" href="#">Fermer</a>');
	    $("#close").click(function() { $("#contact_msg").fadeOut('fast'); });
	    $("#contact_msg").fadeIn('fast');
	}
	else
	{
	    var datastr = 'civ=' + civ.val() +
			  '&surname=' + surname.val() +
			  '&name=' + name.val() +
			  '&job=' + job.val() +
			  '&postcode=' + postcode.val() +
			  '&city=' + city.val() +
			  '&email=' + email.val() +
			  '&phone=' + phone.val() +
			  '&subject=' + subject.val() +
			  '&message=' + message.val().replace(/\r\n|\r|\n/g, "<br />");
	    
	    $("#text_msg").html("Envoi en cours...");
	    $("#action_msg").html('<img src="/images/sending.gif" alt="sending..." />');
	    $("#contact_msg").fadeIn('fast');

	    setTimeout("send('" + datastr + "')", 2000);
	}
	
	return false;
    });
});

function checkEmptyInput(inputObj) {
    if (inputObj.val().length < 1 ||
	inputObj.val() === inputObj.attr('placeholder'))
    {
	inputObj.addClass("ui-state-error");
	return false;
    }
    else
    {
	return true;
    }
}

function send(datastr) {
    $.ajax({
	type: "POST",
	url: "/include/send.php",
	data: datastr,
	cache: false,
	success: function(html){
	    $("#text_msg").html(html);
	    $("#action_msg").html('<a id="close" href="#">Fermer</a>');
	    $("#close").click(function() {
		$("#contact_msg").fadeOut('fast');
		$("#contact_button").trigger('click');
		
		$("select[name='civ']").get(0).selectedIndex = 0;
		$("input[name='surname']").val('');
		$("input[name='name']").val('');
		$("select[name='job']").get(0).selectedIndex = 0;
		$("input[name='postcode']").val('');
		$("input[name='city']").val('');
		$("input[name='email']").val('');
		$("input[name='phone']").val('');
		$("select[name='subject']").get(0).selectedIndex = 0;
		$("textarea[name='message']").val('');
	    });
	}
    });
}

function inputNumbers(input)
{
    $(input).keypress(function (e) { 
	if (String.fromCharCode(e.keyCode).match(/[^0-9]/g))
	{
	    return false;
	}
	else
	{
	    return true;
	}
    });
}

$(function($){
var isPlaceholderSupported = function () {
    var o = document.createElement('input');
    return 'placeholder' in o; };
    
    if (!isPlaceholderSupported()) {
	var togglePlaceholderText = function (el) {
	    var color = '#999', v = $.trim(el.val()), p = el.attr('placeholder');
	    
	    if (v == '') {
		el.val(p);
		el.css({'color':color});
	    }
	    else if (v == p) {
		el.val('');
		el.css({'color':''});
	    }
	};
	
	$('input[placeholder]')
	    .each(function(){togglePlaceholderText($(this));})
	    .click(function(){togglePlaceholderText($(this));})
	    .blur(function(){togglePlaceholderText($(this));})
	    .closest('form').submit(function(){$(this).find('input[placeholder]').each(function(){togglePlaceholderText($(this));})});
    }
});

function openSubjectForm(value) {
    $("select[name='subject']").val(value);
    $("#contact_button").trigger('click');
}

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-26056086-1']);
_gaq.push(['_trackPageview']);

(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();




