function CalculaPedido() {
	var qtd = 0;
	var preco = 0;
	var pedido = 0;
	var valor_unit = 0;
	var retorno = 0;
	var campos = $N("txt_qtd");

	for (i = 0; i < campos.length; i++) {
		
		qtd = $N("hdn_quantidade")[i].value;
		preco = $N("hdn_preco")[i].value;
		pedido = campos[i].value;
		valor_unit = ((preco * pedido) / qtd); // calcula o valor unitário do produto
		$N("hdn_valor")[i].value = valor_unit; // armazena o valor unitário do produto
		retorno += valor_unit;		
	}
	$("txt_sub").value = "R$ " + (retorno.toFixed(2));
    retorno += 3; // Acrescimo de R$ 3,00 (Frete)
    $("txt_total").value = "R$ " + (retorno.toFixed(2));
}

function ValidaQtd() {
	var campos = $N("txt_qtd");
	for (i = 0; i < campos.length; i++) {
		if (parseFloat(campos[i].value) < parseFloat($N("hdn_quantidade")[i].value)) {			
			alert("Quantidade não pode ser menor que " + $N("hdn_quantidade")[i].value);
			campos[i].focus();			
			return false;
		}
	}
}


// FUNÇÃO CONFIRMA PEDIDO //

function ConfirmaPedido(id,formtipo) {
	
	var formtipo=formtipo; // Tipo do Formulário
	var informou = false;
	var campos = $N("txt_qtd"); // OBS: $N
	
	var abre = true;	
	
	if ($(id).style.display == 'block') {
		abre = false;
	}
	if (abre) {
	///////////////////////////////////////////////////////
	if (formtipo==1) {                                     // FORMULARIO SIM ////////////////////////

        if ( $("txt_tipoPessoa").value == "" ) {
            alert("Informe o tipo de pessoa (Física ou Jurídica).");
            $("txt_tipoPessoa").focus();
            return;
        }
        if ( $("txt_cpf").value == "" ) {
            alert("Informe o CPF/CNPJ.");
            $("txt_cpf").focus();
            return;
        }        
        	
		if ( $("txt_nome").value == "" ) {
			alert("Informe o seu nome.");
			$("txt_nome").focus();
			return;
		}
		if ( $("txt_email").value == "" ) {
			alert("Informe o e-mail para contato.");
			$("txt_email").focus();
			return;
		}
		if ( $("txt_fone").value == "" ) {
			alert("Informe o telefone para contato.");
			$("txt_fone").focus();
			return;
		}
		var params;
			params = "nome=" + escape($("txt_nome").value);
            params += "&txt_tipoPessoa=" + escape($("txt_tipoPessoa").value);
            params += "&txt_cpf=" + escape($("txt_cpf").value);
			params += "&tipoform=" + escape($("tipoform").value);
			params += "&email=" + escape($("txt_email").value);
			params += "&fone=" + escape($("txt_fone").value);
			params += "&cel=" + escape($("txt_cel").value);
			params += "&obs=" + escape($("txt_obs").value);
			params += "&total=" + escape($("txt_total").value);
			
		for (i = 0; i < campos.length; i++) {
			if (campos[i].value != "") {
				informou = true;
				params += "&produto[]=" + escape($N("hdn_nome")[i].value);
                params += "&id_item[]=" + escape($N("hdn_id_item")[i].value);
                params += "&hdn_quantidade[]=" + escape($N("hdn_quantidade")[i].value);
				params += "&pedido[]=" + escape(campos[i].value);  
			}
		}
			
		if (!informou) {
			alert("Informe o(s) item(ns) do seu pedido.");
			return;
		}
	}
	///////////////////////////////////////////////////////
	else {                                                 // FORMULARIO NÃO ////////////////////////

        if ( $("txt_tipoPessoa").value == "" ) {
            alert("Informe o tipo de pessoa (Física ou Jurídica).");
            $("txt_tipoPessoa").focus();
            return;
        }
        if ( $("txt_cpf").value == "" ) {
            alert("Informe o CPF/CNPJ.");
            $("txt_cpf").focus();
            return;
        }           
		
		if ( $("txt_nome").value == "" ) {
			alert("Informe o seu nome.");
			$("txt_nome").focus();
			return;
		}
		if ( $("txt_email").value == "" ) {
			alert("Informe o e-mail para contato.");
			$("txt_email").focus();
			return;
		}
		if ( $("txt_fone").value == "" ) {
			alert("Informe o telefone para contato.");
			$("txt_fone").focus();
			return;
		}
		if ( $("txt_endereco").value == "" ) {
			alert("Informe o endereço para entrega.");
			$("txt_endereco").focus();
			return;
		}
		if ( $("txt_bairro").value == "" ) {
			alert("Informe o endereço para entrega.");
			$("txt_bairro").focus();
			return;
		}
		if ( $("txt_cidade").value == "" ) {
			alert("Informe a sua Cidade.");
			$("txt_cidade").focus();
			return;
		}
		if ( $("txt_estado").value == "" ) {
			alert("Informe o seu Estado.");
			$("txt_estado").focus();
			return;
		}
		var params;
			params = "nome=" + escape($("txt_nome").value);
            params += "&txt_tipoPessoa=" + escape($("txt_tipoPessoa").value);
            params += "&txt_cpf=" + escape($("txt_cpf").value);
			params += "&tipoform=" + escape($("tipoform").value);
			params += "&email=" + escape($("txt_email").value);
			params += "&fone=" + escape($("txt_fone").value);
			params += "&cel=" + escape($("txt_cel").value);
			params += "&endereco=" + escape($("txt_endereco").value);
			params += "&bairro=" + escape($("txt_bairro").value);
			params += "&cidade=" + escape($("txt_cidade").value);
			params += "&estado=" + escape($("txt_estado").value);
			params += "&referencia=" + escape($("txt_referencia").value);
			params += "&cep=" + escape($("txt_cep").value);
			params += "&conheceu=" + escape($("txt_conheceu").value);
			params += "&obs=" + escape($("txt_obs").value);
			params += "&total=" + escape($("txt_total").value);
				
		for (i = 0; i < campos.length; i++) {
			if (campos[i].value != "") {
				informou = true;
				params += "&produto[]=" + escape($N("hdn_nome")[i].value);
                params += "&id_item[]=" + escape($N("hdn_id_item")[i].value);
                params += "&hdn_quantidade[]=" + escape($N("hdn_quantidade")[i].value);                
				params += "&pedido[]=" + escape(campos[i].value);
			}
		}
			
		if (!informou) {
			alert("Informe o(s) item(ns) do seu pedido.");
			return;
		}
	}
	/////////////////////////////////////////////////////////////////////////////////////////////////
		ajaxIncludeDiv('ped_valida.php?' + params,id);
		$(id).style.display = 'block';
		$("bt_enviar").disabled = false;
	}
}


