window.onload = init;
function init () {
	
	attachEvent($("motscles"),'focus',effacerTexte,false);
	attachEvent($("motscles"),'blur',afficherTexte,false);
	attachEvent($("categories"),'change',actionMenu,false);
	attachEvent($("categories"),'keyup',actionMenu,false);
	attachEvent($("regions"),'change',actionMenu,false);
	attachEvent($("regions"),'keyup',actionMenu,false);
	attachEvent($("favoris"),'click',favori,false);
	
	/* 
	*  Verification des varaibles post
	*
	*/
	if( recherche == true){
		
		var aMoteurC = new Array(
						$n('typeachm1','0'),
						$n('typeachm2','0'),
						$n('prixachm1','0'),
						$n('prixachm2','0'),
						/*appart loc  4*/
						$n('typeloca1','0'),
						$n('typeloca2','0'),
						$n('loyerloca1','0'),
						$n('loyerloca2','0'),
						/*appart ach 8*/
						$n('typeacha1','0'),
						$n('typeacha2','0'),
						$n('prixacha1','0'),
						$n('prixacha2','0'),
						/*auto 12*/
						$n('prixauto1','0'),
						$n('prixauto2','0'),
						$n('nbrkm1','0'),
						$n('nbrkm2','0'),
						$n('typecab','0'),
						/*utilitaire 17*/
						$n('prixutil1','0'),
						$n('prixutil2','0'),
						$n('nbrkm1','1'),
						$n('nbrkm2','1'),
						$n('typecab','1'),				
						/*appart loc  22*/
						$n('typelocm1','0'),
						$n('typelocm2','0'),
						$n('loyerlocm1','0'),
						$n('loyerlocm2','0')		
						);	
									
	if (typeof post != "undefined") {
			 if(post['cat'] == 1) {
				afficheBarre("rechercheMaisonAch"); 
				for(i=0;i<=4;i++){	aMoteurC[i].disabled = false;}
			}
			else if(post['cat'] == '2') {
				afficheBarre("rechercheAppartAch");
				for(i=8;i<=11;i++){	aMoteurC[i].disabled = false;}
			}
			else if(post['cat'] == '5') {
				afficheBarre("rechercheAutomobile");	
				for(i=12;i<=16;i++){	aMoteurC[i].disabled = false;}
			}
			else if(post['cat'] == '7') {
				afficheBarre("rechercheUtilitaire");
				for(i=17;i<=21;i++){	aMoteurC[i].disabled = false;}
			}	
			else {
			//
			}
		}
		 
	}
	
}

function attachEvent(o,e,f,c){
	if(o.addEventListener)
		o.addEventListener(e,f,c);
	else
		if(o.attachEvent)
			o.attachEvent("on"+e,f);
		else
			eval("o.on"+e+"="+f)
}
	
	/* 
	* effacer le texte 
	*/

function effacerTexte( event ) { 

var target = event.target || event.srcElement;
  if (target.value === "Description, mots clés, référence") { 
    target.value = "";
	event.returnValue = false;
	}
}
function afficherTexte( event ) { 

var target = event.target || event.srcElement;
  if (target.value === "") { 
    target.value = "Description, mots clés, référence";
	event.returnValue = false;
	}
}
	/* 
	* actions des menus select (categories et scategories) 
	*/

	
	
function actionMenu( event ) { 
var aMoteurC = new Array($n('typeachm1','0'),
					$n('typeachm2','0'),
					$n('prixachm1','0'),
					$n('prixachm2','0'),
					/*appart loc  4*/
					$n('typeloca1','0'),
					$n('typeloca2','0'),
					$n('loyerloca1','0'),
					$n('loyerloca2','0'),
					/*appart ach 8*/
					$n('typeacha1','0'),
					$n('typeacha2','0'),
					$n('prixacha1','0'),
					$n('prixacha2','0'),
					/*auto 12*/
					$n('prixauto1','0'),
					$n('prixauto2','0'),
					$n('nbrkm1','0'),
					$n('nbrkm2','0'),
					$n('typecab','0'),
					/*utilitaire 17*/
					$n('prixutil1','0'),
					$n('prixutil2','0'),
					$n('nbrkm1','1'),
					$n('nbrkm2','1'),
					$n('typecab','1'),				
					/*appart loc  22*/
					$n('typelocm1','0'),
					$n('typelocm2','0'),
					$n('loyerlocm1','0'),
					$n('loyerlocm2','0')		
					);	
					
	
	var target = event.target || event.srcElement;
	if (target.id == 'categories') { 
		//alert(aMoteurC);
		for(i=0;i<=25;i++){	aMoteurC[i].disabled = true;}
			sendData('id='+target.value,'verif/lirecat.php');	
			catVal = target.value;
			switch (target.value) {
				case '1': afficheBarre("rechercheMaisonAch"); 
				for(i=0;i<=4;i++){	aMoteurC[i].disabled = false;}
				break;
				case '2': afficheBarre("rechercheAppartAch");
				for(i=8;i<=11;i++){	aMoteurC[i].disabled = false;}
				break;	
				case '5': afficheBarre("rechercheAutomobile");	
				for(i=12;i<=16;i++){	aMoteurC[i].disabled = false;}
				break;
				case '7': afficheBarre("rechercheUtilitaire");
				for(i=17;i<=21;i++){	aMoteurC[i].disabled = false;}
				break;
				default: masqueBarre(); break;
			}
	}
	else if (target.id =='s_catt'){	
	for(i=0;i<=25;i++){	aMoteurC[i].disabled = true;}	
		if(catVal == '1'){
		//si maison select - choix du sous menu Loc ou Achat
			switch (target.value) {
				case '1': afficheBarre("rechercheMaisonAch");
				for(i=0;i<=4;i++){	aMoteurC[i].disabled = false;}
				break;
				case '2': afficheBarre("rechercheMaisonLoc");
				for(i=22;i<=25;i++){	aMoteurC[i].disabled = false;}
				break;
			}
		}
		else if(catVal == '2'){
			//Si appartement select - choix du sous menu Loc ou Achat
			switch (target.value) {
				case '3': afficheBarre("rechercheAppartAch");for(i=8;i<=11;i++){	aMoteurC[i].disabled = false;}	break;

				case '4': afficheBarre("rechercheAppartLoc");for(i=4;i<=7;i++){	aMoteurC[i].disabled = false;}	break;
			}
		}	
	}
		

	else if (target.id =='regions'){
		sendData2('reg='+target.value,'verif/liredep.php'); 
	}
} 

