// TOOLBOX (fonctions communes a l'ensemble des fonctionnnalites et parties du site)
var isIE6 = false

var result_image;

var strChUserAgent = navigator.userAgent;
var intSplitStart = strChUserAgent.indexOf("(",0);
var intSplitEnd = strChUserAgent.indexOf(")",0);
var strChMid = strChUserAgent.substring(intSplitStart, intSplitEnd);

if(strChMid.indexOf("MSIE 6") != -1) isIE6 = true;

//|---------------------------| champs de recherche a vider
function qsearch(oThis){
	//	var savedvalue = oThis.value;
	if (oThis.value==oThis.title) oThis.value = "";
	oThis.onblur = function (){
		if (this.value=='') this.value = this.title;
	}
}

// fonction de changement de classe CSS sur un element donne
function switch_class(elem, to_remove, to_add){
	$(elem).removeClassName(to_remove);
	$(elem).addClassName(to_add);
}
// fonction de fermeture des pop-in
function hide_popin(name){
	//new Effect.Fade(name, {duration : 0.3});
	//$('sel_finition').style.visibility="";
	var homefalsh = document.getElementById('flash_home');
	if (homefalsh!=undefined) {
		if (homefalsh.style.visibility == 'hidden') homefalsh.style.visibility = '';
	}

	$(name).hide();
	if ($('member_menu_2').style.display=='none') onforms.resetForm();
	return false;
}

function choose_popin(action){

	new Ajax.Request('_ajax/user_account.php', {
		method: 'post',
		evalScripts: true,
		parameters: 'step=choose_popin&action='+action,
		onSuccess: function(transport){

			switch(transport.responseText){
				case "OK": // utilisateur connecte et nb_item < 5
					if(action == 'agent') show_popin('pop_in_research_agent');// formulaire creation agent
					else show_popin('pop_in_sms_alert');// formulaire creation alerte
					break;

				case "ITEM_MAX": // utilisateur connecte et nb_item=5
					if(action == 'agent') show_popin('too_many_agents');// formulaire creation agent
					else show_popin('too_many_alerts');// formulaire creation alerte
					break;

				case "NON_CONNECTE": // utilisateur non-connecte
					// affichage formulaire identification intermediaire
					$('register_param').innerHTML = '<input type="hidden" id="action" name="action" value="'+action+'">';
					show_popin('pop_in_register');
					break;
			}
		}
	});
	return false;
}

function show_popin(){
	/*$('sel_finition').style.visibility="hidden";*/

	/*var homefalsh = document.getElementById('flash_home');
	if (homefalsh!=undefined) {
		homefalsh.style.visibility = 'hidden';
	}*/
	switch(show_popin.arguments[0]){

		case 'trial':
			$('form3_id_concession').value	= show_popin.arguments[1];
			$('form3_vehicle_info').value		= show_popin.arguments[2];
			$('id_pournumvo').value 			= show_popin.arguments[3];
			$('id_vehicle_info').innerHTML	= $('saveVehiculInfo').innerHTML;
			$(show_popin.arguments[0]).show();
			break;
		case 'reserv':
			$('form13_id_concession').value		= show_popin.arguments[1];
			$('form13_vehicle_info').value		= show_popin.arguments[2];
			$('id_vehicle_info').innerHTML		= $('saveVehiculInfo').innerHTML;
			$('id_pournumvo_reserv').value		= show_popin.arguments[3];
			$('id_cp_concessionnaire').innerHTML= show_popin.arguments[4].charAt(0)+show_popin.arguments[4].charAt(1);
			$(show_popin.arguments[0]).show();
			break;

		/*case 'rappel':
			//	if ( ($F('telephone')!="") && ($F('telephone')!="Votre numéro de téléphone") ) {
		/ $('form4_rappel_tel').value	= ( ( ($F('telephone')!="") && ( $F('telephone').indexOf("Votre")<0) ) ? $F('telephone') : '');
			$('form4_vehicle_info').value	= $F('vehicle_info');
			$('form4_id_concession').value= $F('id_concession');
			$('id_pournumvo_rappel').value= show_popin.arguments[1];
			$(show_popin.arguments[0]).show();
			//}
			break;
		*/
		default:
			var divs = document.body.getElementsByTagName('div');
			for(var i = 0; i< divs.length; i++){
				if( $(divs.item(i)).hasClassName('pop_in') ||
					$(divs.item(i)).hasClassName('pop_in_fiche') || 
					$(divs.item(i)).hasClassName('pop_in_plan') ){

					$(divs.item(i)).hide();
				}

				if( ( $(divs.item(i)).hasClassName('pop_in') || 
					$(divs.item(i)).hasClassName('pop_in_fiche') || 
					$(divs.item(i)).hasClassName('pop_in_plan') ) && 
					divs.item(i).id == show_popin.arguments[0] ){

					$(divs.item(i)).show();
				}
			}

			break;
	}
	return false;
}

