//remove cart item
function deleteItemRad(rowId){			
	try {
		ExecTelerikCallback("del" + rowId, "");
	} catch(ex) { alert("delete item error: " + ex.description); }
}	

//validate qty
function validate(){
	if (isNaN(document.getElementById('tbQty').value)){
		alert("Invalid Quantity");
		return false;
	}
}

function getItems(){				
	trace('getItems');
    var params = '';
    try {
        // reference total items textbox in my cart control
        var tbMyCartTotalItems = document.getElementById("Header_MyCart_lblItems");
        var tbMyCartTotalPrice = document.getElementById("Header_MyCart_lblTotal");

        var totalPrice = 0;
        var myCartTotalItems = 0;

        // rewrite of below in jQuery :: OMG jQUERY PWNAGE
        $('input[id$=_tbItemId]').each(function(i) {
            try {
                var itemId = this.value;
                var qty = 0;
                var price = 0;
                var subtotal = 0;
                
                if ($('input[id$=_tbQty]').length < i) throw 'Quantity for item ' + itemId + ' not found';
                qty = $('input[id$=_tbQty]')[i].value;
                if (qty <= 0) throw 'Quantity must be greater than 0. If you wish to remove an item, please click on remove button';

                if ($('span[id$=_lblPrice]').length < i) throw 'Price for item ' + itemId + ' not found';
                price = $('span[id$=_lblPrice]')[i].innerHTML.replace('$', '');

                if ($('span[id$=_lblItemSubTotal]').length < i) throw 'Sub total for item ' + itemId + ' not found';
                subtotal = $('span[id$=_lblItemSubTotal]')[i].innerHTML.replace('$', '');
                
                trace('itemId: ' + itemId);
                trace('qty: ' + qty);

                params += itemId + '=' + qty + ';';
                myCartTotalItems += eval(qty);
                totalPrice += parseFloat(subtotal);

                trace(params);
            } catch (e) {
                throw e;
            }
        });

        tbMyCartTotalItems.innerHTML = myCartTotalItems;
        trace(myCartTotalItems);
        tbMyCartTotalPrice.innerHTML = "$" + totalPrice.toFixed(2);
        trace("$" + totalPrice.toFixed(2));
    } catch (ex) {
        if (/^Quantity/i.test(ex) || /^Price/i.test(ex) || /^Sub/i.test(ex)) {
            alert(ex);
        } else {
            alert('Unknown error caught: ' + ex.description);
        }
    } finally {
        return params;
    }
}

//update cart totals with given qty's
function updateCartRad(){		
	try {		
		//reset error control
		if (document.getElementById('ErrorControlFrontEnd_plError') != null)
			document.getElementById('ErrorControlFrontEnd_plError').innerHTML = '';
		//build params
		var param = getItems();			
		//execute callback
		ExecTelerikCallback(param, "");		
	} catch(ex) { alert("update item error: " + ex.description); }	
}

$(function() {
		trace(getItems());			
});

// callback function for ajax panel on MyCart page
MyCart_OnResponseEnd = function(sender, arguments) {
trace(getItems());	
};