// JavaScript Document
function mostraCarrinho(pri) {
	var textview = document.getElementById(pri);
		if (textview.style.display=='none') {
			textview.style.display='';
			} else {
			textview.style.display='none';
	}
}
/***************************************************************************************************************************/
/* Funções da Página: carrinho.asp                                                                                         */
/***************************************************************************************************************************/
function removerProduto(posicao)
{
	if (window.confirm('Você realmente deseja remover o produto do carrinho?'))
	{
		window.location.href = 'produtos_orcamento.asp?acao=remover&posicao='+ posicao +'';
	}
}

function atualizar()
{
	document.formAtualizarCart.submit();
}

function limparCarrinho()
{
	if (window.confirm('Você realmente deseja excluir todos os produtos do seu carrinho de compras?'))
	{
		window.location.href = 'produtos_orcamento.asp?acao=limpar';
	}
}

//###################################################################################
// pessoa física / jurídica
//###################################################################################
function tipoPessoa(tipo)
{
	if (tipo == "Física")
	{
		document.getElementById("tp_CPF").style.display  = "";
		document.getElementById("tp_RG").style.display   = "";
		document.getElementById("tp_CNPJ").style.display = "none";
		document.getElementById("tp_IE").style.display   = "none";
}
	else if (tipo == "Jurídica")
	{
		document.getElementById("tp_CPF").style.display  = "none";
		document.getElementById("tp_RG").style.display   = "none";
		document.getElementById("tp_CNPJ").style.display = "";
		document.getElementById("tp_IE").style.display   = "";
	}
}
//###################################################################################
// mascarar entradas
//###################################################################################
function mascaraEntrada(objeto, sMask, evtKeyPress) // onkeypress="javascript:return mascaraEntrada(this,'99999-999',event);"
{
	var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

	if (document.all)         { nTecla = evtKeyPress.keyCode; }
	else if (document.layers) { nTecla = evtKeyPress.which; }
	else                      { nTecla = evtKeyPress.which; if (nTecla == 8) { return true; } }

	sValue = objeto.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( ")", "" );
	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) == "/") || (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++;
	}

	objeto.value = sCod;

	if (nTecla != 8)
	{
		if (sMask.charAt(i-1) == "9")
		{
			return ((nTecla > 47) && (nTecla < 58));
		}
		else { return true; }
	}
	else { return true; }
}

