var btnClicked = null;

function ButtonState(btn, addingValue) {
	var savedButton = btn;
	var savedType = btn.type;
	var savedState;
	
	if (savedType == "image") {
		savedState = btn.src;
	} else if (savedType == "button" || savedType == "submit") {
		savedState  = btn.value;
	}
	
	this.adding = function () {
		if (savedType == "image") {
			//savedButton.src = "http://images.fonts.com/ProductImages/adding.gif";
			savedButton.src = "/assets/images/buttons/button-addtocart-adding.gif";
		} else if (savedType == "button" || savedType == "submit") {
			if (addingValue.length > 0) {
				savedButton.value = addingValue;
			}
		}
		//savedButton.disabled = true;
	};
	
	this.restore = function () {
		if (savedType == "image") {
			savedButton.src = savedState;
		} else if (savedType == "button" || savedType == "submit") {
			savedButton.value = savedState;
		}
		savedButton.disabled = false;
	};
}

function addToCartResponse(xhr, statusMessage) {

	var result = "fail";
	var title = "";
	var msg = "";
	var count;
	var total;
	
	try {
		if (xhr.status === 200) {
			var xml = xhr.responseXML;
			if ($("rsp", xml).attr("stat") == "ok") {
				result = $("cart_status", xml).text();
				title = $("confirmation_title", xml).text();
				msg = $("confirmation_message", xml).text();
				count = $("cart_item_count", xml).text();
				total = $("cart_subtotal", xml).text();
				
			} else {
				logError(new Error("status = fail\n\n" + statusMessage + "\n\n" + xhr.responseText), "addToCartResponse()");
			}
		} else {
			logError(new Error("XmlHttpRequest.Status " + xhr.status + "\n" + statusMessage), "addToCartResponse()");
		}
	}
	catch (e) {
		logError(e, "addToCartResponse(catch)");
	}
	
	showAddToCartMsg(result, title, msg, count, total);
}

function addToCart(pid, vid, btn, from, lang, adding, onComplete) {

	try {
		$("#addToCartMsgBox").fadeTo("fast", 0);
		
		if (btn != null) {
			if (adding === undefined) {
				adding = "";
			}
			btnClicked = new ButtonState(btn, adding);
			btnClicked.adding();
		}
		if (from === undefined || lang === null) {
			from = "";
		}
		if (lang === undefined || lang === null) {
			lang = "";
		}
		if (!jQuery.isFunction(onComplete)) {
			onComplete = addToCartResponse;
		}
		
		$.ajax({ type: "POST", url: "/services/service.ashx", complete: onComplete,
			data: { method: "addtocart", pid: pid, vid: vid, from: from, lang: lang }
		});
	}
	catch (e) {
		logError(e, "addToCart('" + pid + "', '" + vid + "')");
		showAddToCartMsg(result, "", "");
	}
	
	return false;
}

function referrerAddToCart(productId, versionId) {
    try {
        $.ajax({ type: "POST", url: "/services/service.ashx", complete: function(xhr, statusMessage) {
        
            if (xhr.status === 200) {
                var xml = xhr.responseXML;
                if ($("rsp", xml).attr("stat") === "ok") {
                    result = $("cart_status", xml).text();
                    msg = $("confirmation_message", xml).text();
                    title = $("confirmation_title", xml).text();
                    if (result === "Success") {
                        window.location = '/_order/ShoppingBasket.htm';
                    }
                    else {
                        showAddToCartMsg(result, title, msg, 0, 0);
                    }
                }
            }
        },
            data: { method: "addtocart", pid: productId, vid: versionId, from: 'Affiliate Add to Cart', lang: '' }
        });
    }
    catch (e) {
        logError(e, "addToCart('" + pid + "', '" + vid + "')");
    }
}

function showAddToCartMsg(status, title, message, count, total) {
    try {
        if (!isNaN(count)) {
			$(".cartcount").text("(" + count + ")").show();
		} 
		
		var appendMsg = false;
		var y = $("#addToCartMsgBox");
		if (y.length === 0) {
			appendMsg = true;
			y = $("<div id=\"addToCartMsgBox\"></div>").hide();
		}
		
		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.html("<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.attr("class", "bodygreen").css("background-image", iconSuccess);
		} else {
			y.attr("class", "bodyred").css("background-image", iconError);
		}

		if (appendMsg) {
			$("#bmdBreak").after( y );
		}
		
		if (y.css("display") === "none") {
			y.fadeIn("slow");
		} else {
			y.fadeTo("slow", 1);
		}
		
		if (status != "fail") {
			if (count !== null && count.length > 0) {
				$("#itmcount").text(count);
			}
			
			if (total !== null && total.length > 0) {
				$("#subtotal").text(total + " / ");
			}
		}
	}
	catch (e) {
		logError(e, "showAddToCartMsg()");
	}
	
	if (btnClicked != null) {
		btnClicked.restore();
	}
	window.scrollTo(0,0);
}
function addToCartResponseClose(xhr, statusMessage) {
	addToCartResponse(xhr, statusMessage);
	$("#additionalProductsWrapper").hide("slow");
}
