function ieTrick(sep){
    d = new Date();
    trick = d.getYear() + "ie" + d.getMonth() + "t" + d.getDate() + "r" + d.getHours() + "i" 
             + d.getMinutes() + "c" + d.getSeconds() + "k" + d.getMilliseconds();
 
    if (sep != "?")
    {
             sep = "&";
    }
 
    return sep + "ietrick=" + trick;
}
 


// retourne un objet xmlHttpRequest.
function getXMLHTTP(){
  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) {
      try {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e1) {
        xhr = null;
      }
    }
  }
  else { alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); }
  return xhr;
}

/**********************************************
* Dï¿½but de la partie des fonctions Gï¿½nï¿½riques *
***********************************************/

/* Fonction qui permet de placer un attribut de type String en session */
function addStringParamInSession(paramName, paramValue) {
	if(_xmlHttp&&_xmlHttp.readyState!=0){
		_xmlHttp.abort()
	}
	var _xmlHttp=getXMLHTTP();
	if(_xmlHttp){
		var url = window.location.protocol + "//" + window.location.host;
		pathArray = window.location.pathname.split( '/' );
		if(pathArray[1]!=null) {
			url = url + "/" + pathArray[1];
		}
		if(pathArray[2]!=null && pathArray[2]!= "control") {
			url = url + "/" + pathArray[2];
		}
		url = url + "/control/";
		url = url + "addStringParamInSession?paramName="+paramName+"&paramValue="+paramValue+ieTrick("&");
		
		_xmlHttp.open("GET", url, true);
	    _xmlHttp.onreadystatechange=function() {
	      if(_xmlHttp.readyState==4 && _xmlHttp.responseXML && _xmlHttp.responseText!=null) {
	      	if(_xmlHttp.responseText.indexOf("OK")!=-1) {
	      		return true;
	      	} else {
	      		return false;
	      	}
	      }
	    };
	    // envoi de la requete
	    _xmlHttp.send(null)
	}
}



/* Fonction qui permet de cloner un objet de type script */
/*cloneScript = function(script) {
	if(script != null && typeof(script) == 'object') {
		
		var scriptclone = document.createElement('script');
		if(script.attributes.length > 0) { // boucle de copie des attributs du script dans le nouveau node
			for (var j in script.attributes) {
				
				if(typeof(script.attributes[j]) != 'undefined'
				 && typeof(script.attributes[j].nodeName) != 'undefined'
				 && script.attributes[j].nodeValue != null
				 && script.attributes[j].nodeValue != '' ) { // IE en a besoin sinon il copie des nodes vides
				 	scriptclone.setAttribute(script.attributes[j].nodeName, script.attributes[j].nodeValue);
				}
				
			}
		}
		scriptclone.text = script.text; // on copie le corps du script
		
		return scriptclone;
	} else {
		return null;
	}
}*/



/* Fonction generique de remplacement d'un DIV par un contenu HTML passe en parametre */
function replaceDiv(oldDivId, newDivId, contenuHTML) {
	var currentDiv = document.getElementById(oldDivId);
	
	if(currentDiv != null) {
		var parentDiv = currentDiv.parentNode;
		
		var newElement = document.createElement("div");
		newElement.id = "tempDiv";
		newElement.innerHTML = contenuHTML;
		
		parentDiv.appendChild(newElement);
		
		var elem = newElement.firstChild;
		
		parentDiv.replaceChild(elem, currentDiv);
		
		parentDiv.removeChild(newElement);
		
		currentDiv = document.getElementById(oldDivId);
		
		if(currentDiv != null) {
			
			var scripts = currentDiv.getElementsByTagName('script');
			
			if(scripts != false) { 
				// Il y a des balises SCRIPT dans le div ï¿½ remplacer donc on les parse pour qu'IE exï¿½cute le javascript.
				nbScript = scripts.length;
				for (var i=0; i<nbScript; i++) {
					// Attention le fait d'utiliser eval consomme le tableau de scripts c'est pourquoi on utilise toujours l'élément 0...
					eval(scripts[0].innerHTML);
				}
			}
		}
		
		moveOmbre();
		
		if(contenuHTML.contains("MenuBox")) {
			moveMenuBox();
		}
	
	}
	
}

