var ajaxRequest;
var baseUrlJ = "http://www.objet-publicitaire.info";
var baseUrlJ2 = "http://www.objet-publicitaire.info";

/////////////////////// get AJAX XmlHttpObject  //////////////////////////
function getXmlHttpObject(){
	var httpObject = null;
	try{
  		// Firefox, Opera 8.0+, Safari
  		httpObject = new XMLHttpRequest();
  	}
	catch (e){
  		// Internet Explorer
  		try{
    		httpObject = new ActiveXObject("Msxml2.XMLHTTP");
   	 	}
 	 	catch (e){
   	 		httpObject = new ActiveXObject("Microsoft.XMLHTTP");
    	}
  	}
	return httpObject;
}

function IsNumeric(str){
   var ValidChars = "0123456789.,+- ";
   var IsNumber=true;
   var oneChar;
   for (i = 0; i < str.length; i++) 
      { 
      oneChar = str.charAt(i); 
      if (ValidChars.indexOf(oneChar) == -1) 
         {
         IsNumber = false;
		 return IsNumber;
         }
      }
   return IsNumber;
}


/* //////////////////// open & close the select dummi ///////////// */
function eClicked(eToHandle){
	var e = document.getElementById(eToHandle);
	if(e.style.display == "none" || e.style.display == ""){
		e.style.display = "block";
	}
	else{
		e.style.display = "none";
	}
}

function marqueTabClicked(){
	if(document.avancee.selectedCats.value != ""){
		var eToHandle = "avanceeMarqueList";
	}
	else{
		var eToHandle = "avanceeMarqueFullList";
	}
	eClicked(eToHandle)
}

/* //////////////////////////////////// */
function categorieSelected(catObj, catId){
	var selectedC = document.avancee.selectedCats;
	if(catObj.firstChild.className != "") {
		catObj.firstChild.className = "";
		if(selectedC.value.search(","+catId) != -1){
			selectedC.value = selectedC.value.replace(","+catId, "");
		}
		else if(selectedC.value.search(catId+",") != -1) {
				selectedC.value = selectedC.value.replace(catId+",", "");
		}
		else{
			selectedC.value = selectedC.value.replace(catId, "");
		}
	}
	else{
		catObj.firstChild.className = "boxSelected";
		if(selectedC.value == ""){
			selectedC.value += catId;
		}
		else{
			selectedC.value += "," + catId;
		}	
	}
	
	if(selectedC.value != ""){
		if(document.avancee.selectedMarque.value == ""){
			document.avancee.startedFrom.value = "cat";
		}
		getAjaxCount("catSelected");
	}
	else{
		document.getElementById('avanceeFamList').innerHTML = "";
		document.avancee.selectedFams.value = "";
		document.avancee.selectedCats.value = "";
		document.getElementById('avanceeSelectionDesc').innerHTML = "";
		if(document.avancee.startedFrom.value != "marque") {
			document.avancee.selectedMarque.value = "";
			document.getElementById('avanceeMarqueList').style.display = "none";
			// reseting the selected marques
			var amfl = document.getElementById('avanceeMarqueFullList').getElementsByTagName('li');
			for (var m = 0; m < amfl.length; m++) {
				amfl[m].firstChild.className = "";
			}
			document.getElementById('mrqFull_all').firstChild.className = "boxSelected";
		}
		else{
			getAjaxCount("prix");
		}
		
		if (document.avancee.selectedPrix.value != "") {
			getAjaxCount("catSelected");
		}
		else {
			document.getElementById('avanceeButtonCount').innerHTML = "0 produits";
			document.avancee.totalCount.value = "";			
			document.getElementById('avanceeLoader').style.display = "none";
		}
	}
}


/* ///////////////// famille selected ////////////// */
function familleSelected(famObj, famId){
	var selectedF = document.avancee.selectedFams;
	if(selectedF.value.search(famId) != -1) {
		famObj.firstChild.className = "";
		if(selectedF.value.search(","+famId) != -1){
			selectedF.value = selectedF.value.replace(","+famId, "");
		}
		else if(selectedF.value.search(famId+",") != -1) {
				selectedF.value = selectedF.value.replace(famId+",", "");
		}
		else{
			selectedF.value = selectedF.value.replace(famId, "");
		}
		
		if(selectedF.value == ""){
			document.getElementById('fam_all').firstChild.className = "boxSelected";
		}
	}
	else{
		famObj.firstChild.className = "boxSelected";
		document.getElementById('fam_all').firstChild.className = "";
		if(selectedF.value == ""){
			selectedF.value += famId;
		}
		else{
			selectedF.value += "," + famId;
		}	
	}
	getAjaxCount("famSelected");
}

