// TOOLBOX (fonctions communes a l'ensemble des fonctionnnalites et parties du site)

//|---------------------------| 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];
			$(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: $(show_popin.arguments[0]).show(); break;
	}
	return false;
}

// MOTEUR DE RECHERCHE : GESTION DU FORMULAIRE DE RECHERCHE 
var handleCars = {
	modelDiv : [],
	carosDiv : [],
	serie_tab : [],
	carross_tab : [],
	img_carros :[],
	saveImage4reload : '<img src="_img/research/serie1_3portes.png" />',
	actualSelectedModel : 's1',
	actual_serie : 'c1',
	set: function(){

		this.serie_tab['s1'] = {css: 'serie1', value:'1',	carros_cl: 'portes3',	carros_id: 'c7', 	img: '_img/research/serie1_3portes.png'};
		this.serie_tab['s2'] = {css: 'serie3',	value:'3',	carros_cl: 'berline',	carros_id: 'c1', 	img: '_img/research/serie3_berline.png'};
		this.serie_tab['s3'] = {css: 'serie5', value:'5',	carros_cl: 'berline',	carros_id: 'c1', 	img: '_img/research/serie5_berline.png'};
		this.serie_tab['s4'] = {css: 'serie6', value:'6',	carros_cl: 'cabriolet',	carros_id: 'c3', 	img: '_img/research/serie6_cabriolet.png'};
		this.serie_tab['s5'] = {css: 'serie7', value:'7',	carros_cl: 'berline',	carros_id: 'c1', 	img: '_img/research/serie7_berline.png'};
		this.serie_tab['s6'] = {css: 'serieM', value:'M',	carros_cl: 'coupe',		carros_id: 'c2',	img: '_img/research/serieM_coupe.png'};
		this.serie_tab['s7'] = {css: 'serieX3',value:'X3',	carros_cl: 'quatreX4',	carros_id: 'c6', 	img: '_img/research/serieX3.png'};
		this.serie_tab['s8'] = {css: 'serieX5',value:'X5',	carros_cl: 'quatreX4',	carros_id: 'c6', 	img: '_img/research/serieX5.png'};
		this.serie_tab['s9'] = {css: 'serieX6',value:'X6',	carros_cl: 'quatreX4',	carros_id: 'c6', 	img: '_img/research/serieX6.png'};
		this.serie_tab['s10'] = {css: 'serieZ',value:'Z',	carros_cl: 'coupe',		carros_id: 'c2',	img: '_img/research/serieZ_coupe.png'};
		this.serie_tab['s11'] = {css: 'serieX1',value:'X1',carros_cl: 'quatreX4',	carros_id: 'c6',	img: '_img/research/serieX1.png'};

		this.carross_tab["c1"] = {css:'berline', 	value:'Berline'};
		this.carross_tab["c2"] = {css:'coupe', 	value:'CoupÃ©'};
		this.carross_tab["c3"] = {css:'cabriolet',value:'Cabriolet'};
		this.carross_tab["c4"] = {css:'touring', 	value:'Touring'};
		this.carross_tab["c5"] = {css:'roadster', value:'Roadster'};
		this.carross_tab["c6"] = {css:'quatreX4', value:'4X4'};
		this.carross_tab["c7"] = {css:'portes3', 	value:'3P'};
		this.carross_tab["c8"] = {css:'portes5', 	value:'5P'};
		this.carross_tab["c9"] = {css:'granturismo', 	value:'Gran Turismo'};

		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'};
		this.img_carros["s7"] = {"c6":'_img/research/serieX3.png'};
		this.img_carros["s8"] = {"c6":'_img/research/serieX5.png'};
		this.img_carros["s9"] = {"c6": '_img/research/serieX6.png'};
		this.img_carros["s10"] = {"c2":'_img/research/serieZ_coupe.png', "c5":'_img/research/serieZ_roadster.png'};
		this.img_carros["s11"] = {"c6": '_img/research/serieX1.png'};

		// 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);
				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;
		}
	},
	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;
		var carros_id	= this.serie_tab[id].carros_id;

		// 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);

			// carrosserie : m.a.j du lien
			if( ! $(carros_id).hasClassName('on') ){
				switch_class(carros_id, 'light', 'on');
				$('model').value = this.carross_tab[carros_id].value;
			}
			// deselection de la serie initialement selectionnee
			for (var i=1;i<12;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<10;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);
					}
				}
			}
		}
		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<12;i++){
			var serie_id = 's'+i;
			if($(serie_id).hasClassName('on')){
				current_serie = serie_id;
			}
		}
		// 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;
				for (var i=1;i<10;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);
						}
					}
				}
			}
		}
		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');
				}
			}
			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');
				}
			}
			break;
	}
	return false;
}
// gestion des cases a cocher 
function select_radio(id, id_name1, id_name2, value1, value2, input_name){
	var dual_id;
	var value;
	if(id == id_name1){
			dual_id= id_name2;
			value = value1;
	}else{
			dual_id= id_name1;
			value = value2;
	}
	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('radio_on')){
		switch_class(id, 'radio_on', 'radio');
		$(input_name).value = '';
	}else{
		document.getElementById(id).parentNode.className +=" on";
		switch_class(id, 'radio', 'radio_on');
		$(input_name).value = value;
		if($(dual_id).hasClassName('radio_on')){
			switch_class(dual_id, 'radio_on', 'radio');
		}
	}
	return false;
}

