// ***************************************************************************************
// Cliente                 : Carolina Caceres
// Programación            : John David Torres Barreto
// Lenguaje de Programación: Javascript
// Entorno                 : Internet
// Archivo                 : api_form.js
// Objetivo                : Api de funciones para validación de formulario
//
// Inicio        : 30/09/2008
// Modificaciones: 
// ****************************************************************************************
// Copyright: El presente código está protegido por las leyes de propiedad intelectual.
// 
// ****************************************************************************************
//
// www.estrategiasi.com.ar
// John David Torres Barreto
// Castelar 2935 (1714) Ituzaingó. Provincia de Buenos Aires. República Argentina
// webmaster@ituzaingo-baires.com.ar / webmaster@estrategiasi.com.ar
// Tel.: +54 - 11 - 4621-5459
//
// ****************************************************************************************  

  function validaForm() {
    var nomape = document.getElementById("Name");
    var insti  = document.getElementById("Company_Institution");
    var ciuda  = document.getElementById("City");
    var pais   = document.getElementById("Country");
    var tel    = document.getElementById("Telefono");
    var fax    = document.getElementById("Fax");
    var correo = document.getElementById("Email");
    var motivo = document.getElementById("Subject_Message");
    var texto  = document.getElementById("Message");
    
    var uno    = "The field ";
    var dos    = "\nis required.";
    var patron = /^([0-9])*$/;
    
    if (nomape.value == "") {
      mensa = uno + "Name" + dos;
      window.alert(mensa);
      nomape.focus();
      return false;
    }
    if (insti.value == "") {
      mensa = uno + "Company_Institution" + dos;
      window.alert(mensa);
      insti.focus();
      return false;
    }
    if (ciuda.value == "") {
      mensa = uno + "City" + dos;
      window.alert(mensa);
      ciuda.focus();
      return false;
    }
    if (pais.value == "") {
      mensa = uno + "Country" + dos;
      window.alert(mensa);
      pais.focus();
      return false;
    }
    if (tel.value != "") {
      coincideOk = patron.test(tel.value); 
      
      if (!coincideOk){ 
        mensa  = "The field Telephone accept only numeric values."; 
        window.alert(mensa);
        tel.focus();
        return false;
      } 
    }
    if (fax.value != "") {
      coincideOk = patron.test(fax.value); 
      
      if (!coincideOk){ 
        mensa  = "The field Fax accept only numeric values."; 
        window.alert(mensa);
        fax.focus();
        return false;
      } 
    }
    if (correo.value == "") {
      mensa = uno + "Email" + dos;
      window.alert(mensa);
      correo.focus();
      return false;
    } else {
      ok = validaMail(correo.value);
      
      if (!ok) {
        correo.focus();
        return false;
      }
    }
    if (motivo.value == "nada") {
      mensa = uno + "Subject_Message" + dos;
      window.alert(mensa);
      motivo.focus();
      return false;
    }
    if (texto.value == "") {
      mensa = uno + "Message" + dos;
      window.alert(mensa);
      texto.focus();
      return false;
    }
    
  }
  
  //////
  // Funciones de validación de correo electronico
  // ======================= VALIDA EMAIL ==============================

  function validaMail(cadMail) {
    var mensa = "Email Error";
    /* Verificar si el email tiene el formato user@dominio. */
    var emailPat = /^(.+)@(.+)$/;
  
    /* Verificar la existencia de caracteres. ( ) < > @ , ; : \ " . [ ] */
    var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
  	
    /* Verifica los caracteres que son válidos en una dirección de email */
    var validChars = "\[^\\s" + specialChars + "\]";
  
    var quotedUser="(\"[^\"]*\")";
  
    /* Verifica si la dirección de email está representada con una dirección IP Válida */ 
  
    var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
  
    /* Verificar caracteres inválidos */ 
  
    var atom    = validChars + '+';
    var word    = "(" + atom + "|" + quotedUser + ")";
    var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
  
    /* dominio, como opuesto a ipDomainPat, se muestra abajo. */
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
  
    var matchArray = cadMail.match(emailPat)
    if (matchArray == null) {
      window.alert(mensa);
      return false;
    }
    var user   = matchArray[1];
    var domain = matchArray[2];
  
    // Si el usuario es valido 
    if (user.match(userPat) == null) {
      // Si no
      window.alert(mensa);
      return false;
    }
  
    /* Si la dirección IP es válida */
    var IPArray = domain.match(ipDomainPat)
    if (IPArray != null) {
      for (var i = 1; i <= 4; i++) {
        if (IPArray[i] > 255) {
          window.alert(mensa);
          return false;
        }
      }
      return true;
    }
  
    var domainArray = domain.match(domainPat);
    if (domainArray == null) {
      window.alert(mensa);
      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) { 
      window.alert(mensa);
      return false;
    }
  
    if (len < 2) {
      var errStr = "Email Error";
      window.alert(mensa);
      return false;
    }
  
    // La dirección de email ingresada es Válida
    return true;
  }

// ============= FIN VALIDA EMAIL ======================== 

