###################################################################
# Copyright (C) 2006 RoyaltyShare, Inc.  All Rights Reserved
# $Id$
###################################################################
package Common::FormValidation;

#
# A set up routines to help validate form values.
# A failed validation will return the appropriate error_code.
# Error codes are not structured, so use whatever you want
# just make sure the template knows about it
#

use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::Consts;
use Common::Util;

use Exporter;
use vars qw(@ISA @EXPORT);

@EXPORT = qw(form_validate_password
  form_validate_id
  form_clean_validate_postal_code
  form_validate_postal_code
  form_validate_country
  form_validate_state
  form_validate_access
  form_clean_validate_phone
  form_clean_validate_fax
  form_validate_email
  form_clean_validate_text
  form_validate_text
);

#form_validate_phone # No longer used?
@ISA = 'Exporter';

# --------------------------
# Form validation
# --------------------------
sub form_validate_password {
    my %args = @_;
    my $pw   = $args{value};
    my $user = $args{user};

    # no spaces allowed
    return "password_spaces" if ( $pw =~ /\s/ );

    # between 8 and 20 characters
    return "password_length" if ( length($pw) < 8 || length($pw) > 20 );

    # come on people, get creative
    if ( lc $pw eq 'password' || lc $pw eq lc $user->FirstName || lc $pw eq lc $user->LastName ) {
        return "password_unsafe";
    }

    # hooray for valid passwords!
    return undef;
}

sub form_validate_id {
    my %args     = @_;
    my $id       = $args{value};
    my $required = $args{required};

    if ( $id eq '' ) {
        return ($required) ? "field_blank" : undef;
    }

    if ( $id !~ /^\d+$/ ) {
        return "field_invalid";
    }

    return undef;
}

sub form_clean_validate_postal_code {
    $_[0] =~ s/^\s*//g;
    $_[0] =~ s/\s*$//g;

    return "field_blank" if ( $_[0] eq '' && $_[2] );

    if ( $_[1] eq 'US' ) {

        # accept ##### or #####-####
        return "field_invalid" unless ( $_[0] =~ m/^(\d{5}(-\d{4})?)?$/ );
    } else {

        # accept anything under 15 characters
        return "field_too_long" if ( length( $_[0] ) > 15 );
    }

    # for non-US, 15 chars
    return undef;
}

sub form_validate_postal_code {
    my %args     = @_;
    my $postal   = $args{postal_code};
    my $country  = $args{country};
    my $required = $args{required};

    if ( $postal eq '' ) {
        return ($required) ? "field_blank" : undef;
    }

    if ( $country eq 'US' ) {

        # accept ##### or #####-####
        if ( $postal !~ /^\d{5}(-\d{4})?$/ ) {
            return "field_invalid";
        }
    } else {

        # accept anything under 15 characters
        return "field_too_long" if ( length($postal) > 15 );
    }

    # for non-US, 15 chars
    return undef;
}

# we should probably make sure the value
# can be looked up in that Countries
# hash in the Address class.
sub form_validate_country {
    my %args     = @_;
    my $country  = $args{value};
    my $required = $args{required};

    if ( $country eq '' ) {
        return ($required) ? "field_blank" : undef;
    }

    return undef;
}

# we should probably make sure the value
# can be looked up in that States
# hash in the Address class.
sub form_validate_state {
    my %args     = @_;
    my $state    = $args{value};
    my $required = $args{required};

    if ( $state eq '' ) {
        return ($required) ? "field_blank" : undef;
    }

    return undef;
}

sub form_validate_access {
    my %args     = @_;
    my $access   = $args{value};
    my $required = $args{required};

    if ( $access eq '' ) {
        return ($required) ? "field_blank" : undef;
    }

    if ( scalar( grep /$access/, keys %Common::Consts::PERMISSION_LABELS ) == 0 ) {
        return "field_invalid";
    }

    return undef;
}

sub form_clean_validate_phone {
    my $phone = shift;

    return "field_blank" if ( $phone eq '' && $_[0] );

    $phone =~ s/^\s+//;
    $phone =~ s/\s+$//;
    my ( $extension, $phone_number );
    $phone_number = $phone;

    #($phone_number,$extension) = split("x",$phone) if($phone =~ /x/i);
    #($phone_number,$extension) = split(/\s/,$phone) if ($phone =~ /\s/);
    #$phone_number =~ s/\D//g;
    #$extension =~ s/\D//g;
    #return "phone_bad_format" if (($phone_number !~ /([2-9][0-8][0-9][2-9][0-9]{2}[0-9]{4})/)&&($phone_number !~ /([0-9]{10}|[0-9]{11})/));
    #return "phone_bad_format" if (length $phone_number > 11);
    #return "phone_bad_format" if (length $phone_number < 10);
    return undef;
}

sub form_clean_validate_fax {
    my $phone = shift;

    return "field_blank" if ( $phone eq '' && $_[0] );

    $phone =~ s/^\s+//;
    $phone =~ s/\s+$//;
    my ( $extension, $phone_number );
    $phone_number = $phone;
    ( $phone_number, $extension ) = split( "x",  $phone ) if ( $phone =~ /x/i );
    ( $phone_number, $extension ) = split( /\s/, $phone ) if ( $phone =~ /\s/ );
    $phone_number =~ s/\D//g;
    $extension =~ s/\D//g;
    return "fax_bad_format"
      if ( ( $phone_number !~ /([2-9][0-8][0-9][2-9][0-9]{2}[0-9]{4})/ ) && ( $phone_number !~ /([0-9]{10}|[0-9]{11})/ ) );
    return "fax_bad_format" if ( length $phone_number > 11 );
    return "fax_bad_format" if ( length $phone_number < 10 );
    return undef;
}

# Not used?
sub form_validate_phone {
    my %args     = @_;
    my $phone    = $args{value};
    my $required = $args{required};

    if ( $phone eq '' ) {
        return ($required) ? "field_blank" : undef;
    }

    $phone = Common::Util::clean_phone($phone);
    return "field_too_short" if ( length($phone) < 7 );

    return undef;
}

sub form_validate_email {
    my $email = shift;

    print STDERR "trying to validate $email\n";

    return undef if ( $email =~ m/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$/ );

    return "email_address_invalid";
}

sub form_clean_validate_text {
    $_[0] =~ s/^\s*//;
    $_[0] =~ s/\s*$//;

    return "field_blank" if ( $_[0] eq '' && $_[1] );

    return undef;
}

sub form_validate_text {
    my %args     = @_;
    my $val      = $args{value};
    my $required = $args{required};

    if ( $val eq '' ) {
        return ($required) ? "field_blank" : undef;
    }

    $val =~ s/\s//g;

    if ( $val =~ /^..$/ ) {
        return "field_too_short";
    }

    return undef;
}

###
1;    # Play nicely.
###
