// 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;
	}

	//IRISH TITAN EDIT -- only show shopping cart when there is items in the cart
	if(aItems.length != 0){

	strOut += '<center><table class="summarycart" style="color:white;background-color:#D3111A; width:302px; margin-top: -2px;" width="302">';
	strOut += '<tr style="color:white; text-align:left;"><th style="color:white; text-align:left">&nbsp;&nbsp;Shopping Cart</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 class="summarycart_links" style="color:white;background-color: #D3111A;" href="/estylez_item.aspx?item=' + aData[0][i] + '">' + aData[0][i] + '</a></div>';
		strOut += '<div align="left" style="font-size: 10px; padding-top: 2px; color:white; background-color: #D3111A;">' + aData[4][i] + '</div>';
		strOut += '<div align="right" style="padding-top: 5px; font-size: 11px; color:white; background-color: #D3111A;">';
		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; color: #C84044;"><strong>Your Cart is Empty</strong></div>';
		strOut += '</td></tr>';
	}
	else
	{
		strOut += '<tr><td class="summarycart_total_cell" style="color:white; background-color: #D3111A;">';
		strOut += '<div style="float: left;">Total</div>';
		strOut += '<div style="float: right;">$' + formatPrice(iOrderTotal, 2) + '</div>';
		strOut += '</td></tr>';
		
		//IRISH TITAN EDIT
		
		//strOut += '<tr><td style="padding: 0px; color: #FFF; background-color:#000;">';
		//strOut += '<div align="right" style="padding: 4px 10px 4px 10px; color:white; background-color:#000;">';
		
		// Account for B2C vs B2B
		//if (window.eBusinessUserType && eBusinessUserType == "1")		// B2B
		//{
		//	strOut += '<a class="summarycart_links" href="https://64.122.154.51/IW_ShoppingCartOrder.m4p.pvx?;SUBMIT_SO">';
		//}
		//else 	// default: B2C
		//{
			//strOut += '<a class="summarycart_links" href="https://64.122.154.51/IW_ShoppingCartStore.m4p.pvx?;SC_STEP1"; style="background-color:#000; color: #FFF;">';
		//}
		
		//strOut += 'view cart</a>';	
		//strOut += '</div>';
		//strOut += '</td></tr>';		
	}
	
	strOut += '</table></center>';
	document.getElementById("summarycart").innerHTML = strOut;
	
	} ////IRISH TITAN EDIT  (END)-- only show shopping cart when there is items in the cart

	
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 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
	}
}