// MOTEUR DE RECHERCHE : GESTION DU FORMULAIRE DE RECHERCHE
var handleCars = {
	modelDiv : [],
	carosDiv : [],
	serie_tab : [],
	carross_tab : [],
	img_carros :[],
	flatImages :[],
	saveImage4reload : '<img src="_img/research/serie1_3portes.png" />',
	actualSelectedModel : 's1',
/*	actual_serie : 'c1',*/
	preloadVarJ: 0, preloadMaxVarJ: 11,
	preloadVarI: 0, preloadMaxVarI: 14, preloadTimer : false,
	preload: function(){
		if((handleCars.preloadVarJ < handleCars.preloadMaxVarJ) && (handleCars.preloadVarI==0)) {
			handleCars.preloadVarJ++;
			handleCars.preloadVarI++;

		} else if ((handleCars.preloadVarJ >= handleCars.preloadMaxVarJ) && (handleCars.preloadVarI >= handleCars.preloadMaxVarI)){
			clearInterval(handleCars.preloadTimer);

		} else {
			if(handleCars.preloadVarI < handleCars.preloadMaxVarI) handleCars.preloadVarI++;
			else {
				handleCars.preloadVarI = 0;
				return;
			}
		}
		if(handleCars.img_carros['s'+handleCars.preloadVarJ]['c'+handleCars.preloadVarI] != undefined){
			var preloadImage1 = new Image();
			var preloadImage2 = new Image();
			preloadImage1.src = handleCars.img_carros['s'+handleCars.preloadVarJ]['c'+handleCars.preloadVarI];
			preloadImage2.src = handleCars.img_carros['s'+handleCars.preloadVarJ]['c'+handleCars.preloadVarI].replace('research','research/mini');
		}
	},
	wait: function(n){
		clearTimeout(this.preloadTimer);
		this.preloadTimer = setTimeout('handleCars.preload2('+n+')', 50);
	},
	preloadImage1 : [],

	preload2: function(n){
		var preloadImage1 = new Image();
		var preloadImage2 = new Image();
		preloadImage1.src = this.flatImages[n];
		preloadImage2.src = this.flatImages[n].replace('research','research/mini');

		if(preloadImage1.complete==false) {
//			console.log('wait');
			this.wait(n++);
		} else {
			if(n<this.flatImages.length-1) {
				n++;
				this.preload2(n);
			} else {
				clearTimeout(this.preloadTimer);
//				console.log('done');
			}
		}
	},
	set: function(){
		this.flatImages = Array(
			'_img/research/serie1_coupe.png',
			'_img/research/serie1_cabriolet.png',
			'_img/research/serie1_3portes.png',
			'_img/research/serie1_5portes.png',
			'_img/research/serie3_berline.png',
			'_img/research/serie3_coupe.png',
			'_img/research/serie3_cabriolet.png',
			'_img/research/serie3_touring.png',
			'_img/research/serie5_berline.png',
			'_img/research/serie5_touring.png',
			'_img/research/serie5_gt.png',
			'_img/research/serie6_coupe.png',
			'_img/research/serie6_cabriolet.png',
			'_img/research/serie7_berline.png',
			'_img/research/serieM_coupe.png',
			'_img/research/serieM_cabrio.png',
			'_img/research/serieM_berline.png',
			'_img/research/serieM_touring.png',
			'_img/research/serieM_4x4.png',
			'_img/research/serieX3.png',
			'_img/research/serieX5.png',
			'_img/research/serieX6.png',
			'_img/research/serieZ_coupe.png',
			'_img/research/serieZ_roadster.png',
			'_img/research/serieX3.png',
			'_img/research/serieX5.png',
			'_img/research/serieX6.png',
			'_img/research/serieX1.png'
		);

		this.serie_tab['s1'] = {
			css: 'serie1', value:'1', carros_cl: 'portes3', carros_id: 'c7', img: '_img/research/serie1_3portes.png', textval:''
		};
		this.serie_tab['s2'] = {
			css: 'serie3', value:'3', carros_cl: 'berline', carros_id: 'c1', img: '_img/research/serie3_berline.png', textval:''
		};
		this.serie_tab['s3'] = {
			css: 'serie5', value:'5', carros_cl: 'berline', carros_id: 'c1', img: '_img/research/serie5_berline.png', textval:''
		};
		this.serie_tab['s4'] = {
			css: 'serie6', value:'6', carros_cl: 'cabriolet', carros_id: 'c3', img: '_img/research/serie6_cabriolet.png', textval:''
		};
		this.serie_tab['s5'] = {
			css: 'serie7', value:'7', carros_cl: 'berline', carros_id: 'c1', img: '_img/research/serie7_berline.png', textval:''
		};
		this.serie_tab['s6'] = {
			css: 'serieM', value:'M', carros_cl: 'coupe', carros_id: 'c2', img: '_img/research/serieM_coupe.png', textval:''
		};
		this.serie_tab['s7'] = {
			css: 'serieX1', value:'X', carros_cl: 'quatreX4', carros_id: 'c6', img: '_img/research/serieX3.png', textval:''
		};
		this.serie_tab['s8'] = {
			css: 'serieZ', value:'Z', carros_cl: 'coupe', carros_id: 'c2', img: '_img/research/serieZ_coupe.png', textval:''
		};

		this.carross_tab["c1"] = {
			css:'berline', value:'Berline', serie:'', textval:''
		};
		this.carross_tab["c2"] = {
			css:'coupe', value:'Coupé', serie:'', textval:''
		};
		this.carross_tab["c3"] = {
			css:'cabriolet', value:'Cabriolet', serie:'', textval:''
		};
		this.carross_tab["c4"] = {
			css:'touring', value:'Touring', serie:'', textval:''
		};
		this.carross_tab["c5"] = {
			css:'roadster', value:'Roadster', serie:'', textval:''
		};
		this.carross_tab["c6"] = {
			css:'quatreX4', value:'4X4', serie:'', textval:'X5-X6'
		};
		this.carross_tab["c7"] = {
			css:'portes3', value:'3P', serie:'', textval:'3 Portes'
		};
		this.carross_tab["c8"] = {
			css:'portes5', value:'5P', serie:'', textval:'5 Portes'
		};
		this.carross_tab["c9"] = {
			css:'granturismo', value:'Gran Turismo', serie:'', textval:''
		};
		this.carross_tab["c10"] = {
			css:'quatreX2', value:'4X2', serie:'', textval:''
		};
		this.carross_tab["c11"] = {
			css:'serieX3', value:'X3', serie:'X3', textval:''
		};
		this.carross_tab["c12"] = {
			css:'serieX5', value:'X5', serie:'X5', textval:''
		};
		this.carross_tab["c13"] = {
			css:'serieX6', value:'X6', serie:'X6', textval:''
		};
		this.carross_tab["c14"] = {
			css:'serieX1', value:'X1', serie:'X1', textval:''
		};

		this.img_carros["s1"] = {
			"c2" : '_img/research/serie1_coupe.png',
			"c3" : '_img/research/serie1_cabriolet.png',
			"c7" : '_img/research/serie1_3portes.png',
			"c8" : '_img/research/serie1_5portes.png'
		};
		this.img_carros["s2"] = {
			"c1" : '_img/research/serie3_berline.png',
			"c2" : '_img/research/serie3_coupe.png',
			"c3" : '_img/research/serie3_cabriolet.png',
			"c4" : '_img/research/serie3_touring.png'
		};
		this.img_carros["s3"] = {
			"c1":'_img/research/serie5_berline.png',
			"c4":'_img/research/serie5_touring.png',
			"c9":'_img/research/serie5_gt.png'
		} ;
		this.img_carros["s4"] = {
			"c2":'_img/research/serie6_coupe.png',
			"c3":'_img/research/serie6_cabriolet.png'
		};
		this.img_carros["s5"] = {
			"c1":'_img/research/serie7_berline.png'
		};
		this.img_carros["s6"] = {
			"c2":'_img/research/serieM_coupe.png',
			"c3":'_img/research/serieM_cabrio.png',
			"c1":'_img/research/serieM_berline.png',
			"c4":'_img/research/serieM_touring.png',
			"c6":'_img/research/serieM_4x4.png'
		};
		this.img_carros["s7"] = {
			'c11': '_img/research/serieX3.png',
			'c12': '_img/research/serieX5.png',
			'c13': '_img/research/serieX6.png',
			'c14': '_img/research/serieX1.png'
		};
		this.img_carros["s8"] = {
			"c2":'_img/research/serieZ_coupe.png',
			"c5":'_img/research/serieZ_roadster.png'
		};
		
//		this.preloadTimer = setInterval("handleCars.preload()", 50);
		this.preload2(0);

		// Affichage des carrosseries pour la série
		for(var i=1;i<15;i++){
			if(this.img_carros[this.actualSelectedModel]["c"+i] != undefined){

				$('c'+i).innerHTML = "<img src='"+this.img_carros[this.actualSelectedModel]["c"+i].replace('research','research/mini')+"' /><span>"+((this.carross_tab["c"+i].textval=='')? this.carross_tab["c"+i].value : this.carross_tab["c"+i].textval)+"</span>";
				$('c'+i).style.display = "inline-block";
			}else{
				$('c'+i).style.display = "none";
			}
		}

		// modeles
		this.modelDiv = document.getElementById("type_cars").getElementsByTagName("a");
		for(var i=0;i<this.modelDiv.length;i++){
			this.modelDiv[i].onmouseover = function(){
//				handleCars.show_serie(this.id, false);// not needed
				switch_class(this.id, '', 'light');
			}
			this.modelDiv[i].onmouseout = function(){
				handleCars.displayCarImg(handleCars.saveImage4reload);
				if (handleCars.actualSelectedModel != this.id){
					switch_class(this.id, 'light', '');
				}
			}
			this.modelDiv[i].onclick = function(){
				return handleCars.select_serie(this.id);
			}
		}
		// carosseries
		this.carosDiv = document.getElementById("name_cars").getElementsByTagName("a");
		for(var i=0;i<this.carosDiv.length;i++){

			this.carosDiv[i].onmouseover = function(){
				if (handleCars.img_carros[handleCars.actualSelectedModel][this.id]!=undefined) {
					switch_class(this.id, '', 'light');
				}
			}
			this.carosDiv[i].onmouseout = function(){
				if (handleCars.actualSelectedModel != this.id){
					switch_class(this.id, 'light', '');
				}
			}
			this.carosDiv[i].onclick = function(){
				return handleCars.select_carrosserie(this.id);
			}
		}

	},
	displayCarImg : function(val){
		$('img_cars_fix').innerHTML = val;
		if(isIE6) {
			correctPNG();// IE 6
		}
	},
	show_serie : function (id, save){
		var img2show = '<img src="'+this.serie_tab[id].img+'" />';
		handleCars.displayCarImg(img2show);
		if (save) this.saveImage4reload = img2show;

	},
	show_carooserie : function (seriId, id, save){
		if (this.img_carros[seriId][id]!=undefined) {// test non necessaire en principe
			var img2show = '<img src="'+this.img_carros[seriId][id]+'" />';
			handleCars.displayCarImg(img2show);
			if (save) this.saveImage4reload = img2show;
		}
		$('search_advanced').removeClassName('cache');
	},
	select_serie : function (id){// gestion des images de voiture en fonction de la serie choisie
		this.actualSelectedModel = id;
		var serie_css	= this.serie_tab[id].css;
		var serie_cl	= this.serie_tab[id].carros_cl;
		var serie_val	= this.serie_tab[id].value;

		/** nad for serie "X" */
		if( this.serie_tab[id].value != $('serie').value) {
			$('serie').value = this.serie_tab[id].value ;
		}

		var carros_id	= this.serie_tab[id].carros_id;
		$('search_advanced').removeClassName('cache');
		var ci = 0;
		for( var attrname in this.img_carros[id] ){
			ci++;
		}

		if( ci > 4 ){
			$('name_cars').addClassName('nb6');
		}else{
			$('name_cars').removeClassName('nb6');
		}

		$('car_selected').style.display = "none";

		for (var i=1;i<9;i++){
			var serie_id = 's'+i;
			if($(serie_id).hasClassName('on')){
				current_serie = serie_id;
			}
		}

		// Affichage des carrosseries pour la série
		for(var i=1;i<15;i++){
			if(this.img_carros[id]["c"+i] != undefined){
				$('c'+i).innerHTML = "<img src='"+this.img_carros[this.actualSelectedModel]["c"+i].replace('research','research/mini')+"' /><span>"+((this.carross_tab["c"+i].textval=='')? this.carross_tab["c"+i].value : this.carross_tab["c"+i].textval)+"</span>";
				$('c'+i).style.display = "inline-block";

			} else{
				$('c'+i).style.display = "none";
			}

		}


		// affichage du lien de la serie, de son image et activation du lien de carrosserie correspondant
		if( ! $(id).hasClassName('on') ){
			// serie : m.a.j image et lien
			switch_class(id, 'light', 'on');

			$('serie').value = serie_val;

			handleCars.show_serie(id, true);

			// deselection de la serie initialement selectionnee
			for (var i=1;i<9;i++){// on ne peut pas faire this.serie_tab.length car s1, s2,... comme clees
				var div_id = 's'+i;
				if(id != div_id){
					if( $(div_id).hasClassName('on') ){
						switch_class(div_id, 'on', this.serie_tab[div_id].css);
						switch_class(div_id, 'light', '');
					}
				}
			}
			// deselection de la carrosserie initialement selectionnee
			for (var i=1;i<15;i++){
				var div_id = 'c'+i;
				if(carros_id != div_id){
					if( $(div_id).hasClassName('on') ){
						switch_class(div_id, 'on', this.carross_tab[div_id].css);
					}
				}
			}
		}
		$('model').value = "";
		refreshCountResult('advanced');
		return false;
	},
	select_carrosserie : function(id){ // gestion des images de voiture en fonction de la carrosserie choisie

		var current_serie;

		// recuperation de la serie selectionnee
		for (var i=1;i<9;i++){
			var serie_id = 's'+i;
			if($(serie_id).hasClassName('on')){
				current_serie = serie_id;
			}
		}

		if( this.carross_tab[id].serie != "" ){
			$('model').value = "";
			$('serie').value = this.carross_tab[id].serie ;
			refreshCountResult('advanced');

		}else{
			// verification de l'existance de la carrosserie choisie en fonction de la serie selectionnee
			if(this.img_carros[current_serie][id] != undefined){

				handleCars.show_carooserie(current_serie, id, true);

//				if( ! $(id).hasClassName('on') ){
					switch_class(id, 'light', 'on');
					$('model').value = this.carross_tab[id].value;
					refreshCountResult('advanced');
					for (var i=1;i<15;i++){
						// deselection de la carrosserie precedemment selectionnee
						var div_id = 'c'+i;
						if(id != div_id){
							if( $(div_id).hasClassName('on') ){
								switch_class(div_id, 'on', this.carross_tab[div_id].css);
							}
						}
					}
//				}
			}
		}

		var obj = this;
		$('car_selected').onclick = function(){ obj.select_serie(obj.actualSelectedModel);};
		$('car_selected').innerHTML = "<img src='"+this.img_carros[current_serie][id]+"' /><span>"+((this.carross_tab[id].textval=='')? this.carross_tab[id].value : this.carross_tab[id].textval)+"</span>";
		$('car_selected').style.display = "block";

		return false;
	}
}

