/**
 * JavaScript: index.js
 * Autor: Helio de Paula Nogueira
 * Email: helio.nogueira@unianhanguera.edu.br
 * Data: 01/03/2011
 * Revisao:
 * - 00: Inicial.
 *
 * Pagina que contem os "scripts" padrao.
 *
 */

/**
 * Funcaoo: Requisitos Minimos
 * - Popup com requisitos minimos do site.
 *
 */
function requisitosMinimos() {
    //Configuracao
    var specs = "scrollbars=0, status=0, width=400, height=330, top=160, left=450";
    var requisito = window.open("requisito", "requisito", specs, true);
    requisito.document.open();

    //add requisito
    requisito.document.writeln("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Frameset//EN\" \"http://www.w3.org/TR/html4/frameset.dtd\">");
    requisito.document.writeln("<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"pt-br\" xml:lang=\"pt-br\">");
    requisito.document.writeln("<head>");
    requisito.document.writeln("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
    requisito.document.writeln("<meta name=\"author\" content=\"H&eacute;lio de Paula Nogueira\">");
    requisito.document.writeln("<title>Requisitos M&iacute;nimos de Navegador</title>");
    requisito.document.writeln("<script type=\"text/javascript\" language=\"javascript\">");
    requisito.document.writeln("window.onblur = function(){window.close();};");
    requisito.document.writeln("</script>");
    requisito.document.writeln("<style type=\"text/css\" media='screen'>");
    requisito.document.writeln("body{font-family: 'Arial', sans-serif; font-size: 12px; padding:0 10px;}");
    requisito.document.writeln("img{border:none;}");
    requisito.document.writeln(".requisito #requisito_titulo{text-align: left; color: #162c46; font-weight: bold; font-size:18px; text-indent: 0;}");
    requisito.document.writeln(".requisito p{text-align: justify; text-indent: 50px; font-style: italic;}");
    requisito.document.writeln(".requisito #requisito_download{color:gray; font-style:italic;}");
    requisito.document.writeln(".requisito #requisito_navegador{margin-top:5px; padding-top:20px; border-top:1px solid gray; text-align:right;}");
    requisito.document.writeln(".requisito #requisito_explorer{float: left;}");
    requisito.document.writeln(".requisito #requisito_firefox{float: right;}");
    requisito.document.writeln("</style>");
    requisito.document.writeln("</head>");
    requisito.document.writeln("<body>");
    requisito.document.writeln("<div class='requisito'>");
    requisito.document.writeln("<p id='requisito_titulo'>REQUISITOS M&Iacute;NIMOS DE NAVEGADOR</p>");
    requisito.document.writeln("<p>Este site foi homologado para ser utilizado com a vers&atilde;o mais recente do <strong>Internet Explorer</strong> e <strong>Firefox</strong>. Os requisitos m&iacute;nimos de navegador para este site s&atilde;o o Internet Explorer 8.0 e vers&otilde;es posteriores ou o Firefox 3.6.13 e vers&otilde;es posteriores.</p>");
    requisito.document.writeln("<p>Se voc&ecirc; continuar acessando este site com vers&otilde;es mais antigas desses navegadores ou com outros navegadores al&eacute;m dos recomendados acima, as fun&ccedil;&otilde;es principais talvez n&atilde;o funcionem adequadamente.  Os links abaixo s&atilde;o fornecidos para que voc&ecirc; possa fazer download das vers&otilde;es mais recentes de qualquer dos navegadores recomendados.</p>");
    requisito.document.writeln("<div id='requisito_download'>download:</div>");
    requisito.document.writeln("<div id='requisito_navegador'>");
    requisito.document.writeln("<div id='requisito_explorer'><a title='Internet Explorer' href='http://www.microsoft.com/windows/ie/default.htm' target='_blank'><img alt='Internet Explorer' src='imagens/explorer.png' /></a></div>");
    requisito.document.writeln("<div id='requisito_firefox'><a title='Mozila Firefox' href='http://www.mozilla.com/' target='_blank'><img alt='FireFox' src='imagens/firefox.png' /></a></div>");
    requisito.document.writeln("</div>");
    requisito.document.writeln("</div>");
    requisito.document.writeln("</body>");
    requisito.document.writeln("</html>");

    //Sair
    requisito.document.close();
}

/**
 * Funcaoo: Valida Avaliacao
 * - Verifica se os campos do formularo foram preenchidos.
 *
 */
function validaAvaliacao() {
    var form = document.getElementById("formAcesso");
    
    //radio
    if(!form.pai_aluno.checked && !form.pai_funcionario.checked && !form.pai_professor.checked){
        $("#item_erro").html("Por favor preencha todos os campos.");
        return false;
    }

    //Verifica se os campos estão preenchidos
    for (i=0; i<form.length; i++){
        if(form[i].getAttribute("type") != "radio"
            && (form[i].value == ""
                || form[i].value == null
                || form[i].value == " digite o RA"
                || form[i].value == " sua senha da área restrita"
                || form[i].value == " sua data de nascimento")
            ){
            $("#item_erro").html("Por favor preencha todos os campos.");
            form[i].focus();
            return false;
        }
    }

    //Data Funcionario
    if(form.pai_funcionario.checked && form.pai_professor.checked){
        if(form.password.value.length < 10 || validaData(form.password)){
            $("#item_erro").html("Senha inv&aacute;lida.");
            form.password.focus();
            return false;
        }
    }

    //Caso tudo esteja preenchido
    return true;
}

/**
 * Funcao: Valida Estatisttica
 * - Verifica se os campos do formulario forma preechidos.
 *
 */
function validaEstatistica() {
    var form = document.getElementById("formEstatistica");
    if(form.pai_estatistica.value == "" || form.pai_estatistica.value == null){
        $("#item_erro").html("Por favor preencha o campo acima.");
        document.getElementById("pai_estatistica").focus();
        return false;
    }

    //Caso tudo esteja preenchido
    return true;
}

/**
 * Funcao: Valida Email
 * - Valida email caso esteja na primeira etapa.
 *
 */
function validaEmail() {
    var form = document.getElementById("formEmail");

    //registro
    if(form.email_registro.value == "" || form.email_registro.value == null){
        $("#item_erro").html("Por favor infome o RA.");
        form.email_registro.focus();
        return false;
    }

    //unidade
    if(form.email_unidade.value == "" || form.email_unidade.value == null){
        $("#item_erro").html("Por favor informe a unidade.");
        form.email_unidade.focus();
        return false;
    }

    //formato
    if(form.email_tipo_avaliacao.value == "" || form.email_tipo_avaliacao.value == null){
        $("#item_erro").html("Por favor informe a modelo do curso.");
        form.email_tipo_avaliacao.focus();
        return false;
    }

    //Caso tudo esteja preenchido
    return true;
}

/**
 * Funcao: Valida Reset
 * - Valida Reset caso esteja na primeira etapa.
 *
 */
function validaReset() {
    var form = document.getElementById("formReset");

    //registro
    if(form.reset_registro.value == "" || form.reset_registro.value == null){
        $("#item_erro").html("Por favor infome o RA.");
        form.reset_registro.focus();
        return false;
    }

    //cpf
    if(form.reset_cpf.value == "" || form.reset_cpf.value == null || form.reset_cpf.value.length < 14){
        $("#item_erro").html("CPF inv&aacute;lido.");
        form.reset_cpf.focus();
        return false;
    }

    //Nascimento
    if(form.reset_nascimento.value == "" || form.reset_nascimento.value == null || validaData(form.reset_nascimento) || form.reset_nascimento.value.length < 10){
        $("#item_erro").html("Data de nascimento inv&aacute;lida.");
        form.reset_nascimento.focus();
        return false;
    }

    //unidade
    if(form.reset_unidade.value == "" || form.reset_unidade.value == null){
        $("#item_erro").html("Por favor informe a unidade.");
        form.reset_unidade.focus();
        return false;
    }

    //formato
    if(form.reset_tipo_avaliacao.value == "" || form.reset_tipo_avaliacao.value == null){
        $("#item_erro").html("Por favor informe o modelo do curso.");
        form.reset_tipo_avaliacao.focus();
        return false;
    }

    //Caso tudo esteja preenchido
    return true;
}

/**
 * Funcaoo: Estatistica
 * - Cotrola a exibição das div da avalida e da estatistica.
 *
 */
function estatistica(conteudo) {
    //ocutal todas
    $("#index_conteudo").find("#avaliacao_acesso").css("display","none");
    $("#index_conteudo").find("#estatistica_acesso").css("display","none");
    $("#index_conteudo").find("#email_acesso").css("display","none");
    $("#index_conteudo").find("#reset_acesso").css("display","none");
    $("#index_conteudo").find("#mensagem_acesso").css("display","none");
    
    //ativa sessão
    $("#index_conteudo").find("#" + conteudo).css("display","block");

    //voltar para avaliacao

    //Seleciona a proxima acao
    if(conteudo == "avaliacao_acesso"){
        $("#estatistica_botao").find("a").html("acessar estat&iacute;sticas");
        $("#estatistica_botao").find("a").attr("href", "javascript:estatistica('estatistica_acesso')");
    } else {
        $("#estatistica_botao").find("a").html("acessar avalia&ccedil;&atilde;o");
        $("#estatistica_botao").find("a").attr("href", "javascript:estatistica('avaliacao_acesso')");
    }
}

/**
 * Funcaoo: Valida data
 * - Valida campos de data.
 *
 */
function validaData(campo){
    var hoje = new Date();
    var ano = hoje.getFullYear();
    var data = campo.value.split("/");
    
    if(campo.value.length > 10){
        return true;
    }

    //ANO INVALIDO
    if(data[2] < (ano-100)){
        return true;
    }

    //30 DIAS
    var trinta = new Array("02", "04", "06", "09", "11");
    for(i=0;i<trinta.length;i++){
        if((data[1] == trinta[i]) && (data[0] > 30)){
            return true;
        }
    }

    //31 DIAS
    if(data[0] > 31){
        return true;
    }

    return false;
}

/**
 * Funcao: Expressao Data
 * - Exressao regular para formatar data.
 *
 */
function expressaoData(data){
    document.getElementById(data).value = document.getElementById(data).value.replace(/\D/g,"");
    document.getElementById(data).value = document.getElementById(data).value.replace(/(\d{2})(\d)/,"$1/$2");
    document.getElementById(data).value = document.getElementById(data).value.replace(/(\d{2})(\d)/,"$1/$2");
    return;
}

//Apos carregar o documento
$(document).ready(function() {
    //TRATAR O CARACTER '
    $("body").find("input").blur(function (){
        var string = document.getElementById($(this).attr("id")).value;
        document.getElementById($(this).attr("id")).value = string.replace(/'/g, String.fromCharCode(180));
    });

    $("body").find("textarea").blur(function (){
        var string = document.getElementById($(this).attr("id")).value;
        document.getElementById($(this).attr("id")).value = string.replace(/'/g, String.fromCharCode(180));
    });

    //RADIO
    $("#formAcesso").find("input").click(function(){
        if($(this).attr("type") == "radio"){
            document.getElementById("formAcesso").submit();
        }
    });
    //OPTION
    var itens = new Array("pai_unidade", "pai_tipo_avaliacao", "pai_login", "email_unidade", "email_tipo_avaliacao",  "reset_unidade", "reset_tipo_avaliacao");
    for(i=0; i<itens.length; i++){
        $("#" + itens[i]).find("option").each(function(i){
            if(i > 0){
                $(this).css("color","black");
            }
        });
    }
    
    //UNIDADE
    $("#pai_unidade").change(function(){
        //if(!document.getElementById("pai_aluno").checked){
            document.getElementById("formAcesso").submit();
        //}
    });

    //RA
    $("#pai_login").keyup(function(){
        document.getElementById("pai_login").value=document.getElementById("pai_login").value.replace(/[^0-9\.\-]/g,"");
    });
    $("#pai_login").click(function(){
        document.getElementById("pai_login").value=document.getElementById("pai_login").value.replace(/[^0-9\.\-]/g,"");
    });

    //registro
    $("#email_registro").keyup(function(){
        document.getElementById("email_registro").value=document.getElementById("email_registro").value.replace(/[^0-9\.\-]/g,"");
        $(this).css("color","black");
    });
    $("#email_registro").click(function(){
        document.getElementById("email_registro").value=document.getElementById("email_registro").value.replace(/[^0-9\.\-]/g,"");
        $(this).css("color","black");
    });

    //registro
    $("#reset_registro").keyup(function(){
        document.getElementById("reset_registro").value=document.getElementById("reset_registro").value.replace(/[^0-9\.\-]/g,"");
        $(this).css("color","black");
    });
    $("#reset_registro").click(function(){
        document.getElementById("reset_registro").value=document.getElementById("reset_registro").value.replace(/[^0-9\.\-]/g,"");
        $(this).css("color","black");
    });

    //CPF
    $("#reset_cpf").keyup(function(){
        document.getElementById("reset_cpf").value=document.getElementById("reset_cpf").value.replace(/\D/g,"");
        document.getElementById("reset_cpf").value=document.getElementById("reset_cpf").value.replace(/^(\d{3})(\d)/,"$1.$2");
        document.getElementById("reset_cpf").value=document.getElementById("reset_cpf").value.replace(/^(\d{3})\.(\d{3})(\d)/,"$1.$2.$3");
        document.getElementById("reset_cpf").value=document.getElementById("reset_cpf").value.replace(/\.(\d{3})(\d)/,".$1-$2");
        $(this).css("color","black");
    });
    $("#reset_cpf").blur(function(){
        document.getElementById("reset_cpf").value=document.getElementById("reset_cpf").value.replace(/\D/g,"");
        document.getElementById("reset_cpf").value=document.getElementById("reset_cpf").value.replace(/^(\d{3})(\d)/,"$1.$2");
        document.getElementById("reset_cpf").value=document.getElementById("reset_cpf").value.replace(/^(\d{3})\.(\d{3})(\d)/,"$1.$2.$3");
        document.getElementById("reset_cpf").value=document.getElementById("reset_cpf").value.replace(/\.(\d{3})(\d)/,".$1-$2");
        $(this).css("color","black");
    });

    $("#formAcesso").find("input").focus(function(){
        if($(this).attr("type") == "text"){
            $(this).attr("value","");
            if($(this).attr("id") == "password" && document.getElementById("pai_aluno").checked){
                $(".pai_pass").html("<input name='pai_senha' id='password' type='password' size='25' value='' />");
                document.getElementById("password").focus();
            }
            $(this).css("color","black");
        }
    });
    $("#formAcesso").find("input").click(function(){
        if($(this).attr("type") == "text"){
            $(this).attr("value","");
            if($(this).attr("id") == "password" && document.getElementById("pai_aluno").checked){
                $(".pai_pass").html("<input name='pai_senha' id='password' type='password' size='25' value='' />");
                document.getElementById("password").focus();
            }
            $(this).css("color","black");
        }
    });

    //DATA
    $("#password").keyup(function (){
        //if(document.getElementById("pai_funcionario").checked || document.getElementById("pai_professor").checked){
            expressaoData("password");
        //}
    });
    $("#password").click(function (){
        //if(document.getElementById("pai_funcionario").checked || document.getElementById("pai_professor").checked){
            expressaoData("password");
        //}
    });

    //EMAIL
    $("#reset_nascimento").keyup(function (){
        expressaoData("reset_nascimento");
        $(this).css("color","black");
    });
    $("#reset_nascimento").blur(function (){
        expressaoData("reset_nascimento");
        $(this).css("color","black");
    });
    
    //item erro
    $("#index_conteudo").click(function (){
        $("#item_erro").html("");
    });
});
