function getReadyStateHandler(req,displayqty,quantity,price,description,loggedin,unit,stepQty) 
{
  // Return an anonymous function that listens to the 
  // XMLHttpRequest instance
  return function () {

    // If the request's status is "complete"
    if (req.readyState == 4) {
      
      // Check that a successful server response was received
      if (req.status == 200) {

        // Pass the XML payload of the response to the 
        // handler function
        //responseXmlHandler(req.responseXML);
        updateMiniStatus(displayqty,quantity,price,stepQty);
		toggle('myLayer2');
		if(getCookieValue('BasketInfo') != 'true')
		{ popupwindow(this,'open',description,quantity,price,unit,stepQty); }		
      } else {

        // An HTTP problem has occurred
        alert("HTTP error: "+req.status);
      }
    }
  }
}

function getReadyStateHandlerOffer(req,displayqty, quantity,description,loggedin,unit,stepQty) 
{

  // Return an anonymous function that listens to the 
  // XMLHttpRequest instance
  return function () {

    // If the request's status is "complete"
    if (req.readyState == 4) {
      
      // Check that a successful server response was received
      if (req.status == 200) {

        // Pass the XML payload of the response to the 
        // handler function
        //responseXmlHandler(req.responseXML);        
        updateMiniStatusOffer(displayqty,stepQty);
        clearQtys();
		toggle('offerLayer'); 		
		if(getCookieValue('OfferInfo') != 'true')
		{ popupwindowoffer(this,'open',description,quantity,unit,stepQty); }		
      } else {
        // An HTTP problem has occurred
        alert("HTTP error: "+req.status);
      }
    }
  }
}

function updateMiniStatus(displayqty,quantity,price,stepQty)
{
	var productCountElement = document.getElementById("basketProductCount");
	var totalbasket = document.getElementById("totalbasket");
	var count = 0;
	var dplCount = 0;
	
	quantity = replaceComma(quantity);
	if(parseFloat(quantity) < parseFloat(stepQty))
	{
		quantity = stepQty
	}
	else if((quantity % stepQty) != 0)
	{
		var restVal = quantity % stepQty;
		quantity = (stepQty - restVal) + restVal + (quantity-restVal);
	}		
	
	if(quantity == "")
	{	
		count = 1;		
		dplCount = 1;
	}	
	else
	{
		count = quantity * 1;
		dplCount = replaceComma(displayqty) * 1;
	}
	var qty = (productCountElement.innerHTML * 1) + count;
	var dplQty = (productCountElement.innerHTML * 1) + dplCount;
	productCountElement.innerHTML = dplQty;
	
	
	if(productCountElement != null && totalbasket !=null)
	{
		var total = (convertToNum(totalbasket.innerHTML) * 1 ) + (count * replaceComma(replacePointWithBlank(price)));
		var result= (Math.round(total*100)/100).toFixed(2);  
		totalbasket.innerHTML = formatNumber(convertToString(result));
	}
}

function updateMiniStatusOffer(quantity,stepQty)
{
	var productCountElement = document.getElementById("offerProductCount");
	if(productCountElement != null)
	{
		var count = 0;
		quantity = replaceComma(quantity);		
		if(quantity == "")
			count = 1;
		else
			count = quantity * 1;
		var qty = (productCountElement.innerHTML * 1) + count;
		productCountElement.innerHTML = qty;
	}
}


function clearQtys()
{

	for (i=0;i<document.forms[1].elements.length;i++)
	{
		if (document.forms[1].elements[i].type=="text")
		{
			if (document.forms[1].elements[i].name=="Quantity")
			{ document.forms[1].elements[i].value="";  }
		}
	}

}

function setFilledAmount(qty)
{	
	var theQty = qty;
	document.getElementById("FilledAmount").value=theQty;
	//document.getElementById("FilledAmount").value;
}

function getFilledAmount(minQty)
{
 	var filledAmount = document.getElementById("FilledAmount").value;
 	if(filledAmount==undefined || filledAmount == null || filledAmount=='' || filledAmount=='0')
	{ filledAmount = replaceComma(minQty); }
	return filledAmount;
}

//POPUP WINDOW FOR ADD TO BASKET

//basic popup behavior

function popupwindow (obj,act,desc,quantity,price,unit,stepQty)
{
	
	if (act == 'open') 
	{
		//show popup
		
		document.getElementById("popupwindow").style.display ='inline';  
		document.getElementById("popupwindowshade").style.display ='inline'; 

		//set same size for the shade level div
		
		document.getElementById("popupwindowshade").style.width =  document.getElementById("popupwindow").offsetWidth-5+"px";
		document.getElementById("popupwindowshade").style.height =  document.getElementById("popupwindow").offsetHeight-5+"px";
		
		//check stepquantity
		quantity = replaceComma(quantity);
		if(parseFloat(quantity) < parseFloat(stepQty))
		{
			quantity = stepQty
		}
		else if((quantity % stepQty) != 0)
		{
			var restVal = quantity % stepQty;
			quantity = (stepQty - restVal) + restVal + (quantity-restVal);
		}		
		
		//set productdescription		
		var tmpTotalPrice = replaceComma(replacePointWithBlank(price)) * quantity;
		totalPrice = Math.round(tmpTotalPrice*100)/100		 
		document.getElementById("popupDesc").innerHTML = quantity+" "+unit+" "+desc;
		//set productprice
		document.getElementById("popupPrice").innerHTML = "&euro; " + formatNumber(replacePoint(totalPrice.toFixed(2)));
	}
	
	if (act == 'close') 
	{
		//hide popup
		
		document.getElementById("popupwindow").style.display ='none'; 
		document.getElementById("popupwindowshade").style.display ='none'; 			
	}
	
}

