// Initialisation de la mise en cache des images css pour IE6
try {
	document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

// Lancement du traitement vecto de la typo
Cufon.replace('.fontified', {hover: true});

// Caroussel - http://www.c2is.fr/ [copyright 30th April 2009 by C2iS]
function Carroussel(_name,_container,_containerInner,_btnCarrousselLeft,_btnCarrousselRight){
	this.debug = "";
	this.name = _name;
	this.container = document.getElementById(_container);
	this.containerInner = document.getElementById(_containerInner);
	this.keyTag = "li";
	this.btnCarrousselLeft = document.getElementById(_btnCarrousselLeft);
	this.btnCarrousselRight = document.getElementById(_btnCarrousselRight);
	this.sourcesWidth = new Array();
	this.init = fillCarroussel;
	this.slideLeft = slideLeft;
	this.slideRight = slideRight;
	this.setCssPos = setCssPos;
	this.animSlideLeft = animSlideLeft;
	this.animSlideRight = animSlideRight;
	this.getStopIndex = getStopIndex;
	this.animStop = animStop;
	this.processId = null;
	this.counter = 0;
	this.currentIndexSource = 0;
	this.interval = 1;
	this.screw = 8;
	this.screw_orig = 0;
	this.sourcesOffsetWidth = 0;
	this.pos = 0;
	this.maxLeft = false;
	this.maxRight = true;
	this.getElementsWidth = getElementsWidth;
	this.sourcesLoaded = false;
	this.setClass = setClass;
	this.getNearestMultiple = getNearestMultiple;
	this.getNearestMultipleUp = getNearestMultipleUp;
}
function fillCarroussel(){
	this.setClass(this.btnCarrousselLeft,"inactive");
	this.screw_orig = this.screw;
}
function getElementsWidth(){
		if(! this.sourcesLoaded){
			var elements = this.containerInner.getElementsByTagName(this.keyTag);
			for(var i = 0,l=elements.length;i<l;i++){
				this.sourcesOffsetWidth = this.sourcesOffsetWidth + elements[i].offsetWidth;
				this.sourcesWidth.push(elements[i].offsetWidth);
			}
			this.sourcesLoaded = true;
		}
}
function slideLeft(){
	if(this.maxLeft){ }
	else{
		this.setClass(this.btnCarrousselLeft,"");
		this.animStop();
		this.getElementsWidth();
		this.maxRight = false;
		this.animSlideLeft();
		this.getStopIndex();
	}
}
function animSlideLeft(){
	this.debug += " | "+this.counter;
	this.setCssPos(this.containerInner,"left",this.counter);
	this.counter = this.counter - this.screw ;
	this.processId = setTimeout(this.name + '.startLeft()', this.interval);
	if(this.screw > 1){
		var nearestMultiple = this.getNearestMultiple(this.screw,this.pos - this.sourcesWidth[this.currentIndexSource] - 1);
		if((this.counter < nearestMultiple - (nearestMultiple * 2))){
			this.counter = this.counter + this.screw;
			this.screw = 1;
		}
	}
	if(this.counter == this.pos - this.sourcesWidth[this.currentIndexSource] - 1){
		this.animStop();
		this.pos = this.pos - this.sourcesWidth[this.currentIndexSource];
		if(this.currentIndexSource < this.sourcesWidth.length - 1)
			this.currentIndexSource = this.currentIndexSource + 1;
		if(this.currentIndexSource == this.getStopIndex()){
			this.maxLeft = true;
			this.setClass(this.btnCarrousselRight,"inactive");
			this.setClass(this.btnCarrousselLeft,"");
		}
		this.screw = this.screw_orig;
	}
}
function slideRight(){
	if(this.maxRight){ }
	else{
		this.btnCarrousselRight.className = "";
		this.animStop();
		this.getElementsWidth();
		this.maxLeft = false;
		this.animSlideRight();
	}
}
function animSlideRight(){
	this.processId = setTimeout(this.name + '.startRight()', this.interval);
	this.setCssPos(this.containerInner,"left",this.counter);
	this.counter = this.counter + this.screw ;
	if(this.screw > 1){
		var nearestMultipleUp = this.getNearestMultipleUp(this.screw,this.pos + this.sourcesWidth[this.currentIndexSource-1] + 1);
		if((this.counter > nearestMultipleUp - (nearestMultipleUp * 2))){
			this.counter = this.counter - this.screw;
			this.screw = 1;
		}
	}
	if(this.counter == this.pos + this.sourcesWidth[this.currentIndexSource-1] + 1){
		this.animStop();
		this.pos = this.pos + this.sourcesWidth[this.currentIndexSource-1];
		if(this.currentIndexSource != 0)
			this.currentIndexSource = this.currentIndexSource - 1;
		if(this.currentIndexSource == 0){
			this.maxRight = true;
			this.setClass(this.btnCarrousselRight,"");
			this.setClass(this.btnCarrousselLeft,"inactive");
		}
		this.screw = this.screw_orig;
	}
}
function getStopIndex(){
	var cumul = 0;
	var indexStop = 0;
	for(l=this.sourcesWidth.length-1,i = l;i>=0;i--){
		cumul = cumul + this.sourcesWidth[i];
		if(cumul > this.container.offsetWidth){
			indexStop = i;
			i = -1;
		}
	}
	return indexStop + 1;
}
function setCssPos(obj,attr,posInt){ obj.style[attr] = posInt+ "px"; }
function animStop()
{
  if (this.processId) clearTimeout(this.processId);
  this.processId = null;
}
function setClass(obj,className){obj.className=className;}
function getNearestMultiple(num1, num2){
	num1 = Math.abs(num1); num2 = Math.abs(num2);
	var cum = 0;
	var res = 0;
	for(var i = num1;i<1000;i++){
		if((cum + num1)>num2){
			(cum == num2)?res = cum-num1:res = cum;
			i = 1001;
		}
		else
			cum = cum + num1;
	}
	return res;
}
function getNearestMultipleUp(num1, num2){
	num1 = Math.abs(num1); num2 = Math.abs(num2);
	var cum = 0; var res = 0;
	for(var i = 1;i<1000;i++){
		if((cum + num1)>num2){
			res = cum+num1;
			i = 1001;
		}
		else
			cum = cum + num1;
	}
	return res;
}
Carroussel.prototype.startLeft = animSlideLeft;
Carroussel.prototype.startRight = animSlideRight;


/*
// verifie la validite d'un email
function validemail(champ)
{
	
	
	var email = document.getElementById(champ);
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email.value)) 
	{
	 	alert(get_trad_champ("email"));
		email.focus
		return false;
	}
	else
	{
		return true;
	}
}

// verifie les champs obligatoire d'un formulaire
valider_formulaire = function () {
	var tab = valider_formulaire.arguments;

	taille = tab.length;

	for (i=0;i<taille;i++) {
		if (tab[i]=="email") {
			if (!validemail(tab[i])) {
				return 0; 
			}
		}else{
			if ((document.formulaire.elements[tab[i]].value.trim()=="") || (document.formulaire.elements[tab[i]].value.trim()==" ")) {
				str_temp = tab[i].replace(/_/g," ");
				alert(get_trad_champ(str_temp));
				return 0; 
			}
		}
	}
	
	document.formulaire.submit();
}
*/
/* Fonctions génériques de validation de formulaire */
// Verifie la validite d'un email
function valide_mail_generique ( nameForm, champ ) {
	
	var email = document.forms[nameForm].elements[champ];
		
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if ( !filter.test(email.value) ) {
		
	 	alert(get_trad_champ("email"));
		email.select();
		return false;
	}
	else {
		return true;
	}
}


// Verifie les champs obligatoire d'un formulaire
valider_formulaire_generique = function () {
	var tab = valider_formulaire_generique.arguments;

	taille = tab.length;

	if ( taille > 0 ) {

		nameForm = tab[0];
		
		for ( i = 1; i < taille; i++ ) {
			
			if ( tab[i] == "email" ) {
				if ( !valide_mail_generique( nameForm, tab[i] ) ) {
					return false; 
				}
			}
			else {
				if ( ( document.forms[nameForm].elements[tab[i]].value.trim() == "" ) ) {				
					alert ( get_trad_champ( tab[i] ) );				
					document.forms[nameForm].elements[tab[i]].focus();
					return false; 
				}
			}
		}
		
		return true;
		
	}
	else {
		return false;
	}
}

/*
// Exemple d'appel
function validForm() {
	if ( valider_formulaire_generique('nom_du_formulaire', 'champ_1', 'champ_2' ... ) ) {				
		document.forms['nom_du_formulaire'].submit();					
	}
	else {
		return false;
	}		
}
*/

function empechechar()
{
	if( event.keyCode < 48 || event.keyCode > 57 )
 	{ 
 		alert(get_trad_champ("numeric") );
   		event.returnValue = false;
   	}
}

/*

// fonction qui fait apparaitre le sablier sur soumission de formulaire un peu long..
// Variable témoin de la validation effectuée ou pas
var deja = 0;

function valider(nom_form) {
	// Si on n'a pas déjà validé
	if ( deja == 0 ) {

		// On marque qu'on valide
		deja = 1;
					
		// On affecte le style 'sablier' au corps de la page
		document.body.className = 'sablier';
		
		// On lance la soumission du formulaire
		eval('document.'+nom_form+'.submit()');
	} /*else {
		alert('Le formulaire est déjà validé, veuillez patienter.'); *//*
}

function show_sablier()
{
	document.body.className = 'sablier';
}

*/
// verifie les champs obligatoire d'un formulaire
verif_formulaire_nom = function () {
	var tab = verif_formulaire_nom.arguments;
	var nom_form = "formulaire";
	
	taille = tab.length;

	if (taille>0)
	{
		nom_form = tab[0];
	}	

	for (i=1;i<taille;i++) {
		if (tab[i]=="email") {
			if (!validemail(tab[i])) {
				return 0; 
			}
		}else{
	
			if ((document.getElementById(tab[i]).value.trim()=="")  || (document.getElementById(tab[i]).value.trim()==" ")) {
				//str_temp = tab[i].replace(/_/g," ");
				str_temp = tab[i];
				alert(get_trad_champ(str_temp));
				return 0; 
			}
		}
	}
	
	return true
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function isValidDate ( _fieldId )
{
    if ( _fieldId == "" )
        return true;

    // Récupération de la valeur
    var totalDate    = document.getElementById( _fieldId ).value;
    if ( totalDate == "" )
        return false;
   
    // Découpage de la date récupérée
    var aDate = totalDate.split( "/" );
    if ( aDate.length != 3 )
        return false;

    // Création d'un objet date
    var generatedDate = new Date ( aDate[ 2 ], aDate[ 1 ] - 1, aDate[ 0 ] );
   
    // Test
    if (     generatedDate.getFullYear() == aDate[ 2 ]
        &&    generatedDate.getMonth() + 1 == aDate[ 1 ]
        &&    generatedDate.getDate() == aDate[ 0 ] )
        return true;
   
    return false;
}

// Valide le formulaire de contact
function validContact() {
	if ( valider_formulaire_generique('formContact', 'nom', 'prenom', 'cp', 'adresse', 'ville', 'email' ) ) {				
		document.forms['formContact'].submit();					
	}
	else {
		return false;
	}		
}

// Valide le formulaire de trouver une agence par département
function validTrouverAgenceDepartement() {
	if ( valider_formulaire_generique('formTrouverAgenceDepartement', 'departement' ) ) {				
		document.forms['formTrouverAgenceDepartement'].submit();					
	}
	else {
		return false;
	}		
}

// Valide le formulaire de trouver une agence par code postal
function validTrouverAgenceCp() {
	if ( valider_formulaire_generique('formTrouverAgenceCp', 'cp' ) ) {				
		if ( document.getElementById('cp').value.length < 2 ) {
			alert ( get_trad_champ('cp_plus_2') );
			return false;
		}
		else {		
			document.forms['formTrouverAgenceCp'].submit();
		}
	}
	else {
		return false;
	}		
}