// JavaScript Document
//
// =========================================================
// File Name: validacion.js
// Created: Date 03-Sep-2004
// Programmer: Mike Tirado <mtirado@edaconsultores.com>
// Description: JavaScript functions for validating Forms
//
// =========================================================
// Copyright (C) 2004 Mike Tirado
// =========================================================

//global variable for error flag
var errfound = false;

// display an error alert
function error(elem, text) {
   if (errfound) return;
   window.alert(text);
   elem.select();
   elem.focus();
   errfound = true;
}

function ValidLength(item, len) {
   return (item.length >= len);
}

function Compare(item1,item2) {
	return (item1==item2);
}

function valida_contactos_form() {
error(document.formaContacto.nombre, "Ingrese su nombre.");
}

// contactos_form.asp - Chequear nombre,
function Salida_contactos_form() {
errfound = false;
	if (!ValidLength(document.formaContacto.nombre.value, 1)) 
		error(document.formaContacto.nombre, "Ingrese su nombre.");
	return !errfound;  
	if (!ValidLength(document.formaContacto.email.value, 1)) 
		error(document.formaContacto.email, "Ingrese Email.");
	return !errfound;
	
// true if there are no errors
}

function emailValido(theElement, theElementName)
{
  var s = theElement.value;

  //E-mail address format - localpart@domain
  //The localpart should not contain the following characters - <>()[]\,;:@"<blank space character> and if the localpart includes
  // a period, it should be followed by any character other than those mentioned above. However, special characters are allowed provided
  // the entire local part is enclosed by double quotes e.g. "()[]"@domain
  var localPartfilter1 = /^[^<>()\[\]\x5C.,;:@" ]+(\.[^<>()\[\]\x5C.,;:@" ]+)*@$/;
  var localPartfilter2 = /^"[^\r\n]+"@$/;

  //The domain should start and end with an alphanumeric character and any period should be enclosed by an alphanumeric character.
  //Characters allowed in the domain are a-z A-Z 0-9 and -(hyphen)
  var domainfilter = /^([a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]|[a-zA-Z0-9])(\.([a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]|[a-zA-Z0-9]))*$/;
  var sepPos = 0;
  var localPart;
  var domain;
  var localPartOk = false;
  var domainOk    = false;
  sepPos = s.lastIndexOf("@");
  localPart = s.substring(0,sepPos+1);
  domain    = s.substring(sepPos+1,s.length);

  if  (localPartfilter1.test(localPart))
      localPartOk = true;
  else if (localPartfilter2.test(localPart))
      localPartOk = true;
  else
      localPartOk = false;

  if (domainfilter.test(domain))
      domainOk = true;
  else
      domainOk = false;

  if ( (localPartOk==true && domainOk==true)||(s=="") )
      return true;
  else
      alert(" El campo "+ theElementName + " debe ser una dirección de correo válida. " );
  theElement.focus();
  theElement.select();
  return false;
}


// EOF JavaScript functions