function familleAllSelected(famAllObj){
	var selectedF = document.avancee.selectedFams;
	if(selectedF.value != "" ){
		// reseting the selected familles
		var rf = document.getElementById('avanceeFamList').getElementsByTagName('li');
		for (var m = 0; m < rf.length; m++) {
			rf[m].firstChild.className = "";
		}
		famAllObj.firstChild.className = "boxSelected";
		selectedF.value = "";
		getAjaxCount("famSelected");
	}
}


/* //////////// affinage par prix ///////// */
/*
var prixTimer;
function filterPrixTimer(){
	if(prixTimer) {
		window.clearTimeout(prixTimer);
	}
	
	if(document.avanceePrixForm.prixFin.value != "" && document.avanceePrixForm.prixFin.value != "0") {
		prixTimer = window.setTimeout(ajaxFilterPrix, 800);
	}
	else if(document.avancee.selectedPrix.value != ""){
		prixTimer = window.setTimeout(emptyPrixFin, 800);
	}
}

function ajaxFilterPrix(){
	prixTimer = null;
	var prixFin = document.avanceePrixForm.prixFin.value;
	var prixCom = 0;
	if(document.avanceePrixForm.prixCom.value != "" && document.avanceePrixForm.prixCom.value != 0){
		prixCom = document.avanceePrixForm.prixCom.value;
	}
	
	if(document.avanceePrixForm.prixFin.value != "") {
		document.avancee.selectedPrix.value = prixCom + "_" + prixFin;
	}
	
	getAjaxCount("prix");
}
*/

///////////////////////

var prixTimer;
function filterPrixTimer(){
	if(prixTimer) {
		window.clearTimeout(prixTimer);
	}
	prixTimer = window.setTimeout(ajaxFilterPrix, 800);
}

function ajaxFilterPrix(){
	prixTimer = null;
	var pfCom = document.avanceePrixForm.prixCom.value;
	var pfFin = document.avanceePrixForm.prixFin.value;
	if(pfCom != "" ||  pfFin != ""){
		pfFin = (IsNumeric(pfFin)) ? pfFin : "";	
		if((pfFin != "" && parseInt(pfCom) > parseInt(pfFin)) || (pfCom == "" || pfCom < 0)){
			pfCom = "0";
			document.avanceePrixForm.prixCom.value = 0;
		}
		document.avancee.selectedPrix.value = pfCom + "_" + pfFin;
	}
	else{
		document.avancee.selectedPrix.value = "";
	}
	getAjaxCount("prix");
}


function marqueSelected(mrqObj, mrqId){
	var selectedM= document.avancee.selectedMarque;
	if(selectedM.value.search(mrqId) != -1) {
		mrqObj.firstChild.className = "";
		if(selectedM.value.search(","+mrqId) != -1){
			selectedM.value = selectedM.value.replace(","+mrqId, "");
		}
		else if(selectedM.value.search(mrqId+",") != -1) {
				selectedM.value = selectedM.value.replace(mrqId+",", "");
		}
		else{
			selectedM.value = selectedM.value.replace(mrqId, "");
		}
		
		if(selectedM.value != ""){
			getAjaxCount("prix");
		}
		else{
			if(document.avancee.selectedFams.value != "" || document.avancee.selectedCats.value != ""){
				document.getElementById('mrq_all').firstChild.className = "boxSelected";
				getAjaxCount("prix");
			}
			else{
				if(ajaxRequest) {
					ajaxRequest.abort(); // abort any ajax request that is still running
					document.getElementById('avanceeLoader').style.display = "none";
				}
				
				document.getElementById('avanceeButtonCount').innerHTML = "0 produits";
				document.avancee.totalCount.value = "";
				document.getElementById('mrqFull_all').firstChild.className = "boxSelected";
				setSelection();
			}
		}
	}
	else{
		if(document.getElementById('avanceeMarqueList').style.display == "block"){
			document.getElementById('mrq_all').firstChild.className = "";
		}
		else{
			document.getElementById('mrqFull_all').firstChild.className = "";
		}
		
		mrqObj.firstChild.className = "boxSelected";
		if(selectedM.value == ""){
			selectedM.value += mrqId;
		}
		else{
			selectedM.value += "," + mrqId;
		}	
		if(document.avancee.selectedCats.value == ""){
			document.avancee.startedFrom.value = "marque";
		}
		getAjaxCount("prix");
	}		
}