// gestion du champs de recherche de localisation en fonction de type de vehicule BPS/VC
function select_prem(id){
	var pNodeSp = document.getElementById(id).parentNode.getElementsByTagName("span");
	for (var i=0;i<pNodeSp.length;i++){
		switch_class(pNodeSp[i], 'on', '');
	}
	switch (id){
		case 'BPM':
			if($(id).hasClassName('checkbox_on')){
				switch_class(id, 'checkbox_on', 'checkbox');
				$('flag').value = '';
			}else{
				document.getElementById(id).nextSibling.className +=" on";

				switch_class(id, 'checkbox', 'checkbox_on')
				$('flag').value = 'VO';
				if( $('localisation').getStyle('display') == 'none'){
					$('localisation').show();
				}
				if($('VC').hasClassName('checkbox_on')){
					switch_class('VC', 'checkbox_on', 'checkbox');
				}
			}
			refreshCountResult('advanced');
			break;
		case 'VC':
			if($(id).hasClassName('checkbox_on')){
				switch_class(id, 'checkbox_on', 'checkbox');
				$('localisation').show();
				$('flag').value = '';
			}else{
				document.getElementById(id).nextSibling.className +=" on";
				switch_class(id, 'checkbox', 'checkbox_on');
				$('flag').value = 'VC';
				if( $('localisation').getStyle('display') != 'none'){
					$('localisation').hide();
				//$('depdep').value = $('depdep').title;
				}
				if($('BPM').hasClassName('checkbox_on')){
					switch_class('BPM', 'checkbox_on', 'checkbox');
				}
			}
			refreshCountResult('advanced');
			break;
	}
	return false;
}
// gestion des cases a cocher
function select_radio(id, id_name1, id_name2, value1, value2, id_name3, value3, input_name){

	var dual_id = [];
	var value;
	if(id == id_name1){
		dual_id[0]= id_name2;
		dual_id[1]= id_name3;
		value = value1;
	}else if(id == id_name2){
		dual_id[0]= id_name1;
		dual_id[1]= id_name3;
		value = value2;
	}else{
		dual_id[0]= id_name1;
		dual_id[1]= id_name2;
		value = value3;
	}

	var pNodeSp = document.getElementById(id).parentNode.parentNode.getElementsByTagName("li");
	for (var i=0;i<pNodeSp.length;i++){
		switch_class(pNodeSp[i], 'on', '');
	}

	if($(id).hasClassName('checkbox_on')){
		switch_class(id, 'checkbox_on', 'checkbox');
		$(input_name).value = '';
	}else{
		document.getElementById(id).parentNode.className +=" on";
		switch_class(id, 'checkbox', 'checkbox_on');
		$(input_name).value = value;
		for(var i=0; i<dual_id.length;i++){
			if( $(dual_id[i]) ){
				if($(dual_id[i]).hasClassName('checkbox_on') ){
					switch_class(dual_id[i], 'checkbox_on', 'checkbox');
				}
			}
		}

	}
	refreshCountResult('advanced');
	return false;
}

//	gestion des finitions
function select_finition(id){
	$('finition').value = $(id).value;
	refreshCountResult('advanced');
	return false;
}

// 	gestion des equipements
function select_equip(id){
	var tab = new Array();
	tab["equip_gps"]		= {
		name:'gps',
		value:'2'
	};
	tab["equip_toit"]		= {
		name:'toit',
		value:'3'
	};
	tab["equip_cuir"]		= {
		name:'cuir',
		value:'4'
	};
	tab["equip_xdrive"]	= {
		name:'xdrive',
		value:'7'
	};
	tab["equip_xenon"]	= {
		name:'phare',
		value:'6'
	};
	tab["equip_tel"]		= {
		name:'tel',
		value:'8'
	};

	if($(id).hasClassName('checkbox_on')){
		switch_class(id, 'checkbox_on', 'checkbox');
		$(tab[id].name).value = '';
		document.getElementById(id).parentNode.className ="";
	}else{
		switch_class(id, 'checkbox', 'checkbox_on');
		$(tab[id].name).value = tab[id].value;
		document.getElementById(id).parentNode.className ="on";
	}

	refreshCountResult('advanced');
	return false;
}
// affichage des parametres avances pour la recherche detaillee
function show_criteria_det(){
	//$('depdep').value = $('depdep').title;

	if($('more_details').getStyle('display') == 'none'){
		$('show_more_criteria').hide();
		$('show_less_criteria').show();
		if( isIE6 ){
			$('more_details').style.display = "block";
		}else{
			Effect.SlideDown('more_details', {
				duration : 0.3
			});
		}

	}else{
		$('show_more_criteria').show();
		$('show_less_criteria').hide();
		if( isIE6 ){
			$('more_details').style.display = "none";
		}else{
			Effect.SlideUp('more_details', {
				duration : 0.3
			});
		}

	}

	return false;
}