/* Fonction permettant le rafraichissement du DIV de messages d'erreur */
function refreshMessagesDiv() {
	if(_xmlHttp&&_xmlHttp.readyState!=0){
		_xmlHttp.abort()
	}
	var _xmlHttp=getXMLHTTP();
	if(_xmlHttp){		  	
		var url = window.location.protocol + "//" + window.location.host;
		pathArray = window.location.pathname.split( '/' );
		if(pathArray[1]!=null) {
			url = url + "/" + pathArray[1];
		}
		if(pathArray[2]!=null && pathArray[2]!= "control") {
			url = url + "/" + pathArray[2];
		}
		url = url + "/control/";
		url = url + "refreshMessages"+ieTrick("?");
		
	    _xmlHttp.open("GET", url, true);
	    _xmlHttp.onreadystatechange=function() {
	    
	      if(_xmlHttp.readyState==4 && _xmlHttp.responseXML && _xmlHttp.responseText!=null) {
	      	// remplacement du div messagesContainer par un nouveau
	      	var currentMessagesDiv = document.getElementById("messagesContainer");
			
			if(currentMessagesDiv != null) {
				replaceDiv("messagesContainer", "messagesContainer", _xmlHttp.responseText);
			}
	      }
	    };
	    // envoi de la requete
	    _xmlHttp.send(null)
	}
}

/********************************************
* Fin de la partie des fonctions Gï¿½nï¿½riques *
*********************************************/


/************************************************************
* Dï¿½but de la partie consacrï¿½e ï¿½ la comparaison de produits *
*************************************************************/


function replaceProductsToCompare(productIdFrom, productIdTo, gothere)  {
	if(_xmlHttp&&_xmlHttp.readyState!=0){
		_xmlHttp.abort()
	}
	var _xmlHttp=getXMLHTTP();
	if(_xmlHttp){		  	
		var url = window.location.protocol + "//" + window.location.host;
		pathArray = window.location.pathname.split( '/' );
		if(pathArray[1]!=null) {
			url = url + "/" + pathArray[1];
		}
		if(pathArray[2]!=null && pathArray[2]!= "control") {
			url = url + "/" + pathArray[2];
		}
		url = url + "/control/";
		url = url + "compareProducts?REPLACE_FROM="+productIdFrom+"&REPLACE_TO="+productIdTo+ieTrick("&");
	    
		_xmlHttp.open("GET", url, true);
	    _xmlHttp.onreadystatechange=function() {
	      if(_xmlHttp.readyState==4 &&  _xmlHttp.responseText!=null) {
			replaceDiv("compareProductsContainer", null, _xmlHttp.responseText);
			if(gothere != null && gothere != '') {
				document.location = gothere;
			}
	      }
	    };
	    // envoi de la requete
	    _xmlHttp.send(null)
	}
}

function addRemoveProductToCompare(addRemove, productId) {
	if(_xmlHttp&&_xmlHttp.readyState!=0){
		_xmlHttp.abort()
	}
	var _xmlHttp=getXMLHTTP();
	if(_xmlHttp){		
		
		var url = window.location.protocol + "//" + window.location.host;
		pathArray = window.location.pathname.split( '/' );
		if(pathArray[1]!=null) {
			url = url + "/" + pathArray[1];
		}
		if(pathArray[2]!=null && pathArray[2]!= "control") {
			url = url + "/" + pathArray[2];
		}
		url = url + "/control/";
		url = url + "compareProducts?"+addRemove+"="+productId+ieTrick("&");
		
	    _xmlHttp.open("GET", url, true);
	    _xmlHttp.onreadystatechange=function() {
         if(_xmlHttp.readyState==4 &&  _xmlHttp.responseText!=null) {
         	replaceDiv("compareProductsContainer", null, _xmlHttp.responseText);
			refreshMessagesDiv();
	      }
	    };
	    // envoi de la requete
	    _xmlHttp.send(null)
	}
}

/**********************************************************
* Fin de la partie consacrï¿½e ï¿½ la comparaison de produits *
***********************************************************/



/**********************************************************
* Dï¿½but de la partie consacrï¿½e ï¿½ la recherche de produits *
***********************************************************/

