#!/usr/bin/perl5.00502

#===============================
# Filename: Registration.cgi
#===============================

# Registration CustomIncludes begin
use Common;
use strict "vars";

use Header;
use Footer;

# Registration CustomIncludes end
#-------------------------------


#===============================
# Save Page and File Name available into variables
#-------------------------------
my $sFileName = "Registration.cgi";
my $sTemplateFileName = "Registration.html";
#===============================


#===============================
# Registration PageSecurity begin
# Registration PageSecurity end
#===============================

#===============================
# Registration Open Event start
# Registration Open Event end
#===============================

#===============================
# Registration OpenAnyPage Start
# Registration OpenAnyPage End
#===============================

#-------------------------------
# Initialize error variables
#-------------------------------

my $sFormErr;

#===============================
# Display page
#-------------------------------
# Load HTML template for this page
#-------------------------------
$oTpl->LoadTemplate( $sAppPath . $sTemplateFileName, "main" );


#-------------------------------
# Load HTML template of Header and Footer
#-------------------------------
$oTpl->LoadTemplate( $sHeaderFileName, "Header" );
$oTpl->LoadTemplate( $sFooterFileName, "Footer" );
$oTpl->SetVar( "FileName", $sFileName );
$oTpl->SetVar( "url", $url );
#-------------------------------



#===============================
# Save the name of the form and type of action into the variables
#-------------------------------
my $sAction = GetParam("FormAction");
my $sForm = GetParam("FormName");

#-------------------------------
# Select the FormAction
#-------------------------------
#===============================

# Registration Show begin
  if ( $sForm eq "Form" ) {
    FormAction( $sAction ) }
#===============================

print $page_header;
print "Content-type: text/html\n\n";

#-------------------------------
# Step through each form
#-------------------------------

Header_Show();
Footer_Show();
Form_Show();

#-------------------------------
# Process the page templates
#-------------------------------
$oTpl->Parse( "Header", "False" );
$oTpl->Parse( "Footer", "False" );
$oTpl->Parse( "main", "False" );

#-------------------------------
# Output the page to the browser
#-------------------------------
print $oTpl->PrintVar("main");

# Registration Show end
# Registration Close Event start
# Registration Close Event end
exit 0;



