// Email Validation Javascript
// copyright 23rd March 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

// ejemplo: validateEmail(form.email.value,1,1)

function validateEmail(addr,man,db) {
    
    strResult = 'OK';
    
    if (addr == '' && man) {
       if (db) strResult = 'Es obligatorio rellenar el campo correo eléctronico';
       return strResult;
    }
    if (addr == '') return strResult;
    var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
    for (i=0; i<invalidChars.length; i++) {
       if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
          if (db) strResult = 'El correo eléctronico contiene caracteres inválidos';
          return strResult;
       }
    }
    for (i=0; i<addr.length; i++) {
       if (addr.charCodeAt(i)>127) {
          if (db) strResult = "El correo eléctronico contiene caracteres inválidos.";
          return strResult;
       }
    }

    var atPos = addr.indexOf('@',0);
    if (atPos == -1) {
       if (db) strResult =  'El correo eléctronico debe contener el caracter @';
       return strResult;
    }
    if (atPos == 0) {
       if (db) strResult = 'El correo eléctronico no puede empezar con @';
       return strResult;
    }
    if (addr.indexOf('@', atPos + 1) > - 1) {
       if (db) strResult = 'El correo eléctronico debe tener un formato válido';
       return strResult;
    }
    if (addr.indexOf('.', atPos) == -1) {
       if (db) strResult = 'El correo eléctronico debe tener un formato válido';
       return strResult;
    }
    if (addr.indexOf('@.',0) != -1) {
       if (db) strResult = 'El correo eléctronico debe tener un formato válido';
       return strResult;
    }
    if (addr.indexOf('.@',0) != -1){
       if (db) strResult = 'El correo eléctronico debe tener un formato válido';
       return strResult;
    }
    if (addr.indexOf('..',0) != -1) {
       if (db) strResult = 'El correo eléctronico debe tener un formato válido';
       return strResult;
    }

    return strResult;
}
