function Validar_CPF(campo)
{
    var i; 
    var falhou;
    if (campo.value.length == 11)
    {
        var c = campo.value.substr(0,9); 
        var dv = campo.value.substr(9,2); 
    }
    if (campo.value.length == 14)
    {
        var c = campo.value.substr(0,3) + campo.value.substr(4,3) + campo.value.substr(8,3); 
        var dv = campo.value.substr(12,2); 
    }
     if (c.length != 9 || c== "000000000" || c== "111111111" || c== "222222222" || c== "333333333" || c== "444444444" || c == "555555555" || c== "666666666" || c== "777777777" || c== "888888888" || c== "999999999")
        falhou = true;
   
    var d1 = 0; 
    for (i = 0; i < 9; i++) 
    { 
        d1 += c.charAt(i)*(10-i); 
    } 
    if (d1 == 0){ 
        falhou = false; 
    } 
    d1 = 11 - (d1 % 11); 
    if (d1 > 9) d1 = 0; 
        if (dv.charAt(0) != d1) 
        { 
            falhou = true; 
        } 
    d1 *= 2; 
    for (i = 0; i < 9; i++) 
    { 
        d1 += c.charAt(i)*(11-i); 
    } 
    d1 = 11 - (d1 % 11); 
    if (d1 > 9) d1 = 0; 
        if (dv.charAt(1) != d1) 
        { 
            falhou = true; 
        } 
    if (falhou) 
    {
        displayInlineError(campo, "CPF Inválido");
        campo.focus();
        return false; 
    }
    return true; 
}

function ValidaEmail(addr)
{
    var obj = eval(addr);
    var txt = obj.value;
    if ((txt.length != 0) && ((txt.indexOf("@") < 4) || (txt.indexOf('.') > txt.indexOf("@"))))
    { 
        displayInlineError(addr, "Preencha o e-mail num formato válido");
        addr.focus();
        return false; 
    } 
    return true; 
}
    
// Email Validation Javascript
// copyright 23rd March 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.


function get_radio_value(campo)
{
    for (var i=0; i < campo.length; i++)
    {
        if (campo[i].checked)
        {
            return campo[i].value;
        }
    }
    return false;
}

function campo_obrigatorio(campo)
{
    if ( campo.value == "" ) {
        displayInlineError(campo, "Este campo é obrigatório");
        campo.focus();
        return false;
    }
    return true;    
}

function displayInlineError(sField, sErrMsg)
{
    var itop;
    var ileft;
    var size;
    var sName;
    var oItem;
                
    oItem = sField;
    if(typeof(oItem)!="object")
        return;
                
    itop = 0;
    ileft = 0;            

    itop = oItem.offsetHeight;
    while (oItem.tagName != "BODY" ){
            itop  += oItem.offsetTop;
            ileft += oItem.offsetLeft;
        oItem = oItem.offsetParent;
    }
    document.getElementById("divError").style.left = ileft +"px";
    document.getElementById("divError").style.top  = itop - 10 +"px"; 
    document.getElementById("divError").style.display = "inline";
    document.getElementById("divError").innerText = sErrMsg;
    document.getElementById("divError").innerHTML = sErrMsg;
}

function ClearError(){
    divError.style.display = 'none';
}

function addEvent(obj, evt, func) {
  if (obj.attachEvent) {
    return obj.attachEvent(("on"+evt), func);
  } else if (obj.addEventListener) {
    obj.addEventListener(evt, func, true);
    return true;
  }
  return false;
}