function marqueAllSelected(allMrqObj){
	var selectedM = document.avancee.selectedMarque;
	if(selectedM.value != "" ){
		// reseting the selected marques
		if(document.getElementById('avanceeMarqueList').style.display == "block"){
			var rm = document.getElementById('avanceeMarqueList').getElementsByTagName('li');
		}
		else{
			var rm = document.getElementById('avanceeMarqueFullList').getElementsByTagName('li');
		}
		
		for (var m = 0; m < rm.length; m++) {
			rm[m].firstChild.className = "";
		}
		allMrqObj.firstChild.className = "boxSelected";
		selectedM.value = "";
		setSelection();
		if(document.avancee.selectedCats.value != "" || document.avancee.selectedPrix.value != ""){
			getAjaxCount("prix");
		}
	}
}


/* /////////////////// get the ajax count and details //////////////////////////////// */
function getAjaxCount(from){
	if(!ajaxRequest) {
		ajaxRequest = getXmlHttpObject();
	}
	else if (ajaxRequest.readyState != 0){
		ajaxRequest.abort(); 
	}
	
	var urlStr = "str_start##";
	if (document.avancee.selectedFams.value != "") {
		urlStr += "&fams=" + document.avancee.selectedFams.value;
	}
	if(document.avancee.selectedCats.value != ""){
		urlStr += "&cats=" + document.avancee.selectedCats.value;
	}

	if(document.avancee.selectedPrix.value != ""){
		urlStr += "&prix=" + document.avancee.selectedPrix.value;
		urlStr += "&qte=" + document.avanceeQte.quantite.value;
	}
	
	if(document.avancee.selectedMarque.value != ""){
		urlStr += "&marques=" + document.avancee.selectedMarque.value;
	}
	
	if(urlStr != "str_start##") {
		document.getElementById('avanceeLoader').style.display = "block";
		urlStr = urlStr.replace("str_start##&", "?");
		urlStr += "&from=" + from;
		urlStr += "&sFrom=" + document.avancee.startedFrom.value;
		
		ajaxRequest.onreadystatechange = responseAjaxCount;
		ajaxRequest.open("GET", baseUrlJ + "/ajax/ajax_recherchAvancee.php" + urlStr, true);
		ajaxRequest.send(null);
	}
}

function responseAjaxCount(){
	if (ajaxRequest.readyState == 4 && ajaxRequest.status == 200 && ajaxRequest.responseText != "") {
		var response = ajaxRequest.responseText.split("#");
		
		if (response[0] != -1) {
			document.getElementById('avanceeButtonCount').innerHTML = response[0] + " produits";
			document.avancee.totalCount.value = response[0];
		}
		if (response[1] != "" && document.avancee.selectedCats.value != "") {
			document.avancee.selectedFams.value = response[2];
			document.getElementById('avanceeFam').style.backgroundColor = "#ffffff";
			document.getElementById('avanceeFamList').innerHTML = response[1];
			document.getElementById('avanceeFamList').style.display = "block";
		}
		if (response[3] != "") {
			if(document.getElementById('avanceeMarqueFullList').style.display == "block"){
				document.getElementById('avanceeMarqueFullList').style.display = "none";
				document.getElementById('avanceeMarqueList').style.display = "block";
			}	
			document.getElementById('avanceeMarqueList').style.backgroundColor = "#ffffff";
			document.getElementById('avanceeMarqueList').innerHTML = response[3];
			document.avancee.selectedMarque.value = response[4];
		}
		else 
			if (response[4] == "-1") {
				document.getElementById('avanceeMarqueList').innerHTML = "";
				document.avancee.selectedMarque.value = "";
			}
		document.getElementById('avanceeLoader').style.display = "none";
	}
	
	setSelection();
}

