function getid(elemento)
{
  return document.getElementById(elemento);
}

function getname(elemento)
{
  return document.getElementsByName(elemento);
}


function somente_numero(numero)
{
  var validos = "0123456789";
  var numero_ok = '';
  for(i = 0; i < numero.length; i++)
  {
     if(validos.indexOf(numero.substr(i, 1)) != - 1)
     {
        numero_ok += numero.substr(i, 1);
     }
  }
  return numero_ok;
}


function somente_letras(letra)
{
  var validos = "_abcdefghijklmnopqkrstuvxzywçáéíóú ABCDEFGHJKLMNOPQRSTUVXZWÇÁÉÍÓÚ.àÀ";
  var letra_ok = '';

  for(i = 0; i < letra.length; i++)
  {
     if(validos.indexOf(letra.substr(i, 1)) != - 1)
     {
        letra_ok += letra.substr(i, 1);
     }
  }
  return letra_ok;
}


function LTrim(str)
{
  var whitespace = new String(" \t\n\r");
  var s = new String(str);

  if(whitespace.indexOf(s.charAt(0)) != - 1)
  {
     var j = 0, i = s.length;
     while(j < i && whitespace.indexOf(s.charAt(j)) != - 1)
        j++;
     s = s.substring(j, i);
  }
  return s;
}

function RTrim(str)
{
  var whitespace = new String(" \t\n\r");

  var s = new String(str);

  if(whitespace.indexOf(s.charAt(s.length - 1)) != - 1)
  {
     var i = s.length - 1;
     while(i >= 0 && whitespace.indexOf(s.charAt(i)) != - 1)
        i--;
     s = s.substring(0, i + 1);
  }
  return s;
}

function trim(str)
{
  return RTrim(LTrim(str));
}

