// By Lauren Smith, Information Systems Management
// Modified by Brian Hayes, ISM Barsa 10/23/2008


//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 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 iOrderTotalShort = 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;
	}

	//We need to put the hide this div if we have more then three items
	if (aItems.length >= 3)
	{
		strOut += '<div id="summaryCartFull" class="hide">';
	}
	else
	{
		strOut += '<div id="summaryCartFull">';
	}

	strOut += '<table cellspacing="0" cellpadding="0" border="0" style="width: 100%;">';
	strOut += '<tr><td>';

	for (var i=0; i < aItems.length; i++)
	{
				

		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>';

		
		iOrderTotal += parseFloat(aData[3][i]);
	}
	strOut += '</td></tr>';
			
	// 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
	{
		if (aItems.length >= 3)
		{
			strOut += '<tr><td>';
			strOut += '<div style="padding: 14px 0px 0px 0px; text-align: right;"><img src="layout_images/red_arrow.gif" alt="arrow" /> <a href="javascript:showShortCart()" class="mainlink">less items</a></div>';
			strOut += '</td></tr>';	
		}
		strOut += '<tr><td class="summary_cart_total_cell">';
		strOut += '<div style="padding: 14px 0px 8px 0px; text-align: right;">Total: &nbsp;$' + formatPrice(iOrderTotal, 2) + '</div>';
		strOut += '</td></tr>';		
	}
	
	strOut += '</table></div>';

//****************************************************************************************************************************************************

//Shorter Verison of the same shopping cart table to display if over three items in the cart

//****************************************************************************************************************************************************
	
	//We need to put the hide this div if we have more then three items
	if (aItems.length >= 3)
	{
		strOut += '<div id="summaryCartShort">';
	}
	else
	{
		strOut += '<div id="summaryCartShort" class="hide">';
	}

	strOut += '<table cellspacing="0" cellpadding="0" border="0" style="width: 100%;">';
	strOut += '<tr><td>';

	for (var i=0; i < aItems.length; i++)
	{
		
		//This code is needed because we still need to calculate the total but I do not want to show this item		
		if (i >= 3)
		{
			strOut += '<div class="hide">';
		}	
		
		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>';

		//This code is needed because we still need to calculate the total but I do not want to show this item		
		if (aItems.length >= 3)
		{
			strOut += '</div>';
		}

		
		iOrderTotalShort += parseFloat(aData[3][i]);
	}
	strOut += '</td></tr>';

	if (aItems.length == 3)
	{}
	else
	{	
	strOut += '<tr><td>';
	strOut += '<div style="padding: 14px 0px 0px 0px; text-align: right;"><img src="layout_images/red_arrow.gif" alt="arrow" /> <a href="javascript:showFullCart()" class="mainlink">more items</a></div>';
	strOut += '</td></tr>';	
	}
	strOut += '<tr><td class="summary_cart_total_cell">';
	strOut += '<div style="padding: 14px 0px 8px 0px; text-align: right;">Total: &nbsp;$' + formatPrice(iOrderTotalShort, 2) + '</div>';
	strOut += '</td></tr>';				
	strOut += '</table></div>';
	

	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 showFullCart()
{
	document.getElementById("summaryCartShort").className = 'hide'
	document.getElementById("summaryCartFull").className = 'show'
}

function showShortCart()
{
	document.getElementById("summaryCartShort").className = 'show'
	document.getElementById("summaryCartFull").className = 'hide'
}

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
	}
}