////////////////////////////  ENVIA PEDIDO  ////////////////////////////

function EnviaPedido(formtipo) {
	
	var formtipo=formtipo;
	var informou = false;
	var campos = $N("txt_qtd"); // OBS: $N	
	
	if (formtipo==1) {
		
		var params;
		
		params = "nome=" + escape($("txt_nome").value);
        params += "&txt_tipoPessoa=" + escape($("txt_tipoPessoa").value);
        params += "&txt_cpf=" + escape($("txt_cpf").value);
		params += "&tipoform=" + escape($("tipoform").value);
		params += "&url=" + escape($("url").value);
		params += "&email=" + escape($("txt_email").value);
		params += "&fone=" + escape($("txt_fone").value);
		params += "&cel=" + escape($("txt_cel").value);
		params += "&obs=" + escape($("txt_obs").value);
		params += "&total=" + escape($("txt_total").value);
		
	}
	else {
	
		var params;
		params = "nome=" + escape($("txt_nome").value);
        params += "&txt_tipoPessoa=" + escape($("txt_tipoPessoa").value);
        params += "&txt_cpf=" + escape($("txt_cpf").value);
		params += "&tipoform=" + escape($("tipoform").value);
		params += "&url=" + escape($("url").value);
		params += "&email=" + escape($("txt_email").value);
		params += "&fone=" + escape($("txt_fone").value);
		params += "&cel=" + escape($("txt_cel").value);
		params += "&endereco=" + escape($("txt_endereco").value);
		params += "&bairro=" + escape($("txt_bairro").value);
		params += "&cidade=" + escape($("txt_cidade").value);
		params += "&estado=" + escape($("txt_estado").value);
		params += "&referencia=" + escape($("txt_referencia").value);
		params += "&cep=" + escape($("txt_cep").value);
		params += "&conheceu=" + escape($("txt_conheceu").value);
		params += "&obs=" + escape($("txt_obs").value);
		params += "&total=" + escape($("txt_total").value);
	
	}


	for (i = 0; i < campos.length; i++) {
		if (campos[i].value != "") {
			informou = true;
			params += "&produto[]=" + escape($N("hdn_nome")[i].value);
			params += "&pedido[]=" + escape(campos[i].value);
            params += "&id_item[]=" + escape($N("hdn_id_item")[i].value);
            params += "&hdn_quantidade[]=" + escape($N("hdn_quantidade")[i].value);
		}
	}	

    for (i = 0; i < $N("ra_endereco").length; i++)
    {
        if ($N("ra_endereco")[i].checked)
        {
            params += "&ra_endereco=" + escape($N("ra_endereco")[i].value);
        }
    }

	
	if (!confirm("Confirma o envio do pedido?")) {
		$('divConfirma').style.display = 'none';
		return;
	}

	$("bt_EnviaPedido").disabled = true;
	$("div_enviando").style.display = "block";
	var ajax = new AJAX();
	ajax.url = "send_pedido.php";
	ajax.metodo = "POST";
	ajax.params = params;
	ajax.retorno = function(texto) {	
		//alert(texto);
        //return;
		//$("frm_pedido").reset();	
		$('div_alert').style.display = 'block';
		$('divConfirma').style.display = 'none';
		$("div_enviando").style.display = "none";
		$("bt_EnviaPedido").disabled = false;
		//$('div_alert').innerHTML = texto;		
		//setTimeout(FechaMensagem,5000);	// segundos
	}
	ajax.conectar();	
}
////////////////////////////  FIM ENVIA PEDIDO  ////////////////////////////