function InseriMascara(objCampo, mascara)
{
  switch(mascara)
  {

     case 'cpf':
        objCampo.value = somente_numero(objCampo.value);
        pri = objCampo.value.substring(0, 3);
        seg = objCampo.value.substring(3, 6);
        ter = objCampo.value.substring(6, 9);
        qua = objCampo.value.substring(9, 11);

        objCampo.value = pri +
        ((seg != '') ? '.' + seg : '') +
        ((ter != '') ? '.' + ter : '') +
        ((qua != '') ? '-' + qua : '');
        break;


     case 'cnpj':
        objCampo.value = somente_numero(objCampo.value);
        pri = objCampo.value.substring(0, 2);
        seg = objCampo.value.substring(2, 5);
        ter = objCampo.value.substring(5, 8);
        qua = objCampo.value.substring(8, 12);
        qui = objCampo.value.substring(12, 14);

        objCampo.value = pri +
        ((seg != '') ? '.' + seg : '') +
        ((ter != '') ? '.' + ter : '') +
        ((qua != '') ? '/' + qua : '') +
        ((qui != '') ? '-' + qui : '');
        break;

     case 'telefone':
        objCampo.value = somente_numero(objCampo.value);

        pri = objCampo.value.substring(0, 2);
        seg = objCampo.value.substring(2, 6);
        ter = objCampo.value.substring(6, 10);

        objCampo.value = ((pri != '') ? '(' + pri + ') ' : '') +
        ((seg != '') ? seg : '') +
        ((ter != '') ? '-' + ter : '');
        break;


     case 'cep':
        objCampo.value = somente_numero(objCampo.value);

        pri = objCampo.value.substring(0, 5);
        seg = objCampo.value.substring(5, 8);

        objCampo.value = pri +
        ((seg != '') ? '-' + seg : '');
        break;


     case 'data':
        objCampo.value = somente_numero(objCampo.value);

        pri = objCampo.value.substring(0, 2);
        seg = objCampo.value.substring(2, 4);
        ter = objCampo.value.substring(4, 8);

        objCampo.value = pri +
        ((seg != '') ? '/' + seg : '') +
        ((ter != '') ? '/' + ter : '')
        break;


     case 'venc_cartao':
        objCampo.value = somente_numero(objCampo.value);

        pri = objCampo.value.substring(0, 2);
        seg = objCampo.value.substring(2, 6);

        objCampo.value = pri +
        ((seg != '') ? '/' + seg : '')
        break;


     case 'cartao':
        objCampo.value = somente_numero(objCampo.value);

        pri = objCampo.value.substring(0, 4);
        seg = objCampo.value.substring(4, 8);
        ter = objCampo.value.substring(8, 12);
        qua = objCampo.value.substring(12, 16);

        objCampo.value = pri +
        ((seg != '') ? '-' + seg : '') +
        ((ter != '') ? '-' + ter : '') +
        ((qua != '') ? '-' + qua : '');
        break;

     case 'numero':
        objCampo.value = somente_numero(objCampo.value);
        break;

     case 'letra':
        objCampo.value = somente_letras(objCampo.value);
        break;


     case 'moeda':
        len = 20
        cur = objCampo
        n = '0123456789';
        d = objCampo.value;
        l = d.length;
        r = '';

        if(l > 0)
        {
           z = d.substr(0, l);
           s = '';
           a = 0;

           for(i = 0; i < l; i++)
           {
              c = d.charAt(i);
              if(n.indexOf(c) > a)
              {
                 a = -1;
                 s += c;
              }
              ;
           }
           ;

           l = s.length;
           t = len - 1;
           if(l > t)
           {
              l = t;
              s = s.substr(0, t);
           }
           if(l > 2)
           {
              r = s.substr(0, l - 2) + ',' + s.substr(l - 2, 2);
           }
           else
           {
              if(l == 2)
              {
                 r = '0,' + s;
              }
              else
              {
                 if(l == 1)
                 {
                    r = '0,0' + s;
                 }
              }
           }
           if(r == '')
           {
              r = '0,00';
           }
           else
           {
              l = r.length;
              if(l > 6)
              {
                 j = l % 3;
                 w = r.substr(0, j);
                 wa = r.substr(j, l - j - 6);
                 wb = r.substr(l - 6, 6);
                 if(j > 0)
                 {
                    w += '.';
                 }
                 ;
                 k = (l - j) / 3 - 2;
                 for(i = 0; i < k; i++)
                 {
                    w += wa.substr(i * 3, 3) + '.';
                 }
                 ;
                 r = w + wb;
              }
           }
        }
        if(cur.value.length == len || cur.value.length > len)
        {
           cur.value = cur.value.substring(0, len);
           return false;
        }
        else
        {
           if(r.length <= len)
           {
              cur.value = r;
           }
           else
           {
              cur.value = z;
           }
           ;
        }
        break;
  }
}



function valida_email(email)
{
  email = email.value = trim(email.value);
  if(email == null)
  {
     return false;
  }

  if(email == '')
  {
     return true;
  }

  var atPos = email.indexOf("@");

  if(
  atPos < 1 ||
  email.indexOf(".", atPos) == - 1
  )
  {
     return false
  }

  var login = email.substring(0, atPos);
  var domain = email.substring(atPos + 1, email.length);

  // Regexp declarations
  var atom = "\[^\\s\\(\\)><@,;:\\\\\\\"\\.\\[\\]\]+";
  var word = "(" + atom + "|(\"[^\"]*\"))";
  var loginRE = new RegExp("^" + word + "(\\." + word + ")*$");

  for(i = 0; i < login.length; i++)
  {
     if(login.charCodeAt(i) > 127)
     {
        return false;
     }
  }

  if(!login.match(loginRE))
  {
     return false;
  }

  return this.valida_dominio(domain);
}

