<?php
header('Content-Type: text/javascript');
?>
function enviarCpanel (formulario) {
    var usuario = formulario.user.value;
    conta_executar(usuario);
    formulario.botao.disabled=true;
    formulario.botao.value='Aguarde...';
}

function enviarDominio (formulario) {
    formulario.botao.disabled=true;
    formulario.botao.value='Aguarde...';
    document.getElementById('iframe_dominio').src = 'http://www3.cooternet.com';
    setTimeout("submeteFormDominio('form_dominio');",5000);
    //setInterval("formulario.submit();",50000);
}

function submeteFormDominio (formulario) {
    document.getElementById(formulario).submit()

}

function enviarWebmail (formulario) {
    var email = formulario.user.value.split('@');
    var url = email[1];
    var form_action = 'http://www.'+url+':2095/login/';
    formulario.action = form_action;
    formulario.botao.disabled=true;
    formulario.botao.value='Aguarde...';
    formulario.submit();
}

function validarAssine () {
  var passou = true;
  var alerta = "O sistema encontrou os seguintes erros no preenchimento do formulário:\n\n";

  // validações dos campos gerais

  if (document.getElementById("dominio").value == "") {
     passou = false; alerta += "- Domínio preenchido incorretamente.\n";
  }

  if (document.forms[0].usuario[0].checked) {
      if (document.getElementById("nome").value == "") {
         passou = false; alerta += "- Nome preenchido incorretamente.\n";
      }
  }

  if (document.forms[0].usuario[0].checked) {
      if (document.getElementById("sobrenome").value == "") {
         passou = false; alerta += "- Sobrenome preenchido incorretamente.\n";
      }
  }

  if (document.forms[0].usuario[0].checked) {
      if (document.getElementById("cpf").value == "") {
         passou = false; alerta += "- CPF preenchido incorretamente.\n";
      }else{
             cpf = document.getElementById("cpf").value;
             cpf = cpf.replace(".", "");
             cpf = cpf.replace(".", "");
             cpf = cpf.replace("-", "");
             if (!checacpf(cpf)) {
                 passou = false; alerta += "- CPF preenchido é inválido.\n";
             }
      }
  }

  if (document.forms[0].usuario[0].checked) {
      if (document.getElementById("empresa").value == "") {
         passou = false; alerta += "- Nome da empresa preenchido incorretamente.\n";
      }
  }

  var email1 = document.formAssine.email.value;
  var email2 = document.formAssine.email2.value;
  if (email1 != email2) {
     passou = false; alerta += "- Repita corretamente o e-mail.\n";
  }else if (document.formAssine.email.value == "") {
     passou = false; alerta += "- E-mail preenchido incorretamente.\n";
  }else{
      if (!validarEmail(document.formAssine.email.value)) {
           passou = false; alerta += "- E-mail preenchido incorretamente.\n";
      }
  }

  if (document.forms[0].usuario[0].checked) {
      var senha1 = document.formAssine.senha.value;
      var senha2 = document.formAssine.senha2.value;
      if (senha1 != senha2) {
         passou = false; alerta += "- As senhas são diferentes.\n";
      }else {
         if (senha1.length < 6) {
            passou = false; alerta += "- Senha precisa ter pelo menos seis caracteres. \n";
         }
      }
  }

  if (document.forms[0].usuario[0].checked) {
      if (document.getElementById("form_cep").value.length < 9) {
         passou = false; alerta += "- CEP preenchido incorretamente. Siga o padrão: 00000-000\n";
      }

      if (document.getElementById("form_endereco").value == "") {
         passou = false; alerta += "- Endereço preenchido incorretamente.\n";
      }

      if (document.getElementById("form_numero").value == "") {
         passou = false; alerta += "- Número do imóvel preenchido incorretamente.\n";
      }

      if (document.getElementById("form_bairro").value == "") {
         passou = false; alerta += "- Bairro preenchido incorretamente.\n";
      }

      if (document.getElementById("form_cidade").value == "") {
         passou = false; alerta += "- Cidade preenchida incorretamente.\n";
      }

      if (document.getElementById("form_estado").value == "") {
         passou = false; alerta += "- Estado preenchido incorretamente.\n";
      }
  }

  if (passou == true) { document.getElementById('formAssine').submit(); }
  else { alert(alerta); }
}