function addRemoveSessionSearchCriteria(addRemove, searchCriteriaName, searchCriteriaValue, doRefresh, divIdToRefresh, divIdToReplaceWith, categoryId, subCategoryId) {
	if(_xmlHttp&&_xmlHttp.readyState!=0){
		_xmlHttp.abort()
	}
	var _xmlHttp=getXMLHTTP();
	if(_xmlHttp){
		var url = window.location.protocol + "//" + window.location.host;
		pathArray = window.location.pathname.split( '/' );
		if(pathArray[1]!=null) {
			url = url + "/" + pathArray[1];
		}
		if(pathArray[2]!=null && pathArray[2]!= "control") {
			url = url + "/" + pathArray[2];
		}
		url = url + "/control/";
		
		if(addRemove == "ADD") {
			url = url + "addSessionSearchCriteria";
		} else {
			url = url + "removeSessionSearchCriteria";
		} 
			
		url = url+"?searchCriteriaName="+searchCriteriaName+"&searchCriteriaValue="+searchCriteriaValue+"&category_id="+categoryId+"&subcategory_id="+subCategoryId+ieTrick("&");
		
	    _xmlHttp.open("GET", url, true);
	    _xmlHttp.onreadystatechange=function() {
	      if(_xmlHttp.readyState==4 && _xmlHttp.responseXML && _xmlHttp.responseText!=null) {
	      	replaceDiv("rigthBarKeywords", null, _xmlHttp.responseText);
	      	
	      	if(doRefresh == "true") {
	      		if(divIdToRefresh!=null && divIdToReplaceWith!=null) {
	      			launchSearchFromSessionSearchCriteria(divIdToRefresh, divIdToReplaceWith, categoryId, subCategoryId);
	      		} else {
	      			launchSearchFromSessionSearchCriteria('', '', categoryId, subCategoryId);
	      		}
			}
	      }
	    };
	    // envoi de la requete
	    _xmlHttp.send(null)
	}
}

function resetSessionSearchCriteria(categId, subcategId) {
	resetAddSessionSearchCriteria('true', null, null, null, null, categId, subcategId);
}

function resetAddSessionSearchCriteria(resetOnly, searchCriteriaName, searchCriteriaValue) {
	resetAddSessionSearchCriteria(resetOnly, searchCriteriaName, searchCriteriaValue, null, null, null, null);
}

function resetAddSessionSearchCriteria(resetOnly, searchCriteriaName, searchCriteriaValue, divIdToRefresh, divIdToReplaceWith, categId, subcategId) {
	if(_xmlHttp&&_xmlHttp.readyState!=0){
		_xmlHttp.abort()
	}
	var _xmlHttp=getXMLHTTP();
	if(_xmlHttp){
		
		var url = window.location.protocol + "//" + window.location.host;
		pathArray = window.location.pathname.split( '/' );
		if(pathArray[1]!=null) {
			url = url + "/" + pathArray[1];
		}
		if(pathArray[2]!=null && pathArray[2]!= "control") {
			url = url + "/" + pathArray[2];
		}
		url = url + "/control/";
		url = url + "resetSessionSearchCriteria?category_id="+categId+"&subcategory_id="+subcategId+ieTrick("&");
		
	    _xmlHttp.open("GET", url, true);
	    _xmlHttp.onreadystatechange=function() {
	      if(_xmlHttp.readyState==4 && _xmlHttp.responseXML && _xmlHttp.responseText!=null) {
	      	if (resetOnly == 'true') {
	      		launchSearchFromSessionSearchCriteria('', '', categId, subcategId);
	      		replaceDiv("rigthBarKeywords", null, _xmlHttp.responseText);
	      	} else {
	      		if(divIdToRefresh!=null && divIdToReplaceWith!=null && divIdToRefresh!='' && divIdToReplaceWith!='') {
	      			addRemoveSessionSearchCriteria('ADD', searchCriteriaName, searchCriteriaValue, "true", divIdToRefresh, divIdToReplaceWith, categId, subcategId);
	      		} else {
	      			addRemoveSessionSearchCriteria('ADD', searchCriteriaName, searchCriteriaValue, "true", '', '', categId, subcategId);
	      		}
	      	}
	      }
	    };
	    // envoi de la requete
	    _xmlHttp.send(null)
	}
}