/*
* Valida CPF
*/
function valida_cpf(valor)
{
  valor = valor.value = somente_numero(valor.value);
  //if(valor == '') return true;//tem que usar como obrigatorio
  teste = new Array();
  obj = valor;
  var D1, D2, D1calc, D2calc, aux;
  var j = 0;
  var counter = 0;
  for(i = 0; i < obj.length; i++)
  {
     if(!isNaN(obj.substring(i, i + 1)))
     {
        teste[j] = obj.substring(i, i + 1);
        j++;
     }
  }
  aux = teste[0];
  for(i = 0; i < teste.length; i++)
  {
     if(teste[i] == aux)
     {
        counter++;
     }
  }
  if(counter == teste.length)
  {
     return false;
  }
  D1 = teste[j - 2];
  D2 = teste[j - 1];
  D1calc = 0;
  D2calc = 0;
  var loop = j - 2;
  for(i = 0; i < loop; i++)
  {
     D1calc += Number(teste[i]) * (10 - i);
     D2calc += Number(teste[i]) * (11 - i);
  }
  D1calc = (D1calc * 10) % 11;
  if(D1calc == 10)
  {
     D1calc = 0}
  ;
  D2calc = ((D2calc + (2 * D1calc)) * 10) % 11;
  if(D2calc == 10)
  {
     D2calc = 0}
  ;
  if(D1 == D1calc && D2 == D2calc)
  {
     return true;
  }
  else
  {
     return false;
  }
}

/*
* Valida CNPJ
*/

function cnpj_aux1(c)
{
  if((cx = c.indexOf(",")) != - 1)
  {
     c = c.substring(0, cx) + "." + c.substring(cx + 1);
  }
  if((parseFloat(c) / c != 1))
  {
     if(parseFloat(c) * c == 0)
     {
        return(1);
     }
     else
     {
        return(0);
     }
  }
  else
  {
     return(1);
  }
}

function cnpj_aux2(c)
{
  while((cx = c.indexOf("-")) != - 1)
  {
     c = c.substring(0, cx) + c.substring(cx + 1);
  }
  while((cx = c.indexOf("/")) != - 1)
  {
     c = c.substring(0, cx) + c.substring(cx + 1);
  }
  while((cx = c.indexOf(",")) != - 1)
  {
     c = c.substring(0, cx) + c.substring(cx + 1);
  }
  while((cx = c.indexOf(".")) != - 1)
  {
     c = c.substring(0, cx) + c.substring(cx + 1);
  }
  while((cx = c.indexOf("(")) != - 1)
  {
     c = c.substring(0, cx) + c.substring(cx + 1);
  }
  while((cx = c.indexOf(")")) != - 1)
  {
     c = c.substring(0, cx) + c.substring(cx + 1);
  }
  while((cx = c.indexOf(" ")) != - 1)
  {
     c = c.substring(0, cx) + c.substring(cx + 1);
  }
  return(c);
}

function cnpj_aux3(CNPJ, g)
{
  var VerCNPJ = 0;
  var ind = 2;
  var tam;
  for(f = g; f > 0; f--)
  {
     VerCNPJ += parseInt(CNPJ.charAt(f - 1)) * ind;
     if(ind > 8)
     {
        ind = 2;
     }
     else
     {
        ind++;
     }
  }

  VerCNPJ %= 11;
  if(VerCNPJ == 0 || VerCNPJ == 1)
  {
     VerCNPJ = 0;
  }
  else
  {
     VerCNPJ = 11 - VerCNPJ;
  }
  if(VerCNPJ != parseInt(CNPJ.charAt(g)))
  {
     return(0);
  }
  else
  {
     return(1);
  }
}

function valida_cnpj(CNPJ)
{
  CNPJ = CNPJ.value = somente_numero(CNPJ.value);
  if(CNPJ == '') return true;//tem que usar como obrigatorio
  CNPJ = cnpj_aux2(CNPJ);
  if(cnpj_aux1(CNPJ) != 1)
  {
     return false;
  }
  else
  {
     if(CNPJ == 0)
     {
        return false;
     }
     else
     {
        g = CNPJ.length - 2;
        if(cnpj_aux3(CNPJ, g) == 1)
        {
           g = CNPJ.length - 1;
           if(cnpj_aux3(CNPJ, g) == 1)
           {
              return true;
           }
           else
           {
              return false;
           }
        }
        else
        {
           return false;
        }
     }
  }
}



function valida_minimo(objCampo, valorMinimo)
{
  valorMinimo = parseInt(valorMinimo);
  return(!(objCampo.value.length < valorMinimo));
}