function popupwindowoffer (obj,act,desc,quantity,unit,stepQty)
{
	
	if (act == 'open') 
	{
		//show popup
		
		document.getElementById("popupwindowoffer").style.display ='inline';  
		document.getElementById("popupwindowshade").style.display ='inline';
		
		//stepquantity
		quantity = replaceComma(quantity);
		if(parseFloat(quantity) < parseFloat(stepQty))
		{
			quantity = stepQty
		}
		else if((quantity % stepQty) != 0)
		{
			var restVal = quantity % stepQty;
			quantity = (stepQty - restVal) + restVal + (quantity-restVal);
		}	 

		//set same size for the shade level div		
		document.getElementById("popupwindowshade").style.width =  document.getElementById("popupwindowoffer").offsetWidth-5+"px";
		document.getElementById("popupwindowshade").style.height =  document.getElementById("popupwindowoffer").offsetHeight-5+"px";
		document.getElementById("popupDescOffer").innerHTML = quantity+" "+unit+" "+desc;
	}
	
	if (act == 'close') {
		
		//hide popup
		
		document.getElementById("popupwindowoffer").style.display ='none'; 
		document.getElementById("popupwindowshade").style.display ='none'; 			
	}
	
}

//basic popup behavior

function popupwindowNotLoggedIn(obj,act,desc,quantity,price)
{
	
	if (act == 'open') 
	{
	
	
		//show popup
		
		document.getElementById("popupwindowlogin").style.display ='inline';  
		document.getElementById("popupwindowshadelogin").style.display ='inline'; 

		//set same size for the shade level div
		
		document.getElementById("popupwindowshadelogin").style.width =  document.getElementById("popupwindowlogin").offsetWidth-5+"px";
		document.getElementById("popupwindowshadelogin").style.height =  document.getElementById("popupwindowlogin").offsetHeight-5+"px";
	}
	
	if (act == 'close') {
		
		//hide popup
		
		document.getElementById("popupwindowlogin").style.display ='none'; 
		document.getElementById("popupwindowshadelogin").style.display ='none'; 			
	}
	
}

function popupwindowNotLoggedInOffer(obj,act,desc,quantity)
{
	
	if (act == 'open') 
	{
	
	
		//show popup
		
		document.getElementById("popupwindowloginoffer").style.display ='inline';  
		document.getElementById("popupwindowshadelogin").style.display ='inline'; 

		//set same size for the shade level div
		
		document.getElementById("popupwindowshadelogin").style.width =  document.getElementById("popupwindowloginoffer").offsetWidth-5+"px";
		document.getElementById("popupwindowshadelogin").style.height =  document.getElementById("popupwindowloginoffer").offsetHeight-5+"px";
	}
	
	if (act == 'close') {
		
		//hide popup
		
		document.getElementById("popupwindowloginoffer").style.display ='none'; 
		document.getElementById("popupwindowshadelogin").style.display ='none'; 			
	}
	
}

function replacePoint(entry) 
{
	out = "."; // replace this
	add = ","; // with this
	temp = "" + entry; // temporary holder
	
	while (temp.indexOf(out)>-1) 
	{
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function replaceComma(entry) 
{
	out = ","; // replace this
	add = "."; // with this
	temp = "" + entry; // temporary holder
	
	while (temp.indexOf(out)>-1) 
	{
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function replacePointWithBlank(entry) 
{
	out = "."; // replace this
	add = ""; // with this
	temp = "" + entry; // temporary holder
	
	while (temp.indexOf(out)>-1) 
	{
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

/*Get value from cookie*/
function getCookieValue (cookieName) 
{
  var exp = new RegExp (escape(cookieName) + "=([^;]+)");
  if (exp.test (document.cookie + ";")) {
    exp.exec (document.cookie + ";");
    return unescape(RegExp.$1);
  }
  else return false;
}

//Numberformatter
function formatNumber(number) {
	number += '';
	numberParts = number.split(',');
	x1 = numberParts[0];
	x2 = numberParts.length > 1 ? ',' + numberParts[1] : '';
	var regEx = /(\d+)(\d{3})/;
	while (regEx.test(x1)) {
		x1 = x1.replace(regEx, '$1' + '.' + '$2');
	}
	return x1 + x2;

}

