var xmlRequest = null;
var btnClicked = null;
var btnText = "";

function addToCartResponse() {

	var result = "fail";
	var title = "";
	var msg = "";
	
	try {
		if (xmlRequest.readyState == 4) {
		    if (xmlRequest.status == 200) {
				var xmlDoc = xmlRequest.responseXML.documentElement;
				var xmlStat = xmlDoc.getAttribute("stat");
				if (xmlStat == "ok") {
					try {
						result = getFirstChildValue(xmlDoc.getElementsByTagName("cart_status"));
						title = getFirstChildValue(xmlDoc.getElementsByTagName("confirmation_title"));
						msg = getFirstChildValue(xmlDoc.getElementsByTagName("confirmation_message"));

						if (result.length == 0)
							logError(new Error("result is empty\n\n" + xmlRequest.responseText + "\n"), "addToCartResponse()");
					} catch (e) {
						logError(new Error(e.message + "\n\n" + xmlRequest.responseText + "\n"), "addToCartResponse()");
					}
				} else {
					logError(new Error("status = fail\n\n" + xmlRequest.responseText), "addToCartResponse()");
				}
			} else {
				logError(new Error("XMLHttpRequest.Status " + xmlRequest.status), "addToCartResponse()");
			}        } else {
			if (btnClicked.value.indexOf("....") > -1)
				btnClicked.value = "adding...";
			else
				btnClicked.value += ".";
			result = "pending";
		}
	}
	catch (e) {
		logError(e, "addToCartResponse()");
	}
	
	if (result != "pending")
		showAddToCartMsg(result, title, msg);
}

function addToCart(pid, vid, btn, loc) {

	var result = "fail";
	var title = "";
	var msg = "";

	try {
		var y = document.getElementById("addToCartMsgBox");
		if (y != null)
			y.style.display = "none";
		
		btnText = btn.value;
		btn.value = "adding...";
		btnClicked = btn;

		xmlRequest = getRequestObject(addToCartResponse);
		if (xmlRequest != null) {
			var strURL = "/services/";
			var strPost = "method=addtocart"
			    + "&pid=" + escape_utf8(pid)
			    + "&vid=" + escape_utf8(vid)
			    + "&loc=" + escape_utf8(loc);
			    
			try {
				sendRequest(xmlRequest, "POST", true, strURL, strPost);
				result = "sent";
			}
			catch (e) {
				logError(e, "addToCart('" + pid + "', '" + vid + "', '" + loc + "').sendRequest()");
			}
		} else {
			logError(new Error("getRequestObject failed."), "addToCart('" + pid + "', '" + vid + "', '" + loc + "')");
		}
	}
	catch (e) {
		logError(e, "addToCart('" + pid + "', '" + vid + "', '" + loc + "')");
	}
	
	if (result == "fail")
		showAddToCartMsg(result, title, msg);

	return false;
}

function showAddToCartMsg(status, title, message) {
	try {
		var appendMsg = false;
		var y = document.getElementById("addToCartMsgBox");
		if (y == null) {
			appendMsg = true;
			y = document.createElement("span");
			y.id = "addToCartMsgBox";
		}
		
		if (status == "fail" || status.length == 0) {
			if (title.length == 0)
				title = "Error";
			if (message.length == 0)
				message = "There was a problem adding this product to your shopping cart.";
		}
		
		y.innerHTML = "<strong>" + title + "</strong><br />" + message;
		
		var iconSuccess = "url(/assets/images/success.gif)";
		var iconError = "url(/assets/images/exclamation.gif)"
		
		if (status != null)
			status = status.toLowerCase();

		if (status.indexOf("success") == 0) {
			y.className = "bodygreen";
			y.style.backgroundImage = iconSuccess;
		} else {
			y.style.backgroundImage = iconError;
			y.className = "bodyred";
		}

		if (appendMsg) {
			var x = document.getElementById("bmdBreak");
			x.parentNode.appendChild(y);
			x.parentNode.appendChild(document.createElement("br"));
		}
		
		y.style.display = "block";
	}
	catch (e) {
		logError(e, "showAddToCartMsg()");
	}
	
	btnClicked.value = btnText;
	btnClicked.disabled = false;
	window.scrollTo(0,0);
}

function getFirstChildValue(nodeList) {
	if (nodeList != null && nodeList.length > 0)
		if (nodeList[0].hasChildNodes())
			return nodeList[0].firstChild.nodeValue;

	return "";
}
