
function Product(obj, i) {
	
	var amountValue 	= 0;
	var priceValue 		= jQuery(obj).find('#_price').val();
	
	this.id				= jQuery(obj).find('#productID').val();
	this.name 			= jQuery(obj).find('p').text();
	this.moreButton 	= jQuery(obj).find('#moreButton-' + i);
	this.lessButton 	= jQuery(obj).find('#lessButton-' + i);
	
	this.moreButton.click(function(event) {
		amountValue++;
		updateAmountField();
		return priceToFloat();
	});
	
	this.lessButton.click(function(event) {
		amountValue--;
		if (amountValue < 0) {
			amountValue = 0;
			return amountValue;
		} else {
			updateAmountField();
			return priceToFloat();
		}
	});

	function updateAmountField() {
		var amountValueEnd = amountValue;
		if (amountValueEnd <= 9) amountValueEnd = '0' + amountValueEnd;
		jQuery(obj).find('#_amount-' + i).val(amountValueEnd);
	}

	function priceToFloat() {
		priceValue = priceValue.replace('R$ ', '');
		priceValue = priceValue.replace(',', '.');
		return parseFloat(priceValue);
	}
	
	this.getAmountValue = function() {
		return amountValue;
	}
}

function Store() {

	var products = [];
	var purchaseValue = 0;
	
	jQuery('#table-products tr').each(function(i) {
		var product = new Product(this, i);
		
		jQuery(product.moreButton).click(function(event) {
			purchaseValue += event.result;
			updatePurchaseValue();
		});
		
		jQuery(product.lessButton).click(function(event) {
			purchaseValue -= event.result;
			updatePurchaseValue();
		});
		
		products[i] = product;
	});
	
	jQuery('#finishButton').click(function(event) {
		var amount_str = "";
		var products_str = "";
		var id_str = "";
		
		jQuery(products).each(function() {
			if (this.getAmountValue() > 0) {
				amount_str += "amounts[]=" + this.getAmountValue() + "&";
				products_str += "products[]=" + this.name + "&";
				id_str += "id[]=" + this.id + "&";
			}
		});
		
		jQuery('#products').val(products_str);
		jQuery('#amounts').val(amount_str);
		jQuery('#ids').val(id_str);
	});
	
	function updatePurchaseValue(event) {
		var purchaseValueEnd = purchaseValue;
		jQuery('#purchaseValueField').val('R$ ' + floatToCurrency(roundNumber(purchaseValueEnd)));
	}
	
	function roundNumber(rnum) {
		return Math.round(rnum * Math.pow(10, 2)) / Math.pow(10, 2);
	}
	
	function floatToCurrency(num) {
		x = 0;
		
		if (num < 0) {
			num = Math.abs(num);
			x = 1;
		}
		
		if (isNaN(num)) num = "0";
		cents = Math.floor((num * 100 + 0.5) % 100);
		num = Math.floor((num * 100 + 0.5) / 100).toString();

		if (cents < 10) cents = "0" + cents;
		
		for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
		num = num.substring(0, num.length - (4 * i + 3)) + '.' + num.substring(num.length - (4 * i + 3));
		
		ret = num + ',' + cents; if (x == 1) ret = ' - ' + ret;
		return ret;
	}
	
	function currencyToFloat(currency) {
		currency = currency.replace(".","");
		currency = currency.replace(",",".");
		return parseFloat(currency);
	}
}

jQuery(document).ready(function() {
	var store = new Store();
});














































