var type = '2';
var prix = "";
var operation = 'achat';
var piece = "";
var localisation ='';

function faitrequete(quoi,combien){
	if(quoi=='typebien')
		type = combien;
	else if(quoi=='prix')
		prix = combien;
	else if(quoi=='operation')
		operation = combien;
	else if(quoi=='piece')
		piece = combien;
	else if(quoi=='cp'){
		if(combien!='codes postaux, villes' && combien!=''){
			localisation = combien;
			if(localisation.match(/^[0-9, ]+$/)){
				localisation = localisation.replace(/\s+/g,'');
				var tableau = localisation.split(/,/);
				var where_cp='';
				for(var i=0;i<tableau.length;i++){
					loc = tableau[i];
				
					switch(loc){
						case '72000': where_cp += 'p.cp=72000 or p.cp=72100 or '; break;
						case '49000': where_cp += 'p.cp="49000" or p.cp="49100" or';break;
						case '37000': where_cp += 'p.cp="37000" or p.cp="37100" or';break;
						case '44000': where_cp += 'p.cp="44000" or p.cp="44100" or p.cp="44200" or p.cp="44300" or p.cp="44400" or';break;
						case '35000': where_cp += 'p.cp="35000" or p.cp="35200" or p.cp="35700" or';break;
						default:
							if(loc.length==2)
								where_cp += '"'+loc+'"=LEFT(p.cp,2) or ';
							else
								where_cp += 'p.cp="'+tableau[i]+'" or ';
					}
				}where_cp += ' 0';
				localisation = where_cp;//alert('localisation : '+localisation);
			}
			else{
				var tableau = localisation.split(/,/);
				localisation = "num_ville=ville.numero and ( ";
				for(var i=0;i<tableau.length;i++){
					tableau[i] = tableau[i].replace(/^\s+/g,'');
					tableau[i] = tableau[i].replace(/\s+$/g,'');
					tableau[i] = tableau[i].replace(/\s+/g,' ');
					localisation += '( LCASE(ville.nom)=LCASE("'+tableau[i]+'") or  LCASE(ville.nom) LIKE LCASE("%'+tableau[i]+'%") or INSTR(LCASE("'+tableau[i]+'"),LCASE(ville.nom))   or LCASE(ville.nom) LIKE LCASE("%'+tableau[i].replace(/ /g,'-')+'%") or INSTR(LCASE("'+tableau[i].replace(/ /g,'-')+'"),LCASE(ville.nom))    or LCASE(ville.nom) LIKE LCASE("%'+tableau[i].replace(/-/g,' ')+'%") or INSTR(LCASE("'+tableau[i].replace(/-/g,' ')+'"),LCASE(ville.nom))    or LCASE(ville.nom) LIKE LCASE("%'+tableau[i].replace(/-/g,' ').replace(/ saint /gi,' st ')+'%") or INSTR(LCASE("'+tableau[i].replace(/-/g,' ').replace(/ saint /gi,' st ')+'"),LCASE(ville.nom))    or LCASE(ville.nom) LIKE LCASE("%'+tableau[i].replace(/ /g,'-').replace(/-saint-/gi,'-st-')+'%") or INSTR(LCASE("'+tableau[i].replace(/ /g,'-').replace(/-saint-/gi,'-st-')+'"),LCASE(ville.nom))    or LCASE(ville.nom) LIKE LCASE("%'+tableau[i].replace(/-/g,' ').replace(/^saint /gi,'st ')+'%") or INSTR(LCASE("'+tableau[i].replace(/-/g,' ').replace(/^saint /gi,'st ')+'"),LCASE(ville.nom))    or LCASE(ville.nom) LIKE LCASE("%'+tableau[i].replace(/ /g,'-').replace(/^saint-/gi,'st-')+'%") or INSTR(LCASE("'+tableau[i].replace(/ /g,'-').replace(/^saint-/gi,'st-')+'"),LCASE(ville.nom))		 ) or ';
				}
				localisation += "0 )";
				//alert(localisation);
			}
		}
	}
	//else 
	var requete = '';
	if(type!='')
		requete += " and num_type="+type+" ";
	if(prix!='')
		requete += " and prix "+prix+" ";//alert(requete);
	if(operation!=''){
		if(operation=='achat')
	  		requete += " and loc='0' ";
		else if(operation=='achat neuf')
	  		requete += " and loc='0' and num_neuf=2 ";
		else
			requete += " and loc='1' ";
	}
	if(piece!='')
		requete += " and piece "+piece+" ";//alert(requete);
	if(localisation!='')
		requete += " and ("+localisation+") ";
	//alert(requete);
			//alert(requete);
	if(requete.length > 0) {
		$.post("squelettes/rpc.php", {queryString: ""+requete+""}, function(data){
			if(data.length >0) {
				$('#nombre_biens').html(data);
			}
		});
	}
}