function getXhr(){

	var xhr = null;

	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;
	}
	return xhr
}

function noaccent(chaine) {
  temp = chaine.replace(/[àâä]/gi,"a");
  temp = temp.replace(/[éèêë]/gi,"e");
  temp = temp.replace(/[îï]/gi,"i");
  temp = temp.replace(/[ôö]/gi,"o");
  temp = temp.replace(/[ùûü]/gi,"u");
  temp = temp.replace(/[ç]/gi,"c");
  temp = temp.replace(/[ñ]/gi,"n");
  return temp;
}

function change_gammes(){

	var xhr = 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){

			document.getElementById('bloc_souscat').innerHTML = xhr.responseText;

			change_nb_products();
		}
	}

	collection_id = document.getElementById('collection_id').value;

	xhr.open("POST","ajax_update.php?type=change_gammes",true);

	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	xhr.send("collection_id="+collection_id);

}

function change_collections(){

	var xhr = 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){

			document.getElementById('bloc_cat').innerHTML = xhr.responseText;

			change_nb_products();
		}
	}

	gamme_id = document.getElementById('gamme_id').value;

	xhr.open("POST","ajax_update.php?type=change_collections",true);

	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	xhr.send("gamme_id="+gamme_id);

}

function change_convertible(){

	var xhr = 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){

			change_nb_products();
		}
	}

	convertible_value = document.getElementById('convertible_value').value;

	var new_convertible_value;

	if ( convertible_value == 1 )

	{
		new_convertible_value = 0;
	}
	else
	{
		new_convertible_value = 1;
	}

	document.getElementById('convertible_value').value = new_convertible_value;

	xhr.open("POST","ajax_update.php?type=change_convertible",true);

	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	xhr.send("new_convertible_value="+new_convertible_value);
}

function change_cuir(){

	var xhr = 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){
		
			change_nb_products();
		}

	}

	cuir_value = document.getElementById('cuir_value').value;

	var new_cuir_value;

	if ( cuir_value == 1 )

	{
		new_cuir_value = 0;
	}
	else
	{
		new_cuir_value = 1;
	}

	document.getElementById('cuir_value').value = new_cuir_value;

	xhr.open("POST","ajax_update.php?type=change_cuir",true);

	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	xhr.send("new_convertible_value="+new_cuir_value);

}

function change_taille(min)
{
	
	taille_min = document.getElementById('tb_SizeMin').value;
	taille_max = document.getElementById('tb_SizeMax').value;
	
	if ((taille_min != '') && (taille_min > 0) && (taille_min.length >= 2))
		 document.getElementById('taille_min_value').value = taille_min;
	else
		document.getElementById('taille_min_value').value = 0;
		
	if ((taille_max != '') && (taille_max > 0) && (taille_max.length >= 2))
		document.getElementById('taille_max_value').value = taille_max;
	else
		document.getElementById('taille_max_value').value = 0;
		
	if(min == true)
	{
		if (document.getElementById('taille_min_value').value != 0)
			change_nb_products();
	}
	else
	{
		if (document.getElementById('taille_max_value').value != 0)
			change_nb_products();
	}
}

function change_profondeur(min)
{
	prof_min = document.getElementById('tb_profMin').value;
	prof_max = document.getElementById('tb_profMax').value;
	
	if ((prof_min != '') && (prof_min > 0) && (prof_min.length >= 2))
		 document.getElementById('prof_min_value').value = prof_min;
	else
		document.getElementById('prof_min_value').value = 0;
		
	if ((prof_max != '') && (prof_max > 0) && (prof_max.length >= 2))
		document.getElementById('prof_max_value').value = prof_max;
	else
		document.getElementById('prof_max_value').value = 0;
		
	if(min == true)
	{
		if (document.getElementById('prof_min_value').value != 0)
			change_nb_products();
	}
	else
	{
		if (document.getElementById('prof_max_value').value != 0)
			change_nb_products();
	}
}

function change_searchTerm(term)
{
	term = document.getElementById('tb_texte').value;
	term = noaccent(term);
	
	document.getElementById('tb_texte').value = term;
	
	if ( (term != '') && (term.length > 2) )
	{
		change_nb_products();
	}
	else if (term == '')
		change_nb_products();
}

function change_tri(tri)
{
	document.getElementById('sort_value').value = tri;
	
	change_page(1);
	
	change_search_products();
}

function change_nb_products(){

	$.ajax({

	   type: "POST",

	   url: "ajax_update.php?type=change_nb_products",

	   data: $('#advanced').formSerialize(),

	   success: function(msg){

		document.getElementById('nb_products').innerHTML = msg;
	   }
	 });
}

function change_search_products(){

	$.ajax({

	   type: "POST",

	   url: "ajax_update.php?type=change_search_products",

	   data: $('#advanced').formSerialize(),

	   success: function(msg){

		 document.getElementById('affiche_search_products').innerHTML = msg;
	   }
	 });
}

function change_page(value)
{
	document.getElementById('current_page').value=value;
	change_search_products();
}