function ImprimiPedido(formtipo) {
	
	var formtipo=formtipo;
	var informou = false;
	var campos = $N("txt_qtd");	
	var params;
	
	params = "ped_print.php?";
	
	if (formtipo==1) {
	
		params += "nome=" + escape($("txt_nome").value);
		params += "&tipoform=" + escape($("tipoform").value);
		params += "&email=" + escape($("txt_email").value);
		params += "&fone=" + escape($("txt_fone").value);
		params += "&cel=" + escape($("txt_cel").value);
		params += "&obs=" + escape($("txt_obs").value);
		params += "&total=" + escape($("txt_total").value);		
	}
	else {
	
		params += "nome=" + escape($("txt_nome").value);
		params += "&tipoform=" + escape($("tipoform").value);
		params += "&email=" + escape($("txt_email").value);
		params += "&fone=" + escape($("txt_fone").value);
		params += "&cel=" + escape($("txt_cel").value);
		params += "&endereco=" + escape($("txt_endereco").value);
		params += "&bairro=" + escape($("txt_bairro").value);
		params += "&cidade=" + escape($("txt_cidade").value);
		params += "&estado=" + escape($("txt_estado").value);
		params += "&referencia=" + escape($("txt_referencia").value);
		params += "&cep=" + escape($("txt_cep").value);
		params += "&conheceu=" + escape($("txt_conheceu").value);
		params += "&obs=" + escape($("txt_obs").value);
		params += "&total=" + escape($("txt_total").value);
	
	}
	
	for (i = 0; i < campos.length; i++) {
		if (campos[i].value != "") {
			informou = true;
			params += "&produto[]=" + escape($N("hdn_nome")[i].value);
			params += "&pedido[]=" + escape(campos[i].value);
		}
	}
	window.open(params,'ImprimiPedido','toolbar=0,location=1,status=0,menubar=0,scrollbars=0,resizable=0,width=500,height=500');
	$("frm_pedido").reset();	
	$('div_alert').style.display = 'none';
}

///////////////////////////////////////////////////////////////////////////////////////////////////



// Função para desaparecer a DIV ALERT /\
function FechaMensagem() {
	$('div_alert').style.display = 'none';
	//new Effect.Fade('div_alert');
	$("frm_pedido").reset();
}
function FechaPedido() {
	$('divConfirma').style.display = 'none';	
	//new Effect.Fade('divConfirma');
}

function $(id_obj) {
	return document.getElementById(id_obj);
}

function $N(name_obj) {
	return document.getElementsByName(name_obj);
}

// Permite apenas digitar numeros
function Numero(evt) {
	var key_code = evt.keyCode ? evt.keyCode : evt.charCode ? evt.charCode : evt.which ? evt.which : void 0;

	// Backspace, Tab, Del
	if (key_code == 8 || key_code == 9 || key_code == 46) {
		return true;
	}	
	
	if (key_code > 47 && key_code < 58) { // numeros de 0 a 9
		return true;
	} else {
		if (key_code != 8) { // backspace
			return false;
		} else {
			return true;
		}
	}
}