#===============================
# Action of the Record Form
#-------------------------------
sub FormAction {
  my $sAction  = shift;
#-------------------------------
# Initialize variables  
#-------------------------------
  my $sSQL             = "";
  my $sRes             = "";
  my $bExecSQL         = 1;
  my $sActionFileName  = "";
  my $sWhere           = ""; 
  my $bErr             = 0;
  my $pPKmember_id = "";
  my $fldmember_login = "";
  my $fldmember_password = "";
  my $fldhonorific = "";
  my $fldfirst_name = "";
  my $fldlast_name = "";
  my $fldcomments = "";
  my $fldemail = "";
  my $fldurl = "";
  my $fldaddress = "";
  my $fldcity = "";
  my $fldstate_id = "";
  my $fldzip = "";
  my $fldcountry_id = "";
  my $fldphone_work = "";
  my $fldphone_mobile = "";
  my $fldfax = "";
  my $fldcompany = "";
  my $flddate_created = "";

#-------------------------------

#-------------------------------
# Form Action begin
#-------------------------------
  $sActionFileName = "/thanks.htm";

#-------------------------------
# Build WHERE statement
#-------------------------------
  if ( $sAction eq "update" or $sAction eq "delete" ) {
    my $pPKmember_id = GetParam("PK_member_id");
    if ( $pPKmember_id eq "") { return 0; }
    $sWhere = "member_id=" . ToSQL($pPKmember_id, "Number") . " ";
  }
#-------------------------------


#-------------------------------
# Load all form fields into variables
#-------------------------------
  my $fldmember_login = GetParam("member_login");
  my $fldsecurity_level_id = GetParam("membership");
  my $fldmember_password= crypt ($fldmember_login,"design");
  my $fldhonorific = GetParam("honorific");
  my $fldfirst_name = GetParam("first_name");
  my $fldlast_name = GetParam("last_name");
  my $fldcomments = GetParam("comments");
  my $fldcompany = GetParam("company");
  my $fldemail = GetParam("email");
  my $fldurl = GetParam("url_c");
  my $fldaddress = GetParam("address");
  my $fldcity = GetParam("city");
  my $fldstate_id = GetParam("state_id");
  my $fldzip = GetParam("zip");
  my $fldcountry_id = GetParam("country_id");
  my $fldphone_work = GetParam("phone_work");
  my $fldphone_mobile = GetParam("phone_mobile");
  my $fldfax = GetParam("fax");
  my $flddate_created = GetParam("date_created");
  my $i;

#-------------------------------

#-------------------------------
# Validate fields
#-------------------------------
  if ( $sAction eq "insert" or $sAction eq "update" ) {
#	$sFormErr = $sFormErr . "<font color=red>For Demo Review Only.</font><br>";
    if ( $fldmember_login eq "" ) {
      $sFormErr .= "The value in field Login* is required.<br>"
    }
    if ( $fldhonorific eq "" ) {
      $sFormErr .= "The value in field  is required.<br>"
    }
    if ( $fldfirst_name eq "" ) {
      $sFormErr .= "The value in field First Name* is required.<br>"
    }
    if ( $fldlast_name eq "" ) {
      $sFormErr .= "The value in field Last Name* is required.<br>"
    }
    if ( $fldemail eq "" ) {
      $sFormErr .= "The value in field Email* is required.<br>"
    }
    if ($fldemail =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(\.$)/ ||     
        ($fldemail !~ /^.+\@localhost$/ && 
         $fldemail !~ /^.+\@\[?(\w|[-.])+\.[a-zA-Z]{2,3}|[0-9]{1,3}\]?$/)) {
	$sFormErr .= "The value in field e-Mail* is incorrect.<br>";
    } 
    if ( $fldaddress eq "" ) {
      $sFormErr .= "The value in field Address* is required.<br>"
    }
    if ( $fldcity eq "" ) {
      $sFormErr .= "The value in field City* is required.<br>"
    }
    if ( $fldstate_id eq "" ) {
      $sFormErr .= "The value in field State* is required.<br>"
    }
    if ( $fldcountry_id eq "" ) {
      $sFormErr .= "The value in field Country* is required.<br>"
    }
    if ( $fldphone_work eq "" ) {
      $sFormErr .= "The value in field Phone* is required.<br>"
    }
    if ( !IsNumeric($fldhonorific) ) {
      $sFormErr .= "The value in field  is incorrect.<br>"
    }
    if ( !IsNumeric($fldcountry_id) ) {
      $sFormErr .= "The value in field Country* is incorrect.<br>"
    }
    if ( $fldmember_login ne ""  ) {
      my $iCount = 0;
      if ( $sAction eq "insert" ) {
        $iCount = DLookUp("members", "count(*)", "member_login=" . ToSQL($fldmember_login, "Text"));
      } elsif ( $sAction eq "update" ) {
        $iCount = DLookUp("members", "count(*)", "member_login=" . ToSQL($fldmember_login, "Text") . " and not(" . $sWhere . ")");
      }
      if ( $iCount > 0 ) {
        $sFormErr = $sFormErr . "The value in field Login* is already in database.<br>";
      }
    }
#-------------------------------
# Form Check Event start

# Form Check Event end
#-------------------------------

    if ( length($sFormErr) > 0 ) {
      return 0;
    }
  }
#-------------------------------

#-------------------------------
# Create SQL statement
#-------------------------------
  
  if ( $sAction eq "insert" ) {

#-------------------------------
# Form Insert Event start

# Form Insert Event end
#-------------------------------
       $sSQL = "insert into members (" . 
          "member_login," . 
          "member_password," . 
          "honorific," . 
          "first_name," . 
          "last_name," . 
          "comments," . 
          "company," . 
          "email," . 
          "url," . 
          "address," . 
          "city," . 
          "state_id," . 
          "country_id," . 
          "phone_work," . 
          "phone_mobile," . 
          "fax," . 
          "security_level_id," . 
          "date_created)" . 
          " values (" . 
          ToSQL($fldmember_login, "Text") . "," . 
          ToSQL($fldmember_password, "Text") . "," . 
          ToSQL($fldhonorific, "Number") . "," . 
          ToSQL($fldfirst_name, "Text") . "," . 
          ToSQL($fldlast_name, "Text") . "," . 
          ToSQL($fldcomments, "Memo") . "," . 
          ToSQL($fldcompany, "Text") . "," . 
          ToSQL($fldemail, "Text") . "," .
          ToSQL($fldurl, "Text") . "," . 
          ToSQL($fldaddress, "Text") . "," . 
          ToSQL($fldcity, "Text") . "," . 
          ToSQL($fldstate_id, "Text") . "," . 
          ToSQL($fldcountry_id, "Number") . "," . 
          ToSQL($fldphone_work, "Text") . "," . 
          ToSQL($fldphone_mobile, "Text") . "," . 
          ToSQL($fldfax, "Text") . "," . 
          ToSQL($fldsecurity_level_id, "Number") . "," . 
          ToSQL($flddate_created, "Text") . 
          ")";
  }
  if ( $sAction eq  "update" ) {
#-------------------------------
# Form Update Event start
# Form Update Event end
#-------------------------------

  }
#-------------------------------
#-------------------------------
# Form BeforeExecute Event start
# Form BeforeExecute Event end
#-------------------------------

#-------------------------------
# Execute SQL statement
#-------------------------------
if ( length($sFormErr) > 0 ) { return 0 }
  if ( $bExecSQL ) {
    exec_sql( $sSQL )
  }
  $sFormErr = ProcessError();
  if ( length($sFormErr) > 0 ) { return 0 }

  $sActionFileName = "/thanks.htm";

#------------------------
#Password Generate
#------------------------

my $body;
open(HTML,"< Confirmation.html");    
while (<HTML>) {
   my $text = $_;
   if ($text =~ /^#/) {
      # skip comment lines
      next;
   }
   while ($text =~ /(\$[a-zA-Z_]+)/) {
      # Evaluate the variables
      my $exec = $1;
      my $bla = eval $exec;
      $exec = "\\".$exec;
      $text =~  s/$exec/$bla/g;

   }    
$body .=$text;
}


my ($p,$l)=split("@",$fldemail);
my $emailto_ = $p . "\@" . $l;
my $email = $email_from;

    open(MAIL,"|'/usr/sbin/sendmail' -t");    
    print MAIL "To: $emailto_ ($fldfirst_name $fldlast_name)\n";
    print MAIL "From: $email\n";
    print MAIL "Content-Type: text/html; charset=us-ascii\n";
    
    # Change subject and message text here:
    print MAIL "Subject: Confirmation of Registration\n";    
    print MAIL "$body\n";
    close (MAIL);


  http_redirect( $sActionFileName);
#-------------------------------
# Form Action end
#-------------------------------
}
#===============================


