/**
 * @author Remi
 */

$(document).ready(function() {
  $('.chapitre:not(.actif) .extrait').hide();

  $('#chapitres h3').click(function() {
	if (!$(this).parent().is(".actif")) {
	  $(".chapitre").removeClass("actif");
	  $(".extrait").slideUp("fast");
	  $(this).parent().toggleClass("actif");
	  $(this).next().slideToggle("fast");
	}
  });

  $('#chapitres h3').mouseover(function() {
	$(this).addClass("hover");
  }).mouseout(function() {
	$(this).removeClass("hover");
  });
  
  $('p.imprimer button').click(function() {
	window.print();
  });
  
  $('#pub a').media({
  	caption: false,
	autoplay: true,
	height: 130,
	width: 168
  })
  
});

var valFormatsValidation = new Array();
valFormatsValidation['requis']		        = /^.+$/;
valFormatsValidation['optionnel']		      = /^.*$/;
valFormatsValidation['courriel']		      = /^[_a-zA-Z0-9.\-]*@[a-zA-Z0-9]([_a-zA-Z0-9\-]+\.)+([a-zA-Z]{2,10})$/;
valFormatsValidation['telephone']		      = /^[0-9]{3}\s[0-9]{3}\-[0-9]{4}$/;
valFormatsValidation['codepostal']		    = /^([A-Z]{1}[0-9]{1}){3}$/;
valFormatsValidation['cartecredit']		    = /^[0-9]{16}$/;
valFormatsValidation['cartecredit-auth']	= /^[0-9]{3}$/;

$(document).ready(function() {
  $.meta.setType("class");
  $("form#acheter").submit(function() {

	// variable qui déterminie si le formulaire entier est valide
	var form_valide = true;
	
	// on nettoie le formulaire
	$('span',this).remove();
	$("p.alerte",this).remove();    
	
	// Pour chaque INPUT et SELECT
	$('input, select',this).each(function() {
	  var champ = this;
	  var champ_valide=true;
	  var msg;
	  var champ_optionnel = false;

	  jQuery.each($(champ).data(), function(i, n){
		if (i == "format") {
		  var formats = n;
		  if (formats.length == 0) { return; }

		  for (var compteur=0; compteur<formats.length; compteur++) {
			var format = formats[compteur];
			if (format == "optionnel" && $(champ).val() == "") {
			  champ_optionnel = true;
			}
			champ_valide = ($(champ).validerRegex(valFormatsValidation[format])) ? true : false;
		  }
		}
		if (i == "msg") { msg = n; }
	  });

	  if (champ_optionnel === true) {
		champ_valide=true;
	  }
	
	  var parent = $(champ).parent();
	  parent.removeClass("erreur");        
	  if (champ_valide===true) {
		$("span",parent).remove();
	  } else {
		form_valide=false;
		parent.addClass("erreur");
		if (msg) { parent.append('<span>'+msg+'</span>'); }
	  }        
	  
	});
	
	if (form_valide!==true) {
	  
	  $(this).prepend("<p class=\"alerte\">Le formulaire contient des erreurs. Veuillez les corriger aux endroits&nbsp;indiqués. <a href=\"javascript://\">Effacer les messages d'erreur</a>.</p>");
	  var formulaire = this;
	  $('p.alerte a',this).click(function() {
		$('span',formulaire).remove();
		$("p.alerte",formulaire).remove();
		$('ul li',formulaire).removeClass("erreur");
	  });
	}
	return form_valide;
	
  })
});

jQuery.fn.validerRegex = function(pattern) {
  var valide=false;
  this.each(function(){ valide = (jQuery(this).val().match(pattern) != null) ? true : false; });
  return valide;
}