function FechaMensagem() {
	$('div_alert').style.display = 'none';
}

function abreDuvida(id) {
	var qtd = 2;
	var abre = true;
	if (document.getElementById(id).style.display == 'block') {
		abre = false;
	}
	for (var i = 1; i <= qtd; i++) {
		document.getElementById('div'+i).style.display = 'none';
	}
	if (abre) {
		document.getElementById(id).style.display = 'block';
	}
}

function abreDiv(id) {
	var qtd = 34;
	var abre = true;
	if (document.getElementById(id).style.display == 'block') {
		abre = false;
	}
	for (var i = 1; i <= qtd; i++) {
		document.getElementById('Abre'+i).style.display = 'none';
	}
	if (abre) {
		document.getElementById(id).style.display = 'block';
	}
}

function DivEscolha(id) {
	var qtd = 2;
	var abre = true;
	if (document.getElementById(id).style.display == 'block') {
		abre = false;
	}
	for (var i = 1; i <= qtd; i++) {
		document.getElementById('Escolha'+i).style.display = 'none';
	}
	if (abre) {
		document.getElementById(id).style.display = 'block';
	}
}

function DigitaCEP(campo) {
    var CEP = new String(campo.value);
    var wCEP = '';
	var cont = 0;

    for (i=0; i< CEP.length ; i++) {

	  if (i <= 4) {
		  if ( CEP.charAt(i) >= '0' && CEP.charAt(i) <= '9' )	{
			  	wCEP += CEP.charAt(i);
	  	  }
		  else
		  {
		  		cont++;
		  }
	  }

	  if (i == 5) {
		  if ( CEP.charAt(i) == '-' )	{
			  	wCEP += CEP.charAt(i);
	  	  }
		  else {
			  if ( CEP.charAt(i) >= '0' && CEP.charAt(i) <= '9' )	{
			  		wCEP += '-';
				  	wCEP += CEP.charAt(i);
					cont ++;
			  }
			  else {
			  		wCEP += '-';
					cont ++;
			  }
	  	  }
	  }

	  if (i > 5) {
		  if ( CEP.charAt(i) >= '0' && CEP.charAt(i) <= '9' )	{
			  	wCEP += CEP.charAt(i);
	  	  }
		  else
		  {
		  		cont++;
		  }
	  }

    }

	if ( cont > 0 )
	{
	    // Atualiza o campo
    	campo.value = wCEP;
	}
}

function ExibePagina(pagina) {
	// limpa a tela
	$("insert").innerHTML = "";
	// div aguarde
	$("div_carregando").style.display = "block";	
	// inclua a página
	ajaxIncludeDiv(pagina,"insert");
}

function ValidaDigito(valor, cpf)
{  

    if(valor==0)
    {

        document.getElementById("cpfcnpj").innerHTML="<input type='text' class='input-pedidos'   onkeyup='DigitaCPF(this)' id='txt_cpf' name='txt_cpf' maxlength='14'  size='20'/> <input class='input-buttom' onclick='ValidaCliente()' type='button' value='Buscar'/>";
    }
    else
    {
          document.getElementById("cpfcnpj").innerHTML="<input type='text' class='input-pedidos' onkeyup='DigitaCNPJ(this)' id='txt_cpf' name='txt_cpf' maxlength='19'  size='20'/> <input class='input-buttom' onclick='ValidaCliente()' type='button' value='Buscar'/>";
    }

}

function ValidaCliente(valor)
{
	
//	ajaxIncludeDiv('div_carrega_cliente.php',"carregacliente");

    var params = '';

    if($("txt_cpf").value=="")
    {
        alert("preencha o cpf/cpnj")
        $("txt_cpf").focus;
        return
    }
    params = "txt_cpf=" + escape($("txt_cpf").value);
    //alert($("txt_cpf"))
    //params += "&referencia=" + escape($("txt_referencia").value);

            // limpa a tela
        $("carregacliente").innerHTML = "";
        // div aguarde
        $("div_carregando").style.display = "block";    
        // inclua a página

    var ajax = new AJAX();
    ajax.url = "div_carrega_cliente.php";
    ajax.metodo = "POST";
    ajax.params = params;
    ajax.retorno = function(texto) {    
        $("div_carregando").style.display = "none";    
        // inclua a página
        document.getElementById('carregacliente').innerHTML = texto;

    }
    ajax.conectar();    

}