﻿//*** ShoppingCartSummary.js
//*** Common functions for updating the number of verification and cost in the cart summary.

//*** Function which will update number of verifications and cost in the cart summary.
//*** - Assumes input string in format [#_OF_VER,COST]
function UpdateCartSummary(InfoStr) {
    var ShopCartCount = $get(ShopCartCountId);
    var ShopCartCost = $get(ShopCartCostId);
    var cnt  = InfoStr.substring(InfoStr.indexOf("[")+1, InfoStr.indexOf(","));
    var cost = InfoStr.substring(InfoStr.indexOf(",")+1, InfoStr.indexOf("]"));
    if (ShopCartCount) { ShopCartCount.innerHTML = cnt; }
    if (ShopCartCost) { ShopCartCost.innerHTML = FormatCurrency(cost); }
}

//*** Helper function to format current with JavaScript.
function FormatCurrency(strValue) {
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);
}

//*** Get the number of items in the cart from lblCount.
//*** - If number is greater than zero, return true.
//*** - If number is equal to zero, alert the user and return false.
function VerifyCart() {
    var output = false;
    var ShopCartCountLbl = $get(ShopCartCountId);
    if (ShopCartCountLbl != null) {
        var ItemsInCart = parseInt(ShopCartCountLbl.innerHTML);
        if (ItemsInCart > 0) {
            output = true;
        }
        else {
            var EmptyCartPopup = $find('jsEmptyCartBehavior');
            if (EmptyCartPopup != null) {
                EmptyCartPopup.show();
            }
            output = false;        
        }
    }
    return output;
}

//*** Button implementation which will redirect if Cart is valid.
function HandleCheckout() {
    if (VerifyCart()) {
        document.location='/NLV/EditPayment.aspx';
    }
}

//*** Notify the AJAX system this script is loaded.  
if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