function isNumber(caracter) // onkeypress="javascript:return isNumber(event);"
{
	var tecla = (window.Event) ? caracter.which : caracter.keyCode;
	if (tecla > 47 && tecla < 58)
	{
		return true;
	}
	else
	{
		if (tecla != 8 && tecla != 13 && tecla != 0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}

//***********************************************************************************************
// validar email
//***********************************************************************************************
 function validaEmail(email)
{
  var emailInvalido = false
  var stringTemp = new String(email)
  var index = stringTemp.indexOf("@");
  if (index > 0)
  {
    var pindex = stringTemp.indexOf(".",index);
    if ((pindex > index+1) && (stringTemp.length > pindex+1))
	emailInvalido = true;
  }
  return emailInvalido;
}

//***********************************************************************************************
// validar CNPJ
//***********************************************************************************************
function valida_CNPJ(s)
{
	var i;
	s = limpa_string(s);
	var c = s.substr(0,12);
	var dv = s.substr(12,2);
	var d1 = 0;
	for (i = 0; i < 12; i++)
	{
		d1 += c.charAt(11-i)*(2+(i % 8));
	}
        if (d1 == 0) return false;
        d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(0) != d1)
	{
		return false;
	}

	d1 *= 2;
	for (i = 0; i < 12; i++)
	{
		d1 += c.charAt(11-i)*(2+((i+1) % 8));
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(1) != d1)
	{
		return false;
	}
        return true;
}
//***********************************************************************************************
// validar cpf
//***********************************************************************************************
function validarCPF(cpf)
{
	var soma;
	var valor;
	var digito_verificador;

	var aux;
	var validos = '0123456789'
	var numero = '';
	for (var i = 0; i < cpf.length; i++)
	{
		aux = validos.indexOf(cpf.substring(i, i+1));
		if (aux >= 0) { numero += aux }
	}

	if(numero.length != 11)
	{
		return false;
	}

	// Cálculo do Primeiro dígito do CPF
	soma = 0;
	for (var i = 0; i < 9; i++)
	{
		soma += (10-i) * parseInt(numero.charAt(i));
	}
	digito_verificador = 11 - (soma % 11);
	if ((soma % 11) < 2) digito_verificador = 0;

	if (parseInt(numero.charAt(9)) != digito_verificador)
	{
		return false;
	}

	// Cálculo do Segundo dígito do CPF
	soma = 0;
	for (var i = 0; i < 10; i++)
	{
		soma += (11-i) * parseInt(numero.charAt(i));
	}
	digito_verificador = 11 - (soma % 11);
	if ((soma % 11) < 2) digito_verificador = 0;

	if (parseInt(numero.charAt(10)) != digito_verificador)
	{
		return false;
	}

	return true;
}
//***********************************************************************************************
// limpar string deixando só números
//***********************************************************************************************
function limpa_string(S){ 
// Deixa so' os digitos no numero 
var Digitos = "0123456789"; 
var temp = ""; 
var digito = ""; 

for (var i=0; i<S.length; i++) { 
digito = S.charAt(i); 
if (Digitos.indexOf(digito)>=0) { 
temp=temp+digito } 
} //for 

return temp 
} 
//###################################################################################
// valida formulário de orcamento
//###################################################################################
function validarOrcamento(f)
{
	
	if (f.cmpEmpresa.value == '')
	{
		window.alert('Por favor, digite seu nome completo/Razão Social');
		f.cmpEmpresa.className = "textreq";
		f.cmpEmpresa.focus();
		return false;
	}
	if ((f.tipo[0].checked==false) && (f.tipo[1].checked==false)) {
	    window.alert("Por favor, selecione o tipo de cliente.");
		return false;
	}	
	var cpf1 = limpa_string(f.cpf.value);
	if ((f.tipo[0].checked) && (cpf1.length !== 11))
	{
		window.alert("Por favor, informe o CPF.");
		f.cpf.className = "textreq";
		f.cpf.focus();
		return false;
	}
	
	if ((f.tipo[0].checked) && (cpf1.length == 11) && (validarCPF(cpf1) == false))
	{
		window.alert("Por favor, informe o CPF v\xE1lido.");
		f.cpf.className = "textreq";
		f.cpf.focus();
		return false;
	}
	
	if ((f.tipo[0].checked) && (f.rg.value == ''))
	{
		window.alert("Por favor, digite o RG corretamente.");
		f.rg.className = "textreq";
		f.rg.focus();
		return false;
	}
	
		var cnpj1 = limpa_string(f.cnpj.value);
	if ((f.tipo[1].checked) && (cnpj1.length !== 14))
	{
		window.alert("Por favor, informe o CNPJ da empresa.");
		f.cnpj.className = "textreq";
		f.cnpj.focus();
		return false;
	}
	
	if ((f.tipo[1].checked) && (cnpj1.length == 14) && (valida_CNPJ(cnpj1) == false))
	{
		window.alert("Por favor, informe um CNPJ v\xE1lido.");
		f.cnpj.className = "textreq";
		f.cnpj.focus();
		return false;
	}
	if ((f.tipo[1].checked) && (f.ie.value == ''))
	{
		window.alert("Por favor, digite a Inscri\xE7\xE3o Estadual corretamente.");
		f.ie.className = "textreq";
		f.ie.focus();
		return false;
	}
	if (f.cmpEmail.value.length==0){
        window.alert('Favor preencher Email.');
		f.cmpEmail.className = "textreq";
        f.cmpEmail.focus();
		return false;
	}

   if (!validaEmail(f.cmpEmail.value)){
       window.alert('Favor preencher Email corretamente');
	   f.cmpEmail.className = "textreq";
       f.cmpEmail.focus();
		return false;
	}
	if (f.cmpTelefone.value == '')
	{
		window.alert('Por favor, digite seu Telefone.');
		f.cmpTelefone.className = "textreq";
		f.cmpTelefone.focus();
		return false;
	}
	if (f.cmpCep2.value.length < 9)
	{
		window.alert('Por favor, digite seu CEP corretamente.');
		f.cmpCep2.className = "textreq";
		f.cmpCep2.focus();
		return false;
	}
	if (f.cmpEndereco.value == '')
	{
		window.alert('Por favor, digite seu Endere\xE7o.');
		f.cmpEndereco.className = "textreq";
		f.cmpEndereco.focus();
		return false;
	}
	if (f.cmpBairro.value == '')
	{
		window.alert('Por favor, digite seu Bairro.');
		f.cmpBairro.className = "textreq";
		f.cmpBairro.focus();
		return false;
	}
	if (f.cmpCidade.value == '')
	{
		window.alert('Por favor, digite sua Cidade');
		f.cmpCidade.className = "textreq";
		f.cmpCidade.focus();
		return false;
	}
	if (f.cmpEstado.value == '')
	{
		window.alert('Por favor, selecione seu Estado');
		f.cmpEstado.className = "textreq";
		f.cmpEstado.focus();
		return false;
	}
}
//###################################################################################
//Validar busca produtos
//###################################################################################
function valida_busca() {

	Form = document.busca;
	
    if (Form.produto.value.length == 0) {
		alert('Digite uma palavra para busca');
		Form.produto.className = "textreq";
		Form.produto.focus();
		return false;
	}

    if (Form.produto.value.length < 3) {
		alert('A palavra deve ter mais que 3 caracteres');
		Form.produto.className = "textreq";
		Form.produto.focus();
		return false;
	}
}
//###################################################################################
// valida formulário de orcamento
//###################################################################################
function validarFaleconosco(f)
{
	
	if (f.cmpEmpresa.value == '')
	{
		window.alert('Por favor, digite seu nome completo');
		f.cmpEmpresa.className = "textreq";
		f.cmpEmpresa.focus();
		return false;
	}
	if (f.cmpEmail.value.length==0){
        window.alert('Favor preencher Email.');
		f.cmpEmail.className = "textreq";
        f.cmpEmail.focus();
		return false;
	}

   if (!validaEmail(f.cmpEmail.value)){
       window.alert('Favor preencher Email corretamente');
	   f.cmpEmail.className = "textreq";
       f.cmpEmail.focus();
		return false;
	}
	if (f.cmpTelefone.value == '')
	{
		window.alert('Por favor, digite seu Telefone.');
		f.cmpTelefone.className = "textreq";
		f.cmpTelefone.focus();
		return false;
	}
		if (f.cmpEndereco.value == '')
	{
		window.alert('Por favor, digite seu Endere\xE7o.');
		f.cmpEndereco.className = "textreq";
		f.cmpEndereco.focus();
		return false;
	}
	if (f.cmpBairro.value == '')
	{
		window.alert('Por favor, digite seu Bairro.');
		f.cmpBairro.className = "textreq";
		f.cmpBairro.focus();
		return false;
	}
	if (f.cmpCidade.value == '')
	{
		window.alert('Por favor, digite sua Cidade');
		f.cmpCidade.className = "textreq";
		f.cmpCidade.focus();
		return false;
	}
	if (f.cmpEstado.value == '')
	{
		window.alert('Por favor, selecione seu Estado');
		f.cmpEstado.className = "textreq";
		f.cmpEstado.focus();
		return false;
	}
	if (f.cmpCep2.value.length < 9)
	{
		window.alert('Por favor, digite seu CEP corretamente.');
		f.cmpCep2.className = "textreq";
		f.cmpCep2.focus();
		return false;
	}
	if (f.informacoes.value == '')
	{
		window.alert('Por favor, digite as informações desejadas');
		f.informacoes.className = "textreq";
		f.informacoes.focus();
		return false;
	}
}
//###################################################################################
// valida formulário de orcamento
//###################################################################################
function validarCadastro(f)
{
	
	if (f.cmpEmpresa.value == '')
	{
		window.alert('Por favor, digite seu nome completo');
		f.cmpEmpresa.className = "textreq";
		f.cmpEmpresa.focus();
		return false;
	}
	if (f.cmpEmail.value.length==0){
        window.alert('Favor preencher Email.');
		f.cmpEmail.className = "textreq";
        f.cmpEmail.focus();
		return false;
	}

   if (!validaEmail(f.cmpEmail.value)){
       window.alert('Favor preencher Email corretamente');
	   f.cmpEmail.className = "textreq";
       f.cmpEmail.focus();
		return false;
	}
	if (f.cmpTelefone.value == '')
	{
		window.alert('Por favor, digite seu Telefone.');
		f.cmpTelefone.className = "textreq";
		f.cmpTelefone.focus();
		return false;
	}
		if (f.cmpEndereco.value == '')
	{
		window.alert('Por favor, digite seu Endere\xE7o.');
		f.cmpEndereco.className = "textreq";
		f.cmpEndereco.focus();
		return false;
	}
	if (f.cmpBairro.value == '')
	{
		window.alert('Por favor, digite seu Bairro.');
		f.cmpBairro.className = "textreq";
		f.cmpBairro.focus();
		return false;
	}
	if (f.cmpCidade.value == '')
	{
		window.alert('Por favor, digite sua Cidade');
		f.cmpCidade.className = "textreq";
		f.cmpCidade.focus();
		return false;
	}
	if (f.cmpEstado.value == '')
	{
		window.alert('Por favor, selecione seu Estado');
		f.cmpEstado.className = "textreq";
		f.cmpEstado.focus();
		return false;
	}
	if (f.cmpCep2.value.length < 9)
	{
		window.alert('Por favor, digite seu CEP corretamente.');
		f.cmpCep2.className = "textreq";
		f.cmpCep2.focus();
		return false;
	}
}