// JavaScript Document
function Janela(url, width, height,scr){
window.open(url,"HDS","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=" + scr + ",resizable=no,menubar=no,width="+width+",height="+height);
}
//Barras Status
var hellotext="Concluído"
var thetext=""
var started=false 
var step=0
var times=1 
function welcometext()
{
times--
if (times==0)
{
if (started==false) 
{ 
started = true;
window.status = hellotext; 
setTimeout("anim()",1);
}
thetext = hellotext;
}
}

function showstatustext(txt) 
{
thetext = txt;
setTimeout("welcometext()",1000) 
times++ 
}

function anim() 
{
step++
if (step==5) {step=1}
if (step==1) {window.status=thetext}
if (step==2) {window.status=thetext}
if (step==3) {window.status=thetext}
if (step==4) {window.status=thetext}

setTimeout("anim()",50); 
} 
welcometext();

//Fim Barra Status
function calculaPrecoTotal(qtde,unitario){
	var total = 0;
	total = parseFloat(qtde) * parseFloat(unitario);
	document.getElementById('total').value = total.toFixed(2);
}

//Mascaras
function txtBoxFormat(strField, sMask, evtKeyPress){
	var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
	if(document.all){
		nTecla = evtKeyPress.keyCode;
	}
	else if(document.layers){
		nTecla = evtKeyPress.which;
	}
	sValue = document.getElementById([strField]).value;
	sValue = sValue.toString().replace( "-", "" );      
	sValue = sValue.toString().replace( "-", "" );      
	sValue = sValue.toString().replace( ".", "" );      
	sValue = sValue.toString().replace( ".", "" );      
	sValue = sValue.toString().replace( "/", "" );      
	sValue = sValue.toString().replace( "/", "" );      
	sValue = sValue.toString().replace( "(", "" );      
	sValue = sValue.toString().replace( "(", "" );      
	sValue = sValue.toString().replace( ")", "" );      
	sValue = sValue.toString().replace( ")", "" );      
	sValue = sValue.toString().replace( " ", "" );      
	sValue = sValue.toString().replace( " ", "" );      
	
	fldLen = sValue.length;      
	mskLen = sMask.length;
	i = 0;      
	nCount = 0;      
	sCod = "";      
	mskLen = fldLen;      
	while(i <= mskLen){        
		bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
		bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))
		if(bolMask){
			sCod += sMask.charAt(i);
			mskLen++;
		}
		else{
		    sCod += sValue.charAt(nCount);          
			nCount++;        
		}        
		i++;
	}
	document.getElementById([strField]).value = sCod;      
	if(nTecla != 8){
		if(sMask.charAt(i-1) == "9"){
			return ((nTecla > 47) && (nTecla < 58));
		}
		else{
			return true;
		}
	}
	else{        
		return true;      
	}    
}

// ÁREA ADMINISTRATIVA
function statusProdutos(){
	var produto = document.getElementById('listaProd').value;
	var msg = "";
	
	if(produto=="vazio"){
		msg = "- Escolha um produto válido.\n";
	}
	
	if(msg!="")
		alert(msg);
	else if(msg==""){
		document.form.action="php/scr_altera_status_produto.php?codPro="+produto+"&stat="+document.getElementById('statusHidden').value;
		document.form.submit();
	}
}

function verificarTecla(eventoTecla){
	var browser = navigator.appName;
	browser = browser.split(" ");
	
	if(browser[0]=="Microsoft"){
		tecla = eventoTecla.keyCode;
	}
	else if(browser[0]!="Microsoft"){
		tecla = eventoTecla.which;
	}
	
	return tecla;
}


function verifNovoCodigo(codRepresentante){
	document.getElementById('btCliente').style.display = 'none';
	document.getElementById('cnpj').disabled = 'true';
	
	if(document.getElementById('status').value != 'A'){
		if(document.getElementById('codigo').value == ''){
			Janela('busca_produtos.php?paramEmpresa='+document.getElementById('empresa').value+'&paramCnpj='+document.getElementById('cnpjEscolhido').value+'&representante='+codRepresentante,384,344,'no');
		}
		else{
			MostraProdutos('paramCodigo='+document.getElementById('codigo').value+'&paramEmpresa='+document.getElementById('empresa').value+'&paramCnpj='+document.getElementById('cnpjEscolhido').value+'&representante='+codRepresentante);
		}
	}
	else{
		alert('Você está alterando um item, cancele ou confirme essa operação antes de proseguir!');
	}
}

function btAdicionarItem(itemPodeSerIncluido){	
    var itensIncluidos;
	var itemSelecionado = document.getElementById('codProduto').value;
	var achou = false;
	var resposta;
	
    if(itemPodeSerIncluido == 'S'){
		if(document.getElementById('quantidade').value != '' && document.getElementById('quantidade').value != 0){
			if(document.getElementById('itensJaIncluidos')){
				itensIncluidos = document.getElementById('itensJaIncluidos').value;
				itensIncluidos = itensIncluidos.split(';');
				for(i = 0; i < itensIncluidos.length; i++){
					if(itensIncluidos[i] == itemSelecionado){
						resposta = confirm('Item já incluído! Deseja adicionar " ' + document.getElementById('quantidade').value + ' " item(s) na quantidade atual? ');
						if (resposta) {
							IncluirCesta('paramCodigo='+document.getElementById('codProduto').value+'&paramDescricao='+document.getElementById('descricao').value+'&paramQuantidade='+document.getElementById('quantidade').value+'&paramUnitario='+document.getElementById('unitario').value+'&paramTotal='+document.getElementById('total').value+'&paramEmpresa='+document.getElementById('empresa').value+'&paramCodigoCli='+document.getElementById('codCliente').value);document.getElementById('divFinalizando').style.display='inline';							
						}else{
							document.getElementById('codigo').focus();
						}
						achou = true;
					}
				}
			}
			if(!achou){
				IncluirCesta('paramCodigo='+document.getElementById('codProduto').value+'&paramDescricao='+document.getElementById('descricao').value+'&paramQuantidade='+document.getElementById('quantidade').value+'&paramUnitario='+document.getElementById('unitario').value+'&paramTotal='+document.getElementById('total').value+'&paramEmpresa='+document.getElementById('empresa').value+'&paramCodigoCli='+document.getElementById('codCliente').value);document.getElementById('divFinalizando').style.display='inline';
			}
		}else {
			alert(' - Digite a Quantidade!');
		}
	}else{
		alert(' - Produto com valor incorreto, favor entrar em contato com o Dpt. De Vendas.');
	}
}

function liberaBt(){
	if(document.getElementById('btPro').disabled){
		document.getElementById('btPro').disabled = false;
	}else{
		document.getElementById('btPro').disabled = true;
	}
}
function showPopUp(){
	document.getElementById("promo").style.display = 'inline';
	$("#promo").dialog({ 
		modal: true,
		resizable: false,
		closeOnEscape: false,
		width: 700,
		height: 300,
		overlay: { 
			opacity: 0.5, 
			background: "black" 
		}	 
	});
}

function hidePopup(parametros){
	LogPromocao(parametros);
	$("#promo").dialog("close");
}