function XMLHTTPRequest() {
  try {
    return new XMLHttpRequest(); // FF, Safari, Konqueror, Opera, ...
  } catch(ee) {
    try {
      return new ActiveXObject("Msxml2.XMLHTTP"); // activeX (IE5.5+/MSXML2+)
    } catch(e) {
      try {
        return new ActiveXObject("Microsoft.XMLHTTP"); // activeX (IE5+/MSXML1)
      } catch(E) {
        return false; // doesn't support
      }
    }
  }
}
function verifica_CPF(formulario, id_retorno) 
{
    var s = formulario.cpf.value;
    var f
    if (s.length == 14)
        f = s.substr(0,3)+s.substr(4,3)+s.substr(8,3)+s.substr(12,2); 
        f = f.replace(/[^\d]*/, "");
    
    var ajax = XMLHTTPRequest();
    ajax.open("GET", ("/cadastro_aluno_verifica_cpf.asp?cpf=" + f), true);
    ajax.onreadystatechange = function() 
    {
        if (ajax.readyState == 1) {
        } 
        else if (ajax.readyState == 4) {
            //document.getElementById(id_retorno).innerHTML = ajax.responseText;
            if (ajax.responseText == " CPF já cadastrado.")
            {
                document.getElementById(id_retorno.id).style.display='block';
                form_01.cpf.value=s;
                document.getElementById("senha_login").focus();
                document.getElementById("cad_submit").disabled="true";
                document.getElementById("cad_submit").style.opacity= 0.5;
            }
            else
            {
                document.getElementById(id_retorno.id).style.display='none';
                document.getElementById("cad_submit").disabled=false;
                document.getElementById("cad_submit").style.opacity= 1;
            }
        }
    };
    ajax.send(null);
    //if (formulario.cpf.value.length > 0) 
    //    formulario.cpf.value = StrZero(formulario.cpf.value,11);

}
function buscar_Endereco(formulario) {
    var campos = {
        cep: document.getElementById("cep"),
        endenreco: document.getElementById("endereco"),
        //numero: document.getElementById("numero"),
        bairro: document.getElementById("bairro"),
        cidade: document.getElementById("cidade"),
        uf: document.getElementById("uf")
    };
    var ajax = XMLHTTPRequest();
    ajax.open("GET", ("/buscaCEP.asp?cep=" + formulario.cep.value.replace(/[^\d]*/, "")), true);
    ajax.onreadystatechange = function() {
        if (ajax.readyState == 1) {
            formulario.endereco.value = "carregando...";
            formulario.bairro.value = "carregando...";
            formulario.cidade.value = "carregando...";
            formulario.uf.value = "carregando...";
        } 
        else if (ajax.readyState == 4) {
            var r = ajax.responseText, i, endereco, bairro, cidade, uf;
            endereco = r.substring(0, (i = r.indexOf(',')));
            r = r.substring(++i);
            bairro = r.substring(0, (i = r.indexOf(',')));
            r = r.substring(++i);
            cidade = r.substring(0, (i = r.indexOf(',')));
            r = r.substring(++i);
            uf = r.substring(0, (i = r.indexOf(',')));
            //r = r.substring(++i);
            formulario.endereco.value = endereco;
            formulario.bairro.value = bairro;
            formulario.cidade.value = cidade;
            formulario.uf.value = uf;
            formulario.endereco.disabled=false;
            formulario.bairro.disabled=false;
            formulario.cidade.disabled=false;
            formulario.uf.disabled=false;
            formulario.end_numero.focus();
        }
    };
    ajax.send(null);
}
function Validar_EMAIL(addr,man,db) {
    if (addr == '') return true;
    var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
    for (i=0; i<invalidChars.length; i++) {
       if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
          if (db)
            {
            displayInlineError(form_01.email, "Preencha o e-mail num formato válido");
            form_01.email.focus();
            }
          return false;
       }
    }
    for (i=0; i<addr.length; i++) {
       if (addr.charCodeAt(i)>127) {
          if (db)
            {
            displayInlineError(form_01.email, "Preencha o e-mail num formato válido");
            form_01.email.focus();
            }
          return false;
       }
    }
    
    var atPos = addr.indexOf('@',0);
    if (atPos == -1) {
          if (db)
            {
            displayInlineError(form_01.email, "Preencha o e-mail num formato válido");
            form_01.email.focus();
            }
       return false;
    }
    if (atPos == 0) {
          if (db)
            {
            displayInlineError(form_01.email, "Preencha o e-mail num formato válido");
            form_01.email.focus();
            }
       return false;
    }
    if (addr.indexOf('@', atPos + 1) > - 1) {
          if (db)
            {
            displayInlineError(form_01.email, "Preencha o e-mail num formato válido");
            form_01.email.focus();
            }
       return false;
    }
    if (addr.indexOf('.', atPos) == -1) {
          if (db)
            {
            displayInlineError(form_01.email, "Preencha o e-mail num formato válido");
            form_01.email.focus();
            }
       return false;
    }
    if (addr.indexOf('@.',0) != -1) {
          if (db)
            {
            displayInlineError(form_01.email, "Preencha o e-mail num formato válido");
            form_01.email.focus();
            }
       return false;
    }
    if (addr.indexOf('.@',0) != -1){
          if (db)
            {
            displayInlineError(form_01.email, "Preencha o e-mail num formato válido");
            form_01.email.focus();
            }
       return false;
    }
    if (addr.indexOf('..',0) != -1) {
          if (db)
            {
            displayInlineError(form_01.email, "Preencha o e-mail num formato válido");
            form_01.email.focus();
            }
       return false;
    }
    var suffix = addr.substring(addr.lastIndexOf('.')+1);
    if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' & suffix != 'museum') {
       if (db) alert('invalid primary domain in email address');
       return false;
    }
}