// MOTEUR DE RECHERCHE : EXECUTION DES REQUETES ET AFFICHAGE DES RESULTATS
// execution de la recherche a partir du module de recherche & affichage de la liste de resultatst textuelle
function get_search_result(form_id){

//	if( $('submit_recherche').value != 'Rechercher' ){
		new Ajax.Request('_ajax/recherche.php', {
			method: 'post',
			//evalJS: true,
			evalScripts: true,
			parameters: $(form_id).serialize(true),
			//onFailure: function(){ alert('Something went wrong...') },
			onSuccess: function(transport){
				if(transport.responseText.match('too many result')) {
					$('too_many_result').show();
				}else if(transport.responseText.match('no result')){
					$('no_result').show();
				}else if(transport.responseText.match('exclusion mini')){
					$('exclusion_mini').show();
				}else{
					$('result').update(transport.responseText);
					$('flash_home').hide();
					$('result').show();
					if($(form_id).type.value!='ex') {
						$('search_advanced').addClassName('cache');
					} else {
						handleCars.select_serie(handleCars.modelDiv[0].id);
					}
				}
				refreshCountResult(form_id)
			}
		});
/*		new Ajax.Request('_ajax/count_result.php', {
			method: 'post',
			//evalJS: true,
			//evalScripts: true,
			parameters: $(form_id).serialize(true),
			//onFailure: function(){ alert('Something went wrong...') },
			onSuccess: function(transport){
				//$('nb_result').update(transport.responseText);
				$('submit_recherche').value(transport.responseText);
			}
		});*/
/*	}else{
		if( $('model').value == "" ){
			// TODO insertion de la popin "Choisir une carrosserie"
		}else{
			$('no_result').show();
		}
	}*/
	return false;
}
function refreshCountResult(form_id){
	new Ajax.Request('_ajax/count_result.php', {
		method: 'post',
		//evalJS: true,
		//evalScripts: true,
		parameters: $(form_id).serialize(true),
		//onFailure: function(){ alert('Something went wrong...') },
		onSuccess: function(transport){
			$('submit_recherche').value = transport.responseText;

		}
	});
	return false;
}
//|-------/
function dsp_search_result(id){
	if(id == ''){
		new Ajax.Request('./_ajax/recherche.php', {
			method: 'post',
			evalScripts: true,
			//onFailure: function(){ alert('Something went wrong...') },
			onSuccess: function(transport){
				//new Effect.Appear('no_result', {duration: 0.3});
				//$('too_many_result').appear({duration: 0.3});
				if(transport.responseText.match('too many result')) $('too_many_result').show();
				else if(transport.responseText.match('no result')) $('no_result').show();
				else $('result').update(transport.responseText);
			}
		});
	} else if (id=='collab'){
		document.location.href = 'vehicules_bmw_vehicule_collaborateurs,liste';
	} else if (id=='selection_bmw'){
		document.location.href = 'selection_bmw.php?tpl=showlist';
	} else if (id=='cover'){
		dsp_coverflow('');
	} else {
		var _page = isNaN( id )? 'marques' : 'concession' ;
		new Ajax.Request('_ajax/'+_page+'.php?tpl=showlist', {
			method: 'post',
			parameters: 'id='+id,
			//onFailure: function(){ alert('Something went wrong...') },
			onSuccess: function(transport){
				$('result').update(transport.responseText);
			}
		});
	}
	return false;
}

//|-------/
// reexecution de la requete pour les vehicules uniquement BPS
function dsp_bps_result(id){
	//	if(!$('BPM').hasClassName('checkbox_on')) select_prem("BPM");//NAD
//	document.getElementById('nb_result').innerHTML = 'Rechercher' //
	if(id == ''){
		new Ajax.Request('_ajax/recherche.php', {
			method: 'post',
			parameters: 'gar=BPS',
			evalScripts: true,
			//onFailure: function(){ alert('Something went wrong...') },
			onSuccess: function(transport){
				if(transport.responseText.match('too many result')) $('too_many_result').show();
				else if(transport.responseText.match('no result')) $('no_result').show();
				else $('result').update(transport.responseText);
			}
		});
	} else {
		new Ajax.Request('_ajax/concession.php', {
			method: 'post',
			parameters: 'id='+id+'&gar=BPS',
			//onFailure: function(){ alert('Something went wrong...') },
			onSuccess: function(transport){
				if(transport.responseText.match('too many result')) $('too_many_result').show();
				else if(transport.responseText.match('no result')) $('no_result').show();
				else $('result').update(transport.responseText);
			}
		});
	}
	return false;
}

/** NAD push*/
function dsp_push(){

	new Ajax.Request('./_ajax/recherche.php', {
		method: 'post',
/*		evalJS: true,*/
		evalScripts: true,
		parameters: 'type=push&coverflow=true',
		//onFailure: function(){ alert('Something went wrong...') },
		onSuccess: function(transport){
//alert(transport.responseText);
			if(transport.responseText.match('too many result')) $('too_many_result').show();
			else if(transport.responseText.match('no result')) $('no_result').show();
			else {
				$('result').update(transport.responseText);
				$('flash_home').hide();
				//$('top').hide();
				$('result').show();
			}
		}
	});
	return false;
}

/** \ */

//|-------/
// APPEL de la page resultat coverflow.
function dsp_coverflow(id){

	var file;

	if( id == '' ){
		file = '_ajax/recherche.php';
	}else if( id == 'collab' ){
		document.location.href = 'vehicules_bmw_vehicule_collaborateurs.php';
	}else{
		var _page = isNaN( id )? 'marques' : 'concession' ;
		new Ajax.Request('_ajax/'+_page+'.php', {
			method: 'post',
			parameters: 'id='+id,
			//onFailure: function(){ alert('Something went wrong...') },
			onSuccess: function(transport){
				$('result').update(transport.responseText);
			}
		});
		return true;
	}
	new Ajax.Request('./_ajax/recherche.php', {
		method: 'post',
		/*evalJS: true,*/
		evalScripts: true,
		parameters: 'coverflow=true',
		//onFailure: function(){ alert('Something went wrong...') },
		onSuccess: function(transport){
			
			if(transport.responseText.match('too many result')) $('too_many_result').show();
			else if(transport.responseText.match('no result')) $('no_result').show();
			else $('result').update(transport.responseText);
		}
	});
	return false;
}
//|-------/
// LISTE DES RESULTATS DU MOTEUR DE RECHERCHE
// gestion del'affichage des lignes en survol et de l'apercu correspondant
function result_line_over(origin, id, flag){
	item_array = $(origin).adjacent('tr.lignes');
	for(var i = 0; i<item_array.length; i++){
		if($(item_array[i]).hasClassName('on'))	$(item_array[i]).removeClassName('on');
	}
	if( ! $(origin).hasClassName('on'))	{
		$(origin).addClassName('on');
		new Ajax.Request('./_ajax/apercu_recherche.php', {
			method: 'post',
			evalScripts: true,
			parameters: 'id='+id+'&flag='+flag,
			//onFailure: function(){ alert('Something went wrong...') },
			onSuccess: function(transport){
				//$('result_right').update(transport.responseText);
				$('apercu_result').update(transport.responseText);
			}
		});
	}
	this.onmouseout = function(){
		if( ! $(origin).hasClassName('on') ) $(origin).addClassName('on');
	}
	return false;
}

function show_fiche_right(){
	var fiche_right = $('fiche_right_container').innerHTML;
	$('fiche_right_container').innerHTML = "";
	$('prdRightContainer').innerHTML = fiche_right;
	$('prdRightContainer').show();

	if( show_fiche_right.arguments[0] ){
		$('showSidebar').show();
		$('sidebar').hide();
	}
	var bmw_rappel = new combo_box_research('bmw_rappel','bmw_rappel').set();
}

function hide_fiche_right(){
	$('showSidebar').hide();
	$('sidebar').show();
	$('prdRightContainer').innerHTML = "";
}
// FICHE PRODUITS
// gestion de l'affichage de la fiche detailleée des vehicules
function dsp_fiche_det(id, flag, type){
	new Ajax.Request('./_ajax/fiche_detail.php', {
		method: 'post',
		/*evalJS: true,*/
		//evalScripts: true,
		parameters: 'id='+id+'&flag='+flag+'&affichage='+type,
		//onFailure: function(){ alert('Something went wrong...') },
		onSuccess: function(transport){
			$('result').update(transport.responseText);

			show_fiche_right(true);
			onforms.set('form_rappel',fields, 'message_error');
		}
	});
	return false;
}

// gestion de l'affichage Pgae suivant/precedente de la fiche detaillee des vehicules
function dsp_page_result( id, flag ){
	new Ajax.Request('./_ajax/fiche_detail.php', {
		method: 'post',
		evalScripts: true,
		parameters: 'id='+id+'&flag='+flag,
		//onFailure: function(){ alert('Something went wrong...') },
		onSuccess: function(transport){
			$('result').update(transport.responseText);
			show_fiche_right(false);
		}
	});
return false;
}