function setSelection(){	
		// selection
		var selection = "<span>Vos critères: </span>";
		if(document.avancee.selectedCats.value != ""){
			selection += "<p>Categories:</p>";
			var selectionCats = document.avancee.selectedCats.value.split(",");
			for(var tt=0; tt<selectionCats.length; tt++){
				if(tt!=0){
					selection += ", ";
				}
				selection += document.getElementById("cat_" + selectionCats[tt]).getAttribute("value");
			}
		}
		if(document.avancee.selectedFams.value != ""){
			selection += "<p>Familles:</p>";
			var selectionFams = document.avancee.selectedFams.value.split(",");
			for(var tt=0; tt<selectionFams.length; tt++){
				if(tt!=0){
					selection += ", ";
				}
				selection += document.getElementById("fam_" + selectionFams[tt]).getAttribute("value");
			}
		}
		
		if(document.avanceePrixForm.prixFin.value != "" && document.avanceePrixForm.prixFin.value != "0") {
			selection += "<p>Prix:</p>";
			if(document.avanceePrixForm.prixCom.value != ""){
				selection += "De " + document.avanceePrixForm.prixCom.value;
			}
			else{
				selection += "De 0";
			}
			selection += " à " + document.avanceePrixForm.prixFin.value + " euro/pc";
		}
		
		if(document.avanceeQte.quantite.value  != ""){
			selection += "<p>Quantité:</p> " + document.avanceeQte.quantite.value + " pcs";
		}
			
		if(document.avancee.selectedMarque.value != ""){
			selection += "<p>Marques:</p>";
			var selectionMarques = document.avancee.selectedMarque.value.split(",");
			for(var tt=0; tt<selectionMarques.length; tt++){
				if(tt!=0){
					selection += ", ";
				}
				selection += document.getElementById("mrq_" + selectionMarques[tt]).getAttribute("value");
			}
		}
		document.getElementById('avanceeSelectionDesc').innerHTML = selection;
		document.getElementById('avanceeSelectionDesc').style.display = "block";
}


/* /////////// afficher ///////////////// */
function affichez(){
	if (document.avancee.totalCount.value > 0) {
		var urlStr = "?total=" + document.avancee.totalCount.value;
		if (document.avancee.selectedFams.value != "") {
			urlStr += "&f=" + document.avancee.selectedFams.value;
		}
		if (document.avancee.selectedCats.value != "") {
			urlStr += "&c=" + document.avancee.selectedCats.value;
		}
		
		if (document.avancee.selectedMarque.value != "") {
			urlStr += "&m=" + document.avancee.selectedMarque.value;
		}
		
		if (document.avanceePrixForm.prixFin.value != "") {
			var prixCom = (document.avanceePrixForm.prixCom.value != "") ? document.avanceePrixForm.prixCom.value : "0";
			urlStr += "&pr=" + prixCom + "_" + document.avanceePrixForm.prixFin.value;
		}
		
		if (document.avanceeQte.quantite.value != "") {
			urlStr += "&q=" + document.avanceeQte.quantite.value;
		}
		
		setTimeout(function(){
   			window.location = "./resultat_de_recherche.html" + urlStr;
		}, 0);
	}
	else{
		alert("Aucun produit n'est disponible. Veuillez modifier votre sélection");
	}
}

/* /////////// reset /////////////////// */
function resetAvancee(){
	ajaxRequest = getXmlHttpObject();
	ajaxRequest.onreadystatechange = responseResetAvancee;
	ajaxRequest.open("GET", baseUrlJ + "/ajax/ajax_resetAvancee.php", true);
	ajaxRequest.send(null);
}

function responseResetAvancee(){
	if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
		document.avancee.selectedFams.value = "";
		document.avancee.totalCount.value = "";
		document.avancee.selectedPrix.value = "";
		document.avancee.selectedMarque.value = "";
		document.avancee.startedFrom.value = "";
		document.avanceePrixForm.prixCom.value = "";
		document.avanceePrixForm.prixFin.value = "";
		document.avanceeQte.quantite.value = "";
		
		if(document.avancee.selectedCats.value != "") {
			document.avancee.selectedCats.value = "";
			// reseting the selected cats
			var rc = document.getElementById('avanceeCatList').getElementsByTagName('li');
			for (var m = 0; m < rc.length; m++) {
				rc[m].firstChild.className = "";
			}
		}	
		document.getElementById('avanceeFamList').innerHTML = "";
		document.getElementById('avanceeMarqueList').style.display = "none";
		document.getElementById('avanceeMarqueFullList').style.display = "none";
		document.getElementById('avanceeSelectionDesc').style.display = "none";		
		document.getElementById('avanceeButtonCount').innerHTML = "0 produits";	
	}	
}

/* ///////////////////////////////////// */
function servClicked(head, box){
		if($(head).hasClass("servOpen")){
			$(head).removeClass("servOpen");
			$(head).addClass("servClose");
			$("#" + box).slideDown(100);
		}
		else{
			$(head).removeClass("servClose");
			$(head).addClass("servOpen");
			$("#" + box).slideUp(100);
		}
}

/* //////////////////////////////////// */
var cbTimer;
var currentPage;
var bCategorie = "";
function catalogueBudget(page, bc){
	document.getElementById('catalogueLoader').style.display = "block";
	currentPage = page;
	if(bc != ""){
		bCategorie = bc;
	}
	if(cbTimer) {
		window.clearTimeout(cbTimer);
	}
	cbTimer = window.setTimeout(ajaxCatalogueBudget, 1000);

}

