function AsyncRequest(url, callbackFunction, src)
{  
	var obj = this;
	var urlCall = url;
	this.updating = false;
	this.update = 
				function(passData, postMethod) 
				{
					if (obj.updating==true) 
					{
						return false;
					}
					obj.updating=true;
					var xhr = null;
					if (window.XMLHttpRequest) 
					{
						xhr = new XMLHttpRequest();
					}
					else
					{
						xhr = new ActiveXObject("Microsoft.XMLHTTP");
					}
					if (xhr == null) 
					{
						return false;
					}
					else
					{
						xhr.onreadystatechange = 
												function() 
												{
													if (xhr.readyState == 4) 
													{
														obj.updating = false;
														obj.callback(xhr.responseText, xhr.status, xhr.responseXML, src);
														delete xhr;
													}
												}
						var timestamp = new Date();
						if (postMethod == 'POST')
						{
							var uri = urlCall + '?' + timestamp.getTime();
							xhr.open("POST", uri, true);
							xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
							xhr.send(passData);
						}
						else
						{
							var uri = urlCall + '?' + passData + '&timestamp=' + timestamp.getTime();
							xhr.open("GET", uri, true);
							xhr.send(null);
						}
						return true;
					}
				}
	this.callback = callbackFunction || function () { };
}


function addToCartAsync(lnk, productId, qty)
{
	var a = new AsyncRequest('/sitedata/custom/asyncRequest.cfm', addToCartCallback, lnk);
	var updated = a.update('t=addToCart&qty=' + qty + '&productId=' + productId, 'GET');
	
	//Return false if successful to stop form submitting. If unsuccessful, then form will be submitted instead.
	return !updated;
}

function addToCartCallback(response, status, xml, src)
{
	if (status == 200)
	{
		var result;
		
		//Response is JSON and when evaluated will populate the result variable
		eval(response);
		
		if (result)
		{
		    if (result.success)
		    {
		        var alertBox = document.createElement('div');
		        //Create div to hold response from async request				
		        var alertBox = document.createElement('div');
		        alertBox.id = 'alertBox' + result.timestamp;
		        alertBox.className = 'alertBox';
		        alertBox.style.display = 'none';
		        alertBox.innerHTML = unescape(result.xhtml);

		        //Set events
		        alertBox.onmouseover = function(e) { alertOver(e); };
		        alertBox.onmouseout = function(e) { alertOut(e); };

		        //Create dummy iframe to allow div to appear over selects in IE
		        var iframe = document.createElement('iframe');
		        iframe.id = 'alertBox' + result.timestamp + 'Frame';
		        iframe.src = 'javascript:"<html></html>"';
		        iframe.className = 'alertBoxFrame';
		        iframe.scrolling = 'no';
		        iframe.frameborder = '0';
		        //Must append iframe first
		        document.body.appendChild(iframe);

		        //Add to document and show
		        document.body.appendChild(alertBox);
		        alertBox.style.display = 'block';

		        //Set top so that it appears mid screen
		        var top = getPageScroll() + (getClientHeight() - alertBox.offsetHeight) / 2
		        alertBox.style.top = top.toString() + 'px';
		        iframe.style.top = alertBox.style.top;

		        //Close alert after 5 seconds
		        alertBox.timer = setTimeout(function() { closeAlert('alertBox' + result.timestamp); }, 5000);

		        //Update cart summary with new cart totals
		        updateCartSummary(result.totalItems, result.totalPrice)
		    }
		    else
		    {
		        alert(result.error);
		    }
		}
	}
	else
	{
		//document.body.innerHTML = response;
		alert('There was a problem adding that product. Please try again.');
	}
}

function updateCartSummary(items, price)
{
	var totalItems = document.getElementById('cartTotalItems');
	if (totalItems)
	{
		totalItems.innerHTML = items;
	}
	var totalPrice = document.getElementById('cartTotalPrice');
	if (totalPrice)
	{
		totalPrice.innerHTML = price;
	}
}

function alertOver(e)
{
	if (!e)
	{
		e = event;
	}
	if (e.srcElement)
	{
		clearTimeout(e.srcElement.timer);
		e.srcElement.timer = null;
	}
}

function alertOut(e)
{
	if (!e)
	{
		e = event;
	}
	if (e.srcElement)
	{
		//Close alert after 2 seconds
		var id = e.srcElement.id;
		e.srcElement.timer = setTimeout(function() { closeAlert(id); }, 2000);
	}
}

function closeAlert(alertId)
{
	if (alertId != '')
	{
		var alertBox = document.getElementById(alertId);
		if (alertBox)
		{
			if (alertBox.timer)
			{
				clearTimeout(alertBox.timer);
				alertBox.timer = null;
			}
			document.body.removeChild(alertBox);
		}
		var alertBoxFrame = document.getElementById(alertId + 'Frame');
		if (alertBoxFrame)
		{
			document.body.removeChild(alertBoxFrame);
		}
	}
	return false;
}

function getPageScroll()
{
	var scrollTop = window.pageYOffset;
	if (!scrollTop)
	{
		scrollTop = document.documentElement.scrollTop;
	}
	if (!scrollTop)
	{
		scrollTop = document.body.scrollTop;
	}
	return scrollTop;
}

function getClientHeight()
{
	return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}

function setQuantity(productId, productQty)
{
    var qtyEl = document.getElementById('qty' + productId);
	if (!qtyEl)
	{
        qtyEl = document.getElementById('qty_' + productId);
    }
	if (qtyEl)
	{
		qtyEl.value = productQty;
	}
	return false;
}