// gestion de l'affichage des differents onglets dans la fiche detaillee
function show_onglet(origin, nb_links){
	var tab = new Array();
	tab["link_1"] = {
		onglet:'onglet_caract',
		css:'on1'
	};
	tab["link_2"] = {
		onglet:'onglet_concess',
		css:'on2'
	};
	tab["link_3"] = {
		onglet:'onglet_finance',
		css:'on3'
	};
	tab["link_4"] = {
		onglet:'onglet_bps',
		css:'on4'
	};

	if( ! $(origin).hasClassName(tab[origin].css) ){
		for(var i=1; i<(nb_links+1); i++){
			var div_id = 'link_'+i;
			var sel = tab[div_id];
			if(origin != div_id){
				if($(div_id).hasClassName(sel.css)){
					$(div_id).removeClassName(sel.css);
				}
				$(sel.onglet).className="ongoff";
			}
		}
		$(origin).addClassName(tab[origin].css);
		$(tab[origin].onglet).className="";
	}
	return false;
}

// FICHE CONCESSIONNAIRE
// gestion de l'affichage de la liste des vehicules vendus par un concessionaire
function dsp_fiche_concess(id){
	new Ajax.Request('_ajax/concession.php', {
		method: 'post',
		parameters: 'id='+id+'&coverflow=true',
		//onFailure: function(){ alert('Something went wrong...') },
		onSuccess: function(transport){
			$('result').update(transport.responseText);
			show_fiche_right(true);
		}
	});
	return false;
}

/* -------------------------------------------- */
function get_cover_ordered_result(order){
	new Ajax.Request('./_ajax/recherche.php', {
		method: 'post',
		evalScripts: true,
		parameters: order+'&coverflow=true',
		//onFailure: function(){ alert('Something went wrong...') },
		onSuccess: function(transport){
			if(transport.responseText.match('too many result')) $('too_many_result').show();
			else if(transport.responseText.match('no result')) $('no_result').show();
			else $('result').update(transport.responseText);
		}
	});
	return false;
}
// reaffichage par tri de la liste textuelle
function get_ordered_result(order){
	new Ajax.Request('./_ajax/recherche.php', {
		method: 'post',
		evalScripts: true,
		parameters: order,
		//onFailure: function(){ alert('Something went wrong...') },
		onSuccess: function(transport){
			if(transport.responseText.match('too many result')) $('too_many_result').show();
			else if(transport.responseText.match('no result')) $('no_result').show();
			else $('result').update(transport.responseText);
		}
	});
	return false;
}

// Liste vehicule concession reaffichage par tri de la liste textuelle
function get_c_ordered_result(order){
	new Ajax.Request('_ajax/concession.php', {
		method: 'post',
		evalScripts: true,
		parameters: order,
		//onFailure: function(){ alert('Something went wrong...') },
		onSuccess: function(transport){
			if(transport.responseText.match('too many result')) $('too_many_result').show();
			else if(transport.responseText.match('no result')) $('no_result').show();
			else $('result').update(transport.responseText);
		}
	});
	return false;
}

// Liste vehicule concession reaffichage par tri de la liste textuelle
function get_collab_ordered_result(order){
	new Ajax.Request('_ajax/collaborateur.php', {
		method: 'post',
		evalScripts: true,
		parameters: order,
		//onFailure: function(){ alert('Something went wrong...') },
		onSuccess: function(transport){
			if(transport.responseText.match('too many result')) $('too_many_result').show();
			else if(transport.responseText.match('no result')) $('no_result').show();
			else $('result').update(transport.responseText);
		}
	});
	return false;
}
// NAD : fontion generique : remplacer celles du haut
function getOrderedResult(order, page){
	switch (page){
		case 'concess':
			var getPage = '_ajax/concession.php';
			break;
		case 'collabo':
			var getPage = '_ajax/collaborateur.php';
			break;
		case 'marques':
			var getPage = '_ajax/marques.php';
			break;
		case 'cubeselect':
			var getPage = '_ajax/cubeselection.php';
			break;
		default :
			var getPage = '_ajax/marques.php';
			break;
	//		default : var getPage = '_ajax/recherche.php';
	}
	new Ajax.Request(getPage, {
		method: 'post',
		evalScripts: true,
		parameters: order,
		//onFailure: function(){ alert('Something went wrong...') },
		onSuccess: function(transport){
			if(transport.responseText.match('too many result')) $('too_many_result').show();
			else if(transport.responseText.match('no result')) $('no_result').show();
			else $('result').update(transport.responseText);
		}
	});
	return false;
}

/*----------------------------------------------------------------------------*/

var minigallery = {
	set : function(divId, nomnreDimages){
	}
}

function showFPImg(n){
	var pool = document.getElementById('visuel_grand').getElementsByTagName('img');
	for (var i=0;i<pool.length;i++){
		pool[i].style.display = 'none';
	}
	pool[n].style.display = '';
}
/*
######## VPM
*/
var vpmBMW = {
	constanteDeN : 0.31090128755364806866952789699571,
	nbMois : 12,
	teg : [],
	pourcentage : function() {
		var taux = 7.45;
		return taux - (taux* (this.constanteDeN/(this.nbMois/12))/100);
	//		return taux - (taux* this.constanteDeN /100);
	},
	get : function(duree, montantEmprunt){
		this.nbMois = duree;
		var t_mensuel = Math.floor( ((this.pourcentage()/12)/100)*1000000)/1000000;
		var R = ( 1 - Math.pow( (1+t_mensuel), -duree) ) / t_mensuel;
		R = Math.floor(R*1000000)/1000000;

		var VPM = ((montantEmprunt)/R);
//alert(t_mensuel+' | '+VPM+' | '+montantEmprunt+' | '+R)
		return VPM;
	},
	irr: function(startValue, monthlyFee, duree){
		var cout_de_depart = startValue;
		var remboursements = {};
		for (var i=0; i<duree; i++) {
			remboursements[i+1] = monthlyFee;
		}

		var tri = 0.1;// valeur de départ
		var tri_min = 0;
		var tri_max = 10;
		var solde = 0;
		var exit =1;

		do {
			exit++;
			solde = -cout_de_depart;
			for (var donnees in remboursements){
				solde += remboursements[donnees] / Math.pow((1+tri), donnees);
			}
			if(solde == 0) {
				break;
			} else if(solde < 0) {
				tri_max = tri;
				tri = (tri + tri_min)/2;
			} else {
				tri_min = tri;
				tri = (tri + tri_max)/2;
			}
		} while(Math.abs(solde) >= 0.01 && exit<200);

		return tri;
	},
	set : function(){

		document.getElementById("resultat").style.display = "block";

		var duree = this.nbMois;
		var apport = 0;
		var montantEmprunt = 0;
		var cout_mensuel =0;
		var vpm=0;
		var TRI=0;

		if( $('apportsave') )
			apport = document.getElementById("apportsave").value;

		if( $('dureesave') )
			duree = document.getElementById("dureesave").value;

		if( $('prixtot') && $('apportsave') ){
			montantEmprunt = parseFloat(document.getElementById("prixtot").value) - parseFloat(document.getElementById("apportsave").value);
		}
		vpm = Math.round(this.get(1, Math.round(montantEmprunt/100))*100)/100;
		cout_mensuel = (Math.round( (this.get(duree, montantEmprunt+vpm )*100) ) /100);

		if( $('emprunt') ) {
			var emprunt = cout_mensuel*duree;
			document.getElementById("emprunt").innerHTML = this.formatnumjs(emprunt, 2)+" €";
		}
		if( $('mensualite') )
			document.getElementById("mensualite").innerHTML = this.formatnumjs(cout_mensuel, 2)+" €";
		if( $('mensualite2') )
			document.getElementById("mensualite2").innerHTML = this.formatnumjs(cout_mensuel, 2)+" €";

		if( $('empruntduree') ) {
			document.getElementById("empruntduree").innerHTML = duree;
		}

		TRI = Math.round(this.irr(montantEmprunt, cout_mensuel, duree)*10000000)/10000000;
		if( $('teg') ) {
			document.getElementById("teg").innerHTML = Math.round(parseFloat(Math.pow(1+TRI, 12)-1)*10000)/100;
		}

		document.getElementById('assuanceDI').innerHTML = Math.round(0.0006*montantEmprunt);
		document.getElementById('assuanceICAR').innerHTML = Math.round(0.0013*montantEmprunt)+5;

/*		if( $('fraisdos') )
			document.getElementById("fraisdos").innerHTML = (montantEmprunt/100)+" €";
		if( $('credit') )
			document.getElementById("credit").innerHTML = (Math.round( ((cout_mensuel * duree)-montantEmprunt)*100) /100)+" €";
		if( $('totalcredit') )
			document.getElementById("totalcredit").innerHTML= ((Math.round(cout_mensuel * duree)*100)/100)+" €";*/
	},
	formatnumjs: function(valeur, decimal){
		var separateur = ' ';
		// formate un chiffre avec 'decimal' chiffres aprés la virgule et un separateur
		var deci	= Math.round( Math.pow(10,decimal)*(Math.abs(valeur)-Math.floor(Math.abs(valeur)))) ;
		var val	= Math.floor(Math.abs(valeur));
		if ((decimal==0)||(deci==Math.pow(10,decimal))) {
			val=Math.floor(Math.abs(valeur));
			deci=0;
		}
		var val_format=val+"";
		var nb=val_format.length;
		for (var i=1;i<4;i++) {
			if (val>=Math.pow(10,(3*i))) {
				val_format=val_format.substring(0,nb-(3*i))+separateur+val_format.substring(nb-(3*i));
			}
		}
		if (decimal>0) {
			var decim="";
			for (var j=0;j<(decimal-deci.toString().length);j++) {
				decim+="0";
			}
			deci=decim+deci.toString();
			val_format=val_format+"."+deci;
		}
		if (parseFloat(valeur)<0) {
			val_format="-"+val_format;
		}
		return val_format;
	}
}

