// Inicio da função TestaComboSelecionado
function TestaComboSelecionado(combo)
{
   indice = combo.selectedIndex; 
   selecionado = combo.options[indice].value; 
   return selecionado;
}
// Final da função TestaComboSelecionado

// Inicio da função TestaRadioSelecionado
function TestaRadioSelecionado(radio)
{
    selecionado = "";
    if (!(radio.length >= 1)) 
    {
        if (radio.checked) 
            selecionado = radio.value;
        else
            selecionado = 0;
    }
    for (cont = 0; cont < radio.length; cont++)
    {
        if (radio[cont].checked) 
        {
            selecionado = radio[cont].value; 
            break;
        } 
        else
            selecionado = 0;
    }
    return selecionado;
} 
// Final da função TestaRadioSelecionado

// Inicio da Função Trim
function Trim(Objeto) {
    var pos = 0; 
    var str = Objeto.value.substring(pos, pos + 1);
    var cont = 0; 
    var straux = Objeto.value;

    while ((str == " ") && (cont <= Objeto.value.length)) {
        pos = pos + 1;
        straux = Objeto.value.substring(pos, Objeto.value.length); 
        str = Objeto.value.substring(pos, pos + 1); 
        cont = cont + 1;
    }

    var String = straux;
    pos = String.length;
    str = String.substring(pos - 1, pos);
    cont = 0;
    while ((str == " ") && (cont <= String.length)) {
        pos = pos - 1;
        straux = String.substring(0, pos);
        str = String.substring(pos - 1, pos);
        cont = cont + 1;
    }
    return straux;
}
// Final da Função Trim

// Inicio da Função JavaTrim
function JavaTrim(String) {
    pos = 0; 
    str = String.substring(pos,pos+1); 
    cont = 0; 
    straux = String;

    while ((str==" ")&&(cont<=String.length)) {
        pos = pos + 1;
        straux = String.substring(pos, String.length); 
        str = String.substring(pos,pos+1); 
        cont = cont + 1;
    }

    String = straux;
    pos = String.length;
    str = String.substring(pos-1,pos);
    cont = 0;
    while ((str==" ")&&(cont<=String.length)) {
        pos = pos - 1;
        straux = String.substring(0, pos);
        str = String.substring(pos-1,pos);
        cont = cont + 1;
    }
    return straux;
}
// Final da Função JavaTrim

// Inicio da função TestaDataDividida
function TestaDataDividida(dia1, mes1, ano1, desData)
{
    dia = dia1.value;
    mes = mes1.value;
    ano = ano1.value;
    msgErro = '';
    if ( (isNaN(dia)) || (isNaN(mes)) || (isNaN(ano)) || (ano==0) ) 
    {
        alert(desData + ' inválida, por favor, digite novamente.');
        dia1.value = '';
        dia1.focus();
        return false;
     }
  
    if ( (dia=="") && (mes=="") && (ano=="") ) 
    {
        alert('Por favor, informe corretamente a ' + desData + '.');
        dia1.value = '';
        dia1.focus();
        return false;
    }  
    else
    {           
        if ( (dia=="") || (mes=="") || (ano=="") ) 
        {
            alert('Formato inválido no campo ' + desData + '. \nPor favor, digite novamente.');
            dia1.value = '';
            dia1.focus();
            return false;
        } 
        else
        {
            if ((dia>31)||(dia<1))
            {
                alert('Dia inválido no campo ' + desData + '.');
                dia1.value = '';
                dia1.focus();
                return false;
            }
            if ((mes>12)||(mes<1))
            {
                alert('Mês inválido no campo ' + desData + '. ');
                mes1.value = '';
                mes1.focus();
                return false;
            }
            if (ano.length < 4) 
            {
                alert('Ano inválido no campo ' + desData + '. ');
                ano1.value = '';
                ano1.focus();
                return false;
            }   
            if (mes==2)
            {
                if (((dia)>29)||(dia=='29' && (ano)%4!=0)) 
                {

                    alert('Dia inválido no campo ' + desData + '. ');
                    dia1.value = '';
                    dia1.focus();
                    return false;
                }
            }
            else
            {
                if ( ( (mes==4) || (mes==6) || (mes==9) || mes=='11') && (dia>30) ) 
                {
                    alert('Dia inválido no campo ' + desData + '. ');
                    dia1.value = '';
                    dia1.focus();
                    return false;
                }    
                else
                { 
                    if (dia>31)
                    {
                        alert('Dia inválido no campo ' + desData + '.');
                        dia1.value = '';
                        dia1.focus();
                        return false;
                    }  
                }    
            } 
        }
    }
    return true;

}
// Final da função TestaDataDividida
// Inicio da função DataMaiorDividida
function DataMaiorDividida(dia1, mes1, ano1, dia2, mes2, ano2)
{
    data1= new Date(ano1,mes1,dia1)
	data2= new Date(ano2,mes2,dia2)
 
    if (data1 < data2) 
    {   
        return 1;
    }
    if (data1 > data2) 
    {   
        return 2;
    }
    else
    {   
        return 0;
    }    
}
// Fim da função DataMaiorDividida

