var qtdProduto = 1;

function formatarMoeda(fld, e) {   
 var milSep = "";
 var decSep = ".";
 var sep = 0;   
 var key = '';   
 var i = j = 0;   
 var len = len2 = 0;   
 var strCheck = '0123456789';   
 var aux = aux2 = '';   
 var whichCode = (window.Event) ? e.which : e.keyCode;   
 if (whichCode == 13 || whichCode == 0 || whichCode == 8)
 {
  return true;  // Enter   
 } 

 
 key = String.fromCharCode(whichCode);  // recebe o valor da chave vinda da chave do código  
 if (strCheck.indexOf(key) == -1) return false;  // Chave não válida  
 len = fld.value.length;   
 for(i = 0; i < len; i++)   
 if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;   
 aux = '';   
 for(; i < len; i++)   
 if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);   
 aux += key;   
 len = aux.length;   
 if (len == 0) fld.value = '';   
 if (len == 1) fld.value = '0'+ decSep + '0' + aux;   
 if (len == 2) fld.value = '0'+ decSep + aux;   
 if (len > 2) {   
 aux2 = '';   
 for (j = 0, i = len - 3; i >= 0; i--) {   
 if (j == 3) {   
 aux2 += milSep;   
 j = 0;   
 }   
 aux2 += aux.charAt(i);   
 j++;   
 }   
 fld.value = '';   
 len2 = aux2.length;   
 for (i = len2 - 1; i >= 0; i--)   
 fld.value += aux2.charAt(i);   
 fld.value += decSep + aux.substr(len - 2, len);   
 }   
 return false;   
 }   


function formataMoeda(val)
{
	val = Math.round(val*100)/100;
	val = val.toString();
	
	if(!val.match(/\./))
	{
		return val + ".00";
	}
	
	if(val.match(/\.([0-9]{1,2})/))
	{
		val = val.replace(/\.([0-9]{1,2})/, ".$1");
		if(!val.match(/\.([0-9]{2})/))
		{
			val = val + "0";
		}
	}
	
	
	return val;
}

function escolheCliente(obj)
{
	objForm = document.getElementById('escolhaCliente');
	if(objForm.idCliente[objForm.idCliente.selectedIndex].value != "")
	{
		objForm.submit();
	}
}

function retirar(num)
{
	document.getElementById('tbProdutos').removeChild(document.getElementById('tr_' + num));
}

function camposAdicionais(valor)
{
	if(valor == 'expedido')
	{
		mostra('campos');
	}
	else
	{
		esconde('campos');
	}
}

function mostra(nome)
{
	obj = document.getElementById(nome);
	obj.style.display = '';
}

function esconde(nome)
{
	obj = document.getElementById(nome);
	obj.style.display = 'none';
}

function inserirLinhaProduto()
{
	var tr;
	var td;
	
	qtdProduto++;

	td1 = document.createElement('TD');
	td2 = document.createElement('TD');
	td3 = document.createElement('TD');
	td4 = document.createElement('TD');
	td5 = document.createElement('TD');
	td6 = document.createElement('TD');
	td7 = document.createElement('TD');
	
	td1.setAttribute('align', 'center');
	td2.setAttribute('align', 'center');
	
	tr = document.createElement('TR');
	tr.setAttribute('id', 'tr_' + qtdProduto);

	td1.innerHTML = '<input name="codigo_' + qtdProduto + '" type="text" size="2" />';
	td2.innerHTML = '<input onblur="document.forms[0].total_' + qtdProduto + '.value = (document.forms[0].valor_' + qtdProduto + '.value * this.value)" name="quantidade_' + qtdProduto + '" type="text" size="2" />';
	td3.innerHTML = '<input name="descricao_' + qtdProduto + '" type="text" size="37" />';
	td4.innerHTML = '<input name="unidade_' + qtdProduto + '" type="text" size="4" />';
	td5.innerHTML = '<input onblur="document.forms[0].total_' + qtdProduto + '.value = (document.forms[0].quantidade_' + qtdProduto + '.value * this.value)" onKeyPress="return(formatarMoeda(this,event));" name="valor_' + qtdProduto + '" type="text" size="8" />';
	td6.innerHTML = '<input name="total_' + qtdProduto + '" type="text" size="8" readonly="readonly" />';
	td7.innerHTML = '<a href="javascript:retirar(' + qtdProduto + ')">Retirar</a>';	
		
	tr.appendChild(td1);
	tr.appendChild(td2);
	tr.appendChild(td3);
	tr.appendChild(td4);
	tr.appendChild(td5);
	tr.appendChild(td6);
	tr.appendChild(td7);
	
	document.getElementById('tbProdutos').appendChild(tr);
	qtd = parseInt(document.getElementById('formQuantidade').value);
	qtd++;
	document.getElementById('formQuantidade').value = qtd;
}

/* Exibe e esconde o div com as respostas das perguntas sobre GLP */
function exibeResposta(id)
{
	for(i=1;i<=12;i++)
	{
		if (document.getElementById('idResposta'+i))
		{
			document.getElementById('idResposta'+i).style.display = 'none';
		}
	}
	
	if (document.getElementById(id))
	{
		var display = document.getElementById(id).style.display;
		if (display=='none')
		{
			document.getElementById(id).style.display = 'block';
		}
		else
		{
			document.getElementById(id).style.display = 'none';
		}
	}
}

function campo_busca(obj,evento)
{
	if(evento == 'focus')
	if(obj.value == 'palavra-chave') obj.value = '';
	if(evento == 'blur')
	if(!obj.value) obj.value = 'palavra-chave';
}

function abrepopup(url,sizex,sizey)
{
	if(sizey == 'undefined' || !sizey) sizey = '420';
	if(sizex == 'undefined' || !sizex) sizex = '420';
	dateobject = new Date();
	jan = window.open(url,"login_window",'width='+sizex+',height='+sizey+',scrollbars=yes,resizable=no,location=no,directories=no,status=no,menubar=no,top=150,left=150');
}
