function GetXmlHttpObject()
{
	var xmlhttp=null;
	try
  	{
  		// Firefox, Opera 8.0+, Safari
  		xmlhttp=new XMLHttpRequest();
  	}
	catch (e)
  	{
  		// Internet Explorer
  		try
    	{
    		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
	  	catch (e)
    	{
		    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
  	}
	return xmlhttp;
}

var categoryProductListing_xmlHttp = null;
function ajax_Categorie(catid,region,ville,categorie,lettre)
{
	categoryProductListing_xmlHttp=GetXmlHttpObject();
	
	if (categoryProductListing_xmlHttp==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	}
  	
    	var parameters = '';
        
        if(catid!="-1")
            {
				var parameters 			= parameters+"cat="+catid;
				
				if(catid=="7")
					{
						if(document.getElementById('calendrierSet') != null && document.getElementById('calendrierSet').value == "1")
							{
								var parameters 			= parameters+"&calendrierSet=0";
							}
						else if(document.getElementById('calendrierSet') != null && document.getElementById('calendrierSet').value == "0")
							{
								var parameters 			= parameters+"&calendrierSet=1";
							}
					}
            }
        if(region!="-1")
            {
            var parameters 			= parameters+"&region="+region; 
            }
        if(ville!="-1")
            {
            var parameters 			= parameters+"&ville="+ville;          
            }
        if(categorie!="-1")
            {
            var parameters 			= parameters+"&categorie="+categorie;
            }
        if(lettre!='-1')
            {
            var parameters 			= parameters+"&lettre="+lettre;
            }

        var url='includes/categorie_ajax.php';
		url=url+"?sid="+Math.random();
                url=url+parameters;              

	try{	
		categoryProductListing_xmlHttp.onreadystatechange=categoryProductListing_ReadyChange;
		categoryProductListing_xmlHttp.open("POST",url,true);
		
		
		categoryProductListing_xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		categoryProductListing_xmlHttp.setRequestHeader("Content-length", parameters.length);
		categoryProductListing_xmlHttp.setRequestHeader("Connection", "close");
		
		categoryProductListing_xmlHttp.send(parameters);
	}catch(ex)
	{
		alert = 'SERVER CATCH ERROR : '+ex.message;
	}
}

function categoryProductListing_ReadyChange()
{
	// check is it the final ajax state
	if (categoryProductListing_xmlHttp.readyState!=4)
	{ 
		document.getElementById('categorie').innerHTML = "<img src=images/ajax-loader.gif></img>";
                return;
	}
	// is the responce is OK ?
	if(categoryProductListing_xmlHttp.status!=200)
  	{
 		document.getElementById('categorie').innerHTML = "Server Problem...";
  		return;
  	}
	var message = '';
	try
	{		
        document.getElementById('categorie').innerHTML = categoryProductListing_xmlHttp.responseText;
	}
	catch(ex)
	{
		message = 'SERVER CATCH ERROR : '+ex.message;
	}
}

function preSearch() {
    //Put the form data into a variable
	var theQuery = document.getElementById('recherche').value;
	var theQuery1 = document.getElementById('regions').value;
	var theQuery2 = document.getElementById('villes').value;
	//var theQuery = document.rechercheregion.getElementById('recherche_region').value;
	
    //If the form data is *not* blank, query the DB and return the results
	if(theQuery !== "") {
        //Change the content of the "result" DIV to "Searching..."
        //This gives our user confidence that the script is working if it takes a moment for the result to be returned. However the user will likely never see this...
		document.getElementById('result').innerHTML = "Recherche...";
		
        //This sets a variable with the URL (and query strings) to our PHP script
		var url = 'includes/recherche_ajax.php?q='+ theQuery;
		url=url+"?sid="+Math.random();
		//var url1 = 'includes/recherche_ajax.php?q="+theQuery+"&q_regions="+theQuery1';
		//var url2 = 'includes/recherche_ajax.php?q_ville=' + theQuery2;
        //Open the URL above "asynchronously" (that's what the "true" is for) using the GET method
		xmlhttp.open('GET', "includes/recherche_ajax.php?q="+theQuery+"&q_regions="+theQuery1+"&q_ville="+theQuery2, true);
		//xmlhttp.open('GET', url1, true);
		//xmlhttp.open('GET', url2, true);
        //Check that the PHP script has finished sending us the result
		
		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                //Replace the content of the "result" DIV with the result returned by the PHP script
				document.getElementById('result').innerHTML = xmlhttp.responseText + ' ';
			} else {
                //If the PHP script fails to send a response, or sends back an error, display a simple user-friendly notification
				//document.getElementById('result').innerHTML = 'Aucun résultat !';
			}
		};
		xmlhttp.send(null);  
	}
}

function preSearchRegion() {
    //Put the form data into a variable
	var theQuery1 = document.getElementById('recherche_region').value;
	//var theQuery = document.rechercheregion.getElementById('recherche_region').value;
	
    //If the form data is *not* blank, query the DB and return the results
	if(theQuery1 !== "") {
        //Change the content of the "result" DIV to "Searching..."
        //This gives our user confidence that the script is working if it takes a moment for the result to be returned. However the user will likely never see this...
		document.getElementById('result').innerHTML = "Recherche...";
		
        //This sets a variable with the URL (and query strings) to our PHP script
		var url = 'includes/recherche_ajax.php?q_region=' + theQuery1;
        //Open the URL above "asynchronously" (that's what the "true" is for) using the GET method
		xmlhttp.open('GET', url, true);
        //Check that the PHP script has finished sending us the result
		
		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                //Replace the content of the "result" DIV with the result returned by the PHP script
				document.getElementById('result').innerHTML = xmlhttp.responseText + ' ';
			} else {
                //If the PHP script fails to send a response, or sends back an error, display a simple user-friendly notification
				//document.getElementById('result').innerHTML = 'Aucun résultat !';
			}
		};
		xmlhttp.send(null);  
	}
}
