/////////////////////////////////////////////////////////////////////////
// needed to display the cart of on the main list page
function viewCartContents() { 
//alert("viewCartContents");
	// if we're logged in, show the cart
	http.open('get', './scripts/ajax_catalog.php?action=viewCart');
	if (http.overrideMimeType) { http.overrideMimeType("application/json"); }
//	http.onreadystatechange = showAjaxReturn;
	http.onreadystatechange = setShoppingCart;
	http.send(null);
}
function setShoppingCart() { // return from viewCartContents
//alert("setShoppingCart");
    if(http.readyState == 4){
		document.getElementById('dbg').style.visibility = 'visible';
		document.getElementById('dbg').innerHTML = http.responseText;

		document.getElementById('dialog').style.visibility = 'hidden';
    	var r = eval("(" + http.responseText + ")");
    	
		var text = '<div class="dpTitle">Your Current Selections</div>';
		text += '<div id="dpBody">';

// if there are no items yet (normal for just having logged in, until waitlist is implemented)
		var hasSelection = false;
		var subtext = '';
//		var divHeight = 100;
		if (!r.cart) {
//			subtext = '<p>You have no items selected yet</p>';
			
		} else { // else fill the cart
			var subttl = 0;
			subtext += '<table>';
			for(var i=0; i<r.cart.length; i++) {
				subtext += '<tr>';
////				subtext += '<div class="dpLineItemSp"><nobr>';
////				subtext += '<div class="toLeft">' + r.cart[i].title + '</div>';
				subtext += '<td class="toLeft"><nobr>' + r.cart[i].title + '</nobr></td>';
////				subtext += '<div class="imgRight">' + 
////						'<a href="javascript:removeItem(' + "'" + r.cart[i].pcode + "'" + ')">' +
////						'<img src="./images/checkmark.gif" alt="Remove" border="0" align="bottom">' +
////						'</a>' +
////						'</div>';
////				subtext += '<div class="toRight">' + formatCurrency(r.cart[i].price) + '</div>';
				subtext += '<td class="cartCenter">' + formatCurrency(r.cart[i].price) + '</div>';
				subtext += '<td class="cartCenter">' + 
						'<a href="javascript:removeItem(' + "'" + r.cart[i].pcode + "'" + ')">' +
						'<img src="./images/checkmark.gif" alt="Remove" border="0" align="bottom">' +
						'</a>' +
						'</td>';
////				subtext += '<div class="clr"></div>';
////				subtext += '</nobr></div>';
				subtext += '</tr>';
				subttl += parseFloat(r.cart[i].price);
//				divHeight += 25;
				hasSelection = true;
			}
/*
			subtext += '<p>';
			subtext += '<div class="dpLineItemSp"><nobr>';
			subtext += '<div class="toLeft">&nbsp;&nbsp;&nbsp;</div>';
			subtext += '<div class="toRight"><img src="./images/checkmark.gif" style="visibility: hidden;"></div>';
			subtext += '<div class="toRight">' + formatCurrency(subttl) + '</div>';
			subtext += '<div class="toRight">Total</div>';
			subtext += '<div class="clr"></div>';
			subtext += '</nobr></div>';
			subtext += '</p>';
*/
			subtext += '<tr>';
			subtext += '<td colspan="3" class="line"><hr></td>';
			subtext += '</tr>';

			subtext += '<tr>';
			subtext += '<td align="right">Total</td>';
			subtext += '<td class="cartCenter">' + formatCurrency(subttl) + '</td>';
			subtext += '</tr>';


			subtext += '</table>';
		}

	if (hasSelection) {
		text += subtext;
	} else {
		text += '<div class="dpLineItemSp">' +
				'<p><nobr>You have no webinars selected yet</nobr></p>' +
				'</div>';
	}
	
	text += '</div>';
	if (hasSelection) {		
		text += '<div class="dpBtn">' +
			'<input type="button" value="Select More" onClick="closeDialogToMain()">';
		text += '&nbsp;&nbsp;&nbsp;' +
			'<input type="button" value="Proceed to Checkout" onClick="checkout()">';
	} else {
		text += '<div class="dpBtn">' +
			'<input type="button" value="View Webinars" onClick="closeDialogToMain()">';
	}		
	text +=	'</div>';		

	document.getElementById('dialog').innerHTML = text;
//	document.getElementById('dialog').style.height = parseInt(divHeight) + 'px';
	document.getElementById('dialog').style.height = 'auto';
	moveDiv('dialog');
	document.getElementById('dialog').style.visibility = 'visible';
	}
}

/////////////////////////////////////////////////////////////////////////////
// miscellaneous functions
/////////////////////////////////////////////////////////////////////////////
function closeDialog() {
	document.getElementById('dialog').style.visibility = 'hidden';
}
function closeDialogToMain() {
	document.getElementById('dialog').style.visibility = 'hidden';
	window.location.href = 'index.php';
}

function showAjaxReturn() {
    if(http.readyState == 4){
		document.getElementById('dbg').style.visibility = 'visible';
		document.getElementById('dbg').innerHTML = http.responseText;
	}
}

function checkout() {
	window.location.href = "checkout.php";
}
function getPayment() {
	window.location.href = "getPayment.php";
}
function findMore() {
	window.location.href = "index.php";
}

function comingSoon() {
	alert("Coming soon to a browser near you...");
}

function formatCurrency(num) {
    num = isNaN(num) || num === '' || num === null ? 0.00 : num;
    return parseFloat(num).toFixed(2);
}

/////////////////////////////////////////////////////////////////////////