function validarRegistro () {
  var passou = true;
  var alerta = "O sistema encontrou os seguintes erros no preenchimento do formulário:\n\n";

  // validações dos campos gerais

  if (document.forms[0].usuario[0].checked) {
      if (document.getElementById("nome").value == "") {
         passou = false; alerta += "- Nome preenchido incorretamente.\n";
      }
  }

  if (document.forms[0].usuario[0].checked) {
      if (document.getElementById("sobrenome").value == "") {
         passou = false; alerta += "- Sobrenome preenchido incorretamente.\n";
      }
  }

  if (document.forms[0].usuario[0].checked) {
      if (document.getElementById("cpf").value == "") {
         passou = false; alerta += "- CPF preenchido incorretamente.\n";
      }else{
             cpf = document.getElementById("cpf").value;
             cpf = cpf.replace(".", "");
             cpf = cpf.replace(".", "");
             cpf = cpf.replace("-", "");
             if (!checacpf(cpf)) {
                 passou = false; alerta += "- CPF preenchido é inválido.\n";
             }
      }
  }

  if (document.forms[0].usuario[0].checked) {
      if (document.getElementById("empresa").value == "") {
         passou = false; alerta += "- Nome da empresa preenchido incorretamente.\n";
      }
  }

  var email1 = document.formAssine.email.value;
  var email2 = document.formAssine.email2.value;
  if (email1 != email2) {
     passou = false; alerta += "- Repita corretamente o e-mail.\n";
  }else if (document.formAssine.email.value == "") {
     passou = false; alerta += "- E-mail preenchido incorretamente.\n";
  }else{
      if (!validarEmail(document.formAssine.email.value)) {
           passou = false; alerta += "- E-mail preenchido incorretamente.\n";
      }
  }

  if (document.forms[0].usuario[0].checked) {
      var senha1 = document.formAssine.senha.value;
      var senha2 = document.formAssine.senha2.value;
      if (senha1 != senha2) {
         passou = false; alerta += "- As senhas são diferentes.\n";
      }else {
         if (senha1.length < 6) {
            passou = false; alerta += "- Senha precisa ter pelo menos seis caracteres. \n";
         }
      }
  }

  if (document.forms[0].usuario[0].checked) {
      if (document.getElementById("form_cep").value.length < 9) {
         passou = false; alerta += "- CEP preenchido incorretamente. Siga o padrão: 00000-000\n";
      }

      if (document.getElementById("form_endereco").value == "") {
         passou = false; alerta += "- Endereço preenchido incorretamente.\n";
      }

      if (document.getElementById("form_numero").value == "") {
         passou = false; alerta += "- Número do imóvel preenchido incorretamente.\n";
      }

      if (document.getElementById("form_bairro").value == "") {
         passou = false; alerta += "- Bairro preenchido incorretamente.\n";
      }

      if (document.getElementById("form_cidade").value == "") {
         passou = false; alerta += "- Cidade preenchida incorretamente.\n";
      }

      if (document.getElementById("form_estado").value == "") {
         passou = false; alerta += "- Estado preenchido incorretamente.\n";
      }
  }

  if (passou == true) { document.getElementById('formAssine').submit(); }
  else { alert(alerta); }
}

function displayElement (id, display) {
    document.getElementById(id).style.display = display;
}

function colocaHTML (id, html) {
    document.getElementById(id).innerHTML = html;
}