function viewMappy(url){
	document.getElementById("mappyframe").src=url;
	$('pop_in_mappy').show();
	return false;
}

//|--------
function formatnumjs(valeur) {
	var decimal = 0;
	var separateur = ' ';
	// formate un chiffre avec 'decimal' chiffres aprés la virgule et un separateur
	var deci	= Math.round( Math.pow(10,decimal)*(Math.abs(valeur)-Math.floor(Math.abs(valeur)))) ;
	var val	= Math.floor(Math.abs(valeur));
	if ((decimal==0)||(deci==Math.pow(10,decimal))) {
		val=Math.floor(Math.abs(valeur));
		deci=0;
	}
	var val_format=val+"";
	var nb=val_format.length;
	for (var i=1;i<4;i++) {
		if (val>=Math.pow(10,(3*i))) {
			val_format=val_format.substring(0,nb-(3*i))+separateur+val_format.substring(nb-(3*i));
		}
	}
	if (decimal>0) {
		var decim="";
		for (var j=0;j<(decimal-deci.toString().length);j++) {
			decim+="0";
		}
		deci=decim+deci.toString();
		val_format=val_format+"."+deci;
	}
	if (parseFloat(valeur)<0) {
		val_format="-"+val_format;
	}
	return val_format;
}

//-----------------------------------------------
//	Création alerte & agent de recherche
//-----------------------------------------------

function dsp_login_form(){
	$('pop_in_register').show();
	return false;
}

var vcompare = {
	redir : 0,
	oDiv : null,
	num: 0,
	actualPosX : 0,
	actualPosY : 0,
	actualPosF : 0,
	pos: 0,
	goingToX: 0,
	goingToY: 0,
	goingToF: 0,
	timer: null,
	text :'',
	empty: function(id){
		new Ajax.Request('./_ajax/comparatif.php', {
			method: 'post',
			parameters: 'empty=true&sessid='+id,
			onSuccess: function(transport){
				document.getElementById("jsm"+id).style.display ='none';
				document.getElementById("jsc"+id).style.display ='none';
				document.getElementById("jse"+id).style.display ='none';
				//document.getElementById("jsv"+id).style.display ='none';
				document.getElementById("comparcount_1").innerHTML = transport.responseText;
				document.getElementById("comparcount_2").innerHTML = transport.responseText;

				if( transport.responseText == "(1)"){
					 $('caracteristiques').className = "tabgroup nb1"
					$('equipement').className = "tabgroup nb1"
				}else if( transport.responseText == "(2)"){
					$('caracteristiques').className = "tabgroup nb2"
					$('equipement').className = "tabgroup nb2"
				}else{
					$('caracteristiques').className = "tabgroup nb0"
					$('equipement').className = "tabgroup nb0"
				}

			}
		});
		return false;
	},
	select : function(id,flag){
		this.add(id,flag);
		return false;
	},
	direct : function(id,flag){
		this.redir = 1;
		this.add(id,flag);
		return false;
	},
	add : function(id,flag){
		new Ajax.Request('./_ajax/comparatif.php', {
			method: 'post',
			evalScripts: true,
			parameters: 'id='+id+'&flag='+flag,
			//onFailure: function(){ alert('Something went wrong...') },
			onSuccess: function(transport){
				if (transport.responseText!='exist') {
					if (parseFloat(transport.responseText)>3) {
						show_popin('alerte5max');
					} else {
						if (vcompare.redir==1) {
							setTimeout('document.location.href="comparatif_tableau.php"', 80);
						} else {
							vcompare.floatingdiv(transport.responseText);
						}
					}
				} else {
					if (vcompare.redir==1) {
						setTimeout('document.location.href="comparatif_tableau.php"', 80);
					} else {
						show_popin('alertexiste');
					}
				}
			}
		});
		return false;
	},
	floatingdiv : function(textvar){
		this.text = textvar;
		
		if( document.getElementById('member_menu_1').style.display == 'none' ){
			var container = document.getElementById("animatedCount2");
		}else{
			var container = document.getElementById("animatedCount1");
		}

		if( $('floatdiv') ){
			this.oDiv = $('floatdiv');
			$('floatdiv').style.display = "block";
		}else{
			this.oDiv = document.createElement('div');
			this.oDiv.id = 'floatdiv';
			container.appendChild(this.oDiv);
		}

		this.oDiv.innerHTML = '('+this.text+')';

		this.pos = 1;
		this.goingToX = 98;
		this.goingToY = 100;
		this.goingToF = 10;
		this.fSize = 14;
		this.newSize = 0;
		this.decrement = (this.fSize-this.goingToF)/(500/16);
		this.timer = setInterval('vcompare.slow()', 16);
		setTimeout(function(){ clearInterval(vcompare.timer); $('floatdiv').style.display = "none"}, 500);
	},
	slow : function(){

		this.newSize = ( this.fSize > this.goingToF )?(this.fSize-this.decrement):this.goingToF;

		this.oDiv.style.fontSize = this.newSize+"px";
		document.getElementById("comparcount_1").innerHTML = '('+this.text+')';
		document.getElementById("comparcount_2").innerHTML = '('+this.text+')';

		this.fSize = this.newSize;
	},
	getActual : function (type){
		if (type=='posx') return (parseFloat((isIE)? this.oDiv.currentStyle.left : getComputedStyle(this.oDiv, null).left));
		if (type=='posy') return (parseFloat((isIE)? this.oDiv.currentStyle.top : getComputedStyle(this.oDiv, null).top));
		if (type=='font') return (parseFloat((isIE)? this.oDiv.currentStyle.fontSize : getComputedStyle(this.oDiv, null).fontSize));
	}
};