function valida_maximo(objCampo, valorMaximo)
{
  valorMaximo = parseInt(valorMaximo);
  return(!(objCampo.value.length > valorMaximo));
}



  function valida_contato()
  {

  //Valida o nome para não ser vazio
  if(getid('edit_contato_nome').value==""){
    getid('edit_contato_nome').className = "edits_red";
    getid('edit_contato_nome').focus();
    return false;
  }
  else
  {
    getid('edit_contato_nome').className = "edits";
  }

    //Valida o Email para não ser vazio
  if(getid('edit_contato_email').value == "")
  {
    getid('edit_contato_email').className = "edits_red";
    getid('edit_contato_email').focus();
    return false;
  }
  else
  {
    getid('edit_contato_email').className = "edits";
  }

  //Valida o Email para ser um email válido
  if(getid('edit_contato_email').value !=""){
		parte1 = getid('edit_contato_email').value.indexOf("@");
		parte2 = getid('edit_contato_email').value.indexOf(".");
		parte3 = getid('edit_contato_email').value.length;
		if (!(parte1 >= 3 && parte2 >= 6 && parte3 >= 9)) {
			getid('edit_contato_email').className = "edits_red";
      getid('edit_contato_email').focus();
      return false;
		}
  }
  else
  {
    getid('edit_contato_email').className = "edits";
  }

    //Valida o nome para não ser vazio
  if(getid('edit_contato_assunto').value==""){
    getid('edit_contato_assunto').className = "edits_red";
    getid('edit_contato_assunto').focus();
    return false;
  }
  else
  {
    getid('edit_contato_assunto').className = "edits";
  }

      //Valida o nome para não ser vazio
  if(getid('edit_contato_msg').value==""){
    getid('edit_contato_msg').className = "edits_red";
    getid('edit_contato_msg').focus();
    return false;
  }
  else
  {
    getid('edit_contato_msg').className = "edits";
  }



    if(confirm('Confirma os dados digitados na ficha de inscrição ?'))
  {
    return true;
  }
  else
  {
    return false;
  }

  }


  function valida_inscricao()
  {

  //Valida o nome
  if(getid('edit_nome').value=="" || getid('edit_nome').value.length < 15)
  {
    getid('edit_nome').className = "edits_red";
    getid('edit_nome').focus();
    return false;
  }
  else
  {
    getid('edit_nome').className = "edits";
  }

  //Valida Nome no Crachá
  if(getid('edit_nome_cracha').value=="" || getid('edit_nome_cracha').value.length < 15){
    getid('edit_nome_cracha').className = "edits_red";
    getid('edit_nome_cracha').focus();
    return false;
  }
  else
  {
    getid('edit_nome_cracha').className = "edits";
  }

  //Valida o Email
  if(getid('edit_email').value=="" || getid('edit_email').value.length < 15){
    getid('edit_email').className = "edits_red";
    getid('edit_email').focus();
    return false;
  }
  else
  {
    getid('edit_email').className = "edits";
  }

  if(getid('edit_email').value !=""){
		parte1 = getid('edit_email').value.indexOf("@");
		parte2 = getid('edit_email').value.indexOf(".");
		parte3 = getid('edit_email').value.length;
		if (!(parte1 >= 3 && parte2 >= 6 && parte3 >= 9)) {
			getid('edit_email').className = "edits_red";
      getid('edit_email').focus();
      return false;
		}
  }
  else
  {
    getid('edit_email').className = "edits";
  }

  //Valida Endereço
  if(getid('edit_endereco').value=="" || getid('edit_endereco').value.length < 15)
  {
    getid('edit_endereco').className = "edits_red";
    getid('edit_endereco').focus();
    return false;
  }
  else
  {
    getid('edit_endereco').className = "edits";
  }

  //Valida Bairro
  if(getid('edit_bairro').value=="" || getid('edit_bairro').value.length < 2){
    getid('edit_bairro').className = "edits_red";
    getid('edit_bairro').focus();
    return false;
  }
  else
  {
    getid('edit_bairro').className = "edits";
  }

  //Valida CEP
  if(getid('edit_cep').value=="" || getid('edit_cep').value.length < 9){
    getid('edit_cep').className = "edits_red";
    getid('edit_cep').focus();
    return false;
  }
  else
  {
    getid('edit_cep').className = "edits";
  }

  //Valida Cidade
  if(getid('edit_cidade').value=="" || getid('edit_cidade').value.length < 2){
    getid('edit_cidade').className = "edits_red";
    getid('edit_cidade').focus();
    return false;
  }
  else
  {
    getid('edit_cidade').className = "edits";
  }

  //Valida Estado
  if(getid('edit_estado').value=="" || getid('edit_estado').value.length < 2){
    getid('edit_estado').className = "edits_red";
    getid('edit_estado').focus();
    return false;
  }
  else
  {
    getid('edit_estado').className = "edits";
  }

  //Valida Telefone Residencial
  if(getid('edit_tel_residencial').value=="" || getid('edit_tel_residencial').value.length < 14){
    getid('edit_tel_residencial').className = "edits_red";
    getid('edit_tel_residencial').focus();
    return false;
  }
  else
  {
    getid('edit_tel_residencial').className = "edits";
  }

  //Valida Telefone Celular
  if(getid('edit_tel_cel').value=="" || getid('edit_tel_cel').value.length < 14){
    getid('edit_tel_cel').className = "edits_red";
    getid('edit_tel_cel').focus();
    return false;
  }
  else
  {
    getid('edit_tel_cel').className = "edits";
  }

  //Valida Telefone Comercial
  if(getid('edit_tel_comercial').value=="" || getid('edit_tel_comercial').value.length < 14)
  {
    getid('edit_tel_comercial').className = "edits_red";
    getid('edit_tel_comercial').focus();
    return false;
  }
  else
  {
    getid('edit_tel_comercial').className = "edits";
  }

  //Valida CPF
  if(getid('edit_cpf_medico').value=="" || getid('edit_cpf_medico').value.length < 14){
    getid('edit_cpf_medico').className = "edits_red";
    getid('edit_cpf_medico').focus();
    return false;
  }
  else
  {
    getid('edit_cpf_medico').className = "edits";
  }


  cpf = somente_numero(getid('edit_cpf_medico').value);
			 erro = new String;
			 if (cpf.length < 11) erro += "São necessários 11 digitos para verificação do cpf! \n\n";
			 var nonNumbers = /\D/;
			 if (nonNumbers.test(cpf)) erro += "A verificacao de cpf suporta apenas números! \n\n";
			 if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
					erro += "Numero de cpf invalido!"
		   }
		   var a = [];
		   var b = new Number;
		   var c = 11;
		   for (i=0; i<11; i++){
				   a[i] = cpf.charAt(i);
				   if (i < 9) b += (a[i] * --c);
		   }
		   if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
		   b = 0;
		   c = 11;
		   for (y=0; y<10; y++) b += (a[y] * c--);
		   if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
		   if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
				   erro +="Digito verificador com problema!";
		   }
		   if (erro.length > 0){
           getid('edit_cpf_medico').className = "edits_red";
				   getid('edit_cpf_medico').focus();
           //alert('Por favor preencha todos os campos em vermelho corretamente');
           return false;
			}
      else
      {
        getid('edit_cpf_medico').className = "edits";
      }


  //Valida CRM
  if(getid('edit_crm_medico').value=="" || getid('edit_crm_medico').value.length < 5){
    getid('edit_crm_medico').className = "edits_red";
    getid('edit_crm_medico').focus();
    return false;
  }
  else
  {
    getid('edit_crm_medico').className = "edits";
  }

  //Valida Instituicao
  if(getid('edit_instituicao').value=="" || getid('edit_instituicao').value.length < 2){
    getid('edit_instituicao').className = "edits_red";
    getid('edit_instituicao').focus();
    return false;
  }
  else
  {
    getid('edit_instituicao').className = "edits";
  }

  //Valida Especialidade
  if(getid('edit_especialidade').value=="" || getid('edit_especialidade').value.length < 2){
    getid('edit_especialidade').className = "edits_red";
    getid('edit_especialidade').focus();
    return false;
  }
  else
  {
    getid('edit_especialidade').className = "edits";
  }


    if(confirm('Confirma os dados digitados na ficha de inscrição ?'))
  {
    return true;
  }
  else
  {
    return false;
  }



  }