
/*
** Popup d'ajout de produit au panier
*/

var _xmlHttp = null;

// @params: product_id (int), lang_id (int)

function addToCart(piProdId, piLangId, piQty) {
	
	 if(piQty) {} else {piQty = 1;}
	
	  if(_xmlHttp&&_xmlHttp.readyState!=0){
	    _xmlHttp.abort();
	  }
	  _xmlHttp=getXMLHTTP();
	  if(_xmlHttp){
	    //appel à l'url distante
	    _xmlHttp.open("GET", "http://www.comptoirdelhomme.com/index.php?pag=addtocart&product_id=" + piProdId + "&quantity=" + piQty, true);
	    _xmlHttp.onreadystatechange=function() {
	      if(_xmlHttp.readyState==4 && _xmlHttp.responseText) {
	    	  popInUp(390, 264, true, _xmlHttp.responseText);
	      }
	    };
	    // envoi de la requête
	    _xmlHttp.send(null);
	  }
}

function addToFavorite(piProdId, piLangId) {
	  if(_xmlHttp&&_xmlHttp.readyState!=0){
	    _xmlHttp.abort();
	  }
	  _xmlHttp=getXMLHTTP();
	  if(_xmlHttp){
	    //appel à l'url distante
	    _xmlHttp.open("GET", "http://www.comptoirdelhomme.com/index.php?pag=addtofavorite&product_id=" + piProdId + "&quantity=1", true);
	    _xmlHttp.onreadystatechange=function() {
	      if(_xmlHttp.readyState==4 && _xmlHttp.responseText) {
	    	  popInUp(390, 264, true, _xmlHttp.responseText);
	      }
	    };
	    _xmlHttp.send(null);
	  }
}

function popItUp(psPagName, psWindowName, piWidth, piHeight, pbCenter, options)
{
	var top, left;
	if (pbCenter == true)
	{
		top = (screen.height - piHeight) / 2;
		left = (screen.width - piWidth) / 2;
	}
	window.open (psPagName, psWindowName, "top="+top+",left="+left+",width="+piWidth+",height="+piHeight+","+options);
}

/**
 * Renvoie la taille de la surface utile de la fenetre en fonction du navigateur
 */
function getScreenSizes() {
      
      var loScreenSizes = {
            width:      window.innerWidth || document.documentElement.clientWidth || document.body.offsetWidth,
            height: window.innerHeight || document.documentElement.clientHeight || document.body.offsetHeight
      };
      
      return loScreenSizes;
}

/**
 * Renvoi les distances de scroll en fonction du naviateur
 */
function getScroll() {
      
      var loScroll = {
                  left:       window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
                  top: window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop 
      };
      
      return loScroll;
}


function	popInUp(piWidth, piHeight, pbCenter, psContent)
{
	var		oContainer = document.createElement("DIV");
	var		oElem = document.createElement("DIV");
	var 	loScreenSizes = getScreenSizes();
    var 	loScroll = getScroll();
	var		top = 0;
	var		left = 0;
	
	if (pbCenter == true)
	{
		left = ((loScreenSizes.width - piWidth) / 2) + loScroll.left;
		top = ((loScreenSizes.height - piHeight) / 2) + loScroll.top;
	}
	oContainer.setAttribute("id", "Background");
	oElem.setAttribute("id", "PopinContainer");
	if (document.all)
		oElem.style.setAttribute("cssText", "width: " + piWidth + "px; height: " + piHeight + "px; z-index: 0; top: " + top + "px; left: " + left + "px; position: absolute;");
	else
		oElem.setAttribute("style", "width: " + piWidth + "px; height: " + piHeight + "px; z-index: 0; top: " + top + "px; left: " + left + "px; position: absolute;");
	
	
	
	oElem.innerHTML = psContent;

	oContainer.style.width = '100%';
	oContainer.style.height = '100%';
	
	oContainer.appendChild(oElem);
	
	document.getElementsByTagName("BODY")[0].appendChild(oContainer);
}

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 { // XMLHttpRequest non supporté par le navigateur
	    alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
	  }
	  return xhr;
	}


