// By Lauren Smith, Information Systems Management



//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- CONFIGURATION

// Set the list delimiter char.
var config_Delimiter = ";";

// Set the URL of the processing pages.
var config_summaryCartInfoURL = "/GetSC.aspx";



//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- PO FUNCTIONS

function comet_drawSummaryCart(userid)
// Wrapper function that initiates the background XML Request.
{
	comet_getSummaryCartInfo(userid);
}

function comet_getSummaryCartInfo(userid)
// Initiates the request for the shopping cart data.
{
	var now = new Date();		// We will append the request with the time in milliseconds to defeat caching.
	comet_GetXML(config_summaryCartInfoURL + "?userid=" + userid + "&unique=" + now.getTime(), "comet_handleShoppingCartInfo");
}

function comet_handleShoppingCartInfo(xmlSC)
// Handler for the XML Request.  Once the XML Request gets some data back, we'll call the function that
// actually does the work.
{
	comet_doDrawSummaryCart(xmlSC);	
}

function comet_doDrawSummaryCart(xmlSC)
// Processes the shopping cart XML data and builds an output table.
{
	var strOut = "";
	var iOrderTotal = 0;
	var aItems = xmlSC.getElementsByTagName("ItemCode");
	var aData = new Array;
	
	aData[0] = new Array;				// ItemCode
	aData[1] = new Array;				// Quantity
	aData[2] = new Array;				// Price
	aData[3] = new Array;				// ExtAmt
	aData[4] = new Array;				// ItemDescription

	for (var i=0; i < aItems.length; i++)
	{
		aData[0][i] = aItems[i].childNodes[0].nodeValue;
		aData[1][i] = aItems[i].nextSibling.childNodes[0].nodeValue;
		aData[2][i] = aItems[i].nextSibling.nextSibling.childNodes[0].nodeValue;
		aData[3][i] = aItems[i].nextSibling.nextSibling.nextSibling.childNodes[0].nodeValue;
		aData[4][i] = aItems[i].nextSibling.nextSibling.nextSibling.nextSibling.childNodes[0].nodeValue;
	}

	strOut += '<table class="summarycart">';
	strOut += '<tr><th><img src="layout_images/shopping_cart_title.gif" width="188" height="18" /></th></tr>';

	for (var i=0; i < aItems.length; i++)
	{
		//document.body.innerHTML = document.body.innerHTML + aData[0][i] + "<br>" + aData[1][i] + "<br>" + aData[2][i] + "<br>" + aData[3][i] + "<br>" + aData[4][i] + "<br>";
		strOut += '<tr><td>';
		strOut += '<div align="left"><a href="/estylez_item.aspx?item=' + aData[0][i] + '">' + aData[0][i] + '</a></div>';
		strOut += '<div align="left" style="font-size: 9px; padding-top: 2px;">' + aData[4][i] + '</div>';
		strOut += '<div align="right" style="padding-top: 5px;">';
		strOut += '<strong>Qty: </strong>' + parseInt(aData[1][i]);
		strOut += '&nbsp;&nbsp;&nbsp;';
		strOut += '<strong>Amt: </strong>$' + formatPrice(aData[3][i], 2);
		strOut += '</div>';
		strOut += '</td></tr>';
		
		iOrderTotal += parseFloat(aData[3][i]);
	}
	
	// If we have no rows then just display "nothing in your cart" message.
	if (aItems.length == 0)
	{
		strOut += '<tr><td>';
		strOut += '<div style="padding: 80px 0px 1px 2px;"><strong>Shopping Cart is Empty</strong></div>';
		strOut += '</td></tr>';
	}
	else
	{
		strOut += '<tr><td>';
		strOut += '<div style="float: left;"><strong>Total</strong></div>';
		strOut += '<div style="float: right;"><strong>$' + formatPrice(iOrderTotal, 2) + '</strong></div>';
		strOut += '</td></tr>';
		strOut += '<tr><td>';
		strOut += '<div align="right" style="padding: 2px 0px 2px 0px;">';
		
		// Account for B2C vs B2B
		if (window.eBusinessUserType && eBusinessUserType == "1")		// B2B
		
		{
			strOut += '<a href="https://www.capitolscientific.com/IW_ShoppingCartOrder.m4p.pvx?;SUBMIT_SO">';
		}
		else 	// default: B2C
		{
			strOut += '<a href="/IW_ShoppingCartStore.m4p.pvx?;SC_STEP1">';
		}
		
		strOut += '<img src="/layout_images/btn_view_cart.gif" height="16" width="78" />';
		strOut += '</a>';	
		strOut += '</div>';
		strOut += '</td></tr>';		
	}
	
	strOut += '</table>';
	document.getElementById("summarycart").innerHTML = strOut;

	
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- UTILITY FUNCTIONS


function formatPrice(strInput, iPrecision)
{
	if (parseFloat(strInput))
	{
		if (window.Number)
		{
			var oNum = new Number(parseFloat(strInput));
			strInput = oNum.toFixed(iPrecision);
		}
		else { strInput = parseFloat(strInput); }
	}
	else { strInput = ""; }
	
	return(strInput);
}

function string_trim_spaces(strInput)
// Trims leading and trailing spaces off of strInput
{
	// Any data to work with?
	if (strInput.length == 0)	{ return null }
	
	// OK, we have data
	else
	{
		// First cut off leading spaces
		while(strInput.indexOf(" ") == 0)	{ strInput = strInput.slice(1,strInput.length) }
		
		// Next cut off trailing spaces
		while(strInput.indexOf(" ", strInput.length - 1) == (strInput.length - 1))
		{ strInput = strInput.slice(0,strInput.length - 1) }
		
		return strInput
	}
}