function validarEmail(email)
{
	var s = email;
	var filter=/^[A-Za-z][A-Za-z0-9_.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	if (s.length == 0 ) return true;
	if (filter.test(s))
	return true;
	else
	return false;
}

function checacpf(cpf) {
   if(cpf.length < 11 || cpf == '11111111111' || cpf == '22222222222' || cpf == '33333333333' || cpf == '44444444444' || cpf == '55555555555' || cpf == '66666666666' || cpf == '77777777777' || cpf == '88888888888' || cpf == '99999999999' || cpf == '00000000000')
   {
     var cpf_status = false;
   }
   else
   {
     var dv_informado = cpf.substring(9,11);
     digito = new Array();
     for(i=0; i<=8; i++) {
       digito[i] = cpf.substring(i,(i+1));
     }

     var posicao = 10;
     var soma = 0;

     for(i=0; i<=8; i++) {
       soma = soma + digito[i] * posicao;
       posicao = posicao - 1;
     }

     digito[9] = soma % 11;

     if(digito[9] < 2)
     {
       digito[9] = 0;
     }
     else
     {
       digito[9] = 11 - digito[9];
     }

     posicao = 11;
     soma = 0;

     for (i=0; i<=9; i++) {
       soma = soma + digito[i] * posicao;
       posicao = posicao - 1;
     }

     digito[10] = soma % 11;

     if (digito[10] < 2)
     {
       digito[10] = 0;
     }
     else
     {
       digito[10] = 11 - digito[10];
     }

     dv = digito[9] * 10 + digito[10];
     if (dv != dv_informado)
     {
       cpf_status = false;
     }
     else
     {
       cpf_status = true;
     }
     return cpf_status;
   }
}

function checacnpj(CNPJ) {
     erro = new String;
     if (CNPJ.length < 18) erro += "É necessario preencher corretamente o número do CNPJ! ";
     if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-"))
     {
         if (erro.length == 0) erro += "É necessário preencher corretamente o número do CNPJ! ";
     }
               //substituir os caracteres que não são números
     if(document.layers && parseInt(navigator.appVersion) == 4)
     {
         x = CNPJ.substring(0,2);
         x += CNPJ. substring (3,6);
         x += CNPJ. substring (7,10);
         x += CNPJ. substring (11,15);
         x += CNPJ. substring (16,18);
         CNPJ = x;
     }
     else
     {
         CNPJ = CNPJ. replace (".","");
         CNPJ = CNPJ. replace (".","");
         CNPJ = CNPJ. replace ("-","");
         CNPJ = CNPJ. replace ("/","");
     }
     var nonNumbers = /\D/;
     if (nonNumbers.test(CNPJ)) erro += "A verificação de CNPJ suporta apenas números! ";
     var a = [];
     var b = new Number;
     var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
     for (i=0; i<12; i++){
         a[i] = CNPJ.charAt(i);
         b += a[i] * c[i+1];
     }
     if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
     b = 0;
     for (y=0; y<13; y++) {
         b += (a[y] * c[y]);
     }
     if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
     if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13]))
     {
         erro +="Dígito verificador com problema!";
     }
     if (erro.length > 0)
     {
         //alert(erro);
         return false;
     }
     else
     {
         //alert("CNPJ valido!");
         return true;
     }

}


function numero (evento) {
   navegador = /msie/i.test(navigator.userAgent);
   if (navegador) {
      var tecla = event.keyCode;
   } else {
      var tecla = evento.which;
   }

   if (tecla > 47 && tecla < 58) {
      return true;
   } else {
      if (tecla != 8 && tecla != 9) {
      return false;
      } else {
      return true;
      }
   }
}

function alfa (evento) {
   navegador = /msie/i.test(navigator.userAgent);
   if (navegador) {
      var tecla = event.keyCode;
   } else {
      var tecla = evento.which;
   }

   if (tecla > 64 && tecla < 91 || tecla > 96 && tecla < 123) {
      return true;
   } else {
      if (tecla != 8 && tecla != 9) {
      return false;
      } else {
      return true;
      }
   }
}

function alfanumerico (evento) {
   navegador = /msie/i.test(navigator.userAgent);
   if (navegador) {
      var tecla = event.keyCode;
   } else {
      var tecla = evento.which;
   }

   if (tecla > 47 && tecla < 58 || tecla > 64 && tecla < 91 || tecla > 96 && tecla < 123) {
      return true;
   } else {
      if (tecla != 8 && tecla != 9) {
      return false;
      } else {
      return true;
      }
   }
}

function numero (evento) {
   navegador = /msie/i.test(navigator.userAgent);
   if (navegador) {
      var tecla = event.keyCode;
   } else {
      var tecla = evento.which;
   }

   if ((tecla > 47 && tecla < 58) || (tecla > 95 && tecla < 106)) {
      return true;
   } else {
      if (tecla != 8 && tecla != 9) {
      return false;
      } else {
      return true;
      }
   }
}

function mudarAcaoRegistro (registro) {
    if (registro == 'nada') {
        displayElement('tr_periodo', 'none');
    }
    else if (registro == 'transferir' || registro == 'registrar') {
        displayElement('tr_periodo', '');
    }
}

function mudaUsuario (tipo) {
    if (tipo == 'novo') {
        document.getElementById('tr_nome').style.display = '';
        document.getElementById('tr_sobrenome').style.display = '';
        document.getElementById('tr_cpf').style.display = '';
        document.getElementById('tr_empresa').style.display = '';
        document.getElementById('tr_senha').style.display = '';
        document.getElementById('tr_repitasenha').style.display = '';
        document.getElementById('tr_cep').style.display = '';
        document.getElementById('tr_info').style.display = '';
    }
    if (tipo == 'existente') {
        document.getElementById('tr_nome').style.display = 'none';
        document.getElementById('tr_sobrenome').style.display = 'none';
        document.getElementById('tr_cpf').style.display = 'none';
        document.getElementById('tr_empresa').style.display = 'none';
        document.getElementById('tr_senha').style.display = 'none';
        document.getElementById('tr_repitasenha').style.display = 'none';
        document.getElementById('tr_cep').style.display = 'none';
        document.getElementById('tr_info').style.display = 'none';
    }
}

