//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// DOJO functions
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dojo.require("dojo.NodeList-fx");
dojo.require("dojo.fx");
dojo.require("dojo.parser");
dojo.require("dijit.form.Button");
dojo.require("dijit.Dialog");
dojo.require("dijit.form.TextBox");

var addsku = "";
var addname = "";
var adddesc = "";
var addprice = "";
var addqty = "";
var addcartqty = "";
var addcarttotal = "";

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Product Display
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function showProductDetails()
{
	//run the parser. defaults to dojo.body(), but you can pass any node
	//it runs async, so fadeout when it's done. 

	//commented this out because if more than one dialog or dialog and lightbox used, this causes error
	//dojo.parser.parse();
	var animNode = dijit.byId("dialogProductDisplay");
	var animArgs = {
		errorMessage:"Image not found.",
		errorImg:dojo.moduleUrl("dojox.image","resources/images/warning.png")
	}

	animNode.show();
	//dojo.fadeOut(animArgs).play();
}

function hideOnResize()
{
	if (dijit.byId('dialogProductDisplay') != null)
	{
		dijit.byId('dialogProductDisplay').hide();
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Show Options
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function showOverlay(varOverlayId)
{
	var animNode = dijit.byId(varOverlayId);
	animNode.show();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Cart Return
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function showDialog()
{
	// run the parser. defaults to dojo.body(), but you can pass any node
	// it runs async, so fadeout when it's done. 

	//commented this out because if more than one dialog or dialog and lightbox used, this causes error
	//dojo.parser.parse();
	var animNode = dijit.byId("dialogBuyIt");
	var animArgs = {
		node: animNode,
		//delay: 5000, //ms to stall before playing
		//duration: 4000,
		onEnd: function(){
			//hide it completely after fadeout
			animNode.hide();
		}
	}

	animNode.show();
	//dojo.fadeOut(animArgs).play();
}

function addToCartCallback(data, ioArgs)
{
	if (data.getElementsByTagName("ItemAdded")[0].hasChildNodes)
	{
		addsku = data.getElementsByTagName("ItemAdded")[0].childNodes[0].nodeValue;
	}
	if (data.getElementsByTagName("QtyAdded")[0].hasChildNodes)
	{
		addquantity = data.getElementsByTagName("QtyAdded")[0].childNodes[0].nodeValue;
	}
	if (data.getElementsByTagName("Description")[0].hasChildNodes)
	{
		addname = data.getElementsByTagName("Description")[0].childNodes[0].nodeValue;
	}
	if (data.getElementsByTagName("Attributes")[0].firstChild != null)
	{
		adddesc = data.getElementsByTagName("Attributes")[0].childNodes[0].nodeValue;
	}
	if (data.getElementsByTagName("Price")[0].hasChildNodes)
	{
		addprice = data.getElementsByTagName("Price")[0].childNodes[0].nodeValue;
	}
	if (data.getElementsByTagName("Image")[0].hasChildNodes)
	{
		addimage = data.getElementsByTagName("Image")[0].childNodes[0].nodeValue;
	}
	if (data.getElementsByTagName("CartQty")[0].hasChildNodes)
	{
		addcartqty = data.getElementsByTagName("CartQty")[0].childNodes[0].nodeValue;
	}
	if (data.getElementsByTagName("CartTotal")[0].hasChildNodes)
	{
		addcarttotal = data.getElementsByTagName("CartTotal")[0].childNodes[0].nodeValue;
	}
	document.getElementById("itemSku").innerHTML = addsku;
	document.getElementById("itemName").innerHTML = addname;
	document.getElementById("itemDesc").innerHTML = adddesc;
	document.getElementById("itemQty").innerHTML = addquantity;
	document.getElementById("itemPrice").innerHTML = addprice;
	document.getElementById("itemImage").src = addimage;
	document.getElementById("itemImage").alt = addname;
	document.getElementById("cartQty").innerHTML = addcartqty;
	document.getElementById("cartTotal").innerHTML = addcarttotal;

	if (document.getElementById("cartSummaryQty") != null)
	{
		document.getElementById("cartSummaryQty").innerHTML = addcartqty;
	}
	if (document.getElementById("CartSummary") != null)
	{
		document.getElementById("CartSummary").className = "CartSummary CartBackgroundOn";
	}

	showDialog();
}

function turnOnCartSummary()
{
	document.getElementById("CartSummary").className = "CartSummary CartBackgroundOn";
}

function addToCart(varFormName)
{
	dojo.xhrPost({
		url: '/ClientAction.aspx?return=true',
		load: addToCartCallback,
		handleAs: 'xml',
		error: function (error) {console.error('Error: ', error); alert(error);},
		form: varFormName
	});
}