function resetAddSessionSearchCriteriaThenOpenCateg(searchCriteriaName, searchCriteriaValue, subcategoryId, defaultUrl) {
	if(_xmlHttp&&_xmlHttp.readyState!=0){
		_xmlHttp.abort()
	}
	var _xmlHttp=getXMLHTTP();
	if(_xmlHttp){
		var url = window.location.protocol + "//" + window.location.host;
		pathArray = window.location.pathname.split( '/' );
		if(pathArray[1]!=null) {
			url = url + "/" + pathArray[1];
		}
		if(pathArray[2]!=null && pathArray[2]!= "control") {
			url = url + "/" + pathArray[2];
		}
		url = url + "/control/";
		url = url + "resetSessionSearchCriteriaThenAddANewOne?searchCriteriaName="+searchCriteriaName+"&searchCriteriaValue="+searchCriteriaValue;
		url = url + ieTrick("&");
		
		_xmlHttp.open("GET", url, true);
	    _xmlHttp.onreadystatechange=function() {
	      if(_xmlHttp.readyState==4 && _xmlHttp.responseXML && _xmlHttp.responseText!=null) {
	      		openCategory(subcategoryId, defaultUrl);
	      	}
	    };
	    // envoi de la requete
	    _xmlHttp.send(null)
	}
}

function launchSearchFromSessionSearchCriteria(divIdToReplace, newDivId, categoryId, subCategoryId) {
	if(_xmlHttp&&_xmlHttp.readyState!=0){
		_xmlHttp.abort()
	}
	var _xmlHttp=getXMLHTTP();
	if(_xmlHttp){
		var url = window.location.protocol + "//" + window.location.host;
		pathArray = window.location.pathname.split( '/' );
		if(pathArray[1]!=null) {
			url = url + "/" + pathArray[1];
		}
		if(pathArray[2]!=null && pathArray[2]!= "control") {
			url = url + "/" + pathArray[2];
		}
		url = url + "/control/";
		url = url + "launchSearchFromSessionSearchCriteria?category_id="+categoryId+"&subcategory_id="+subCategoryId+ieTrick("&");
		
		_xmlHttp.open("GET", url, true);
	    _xmlHttp.onreadystatechange=function() {
	      if(_xmlHttp.readyState==4 && _xmlHttp.responseXML && _xmlHttp.responseText!=null) {
	      	if(divIdToReplace != null && newDivId != null && divIdToReplace != '' && newDivId != '') {
	      		replaceDiv(divIdToReplace, newDivId, _xmlHttp.responseText);
	      	} else {
	      		replaceDiv("categoryDetailContainer", null, _xmlHttp.responseText);
	      	}
	      }
	    };
	    // envoi de la requete
	    _xmlHttp.send(null)
	}
}

/********************************************************
* Fin de la partie consacrï¿½e ï¿½ la recherche de produits *
*********************************************************/


/*************************************************
* Debut de la partie consacrï¿½e au menu de gauche *
**************************************************/

function openCategory(subcategoryId, defaultUrl) {
	if(_xmlHttp&&_xmlHttp.readyState!=0){
		_xmlHttp.abort()
	}
	var _xmlHttp=getXMLHTTP();
	if(_xmlHttp){
		var url = window.location.protocol + "//" + window.location.host;
		pathArray = window.location.pathname.split( '/' );
		if(pathArray[1]!=null) {
			url = url + "/" + pathArray[1];
		}
		if(pathArray[2]!=null && pathArray[2]!= "control") {
			url = url + "/" + pathArray[2];
		}
		url = url + "/control/";
		url = url + "isSearchConstraintInSession"+ieTrick("?");
		
		_xmlHttp.open("GET", url, true);
	    _xmlHttp.onreadystatechange=function() {
         if(_xmlHttp.readyState==4 &&  _xmlHttp.responseText!=null) {
	      	if(_xmlHttp.responseText.indexOf("OK")!=-1) {
	      		// dans ce cas, il y a dï¿½jï¿½ des options de recherche en session.
	      		// on place la subcategory en session et on lance la recherche
	      		//alert("open " + defaultUrl+"&doSearch=true");
	      		addStringParamInSession("SubCategorySearchConstraint", subcategoryId);
	      		document.location = defaultUrl+"&doSearch=true";
	      	} else {
	      		//alert("open default");
	      		// il n'y a pas d'option de recherche en session, on va vers l'url par dï¿½faut.
	      		document.location = defaultUrl;
	      	}
	      }
	    };
	    // envoi de la requete
	    _xmlHttp.send(null)
	}
}