//	gestion des finitions
function select_finition(id){
	$('finition').value = $(id).value;
	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";
	}
	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();
		new Effect.SlideDown('more_details', {duration : 0.3});
	}else{
		$('show_more_criteria').show();
		$('show_less_criteria').hide();
		new 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){
	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();
				$('top').hide();
/*				$('topclick1').hide();
				$('topclick2').hide();
				$('topclick3').hide();
				$('topclick4').hide();
				$('topclick5').hide();*/

				$('result').show();

			}
		}
	});
	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);
		}
	});
	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){
			$('nb_result').update(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_collaborateurs.php';
	} else if (id=='selection_bmw'){
		document.location.href = 'selection_bmw.php';
	} else if (id=='cover'){
		dsp_coverflow();
	} 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 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 = '&nbsp;' //
	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;
}
//|-------/
// APPEL de la page resultat coverflow.
function dsp_coverflow(){
	new Ajax.Request('./_ajax/coverflow.php', {
		method: 'post',
		evalJS: true,
		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);
		}
	});
	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;
}

// 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',
		evalScripts: true,
		parameters: 'id='+id+'&flag='+flag+'&affichage='+type,
		//onFailure: function(){ alert('Something went wrong...') },
		onSuccess: function(transport){
			$('result').update(transport.responseText);
		}
	});
	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);
		}
	});
	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,
		//onFailure: function(){ alert('Something went wrong...') },
		onSuccess: function(transport){
			$('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';
		case 'collabo': var getPage = '_ajax/collaborateur.php';
		case 'marques': var getPage = '_ajax/marques.php';
		default : var getPage = '_ajax/marques.php';
//		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 = {
	pool : [], nbImage : 0,
	gallery : null,
	oParent : null,
	elementW : 0, actualPos : 0, parentW : 0, galleryW : 0, goingTo : 0, timer: null, pos: 0, num : 0,
	fleches : {},
	set : function(divId, nomnreDimages){
		this.num = 0;
		this.nbImage = nomnreDimages;
		this.gallery = document.getElementById(divId);
		this.fleches = { 'previous' : document.getElementById("previous"), 'next' : document.getElementById("next") };

		this.pool = this.gallery.getElementsByTagName('a');
		if (this.pool.length>0) {
			var elementMargin = parseFloat((window.ActiveXObject)? this.pool[0].currentStyle.marginLeft : getComputedStyle(this.pool[0], null).marginLeft);
			this.elementW = this.pool[0].offsetWidth+elementMargin;

			for(var i=0;i<this.pool.length; i++){//positionnement <a>
				this.pool[i].style.left = ( i<1 ? 0 : (parseFloat(this.pool[i-1].style.left)+this.elementW ) ) +"px";
			}

			this.oParent = this.gallery.parentNode;
			this.parentW = this.oParent.offsetWidth;
			this.galleryW = this.pool.length*this.elementW;

			this.fleches["previous"].onclick = function(){ minigallery.prev(); }
			this.fleches["next"].onclick = function(){ minigallery.next(); }

			this.flechesDisplay("previous", "gauche_off");
			if (this.pool.length <= this.nbImage) {
				this.flechesDisplay("next", "droite_off");
			}

			if (window.addEventListener) {
				this.gallery.addEventListener('DOMMouseScroll', weel, false);//moz
				this.gallery.addEventListener('mousewheel', weel, false);//safari
			} else {// IE
				this.gallery.onmousewheel = function () {
					if (event.wheelDelta>0) minigallery.prev();
					else minigallery.next();
				}
			}
		}
		function weel(e) {
			var raw = e.detail ? e.detail : e.wheelDelta* -1;
			if (raw>0) minigallery.next();
			else minigallery.prev();
		}
	},
	next : function() {
		if ( (Math.abs(this.num) + this.nbImage) <this.pool.length) {
			this.flechesDisplay("previous", "gauche");
			this.num--;
			if ( (Math.abs(this.num) + this.nbImage) == this.pool.length) {
				this.flechesDisplay("next", "droite_off");
			}

			this.actualPos = minigallery.getActualPos();
			this.pos = 1;
			this.goingTo = this.num*this.elementW;
			if (this.timer==null) this.timer = setInterval('minigallery.slow()', 30);
		}
	},
	prev : function() {
		if ( this.num<0) {
			this.flechesDisplay("next", "droite");
			this.num++;
			if ( this.num == 0) {
				this.flechesDisplay("previous", "gauche_off");
			}

			this.actualPos = minigallery.getActualPos();
			this.pos = -1;
			this.goingTo = this.num*this.elementW;
			if (this.timer==null) this.timer = setInterval('minigallery.slow()', 30);
		}
	},
	slow : function(){
		this.actualPos = minigallery.getActualPos();
		var dx = ((this.pos*this.goingTo) - (this.pos*this.actualPos));
		var doMath = this.actualPos+((this.pos)*(0.2*dx));
		doMath = ((this.pos)>0 ? Math.floor(doMath) : Math.ceil(doMath));

		if ((this.pos*this.actualPos)>(this.pos*this.goingTo)) {
			this.gallery.style.left = doMath+"px";
		} else {
			clearInterval(this.timer);
			this.timer = null;
		}
	},
	flechesDisplay : function(o,set){
		var img = '<img src="_img/result_list/fleche_'+set+'.gif">';
		if (this.fleches[o].innerHTML!=img) {
			this.fleches[o].innerHTML = img;
		}
	},
	getActualPos : function (){
		return (parseFloat((window.ActiveXObject)? this.gallery.currentStyle.left : getComputedStyle(this.gallery, null).left));
	}
}

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,
	mensualites : 12,
	teg : [],
	pourcentage : function(agevehicule) {
		var taux=0;
		if (agevehicule>=5)	taux	= 11.65;
		if (agevehicule<5)	taux	= 9.65;
		return taux - (taux* (this.constanteDeN/(this.mensualites/12))/100);
//		return taux - (taux* this.constanteDeN /100);
	},
	get : function(mensualite, emprunt, agevehicule){
		this.mensualites = mensualite;
		var t_mensuel = (this.pourcentage(agevehicule)/12)/100;
		var R = ( 1 - Math.pow( (1+t_mensuel), -mensualite) ) / t_mensuel;
		var VPM = ((emprunt)/R);
		return VPM;
	},
	set : function(){

		document.getElementById("resultat").style.display = "block";

		this.teg[12] = { p5Pro:'13.52', p5Part:'14.39', m5Pro:'11.52', m5Part:'12.15'};
		this.teg[13] = { p5Pro:'13.39', p5Part:'14.25', m5Pro:'11.39', m5Part:'12'};
		this.teg[14] = { p5Pro:'13.28', p5Part:'14.12', m5Pro:'11.27', m5Part:'11.87'};
		this.teg[15] = { p5Pro:'13.18', p5Part:'14.01', m5Pro:'11.17', m5Part:'11.76'};
		this.teg[16] = { p5Pro:'13.09', p5Part:'13.91', m5Pro:'11.09', m5Part:'11.67'};
		this.teg[17] = { p5Pro:'13.01', p5Part:'13.82', m5Pro:'11.01', m5Part:'11.58'};
		this.teg[18] = { p5Pro:'12.95', p5Part:'13.74', m5Pro:'10.94', m5Part:'11.5'};
		this.teg[19] = { p5Pro:'12.88', p5Part:'13.67', m5Pro:'10.88', m5Part:'11.43'};
		this.teg[20] = { p5Pro:'12.83', p5Part:'13.61', m5Pro:'10.82', m5Part:'11.37'};
		this.teg[21] = { p5Pro:'12.77', p5Part:'13.55', m5Pro:'10.77', m5Part:'11.32'};
		this.teg[22] = { p5Pro:'12.73', p5Part:'13.5', m5Pro:'10.72', m5Part:'11.26'};
		this.teg[23] = { p5Pro:'12.68', p5Part:'13.45', m5Pro:'10.68', m5Part:'11.22'};
		this.teg[24] = { p5Pro:'12.64', p5Part:'13.4', m5Pro:'10.64', m5Part:'11.17'};
		this.teg[25] = { p5Pro:'12.61', p5Part:'13.36', m5Pro:'10.6', m5Part:'11.13'};
		this.teg[26] = { p5Pro:'12.57', p5Part:'13.32', m5Pro:'10.57', m5Part:'11.09'};
		this.teg[27] = { p5Pro:'12.54', p5Part:'13.29', m5Pro:'10.54', m5Part:'11.06'};
		this.teg[28] = { p5Pro:'12.51', p5Part:'13.26', m5Pro:'10.51', m5Part:'11.03'};
		this.teg[29] = { p5Pro:'12.49', p5Part:'13.23', m5Pro:'10.48', m5Part:'11'};
		this.teg[30] = { p5Pro:'12.46', p5Part:'13.2', 	m5Pro:'10.45', m5Part:'10.97'};
		this.teg[31] = { p5Pro:'12.44', p5Part:'13.17', m5Pro:'10.43', m5Part:'10.94'};
		this.teg[32] = { p5Pro:'12.41', p5Part:'13.14', m5Pro:'10.41', m5Part:'10.92'};
		this.teg[33] = { p5Pro:'12.39', p5Part:'13.12', m5Pro:'10.39', m5Part:'10.89'};
		this.teg[34] = { p5Pro:'12.37', p5Part:'13.1', 	m5Pro:'10.37', m5Part:'10.87'};
		this.teg[35] = { p5Pro:'12.35', p5Part:'13.08', m5Pro:'10.35', m5Part:'10.85'};
		this.teg[36] = { p5Pro:'12.34', p5Part:'13.06', m5Pro:'10.33', m5Part:'10.83'};
		this.teg[37] = { p5Pro:'12.32', p5Part:'13.04', m5Pro:'10.31', m5Part:'10.81'};
		this.teg[38] = { p5Pro:'12.3', 	p5Part:'13.02', m5Pro:'10.3', m5Part:'10.8'};
		this.teg[39] = { p5Pro:'12.29', p5Part:'13', 	m5Pro:'10.28', m5Part:'10.78'};
		this.teg[40] = { p5Pro:'12.27', p5Part:'12.99', m5Pro:'10.27', m5Part:'10.76'};
		this.teg[41] = { p5Pro:'12.26', p5Part:'12.97', m5Pro:'10.25', m5Part:'10.75'};
		this.teg[42] = { p5Pro:'12.25', p5Part:'12.96', m5Pro:'10.24', m5Part:'10.73'};
		this.teg[43] = { p5Pro:'12.23', p5Part:'12.94', m5Pro:'10.23', m5Part:'10.72'};
		this.teg[44] = { p5Pro:'12.22', p5Part:'12.93', m5Pro:'10.21', m5Part:'10.71'};
		this.teg[45] = { p5Pro:'12.21', p5Part:'12.92', m5Pro:'10.2', m5Part:'10.69'};
		this.teg[46] = { p5Pro:'12.2', 	p5Part:'12.9', 	m5Pro:'10.19', m5Part:'10.68'};
		this.teg[47] = { p5Pro:'12.19', p5Part:'12.89', m5Pro:'10.18', m5Part:'10.67'};
		this.teg[48] = { p5Pro:'12.18', p5Part:'12.88', m5Pro:'10.17', m5Part:'10.66'};
		this.teg[49] = { p5Pro:'12.17', p5Part:'12.87', m5Pro:'10.16', m5Part:'10.65'};
		this.teg[50] = { p5Pro:'12.16', p5Part:'12.86', m5Pro:'10.15', m5Part:'10.64'};
		this.teg[51] = { p5Pro:'12.15', p5Part:'12.85', m5Pro:'10.14', m5Part:'10.63'};
		this.teg[52] = { p5Pro:'12.14', p5Part:'12.84', m5Pro:'10.13', m5Part:'10.62'};
		this.teg[53] = { p5Pro:'12.13', p5Part:'12.83', m5Pro:'10.13', m5Part:'10.61'};
		this.teg[54] = { p5Pro:'12.13', p5Part:'12.82', m5Pro:'10.12', m5Part:'10.6'};
		this.teg[55] = { p5Pro:'12.12', p5Part:'12.81', m5Pro:'10.11', m5Part:'10.59'};
		this.teg[56] = { p5Pro:'12.11', p5Part:'12.81', m5Pro:'10.1', m5Part:'10.58'};
		this.teg[57] = { p5Pro:'12.1', 	p5Part:'12.8', 	m5Pro:'10.1', m5Part:'10.58'};
		this.teg[58] = { p5Pro:'12.1', 	p5Part:'12.79', m5Pro:'10.09', m5Part:'10.57'};
		this.teg[59] = { p5Pro:'12.09', p5Part:'12.78', m5Pro:'10.08', m5Part:'10.56'};
		this.teg[60] = { p5Pro:'12.08', p5Part:'12.78', m5Pro:'10.08', m5Part:'10.55'};

		var mensualite = document.getElementById("dureesave").value;
		var emprunt = parseFloat(document.getElementById("prixtot").value) - parseFloat(document.getElementById("apportsave").value);
		var agevehicule = document.getElementById("agevehicule").value;

		document.getElementById("apportperso").innerHTML	= document.getElementById("apportsave").value+" â‚¬";
		document.getElementById("emprunt").innerHTML			= emprunt+" â‚¬";

		var cout_mensuel = (Math.round( (this.get(mensualite, emprunt+this.get(1, emprunt/100, agevehicule), agevehicule )*100) ) /100);

		document.getElementById("montantmensualite").innerHTML	= cout_mensuel+" â‚¬";
		document.getElementById("montantmensualite2").innerHTML	= cout_mensuel+" â‚¬";
		document.getElementById("mensualite").innerHTML				= mensualite;
		if(agevehicule>4){
			document.getElementById("teg1").innerHTML		= this.teg[mensualite].p5Pro;
			document.getElementById("teg2").innerHTML		= this.teg[mensualite].p5Part;
		}else{
			document.getElementById("teg1").innerHTML		= this.teg[mensualite].m5Pro;
			document.getElementById("teg2").innerHTML		= this.teg[mensualite].m5Part;
		}
		document.getElementById("fraisdos").innerHTML	= (emprunt/100)+" â‚¬";
		document.getElementById("credit").innerHTML		= (Math.round( ((cout_mensuel * mensualite)-emprunt)*100) /100)+" â‚¬";
		document.getElementById("totalcredit").innerHTML= ((Math.round(cout_mensuel * mensualite)*100)/100)+" â‚¬";
	}
}

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;
			}
		});
		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)>5) {
						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;
		var container = document.getElementById("innercontainer");
		this.oDiv = document.createElement('div');
		this.oDiv.id = 'floatdiv';
		this.oDiv.innerHTML = document.getElementById("texte").getElementsByTagName('H1')[0].innerHTML;
		container.appendChild(this.oDiv);

		this.pos = 1;
		this.goingToX = 98;this.goingToY = 100;this.goingToF = 4;
		this.timer = setInterval('vcompare.slow()', 40);
	},
	slow : function(){
		this.actualPosX = vcompare.getActual('posx');
		this.actualPosY = vcompare.getActual('posy');
		this.actualPosF = vcompare.getActual('font');
		var dp = ((this.pos*this.goingToX) - (this.pos*this.actualPosX));
		var dpY = ((this.pos*this.goingToY) - (this.pos*this.actualPosY));
		var dpF = ((this.pos*this.goingToF) - (this.pos*this.actualPosF));
		var doMathX = this.actualPosX+((this.pos)*(0.2*dp));
		var doMathY = this.actualPosY+((this.pos)*(0.2*dpY));
		var doMathF = this.actualPosF+((this.pos)*(0.2*dpF));

		doMathX = ( (this.pos>0) ? Math.floor(doMathX) : Math.ceil(doMathX));

		if ((this.pos*this.actualPosX)>(this.pos*this.goingToX)) {
			this.oDiv.style.left = doMathX+"px";
			this.oDiv.style.top = doMathY+"px";
			this.oDiv.style.fontSize = doMathF+"px";
		} else {
			clearInterval(this.timer);
			this.oDiv.style.display = 'none';
			document.getElementById("comparcount_1").innerHTML = '('+this.text+')';
			document.getElementById("comparcount_2").innerHTML = '('+this.text+')';
			this.timer = null;
		}
	},
	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));
	}
}