// 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 cellspacing="0" cellpadding="0" border="0" style="width: 100%;">';

	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 class="summary_cart_data_cell_padding">';
		strOut += '<div class="div_summary_cart_item_number"><a class="mainlink" href="/estylez_item.aspx?item=' + aData[0][i] + '">' + aData[0][i] + '</a></div>';
		strOut += '<div class="div_summary_cart_item_description">' + aData[4][i] + '</div>';
		strOut += '<div class="div_summary_cart_qty_amount">';
		strOut += '<b>Qty: </b> ' + parseInt(aData[1][i]);
		strOut += '&nbsp;';
		strOut += ' <b>Amt: </b>$' + 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 class="summary_cart_empty_message">Your Cart is Empty</div>';
		strOut += '</td></tr>';
	}
	else
	{
		strOut += '<tr><td class="summary_cart_total_cell">';
		strOut += '<div class="hr"><hr></div>';
		strOut += '<div style="padding: 7px 14px 7px 0px; text-align: right;">Total: &nbsp;$' + formatPrice(iOrderTotal, 2) + '</div>';
		strOut += '<div class="hr"><hr></div>';
		strOut += '</td></tr>';
		strOut += '<tr><td style="padding: 0px;">';
		strOut += '<div class="summary_cart_view_cart">';
		strOut += '<img src="/layout_images/black_arrow_white_background.gif" alt="arrow" /> ';
		
		// Account for B2C vs B2B
		if (window.eBusinessUserType && eBusinessUserType == "1")		// B2B
		{
			strOut += '<a class="mainlink" href="https://www.midwestswinevets.com/IW_ShoppingCartOrder.m4p.pvx?;SUBMIT_SO">';
		}
		else 	// default: B2C
		{
			strOut += '<a class="mainlink" href="/IW_ShoppingCartStore.m4p.pvx?;SC_STEP1?company=VPI">';
		}
		
		strOut += 'check out</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
	}
}