function IsEmail(email)
{
// Verifica se o texto recebido é um endereço de e-mail válido e retorna true ou false.
// Ex.: if (!IsEmail(document.form.email.value)) {...}
// Willian Cruz, 03/Out/02 - wcruz@pobox.com
    // Não pode ser vazio
    email = trim(email);
    if (email == '') {
        return false;
    }
    // Procura caracteres inválidos
    Invalid  = " /:,;";
    for (i=0; i<Invalid.length; i++) {
        if (email.indexOf(Invalid.charAt(i),0) > -1) {
            return false;
        }
    }
    // Não pode começar com www
    upper = email;
    upper.toLowerCase;
    if (upper.substr(0,3) == 'www') {
        return false;
    }
    // Outras checagens
    Arroba   = email.indexOf('@');
    Arroba2  = email.lastIndexOf('@');
    Ponto    = email.lastIndexOf('.');
    if (    (Arroba < 1)        ||  // '@' nao pode ser o primeiro caractere
        (Arroba != Arroba2)     ||  // não pode ter duas '@'
        (Ponto <= Arroba+1) ||  // tem que haver pelo menos um caractere valido entre '@' e '.'
        (Ponto >= email.length-2 )  // tem que haver pelo menos dois caracteres após o último '.'
        ) {
            return false;
    }
    return true;
}

function trim(texto) {
    while (texto.length > 0 && texto.charAt(0) == ' ') {
        texto = texto.substr(1);
    }
    while (texto.length > 0 && texto.charAt(texto.length-1) == ' ') {
        texto = texto.substr(0,texto.length-1);
    }
    return texto;
}

function textCounter(field, maxlimit) {
    if (field.value.length > maxlimit)
        field.value = field.value.substring(0, maxlimit);
}


function y2k(number) { return (number < 1000) ? number + 1900 : number; }


function isValidDate (myDate,sep) {
var reason = '';
// checks if date passed is in valid dd/mm/yyyy format
    if (myDate.length == 10) {
        if (myDate.substring(2,3) == sep && myDate.substring(5,6) == sep) {
            var date  = myDate.substring(0,2);
            var month = myDate.substring(3,5);
            var year  = myDate.substring(6,10);

            var test = new Date(year,month-1,date);

            if (year == y2k(test.getYear()) && (month-1 == test.getMonth()) && (date == test.getDate())) {
                reason = '';
                return true;
            }
            else {
                reason = 'data inválida';
                return false;
            }
        }
        else {
            reason = 'use o formato dd/mm/aaaa';
            return false;
        }
    }
    else {
        reason = 'tamanho inválido';
        return false;
    }
}
