function addProdutoCarrinhoCompra()
{
	
	var pr_id  = document.getElementById('pr_id').value;
	
	if(pr_id=='')
	{
		document.getElementById('pr_id').focus;
		alert('Por favor, selecione um título!');
		return false;
	}	
		
	$.ajax({ 
		type: "POST", 
		url: "/paginas/controlaAcoesAjax.php", 
		data: {acao:'addProdutoCarrinhoCompra',pr_id:pr_id},
		success: function(data) {
			//
			var dados = eval(data)
			//
			document.getElementById("compra").style.visibility='visible';
			//
			addRowToTable(pr_id,dados[0].pr_titulo,dados[0].pr_valor);
		} 
	});

}

function addRowToTable(pr_id,titulo,valor)
{ 
    var tbl = document.getElementById('carrinhoCompra'); 
    var lastRow = tbl.rows.length; 
    // if there's no header row in the table, then iteration = lastRow + 1 
    var iteration = lastRow;
    var row = tbl.insertRow(lastRow); 

    //TÍTULO DO PRODUTO
    var cellRight0 = row.insertCell(0); 
    var e0 = document.createElement('input'); 
    cellRight0.setAttribute('align', 'left'); 
    //el.setAttribute('class', 'campo'); 
    e0.setAttribute('type', 'hidden'); 
    e0.setAttribute('name', 'pr_id[]'); 
    e0.setAttribute('id', 'pr_id'+iteration); 
    e0.setAttribute('value', pr_id); 
    //el.setAttribute('size', '40'); 
    //cellRight0.appendChild(z);
    cellRight0.appendChild(document.createTextNode(pr_id+' - '+titulo));
    cellRight0.appendChild(e0);
    
    //QTDE  
    var cellRight1 = row.insertCell(1); 
    var e1 = document.createElement('input'); 
    cellRight1.setAttribute('align', 'right'); 
    ///e1.setAttribute('class', 'campo'); 
    e1.setAttribute('type', 'text');
    e1.setAttribute('value', 1);
    e1.setAttribute('name', 'qtde[]');
    e1.setAttribute('id', 'qtde' + iteration); 
    e1.setAttribute('maxlength', '6');
    //e1.setAttribute('style', 'text-align:right');
    e1.setAttribute('size', '6'); 
    e1.setAttribute('onblur', 'calculaValorTotalProduto('+iteration+')');
    e1.setAttribute('onkeypress', 'return blockNumbers(event);');
    cellRight1.appendChild(e1);
    
 	// VALOR UNITÁRIO 
    var cellRight2 = row.insertCell(2); 
    var e3 = document.createElement('input'); 
    cellRight2.setAttribute('align', 'right'); 
    //e3.setAttribute('class', 'campo'); 
    e3.setAttribute('type', 'hidden'); 
    e3.setAttribute('name', 'valor_unitario[]'); 
    e3.setAttribute('id', 'valor_unitario' + iteration); 
    //e3.setAttribute('maxlength', '10'); 
    e3.setAttribute('value', valor.replace(",",".")); 
    //e3.setAttribute('onkeypress', 'return txtBoxFormat(this.form, this.id, \'99/99/9999\', event)');
    cellRight2.appendChild(document.createTextNode(valor));
    cellRight2.appendChild(e3);

 	// VALOR TOTAL 
    var cellRight3 = row.insertCell(3); 
    var e3 = document.createElement('input'); 
    cellRight3.setAttribute('align', 'right'); 
    e3.setAttribute('disabled', 'disabled'); 
    e3.setAttribute('type', 'text'); 
    e3.setAttribute('name', 'valor_total[]'); 
    e3.setAttribute('id', 'valor_total' + iteration); 
    e3.setAttribute('maxlength', '6');
    //e3.setAttribute('style', 'text-align:right');
    e3.setAttribute('value', valor);
    e3.setAttribute('size', '6'); 
    //e4.setAttribute('onkeypress', 'return txtBoxFormat(this.form, this.id, \'99/99/9999\', event)');
    //cellRight3.appendChild(document.createTextNode(parseFloat(valor)*1+',00'));
    cellRight3.appendChild(e3);

    //<img src="/imagens/bt-excluir.jpeg" alt="EXCLUIR" title="EXCLUIR" width="20"/>
    
 	// BOTÃO REMOVER LINHA 
    var cellRight4 = row.insertCell(4); 
    var e4 = document.createElement('img'); 
    cellRight4.setAttribute('align', 'center'); 
    e4.setAttribute('src', '/imagens/bt-excluir.jpeg'); 
    e4.setAttribute('alt', 'Excluir produto da lista.'); 
    e4.setAttribute('title', 'Excluir produto da lista.'); 
    e4.setAttribute('width', '20'); 
    e4.setAttribute('onClick', 'deleteRow(this.parentNode.parentNode.rowIndex)');
    cellRight4.appendChild(e4);
    
    //CAMPO UTILIZADO PARA CALCULAR O VALOR DO PEDIDO
    var e5 = document.createElement('input'); 
    e5.setAttribute('type', 'hidden'); 
    e5.setAttribute('name', 'valor[]'); 
    e5.setAttribute('id', 'valor'+iteration);
    e5.setAttribute('value',valor);
    cellRight4.appendChild(e5);
    
    /*
     * Funcionalidade que atualiza o valor do pedido 
     * do cliente. 
     */
    atualizaValorPedido();
     
} 

