//various fonctions for ve_search_engine by D.PECCHIOLI
//need to import js/tools/list.js first !

var IE = document.all?true:false;

if (!IE) document.captureEvents(Event.MOUSEMOVE);
document.onmouseup = MyMouseUp;
	
function MyMouseUp(e) {
	if(typeof(vs)!='undefined')vs.closeList();
}

function CleanDptCrit(){
	if(document.getElementById("cp") && document.getElementById("cp").value == 'Dépt.')
		document.getElementById("cp").value = '';
}

function check_dpt(){
	var cp = document.getElementById('cp').value.toUpperCase().replace(/[,]+/g,',');
	var Tcp = cp.split(',');
	var i,j;
	var Tres = new Array();
	var res='';
	for(i=0;i<Tcp.length;i++){
		Tcp[i] = Tcp[i].replace(/[ ]+/g,'');
		Tcp[i] = Tcp[i].substr(0,2);
		if(i>0 && Tres.length>0 && Tcp[i].length==0)Tres[Tres.length]=Tcp[i];
		else if(/^[0-9][0-9]$/.test(Tcp[i]))Tres[Tres.length]=Tcp[i];
		else if(/^[0-9]$/.test(Tcp[i]))Tres[Tres.length]=Tcp[i];
		else if(/^(2A|2B|[A-Z]|[A-Z][A-Z])$/.test(Tcp[i]))Tres[Tres.length]=Tcp[i];
	}
	for(i in Tres){
		j=(parseInt(i)+1);
		while(j<Tres.length && Tres[i]!=Tres[j])j++;
		if(j==Tres.length)res+=(res!=''?',':'')+Tres[i];
	}
	document.getElementById('cp').value = res;
	document.formulaire.departement.disabled=!(res>0 || res=='2A' || res=='2B');
}

