//Função usada para recuperar os dados dos formulário. Sem precisar repetir toda vez o comando
function $m(elemento)
{
	return document.getElementById(elemento);
}
//Função que valida se o e-mail informado está no formato válido ou não.
function checkMail(mail)
{
	var er = new RegExp(/^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/);
	if(typeof(mail) == "string")
	{
		if(er.test(mail))
		{
			return true;
		}
    }
	else if(typeof(mail) == "object")
	{
    	if(er.test(mail.value))
		{ 
        	return true; 
		}
    }
	else
	{
		return false;
	}
}

function limparCampo(campo)
{
    if (campo.value == campo.defaultValue)
    {
        campo.value = "";
    }
} 
function escreverCampo(campo)
{
    if (campo.value == "")
    {
        campo.value = campo.defaultValue;
    }
}

//Função que só vai permitir ser digitado números, para chamar essa função: onKeyUp="soNumeros(this);"
function soNumeros(campo)
{
    var digits="0123456789"
    var campo_temp 
    for (var i=0;i<campo.value.length;i++)
	{
    	campo_temp=campo.value.substring(i,i+1)    
		if (digits.indexOf(campo_temp)==-1)
		{
			campo.value = campo.value.substring(0,i);
            break;
		}
	}
}

//Função usada para vericar se todos os campos da data doselecionados
/*
    msg é a mensagem a ser concatenada na exibição
    tipo é para verificar como será a validação, se todos a data é obrigatorio (ob)) ou opcial (op)
*/
function checkData(dia_c,mes_c,ano_c,msg,tipo)
{
    if(msg != "")
        msg = " "+msg+".";
    else
        msg = ".";
    
    if(tipo == "ob")
    {
        if(!chkData(dia_c,mes_c,ano_c,msg))
            return false;
        else
            return true;
    }
    else if(tipo == "op")
    {
        if(dia_c.value != "" || mes_c.value != "" || ano_c.value != "")
        {
            if(!chkData(dia_c,mes_c,ano_c,msg))
                return false;
            else
                return true;
        }
    }
}
function chkData(dia_v,mes_v,ano_v,msg_v)
{
    if(dia_v.value == "")
    {
        alert("Selecione o Dia "+msg_v);
        dia_v.focus();
        return false;
    }
    else if(mes_v.value == "")
    {
        alert("Selecione o Mês "+msg_v);
        mes_v.focus();
        return false;
    }
    else if(ano_v.value == "")
    {
        alert("Selecione o Ano "+msg_v);
        ano_v.focus();
        return false;
    }
    return true;
}
//Função usada para vericar se o telefone foi preenchido corretamente
/*
    msg é a mensagem a ser concatenada na exibição
    tipo é para verificar como será a validação, se todos a data é obrigatorio (ob)) ou opcial (op)
*/
function checkTel(ddd,ntelefone,msg,tipo)
{
    if(tipo == "ob")
    {
        if(chkTel(ddd,ntelefone,msg))
            return true;
        else
            return false;
    }
    else if(tipo == "op")
    {
        if(ddd.value != "" || ntelefone.value != "")
        {
            if(chkTel(ddd,ntelefone,msg))
                return true;
            else
                return false;
        }
    }
}
function chkTel(ddd_v,ntelefone_v,msg_v)
{
    if(ddd_v.value == ddd_v.defaultValue)
    {
        alert("Informe o DDD " + msg_v);
        ddd_v.focus();
        return false;
    }
    if(removeE(ddd_v.value) <= 0)
    {
        alert("Informe o DDD " + msg_v);
        ddd_v.focus();
        return false;
    }
    else if(ddd_v.value.length < 2)
    {
        alert("DDD do " + msg_v + " deve ter 2 (dois) dígitos.");
        ddd_v.focus();
        return false;
    }
    else if(ntelefone_v.value == ntelefone_v.defaultValue)
    {
        alert("Informe o Número do " + msg_v);
        ntelefone_v.focus();
        return false;
    }
    else if(removeE(ntelefone_v.value) <= 0)
    {
        alert("Informe o Número do " + msg_v);
        ntelefone_v.focus();
        return false;
    }
    else if(ntelefone_v.value.length < 8)
    {
        alert("Número do " + msg_v + " deve ter 8 (oito) dígitos.");
        ntelefone_v.focus();
        return false;
    }
    return true;
}

//Função para validar preenchimento da localização
function checkEndereco(cep,logradouro,numero,bairro,uf,municipio)
{
    if(cep.value == "")
    {
        alert("O campo CEP é obrigatório.");
        cep.focus();
        return false;
    }
    if(cep.value.length < 8)
    {
        alert("CEP deve ter 8 (oito) dígitos.");
        cep.focus();
        return false;
    }
    if(logradouro.value == "")
    {
        alert("O campo Logradouro é obrigatório.");
        logradouro.focus();
        return false;
    }
    if(numero.value == "")
    {
        alert("O campo Número (Nº) é obrigatório.");
        numero.focus();
        return false;
    }
    if(bairro.value == "")
    {
        alert("O campo Bairro é obrigatório.");
        bairro.focus();
        return false;
    }
    if(uf.value == "")
    {
        alert("Selecione a UF.");
        uf.focus();
        return false;
    }
    if(municipio.value == "")
    {
        alert("O campo Município é obrigatório.");
        municipio.focus();
        return false;
    }
    return true;
}

//Função para não permitir acentos
function semAcento(obj)
{
    var str         = new String(obj.value);
    var acentos     = new String('àâêôûãõáéíóúçüÀÂÊÔÛÃÕÁÉÍÓÚÇÜ {}[]');
    var SemAcento   = new String('aaeouaoaeioucuAAEOUAOAEIOUCU_');
    var c           = new String();
    var i           = new Number();
    var x           = new Number();
    var res         = '';
    
    for (i = 0; i<str.length; i++)
    {
        c = str.substring(i,i+1);
        for (x=0; x< acentos.length; x++)
        {
            if(checkString(c))
            {
                if (acentos.substring(x,x+1) == c)
                {
                    c = SemAcento.substring(x,x+1);
                }
            }
            else
            {
                c = acentos.substring(x,x-1)
            }
        }
        res += c;
    }
    obj.value = res;
}

//Função para checar caracteres
function checkString(str)
{
	var re;
	re = /[\\/:*?"<>|!@#$%&()¨¬+=§ªº°;,'¹²³]/gi;
	if (re.test(str))
		return false;	
	else 
		return true;
}

function removeE(string)
{
    var re;
	re      = /[\\/ ]/gi;
    string  = limparstring(string);
	/*if (re.test(string))
        if(string.length <=0)
		  return 0;
    }	*/
	
	return string.length;
   /* var texto  = string.replace(' ',"");
    return texto;*/
}

function limparstring(s)
{
    while (s.search(" ")!=-1)
    {
        s   = s.replace(" ","");
    }
    return(s);
}