function ajaxCatalogueBudget(){
	cbTimer = null;
	var pfCom = document.btPrixForm.btPrixCom.value;
	var pfFin = document.btPrixForm.btPrixFin.value;
	if (pfCom != "" || pfFin != "") {
		pfFin = (IsNumeric(pfFin)) ? pfFin : "";
		if ((pfFin != "" && parseInt(pfCom) > parseInt(pfFin)) || (pfCom == "" || pfCom < 0)) {
			pfCom = "0";
			document.btPrixForm.btPrixCom.value = 0;
		}
		var prix = pfCom + "_" + pfFin;
		var urlStr = "?prix=" + prix + "&cPage=" + currentPage;
	}
	else{
		var urlStr = "?cPage=" + currentPage;
	}
	
	if(bCategorie != ""){
		urlStr += "&dCat=" + bCategorie
	}
	ajaxRequest = getXmlHttpObject();
	ajaxRequest.onreadystatechange = responseCatalogueBudget;
	ajaxRequest.open("GET", baseUrlJ + "/ajax/ajax_catalogueBudget.php" + urlStr, true);
	ajaxRequest.send(null);
	//window.open(baseUrlJ + "/ajax/ajax_catalogueBudget.php" + urlStr);
}

function responseCatalogueBudget(){	
	if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200){
		var response = ajaxRequest.responseText.split("#");
		var cels = document.getElementsByName('proCountPane');
		for(var x = 0; x < cels.length; x++) {
			 var countElement = cels[x].getElementsByTagName('span');
			 countElement[0].innerHTML = "(0 Produits)";
			 countElement[0].className = "zeroProduit";
		}	
		
		var toutCount = 0;
		for(var z=0; z<response.length; z++){
			var cd = response[z].split("_");
			document.getElementById('cbProCount' + cd[0]).innerHTML = "(" + cd[1] + " produits)";
			document.getElementById('cbProCount'  + cd[0]).className = "";
			toutCount = toutCount + parseInt(cd[1]);
		}

		document.getElementById('cbProCount0').innerHTML = "(" + toutCount + " produits)";
		document.getElementById('catalogueLoader').style.display = "none";
	}
}

/* check */
function parBudgetLink(){
	var prixFin = document.btPrixForm.btPrixFin.value;
	var prixCom = 0;
	if(document.btPrixForm.btPrixCom.value != ""){
		prixCom = document.btPrixForm.btPrixCom.value;
	}
	
	if(prixFin != "" && IsNumeric(prixFin) && IsNumeric(prixCom)){
		window.location = baseUrlJ +  "/catalogue-publicitaire/touts-les-objets-publicitaire_0.0.html?pr=" + prixCom + "_" + prixFin;
	}
	else{
		
	}
}



function isPrixFilter(id, link){
	if(document.getElementById('cbProCount' + id).innerHTML != "(0 Produits)") {
		var prixFin = document.btPrixForm.btPrixFin.value;
		var prixCom = 0;
		if (document.btPrixForm.btPrixCom.value != "") {
			prixCom = document.btPrixForm.btPrixCom.value;
		}
		if ((prixFin != "" && IsNumeric(prixFin)) || (prixCom != "" && IsNumeric(prixCom))) {
			window.location = link + "?pr=" + prixCom + "_" + prixFin;
			return false;
		}
		else {
			return true;
		}
	}
	else{
		alert("Aucun produit n'est disponible avec le budget que vous avez spécifié.");
		return false;
	}
	
}

/* remove if not needed */
function onloadFilterPrix(){
	filterPrixTimer();
}


/////////////////////////////////////////
//
function showColorisNom(nom, cssId){
	if(cssId == ""){
		cssId = 'colorisHeadNom';
	}
	document.getElementById(cssId).innerHTML = "&nbsp;&nbsp;" + nom;
}

/////////////////////////////////////////
//
function clearColorisNom(){
	document.getElementById('colorisHeadNom').innerHTML = "";
}


/* //////////////////////////// */
function showInteractive(tabsImageClass, interPane){
	if($("#" + interPane).css("display") == "none") {
		$("#interactivePages > div").css({"display":"none"});
		$("#" + interPane).fadeIn("100");
		$("#interactiveStages").removeClass();
		$("#interactiveStages").addClass(tabsImageClass);
			
	}
}

function showInterAnswers(tabSelected, answerPane){
	if($("#" + answerPane).css("display") == "none") {
		$("#" + answerPane).slideDown(250);	
		$(tabSelected).addClass("interAnswerSelected");		
	}
	else{
		$("#" + answerPane).slideUp(250);	
		$(tabSelected).removeClass("interAnswerSelected");
		
	}
}