/*
*	Modifications de l'affichage via prop css (menu complémentaire de recherche)
*/
var moteurS= '';
function afficheBarre(moteur) {
		masqueBarre();
		$(moteur).style.display = "block";
		//alert(selects.length);
		moteurS = moteur;
		$("recherchebarf").style.height = "69px";
		$("recherchebard").style.height = "69px";
		$("recherchebar").style.height = "69px";

	}
	
function masqueBarre() {
		if (moteurS != '') {
			$(moteurS).style.display = "none";
			moteurS = '';
		}
		$("recherchebarf").style.height = "39px";
		$("recherchebard").style.height = "39px";
		$("recherchebar").style.height = "39px";
		
}
	
		/**
	 * Permet d'envoyer des données en GET ou POST en utilisant les XmlHttpRequest
	 */
function sendData(param, page)
{
	if(document.all)
	{
		//Internet Explorer
		var XhrObj = new ActiveXObject("Microsoft.XMLHTTP") ;
	}//fin if
	else
	{
		//Mozilla
		var XhrObj = new XMLHttpRequest();
	}//fin else

	//définition de l'endroit d'affichage:
	var content = $("Scat");
	
	XhrObj.open("POST", page, false);

	//Ok pour la page cible
	XhrObj.onreadystatechange = function()
	{
		if (XhrObj.readyState == 4 && XhrObj.status == 200)
			content.innerHTML = XhrObj.responseText ;
			if (XhrObj.responseText == "") {
			content.innerHTML = ' <select id="s_catt" name="souscat" class="R_deroulant" disabled="disabled">	<option value="0">Les Sous-catégories</option></select>';
			}
	}
	XhrObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	XhrObj.send(param);
	if (XhrObj.readyState == 4){
		if (param == 'id=1' || param == 'id=2') {
			attachEvent($("s_catt"),'change',actionMenu,false);
			attachEvent($("s_catt"),'keyup',actionMenu,false);
		}		
	}	
}//fin fonction SendData


/*
* departement
*/
function sendData2(param, page)
{
	if(document.all)
	{
		//Internet Explorer
		var XhrObj = new ActiveXObject("Microsoft.XMLHTTP") ;
	}//fin if
	else
	{
		//Mozilla
		var XhrObj = new XMLHttpRequest();
	}//fin else

	//définition de l'endroit d'affichage:
	var content = $("dep");
	
	XhrObj.open("POST", page);

	//Ok pour la page cible
	XhrObj.onreadystatechange = function()
	{
		if (XhrObj.readyState == 4 && XhrObj.status == 200)
			content.innerHTML = XhrObj.responseText ;
			if (XhrObj.responseText == "") {
			content.innerHTML = ' <select id="lesdep" name="lesdep" class="R_deroulant" disabled="disabled"><option value="0">Tous les Départements</option></select>	';
			}
	}

	XhrObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	XhrObj.send(param);
}//fin fonction SendData

function $(id) {
	return  document.getElementById(id);	
}
function $n(name,pos) {
	
}

function $n(name, pos) {
if(document.all) //ie
	{	
	if (name=='km'){tag='input';} else{tag = 'select';}
     var elem = document.getElementsByTagName(tag);
     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++) {
          att = elem[i].getAttribute("name");
          if(att == name) {
               arr[iarr] = elem[i];
               iarr++;
          }
	}
	 return arr[pos]; 
} 
else{
	 return  document.getElementsByName(name).item(pos);	
}
}

function favori(){
	phrase = 'c a vendre, petites annonces';
	lien= 'http://www.cavendre.fr'
   if (window.sidebar)   {
   window.sidebar.addPanel(phrase, lien,"");
   }
   else if( document.all )   {
   window.external.AddFavorite(lien, phrase);
   }
   else        {
   return true;
   }
} 

