//FG: javascript and ajax functions collection

// Get the HTTP Object
function getHTTPObject()
{
	if (window.XMLHttpRequest) // code for IE7+, Firefox, Chrome, Opera, Safari
	{
		return new XMLHttpRequest();
	}
	else if (window.ActiveXObject) // code for IE6, IE5
	{
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	else 
	{
		alert("Your browser does not support AJAX.");
		return null;
	}
} 

// Do the ajax call to show the popup for quantity selection
function selectQuantityToAddToEstimate(productID)
{
	httpObject = getHTTPObject();
	
	if (httpObject != null) 
	{
		httpObject.open("GET", "ajaxCallsHandler.php?function=selectQuantityToAddToEstimate&productID="+productID, true);
		httpObject.send(null);
				
		httpObject.onreadystatechange = function () {
				if (httpObject.readyState == 4) {
				displayUserBox(httpObject);
			}
		}
	}
}	

// Do the ajax call to add a product to the estimate
function addProductToEstimate(productID)
{
	httpObject = getHTTPObject();
	
	if (httpObject != null) 
	{
		var quantity_element = document.getElementById('txt_quantity'); 
		var quantity = quantity_element.value; 
				
		httpObject.open("GET", "ajaxCallsHandler.php?function=addProductToEstimate&productID="+productID+"&productQuantity="+quantity, true);
		httpObject.send(null);
							
		httpObject.onreadystatechange = function () {
				if (httpObject.readyState == 4) {
				displayUserBox(httpObject);
			}
		}
	}
}	

// Do the ajax call to show the popup for quantity selection
function selectQuantityToAddToCart(productID)
{
	httpObject = getHTTPObject();
	
	if (httpObject != null) 
	{
		httpObject.open("GET", "ajaxCallsHandler.php?function=selectQuantityToAddToCart&productID="+productID, true);
		httpObject.send(null);
				
		httpObject.onreadystatechange = function () {
				if (httpObject.readyState == 4) {
				displayUserBox(httpObject);
			}
		}
	}
}	

// Do the ajax call to add a product to the shopping cart
function addProductToCart(productID)
{
	httpObject = getHTTPObject();
	
	if (httpObject != null) 
	{
		var quantity_element = document.getElementById('txt_quantity'); 
		var quantity = quantity_element.value; 
					
		httpObject.open("GET", "ajaxCallsHandler.php?function=addProductToCart&productID="+productID+"&productQuantity="+quantity, true);
		httpObject.send(null);
				
		httpObject.onreadystatechange = function () {
				if (httpObject.readyState == 4) {
				displayUserBox(httpObject);
			}
		}
	}
}	
	
// Display the box to notify to the user the result of the ajax call
function displayUserBox(httpObject)
{	
	var displayboxdiv = document.getElementById('displaybox');
	var boxcenterdiv = document.getElementById('boxcenter');
		
	if(displayboxdiv.style.display == "none" && boxcenterdiv.style.display == "none")
	{
		displayboxdiv.style.display = "";
		boxcenterdiv.style.display = "";
		
		boxcenterdiv.innerHTML = httpObject.responseText;
	}
	else
	{
		boxcenterdiv.innerHTML = httpObject.responseText;
		
		setTimeout('window.location.reload(true);',3000);
	}
}

// Hide the user box
function hideUserBox()
{	
	var displayboxdiv = document.getElementById('displaybox');
	var boxcenterdiv = document.getElementById('boxcenter');
			
	if(displayboxdiv.style.display == "" && boxcenterdiv.style.display == "")
	{
		displayboxdiv.style.display = "none";
		boxcenterdiv.style.display = "none";
		
		displayboxdiv.innerHTML = "";
		boxcenterdiv.innerHTML = "";
		
		window.location.reload(true);
	}
}

// Display the box to view the passed image
function displayImageBox(imageSRC)
{	
	var displayboxdiv = document.getElementById('displaybox');
	var boxcenterdiv = document.getElementById('imageboxcenter');
		
	if(displayboxdiv.style.display == "none" && boxcenterdiv.style.display == "none")
	{
		displayboxdiv.style.display = "";
		boxcenterdiv.style.display = "";
	}
	
	var boxContent = "<p style='margin: 0 auto 20px auto; text-align: right;'>";
	boxContent = boxContent + "<a href='#' onclick='return hideImageBox();'>X</a>";
	boxContent = boxContent + "</p>";
	boxContent = boxContent + "<img src=\"" + imageSRC + "\">";
	
	boxcenterdiv.innerHTML = boxContent;
}

// Hide the image box
function hideImageBox()
{	
	var displayboxdiv = document.getElementById('displaybox');
	var boxcenterdiv = document.getElementById('imageboxcenter');
			
	if(displayboxdiv.style.display == "" && boxcenterdiv.style.display == "")
	{
		displayboxdiv.style.display = "none";
		boxcenterdiv.style.display = "none";
		
		displayboxdiv.innerHTML = "";
		boxcenterdiv.innerHTML = "";
	}
}

document.onkeyup = KeyCheck;       

function KeyCheck(e){
   var KeyID = (window.event) ? event.keyCode : e.keyCode;

   switch(KeyID){
      case 27:
      	hideImageBox();
				hideUserBox();
      break; 
   }
}
