// Fonction captcha en javascript
var Captcha = function(form,captcha,error,target){
	var form = $(form);
	var captcha = $(captcha);
	// Crée un test aléatoire
    var a = Math.ceil(Math.random() * 10);
    var b = Math.ceil(Math.random() * 10);
    var c = a + b;
	// Initialiser le captcha
	var label = document.createElement("span");
	label.innerHTML = a + " + " + b + " = ";
	var input = document.createElement("input");
	input.type = "text";
	captcha.appendChild(label);
	captcha.appendChild(input);
	// Initialiser le formulaire
	var oldSubmit = form.onsubmit;
	form.onsubmit = function(){
		if (input.value == c){
			form.target = target;
			return oldSubmit();
		}else{
			alert(error);
			return false;
		}
	}
}

// Fonctions Standard Macromedia Dreamweaver
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_validateForm() { //v3.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name;obj=val; if ((val=val.value)!="") {
      if (obj.type=="checkbox")	if (!obj.checked) errors+='- La case '+nm+' doit être cochée.\n';
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- Le champ '+nm+' doit contenir une adresse mail valide.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (val!=''+num) errors+='- Le champ '+nm+' doit contenir une valeur numérique.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- Le champ '+nm+' doit contenir une valeur numérique comprise entre '+min+' et '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' est obligatoire.\n'; }
  } if (errors) alert('Le formulaire n\' a pas pu être validé pour les raisons suivantes :\n'+errors);
  document.MM_returnValue = (errors == '');
}

window.addEvent("domready",function(){
	// Formulaire de contact
	var myFx = new Fx.Slide('form_contact', {
	    duration: 1000,
	    transition: Fx.Transitions.Pow.easeOut
	}).hide();
	[$('fermerForm'),$('contact'),$('lienContact'),$('contactbas')].each(function(e,i){
		if($chk($(e))) e.addEvent("click",function(event){
			myFx.toggle();
		});
	});

    var reg = /contact/g;
    var loc = window.location.href;
    if(loc.match(reg)){
        myFx.toggle();
    }

	// Captcha
	Captcha('form1','captcha1','Test antispam incorrect !','http://pro.ircf.fr/communaute/mail.php');
	// Galerie
	if($chk($('masque'))){
		if($chk($('accueil'))) new Gallery('masque',{buttons:'.fleche'});
		if($chk($('showcase'))) new Gallery('masque',{buttons:'.fleche',elemsPerView:4});
	}
	// Menu
	$$("#menu>li>a").each(function(e,i){
		var myFx = new Fx.Tween(e,{link:"chain",duration:"200"});
		e.addEvents({
			'mouseenter' : function(){
				var pos = e.getStyle('background-position');
				myFx.start('background-position', pos, '0 0');
			},
			'mouseleave' : function(){
				var pos = e.getStyle('background-position');
				myFx.start('background-position', pos, '0 70px');
			}
		});
	});
	// Menu ProActiv
	$('lien_sites_internet').addEvents({
		"mouseenter" : function(e,i){
			$('menu_sites_internet').setStyle("display","block");
		},
		"mouseleave" : function(e,i){
			$('menu_sites_internet').setStyle("display","none");
		}
	});
	// Formulaires
	$("voile").setStyle("height",document.getSize().x);
	$("voile").addEvent("click",function(event){
		$("voile").setStyle("display","none");
		$$(".formulaire").each(function(e,i){
			if($chk($(e))) e.setStyle("display","none");
		});
	});
	var afficheur_form = ["pro","mail","go_form_proactiv","go_form_proactivplus","go_form_proactivcommerce"];
	afficheur_form.each(function(e,i){
		if($chk($(e))) $(e).addEvent("click", function(event){
			$("voile").setStyle("display","block");
			$$($(e).getProperty('href')).setStyle("display","block");
			$$($(e).getProperty('href')).each(function(fm){
				fm.getElement("input[type=reset]").addEvent("click",function(){
					$('voile').fireEvent("click");
				});
			});
			new Fx.Scroll($(document.body)).start(0, 0);
			return false;
		});
	});
	// Afficher le formulaire correspondant à l'ancre dans l'URL
	if (location.hash.length>0){
		var f= $(document.body).getElement(".formulaire"+location.hash);
		if (f){
			f.setStyle("display","block");
			new Fx.Scroll($(document.body)).start(0, 0);
		}
	}
	
	// Faire clignoter le lien menant au showcase
	if($("pub_showcase")){
		var clignotement = new Fx.Tween("pub_showcase", {property: 'opacity'});
		(function(){
			clignotement.start(1,0.1).chain(
				function(){ this.start(0,1); },
			    function(){ this.start(1,0); },
			    function(){ this.start(0,1); }
			);
		}).periodical(10000);
	}
});