#===============================
# Display Record Form
#-------------------------------
sub Form_Show {
#-------------------------------
# Form Show begin
#-------------------------------
  my $sWhere  = "";
  my $sFormTitle = "Registration";
  my $bIsUpdateMode;
  my $bPK = 1;
  my $pmember_id;
  my $flddate_created;
  my $fldmember_id;
  my $fldmember_login;
  my $fldmember_password;
  my $fldhonorific;
  my $fldfirst_name;
  my $fldlast_name;
  my $fldemail;
  my $fldurl;
  my $fldcomments;
  my $fldcompany;
  my $fldaddress;
  my $fldcity;
  my $fldstate_id;
  my $fldzip;
  my $fldcountry_id;
  my $fldphone_work;
  my $fldphone_mobile;
  my $fldfax;
  my $sOrder;

#-------------------------------
# Load primary key and form parameters
#-------------------------------
  if ( $sFormErr eq "" ) {
    $pmember_id = GetParam("member_id");
    $oTpl->SetVar( "FormError", "" );
  } else {
    $fldmember_id = GetParam("member_id");
    $fldmember_login = GetParam("member_login");
    $fldmember_password = GetParam("member_password");
    $fldhonorific = GetParam("honorific");
    $fldfirst_name = GetParam("first_name");
    $fldlast_name = GetParam("last_name");
    $fldemail = GetParam("email");
    $fldurl = GetParam("url_c");
    $fldaddress = GetParam("address");
    $fldcity = GetParam("city");
    $fldstate_id = GetParam("state_id");
    $fldzip = GetParam("zip");
    $fldcomments = GetParam("comments");
    $fldcompany = GetParam("company");
    $fldcountry_id = GetParam("country_id");
    $fldphone_work = GetParam("phone_work");
    $fldphone_mobile = GetParam("phone_mobile");
    $fldfax = GetParam("fax");
    $flddate_created = GetParam("date_created");
    $pmember_id = GetParam("PK_member_id");
    $oTpl->SetVar( "sFormErr", $sFormErr );
    $oTpl->SetVar( "FormTitle", $sFormTitle );
    $oTpl->Parse( "FormError", "False" )
  }
#-------------------------------

#-------------------------------
# Load all form fields

#-------------------------------

#-------------------------------
# Build WHERE statement

  $pmember_id = GetSessionVar("UserID");
  if ($pmember_id eq "") { $bPK = 0 }
    
  $sWhere .= "member_id=" . ToSQL($pmember_id, "Number");
  if ( $sWhere ne "" ) { $sWhere .= " and " }
  $oTpl->SetVar( "PK_member_id", $pmember_id );
#-------------------------------
#-------------------------------
# Form Open Event start
# Form Open Event end
#-------------------------------

  $oTpl->SetVar( "FormTitle", $sFormTitle);

#-------------------------------
# Build SQL statement and open recordset
#-------------------------------
  my $sSQL = "select * from members where " . $sWhere;
  # Open recordset
  my @db_rs = openrs($sSQL);
  my $ref_db_record = $db_rs[0];
  $bIsUpdateMode = ($bPK and !($sAction eq "insert" and $sForm eq "Detail") and $ref_db_record);
#-------------------------------
  if ($bIsUpdateMode) { 
    $flddate_created = GetValue($ref_db_record, "date_created");
    $fldmember_id = GetValue($ref_db_record, "member_id");
#-------------------------------
# Load data from recordset when form displayed first time
#-------------------------------
    if ( $sFormErr eq "" ) {
      $fldmember_login = GetValue($ref_db_record, "member_login");
      $fldmember_password = GetValue($ref_db_record, "member_password");
      $fldhonorific = GetValue($ref_db_record, "honorific");
      $fldfirst_name = GetValue($ref_db_record, "first_name");
      $fldlast_name = GetValue($ref_db_record, "last_name");
      $fldemail = GetValue($ref_db_record, "email");
      $fldurl = GetValue($ref_db_record, "url");
      $fldaddress = GetValue($ref_db_record, "address");
      $fldcity = GetValue($ref_db_record, "city");
      $fldstate_id = GetValue($ref_db_record, "state_id");
      $fldzip = GetValue($ref_db_record, "zip");
      $fldcountry_id = GetValue($ref_db_record, "country_id");
      $fldphone_work = GetValue($ref_db_record, "phone_work");
      $fldphone_mobile = GetValue($ref_db_record, "phone_mobile");
      $fldfax = GetValue($ref_db_record, "fax");
      $flddate_created = GetValue($ref_db_record, "date_created");
    }
    $oTpl->SetVar( "FormDelete", "" );
    $oTpl->SetVar( "FormInsert", "" );
    $oTpl->Parse ( "FormEdit", "False" );
#-------------------------------
# Form ShowEdit Event start
# Form ShowEdit Event end
#-------------------------------
  } else {
    if ( $sFormErr eq "" ) {
      $fldmember_id = ToHTML(GetSessionVar("UserID"));
    }
    $oTpl->SetVar( "FormEdit", "" );
    $oTpl->Parse( "FormInsert", "False" );
#-------------------------------
# Form ShowInsert Event start
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time());
$flddate_created= ($year+1900)."-".($mon+1)."-$mday $hour:$min";
# Form ShowInsert Event end
#-------------------------------
  }
  $oTpl->SetVar( "FormCancel", "" );