// Inicio da função ChecaEmail

function ChecaEmail (emailStr, Mensagem) {
    var emailPat=/^(.+)@(.+)$/;
    var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
    var validChars="\[^\\s" + specialChars + "\]";
    var quotedUser="(\"[^\"]*\")";
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
    var atom=validChars + '+';
    var word="(" + atom + "|" + quotedUser + ")";
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
    var matchArray=emailStr.match(emailPat);
    if (matchArray==null) {
        alert(Mensagem + ' incorreto. Por favor, digite novamente.');
        return false;
    }
    var user=matchArray[1];
    var domain=matchArray[2];
    if (user.match(userPat)==null) {
        alert(Mensagem + ' incorreto. Por favor, digite novamente.')
        return false;
    }
    var IPArray=domain.match(ipDomainPat);
    if (IPArray!=null) {
        for (var i=1;i<=4;i++) {
            if (IPArray[i]>255) {
                alert(Mensagem + ' incorreto. Por favor, digite novamente.');
                return false;
            }
        }
        return true;
    }
    var domainArray=domain.match(domainPat);
    if (domainArray==null) {
        alert(Mensagem + ' incorreto. Por favor, digite novamente.');
        return false;
    }
    var atomPat=new RegExp(atom,"g");
    var domArr=domain.match(atomPat);
    var len=domArr.length;
    if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
       alert(Mensagem + ' incorreto. Por favor, digite novamente.');
       return false;
    }
    if (len<2) {
        var errStr=Mensagem + ' incorreto. Por favor, digite novamente.';
        alert(errStr);
        return false;
    }
    return true;
}
// Fim da função ChecaEmail


// Inicio da Função ChecaCep
function ChecaCep(cep, tipo)
{
    if (tipo == 1)
    {
        if (cep.value.length != 8) 
        {
            alert('Cep inválido. Por favor, digite novamente.');
            cep.focus();
            return false;            
        }
        if (isNaN(cep.value))
        {
            alert('Cep inválido. Por favor, digite novamente.');
            cep.focus();
            return false;
        }
        return true;
    }
    else
    {
        if (cep.value.length != 9) 
        {
            alert('Cep inválido. Por favor, digite novamente.');
            cep.focus();
            return false;            
        }
        cep1 = cep.value.substring(0,5);
        cep2 = cep.value.substring(6,9);
        traco = cep.value.substring(5,6);
        if (isNaN(cep1) || isNaN(cep2))
        {
            alert('Cep inválido. Por favor, digite novamente.');
            cep.focus();
            return false;            
        }
        if (traco != '-')
        {
            alert('Cep inválido. Por favor, digite novamente.');
            cep.focus();
            return false;            
        }
        return true;
    }
}

// Inicio da função Numerico
function Numerico(objValor, iDecimais) {
    var sValor = objValor.value.replace(",", ".");

    if (isNaN(sValor)) {
        alert("O valor informado não é válido.");
        objValor.focus();
        return false;
    }

    // Coloca o número de decimais informado
    if (sValor.indexOf(".") != -1) {
        sInteiros = sValor.substring(0, sValor.indexOf("."));
        sDecimais = sValor.substring(sValor.indexOf(".") + 1, sValor.length);
    } else {
        sInteiros = sValor;
        sDecimais = "";

    }
    if (sInteiros == "") {
        sInteiros = "0";
    }
    for (i = 0; i < iDecimais; i++) {
        sDecimais = sDecimais + "0";
    }

    // Pega somente o número de decimais informado
    sDecimais = sDecimais.substring(0, iDecimais);
    
    if (iDecimais > 0) {
        objValor.value = sInteiros + "." + sDecimais;
    } else {
        objValor.value = sInteiros;
    }
    
    return true;
}
// Fim da função Numerico