function slideShowHomepage(id){
	var id;
	var element;
	var nb_elements;
	var current_el;
	var tab_images;
	var tab_puces;
	var puce_clickable;
	var interval;

	this.id = id;
	this.element = $(id);
	this.tab_puces = [];
	this.tab_images = [];
	this.puce_clickable = true;

	this.set = function(){

		if( this.element ){

			var uls = this.element.getElementsByTagName('ul');

			var lis = uls[0].getElementsByTagName('li');
			for( i=0; i<lis.length;i++){
				this.tab_images[i] = lis.item(i);
			}

			var lis = uls[1].getElementsByTagName('li');
			var obj = this;
			for( var i=0; i<lis.length;i++){
				this.tab_puces[i] = $(lis.item(i));
				this.tab_puces[i].onclick = function(e){

					var _i = (this.innerText)?this.innerText:this.textContent;

					if( obj.puce_clickable && _i != obj.current_el ){
						obj.puce_clickable = false;
						obj.selectElement(_i);
						setTimeout(function(){
							obj.puce_clickable = true;
						}, 500);
					}
				};
			}

			for(var i=0; i<this.tab_images.length;i++){

				if( i == 0 ){
					this.tab_images[i].style.display = "block";
				}else{
					this.tab_images[i].style.display = "none";
				}

				this.tab_images[i].onmouseout = function(){
					obj.setInterval();
				};

				this.tab_images[i].onmouseover = function(){
					obj.clearInterval();
				};
			}

			this.current_el = this.setActiv((this.tab_puces[0].innerText)?this.tab_puces[0].innerText:this.tab_puces[0].textContent);
			this.setInterval();

			return this;
		}else{
			return false;
		}
	};

	this.setActiv = function(c){
		for(var i = 0; i<this.tab_puces.length; i++){
			if( i == c ){
				this.tab_puces[i].className = "active";
			}else{
				this.tab_puces[i].className = "";
			}
		}
		return c;
	};

	this.selectElement = function(e){
		var selected = e;
		if( this.tab_images[selected] != "undefined" ){
			for(var i=0; i<this.tab_images.length;i++){
				if( i == selected ){
					this.animShow(this.tab_images[i]);
				}else{
					this.animHide(this.tab_images[i]);
				}
			}
			this.current_el = this.setActiv(selected);
		}
	};

	this.setInterval = function(){
		var obj = this;
		var newEl;
		this.interval = setInterval(function(){
			nb = (parseInt(obj.current_el)+1);
			newEl = (nb < obj.tab_puces.length)?nb:0;
			obj.current_el = newEl;
			obj.selectElement(obj.current_el);
		}, 5000);
		delete obj;
	};

	this.clearInterval = function(){
		clearInterval(this.interval);
	};

	this.animShow = function(el){
		$(el).setOpacity(0);
		$(el).setStyle({
			display: 'block'
		});
		new Effect.Opacity(
			el, {
				from: 0.0,
				to: 1.0,
				duration: 0.5
			}
		);
	};

	this.animHide = function(el){
		//el.style.display = "none";
		$(el).setOpacity(1);
		$(el).setStyle({
			display: 'block'
		});
		Effect.Fade(el, {
			duration:0.5
		});
	};

	return this;
}

/**
 *
 * @param id
 * @param mode
 */
function combo_box_research(id, mode){
	
	var id;
	var element;
	var label;
	var options;
	var mode;
	var clickable;

	this.id = id;
	this.mode = mode;
	this.clickable = true;

	this.set = function(){

		var obj = this;

		this.element = $(this.id);

		if( this.element ){

			var divs = this.element.getElementsByTagName('div');

			for(var i = 0; i<divs.length; i++){
				if( divs.item(i).className == "labeled" ){
					this.label = divs.item(i);
				}

				if( divs.item(i).className == "options" ){
					this.options = divs.item(i);
				}
			}

			if( !isIE6 ){
				$(this.options).slideUp({
					duration:0
				});
			}else{
				$(this.options).style.display = "none";
			}

			this.label.onclick = function(){
				if( obj.clickable ){

					obj.clickable = false;
					setTimeout(function(){
						obj.clickable = true;
					}, 500);

					obj.clickOnLabel(obj);
				}
			};

			var as = this.options.getElementsByTagName('a');

			for(var i=0; i<as.length; i++){
				as.item(i).onclick = function(e){

					if( obj.clickable ){

						/*if( obj.mode != "equip" ){

								obj.clickable = false;
								setTimeout(function(){
										obj.clickable = true;
								}, 500);

						}*/

						// Callback function
						if( obj.mode == "energie" ){
							//obj.updateLabel((this.innerText)?this.innerText:this.textContent);
							//obj.clickOnLabel(obj);
							select_radio(this.id, 'energie_e', 'energie_d', 'essence', 'diesel', 'energie_h', 'hybrid', 'energie');
						}else if( obj.mode == "transmission" ){
							//obj.updateLabel((this.innerText)?this.innerText:this.textContent);
							//obj.clickOnLabel(obj);
							select_radio(this.id, 'trans_man', 'trans_auto', 'manu', 'auto', '', '', 'trans');
						}else if( obj.mode == "equip" ){
							select_equip(this.id);
						}else if( obj.mode == 'bmw_rappel'){
							if( $(this.id).hasClassName('horaire') ){
								select_bmw_rappel(this.id, this.rel,['des_que_possible','un_autre_jour'], 'form4_semaine', false);
							}else if($(this).hasClassName('horaires_plage')){
								select_bmw_rappel(this.id, this.rel,['10h12h','15h15h','15h18h'], 'tranche_horaire', true);
							}else if($(this).hasClassName('civ')){
								select_bmw_rappel(this.id, this.rel,['civilite_1','civilite_2','civilite_3'], 'form4_civilite', true);
							}
						}else if( obj.mode == "dispo"){
							obj.updateLabel(this.innerHTML);
							$('semainedispo').value = obj.label.innerHTML;
							obj.clickOnLabel(obj);
						}else if( obj.mode == "dispob"){
							obj.updateLabel(this.innerHTML);
							$('form13_semaine').value = obj.label.innerHTML;
							obj.clickOnLabel(obj);
						}else if( obj.mode == "tranche"){
							obj.updateLabel(this.innerHTML);
							$('tranche').value = obj.label.innerHTML;
							obj.clickOnLabel(obj);
						}else if( obj.mode == "trancheb"){
							obj.updateLabel(this.innerHTML);
							$('trancheb').value = obj.label.innerHTML;
							obj.clickOnLabel(obj);
						}

					}
				};

			}

			return this;
		}else{
			return false;
		}

	};

	this.updateLabel = function(txt){
		this.label.innerHTML = txt;
	};

	this.clickOnLabel = function(obj){
		if( $(obj.label).hasClassName('clicked') ){
			$(obj.label).removeClassName("clicked");
			obj.hideOptions();
		}else{
			$(obj.label).addClassName("clicked");
			obj.showOptions();
		}
	};

	this.hideOptions = function(){
		if( !isIE6 ){
			$(this.options).slideUp({
				duration:0.5
			});
		}else{
			$(this.options).style.display = "none";
		}

	};

	this.showOptions = function(){
		if( !isIE6){
			$(this.options).slideDown({
				duration:0.5
			});
		}else{
			$(this.options).style.display = "block";
		}

	};

	return this;
}

/**
 *
 * @param el
 * @param place
 */
function switch_background(el, place){
	if( place != '' ){
		el.style.backgroundPosition = place;
	}else{
		if( el.value == "" ){
			el.style.backgroundPosition = "top";
		}else{
			el.style.backgroundPosition = "bottom";
		}
	}
}

/**
 *
 * @param e
 * @param obj
 */
function check_image_position(e, obj){
	var toInsert;
	var win = {
		windowSize:(window.innerHeight)?window.innerHeight:document.documentElement.offsetHeight,
		mouseY:e.clientY,
		limitY:e.clientY+257
	};
	var pos ={ x:0,y:0};

	pos.x = $(obj).offsetLeft;
	pos.y = $(obj).offsetTop;

	if( obj ){

		var divs = obj.getElementsByTagName('div');

		for( var i = 0;i<divs.length;i++){
			if( $(divs.item(i)).hasClassName('image') ){
				toInsert = divs.item(i);
				if( win.limitY > win.windowSize ){
					$('floatImageContainer1').addClassName('bottom_align');
					$('floatImageContainer1').style.top = (pos.y-255)+"px";
					$('floatImageContainer1').style.left = (pos.x+200)+"px";
				}else{
					$('floatImageContainer1').style.top = (pos.y+32)+"px";
					$('floatImageContainer1').style.left = (pos.x+200)+"px";
					$('floatImageContainer1').removeClassName('bottom_align');
				}
			}
		}

	}
	if( toInsert != undefined ){
		$('floatImageContainer1').innerHTML = toInsert.innerHTML;
		$('floatImageContainer1').onclick = obj.onclick;
		$('floatImageContainer1').style.display = "block";
	}

}

/**
 *
 * @param id
 * @param el
 * @returns {result_image_segment}
 */
