function intval (mixed_var, base) 
{
	// Get the integer value of a variable using the optional base for the conversion  
    // 
    // version: 910.813
    // discuss at: http://phpjs.org/functions/intval    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: stensi
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Matteo
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)    // *     example 1: intval('Kevin van Zonneveld');
    // *     returns 1: 0
    // *     example 2: intval(4.2);
    // *     returns 2: 4
    // *     example 3: intval(42, 8);    // *     returns 3: 42
    // *     example 4: intval('09');
    // *     returns 4: 9
    // *     example 5: intval('1e', 16);
    // *     returns 5: 30
	
    var tmp;

	var type = typeof(num);
	
	tmp = parseInt(mixed_var, base || 10);
    return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp;
}

function chgNumber(num, amt, dir)
{
	var numI = intval(num);
	
	if(dir == 'up')
	{
		return (numI + amt);
	}
	
	if(dir == 'down')
	{
		newNum = numI - amt;
		
		return (newNum < 0) ? 0 : newNum;
	}
}

function numberFormat(nStr, prefix)
{
	var prefix = prefix || '';
	nStr += '';
	
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	
	var rgx = /(\d+)(\d{3})/;
	
	while (rgx.test(x1))
    	x1 = x1.replace(rgx, '$1' + ',' + '$2');
	
    return prefix + x1 + x2;
}

function updateTotal(quantity, price, symbol)
{
	var num = quantity * price;
	
	if(num < 0)
		num = 0;

	return numberFormat(num.toFixed(2), symbol)
}

var valueArr = [];

function updateGrandTotal(currencySymbol, arr, cartTotalWithoutItem)
{
	var grandTotal = 0;
	arr.each(function(item) {
//			alert($(item[0]).value);
//			alert(item[1]);
		grandTotal = grandTotal + ($(item[0]).value * item[1]);
	});
	var totalTotal = cartTotalWithoutItem + grandTotal;
	$('item-grand-total').update(numberFormat(grandTotal.toFixed(2), currencySymbol));
//	$('item-combined-total').update(numberFormat(totalTotal.toFixed(2), currencySymbol));
}

var valueArrOld = [];

function updateGrandTotalOld(currencySymbol, arr, cartTotalWithoutItem)
{
	grandTotal = 0;
	
	arr.each(function(item) 
	{
//		alert($(item[0]).value);
//		alert(item[1]);
			
		grandTotal = grandTotal + ($(item[0]).value * item[1]);
	});
	
	totalTotal = cartTotalWithoutItem + grandTotal;
	
	$('old-item-grand-total').update(numberFormat(grandTotal.toFixed(2), currencySymbol));
//	$('old-item-combined-total').update(numberFormat((totalTotal).toFixed(2), currencySymbol));
}

function updateJSON(oReq, currencySymbol)
{
	var data = eval(oReq.responseText);
	
	for(elId in data)
	{
		if(elId != 'action')
		{
			if(elId == 'popcart-total-top')
			{
				Element.update(elId, "("+ data[elId]['itemcount'] +" items) " + numberFormat(data[elId]['itemtotal'].toFixed(2), currencySymbol));
			}
			else
				Element.update(elId, numberFormat(data[elId].toFixed(2), currencySymbol));
		}
		else
		{
			if(elId == 'action')
			{
				if(data['action']['q'] == 1)
					$('item-up-' + data['action']['uq']).disabled = true;
				else
					$('item-up-' + data['action']['uq']).disabled = false;
			}
		}
	}

	return true;
}

function updatePackageItemJSON(oReq, currencySymbol)
{
	var data = eval(oReq.responseText);
	
	for(elId in data)
	{
		if(elId != 'action' && elId != 'status')
		{
			if(elId == 'popcart-total-top')
			{
				Element.update(elId, "("+ data[elId]['itemcount'] +" items) " + numberFormat(data[elId]['itemtotal'].toFixed(2), currencySymbol));
			}
			else
			{
				Element.update(elId, data[elId]);
			}
		}
		else
		{
			if(elId == 'action')
			{
				if(data['action']['q'] == 0)
					$('item-up-' + data['action']['uq']).disabled = true;
				else
					$('item-up-' + data['action']['uq']).disabled = false;
			}
			
			if(elId == 'status')
			{
				if(data['status']['s'] == 1)
				{
					$(data['status']['id']).removeClassName('incomplete');
					$(data['status']['id']).addClassName('complete');
				}
				else
				{
					$(data['status']['id']).removeClassName('complete');
					$(data['status']['id']).addClassName('incomplete');
				}	
			}
		}
	}

	return true;
}

function checkPackageItemQuantity(id, maxQuantity, currVal) 
{
	if(isNaN($(id).value))
	{
		$(id).value = currVal;
	}
	
	if($(id).value < 0)
	{
		$(id).value = $(id).value * -1;
	}
	
	$(id).value = Math.floor($(id).value);
	
	if($(id).value > maxQuantity)
	{
		$(id).value = maxQuantity;
		alert('The maximum available quantity for this item is ' + maxQuantity + '.');
	}
}

function togglePackageItem(id)
{
	$('pb-' + id).toggle();
	$('pw-' + id).toggleClassName('closed');
	$('pw-' + id).toggleClassName('open');
	
	fleXenv.fleXcrollMain('productList');
}

function updatePackagesInCartJSON(oReq)
{
	var data = eval(oReq.responseText);
	
	for(elId in data)
	{
//		if(elId != 'remaining-packages')
//			Element.update(elId, data[elId]);
//		else
//		{
			if(elId == 'remaining-packages')
			{
				if(data['remaining-packages']['value'] == 0)
				{
					$$('.' + data['remaining-packages']['class']).invoke('hide');
					$('package-table').hide();
					$('active-packages-button').removeClassName('has-active-packages');
					$('no-packages-message').show();
				}
			}
//		}
	}

	return true;
}

function checkDownloadQuantity(id, maxQuantity, currVal) 
{
	if(isNaN($(id).value))
	{
		$(id).value = currVal;
	}
	
	if($(id).value < 0)
	{
		$(id).value = $(id).value * -1;
	}
	
	$(id).value = Math.floor($(id).value);
	
	if($(id).value > maxQuantity)
	{
		$(id).value = maxQuantity;
		alert('The maximum available quantity for this item is ' + maxQuantity + '.');
	}
}

function updateDownloadJSON(oReq)
{
	var data = eval(oReq.responseText);
	
	for(elId in data)
	{
		if(elId == 'download-url')
		{
			window.location = data[elId];
		}
	}

	return true;
}

