// JavaScript Document
//###################################################################################
// pop ups
//###################################################################################
function PopUp(url, nome, largura, altura, sroll)
{
	var esq = (screen.width - largura) / 2;
	var top = (screen.height - altura) / 2;
	window.open(''+ url +'',''+ nome +'','width=' + largura + ',height=' + altura + ',top=' + top + ',left=' + esq + ',scrollbars='+ sroll +'');
}

//###################################################################################
// auto tab
//###################################################################################
var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {
    var keyCode = (isNN) ? e.which : e.keyCode; 
    var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
    if(input.value.length >= len && !containsElement(filter,keyCode)) {
        input.value = input.value.slice(0, len);
        input.form[(getIndex(input)+1) % input.form.length].focus();
}

function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
        if(arr[index] == ele)
            found = true;
        else
            index++;
        return found;
    }

function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
        if (input.form[i] == input)index = i;
        else i++;
        return index;
    }
return true;
}
//###################################################################################
// 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; }
}
//###################################################################################
//  Limpa String
//###################################################################################
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}
    }
    return temp
}
//###################################################################################
//  Limpa String
//###################################################################################
function mascaraMoeda(campo, sepMilesimo, sepDecimal, e, tam) // onKeyPress="javascript:return mascaraMoeda(this,'.',',',event, tam);"
{
	var strCheck = '0123456789';
	var tecla = (window.Event) ? e.which : e.keyCode;
	if ( tecla == 8 || tecla == 9 || tecla == 13 || tecla == 0 ) { return true; }
	var key = String.fromCharCode(tecla);
	if ( strCheck.indexOf(key) == -1 ) { return false; }
	var len = campo.value.length;
	if (len >= tam) { return false; }
	for( var i = 0; i < len; i++ )
	{
		if ( (campo.value.charAt(i) != '0') && (campo.value.charAt(i) != sepDecimal) ) break;
	}
	var aux = '';
	for(; i < len; i++)
		if (strCheck.indexOf(campo.value.charAt(i))!=-1) aux += campo.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) campo.value = '';
	if (len == 1) campo.value = '0'+ sepDecimal + '0' + aux;
	if (len == 2) campo.value = '0'+ sepDecimal + aux;
	if (len > 2) {
		var aux2 = '';
		for (var j = 0, i = len - 3; i >= 0; i--) {
			if (j == 3) {
				aux2 += sepMilesimo;
				j = 0;
			}
			aux2 += aux.charAt(i);
			j++;
		}
		var valor = ''
		var len2 = aux2.length;
		for (i = len2 - 1; i >= 0; i--)
		valor += aux2.charAt(i);
		valor += sepDecimal + aux.substr(len - 2, len);
		campo.value = valor;
	}
	return false;
}