#-------------------------------
# Form Show Event start
# Form Show Event end
#-------------------------------

#-------------------------------
# Show form field
#-------------------------------
      $oTpl->SetVar( "member_id", ToHTML($fldmember_id));
      $oTpl->SetVar( "member_login", ToHTML($fldmember_login));
      $oTpl->SetVar( "member_password", ToHTML($fldmember_password));
  $oTpl->SetVar( "FormLBhonorific", "");
  my @honorific = openrs_arr( "select honorific_id, honorific_desc from lookup_honorifics order by 1" );
  foreach my $honorific ( @honorific ) {
    $oTpl->SetVar( "ID", $honorific->[0] );
    $oTpl->SetVar( "Value", $honorific->[1] );
    if ( $honorific->[0] eq $fldhonorific) { $oTpl->SetVar( "Selected", "SELECTED" ) } else { $oTpl->SetVar( "Selected", "" ) }
      $oTpl->Parse( "FormLBhonorific", "True");
    }
  
      $oTpl->SetVar( "first_name", ToHTML($fldfirst_name));
      $oTpl->SetVar( "last_name", ToHTML($fldlast_name));
      $oTpl->SetVar( "email", ToHTML($fldemail));
      $oTpl->SetVar( "url_c", ToHTML($fldurl));
      $oTpl->SetVar( "address", ToHTML($fldaddress));
      $oTpl->SetVar( "city", ToHTML($fldcity));
      $oTpl->SetVar( "state_id", ToHTML($fldstate_id));  
      $oTpl->SetVar( "zip", ToHTML($fldzip));
  $oTpl->SetVar( "FormLBcountry_id", "");
  my @country_id = openrs_arr( "select country_id, country_desc from lookup_countries order by 1" );
  foreach my $country_id ( @country_id ) {
    $oTpl->SetVar( "ID", $country_id->[0] );
    $oTpl->SetVar( "Value", $country_id->[1] );
    if ( $country_id->[0] eq $fldcountry_id) { $oTpl->SetVar( "Selected", "SELECTED" ) } else { $oTpl->SetVar( "Selected", "" ) }
      $oTpl->Parse( "FormLBcountry_id", "True");
    }
  
      $oTpl->SetVar( "phone_work", ToHTML($fldphone_work));
      $oTpl->SetVar( "phone_mobile", ToHTML($fldphone_mobile));
      $oTpl->SetVar( "fax", ToHTML($fldfax));    
      $oTpl->SetVar( "comments", ToHTML($fldcomments));    
      $oTpl->SetVar( "company", ToHTML($fldcompany));    
      $oTpl->SetVar( "date_created", ToHTML($flddate_created));
  $oTpl->Parse( "FormForm", "False" );


#-------------------------------
# Form Close Event start
# Form Close Event end
#-------------------------------

#-------------------------------
# Form Show end
#-------------------------------
}
#===============================
