var
	par = '';
/*******************************************/
//                  LOGIN
/*******************************************/
function loadXMLDocLogin(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeLogin;
        req.open("GET", url + '?' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeLogin;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeLogin(){
	var retorno;
    if (req.readyState == 4) {
        if (req.status == 200){
			window.location = req.responseText;
			document.form.reset();
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function Login(parametros){
    /*alert('Prezado Representante,\n\n          Estamos atualizando nosso sistema de pedidos online, por este motivo, o sistema poderá passar por momentos de instabilidade.\n\n          Qualquer problema entre em contato conosco.\n\n          Obrigado!\n\n\n');*/
    document.getElementById('loading').style.visibility = 'visible';
    document.getElementById('Login').style.display = 'none';
    loadXMLDocLogin("inc/loginUsuario.php",parametros);
}
/*******************************************/
//          CONSULTA PRODUTO
/*******************************************/
function loadXMLDocProduto(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeProduto;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeProduto;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeProduto(){
	var retorno;
    if (req.readyState == 4) {
        if (req.status == 200){
			document.getElementById('loading').style.visibility = 'hidden';
			document.getElementById('Consulta').innerHTML = req.responseText;
			document.getElementById('Consulta').style.display = 'inline';
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function Produto(parametros){
	document.getElementById('loading').style.visibility = 'visible';
	document.getElementById('Consulta').style.display = 'none';
    loadXMLDocProduto("inc/scr_consulta_produto.php",parametros);
}

/*******************************************/
//          CONSULTA CLIENTE
/*******************************************/
function loadXMLDocCliente(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeCliente;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeCliente;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeCliente(){
	var retorno;
    if (req.readyState == 4) {
        if (req.status == 200){
			document.getElementById('loading').style.visibility = 'hidden';
			document.getElementById('Consulta').innerHTML = req.responseText;
			document.getElementById('Consulta').style.display = 'inline';
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function Cliente(parametros){
	document.getElementById('loading').style.visibility = 'visible';
	document.getElementById('Consulta').style.display = 'none';
    loadXMLDocCliente("inc/scr_consulta_cliente.php",parametros);
}
/*******************************************/
//          DETALHES CLIENTE
/*******************************************/
function loadXMLDocClienteDetalhes(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeClienteDetalhes;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeClienteDetalhes;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeClienteDetalhes(){
	var retorno;
    if (req.readyState == 4) {
        if (req.status == 200){
			document.getElementById('loading').style.visibility = 'hidden';
			document.getElementById('Consulta').innerHTML = req.responseText;
			document.getElementById('Consulta').style.display = 'inline';
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function ClienteDetalhes(parametros){
	document.getElementById('loading').style.visibility = 'visible';
	document.getElementById('Consulta').style.display = 'none';
    loadXMLDocClienteDetalhes("detalhes_cliente.php",parametros);
}
/*******************************************/
//          CONSULTAR NF
/*******************************************/
function loadXMLDocNotaFiscal(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeNotaFiscal;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeNotaFiscal;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeNotaFiscal(){
	var retorno;
    if (req.readyState == 4) {
        if (req.status == 200){
			document.getElementById('loading').style.visibility = 'hidden';
			document.getElementById('Consulta').innerHTML = req.responseText;
			document.getElementById('Consulta').style.display = 'inline';
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function NotaFiscal(parametros){
	document.getElementById('loading').style.visibility = 'visible';
	document.getElementById('Consulta').style.display = 'none';
    loadXMLDocNotaFiscal("inc/scr_consulta_nf.php",parametros);
}
/*******************************************/
//          DETALHES NF
/*******************************************/
function loadXMLDocNotaFiscalDetalhes(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeNotaFiscalDetalhes;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeNotaFiscalDetalhes;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeNotaFiscalDetalhes(){
	var retorno;
    if (req.readyState == 4) {
        if (req.status == 200){
			document.getElementById('loading').style.visibility = 'hidden';
			document.getElementById('Consulta').innerHTML = req.responseText;
			document.getElementById('Consulta').style.display = 'inline';
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function NotaFiscalDetalhes(parametros){
	document.getElementById('loading').style.visibility = 'visible';
	document.getElementById('Consulta').style.display = 'none';
    loadXMLDocNotaFiscal("detalhes_nf.php",parametros);
}
/*******************************************/
//          LEMBRAR SENHA
/*******************************************/
function loadXMLDocLembrarSenha(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeLembrarSenha;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeLembrarSenha;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeLembrarSenha(){
	var retorno;
    if (req.readyState == 4) {
        if (req.status == 200){
			document.getElementById('loading').style.visibility = 'hidden';
			document.getElementById('Consulta').innerHTML = req.responseText;
			document.getElementById('Consulta').style.display = 'inline';
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function LembrarSenha(parametros){
	document.getElementById('loading').style.visibility = 'visible';
	document.getElementById('Consulta').style.display = 'none';
    loadXMLDocLembrarSenha("inc/scr_lembrar_senha.php",parametros);
}
/*******************************************/
//          TROCAR SENHA
/*******************************************/
function loadXMLDocTrocarSenha(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeTrocarSenha;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeTrocarSenha;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeTrocarSenha(){
	var retorno;
    if (req.readyState == 4) {
        if (req.status == 200){
			document.getElementById('loading').style.visibility = 'hidden';
			document.getElementById('Consulta').innerHTML = req.responseText;
			document.getElementById('Consulta').style.display = 'inline';
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function TrocarSenha(parametros){
	document.getElementById('loading').style.visibility = 'visible';
	document.getElementById('Consulta').style.display = 'none';
    loadXMLDocTrocarSenha("inc/scr_trocar_senha.php",parametros);
}
/*******************************************/
//          BUSCA CLIENTES
/*******************************************/
function loadXMLDocBuscaClientes(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeBuscaClientes;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeBuscaClientes;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeBuscaClientes(){
	var retorno;
    if (req.readyState == 4) {
        if (req.status == 200)
			document.getElementById('clientes').innerHTML = req.responseText;
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function BuscaClientes(parametros){
    loadXMLDocBuscaClientes("inc/scr_busca_clientes.php",parametros);
}

/*******************************************/
//          MOSTRA DADOS CLIENTES
/*******************************************/
function loadXMLDocMostraDadosClientes(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeMostraDadosClientes;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeMostraDadosClientes;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeMostraDadosClientes(){
	var retorno = false;
    if (req.readyState == 4) {
        if (req.status == 200){					
				if(req.responseText == ""){
					document.getElementById('divClienteNaoEncontrado').innerHTML = " - Cliente não Encontrado!";
					document.getElementById('cnpjEscolhido').value = '';
					document.getElementById('cnpj').value = '';
					document.getElementById('divInfPedidos').style.display = 'inline';
					document.getElementById('divClienteDados').style.display = 'none';
				}			
				else
				if(document.getElementById('divClienteDados')){
					document.getElementById('divClienteDados').style.display = 'inline';
					document.getElementById('divClienteDados').innerHTML = req.responseText;
					document.getElementById('cnpj').value = '';				
					document.getElementById('divInfPedidos').style.display = 'none';
					document.getElementById('divClienteNaoEncontrado').innerHTML = "";
					document.getElementById('codigo').focus();
				}
				else{
					window.opener.document.getElementById('divClienteDados').style.display = 'inline';
					window.opener.document.getElementById('divClienteDados').innerHTML = req.responseText;
					window.opener.document.getElementById('cnpj').value = '';
					window.opener.document.getElementById('divInfPedidos').style.display = 'none';
					window.opener.document.getElementById('divClienteNaoEncontrado').innerHTML = "";
					window.opener.document.getElementById('codigo').focus();
					window.close();
				}	
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function MostraDadosClientes(parametros){
    /*if(window.opener && window.opener.document.getElementById('divClienteNaoEncontrado')){
		window.opener.document.getElementById('divClienteNaoEncontrado').innerHTML = 'Aguarde...';
	}else if(document.getElementById('divClienteNaoEncontrado')){
		document.getElementById('divClienteNaoEncontrado').innerHTML = 'Aguarde...';
	}*/
	if(document.getElementById('divClienteNaoEncontrado')){
		document.getElementById('divClienteNaoEncontrado').innerHTML = 'Aguarde...';
	}
    loadXMLDocMostraDadosClientes("inc/scr_mostra_dados_clientes.php",parametros);	
}

/*******************************************/
//          BUSCA PRODUTOS
/*******************************************/
function loadXMLDocBuscaProdutos(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeBuscaProdutos;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeBuscaProdutos;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeBuscaProdutos(){
	var retorno;
    if (req.readyState == 4) {
        if (req.status == 200)
			document.getElementById('produtos').innerHTML = req.responseText;
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function BuscaProdutos(parametros){
    loadXMLDocBuscaProdutos("inc/scr_busca_produtos.php",parametros);
}

/*******************************************/
//          MOSTRA PRODUTOS
/*******************************************/
function loadXMLDocMostraProdutos(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeMostraProdutos;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeMostraProdutos;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeMostraProdutos(){
	var retorno = false;
    if (req.readyState == 4) {
        if (req.status == 200){					
				if(req.responseText == 0){
					document.getElementById('divProdutoNaoEncontrado').style.display = 'inline';
					document.getElementById('codigo').value = '';
					document.getElementById('divProdutoNaoEncontrado').innerHTML = " - Produto não Encontrado!";
				}				
				else
				if(document.getElementById('divProdutoIncluir')){
					document.getElementById('divProdutoIncluir').innerHTML = req.responseText;
					document.getElementById('codigo').value = '';
					document.getElementById('divProdutoIncluir').style.display = 'inline';
					document.getElementById('divProdutoNaoEncontrado').innerHTML = "";
					if(document.getElementById('quantidade')){
						document.getElementById('quantidade').focus();
					}
				}
				else{
					window.opener.document.getElementById('divProdutoIncluir').innerHTML = req.responseText;
					window.opener.document.getElementById('codigo').value = '';
					window.opener.document.getElementById('divProdutoIncluir').style.display = 'inline';
					window.opener.document.getElementById('divProdutoNaoEncontrado').innerHTML = "";
					window.opener.document.getElementById('quantidade').focus();
					window.close();
				}	
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function MostraProdutos(parametros){
    /*if(window.opener && window.opener.document.getElementById('divProdutoNaoEncontrado')){
		window.opener.document.getElementById('divProdutoNaoEncontrado').innerHTML = 'Aguarde...';
	}else 
	*/
	if(document.getElementById('divProdutoNaoEncontrado')){
		document.getElementById('divProdutoNaoEncontrado').innerHTML = 'Aguarde...';
	}
    loadXMLDocMostraProdutos("inc/scr_mostra_produtos.php",parametros);
}

/*******************************************/
//          INCLUIR PRODUTOS
/*******************************************/
function loadXMLDocIncluirCesta(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeIncluirCesta;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeIncluirCesta;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeIncluirCesta(){
	var retorno = false;
    if (req.readyState == 4) {
        if (req.status == 200){							
				document.getElementById('divProdutoIncluir').style.display = 'none';
				document.getElementById('cnpj').value = ' ';
				document.getElementById('cnpj').disabled = true;
				document.getElementById('divCestaProdutos').innerHTML = req.responseText;
				document.getElementById('divProdutoNaoEncontrado').innerHTML = "";
				document.getElementById('codigo').focus();
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function IncluirCesta(parametros){
	if(document.getElementById('divProdutoNaoEncontrado')){
		document.getElementById('divProdutoNaoEncontrado').innerHTML = 'Aguarde...';
	}
    loadXMLDocIncluirCesta("inc/scr_incluir_produto.php",parametros);
}

/*******************************************/
//          EXCLUIR PRODUTOS
/*******************************************/
function loadXMLDocExcluirCesta(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeExcluirCesta;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeExcluirCesta;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeExcluirCesta(){
	var retorno = false;
    if (req.readyState == 4) {
        if (req.status == 200){					
			if(req.responseText == '0'){
				document.getElementById('cnpj').value = '';
				document.getElementById('cnpj').disabled = false;
				document.getElementById('divCestaProdutos').innerHTML = "";
				document.getElementById('divFinalizando').style.display = 'none';
			}else
				document.getElementById('divCestaProdutos').innerHTML = req.responseText;
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function ExcluirCesta(parametros){
    loadXMLDocExcluirCesta("inc/scr_excluir_produto.php",parametros);
}
/*******************************************/
//          ALTERA PRODUTOS
/*******************************************/
function loadXMLDocAlteraCesta(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeAlteraCesta;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeAlteraCesta;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeAlteraCesta(){
	var retorno = false;
    if (req.readyState == 4) {
        if (req.status == 200){					
				document.getElementById('divProdutoIncluir').style.display = 'inline';
				document.getElementById('divProdutoIncluir').innerHTML = req.responseText;
				ExcluirCesta(par);
				document.getElementById('quantidade').focus();
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function AlteraCesta(parametros){
    loadXMLDocAlteraCesta("inc/scr_mostra_alterar_produtos.php",parametros);
	par = parametros;
}
/*******************************************/
//          ALTERA QTDE PRODUTOS
/*******************************************/
function loadXMLDocAlteraQtdeItem(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeAlteraQtdeItem;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeAlteraQtdeItem;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeAlteraQtdeItem(){
	var retorno = false;
    if (req.readyState == 4) {
        if (req.status == 200){					
				document.getElementById('divProdutoIncluir').style.display = 'none';
				document.getElementById('divCestaProdutos').innerHTML = req.responseText;
				document.getElementById('divFinalizando').style.display = 'inline';
				document.getElementById('codigo').focus();
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function AlteraQtdeItem(parametros){
    loadXMLDocAlteraQtdeItem("inc/scr_alterar_produtos.php",parametros);
}

/*******************************************/
//          INSERIR PEDIDO
/*******************************************/
var alertaMsgPromocional = function() {
	    var msgPedidoPromo = '*** PEDIDO PROMOCIONAL: SIM ***';
		var msgPedidoNaoPromo = '*** PEDIDO PROMOCIONAL: NÃO ***';
		//$('body').append('<a href="#" id="alerta-padrao"></a>');
		$('#alerta-padrao').m2brDialog({
			tipo: 		'pergunta',
			titulo:		'Confirme',
			texto:		'Este pedido é promocional?',
			draggable: true,
			botoes: {
				1: {
					label: 'Sim',
					tipo : 'script',
					funcao : function() { 
								if(document.getElementById('obs').value != ''){
									msgPedidoPromo = ' - ' + msgPedidoPromo;
								}
								InserirPedido('paramCnpj='+document.getElementById('cnpjEscolhido').value+'&paramEmpresa='+document.getElementById('empresa').value+'&paramObs='+document.getElementById('obs').value+ msgPedidoPromo +'&paramCodPedCli='+document.getElementById('paramCodPedCli').value+'&paramDescontoAdicional='+document.getElementById('descontoAdicionalSel').value.replace(/\+/g,';'));document.getElementById('divGeraPedido').innerHTML='Aguarde...<br>Gerando Pedido...';
					} 
				},
				2: {
					label: 'Não',
					tipo : 'script',
					funcao : function() { 	
								if(document.getElementById('obs').value != ''){
									msgPedidoNaoPromo = ' - ' + msgPedidoNaoPromo;
								}					
								InserirPedido('paramCnpj='+document.getElementById('cnpjEscolhido').value+'&paramEmpresa='+document.getElementById('empresa').value+'&paramObs='+document.getElementById('obs').value + msgPedidoNaoPromo +'&paramCodPedCli='+document.getElementById('paramCodPedCli').value+'&paramDescontoAdicional='+document.getElementById('descontoAdicionalSel').value.replace(/\+/g,';'));document.getElementById('divGeraPedido').innerHTML='Aguarde...<br>Gerando Pedido...'; 
					} 
				}
			}								   
		});
		$('#alerta-padrao')
			.click()
			.remove();
};

function loadXMLDocInserirPedido(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeInserirPedido;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeInserirPedido;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeInserirPedido(){
	var retorno = false;
	var txt = '';
    if (req.readyState == 4) {
        if (req.status == 200){			
				//document.getElementById('divFinalizando').innerHTML = req.responseText;
				txt = req.responseText;
				if(txt.substring(0,1) == '<'){
					document.getElementById('divFinalizando').innerHTML = txt;
				}else{
					document.getElementById('divFinalizando').innerHTML = "<center><br><br>Pedido Incluído nº " + txt + " com Sucesso!<br><br>Aguarde...</center>";
					alert("Pedido nº " + txt + " incluído com sucesso!");
				}
				window.setTimeout('redirecinaInserirPedido()',2000);
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function InserirPedido(parametros){
    loadXMLDocInserirPedido("inc/scr_inserir_pedido.php",parametros);
}
/*******************************************/
//          INSERIR PEDIDO TMP
/*******************************************/
function loadXMLDocInserirPedidoTmp(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeInserirPedidoTmp;
        req.open("GET", url + '?' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeInserirPedidoTmp;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeInserirPedidoTmp(){
	var retorno = false;
	var txt = '';
    if (req.readyState == 4) {
        if (req.status == 200){			
				txt = req.responseText;
				alert(txt);
				document.getElementById('codPedidoTmp').value = txt;
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function InserirPedidoTmp(parametros){
    loadXMLDocInserirPedidoTmp("inc/scr_inserir_pedido_tmp.php",parametros);
}
/*******************************************/
//          CONSULTAR PEDIDOS
/*******************************************/
function loadXMLDocConsultaPedidos(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeConsultaPedidos;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeConsultaPedidos;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeConsultaPedidos(){
	var retorno;
    if (req.readyState == 4) {
        if (req.status == 200){
			document.getElementById('loading').style.visibility = 'hidden';
			document.getElementById('Consulta').innerHTML = req.responseText;
			document.getElementById('Consulta').style.display = 'inline';
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function ConsultaPedidos(parametros){
	document.getElementById('loading').style.visibility = 'visible';
	document.getElementById('Consulta').style.display = 'none';
    loadXMLDocNotaFiscal("inc/scr_consulta_pedidos.php",parametros);
}

/*******************************************/
//          MOSTRA PEDIDOS
/*******************************************/
function loadXMLDocMostraPedido(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeMostraPedido;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeMostraPedido;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeMostraPedido(){
	var retorno;
    if (req.readyState == 4) {
        if (req.status == 200){
			document.getElementById('loading').style.visibility = 'hidden';
			document.getElementById('Consulta').innerHTML = req.responseText;
			document.getElementById('Consulta').style.display = 'inline';
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function MostraPedido(parametros){
	document.getElementById('loading').style.visibility = 'visible';
	document.getElementById('Consulta').style.display = 'none';
	document.getElementById('divImprimir').style.display = 'inline';
	//document.getElementById('divImprimir').innerHTML = "<img style=cursor:hand onclick=\"javascript:Janela('geraPdf.php?" + parametros + "',300,300,'no');\" src='imgs/imprimir.jpg' width='92' height='25'>";
	document.getElementById('titulo').innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VISUALIZAR PEDIDO';
    loadXMLDocMostraPedido("inc/scr_mostra_pedido.php",parametros);
}

/*******************************************/
//          ALTERA PEDIDOS
/*******************************************/
function loadXMLDocAlteraPedido(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeAlteraPedido;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeAlteraPedido;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeAlteraPedido(){
	var retorno;
    if (req.readyState == 4) {
        if (req.status == 200){
			document.getElementById('loading').style.visibility = 'hidden';
			document.getElementById('Consulta').innerHTML = req.responseText;
			document.getElementById('Consulta').style.display = 'inline';
			document.getElementById('codigo').focus();
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function AlteraPedido(parametros){
	document.getElementById('loading').style.visibility = 'visible';
	document.getElementById('Consulta').style.display = 'none';
	document.getElementById('divImprimir').style.display = 'none';
	document.getElementById('titulo').innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ALTERAR PEDIDO';
    loadXMLDocAlteraPedido("inc/scr_altera_pedido.php",parametros);
}

/*******************************************/
//         CONFIRMA ALTERAR PEDIDOS
/*******************************************/
function loadXMLDocConfAlteraPedido(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeConfAlteraPedido;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeConfAlteraPedido;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeConfAlteraPedido(){
	var retorno;
    if (req.readyState == 4) {
        if (req.status == 200){
			document.getElementById('loading').style.visibility = 'hidden';
			document.getElementById('divFinalizando').innerHTML = req.responseText;
			window.setTimeout('redirecinaAlterarPedido()',2000);
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function ConfAlteraPedido(parametros){
	document.getElementById('loading').style.visibility = 'visible';
	document.getElementById('divFinalizando').innerHTML = 'Aguarde ...';
    loadXMLDocConfAlteraPedido("inc/scr_conf_altera_pedido.php",parametros);
	
	//location="inc/scr_conf_altera_pedido.php?"+parametros;
}
/*******************************************/
//          CONSULTAR SALDO PEDIDOS
/*******************************************/
function loadXMLDocConsultaSaldoPedidos(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeConsultaSaldoPedidos;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeConsultaSaldoPedidos;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeConsultaSaldoPedidos(){
	var retorno;
    if (req.readyState == 4) {
        if (req.status == 200){
			document.getElementById('loading').style.visibility = 'hidden';
			document.getElementById('Consulta').innerHTML = req.responseText;
			document.getElementById('Consulta').style.display = 'inline';
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function ConsultaSaldoPedidos(parametros){
	document.getElementById('loading').style.visibility = 'visible';
	document.getElementById('Consulta').style.display = 'none';
    loadXMLDocConsultaSaldoPedidos("inc/scr_consulta_saldo_pedidos.php",parametros);
}

/*******************************************/
//          MOSTRA SALDO PEDIDOS
/*******************************************/
function loadXMLDocMostraSaldoPedido(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeMostraSaldoPedido;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeMostraSaldoPedido;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeMostraSaldoPedido(){
	var retorno;
    if (req.readyState == 4) {
        if (req.status == 200){
			document.getElementById('loading').style.visibility = 'hidden';
			document.getElementById('Consulta').innerHTML = req.responseText;
			document.getElementById('Consulta').style.display = 'inline';
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function MostraSaldoPedido(parametros){
	document.getElementById('loading').style.visibility = 'visible';
	document.getElementById('Consulta').style.display = 'none';
	document.getElementById('titulo').innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VISUALIZAR SALDO PEDIDO';
    loadXMLDocMostraSaldoPedido("inc/scr_mostra_saldo_pedido.php",parametros);
}

/*******************************************/
//                 MOSTRA CESTA
/*******************************************/
function loadXMLDocMostraCesta(url, parametros){
    req = null;
    if (window.XMLHttpRequest) {		
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeMostraCesta;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeMostraCesta;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeMostraCesta(){
	var retorno;
    if (req.readyState == 4) {
        if (req.status == 200){
			document.getElementById('loading').style.visibility = 'hidden';
			document.getElementById('divCestaProdutos').innerHTML = req.responseText;
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function MostraCesta(parametros){
	loadXMLDocMostraCesta("inc/scr_mostra_cesta.php",parametros);
}

/*******************************************/
//    CONFIRMA ALTERAR STATUS PRODUTOS
/*******************************************/
function loadXMLDocConfAlteraProduto(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeConfAlteraProduto;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeConfAlteraProduto;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeConfAlteraProduto(){
	var retorno;
    if (req.readyState == 4) {
        if (req.status == 200){
			//document.getElementById('loading').style.visibility = 'hidden';
			document.getElementById('divProdStatus').innerHTML = req.responseText;
			//window.setTimeout('redirecinaAlterarPedido()',2000);
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }
}

function ConfAlteraProduto(parametros){
	//document.getElementById('loading').style.visibility = 'visible';
	document.getElementById('divProdStatus').innerHTML = '<center><span style="color:#CC0000; font-weight:bold;">Aguarde ...</span></center>';
    loadXMLDocConfAlteraProduto("php/scr_altera_produto.php",parametros);
}

/*******************************************/
//    LOG PROMOCAO
/*******************************************/
function loadXMLDocLogPromocao(url,parametros){
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChangeLogPromocao;
        req.open("GET", url + '?no-cache=' + Math.ceil (Math.random() * 100000) + '&' + parametros, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChangeLogPromocao;
        	req.open("GET", url + '?' + parametros, true);
            req.send();
        }
    }
}

function processReqChangeLogPromocao(){
	var retorno;
    /*if (req.readyState == 4) {
        if (req.status == 200){
			//document.getElementById('loading').style.visibility = 'hidden';
			//document.getElementById('divProdStatus').innerHTML = req.responseText;
			//window.setTimeout('redirecinaAlterarPedido()',2000);
		}
        else
            alert("[ERRO!]\n" + req.statusText);
    }*/
}

function LogPromocao(parametros){
    loadXMLDocLogPromocao("inc/scr_log_promocao.php",parametros);
}

/*******************************************/
//          CARREGANDO
/*******************************************/
function loading(disp) { 
    document.getElementById('loading').style.display = disp; 
}

function redirecinaInserirPedido(){
	window.location='index_.php?id=5';
}
function redirecinaAlterarPedido(){
	window.location='index_.php?id=6';
}


