#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package Corp::Contact::Contact;

use strict;
use warnings;

use Mail::Mailer;

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::Util;
use Common::FormObject;
use base 'Common::FormObject';



sub _init
{
    my ($self, %args) = @_;

    $self->SUPER::_init(%args);

    my %properties;

    $self->_initProperties(\%properties);

    my $rval = $self->_initProperties(\%properties);

    return $rval;
}

sub _propertiesFromDefaults
{
    my ($self, $hrProperties) = @_;

}


sub _initProperties
{
    my ($self, $props) = @_;  
  
    $self->{Email}      = Common::FormObject::Scalar::EmailAddress->new();
    $self->{FirstName}  = Common::FormObject::Scalar::String->new();
    $self->{LastName}   = Common::FormObject::Scalar::String->new();
    $self->{Title}      = Common::FormObject::Scalar::String->new();
    $self->{Company}    = Common::FormObject::Scalar::String->new();
    $self->{Phone}      = Common::FormObject::Scalar::PhoneNumber->new();
    $self->{Website}    = Common::FormObject::Scalar::String->new();
    $self->{Business}   = Common::FormObject::Scalar::String->new();
    $self->{Type}       = Common::FormObject::Scalar::String->new();
    $self->{Interest}   = Common::FormObject::Scalar::String->new();
    $self->{Comments}   = Common::FormObject::Scalar::String->new();   

    return $self;
}


sub save
{
    my ($self) = @_;
		
    my $FILE = "/app/data/corp/contact.txt";
    if (! -e $FILE)
    {
        `touch $FILE`;
    }

    my $date = `/bin/date +%Y%m%d%H%M%S`;
    chomp $date;
    open(FILE, ">> $FILE") or die "couldn't open file: $!\n";
    print FILE $date . "\t";
    my $email = $self->{Email}->{_value};
    print FILE $email . "\t";
    my $firstname = $self->{FirstName}->{_value};
    print FILE $firstname . "\t";
    my $lastname = $self->{LastName}->{_value};
    print FILE $lastname . "\t";
    my $company = $self->{Company}->{_value};
    print FILE $company . "\t";
    my $title = $self->{Title}->{_value};
    print FILE $title . "\t";
    my $phone = $self->{Phone}->{_value};
    print FILE $phone . "\t";
    my $website = $self->{Website}->{_value};
    if ($website)
    {
    	$website = "http://" . $website if ($website !~ /^http:\/\//);
    }
    print FILE $website . "\t";
    my $business = $self->{Business}->{_value};
    print FILE $business . "\t";
    my $type = $self->{Type}->{_value};
    print FILE $type . "\t";
    my $interest = $self->{Interest}->{_value};
    print FILE $interest . "\t";
    my $comments = $self->{Comments}->{_value};
    $comments =~ s/\r//g;
    $comments =~ s/\n/<br>/g;
    print FILE $comments . "\n";
    close FILE;
    
    _sendEmail($email, $firstname, $lastname, $company, $title, $phone, $website, $business, $type, $interest, $comments);

}

sub _sendEmail
{
	my ($email, $firstname, $lastname, $company, $title, $phone, $website, $business, $type, $interest, $comments) = @_;

	$comments =~ s/<br>/\n/g;

	my ($mailer) = new Mail::Mailer qw(sendmail);

	my (%headers) = ( 'To'      => 'webcontacts@royaltyshare.com', 
					  'From'    => 'noreply@royaltyshare.com',
					  'Subject' => "New Contact Request - $company",
					  );

	$mailer->open( \%headers );

	print $mailer <<EOM;

Email: $email     
First Name: $firstname 
Last Name: $lastname  
Title: $title     
Company: $company   
Phone: $phone     
Website: $website   
Business: $business 
Type: $type      
Interest: $interest 
Comments: $comments   
   
EOM

	$mailer->close;

	return 1;
}


sub validate
{
	  my ($self, %args) = @_;

	  my $valid = $self->SUPER::validate(%args);
	
    my ($email) = $self->{Email}->{_value};


    if ($email) {

        # Simple domain name/host validation
        my @fields = split('@', $email);
        if( $#fields <= 0 ) {
            $self->{Email}->setError("E-mail Address must be in user\@host format.");
            $valid = 0;
        }

        my $i;
        for ( $i = 1; $i <= $#fields; $i++ ) {
            my @names = reverse split( /\./, lc( $fields[$#fields] ) );

            unless( $names[0] =~ m/^[a-z]{2,6}$/ ) {
            	  $self->{Email}->setError("E-mail Address has an invalid top-level domain name.");
                $valid = 0;
            }

            my $host;
            foreach $host ( @names ) {
                unless( $host =~ m/^[a-z0-9][-_a-z0-9]{0,31}$/ ) {
                	  $self->{Email}->setError("E-mail Address has an invalid host name.");
                    $valid = 0;
                }
            }
        }

        # Check username for reasonableness
        if ( $fields[0] =~ /([^-A-Za-z0-9%&*_+=|:.?\/])/) {
        	  $self->{Email}->setError("Illegal character '$1' in E-mail Address.");
            $valid = 0;
        }

    }
    else
    {
    	  $self->{Email}->setError("E-mail address is required.");
        $valid = 0;
    }

    return $valid;

}


1;

