var currency = 'R$';
/**
 * @param {Int} itemID
 */
function cartRemoveItem(itemID){
	$.get("/cart.php", { option: "removeItem", itemID: itemID },
		function(data){
			var obj = $('#cartRow'+data);
			obj.css({'background-color' : '#FF8888'});
			obj.fadeOut('normal', function(){				
				obj.remove();
			});
	});
}
function cartRemoveAll(){
	$.get("/cart.php", { option: "removeAll"},
		function(data){
			var obj = $('.tbcart > tbody > *'); 
			obj.css({'background-color' : '#FF8888'});
			obj.fadeOut('normal', function(){
				obj.remove();
				$('#cartTotal').text(currency+' 0,00');
			});
	});
}
function cartChangeQuantity(productID, newQuantity){
	//alert('itemID: '+itemID + "\nnewQuantity: "+newQuantity);
	$.getJSON("/cart.php", {
		option: "changeQuantity", 
		itemID: productID, 
		newQuant: newQuantity		
		}, 
		function(json){
			$('#t_'+productID).text(json.itemTotal);			
			$('#totalValue').text(currency+' '+json.cartTotal);			
		}
	);
}
$(document).ready(function() {	
	$(".quantity").change(function () {		
		var itemID = $(this).attr("id");
		// n_
		itemID = itemID.substr(2);
		cartChangeQuantity(itemID, this.value);
	});
	
	/*
	$("#cartClear").click(function () {
		 cartRemoveAll();
		 return false;
	});

	$(".cartRemoveItem").click(function () {				
		var itemID = $(this).attr("id");
		// n_
		itemID = itemID.substr(2);
		cartRemoveItem(itemID);
		return false;
	});
	*/	
});

function valida(form){
	var radios = new Array();
	for(i=0;i<form.elements.length; i++){
		var el = form.elements[i];
		
		// Radio
		if(el.type == "radio"){			
			var achou = false;
			for(x=0;x<radios.length;x++){
				if(radios[x].nome==el.name){
					achou = true;
					if (el.checked){
						radios[x].checked = true
						radios[x].valor = el.value;
						radios[x].titulo = el.title;
					}
					break;
				}
			}
			if(! achou){
				var obj = new objetos();
				obj.nome = el.name;
				obj.valor = el.value;
				obj.checked = el.checked;
				obj.titulo = el.title;
				radios.push(obj);
			}			
			aux = '';
		}
		
		// Outros componentes
		else{		
			if(el.title != ''){
				if(el.value.length == 0){
					alert(el.title + ' é uma informação necessária!');
					el.focus();
					return false;
				}
			}
		}
	}
	
	for(x=0;x<radios.length;x++){
		if(!radios[x].checked){
			alert(radios[x].titulo);
			return false;
		}
	}		
	return true;
}