var xhr = null;

function getXhr(){
if(window.XMLHttpRequest) // Firefox et autres
   xhr = new XMLHttpRequest();
else if(window.ActiveXObject){ // Internet Explorer
   try {
             xhr = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
             xhr = new ActiveXObject("Microsoft.XMLHTTP");
         }
}
else { // XMLHttpRequest non support? par le navigateur
   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
   xhr = false;
}
}

/**
* M?thode qui sera appel?e sur le click du bouton
*/
function go(item,val,get){
   getXhr();
   // On d?fini ce qu'on va faire quand on aura la r?ponse
   xhr.onreadystatechange = function(){
   	// On ne fait quelque chose que si on a tout re?u et que le serveur est ok
   	if(xhr.readyState == 4 && xhr.status == 200){

   		leselect = xhr.responseText;
   		//alert(leselect);
   		// On se sert de innerHTML pour rajouter les options a la liste
   		if (item == 'cat_id')
   		  document.getElementById("district_div").innerHTML = leselect;

         if (item == 'sub_district')
   		  document.getElementById("city_div").innerHTML = leselect;

   		if (item == 'city'){
              var pos=leselect.indexOf('</ul>');
   		     document.getElementById("neighborhood_div").innerHTML = leselect.substring(0,pos);
   		     document.getElementById("type_div").innerHTML = leselect.substring(pos+5);
   		}
   		if (item == 'new_old')
                   document.getElementById("comp_div").innerHTML = leselect;

   	}
   }

   // Ici on va voir comment faire du post
   xhr.open("POST","http://www.lagur.com/french/new_ajax_search.php",true);
   xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      // DeleteName('cat','ListCat');
      // DeleteName('h_type','ListType');
       DeleteName('room','ListRoom');

   if (item == 'sub_district'){
      sel = document.mini_search.sub_district_id;
   	district = sel.options[sel.selectedIndex].value;
   	var Alldistrict = sel.options[sel.selectedIndex].id;
   	document.getElementById("district_id").value = Alldistrict;
   	//alert(document.getElementById("district_id").value);
   	//sel = document.mini_search.cat_id;
        //idcat_id = sel.options[sel.selectedIndex].value;
    idcat_id = document.mini_search.cat_id.value;
   	if (idcat_id == 1){
         idcat_id = 6;
         document.getElementById("from_private").value = 'Y';
      }
      if (idcat_id == 2){
         idcat_id = 7;
         document.getElementById("from_private").value = 'Y';
      }
       if(idcat_id == "3"){ 
         idcat_id = 7;
         document.getElementById("board_type").value = 3;
      }
      if (document.getElementById("from_private").value == 'Y')
          board_type = 4;
      else
      board_type = document.getElementById("board_type").value;
   	xhr.send("Alldistrict="+Alldistrict+"&district="+district+"&cat_id2="+idcat_id+"&board_type="+board_type+"&get_city="+get);
   }

   if (item == 'cat_id'){
      document.mini_search.sub_district_id.options[0].text = "Veuillez patienter...";
   	sel = document.mini_search.cat_id;
   	idcat_id = sel.options[sel.selectedIndex].value;

       if(idcat_id == "6" || idcat_id == "7" || idcat_id == "3"){ 
         document.getElementById("from_private").value = '-1';
      }

   	  if (idcat_id == "1"){
         idcat_id = 6;
         document.getElementById("from_private").value = 'Y';
      }
      if(idcat_id == "2"){
         idcat_id = 7;
         document.getElementById("from_private").value = 'Y';
      }

      if(idcat_id == "3"){ 
         idcat_id = 7;
         document.getElementById("board_type").value = 3;
      }

      if (document.getElementById("from_private").value == 'Y')
          board_type = 4;
      else
      board_type = document.getElementById("board_type").value;
   	xhr.send("district=Y&cat_id="+idcat_id+"&board_type="+board_type);

   }

   if (item == 'city'){
   	sel = document.mini_search.city;
   	city = sel.options[sel.selectedIndex].value;
   	idcat_id = document.mini_search.cat_id.value;
	if (idcat_id == 1) idcat_id = 6; //private sale
	if (idcat_id == 2) idcat_id = 7; //private rent
    if(idcat_id == "3"){ 
         idcat_id = 7;
         document.getElementById("board_type").value = 3;
      }
   	if (document.getElementById("from_private").value == 'Y')
               board_type = 4;
           else
   	    board_type = document.getElementById("board_type").value;
   	xhr.send("city="+city+"&cat_id2="+idcat_id+"&board_type="+board_type+"&get_htype="+get);
   }

   if (item == 'new_old'){
   	sel = document.mini_search.new_old;
   	new_old = sel.options[sel.selectedIndex].value;
   	xhr.send("new="+new_old);
   }


}


