var pl = new Image();
pl.src = '/ShopCart/images/button_headerCheckout.gif';

function cbSwap(img,which) {
	if(which == 'on') {
		img.src = '/ShopCart/images/button_headerCheckout_on.gif';
	} else {
		img.src = '/ShopCart/images/button_headerCheckout.gif';	
	}
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}


function getHTTPRequestObject() {
	var xmlHttpRequest;

	if (!xmlHttpRequest && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlHttpRequest = new XMLHttpRequest();
		} catch (exception) {
			xmlHttpRequest = false;
		}
	}
	return xmlHttpRequest;
}

function addItemToCart2(form,snapToObjId,offX,offY,cfid,cftoken,itemId) {
	if (itemId == '1140c806-e853-4fd1-a9cd-b81e80ef6764' || itemId == 'c59b9215-f76f-4a2e-8079-cbb8937ab3fc') {
		addItemToCart1(form,snapToObjId,offX,offY,itemId,cfid,cftoken);
		return false;
	} else {
		P7_Snap(snapToObjId,'light',offX,offY);
		document.getElementById('light').style.display = 'block';
		document.getElementById('dark').style.display = 'block';
		document.getElementById('dark').style.opacity = .5;
		document.getElementById('dark').style.filter= 'alpha(opacity=50)';
		return false;
	}
}

var httpRequester = getHTTPRequestObject(); // Create the xml http object on the page load
var httpRequester2 = getHTTPRequestObject();
var couldProcess = false;

function addItemToCart1(form,snapToObjId,offX,offY,IDofItem,cfid,cftoken) {
	if (!couldProcess && httpRequester) {
		document.getElementById('light').style.display = 'none';
		document.getElementById('dark').style.display = 'none';
		var buttonText = document.productOrderForm.submitButton.value;
	
		document.productOrderForm.submitButton.value='Adding...';
		document.productOrderForm.submitButton.disabled=true;
		
		var itemId;
		if (!IDofItem) {
			if(document.productOrderForm.ItemID.type == 'select-one') {
				itemId = document.productOrderForm.ItemID.options[form.ItemID.selectedIndex].value;
			} else {
				itemId = document.productOrderForm.ItemID.value;
			}
		} else {
			itemId = IDofItem;
		}
		var quantity;
		if(document.productOrderForm.quantity.type == 'select-one') {
			quantity = document.productOrderForm.quantity.options[document.productOrderForm.quantity.selectedIndex].value;
		} else {
			quantity = document.productOrderForm.quantity.value;
		}
		var currentDate = new Date();
		var ms = currentDate.valueOf();
		
		var url = document.productOrderForm.action + '&ajaxcall=1&ItemId=' + itemId + '&quantity=' + quantity + '&cfid=' + cfid + '&cftoken=' + cftoken + '&ms=' + ms;

		P7_Snap(snapToObjId,'addItemDisplay',offX,offY);
		document.getElementById('addItemDisplay').style.visibility='visible';
	
		httpRequester.open("GET", url, true);
		httpRequester.onreadystatechange = function() {
			if (httpRequester.readyState==4) {
				var content = trim(httpRequester.responseText);
				document.getElementById('addItemDisplay').innerHTML = content;
				
				//update cart summary
				var url2 = '/index.cfm?fuseaction=ShopCart.CartSummary&ajaxcall=1' + '&cfid=' + cfid + '&cftoken=' + cftoken + '&ms=' + ms;
				httpRequester2.open("GET", url2, true);
				httpRequester2.onreadystatechange = function() {
					if (httpRequester2.readyState==4) {
						var content = trim(httpRequester2.responseText);
						document.getElementById('cartSummary').innerHTML = content;
						document.productOrderForm.submitButton.value=buttonText;
						document.productOrderForm.submitButton.disabled=false;
						couldProcess = false;
					}
				}
				httpRequester2.send(null);
				
				document.productOrderForm.submitButton.value=buttonText;
				document.productOrderForm.submitButton.disabled=false;
				couldProcess = false;
			}
		}
		couldProcess = true;
		httpRequester.send(null);
	} else {
		//ajax not supported
		return true;
	}
	
	return false;
}

function addItemToCart(form,snapToObjId,offX,offY,cfid,cftoken) {
	if (!couldProcess && httpRequester) {
		var buttonText = form.submitButton.value;
	
		form.submitButton.value='Adding...';
		form.submitButton.disabled=true;
		
		var itemId;
		if(form.ItemID.type == 'select-one') {
			itemId = form.ItemID.options[form.ItemID.selectedIndex].value;
		} else {
			itemId = form.ItemID.value;
		}
		var quantity;
		if(form.quantity.type == 'select-one') {
			quantity = form.quantity.options[form.quantity.selectedIndex].value;
		} else {
			quantity = form.quantity.value;
		}
		var currentDate = new Date();
		var ms = currentDate.valueOf();
		
		var url = form.action + '&ajaxcall=1&ItemId=' + itemId + '&quantity=' + quantity + '&cfid=' + cfid + '&cftoken=' + cftoken + '&ms=' + ms;

		P7_Snap(snapToObjId,'addItemDisplay',offX,offY);
		document.getElementById('addItemDisplay').style.visibility='visible';
	
		httpRequester.open("GET", url, true);
		httpRequester.onreadystatechange = function() {
			if (httpRequester.readyState==4) {
				var content = trim(httpRequester.responseText);
				document.getElementById('addItemDisplay').innerHTML = content;
				
				//update cart summary
				var url2 = '/index.cfm?fuseaction=ShopCart.CartSummary&ajaxcall=1' + '&cfid=' + cfid + '&cftoken=' + cftoken + '&ms=' + ms;
				httpRequester2.open("GET", url2, true);
				httpRequester2.onreadystatechange = function() {
					if (httpRequester2.readyState==4) {
						var content = trim(httpRequester2.responseText);
						document.getElementById('cartSummary').innerHTML = content;
						form.submitButton.value=buttonText;
						form.submitButton.disabled=false;
						couldProcess = false;
					}
				}
				httpRequester2.send(null);
				
				form.submitButton.value=buttonText;
				form.submitButton.disabled=false;
				couldProcess = false;
			}
		}
		couldProcess = true;
		httpRequester.send(null);
	} else {
		//ajax not supported
		return true;
	}
	
	return false;
}

function clearAddItemDisplay() {
	document.getElementById('addItemDisplay').innerHTML='';
	document.getElementById('addItemDisplay').style.visibility='hidden';
}