function topo()
{
    window.scrollBy(0,-1000);
}

function mostra(msg)
{
    $('mensagens').innerHTML = msg;
    Element.show('mensagens');
    topo(); //rolar até o topo
}

function get_values(form)
{
    var obj = $(form);
    var getstr = '';
    for ( i = 0; i < obj.length; i++ )
        {
         if (obj[i].tagName == "INPUT")
            {
                if (obj[i].type == "text" || obj[i].type == "hidden")
                   {
                    getstr += obj[i].name + "=" + obj[i].value + "&";
                   }
                if (obj[i].type == "checkbox")
                   {
                    if (obj[i].checked)
                       {
                           getstr += obj[i].name + "=" + obj[i].value + "&";
                       }
                      else
                       {
                           getstr += obj[i].name + "=&";
                       }
                   }
                if (obj[i].type == "radio")
                   {
                     if (obj[i].checked)
                        {
                            getstr += obj[i].name + "=" + obj[i].value + "&";
                        }
                   }
            }
         if (obj[i].tagName == "TEXTAREA")
            {
                getstr += obj[i].name + "=" + obj[i].value + "&";
            }

         if (obj[i].tagName == "SELECT")
            {
               var sel = obj[i];
                getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
            }
        }
    return getstr;
}

function valida_campo(campo)
{
if ( ! valida_text(campo) )
   return form_error(mensagens[validar.indexOf(campo)],campo);
  else
   {
    form_limpa(campo);
    return true;
   }
}

function valida_cadastro(resp,funcao,form)
    {
        //Element.hide('erro_');
        $('submit').innerHTML='aguarde...';
        $('submit').disabled=true;

        $(form).blur();
        var dados = get_values(form);
        var erro = 'Os seguintes campos não estão preenchidos:<br />';
        var n = 0;

        validar.each(function(s) {
            val = valida_campo(s);
            if ( val != true )
               {
                n++;
                if ( n > 1 ) erro += ',';
                erro += val;
               }
        });

        if ( n == 0 )
           {
            eval(funcao+'(dados)')//cadastrar_aluno(dados);
            return false;
           }
           else
           {
            erro += '.';
            mostra(erro);
            topo();
            $('submit').innerHTML='Enviar';
            $('submit').disabled=false;
            return false;
           }
    }


/*
function limpatudo(nao)
{
    var obj = $(form);
    for ( i = 0; i < obj.length; i++ )
        {
         if (obj[i].tagName == "INPUT")
            {
                if (obj[i].type == "text")
                   {
                    getstr += obj[i].name + "=" + obj[i].value + "&";
                   }
                if (obj[i].type == "checkbox")
                   {
                    if (obj[i].checked)
                       {
                           getstr += obj[i].name + "=" + obj[i].value + "&";
                       }
                      else
                       {
                           getstr += obj[i].name + "=&";
                       }
                   }
                if (obj[i].type == "radio")
                   {
                     if (obj[i].checked)
                        {
                            getstr += obj[i].name + "=" + obj[i].value + "&";
                        }
                   }
            }
         if (obj[i].tagName == "SELECT")
            {
               var sel = obj[i];
                getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
            }
        }
}
*/


/**
* Função que valida CPF
* @param String $s CPF
* @return boolean
*/
function validaCpf(s) {
    if(s != "")
    {
        var cpf = s.replace(/\D/g,'');
        if (
    		cpf == '00000000000' ||
    		cpf == '11111111111' ||
    		cpf == '22222222222' ||
    		cpf == '33333333333' ||
    		cpf == '44444444444' ||
    		cpf == '55555555555' ||
    		cpf == '66666666666' ||
    		cpf == '77777777777' ||
    		cpf == '88888888888' ||
    		cpf == '99999999999' ||
    		cpf.length != 11
        )
    	{
    	    return false;
    	}
        var soma = 0;
        for ( var i = 10 ; i > 1; i--)
        {
            soma += cpf[10 - i] * i;
        }
        d1 = (soma % 11 < 2) ? 0 : (11 - (soma % 11));
        if (d1 != cpf[9])
        {
            return false;
        }
        soma = 0;
        for ($i=11 ; $i>1; $i--)
        {
            soma += cpf[11 - i] * i;
    	}
        d2 = (soma % 11 < 2) ? 0 : (11 - (soma % 11) );

        if (d2 != cpf[10])
        {
            return false;
        }
        return true;
    }
}

/**
* Função que valida CEP
* @param String $cep CEP
* @return boolean
*/
function validaCep(cep)
{
    if(cep != "")
    {
        expressao = /^[0-9]{5}-[0-9]{3}$/;
        if(!expressao.test(cep))
        {
            return false;
        }
        if(!cep.length == 9)
        {
            return false;
        }
        return true;
    }
}

/**
* Função que valida o fone
* @param String $value fone
* @return boolean
*/
function validaFone(value){
    if(value != "")
    {
        expressao = /^\([0-9]{2}\)[0-9]{4}-[0-9]{4}$/;
        if(!expressao.test(value))
        {
    	    return false;
    	}
        if(!value.length == 13)
        {
    	    return false;
    	}
        return true;
    }
}

/**
* Função que valida o e-mail
* @param String $value e-mail
* @return boolean
*/
function validaEmail(value) {
    expressao = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/i;
    if(!expressao.test(value))
    {
        return false;
    }
    return true;
}