/***********************************************
* Fin de la partie consacrï¿½e au menu de gauche *
************************************************/

/********************************************************************************************
* DEBUT de la partie consacrï¿½e ï¿½ la mise ï¿½ jour de la liste des produits avec previous/next *
*********************************************************************************************/

function refreshCategoryProductListByStep(category_id, subcategory_id, VIEW_INDEX, searchDone, moreUrlParameters) {
	if(_xmlHttp&&_xmlHttp.readyState!=0){
		_xmlHttp.abort()
	}
	var _xmlHttp=getXMLHTTP();
	if(_xmlHttp){
		var url = window.location.protocol + "//" + window.location.host;
		pathArray = window.location.pathname.split( '/' );
		if(pathArray[1]!=null) {
			url = url + "/" + pathArray[1];
		}
		if(pathArray[2]!=null && pathArray[2]!= "control") {
			url = url + "/" + pathArray[2];
		}
		url = url + "/control/";
		url = url + "categoryDetail?category_id="+category_id+"&subcategory_id="+subcategory_id+"&VIEW_INDEX="+VIEW_INDEX;
		url = url + "&searchDone="+searchDone;
		
		if(moreUrlParameters != null && moreUrlParameters != "") {
			url = url + moreUrlParameters;
		}
		
		url = url + ieTrick("&");
		
		_xmlHttp.open("GET", url, true);
	    _xmlHttp.onreadystatechange=function() {
	      if(_xmlHttp.readyState==4 && _xmlHttp.responseText!=null) {
	      	replaceDiv("categoryDetailContainer", null, _xmlHttp.responseText);
	      }
	    };
	    // envoi de la requete
	    _xmlHttp.send(null)
	}
}

/********************************************************************************************
* FIN de la partie consacrï¿½e ï¿½ la mise ï¿½ jour de la liste des produits avec previous/next *
*********************************************************************************************/



/***************************************************************
* Debut de la partie consacrï¿½e ï¿½ l'ordonnancement des produits *
****************************************************************/

function changeOrderResult(order, subCategoryId, defaultUrl) {
	if(_xmlHttp&&_xmlHttp.readyState!=0){
		_xmlHttp.abort()
	}
	var _xmlHttp=getXMLHTTP();
	if(_xmlHttp){
		var url = window.location.protocol + "//" + window.location.host;
		pathArray = window.location.pathname.split( '/' );
		if(pathArray[1]!=null) {
			url = url + "/" + pathArray[1];
		}
		if(pathArray[2]!=null && pathArray[2]!= "control") {
			url = url + "/" + pathArray[2];
		}
		url = url + "/control/";
		url = url + "addStringParamInSession?paramName=ORDER_RESULT_BY&paramValue="+order+ieTrick("&");
		
		_xmlHttp.open("GET", url, true);
	    _xmlHttp.onreadystatechange=function() {
	      if(_xmlHttp.readyState==4 && _xmlHttp.responseText!=null) {
	      	if(_xmlHttp.responseText.indexOf("OK")!=-1) {
	      		if(subCategoryId!=null && defaultUrl!=null && ""!=subCategoryId && ""!=defaultUrl) {
					openCategory(subCategoryId, defaultUrl);
				} else {	
					//document.location.reload();
					runLastSearch();
				}
	      	}
	      }
	    };
	    // envoi de la requete
	    _xmlHttp.send(null)
	}
}

function runLastSearch() {

	runLastSearchForm = document.getElementById("runLastProductSearch");
	
	if(runLastSearchForm != null) {
		runLastSearchForm.submit();
	}
}

/*************************************************************
* Fin de la partie consacrï¿½e ï¿½ l'ordonnancement des produits *
**************************************************************/
