package OneUnified::ValidateFields;

# Created on 2007/02/28 by Ray Burkholder
# Copywrite 2007 One Unified

#use DBI qw(:sql_types);

use Email::Valid;

require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(validateEmailAddress validatePassword validateNewPasswords);

use strict;

sub validateEmailAddress ( $$ ) {

  my ( $strErrors, $fldEmailAddress ) = @_;
#      ref

  my $bValid = 1;

  if ( 0 == ( $fldEmailAddress cmp "" ) ) {
    $$strErrors .= "No Email Address provided.<br>\n";
    $bValid = 0;
    }
  if ( 100 < length( $fldEmailAddress ) ) {
    $$strErrors .= "Email address is too long.<br>\n";
    $bValid = 0;
    }
  if ( Email::Valid -> address( $fldEmailAddress ) ) {
    if ( not Email::Valid -> mx( $fldEmailAddress ) ) {
      $$strErrors .= "Email domain not found.<br>\n";
      $bValid = 0;
      }
    }
  else {
    $$strErrors .= "Improper email address.<br>\n";
    $bValid = 0;
    }

  return $bValid;

  }

sub validatePassword ($$$) {

  my ( $strErrors, $fldPassword, $sPosition ) = @_;
#      ref

  my $bValid = 1;

  if ( 0  == ( $fldPassword cmp "" ) ) {
    $$strErrors .= "$sPosition Password is blank.<br>\n";
    $bValid = 0;
    }
  if ( 32 < length( $fldPassword ) ) {
    $$strErrors .= "$sPosition password is too long.<br>\n";
    $bValid = 0;
    }

  return $bValid;

  }

sub validateNewPasswords ($$$) {

  my ( $strErrors, $fldPassword1, $fldPassword2 ) = @_;
#      ref

  my $bValid = 1;

  $bValid = 0 if not validatePassword( $strErrors, $fldPassword1, "First" );
  $bValid = 0 if not validatePassword( $strErrors, $fldPassword2, "Second" );

  if ( 0 != ( $fldPassword1 cmp $fldPassword2 ) ) {
    $$strErrors .= "Passwords do not match.<br>\n";
    $bValid = 0;
    }

  return $bValid;

  }

1;