//***********************************************************************************************
// 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;
	}
	if (s == "000000000000" || s == "111111111111" || s == "222222222222" || s == "333333333333" || s == "444444444444" || s == "555555555555" || s == "666666666666" || s == "777777777777" || s == "888888888888" || s == "999999999999") 
    {
		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;
	}
     if (numero == "00000000000" || numero == "11111111111" || numero == "22222222222" || numero == "33333333333" || numero == "44444444444" || numero == "55555555555" || numero == "66666666666" || numero == "77777777777" || numero == "88888888888" || numero == "99999999999") 
    {
		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;
}
//***********************************************************************************************
// 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;
}
//***********************************************************************************************
// pagina de revendas
//***********************************************************************************************
function validarRevenda(f)
{
	//var s;
	
	if (f.cmpEmpresa.value == '')
	{
		window.alert("Por favor, digite o nome da Revenda.");
		f.cmpEmpresa.className = "textreq";
		f.cmpEmpresa.focus();
		return false;
	}
	if (f.cmpContato.value == '')
	{
		window.alert("Por favor, digite o nome do respons\xE1vel.");
		f.cmpContato.className = "textreq";
		f.cmpContato.focus();
		return false;
	}
	/*if (f.nome_usuario.value == '')
	{
		window.alert("Por favor, digite o nome de usu\xE1rio.");
		f.nome_usuario.className = "textreq";
		f.nome_usuario.focus();
		return false;
	}
	if (f.nome_usuario.value.length < 6) {
	window.alert("O nome de usu\xE1rio deve ter no m\xEDnimo 6 caracteres !");
		f.nome_usuario.className = "textreq";
		f.nome_usuario.focus();
		return false;
	}
	if (f.nome_usuario.value.length > 14) {
	window.alert("O nome de usu\xE1rio deve ter no m\xE1ximo 14 caracteres !");
	f.nome_usuario.className = "textreq";
	f.nome_usuario.focus();
	return false;
	}*/

	if ((f.tipo[0].checked==false) && (f.tipo[1].checked==false)) {
	    window.alert("Por favor, selecione o tipo de empresa.");
		return false;
	}	
	var cpf1 = limpa_string(f.cpf_cic.value);
	if ((f.tipo[0].checked) && (cpf1.length !== 11))
	{
		window.alert("Por favor, informe o CPF da Revenda.");
		f.cpf_cic.className = "textreq";
		f.cpf_cic.focus();
		return false;
	}
	
	if ((f.tipo[0].checked) && (cpf1.length == 11) && (validarCPF(cpf1) == false))
	{
		window.alert("Por favor, informe o CPF v\xE1lido.");
		//window.alert('Por favor, informe um CPF v\xElido.');
		f.cpf_cic.className = "textreq";
		f.cpf_cic.focus();
		return false;
	}
	
	if ((f.tipo[0].checked) && (f.identidade.value == ''))
	{
		window.alert("Por favor, digite o RG corretamente.");
		f.identidade.className = "textreq";
		f.identidade.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 Revenda.");
		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.");
		//window.alert('Por favor, informe um CPF v\xElido.');
		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.cmpEndereco.value == '')
	{
		window.alert("Por favor, digite o endere\xE7o.");
		f.cmpEndereco.className = "textreq";
		f.cmpEndereco.focus();
		return false;
	}
	if (f.cmpEndereconumero.value == '')
	{
		window.alert("Por favor, digite o n\xFAmero do endere\xE7o.");
		f.cmpEndereconumero.className = "textreq";
		f.cmpEndereconumero.focus();
		return false;
	}
	if (f.cmpBairro.value == '')
	{
		window.alert("Por favor, digite o Bairro.");
		f.cmpBairro.className = "textreq";
		f.cmpBairro.focus();
		return false;
	}
	if (f.cmpCidade.value == '')
	{
		window.alert("Por favor, digite a Cidade.");
		f.cmpCidade.className = "textreq";
		f.cmpCidade.focus();
		return false;
	}
	if (f.cmpEstado.value == '')
	{
		window.alert("Por favor, selecione o Estado.");
		f.cmpEstado.className = "textreq";
		f.cmpEstado.focus();
		return false;
	}
	if (f.cmpCep2.value.length < 9)
	{
		window.alert("Por favor, digite o CEP corretamente.");
		f.cmpCep2.className = "textreq";
		f.cmpCep2.focus();
		return false;
	}
 
	if (f.cmpTelefone.value == '')
	{
		window.alert("Por favor, digite o Telefone.");
		f.cmpTelefone.className = "textreq";
		f.cmpTelefone.focus();
		return false;
	}
	if (f.cmpEmail.value.length==0){
        window.alert('Favor preencher Email da Revenda.');
        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;
	}
}
//###################################################################################
// consiste dados do login das revendas
//###################################################################################
function checar_revenda(f) {
  
   if (f.ass_usuario.value == '') {
      window.alert("O usu\xE1rio \xE9 um campo obrigat\xF3rio !");
	  f.ass_usuario.className = "textreq";
      f.ass_usuario.focus();
      return false;
   }
    if (f.acao.checked == false) {
        if (f.senha.value.length == 0) {
            alert("A senha \xE9 um campo obrigat\xF3rio !");
            f.senha.focus();
            return false;
        }
    }

    return true;
}
//######################################################################################
//    validar fale conosco
//######################################################################################
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.informacoes.value == '')
	{
		window.alert('Por favor, digite a mensagem desejada');
		f.informacoes.className = "textreq";
		f.informacoes.focus();
		return false;
	}
}
//###################################################################################
// valida formulário de orcamento
//###################################################################################
function validarOrcamento(f)
{
	
	if (f.fpagamento.value == '')
	{
		window.alert('Por favor, selecione a forma de pagamento desejada');
		f.fpagamento.className = "textreq";
		f.fpagamento.focus();
		return false;
	}
	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;
	}
}