function is_enabled(){
	var grey='#CCCCCC';var white='#FFFFFF';
	var cat 	 		= document.formulaire.CATEGORIE.value;
	var sscat  		= document.formulaire.SS_CATEGORIE.value;
	var marque 		= document.formulaire.marque;
	var model  		= document.formulaire.modele;
	var energie 	= document.formulaire.energie;
	var garantie 	= document.formulaire.garantie;
	var couleur		= document.formulaire.couleur;
	var trans			= document.formulaire.transmission;
	var km_maxi		= document.formulaire.km_maxi;
	var annee			= document.formulaire.annee;
	var annee2		= document.formulaire.annee2;
	var prem_main	= document.formulaire.premiere_main;
	var version   = document.formulaire.version;
	
	var reg1 = new RegExp('^-1|-2|264|268|266');
	var reg2 = new RegExp('^428|427|424|422');
	var reg3;

	var brand_enabled = ((reg1.test(cat) && !reg2.test(sscat)) || cat=='');/*pour la recherche par marque cat=''*/
	marque.selectedIndex=(!brand_enabled?0:marque.selectedIndex);
	marque.style.background=(brand_enabled?white:grey);
	marque.disabled=!brand_enabled;

	reg1 = new RegExp('^-1|-2|264|268|266');
	reg2 = new RegExp('^428|426|427|424|422');
	var model_enabled = ((reg1.test(cat) && !reg2.test(sscat) && marque.value!='') || cat=='');/*pour la recherche par marque cat=''*/

	model.selectedIndex=(!model_enabled?0:model.selectedIndex);
	model.style.background=(model_enabled?white:grey);
	model.disabled=!model_enabled;
	if(energie!=null){
		reg1 = new RegExp('^-1|-2|264|268');
		var energie_enabled = (reg1.test(cat)  || cat=='');/*pour la recherche par marque cat=''*/
		energie.selectedIndex=(!energie_enabled?0:energie.selectedIndex);
		energie.style.background=(energie_enabled?white:grey);
		energie.disabled=!energie_enabled;
	}
	
	if(garantie!=null){
		reg1 = new RegExp('^266');
		var garantie_enabled = (!reg1.test(cat));
		garantie.disabled=!garantie_enabled;
	}
	
	if(couleur!=null){
		reg1 = new RegExp('^426');
		var couleur_enabled = (!reg1.test(sscat));
		couleur.selectedIndex=(!couleur_enabled?0:couleur.selectedIndex);
		couleur.style.background=(couleur_enabled?white:grey);
		couleur.disabled=!couleur_enabled;
	}
	
	if(trans!=null){
		reg1 = new RegExp('^266');
		var trans_enabled = (!reg1.test(cat));
		trans.selectedIndex=(!trans_enabled?0:trans.selectedIndex);
		trans.style.background=(trans_enabled?white:grey);
		trans.disabled=!trans_enabled;
	}
	
	if(km_maxi!=null){
		reg1 = new RegExp('^266');
		var km_maxi_enabled = (!reg1.test(cat));
		km_maxi.selectedIndex=(!km_maxi_enabled?0:km_maxi.selectedIndex);
		km_maxi.style.background=(km_maxi_enabled?white:grey);
		km_maxi.disabled=!km_maxi_enabled;
	}
	
	if(annee!=null){
		reg1 = new RegExp('^426');
		var annee_enabled = (!reg1.test(sscat));
		annee.selectedIndex=(!annee_enabled?0:km_maxi.selectedIndex);
		annee2.selectedIndex=(!annee_enabled?0:km_maxi.selectedIndex);
		annee.style.background=(annee_enabled?white:grey);
		annee2.style.background=(annee_enabled?white:grey);
		annee.disabled=!annee_enabled;
		annee2.disabled=!annee_enabled;
	}
	
	if(prem_main!=null){
		reg1 = new RegExp('^426');
		var prem_main_enabled = (!reg1.test(sscat));
		prem_main.selectedIndex=(!prem_main_enabled?0:km_maxi.selectedIndex);
		prem_main.disabled=!prem_main_enabled;
	}
	
 if(version!=null){
		reg1 = new RegExp('^428|426|427|424|422');
		var version_enabled = (!reg1.test(sscat));
		version.style.background=(version_enabled?white:grey);
		version.disabled=!version_enabled;
	}			
}	
///////////////////////////////////////////////////////////////////////////////////////
function fill_make(ss_cat,the_brand){
	var nrj = (document.formulaire.energie!=null?document.formulaire.energie.value:-1);
	var Tnrj=new Array(1,2,3,4);
	var combo = document.formulaire.marque;
	var i;var j;var k;
	var head  = null;/*pointeur tête de liste*/
	var tlist = null;/*pour le parcours*/
	var old;
	
	while((i = combo.options.length)>0)
			combo.options[i-1]=null;/*vide la combo*/
			
	combo.options[0] = new Option ( 'Marque', '' );
	if(ss_cat.search(',')>0)
		{/*fusionne à partir du tableau des marques*/
			var Tss_cat=new Array();
			Tss_cat = ss_cat.split(',');
			if(nrj>0){/*énergie sélectionnée, on fusionne les marques pour les sscat dans Tss_cat pour l'nrj sélectionnée*/
				for(i=0;i<Tss_cat.length;i++)
				{/*fusionne le tableau des marques dans la liste head*/
					if(Tmarque[Tss_cat[i]]!=null && Tmarque[Tss_cat[i]][nrj]!=null){
						for(k=0;k<Tmarque[Tss_cat[i]][nrj].length;k++)
						{
							head = insert(head,Tmarque[Tss_cat[i]][nrj][k]);
						}
					}					
				}
			}
			else{/*pas d'énergie sélectionnée, on fusionne les marques pour les sscat dans Tss_cat et toutes les énergies*/
				for(i=0;i<Tss_cat.length;i++)
				{/*fusionne le tableau des marques dans la liste head*/
					for(j=0;j<Tnrj.length;j++){
						if(Tmarque[Tss_cat[i]]!=null && Tmarque[Tss_cat[i]][Tnrj[j]]!=null){
							for(k=0;k<Tmarque[Tss_cat[i]][Tnrj[j]].length;k++)
							{
								head = insert(head,Tmarque[Tss_cat[i]][Tnrj[j]][k]);
							}
						}
					}
				}
			}
		}
	else if(Tmarque[ss_cat]!=null)
	{
		/*rempli le combo marque à partir d'un tableau lorsque l'énergie est sélectionnée*/
		if(nrj>0){
			if(Tmarque[ss_cat]!=null && Tmarque[ss_cat][nrj]!=null){
				for(i=0;i<Tmarque[ss_cat][nrj].length;i++)
				{
					combo.options[combo.options.length] = new Option ( Tmarque[ss_cat][nrj][i].replace(new RegExp('\\*','g'),'/').replace(new RegExp('~','g'),'+').replace(new RegExp('`','g'),'\'').replace(new RegExp('_','g'),'-').replace(new RegExp('\\^','g'),'&'), Tmarque[ss_cat][nrj][i] );
					if(Tmarque[ss_cat][nrj][i].toUpperCase()==the_brand)combo.options[combo.options.length-1].selected=true;
				}
			}
		}
		else {
			/*sinon fusionne les marques pour toutes les énergies*/			
			for(j=0;j<Tnrj.length;j++){
				if(Tmarque[ss_cat]!=null && Tmarque[ss_cat][Tnrj[j]]!=null){
					for(i=0;i< Tmarque[ss_cat][Tnrj[j]].length;i++)
					{
						head = insert(head,Tmarque[ss_cat][Tnrj[j]][i]);
					}
				}
			}
		}
	}
	/*remplie le combo à partir de la liste*/
	tlist = head;
	while (tlist!=null){/*crée le combo à partir de la liste et vide la liste*/
		combo.options[combo.options.length] = new Option ( tlist.val.replace(new RegExp('\\*','g'),'/').replace(new RegExp('~','g'),'+').replace(new RegExp('`','g'),'\'').replace(new RegExp('_','g'),'-').replace(new RegExp('\\^','g'),'&') , tlist.val );
		if(tlist.val.toUpperCase()==the_brand)combo.options[combo.options.length-1].selected=true;
		old = tlist;
		tlist=tlist.suiv;
		old = null;
	}
	is_enabled();
}
///////////////////////////////////////////////////////////////////////////////////////
function min_millesime(annee, label){
	/*fill min millesime combo*/
	var combo = document.getElementById('annee');
	var ss_cat=document.formulaire.SS_CATEGORIE;
	if(ss_cat.value=='49'){
	  var date_min=1969;
	  var date_max=1989;
	}else{
		var date_min=1989;
	  var date_max = (new Date()).getFullYear();
  }
	while((i = combo.options.length)>0)
			combo.options[i-1]=null;/*vide la combo*/
	var libelle = label?label:'Millésime de';
	combo.options[combo.options.length] = new Option ( libelle , '');
	if(annee=='')combo.options[combo.options.length-1].selected=true;
	for(i=date_min;i<=date_max;i++)
	{
		combo.options[combo.options.length] = new Option ( i , i );
		if(((annee!='')&&(i==annee))||i == prefered_min_millesime)combo.options[combo.options.length-1].selected=true;
	}
}
///////////////////////////////////////////////////////////////////////////////////////
function max_millesime(borne_inf, label){
	/*fill max millesime combo*/
	var combo = document.getElementById('annee2');
	var ss_cat=document.formulaire.SS_CATEGORIE;
	if(ss_cat.value=='49'){
	  var date_min=1969;
	  var date_max=1989;
	}else{
		var date_min=1989;
	  var date_max = (new Date()).getFullYear();
  }
	while((i = combo.options.length)>0)
			combo.options[i-1]=null;/*vide la combo*/
	var min = (borne_inf>0)?parseInt(borne_inf,10):date_min;
	var max = date_max;
	if( (combo.selectedIndex>-1)&&(combo[combo.selectedIndex].value>borne_inf) )
		 prefered_max_millesime = combo[combo.selectedIndex].value;
	
	while((i = combo.options.length)>0)
			combo.options[i-1]=null;
	
	var libelle = label?label:'Millésime à';
	combo.options[combo.options.length] = new Option ( libelle , '');
	/*build list starting from price max and trying to restore old value*/
	for(i=max;i>=min;i--)
	{
		combo.options[combo.options.length] = new Option ( i , i );
		if(i == prefered_max_millesime)combo.options[combo.options.length-1].selected=true;
	}
}
///////////////////////////////////////////////////////////////////////////////////////
function format(the_string){
	/*return a well formated string 3000 => 3 000*/
	var str = ''+the_string;
	var step = 3;
	var tmp = '';
	var i;
	var nbspace=0;
	for(i=str.length-1;i>=0;i--){
		tmp=str.charAt(i)+tmp;
		if(((tmp.length-nbspace)%step==0)&&(i>0)){tmp=' '+tmp;nbspace++;}
	}	
	return tmp;
}
//////////////////////////////////////////////////////////////////////////////////////////
function radio_change(cat){
	switch (cat){
	case 'scooter' :
	  document.getElementById('quad').checked=false;
	  document.getElementById('moto').checked=false;
	  document.getElementById('scooter').checked=true;
		document.formulaire.SS_CATEGORIE.value = '71';
	break;
	case 'quad' : document.formulaire.SS_CATEGORIE.value = '70';
	 							document.getElementById('scooter').checked=false;
	 							document.getElementById('moto').checked=false;
	 							document.getElementById('quad').checked=true;
	break;
	case 'moto' :  document.formulaire.SS_CATEGORIE.value = '60,61,62,63,64,65,66,67,68,69,72';
 							document.getElementById('scooter').checked=false;
	  					document.getElementById('moto').checked=true;
	  					document.getElementById('quad').checked=false;
	  					break;	
 }
update_me();
return; 
}
///////////////////////////////////////////////////////////////////////////////////////
function update_me(){
	var ss_cat=document.formulaire.SS_CATEGORIE;
	var cat=document.formulaire.CATEGORIE; 
	var the_brand=document.formulaire.marque.value;
	cat.value=get_cat(ss_cat.value);
	/*need to refresh if and only if a brand is selected to fill model*/
	if(document.formulaire.marque.selectedIndex>0)
		{
			document.getElementById('sablier').style.display='block';
			document.getElementById('main_search').style.display='none';
			//Ajout‚ par TR
			document.formulaire.target = "_self";
			//Fin d'ajout
			document.formulaire.submit();
			return;
		}
	fill_make(ss_cat.value,the_brand);
	if(ss_cat.value=='71')fill_cubic(document.formulaire.cylindree.value);
	
	min_millesime('');
	max_millesime(document.getElementById('annee')[document.getElementById('annee').selectedIndex].value);
}
///////////////////////////////////////////////////////////////////////////////////////
function min_price(the_price_min, label){
	/*fill min price combo*/
	var combo = document.formulaire.prix_mini;
	var min = 3000;
	var max = 100500;
	var step = 1500;	
	var libelle = label?label:'Prix minimum';
	combo.options[combo.options.length] = new Option ( libelle , '');
	for(i=min;i<=max;i+=step)
	{
		combo.options[combo.options.length] = new Option ( format(i) + ' €' , i );
		if((the_price_min!='')&&(i==the_price_min))combo.options[combo.options.length-1].selected=true;
	}
}
///////////////////////////////////////////////////////////////////////////////////////
function max_price(label){
	/*fill max price combo*/
	var combo = document.formulaire.prix_maxi;
	var min_price = document.formulaire.prix_mini[document.formulaire.prix_mini.selectedIndex].value;
	var step = 1500;
	var min = (min_price>0)?parseInt(min_price,10) + step:3000;
	var max = 100500;
	
	if( (combo.selectedIndex>-1)&&(combo[combo.selectedIndex].value>parseInt(min_price,10)) )
		 prefered_max_price = combo[combo.selectedIndex].value;
	
	while((i = combo.options.length)>0)
			combo.options[i-1]=null;
			
	var libelle = label?label:'Prix maximum';
	combo.options[combo.options.length] = new Option ( libelle , '');
	/*build list starting from price min + step and trying to restore old value*/
	for(i=min;i<=max;i+=step)
	{
		combo.options[combo.options.length] = new Option ( format(i) + ' €' , i );
		if(i == prefered_max_price)combo.options[combo.options.length-1].selected=true;
	}
}
///////////////////////////////////////////////////////////////////////////////////////
function km_maxi(the_min_mileage, label){
	/*fill maxi km combo*/
	var combo = document.formulaire.km_maxi;
	var min = 10000;
	var max = 200000;
	var step = 10000;
	if(combo!=null){
		var libelle = label?label:'Kilométrage maximum';
		combo.options[combo.options.length] = new Option ( libelle , '');
		for(i=min;i<=max;i+=step)
		{
			combo.options[combo.options.length] = new Option ( format(i) + ' km' , i );
			if((the_min_mileage!='')&&(i==the_min_mileage))combo.options[combo.options.length-1].selected=true;
		}
	}
}
///////////////////////////////////////////////////////////////////////////////////////
function go_and_submit(partner){
	var form = document.formulaire;
	if (partner == "caradisiac" || partner == "20minutes" || partner == "forumauto") {
		form.target='_blank';
	} else {
		form.target='_top';
	}
	form.go.value='true';/*force request*/
	
	if(form.curCat!=null)/*pour la home, on peut faire des recherche auto ET moto donc on détermine la curCat*/
	{
		var reg1 = new RegExp('^-1|264|268|266');
		form.curCat.value=(reg1.test(form.CATEGORIE.value)?'auto':'moto');
	}
	if(form.cp!=null)form.cp.value=form.cp.value.replace(/,$/,'');
	if(form.departement!=null)form.departement.checked=((form.departement.checked)&&(form.cp.value.length>0));
	CleanDptCrit();
  form.submit();
}
///////////////////////////////////////////////////////////////////////////////////////
function get_cat(ss_cat){
 var i;
 allcat_auto='40,41,42,43,44,45,46,47,48,49';
 allcat_264='80,81,82,83,84';
 allcat_268='506,507,508,509,510';
 allcat_266='423,425,426,427,428';
 all_cat_moto ='60,61,62,63,64,65,66,67,68,69,70,71,72';
 cat = ss_cat.split(',');
 i=0;
 while(i<cat.length){
 	  	if(allcat_auto.match(new RegExp('(^|,)'+cat[i]+'(,|$)')))
 	  	  return '-1';
 	  	else if(allcat_264.match(new RegExp('(^|,)'+cat[i]+'(,|$)')))
 	  		return '264';
 	  	else if(allcat_268.match(new RegExp('(^|,)'+cat[i]+'(,|$)')))
 	  		return '268';
 	  	else if(allcat_266.match(new RegExp('(^|,)'+cat[i]+'(,|$)')))
 	  		return '266';
 	  	else if(all_cat_moto.match(new RegExp('(^|,)'+cat[i]+'(,|$)')))
 	  	  return '-2';				 
 i++;
 }	
	return null;	
}
///////////////////////////////////////////////////////////////////////////////////////
function select_it(witch){
	switch (witch){
		case 1://rech avancée
		var onglet1=document.getElementById('btSwitch1');var a1 = document.getElementById('aSwitch1');
		var onglet2=document.getElementById('btSwitch2');var a2 = document.getElementById('aSwitch2');
		var onglet3=document.getElementById('btSwitch3');var a3 = document.getElementById('aSwitch3');
		document.getElementById('bymodel').style.display='none';
		document.getElementById('bybrand').style.display='none';
		document.getElementById('search').style.display='block';
		break;
		case 2://tous les modèles
		var onglet1=document.getElementById('btSwitch2');var a1 = document.getElementById('aSwitch2');
		var onglet2=document.getElementById('btSwitch1');var a2 = document.getElementById('aSwitch1');
		var onglet3=document.getElementById('btSwitch3');var a3 = document.getElementById('aSwitch3');
		document.getElementById('bymodel').style.display='block';
		document.getElementById('search').style.display='none';
		document.getElementById('bybrand').style.display='none';
		break;
		case 3://toutes les marques
		var onglet1=document.getElementById('btSwitch3');var a1 = document.getElementById('aSwitch3');
		var onglet2=document.getElementById('btSwitch1');var a2 = document.getElementById('aSwitch1');
		var onglet3=document.getElementById('btSwitch2');var a3 = document.getElementById('aSwitch2');
		document.getElementById('bymodel').style.display='none';
		document.getElementById('bybrand').style.display='block';
		document.getElementById('search').style.display='none';
	}
	var reg = new RegExp('^428|426|423|427|425');
	var reg1 = new RegExp(',');
	var no_link = (reg.test(document.formulaire.SS_CATEGORIE.value) && !reg1.test(document.formulaire.SS_CATEGORIE.value));
	if(onglet1!=null){onglet1.style.background='#999999';	onglet1.style.fontWeight='bold';a1.style.color='#ffffff';}
	if(onglet2!=null){onglet2.style.background='#cccccc'; onglet2.style.fontWeight='normal';a2.style.color=(no_link?'#999999':'#000000');}
	if(onglet3!=null){onglet3.style.background='#cccccc'; onglet3.style.fontWeight='normal';a3.style.color=(no_link?'#999999':'#000000');}
}
///////////////////////////////////////////////////////////////////////////////////////
function fill_cubic(the_cubic){
	var combo = document.formulaire.cylindree;
	var sscat = document.formulaire.SS_CATEGORIE.value;
	var i;
	while((i = combo.options.length)>0)
			combo.options[i-1]=null;/*vide la combo*/
	
	combo.options[combo.options.length] = new Option ( 'Cylindrée',					'' );
	combo.options[combo.options.length] = new Option ( 'jusqu\'à 50 cm3',		'-1|50' );
	if(sscat=='607'){
		combo.options[combo.options.length] = new Option ( 'de 51 à 125 cm3',		'51|125' );
		combo.options[combo.options.length] = new Option ( 'plus de 125 cm3',		'126|-1' );
	}
	else{
		combo.options[combo.options.length] = new Option ( 'de 51 à 80 cm3',		'51|80' );
		combo.options[combo.options.length] = new Option ( 'de 81 à 125 cm3',		'81|125' );
		combo.options[combo.options.length] = new Option ( 'de 126 à 250 cm3',	'126|250' );
		combo.options[combo.options.length] = new Option ( 'de 251 à 400 cm3',	'251|400' );
		combo.options[combo.options.length] = new Option ( 'de 401 à 500 cm3',	'401|500' );
		combo.options[combo.options.length] = new Option ( 'de 501 à 600 cm3',	'501|600' );
		combo.options[combo.options.length] = new Option ( 'de 601 à 750 cm3',	'601|750' );
		combo.options[combo.options.length] = new Option ( 'de 751 à 900 cm3',	'751|900' );
		combo.options[combo.options.length] = new Option ( 'de 901 à 1000 cm3',	'901|1000' );
		combo.options[combo.options.length] = new Option ( 'de 1001 à 1100 cm3','1001|1100' );
		combo.options[combo.options.length] = new Option ( 'de 1101 à 1200 cm3','1101|1200' );
		combo.options[combo.options.length] = new Option ( 'plus 1200 cm3',			'1201|-1' );
	}
	
	for(i=0;i<combo.options.length;i++)
	{
		if(the_cubic==combo.options[i].value){
			combo.options[i].selected=true;
			return;
		}
	}
}