function deleteRow(i)
{
	var tbl = document.getElementById('carrinhoCompra'); 
    var lastRow = tbl.rows.length;
	//
    //alert(lastRow);
    //
	if(confirm("Tem certeza que deseja excluir esse produto?"))
	{
		//
		document.getElementById('carrinhoCompra').deleteRow(i);
		//
		atualizaValorPedido()
		//
		if(lastRow==2)
			document.getElementById("compra").style.visibility='hidden';	
	}	
	
}

function calculaValorTotalProduto(i)
{
	//	
	var qtde = document.getElementById('qtde'+i).value;
	//
	var valor_unitario = document.getElementById('valor_unitario'+i).value.replace(",",".");
	
	if(parseFloat(qtde)==0)
	{
		alert('Valor inválido!');
		//
		document.getElementById('qtde'+i).value = 1;
		//
		document.getElementById('valor'+i).value = parseFloat(valor_unitario)*1;
		//
		document.getElementById('valor_total'+i).value = formatar(parseFloat(valor_unitario)*1);
		//
		atualizaValorPedido()
		//
		return false
	}	
	
	//
	var valor_unitario = document.getElementById('valor_unitario'+i).value.replace(",",".");
	//
	var valor_total = parseFloat(valor_unitario)*qtde;
	//
	//document.getElementById('valor_total'+i).value = valor_total.toFixed(2).replace(".",",");
	//
	document.getElementById('valor_total'+i).value = formatar(valor_total);
	//
	document.getElementById('valor'+i).value = valor_total;
	
	atualizaValorPedido();
	
}	

/*
 * 
 * 
 */
function atualizaValorPedido()
{
	//
	var numElement   = eval(document.getElementById('form1').length);
	var obj          = document.getElementById('form1');
	var valor_pedido = 0;
	//
	for(i=0;i<numElement;i++)
	{
		 //
		if(obj.elements[i].name=='valor[]')
		{
			//
			//alert(obj.elements[i].value);
			//
			valor_pedido = valor_pedido + parseFloat(obj.elements[i].value.replace(",","."));
		}	 
	}	 
	//
	document.getElementById('valor_do_pedido').value = formatar(valor_pedido); 
}

function formatar(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 registraCarrinhoLivraria()
{
	//dados = jQuery("#form1").serialize();
	//alert(dados);
	document.form1.submit();
}