function result_image_segment(id, el){

	var element;
	var btn;
	var id;
	var vignettes;
	var step;
	var last;

	this.step = 5*3;
	this.id = id;
	this.btn = $(id);
	this.element = $(el);
	this.vignettes = [];
	this.nbVignettesOn = 0;

	this.set = function(){
		if( this.element && this.btn ){

			this.newHeight = 0;

			var obj = this;
			if( this.element.id == "resultats_liste"){
				var lis = this.element.getElementsByTagName('a');
			}else{
				var lis = this.element.getElementsByTagName('a');
			}

			for( var i=0; i<lis.length;i++){
				if( $(lis.item(i)).hasClassName('lignes') ){
					this.vignettes.push(lis.item(i));
				}
			}

			this.showVignettes(this.step);

			if( this.nbVignettesOn >= this.vignettes.length){
				this.hideBtn(true);
			}

			this.btn.onclick = function(e){
				obj.showVignettes(obj.nbVignettesOn+obj.step);
				new Effect.ScrollTo($(obj.last), {
					duration:'0.5'
				});
			};

		}
	};

	this.showVignettes = function(nb){
		for(var i=0; i<this.vignettes.length; i++){
			if( i < nb ){
				this.vignettes[i].style.display = "block";
				this.last = this.vignettes[i];
			}else{
				this.vignettes[i].style.display = "none";
			}

		}

		this.nbVignettesOn = nb;

		if( this.nbVignettesOn >= this.vignettes.length){
			this.hideBtn(true);
		}

		var nbRestants = this.vignettes.length-this.nbVignettesOn;

		if( nbRestants < this.step){
			if( nbRestants > 1 ){
				this.btn.innerHTML = "AFFICHER LES "+nbRestants+" PROCHAINS RÉSULTATS";
			}else{
				//this.btn.innerHTML = "AFFICHER LE "+nbRestants+" PROCHAIN RÉSULTAT";
				this.btn.innerHTML = "AFFICHER LE PROCHAIN RÉSULTAT";
			}
		}
	};

	this.hideBtn = function(s){
		if( s ){
			this.btn.style.display = "none";
		}else{
			this.btn.style.display = "block";
		}

	};

	return this;
}

function select_bmw_rappel(id, value, id_off, input_name,no_hide){

	$(input_name).value = "";
	if( !no_hide ) $('horaires_plage').hide();
	if( $(id).hasClassName('checkbox_on')){
		switch_class(id, 'checkbox_on', 'checkbox');

	}else{
		switch_class(id, 'checkbox', 'checkbox_on');
		$(input_name).value = value;
		if( id == 'un_autre_jour' ){
			if( !no_hide ) $('horaires_plage').show();
		}
	}

	for( var i = 0; i<id_off.length;i++){
		if( id_off[i] != id ) switch_class(id_off[i], 'checkbox_on', 'checkbox');
	}

}

function btn_radio(id, value, id_off, input_name){

	$(input_name).value = "";
	if( $(id).hasClassName('checkbox_on')){
		switch_class(id, 'checkbox_on', 'checkbox');

	}else{
		switch_class(id, 'checkbox', 'checkbox_on');
		$(input_name).value = value;
	}

	for( var i = 0; i<id_off.length;i++){
		if( id_off[i] != id ) switch_class(id_off[i], 'checkbox_on', 'checkbox');
	}

}


function slideShowPlayable(id){
	var element;
	var images;
	var current;
	var interval;
	var links;
	var paused;
	var controls;
	var obj = this;
	this.element = $(id);
	this.images = [];
	this.links = [];
	this.current = 0;
	this.paused = false;

	this.set = function(){

		this.images = this.element.getElementsByTagName('img');

		for(var i=0; i<this.images.length;i++){
			this.hideImage(this.images[i]);
		}

		var uls = this.element.getElementsByTagName('ul');

		for(var i=0; i<uls.length;i++){
			if( $(uls[i]).hasClassName('controls') ){
				this.controls = uls[i];
			}
		}

		this.goTo(0);
		this.setInterval();
		if( this.controls ){
			this.hideControls();
			this.element.onmouseover = function(){
				obj.showControls();
			};

			this.element.onmouseout = function(){
				obj.hideControls();
			};
		}

		var as = document.getElementById('fullscreen_img_container').getElementsByTagName('a');

/*		for(var i=0; i<as.length;i++){
			//as.item(i).onclick = function(){ obj.hideFullscreen(); };
		}*/

		this.links = this.element.getElementsByTagName('a');

		for(var i = 0; i<this.links.length; i++){
			if( $(this.links[i]).hasClassName('prev') ){
				this.links[i].onclick = function(){
					obj.prev(true);
				};
			}else if( $(this.links[i]).hasClassName('pause') ){
				this.links[i].title = 'pause';
				this.links[i].onclick = function(){
					if(this.hasClassName('play')) {
						this.title = 'pause';
						switch_class(this, 'play', 'pause');
					} else {
						this.title = 'play';
						switch_class(this, 'pause', 'play');
					}
					obj.pause();
				};
			}else if( $(this.links[i]).hasClassName('fullscreen') ){
				this.links[i].onclick = function(){
					obj.fullscreen();
				};
			}else if( $(this.links[i]).hasClassName('next') ){
				this.links[i].onclick = function(){
					obj.next(true);
				};
			}
		}
	};

	this.showControls = function(){
		this.controls.style.display = "block";
	};

	this.hideControls = function(){
		this.controls.style.display = "none";
	};

	this.setInterval = function(){
		var obj = this;
		this.interval = setInterval(function(){
			obj.next()
		}, 4000);
	}

	this.clearInterval = function(){
		clearInterval(this.interval);
	};

	this.restartInterval = function(){
		obj.clearInterval();
		obj.setInterval();
	};

	this.pause = function(){
		if( this.paused ){
			this.setInterval();
			this.paused = false;
		}else{
			this.clearInterval();
			this.paused = true;
		}
	};

	this.fullscreen = function(){
		var img = $(this.images[this.current]).alt;
		if( img ){
			$('fullscreen_img').innerHTML = '<img src="'+img+'"></img>';
			$('popinFullscreenContent').innerHTML = $('fullscreen_img_container').innerHTML;
			$('popinFullscreen').style.display = "block";
			this.clearInterval();
		}
	};

	this.hideFullscreen = function(){
		$('popinFullscreen').style.display = "none";
		this.setInterval();
	}

	this.prev = function(){
		if( this.prev.arguments[0] ) this.restartInterval();

		if( (this.current) == 0 ){
			this.current = this.images.length-1;
		}else{
			this.current--;
		}
		this.goTo(this.current);
	};

	this.next = function(){
		if( this.next.arguments[0] ) this.restartInterval();

		if( (this.current) == this.images.length-1 ){
			this.current = 0;
		}else{
			this.current++;
		}
		this.goTo(this.current);
	};

	this.goTo = function(n){
		for( var i = 0;i<this.images.length;i++){
			if( i == n ){
				this.showImage(this.images[i]);

			}else{
				this.hideImage(this.images[i]);
			}
		}
	};

	this.showImage = function(el){
		el.style.display = "inline-block";
	};

	this.hideImage = function(el){
		el.style.display = "none";
	};

}

function close_popin_fullscreen(){
	$('popinFullscreen').style.display = "none";
}
function hide_image_detail(obj){
	if( $('floatImageContainer') ){
		$('floatImageContainer').style.display = "none";
	}else if( $('floatImageContainer1') ){
		$('floatImageContainer1').style.display = "none";
	}
}

function show_image_detail(obj){
	var toInsert;
	var divs = obj.getElementsByTagName('div');
	var pos ={ x:0,y:0};
	var lastpos;
	var firstpos;
	var yPos;
	var xPos;

	pos.x = $(obj).offsetLeft;
	pos.y = $(obj).offsetTop;

	for( var i = 0; i<divs.length; i++){
		if( divs.item(i).className == 'image'){
			toInsert = divs.item(i);
		}
	}

	var vs = obj.parentNode.getElementsByTagName('a');

	for( var i = 0; i<vs.length; i++){
		if( i == 0 ){
			firstpos = $(vs.item(i)).offsetTop;
		}
		if( vs.item(i).style.display == "block"){
			lastpos = $(vs.item(i)).offsetTop;
		}
	}

	if( firstpos == pos.y ){
		// top
		yPos = -1;

	}else if( pos.y >= lastpos ){
		// bottom
		yPos = -199;

	}else{
		// middle
		yPos = -100;
	}

	if( pos.x < 200 ){
		// left
		xPos = -1;
	}else if( pos.x > 400 ){
		// right
		xPos = -83;
	}else{
		// center
		xPos = -40;
	}
	try{
		if( toInsert != undefined ){
			$('floatImageContainer').innerHTML = toInsert.innerHTML;
			$('floatImageContainer').style.top = (pos.y+yPos)+"px";
			$('floatImageContainer').style.left = (pos.x+xPos)+"px";
			$('floatImageContainer').style.display = "block";
			$('floatImageContainer').onclick = obj.onclick;
		}
	}catch(e){}

}

function keyPressDepdep(obj){
	var val = obj.value;
	$('depdep2').value = val;

	if(val.length>=2) {
		var arr = val.split(',');
		for(var i=0;i< arr.length;i++) {
			if (arr[i].length==2 || arr[i].length==5)
				refreshCountResult('advanced');
		}
	}
}