function SetBoardProp(x){
    if (x != '4'){
        document.getElementById("board_type").value = x;
        if (x == '1')document.getElementById("board_type").value = '1,2,3';
        document.getElementById("from_private").value = ''
    }
    else document.getElementById("from_private").value = 'Y';
}

function DeleteName(o,lst){
  var txt = '';

  var o = document.getElementById(o);
	if (o) {

		allInput = o.getElementsByTagName("input");
		for (i=0; i<allInput.length;i++) allInput[i].checked = false;

	}
	o=document.getElementById(lst);
    if (o!=null) o.value='';
}

 function ShowCat(item){
    obj=  document.getElementById(item);
    if (obj)obj.style.display="block";
}

function HideCat(item){
    obj=  document.getElementById(item);
    if (obj) obj.style.display="none";
}

function AddName(o,lst,id){
  var txt = '';
  var txtID = '';

	var oo = document.getElementById(o);
	  if (oo) {

		allInput = oo.getElementsByTagName("input");
		for (i=0; i<allInput.length;i++) {
            if (lst == 'ListRoom')document.getElementById('rooms_min').value = '-1';
			if (allInput[i].checked) {
                if (lst == 'ListRoom'){
                    document.getElementById('rooms_min').value = '5';
                    txt=txt+allInput[i].id+', ';
                }else{
                    if (lst == 'ListCat' || lst == 'ListType'){
    				     txt=txt+allInput[i].name+', ';
    				     txtID=txtID+allInput[i].id+', ';
    				    }
    				else
                    txt=txt+allInput[i].myname+', ';
                }

			}
		}
	}


	ooo=document.getElementById(lst);

    	if (ooo!=null) {
    		txt=String(txt).substring(0,txt.length-2);
   			ooo.value=txt;

    	}


   if (o == 'cat'){
    txtID=String(txtID).substring(0,txtID.length-2);
    document.getElementById('ListCatID').value=txtID;

  }
  if (o == 'h_type'){
    var h_type = document.getElementById(o)
    var hid = document.createElement('input');
    hid.type = 'hidden';
    hid.name = 'ht_control[]';
    hid.value = id;
    h_type.appendChild(hid);
  }


}

function update_neighborhood(item){
   document.getElementById("ListCatID").value = item;
}

function show_advanced_search() {
	var search_advanced_form_opener = document.getElementById('search_advanced_form_opener');
	var search_advanced_form_close = document.getElementById('search_advanced_form_close');
	var advanced_search_form = document.getElementById('advanced_search_form');

	if (search_advanced_form_opener.style.display == '') {
		search_advanced_form_opener.style.display = 'none';
		advanced_search_form.style.display = '';
		search_advanced_form_close.style.display = '';
	} else {
		search_advanced_form_opener.style.display = '';
		advanced_search_form.style.display = 'none';
		search_advanced_form_close.style.display = 'none';
	}
}

// show telephone - element_show_board_item.tpl - ajax_show_tel.php

function show_tel(item,showman){
   getXhr();
   xhr.onreadystatechange = function(){
   	if(xhr.readyState == 4 && xhr.status == 200){
   		leselect = xhr.responseText;
   		//alert(leselect);
   		document.getElementById("tel_div").innerHTML = leselect;
      }
   }

   xhr.open("POST","http://www.lagur.com/french/ajax_show_tel.php",true);
   xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

   if (showman == 'Y') xhr.send("board_items_id="+item+"&showman=Y");
   else xhr.send("board_items_id="+item);

}
function show_site(item){
   getXhr();
   xhr.onreadystatechange = function(){
       if(xhr.readyState == 4 && xhr.status == 200){
           leselect = xhr.responseText;
           //alert(leselect);
           document.getElementById("site_div").innerHTML = leselect;
      }
   }
   
   xhr.open("POST","http://www.lagur.com/french/ajax_show_tel.php",true);
   xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

    xhr.send("board_items_id="+item+"&site=Y");


}
