function returnWndSize() 
{
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  var result = new Array(myWidth,myHeight);
  return result;
}

function windowCenter(myId,event) 
{
	result=returnWndSize();
	var myWidth=result[0];
	var myHeight=result[1];
	var elementWidth=document.getElementById(myId).offsetWidth;
	var elementHeight=document.getElementById(myId).offsetHeight;
    var myScrollLeft=document.documentElement.scrollLeft;
	var myScrollTop=document.documentElement.scrollTop;
	var posX = myScrollLeft + myWidth - myWidth/2 - elementWidth/2;
	var posY = myScrollTop + myHeight - myHeight/2 - elementHeight/2;

	document.getElementById(myId).style.left=posX+"px";
	document.getElementById(myId).style.top=posY+"px";
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function centerPopup(element, clickElement) {
	var wndSize = returnWndSize();
	var wndWidth = wndSize[0];
	var wndHeight = wndSize[1];
	
	var me = document.getElementById(element);
	var height = me.offsetHeight;//hauteur de l'?l?ment ? positionner
	var width = me.offsetWidth;//largeur de l'?l?ment ? positionner
	
	var findedpos = findPos(document.getElementById(clickElement));

	var posY =  findedpos[1];//Calcul de la position en Y
	var posX = (wndWidth/2)  - (width/2);//Calcul de la position en X
	
	me.style.top=posY+"px";
	me.style.left=posX+"px";
}

function getTextHttpRequestObject()
{
	if (window.XMLHttpRequest)
	{
		return new XMLHttpRequest(); //Not IE
	}
	else if(window.ActiveXObject)
	{
		return new ActiveXObject("Microsoft.XMLHTTP"); //IE
	}
	else
	{
		alert("Your browser doesn't support the XmlHttpRequest object.  Better upgrade to Firefox.");
	}
}

function getXmlHttpRequestObject()
{

	if (window.XMLHttpRequest)// if Mozilla, Safari etc
	{
		httprequest=new XMLHttpRequest()
		if (httprequest.overrideMimeType)
			httprequest.overrideMimeType('text/xml')
	}
	else if (window.ActiveXObject)// if IE
	{
		try
		{
			httprequest=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				httprequest=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){}
		}
	}
	return httprequest
}


	
/*************************************************************************/

var cartRequest = getXmlHttpRequestObject();

function launchAddToCart(url, idProduct)
{
	if (cartRequest.readyState == 4 || cartRequest.readyState == 0)
	{
		var url2 = url + "?idProduct=" + idProduct;	
		//alert(url2);	
		cartRequest.open("GET", url2, true);		
		cartRequest.onreadystatechange = handleAddCartResponse;		
		cartRequest.send(null);		
	}
}


function handleAddCartResponse()
{
	var msgBlock = document.getElementById("messageBlock");
	

	if (cartRequest.readyState == 4) //Check to see if the XmlHttpRequests state is finished.
	{
		var xml = cartRequest.responseXML;
		//alert(cartRequest.responseText);		
		
		//******* alert message :  ***********/
		var idProduct = xml.getElementsByTagName('idProduct').item(0).firstChild.data;		
		//alert(idProduct);		
		centerPopup("messageBlock", "productBlock"+idProduct);	
		msgBlock.style.visibility = "visible";	
		
		var cartBack = document.getElementById("grisage"); 
		cartBack.style.visibility = "visible";
		
		var H = document.getElementById("mainDIV").offsetHeight;		
		var W = document.getElementById("mainDIV").offsetWidth;		
	  	cartBack.style.width   = W;	  	
	  	cartBack.style.height  = H;
	  	
      	
		//********* 	update cart display **************/
		var blocksize = document.getElementById("cartsize");	
		var blockcaption = document.getElementById("cartsizecaption");			
	  	var size = xml.getElementsByTagName('size').item(0).firstChild.data;		
		var caption = xml.getElementsByTagName('caption').item(0).firstChild.data;		 
		if (blocksize == null && blockcaption == null)
		{
			blocksize = opener.document.getElementById("cartsize");
			blockcaption = opener.document.getElementById("cartsizecaption");
		}
		blocksize.innerHTML = size;
		blockcaption.innerHTML = caption;
		
	}
}

function closeCartMessage(action)
{
	var msgBlock = document.getElementById("messageBlock");	
	msgBlock.style.visibility = "hidden";
	document.getElementById("grisage").style.visibility = "hidden";		
	if (opener != null && action == 1)
	{
			opener.location = "../displaycart.php";
			window.close();	
	}
}

function popup(url,name) 
{	
	window.open(url,name,'scrollbars=yes,resizable=yes,menubar=no,statusbar=no,left=150,top=200,screenX=150,screenY=200,width=600,height=480');
}


