#!/usr/bin/perl
$test_mode = "TRUE";
$test_mode = "FALSE";
$STYLE = "RACE";
$asknat = "hidden";
##############################################################################
# singmeup 
##############################################################################
require "/var/www/custom/cgi/FORMCODE.cgi" ;
require "/var/www/custom/cgi/formmisc.cgi" ;
require "/var/www/custom/cgi/errors.cgi" ;
require "/var/www/custom/cgi/CC_VER" ;
require "/var/www/custom/cgi/ChipCheck" ;
# Define Variables                                                           #
#      Detailed Information Found In README File.                            #
$CAT = '/bin/cat' ;
$GREP = '/bin/grep' ;
$SORT = '/bin/sort' ;
$RACELIST ="/var/race/RACE_LIST" ;
$EVENTLIST ="/var/race/EventID" ;
$mailprog = '/usr/sbin/sendmail -i -t';
$mailprog1= '/usr/sbin/sendmail -i -t -fsignups@comfortinc.com';
$mailto = "signups\@comfortinc.com" ;
$memberfile = "/var/race/members/memberfile" ;
$PPfile = "/var/race/members/Pprofiles" ;
#@referers = ('www.comfortsignups.com','comfortinc.com','srnet.com');
@referers = ('*');
@recipients = &fill_recipients(@referers);
@valid_ENV = ('REMOTE_HOST','REMOTE_ADDR','REMOTE_USER','HTTP_USER_AGENT');
##############################################################################
$TableCount = 0;
@theLINE = split(/\//,$0);
for($i=0;$i<15;++$i){
 $parting = $theLINE[$i];
 if($parting eq ""){$scriptname = $theLINE[$i-1];$i=16;}
}
if($ENV{'QUERY_STRING'}) {$scriptname .="?$ENV{'QUERY_STRING'}";}
$FORM1 = "/signup/$scriptname";
$FORM1 = "/cgi-bin/$scriptname";
$FORM2 = "/cgi-bin/$scriptname";
#require "/var/www/racedata/demo/demo/raceopts";
$formactionX = "http://www.comfortsignups.com$FORM1";
$incdir = "/var/www/racedata" ;
$incdir = "/home/signups/racedata" ;
$customdir = "/var/www/custom"  ;
$HTMLdir = "/var/www/signups/docs/html"  ;
$HTMLdir = "/var/www/custom/html"  ;
$CGIdir = "/var/www/signups/cgi-bin"  ;
$TEAMNAMES ="/var/race/TEAMNAMES";
$TEAMNAMES ="/var/race/TeamNames";
$FlatAdd = 1 ;
$PercentAdd = 0.05 ;
$PercentMore = 0.00 ;
$TeamMemberAdd = $FlatAdd;
$TeamMemberAdd = .50 ;
$Step = 1 ;
$AskForChip ="YES";
$Trust_Logo = "YES";

# Check Referring URL


&check_url;

# Retrieve Date
&get_date;
# Parse Form Contents
$AllUpper="TRUE";

&parse_form;
if ("$Form{lastname}" eq "BOULTON" && "$Form{firstname}" eq "BENN") {
&html_header;
$debug_mode = 3;
print "<br>DOBM-1 $Form{DOBmonth} ";
}
if($Form{Event} == 2023 || $Form{Event} eq 'cigna|Corporate'){
print "Location: http://www.comfortsignups.com/cgi-bin/cigna \n\n";
exit;
}
#if($Form{Event} == 2023 || $Form{Event} eq 'cigna|Corporate'){require "cigna";exit;}
#$WaiverIsSigned = "NEED WAIVER";
#if( $Form{agreement} ne ""){$WaiverIsSigned = "WAIVER OK";}
$Form{other_team_name} =~ tr [a-z] [A-Z];
(@Kids)=split(/\r\n/,$Form{KidsNames});
(@TeamMember)=split(/\r\n/,$Form{TeamMembers});
(@Series)=split(/\, /,$Form{SeriesEvent});
 foreach (@TeamMember){ ++$TotalTeam ;}

$Form{country} =~ tr [a-z] [A-Z] ;
$Form{address} =~ s/\'//g;
&html_header("Computer Comfort Sign-ups","BGCOLOR=#FFFEEA");



# print " -$goodEvent - $Form{ProfileID}$Form{ProfilePassword} --";
    $sdlname = $Form{lastname};
    $sdfname = $Form{firstname};
    $sdfname =~ s/ //g;
    $sdfname =~ s/\.//g;
    $sdfname =~ s/\'//g;
    $sdlname =~ s/ //g;
    $sdlname =~ s/\.//g;
    $sdlname =~ s/\'//g;
    $sdtos = substr($sdlname,0,1);
    $sdtos .= substr($sdlname,length($sdlname)-1,1);
    $sdtos .= substr($sdfname,0,1);
   $control = "$dtos-$ttos-$sdtos" ;
   $control =~ tr [a-z] [A-Z] ;

if($Form{credit_card_number} eq "PAPER-ENTRY") { $Form{PaperEntry} = "YES" ;}
if($Config{control} eq "") {$Config{control} = $control;}
$Action = $Config{Action};

$Step = "$Config{Step}$Config{step}";
if($ENV{'QUERY_STRING'} >0) {$Form{Event}="$ENV{'QUERY_STRING'}";}
$Event = "$Form{Event}$Form{event}$Form{EVENT}";
#print "$Event -- $ENV{'QUERY_STRING'}";
if ($Event eq "") { &notnow("NoEvent"); }

$goodEvent = (`$GREP \"|$Event|\" $EVENTLIST`);
if ($goodEvent) { 
 chop $goodEvent;
 ($fil1,$IDnum,$IDp1,$IDp2) = split(/\|/,$goodEvent);
 if($Event == $IDnum) {$Event = "$IDp1|$IDp2" ;}
# $goodEvent = substr($goodEvent,1);
# $Event = substr($goodEvent,index($goodEvent,"|")+1);
}
$PP_Event = $Event ;

$goodEvent = (`$GREP \"$Event|\" $RACELIST`);
if (!$goodEvent) { &notnow("NoEvent") ; }

($CompanyID,$EventID,$EventName,$EventTown,$EventState,$EventDTOS,$EventContact,$EventxEmail,$EventPhone,$EventOn,$TimeOn,$EventOff,$TimeOff,$filler1,$filler2,$SeriesID) =split(/\|/, $goodEvent);
$goodEvent = (`$GREP \"$CompanyID|\" $memberfile | $GREP \"|ACTIVE|\"`);

if (!$goodEvent) { &notnow("NoEvent") ; }
($XX1,$CompanyID,$XX3,$XX4,$CompanyContact,$CompanyPhone,$EventEmail,$CompanyName,$CompanyAddress1,$CompanyAddress2,$CompanyAddress3,$filler1,$filler2,$COSTATUS,$SeriesIDx) =split(/\|/, $goodEvent);
#&html_header("$EventName");
#print "$dtos - $EventOn - $EventOff";
#$TimeOff="2400";
    $EventStart = "$EventOn$TimeOn";
    $EventStop = "$EventOff$TimeOff";
    $EventStart = "$EventOn"."0000";
    $EventStop = "$EventOff"."2400";
    $isNow = "$dtos".substr($ttos,0,4);
$Event =~ s/\|/\//g ;
#$incdir .= "$Event" ;

if ("$Form{lastname}" eq "BOULTON" && "$Form{firstname}" eq "BENN") {
$debug_mode = 3;
print "<br>DOBM1 $Form{DOBmonth} ";
&html_header ;
  print "$CompanyID --- $EventID  --<br>";
#   print "$goodEvent";
print "<br>EQ$require<br>\n";

  &return_html ;
  }
if ($ENV{REMOTE_ADDR} eq "216.107.209.01"){ $EventStart=20000000;$EventStop=999999999999;}

if (index($ENV{QUERY_STRING},"TestMode=YES" || $Form{PaperEntry} eq "YES")>0 ||$Action eq 'Re-Print'){ $EventStart=20000000;$EventStop=999999999999;}
if($Form{PaperEntry} eq "YES"){ $EventStop = "999999999999";}
if (index($ENV{QUERY_STRING},"TestMode=YES")>0){ 
}else{
#print "Registration Services are Temporary Unavailable.\n";
#print "<br> We expect to return service by 8:00 am Saturday (7-11-2009)\n";
#print "<br> We are sory for any inconvenience.\n";
#exit;
}
$Reg_Start = $EventStart;
$Reg_Stop = $EventStop;
$Reg_Count = `$GREP -c "-" $incdir/$Event/REGDATA.csv`;
$optfile = "$incdir/$Event/raceopts" ;
$TeamFile = "$incdir/$Event/SpecialTeamList" ;
$PayPassFile = "$incdir/$Event/paypass.pw";
$PayPass = `$CAT $PayPassFile`;
chomp $PayPass;
if (!-f "$optfile") { &notnow("Cannot Locate Options File"); }
#require "$optfile";
&getraceopts("$optfile") ;
if($MaxReg > 0 && $Reg_Count >= $MaxReg && $Form{PaperEntry} ne "YES" && $Action ne 'Re-Print'){&notnow("This Event Is Full");}
 if($Form{category} ne ""){
  ($CatName,$CatCode,$CatBib,$CatTeamSize,$CatFee,$CGchangedate,$CGnewfee)= split(/\|/,$Form{category});
  $EventFee = $CatFee;
  $AltFee = $CGnewfee;
  $AltFeeDate = $CGchangedate;
  ($EventTempName)= split(/\(/,$CatName);
  $EventName = "$EventName $EventTempName";
 }
 if( $Form{other_team_name} ne ""){
 if ($AutoAddTeam ne "") {
 open (OTN, ">>$TeamFile");
 print OTN "$Form{other_team_name}\n";
 close (OTN);
 }
 $Form{team_name} = $Form{other_team_name};
 $Form{other_team_name} = "" ;
 }
if($Form{ForcePrice} ne ""){$EventFee=$Form{ForcePrice} ;}
$ChipRace =~ tr [a-z] [A-Z];
if($ChipRace eq "CHECKED"){$ChipRace = "YES";}
$ClyFyl =~ tr [a-z] [A-Z];
$STYLE =~ tr [a-z] [A-Z];
    $FreeShirt = "$tsizeC$tsizeS$tsizeM$tsizeL$tsizeXL$tsizesXXL" ;

$PProfile = "$Form{ProfileID}$Form{ProfilePassword}";
if ($PProfile && $STYLE ne "PAYMENT") {
$PProfile = "|$Form{ProfileID}|$Form{ProfilePassword}|";
$PPfile = "/var/race/members/PROFILES/$Form{ProfileID}-$Form{ProfilePassword}";
  $PProfile = (`$GREP \"$PProfile\" $PPfile`);
 if ($PProfile){
#print "<br>$PProfile<br> $PPfile";
   ($XX1,$XX2,$XX3,$lastname,$firstname,$address,$city,$state,$zip,$phone,$email,$gender,$DOBmonth,$DOBday,$DOByear,$ChipCode,$Country) = split (/\|/, $PProfile);
 $isProfile = "YES"; 
  }
}
if ($STYLE eq "PAYMENT" && $Step < 3) {
 $ffg = crypt($Form{ProfilePassword},$Form{ProfileID});
#print "---$STYLE--- $Step $PProfile <br>$PayPass - $ffg - \n";
 if($ffg ne $PayPass){$Form{Step} =  $Step = 0;}
#print "  $Step  ";
 if ($Step > 0) {
#print " ok ";
 $Config{required} = "ProfileID,ProfilePassword";
 $ForcePrice = " Charge: \$<input type=text size=5 name=ForcePrice value=$EventFee>";
 }
}
# if ($Form{lastname} eq "BOULTON" && $Form{firstname} eq "BENN") { $isNow = "200304040800";}
  if($Form{PaperEntry} eq "YES"){ $EventStop = "999999999999";}
if ($EventID ne "demo") {
 if ($isNow < $Reg_Start) { &notnow("NotYet"); }
 if ($isNow > $Reg_Stop) { &notnow("TooLate"); }
}
#print "$isNow -- $EventStart ";
$EventDate = "$months[$EventMonth-1] $EventDay, $EventYear";
if($AltFeeDate ne "" && length($AltFeeDate)==8){ $AltFeeDate .= "0000";}
if($AltFeeDate ne "" && $isNow > $AltFeeDate){ $EventFee = $AltFee;}
if($Form{ForcePrice} ne ""){$EventFee=$Form{ForcePrice} ;}
$RACE = $EventName;
$FEE = $EventFee;
if ($Action eq "" ) { $Action = $Form{action}; }
$Action =~ tr [a-z] [A-Z] ;
if ($Event eq "" && $Action eq "") { &notnow("NoEvent") ; }
if ($Action eq "") { $Action = "CONTINUE"; $Step = 0 }
&html_header("Computer Comfort Sign Ups");
print "<br><font size=5 color=green> $EventName<br>";
   if ($EventID ne "demo" && $STYLE ne "PAYMENT") { print "$EventDate  $EventStart"; }
  if($Form{PaperEntry} eq "YES"){ print "<br>Paper Entry";}
   print "</font><br>\n";
###BBB&Table_End;
###BBB&Table_Start("1");
$waiverfile = "$incdir/$Event/waiver.txt" ;
$Config{recipient} = $EventEmail;
if($Form{PaperEntry} eq "YES"){
 $ForcePrice = "Event Fee: \$<input type=text size=5 name=ForcePrice value=$EventFee>";
 if($PaperFee ne "" && $PaperFee != $EventFee){ $EventFee = $PaperFee;}
 $AskBib = "Event/Bib #:<input type=text size=5 name=EventIDNum value=$Form{EventIDNum}>";
 $AskAge = "Age On Race Day: <input type=text size=5 name=AskAge>";
 #$email = "$EventEmail";
 #$email2 = "$EventEmail";
 $Config{email2} = $Config{email} ;
 if($FeeOverride eq "checked"){
 $ForcePrice = "Event Fee: \$<input type=text size=5 name=ForcePrice value=$EventFee>";
 }else{
 $SkipEmail = "";
 $NoShirt = "";
 }
 }
# Check Required Fields
&more_required ;
&check_required;
$NextStep = $Config{NextStep} ;
#print "<br>$PProfile<br> $PPfile";
$PProfile = "$Form{ProfileID}$Form{ProfilePassword}";
 if ($PProfile) {
$PProfile = "|$Form{ProfileID}|$Form{ProfilePassword}|";
#   $Form{ProfileID} =~ tr [a-z] [A-Z] ;
#   $Form{ProfilePassword} =~ tr [a-z] [A-Z] ;
##   $PProfile = (`$GREP \"|$Form{ProfileID}|$Form{ProfilePassword}|\" $PPfile`);
  $PProfile = (`$GREP \"$PProfile\" $PPfile`);
  if ($PProfile){
    ($XX1,$XX2,$XX3,$lastname,$firstname,$address,$city,$state,$zip,$phone,$email,$gender,$DOBmonth,$DOBday,$DOByear,$ChipCode) = split (/\|/, $PProfile);
  $isProfile = "YES"; 
   }
 }
$TYPE = "ON-LINE" ;
if ($TYPE eq "ONLINE") { $TYPE = "ON-LINE"; }
if($Form{PaperEntry} eq "YES") {$TYPE="PAPER-ENTRY";$credit_card_number ="TESTING";}
if($Form{credit_card_number} eq "TESTING"){$TYPE = "MAIL-IN";}
if($Form{credit_card_number} eq "PAPER-ENTRY"){$TYPE = "MAIL-IN";}
$CSVFILE = "$incdir/$Event/regfiles/$Config{control}";

 if ($Step == 5) {
   if ($TYPE eq "ON-LINE") { 
    if($Form{credit_card_number} eq "TESTING"){
      $Form{TestMode} = "YES";
      $test_mode ="TRUE"; 
    }else {
     &CC_Verify;
     $last4 = "XXXX-XXXX-XXXX-".substr($cardnumber,length($cardnumber)-4,4) ;
    } 
   } # else {$FEE = $MAILINFEE; }
  $Responce_Code = 0 ;
     if($Form{credit_card_number} eq "TESTING") {$Responce_Code = 1;}
     if($Form{PaperEntry} eq "YES") {$Responce_Code = 1;}
## removed @()'s from here
foreach (@Series){(@SerEV)=split(/\|/,$_);$TOTAL = $TOTAL +$SerEV[2]; }
foreach (@Kids){$TOTAL = $TOTAL +$funrunfee; }
#foreach (@TeamMember){print "$_ 1"; $TOTAL = $TOTAL +$FEE +$TeamMemberAdd; ++$FlatAdd; }
     $CCFEE = $FEE+$TOTAL+$ServiceCharge;
     $CCFEE = $ONLINEFEE+$TOTAL+$OnLineCharge ;
     $CCFEE = $Form{CCFEE} ;
if ($Form{lastname} eq "BENN BOULTON" && $Form{firstname} eq "BENN BOULTON") {
$debug_mode = 3;
 $CCFEE = "1.00" ;
}
#$Form{firstname} = $RunnerFirst;
#$Form{lastname} = $RunnerLast;
   if($Form{TestMode} ne "YES" && $test_mode eq "FALSE") {  
     print "<div id=\"processing\">$TYPE - $test_modeProcessing Please Wait</div><br>\n";
     &New_Table;
     require "/var/www/custom/cgi/authnet.pl";
     $email_notice     = "FALSE";
     $email_notice     = "TRUE";
     if (-f $CSVFILE) { $Responce_Code = 1;
      $Responce_Text = "Transaction Processed";}
     else {
      if ($test_mode eq "FALSE" && $TYPE eq "ON-LINE") {;&process_trans;}
     }
     print "$Responce_Text<br>\n";
    }
    if($Responce_Code != 1 || $test_mode eq "FAIL") { 
      $Action = "CONTINUE";
      $NEXT = "";
      $Step = 4;
     &error("$Responce_Text<p>Card Declined<br>Click your Browser's BACK button
     <br>and make sure the information is correct<br>
     or use a different credit card.") ; 
    }
   }
if ($Action eq "CONTINUE") {
   $Form{chipcode} =~ s/ //g;
   $Form{chipcode} =~ s/-//g;
   $Form{chipcode} =~ tr [a-z] [A-Z] ;

  if ($Step == 0) { 
   &form_begin("form1","$FORM1");
   if($STYLE eq "PAYMENT" && $NoPaymentPassword ne "YES"){
    print "Enter Your Payment ID and Password<br>";
    }else{
#    if($CompanyID eq "cigna"){&AskSigningUp;}
    print "If you have a Comfort Signups <br>";
    print "participant profile enter information<br>";
   }
   print "ID: "; 
   &FORM_input("text","ProfileID","",10);
   print "Password: "; &FORM_input("password","ProfilePassword","",10);
   if($STYLE ne "PAYMENT"){
#    print "<br><font color=red>NOTE: Due to error, all profiles saved before";
#    print "<br> April 13th 2007, were lost. Sorry! </font>";
     print "<p>"; 
#     print "If you DO NOT have a profile, you can save one <br>";
#    print "at the end of the registration process.<br>";
   }
   &form_end("1") ;

## or reprint  ##
&_show_reprint_form;
#  &form_begin("reprint","$FORM1");
#print "<br> To re-print a Confirmation page, fill in your Registration Code and click Re-Print.<br>";
#print "<input type=\"text\" name=\"RegCode\" size=\"20\">";
#&form_end("1","Re-Print");

   &html_footer;
  exit;
 }
}
  if($Action eq "RE-PRINT"){
   print "$Action<br>";
   $Action = "FINISH";
   $Config{control} = $control = $Reg_Code =$RegCode= $Form{RegCode};
   $last4 = "Not displayed on re-prints";
   $Step = 5;
    require "reprint-race-reg";
    exit;
  }
  if ($Action eq "COMPLETE") { 
   &add_to_file;

#   &mail_ack ;
   exit ;
  }

if ($Action eq "CONTINUE") {
 if ($Step == 1) { 
  if ($isProfile) {
   print "<font color=#008080>Welcome back $firstname $lastname</font><br>\n";
  }
  &help_script;
  &form_begin("form1","$FORM1");
  &get_waiver_ok; 
#  $CorpSignup ="checked";
#  if($SeriesID ne "") { &ShowSeries ;}
  if($CorpSignup eq "checked") { &AskSigningUp; }
  &form_end($Step+1);
  &html_footer;
 }

 if ($Step == 2) {
  &Table_End;
   &form_begin("form1","$FORM1");
   if ($Form{signup_type} eq "GROUP") {
    $Group = "YES" ;
    print "<input type=hidden name=Group value=YES>";
   }
   &FORM_input("hidden","PaperEntry","$Form{PaperEntry}");
   if ($isProfile) {print "<input type=hidden name=isProfile value=YES>"; }
   if($Form{CorporInd} eq "CorpTeam"){
    print"<font color=red size=+1>Please Enter Company Contact/Captian Information</font>\n";
    print "</center><pre>Company Name<input type=text name=CompanyName size=50>\n";
    print "Team Name   <input type=text name=team_name size=50></pre>\n";
     if(-f "$incdir/$Event/TeamList.csv"){$TEAMNAMES.=" $incdir/$Event/TeamList.csv";}
#    if($CompanyID eq "cigna"){$TEAMNAMES.=" $incdir/Corporate/TeamList.csv";}
    &teamnames;
    $CorpBypassOn = "<!--";
    $CorpBypassOff = "-->";
}
print"</font></font>";
if($Form{CorporInd} eq "CorpTeam"){
print"<font color=red size=+1>Team Contact Information</font>\n";
}
$Country_List ='<input type=text size=20 name="country" value="{country}" {ReadOnly}>';
$Country_List ="<select id='countrySelect' name='country' onchange='updateState(this.id)'></select>";
$State_List ="<select id='stateSelect' name='state'></select>";
#####$State_List = '<input type=text size=30 name="state" value="{state}" maxlength=30 {ReadOnly}>';
if(-f "/var/www/custom/html/XState_list.html"){
 $State_List = `$CAT /var/www/custom/html/State_list.html`;
 if(-f "$incdir/$Event/state_list.txt"){@Add_States=` $CAT $incdir/$Event/state_list.txt`;}
 foreach $newState (@Add_States) {
  chomp($newState);
  ($st_opt,$st_desc) = split(/\=/,$newState);
  $State_List .= "<option value=\"$st_opt\">$sp_desc</option>"
 }
 $State_List .= "</select>"
}
$State_List ="<div><select id='stateSelect' name='state'></div></select>";
#####$State_List = "<input type=text size=30 name=\"state\" value=\"$state\" maxlength=30 {ReadOnly}>";
if ($STYLE eq "TICKETS" || $STYLE eq "CONCERT"){
    $CorpBypassOn = "<!--";
    $CorpBypassOff = "-->";
}
if ($STYLE ne "PAYMENT"){
 print &getTemplate('/var/www/custom/html/racereg.html');
 print &getTemplate('/var/www/custom/html/racereg2.html');
 print &getTemplate('/var/www/custom/html/racereg3.html');
} else {
 print &getTemplate('/var/www/custom/html/payments.html');

}
print '<SCRIPT  type="text/javascript">initCountry(); </SCRIPT>';
	
 if($SeriesID ne "") { &ShowSeries ;}
# if($WaveStart ne "") {
  $EvCatCnt = 0;
#  foreach (`$CAT $incdir/$Event/category.txt`) {
#   if($EvCatCnt == 0){
#   print "Please Select Your Event Category <br>\n";
#   print "<select name=category size=7>\n";
#   $EvCatCnt = 1;
#   }
#  chop $_;
#  ($CatName,$CatCode,$CatBib,$CatTeamSize,$CatFee)= split(/\|/,$_);
#  print "<option value=\"$_\">$CatName \$ $CatFee</option>\n";
#  }
#   if($EvCatCnt == 1){print "</select>";}
  foreach (`$CAT $incdir/$Event/category.txt`) {
   if($EvCatCnt == 0){
   print "<table border=0>\n";
   print "<tr><td colspan=3 align=center>";
   print "<b>Please Select Your Event Category</b> <br>\n";
   print "</td></tr>\n";
   $EvCatCnt = 1;
   }
  chop $_;
  ($CatName,$CatCode,$CatBib,$CatTeamSize,$CatFee,$CGchangedate,$CGnewfee)= split(/\|/,$_);
   if($CGchangedate ne "" && length($CGchangedate)==8){ $CGchangedate .= "0000";}
   if($CGchangedate ne "" && $isNow > $CGchangedate){ $CatFee = $CGnewfee;}
  ($CatName,$CatExtra)= split(/\(/,$CatName);
  if($CatExtra ne '') {$CatExtra = "($CatExtra";}
  print "<tr><td>\n";
  print "<input type=radio name=category value=\"$_\"><b>$CatName</b> </td>";
  print "<td>$CatExtra</td>\n";
  print "<td align=right><font color=green size=+1> \$$CatFee</font>\n </td></tr>\n";
  }
   if($EvCatCnt == 1){print "</table>";}
 &Free_Shirt;
 &ExtraItems;

if($Form{CorporInd} eq "CorpTeam"){&CorpSignup;}
     &Table_Start;
if($funrun eq "checked" && $Form{CorporInd} ne "CorpTeam"){
$funRunDisplay = "Kids Fun Run: ";
if($funRunTitle ne "") {$funRunDisplay = "$funRunTitle";}
if($funrunage ne "")   {$funRunDisplay = "$funRunDisplay Ages: $funrunage ";}
if($funrunfee ne "")   {$funRunDisplay = "$funRunDisplay Fee: \$$funrunfee each";}
print "<h3>$funRunDisplay</h3>";
$funRunMessage = `$CAT $incdir/$Event/raceopts-frmessage`;
chomp $funRunMessage ;
if($funRunMessage ne ""){
  $funRunMessage =~ s/\n/\<br\>\n/g;
  print "$funRunMessage";
}else{

print "I would also like to signup the following family members.<BR>\n";
print " I am the Parent or Legal Gardian of the names listed<br>\n";
print " and I agree to the same conditions of the wavier for these people.<br>\n";
}
print "<p align=left>Please enter<br> LAST NAME,FIRST NAME,AGE(on Race Day),GENDER(M or F)";
if ($FreeShirt) {print ",Shirt Size(as listed above)"; }
print " seperated by commas:<br></p>\n";
print "(<b>Example:</b>  Smith,Tom,6,M";
if ($FreeShirt) {print ",Small"; }
print ")<br>\n";
if ($EventID eq "demo" || $RACE eq "DEMO RACE EVENT") {
  print "<table><tr valign=\"bottom\"><th>Last Name</th><th>First Name</th><th>Age<br>(Event Day)</th><th>Gender</th>\n";
  if ($FreeShirt) {print "<th>Shirt Size</th>\n";}
  print "</tr>\n<tr>\n";
  print "<td><input type=\"text\" name =\"add_lastname\" size=\"20\" /> </td>";
  print "<td><input type=\"text\" name =\"add_firstname\" size=\"20\" /> </td>";
  print "<td><input type=\"text\" name =\"add_age\" size=\"3\" /> </td>";
  print "<td><input type=\"radio\" name =\"add_gender\" value=\"M\" />M ";
  print "<input type=\"radio\" name =\"add_gender\" value=\"F\" />F </td>";
  if ($FreeShirt) {
    print "<td><input type=\"text\" name =\"add_shirt\ size=\"10\" /></td>";
  }
  print "<td><button onclick=\"AddFamilyMember()\">ADD</button</td>";
  print "</tr></table>\n";
}

print "<textarea name=KidsNames rows=3 cols=40 wrap=off ></textarea><br>\n";
print "Please enter one name on each line.\n";
#print "</table>";
###&Table_End;
}
print "<p>";
&Table_Start;
     $ChipRace =~ tr [a-z] [A-Z];
   if($ChipRace eq "CHECKED"){$ChipRace = "YES";}
     $MODE1 = "DISPLAY";
if($ChipRace eq "YES" && $Form{CorporInd} ne "CorpTeam"){&pr_chipinfo;}
       
   if ($ClyFyl eq "YES") {
     print "<p>Clydesdale/Filly ";
     print "<input type=checkbox name=clyfil value=YES>Check here only if you are male 190+ pounds or female 140+ pounds.<br>\n";
   }
if($UseTeams eq "checked" && $Form{CorporInd} ne "CorpTeam"){     &teamnames; }
    if ($Form{signup_type} eq "BYPASS"){
      open (ST2,"/$incdir/$Event/bypassoptions.htm");
      while (<ST2>) { print $_ ; }
      close (ST2);
    }
#print "</table>\n";
# print "<tr>";
# print sprintf("<td nowrap><font color=green>\$%9.2f </font></td>",$EventFee );
# print "</td></tr>";
&Table_End;
  if($EvCatCnt == 0){
if($STYLE eq "TICKETS"){$rpalign="center";}else{$rpalign="left";}
print "<p align=$rpalin><font color=blue><b>$RACE</b></font>\n" ;
if($FEE > 0){
print "<font color=green><b>\$ ";
print sprintf ("%5.2f",$FEE);
}
print "</b></font></p>\n" ;
}
&Table_Start;
    &pr_prices;
    $FSIStop= "";
    if ($ForSale1 ne "" or $FSIStop ne ""){
     require "/var/www/custom/cgi/forsaleitems";
     &forsaleitems($optfile) ;
    }
    if ($Form{signup_type} ne "GROUP"){
#    &ccgetinfo;
       print "<input type=hidden name=ProcessMethod value=ON-LINE>\n";
      }else{
       print "<input type=hidden name=ProcessMethod value=MAIL-IN>\n";
     }
    $require="lastname,firstname";
    if($STYLE ne "PAYMENT" && $STYLE ne "TICKETS"){
     $require=$require.",gender,DOBmonth,DOBday,DOByear";
    }
    $require= $require.",city,state,zip,email,email2,Phone,shirt_size";
if($debug_mode > 2){print "<br>req2$require $Config{required}";}
   if(-s "$incdir/$Event/category.txt") {$require= $require.",category";}
     
    if($AskCountry ne "") {$require = $require.",country";}

       print "<input type=hidden name=CorporInd value=$Form{CorporInd}>\n";
    &form_end($Step+1);
    &html_footer;
  }
  if ($Step > 2) {
   &calcage;
   $MODE1 = "DISPLAY";
   &Step3A  ;
   if ($Step == 4) {
    &form_begin("form1","$FORM2");
    if (!$Form{isProfile} && $Form{PaperEntry} ne "YES"  && $STYLE ne "PAYMENT") {
     print "<p>Check here <input type=checkbox name=Save_Me value=SAVE_ME> to";
     print " remember above the information as a ";
     print "<font color=#008080>Comfort Signup Profile.</font></p>";
    }
#    if($TYPE eq "ON-LINE") { &ccgetinfo; }
   }else{
   if ($Form{PaperEntry} ne "YES") {$TheUrl = "s";}
   &form_begin("form1","http$TheUrl://www.comfortsignups.com/cgi-bin/$scriptname");
   }
   &confirm;
#   &pr_form_fields;
  $NEXT = "";
  if ($Step == 4) {$NEXT="Finish";}
   if ($Step == 3) {
   $control = "$dtos-$ttos-$sdtos" ;
   $control =~ tr [a-z] [A-Z] ;
 print "<input type=hidden name=control value=\"$control\">\n";
 print "<font color=green>PLEASE CHECK THE INFORMATION FOR ACCURACY</font><br>\n";
 print "Click the browser BACK BUTTON to go back and make any corrections<br>\n";
   }
    $require="lastname,firstname";
    if($STYLE ne "PAYMENT" && $STYLE ne "TICKETS"){
     if(index($require,'DOBmonth')<0){
     $require=$require.",gender,DOBmonth,DOBday,DOByear";i
     }
     }
    $require= $require.",city,state,zip,email,email2,Phone,shirt_size";
if($debug_mode > 2){print "<br>req3$require $Config{required}";}
    if($AskCountry ne "") {$require = $require.",country";}
#    $require="lastname,firstname,gender,DOBmonth,DOBday,DOByear,city,state,zip,email,email2,Phone,shirt_size";
    $sdlname = $Form{lastname};
    $sdfname = $Form{firstname};
    $sdfname =~ s/ //g;
    $sdfname =~ s/\.//g;
    $sdlname =~ s/ //g;
    $sdlname =~ s/\.//g;
    $sdtos = substr($sdlname,0,1);
    $sdtos .= substr($sdlname,length($sdlname)-1,1);
    $sdtos .= substr($sdfname,0,1);
#    $sdtos .= substr($dtos,6);
   $control = "$dtos-$ttos-$sdtos" ;
   $control =~ tr [a-z] [A-Z] ;
#   print "<input type=hidden name=control value=\"$control\">";
if(index($ENV{'QUERY_STRING'},'TestMode=YES') >= 0){$TestMode = 'YES';}
if ($Step != 4 || $TestMode eq 'YESx') {
   &form_end($Step+1,"$NEXT");
}
if($EventName eq "CMN Run For The Kids"){

  print "</form>";
#if($TestMode ne 'YES'){
if(index($ENV{'QUERY_STRING'},'TestMode=YES') < 0){
 if($FEE == 20){      $PP_ID ='36UASN4NN6M2U' ;
 }elsif ($FEE == 25){ $PP_ID ='5DPHPGKMV334S' ;
 }elsif ($FEE == 30){ $PP_ID ='8672VTS2NLSDC' ;
 }elsif ($FEE == 35){ $PP_ID ='EYQ7LAPE3NJUW' ;
 }elsif ($FEE == 40){ $PP_ID ='6DC2ZZS4F4LKQ' ;
 }elsif ($FEE == 45){ $PP_ID ='T2VZZEM65KVAN' ;
 }elsif ($FEE >= 50){ $PP_ID ='6XUXT3NB22M32' ;
 }  
}else{
$PP_ID = ($FEE == 20) ? 'QRP9X8W6X3SGA' : 'GC37UTQG4HV5Y' ;
}

if($Step == 4){
  $CSVFILE = "$incdir/$Event/pending/$Config{control}";
  if (!-f "$CSVFILE") {    &do_file_stuff; }
#<br>Please Paste this Code into the REG CODE section <br><br><b>$Config{control}</b>
$pp_btn = qq^ <input type="hidden" name="hosted_button_id" value="$PP_ID"> ^;
$pp_click ='_s-xclick';
$newamt = '';
if($Form{lastname} eq 'McColl Boulton'){
    $pp_click = '_xclick';
    $pp_btn = qq^ <input type="hidden" name="bn" value="ComfortSignups_BuyNow_WPS_US"> 
    <input type="hidden" name="business" value="GERZKSSJCJGZN">
    <input type="hidden" name="notify_url" value="https://www.comfortsignups.com/cgi-bin/regok.cgi">
    <input type="hidden" name="return" value="https://www.comfortsignups.com/regthankyou.html">
    <input type="hidden" name="amount" value="$FEE">
    ^;
}

print qq^
<br>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="$pp_click">
$pp_btn
<input type="hidden" name="custom" value="$PP_Event|$Config{control}">
<input type="hidden" name="invoice" value="$EventYear\_$IDnum\_$Config{control}">
<input type="hidden" name="item_name" value="$EventYear $EventName">
<input type="hidden" name="item_number" value="$IDnum\_$EventID">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_paynow_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
<input type="hidden" name="first_name" value="$Form{firstname}">
<input type="hidden" name="last_name" value="$Form{lastname}">
<input type="hidden" name="address1" value="$Form{address}">
<input type="hidden" name="city" value="$Form{city}">
<input type="hidden" name="zip" value="$Form{zip}">
<input type="hidden" name="email" value="$Config{email}">
</form> 
 Pay with a Debit Card, Credit Card or a PayPal Account <br>
(We are giving PayPal a try this year.)
^;   
}
}   
}
}

if ($Action eq "FINISH") {
  if ($Step == 5) {
   
#   $TYPE="ON-LINE"; 
#   &FORMinput("hidden","RaceDayAge","0");
   $FEE = $ONLINEFEE;
   $FEE = $EventFee;
   if ($Form{team_name} eq "NONE") { $Form{team-name} == "" ;}
   if ($Form{Group} eq "YES" || $Form{Group} eq "ON") { 
     $Group = "YES";
     $FEE = $MAILINFEE; 
     $TYPE="MAIL-IN"; 
    }
#   if ($TYPE eq "ON-LINE") { 
#     &CC_Verify;
#     $last4 = "XXXX-XXXX-XXXX-".substr($cardnumber,length($cardnumber)-4,4) ;
#    }else {$FEE = $MAILINFEE; }
   &calcage;
   $MODE1 = "DISPLAY";
   &Step3A  ;
if ($Form{lastname} eq "BOULTON" && $Form{firstname} eq "BENN") {
$debug_mode = 3;
  print "$Form{EventID} <br> --";
  &return_html ;
}
  &html_footer;
#   if ($TYPE eq "ON-LINE") {  &ccgetinfo; }
#   if ($TYPE eq "MAIL-IN") {  $Step = 4; }

  if ($Adjustment eq "INCLUDED") { 
   if($Address_VCode eq "Y"){ $PercentMore = 0;}
   $TeamMemberAdd = 0;
   $DiVisor = $PercentAdd+$PercentMore ;
   $ServiceCharge = int($TOTAL * $DiVisor * 100)/100 + $FlatAdd   ;
  if ($CompanyID eq "prponh" && $ServiceCharge == 2.05){
#  $ServiceCharge = 2.00  ;
}
   if ($TotalTeam > 1) {
    $ServiceCharge = int($TOTAL*$DiVisor * 100)/100 + ($FlatAdd*$TotalTeam) ;
   }  
   if($ServiceCharge < $MinServiceCharge) {$ServiceCharge = $MinServiceCharge;}
   $FEE = $TOTAL - $ServiceCharge ;
   $FEE =~ s/ //g;
   $TOTAL = $FEE  ;
   $FEE = sprintf("%9.2f",$FEE ) ;
  }
  $CSVFILE = "$incdir/$Event/regfiles/$Config{control}";
  if (-f "$CSVFILE") { 
  }else {
   $RunnerFirst = $Form{firstname};
   $RunnerLast = $Form{lastname};
   if($SeriesAdd > 0) {
   $X_Event = $Event;
#    (@Series)=split(/\, /,$Form{SeriesEvent});
   foreach (@Series){
    (@SerEV)=split(/\|/,$_);$Event = "$SerEV[0]/$SerEV[1]";
    $TOTAL = $SerEV[2];
    $RACE = $SerEV[3];
    if ($SerEV[4] eq "ADD"){ 
     $ServiceCharge = (($TOTAL +$FlatAdd) * $PercentAdd)+$FlatAdd ;
    }else{
     $DiVisor = $PercentAdd+$PercentMore ;
     $ServiceCharge = int($TOTAL * $DiVisor * 100)/100 + $FlatAdd   ;
    }
    if($ServiceCharge < $MinServiceCharge) {$ServiceCharge = $MinServiceCharge;}
    if($TYPE eq "ON-LINE"){&make_ccdata ;&add_to_cc_file ; }
if ($CompanyID eq 'nedin'){ print " Ta ";}
    &do_file_stuff;
   }
   $TOTAL = $EventFee;
  }
  if($TYPE eq "ON-LINE"){&make_ccdata ;&add_to_cc_file ; }
  if($TotalTeam > 0) {
   $X_TOTAL = $TOTAL ;
   $X_SERVICECHARGE = $ServiceCharge;
   $T_COMPANY = $Form{CompanyName} ;
   $T_ADDRESS = $Form{address} ;
   $T_CSZ = "$Form{city} $Form{state} $Form{zip}" ;
   $T_CONTACT = "$Form{firstname} $Form{lastname}";
   $T_EMAIL = $Config{email} ;
   $T_PHONE = "$Phone $Form{phone2}" ;
   foreach (@TeamMember){
    if ($Adjustment eq "INCLUDED") { 
     $TOTAL = $FEE;
     $FlatAdd = $TeamMemberAdd;
     if($Address_VCode eq "Y"){ $PercentMore = 0;}
     $DiVisor = $PercentAdd+$PercentMore ;
     $ServiceCharge = int($TOTAL * $DiVisor * 100)/100 + $FlatAdd   ;
     $FEE = $TOTAL - $ServiceCharge ;
     $FEE =~ s/ //g;
     $TOTAL = $FEE  ;
     $FEE = sprintf("%9.2f",$FEE ) ;
    }else {
     $TOTAL= $EventFee;
     $ServiceCharge = (($TOTAL +$TeamMemberAdd) * $PercentAdd)+$TeamMemberAdd ;
#     $TOTAL = $TOTAL - $ServiceCharge ;
    }
    (@tmemberinfo) = split(/\^/,$_);
    $Form{firstname}=$tmemberinfo[0];
    $Form{lastname}=$tmemberinfo[1];
    $Form{gender}=$tmemberinfo[2];
    $Form{RaceDayAge} = $tmemberinfo[3];
    $Form{city}=$tmemberinfo[4];
    $Form{state}=$tmemberinfo[5];
    $Form{shirt_size}=$tmemberinfo[6];
    $Form{Chipcode}= $tmemberinfo[7];
    #$Form{team_name} .= $Form{CompanyTeam};
    $Form{zip}= "";
if ($CompanyID eq 'nedin'){ print " Tb ";}
    &do_file_stuff;
   }
  }else{
if ($CompanyID eq 'nedin'){ print " Tc ";}
   &do_file_stuff;
  }
#if($Form{PaperEntry} eq "YES"){ print "<script>window.close();</script>";exit;}
   &send_ack;
    if ($Form{Save_Me} eq "SAVE_ME") { &SaveMe; }
#   &do_alert;
  }
 }
}

exit ;

sub do_file_stuff {
  $CSVFILE = "$incdir/$Event/pending/$Config{control}";
if (!-f "$CSVFILE") { 

if ($CompanyID eq 'nedin'){ print " T1 ";}
   &make_filedata;
if ($CompanyID eq 'nedin'){ print " T3 ";}
   &add_to_file;
if ($CompanyID eq 'nedin'){ print " T2 ";}
   &save_data;
if ($CompanyID eq 'nedin'){ print " T4 ";}
}

}
sub form_begin {
    local($formname,$formaction) = @_;
 print "<form name=$formname action=$formaction method=post>\n";
#print "<form name=$formname action=$formactionX method=post>\n";
}
sub form_end {
 $NextStep = $_[0];
 $Caption = $_[1];
 if ($Caption eq "") {$Caption = "Continue";}
 if ($NextStep == 0){ $NextStep= $Step +1 ; }
if ($Caption eq "Finish"){ 
  $CCFEE =sprintf ("%9.2f",$TOTAL+$ServiceCharge);
  print "<font color=green>PLEASE CHECK THE INFORMATION FOR ACCURACY<br>\n";
  print "</font>click your back button to go back and make changes";
  print "<p>";
  print "Check here <input type=checkbox name=\"OK CheckBox\" value=OK> if everything is ok.";
  print "<p>";
  print "Total Cost = \$";
  print "$CCFEE";
  $require .= ",OK CheckBox";
  print "<input type=hidden name=CCFEE value=$CCFEE>\n" ;
}
 print "<input type=hidden name=Step value=$NextStep>\n" ;
# print "<input type=hidden name=print_config value=1>\n";
 print "<input type=hidden name=required value=\"$require\">\n";
 print "<input type=hidden name=Event value=\"$Form{Event}\">\n";
 if ($NextStep == 2){ 
   &FORM_input("hidden","ProfileID","$Form{ProfileID}");
   &FORM_input("hidden","ProfilePassword","$Form{ProfilePassword}");
 }
 print "<center><input type=submit name=Action value=$Caption></center>\n";
if ($Caption eq "Finish") { print "CLICK FINISH ONLY ONCE<BR>"; }
 print "</form>\n";
}
sub parse_form {

    # Define the configuration associative array.                            #
    %Config = ('recipient','',          'subject','',
               'email','',              'realname','',
               'redirect','',           'Step','',
               'background','',         'control','',
               'vlink_color','',        'NextStep','',
               'alink_color','',        'title','',
               'sort','',               'print_config','',
               'required','',           'env_report','',
               'return_link_title','',  'return_link_url','',
               'print_blank_fields','', 'missing_fields_redirect','',
               'Action','',              'ContactEmail','',
               'email2','',              'XontactEmail','');

#    print " METHOD = $ENV{'REQUEST_METHOD'}" ;
    if ($ENV{'REQUEST_METHOD'} eq 'GET') {
        # Split the name-value pairs
        @pairs = split(/&/, $ENV{'QUERY_STRING'});
    }
    elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
        # Get the input
        read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
 $buffer .= "&RaceDayAge=0";
        # Split the name-value pairs
        @pairs = split(/&/, $buffer);
    }
    else {
#        &Login ;
        &error('request_method');
    }
    # For each name-value pair:                                              #
    foreach $pair (@pairs) {
        # Split the pair up into individual variables.                       #
        local($name, $value) = split(/=/, $pair);
        # Decode the form encoding on the name and value variables.          #
        # v1.92: remove null bytes                                           #
        $name =~ tr/+/ /;
        $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        $name =~ tr/\0//d;

        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        $value =~ tr/\0//d;
    #    $value =~ s/\,//g;

        # If the field name has been specified in the %Config array, it will #
        # return a 1 for defined($Config{$name}}) and we should associate    #
        # this value with the appropriate configuration variable.  If this   #
        # is not a configuration form field, put it into the associative     #
        # array %Form, appending the value with a ', ' if there is already a #
        # value present.  We also save the order of the form fields in the   #
        # @Field_Order array so we can use this order for the generic sort.  #
        if (defined($Config{$name})) {
            $Config{$name} = $value;
        }
        else {
            if ($Form{$name} ne '') {
                $Form{$name} = "$Form{$name}, $value";
                $Form{$name} = "$value";
            }
            else {
                push(@Field_Order,$name);
        if($name eq "TeamMembers"){
          $value =~ tr [a-z] [A-Z] ;
          $value =~ s/\,/^/g;}
        if($name ne "KidsNames"){
        $value =~ s/\,/ /g;
       }
#        $value =~ tr [a-z] [A-Z] ;
                $Form{$name} = $value;
            }
        }
    }

    # The next six lines remove any extra spaces or new lines from the       #
    # configuration variables, which may have been caused if your editor     #
    # wraps lines after a certain length or if you used spaces between field #
    # names or environment variables.                                        #
    $Config{'required'} =~ s/(\s+|\n)?,(\s+|\n)?/,/g;
    $Config{'required'} =~ s/(\s+)?\n+(\s+)?//g;
    $Config{'env_report'} =~ s/(\s+|\n)?,(\s+|\n)?/,/g;
    $Config{'env_report'} =~ s/(\s+)?\n+(\s+)?//g;
    $Config{'print_config'} =~ s/(\s+|\n)?,(\s+|\n)?/,/g;
    $Config{'print_config'} =~ s/(\s+)?\n+(\s+)?//g;

    # Split the configuration variables into individual field names.         #
    @Env_Report = split(/,/,$Config{'env_report'});
    @Print_Config = split(/,/,$Config{'print_config'});

    # ACCESS CONTROL FIX: Only allow ENV variables in @valid_ENV in          #
    # @Env_Report for security reasons.                                      #
    foreach $env_item (@Env_Report) {
        foreach $valid_item (@valid_ENV) {
            if ( $env_item eq $valid_item ) { push(@temp_array, $env_item) }
        }
    } 
    @Env_Report = @temp_array;
}

sub check_required {

    # Localize the variables used in this subroutine.                        #
    local($require, @error);

    # The following insures that there were no newlines in any fields which  #
    # will be used in the header.                                            #
if($debug_mode > 2){print "<br>req3$require $Config{required}";}
    @Required = split(/,/,$Config{'required'});
    if ($Config{'subject'} =~ /(\n|\r)/m || $Config{'email'} =~ /(\n|\r)/m ||
        $Config{'realname'} =~ /(\n|\r)/m || $Config{'recipient'} =~ /(\n|\r)/m) {
        &error('invalid_headers');
    }

    if (!$Config{'recipient'}) {
        if (!defined(%Form)) { &error('bad_referer') }
        else                 { &error('no_recipient') }
    }
    else {
        # This block of code requires that the recipient address end with    #
        # a valid domain or e-mail address as defined in @recipients.        #
        $valid_recipient = 0;
        foreach $send_to (split(/,/,$Config{'recipient'})) {
            foreach $recipient (@recipients) {
                if ($send_to =~ /$recipient$/i) {
                    push(@send_to,$send_to); last;
                }
            }
        }
        if ($send_to < 0) { &error('no_recipient') }
        $Config{'recipient'} = join(',',@send_to);
    }

    # For each require field defined in the form:                            #
    foreach $require (@Required) {

        if ($require eq 'cardemail' && !&check_email($Form{$require})) {
            push(@error,$require);
        }
        # If the required field is the email field, the syntax of the email  #
        # address if checked to make sure it passes a valid syntax.          #
        if ($require eq 'email' && !&check_email($Config{$require})) {
            push(@error,$require);
        }

        # Otherwise, if the required field is a configuration field and it   #
        # has no value or has been filled in with a space, send an error.    #
        elsif (defined($Config{$require})) {
            if ($Config{$require} eq '') { push(@error,$require); }
        }

        # If it is a regular form field which has not been filled in or      #
        # filled in with a space, flag it as an error field.                 #
        elsif (!defined($Form{$require}) || $Form{$require} eq '') {
            push(@error,$require);
        }
    }

    # If any error fields have been found, send error message to the user.   #
    if (@error) { &error('missing_fields', @error) }
}

sub notnow { 
    # Localize variables and assign subroutine input.                        #
    local($error,@error_fields) = @_;

    &html_header;
    &error_header;
    if ($error eq 'NotYet') {
       $error = " Sorry <br>$EventName<br>Registrations Are Not Activated Yet.";
       $error .= "<br>Please come back after ";
       if($TimeOn >1159) {
       $error .= substr($TimeOn,0,2) -12; 
        }else{
       $error .= substr($TimeOn,0,2); 
        }
       $error .= ":".substr($TimeOn,2,2); 
       if($TimeOn <1159) {$error .= "am";}else{$error .= "pm";}
       $error .= " on ";
       $error .= substr($EventOn,4,2)."/"; 
       $error .= substr($EventOn,6,2)."/"; 
       $error .= substr($EventOn,0,4); 
       }
    if ($error eq 'TooLate') {
     $error = " Sorry <br>$EventName<br>Online Registrations Are CLOSED.<br>";
     if($RaceDayReg eq "YES"){ $error="$error You can still register on race day at the event.";}
     if($CloseLink ne ""){ $error="$error <br><a href=http://$CloseLink>More Information</a>";}
    }
    if ($error eq 'NoEvent') {
        $error = " NOT a valid Event.";
       }
    print "$error";
if($error eq 'This Event Is Full'){&_show_reprint_form;}
    &error_footer; 
print "$isNow - $EventStart - $EventStop";
    exit;
}
sub error_header {
  &html_header("","bgcolor=#FFFFFF text=#000000");
 print <<"(END ERROR HTML)";
  <center>
   <table border=0 width=600 bgcolor=#9C9C9C>
    <tr><th><font size=+2>$racetitle1</font></th></tr><tr><th>
   <table border=0 width=600 bgcolor=#CFCFCF><tr><td><center>
    <font color=red size=+2>
(END ERROR HTML)
}
sub error_footer{
 print <<"(END ERROR HTML)";
 </font></td></tr></table>
     <center><font size=-1>
     Customized Forms by Benn Boulton of <a href="http://www.comfortinc.com/">Computer comfort Inc.</a>
     </font></center>
 </td></tr></table>
 </center>
 </body>
 </html>
(END ERROR HTML)
}

sub register {
}

sub get_type {
#$TYPE = $Form{racetype};
($RACE,$XYPE,$FEE1,$FEE2) = split(/\|/,$Form{racetype});
$FEE = $FEE1 ;
if ($tmon == $RACEmonth && $year == $RACEyear && $mday >= $FEE2day && $FEE == $FEE1){ $FEE=$FEE2; }
}

sub calcage {
 if($Form{AskAge} ne ""){ 
  $Form{DOByear} = $EventYear - $Form{AskAge};
  $Form{DOBmonth} = "-";
  $Form{DOBday} = "-";
 }
 $wrk = $EventYear - $Form{DOByear};
 if ($Form{DOBmonth} > $EventMonth ) { $wrk = $wrk -1; }
 if ($Form{DOBmonth} == $EventMonth && $Form{DOBday} > $EventDay)  { $wrk = $wrk -1; }
 $Form{RaceDayAge} = $wrk ;
 if($wrk < $MinAge){&error('Invalid Entry',"Invalid Entry, You Must be $MinAge To Participat."); }
}

sub more_required {
#print "$Form{CorporInd} 12335";
   if($Form{CorporInd} eq "CorpTeam"){ $Form{gender}=$Form{DOBmonth}=$Form{DOBday}=$Form{DOByear}= "xxxx";}
   &calcage;
    if (length($Form{DOByear}) > 0 && length($Form{DOByear}) < 4) { 
     $Config{required} .= "DOB YEAR must be 4 digits";
    }
    
    if (length($Form{chipcode}) > 0 ) {
     &CheckChip("$Form{chipcode}");
     if($errcode  != 0){
     $Config{required} .= ",Chip Code: - $strChip - is not a valid chip Code. ";
 #     &error("- $strChip - is not a valid chip Code. ");
     }
     }
   if ($Step == 5 && $Form{PaperEntry} ne "YES") {
    if ($Form{useabove} eq "YES") { 
      $Form{cardfirstname} =$Form{firstname}  ; 
      $Form{cardlastname} = $Form{lastname} ; 
      $Form{cardaddress} = $Form{address} ;
      $Form{cardcity} = $Form{city} ;
      $Form{cardstate} = $Form{state} ;
      $Form{cardzip} = $Form{zip} ;
      $Form{cardemail} = $Config{email} ;
     }else {
      $Config{'required'} .= ",cardfirstname,cardlastname" ;
      $Config{'required'} .= ",cardaddress,cardcity" ;
      $Config{'required'} .= ",cardstate,cardzip" ;
      $Config{'required'} .= ",cardemail" ;
     }
     if($Form{credit_card_number} eq "TESTING") {
      $Form{credit_card_type} = "VISA";
      $Form{card_expire_month} = "12";
      $Form{card_expire_year} = $year;
      $Form{ccvcode} = "123";
      $Form{TestMode} = "YES";
      $test_mode = "TRUE";
      $Responce_Code = 1 ;
 #     $dtos="TESTING"; $ttos="TESTING"; $Config{control}="TESTING";
     }
      $Config{'required'} .= ",credit_card_type,credit_card_number,card_expire_month,card_expire_year";
if($STYLE ne "PAYMENT"){$Config{'required'} .=",ccvcode";}
     if(length($Form{card_expire_month}) == 1){
      $Form{card_expire_month} = "0".$Form{card_expire_month}; }
    }
    if ($Form{teammember} eq "YES") {
      $Config{'required'} .= ",team_name" ;
    }
    if ($Form{team_name} eq "" && $Form{teammember} eq "YES")  {
       $Form{team_name} = "Other" ; 
     }
    if ($Form{team_name} eq "Other") {
      $Config{'required'} .= ",other_team_name" ;
     }else{
    }
#if($Form{Group}) { $Config{required} .= GroupName,GroupPasswd";}
if($Form{lastname} eq "BENN" && $Form{firstname} eq "BOULTON") { 
    $Config{required} = "email" ; 
  }
    if($Config{email} ne $Config{email2}) { $Config{required} = "E-mail: The two email entries do NOT match";}
    if($Step == 3 && $STYLE eq "TICKETS" &&
      $Form{ForSale1} +
      $Form{ForSale2} +
      $Form{ForSale3} +
      $Form{ForSale4} +
      $Form{ForSale5} +
      $Form{ForSale6} +
      $Form{ForSale7} +
      $Form{ForSale8} +
      $Form{ForSale9} +
      $Form{ForSale10} == 0 )
    { $Config{required} .= ",Pleas Select Items To Purchase.";}
if (substr($Config{required},0,1) eq ",") { $Config{required} = substr($Config{required},1); }
}

sub Step3A 
{
$RACE = $racetitle1 = $EventName;
$Phone = "$Form{Phone}";
$TOTAL = $FEE;
if ($Action eq "FINISH" && $EventID ne "demo") {
 if($BarCodeIt eq "YES"){
  print "<img align=right src=/cgi-bin/dabarcode.cgi?Code39_$Config{control}><br>";
 }
}
print "<table border=0 cellpadding=0 align=center width=600><tr><td>\n" ;
if ($Action eq "FINISH"){print "PLEASE PRINT THIS PAGE FOR YOUR RECORDS"; }
print "</td><td align=right>\n";
if (($Action eq "FINISH" || $Step == 4) && $EventID ne "demo") {
 #print "<img src='http://www.barcodemill.com/cgi-bin/barcodemill/bcmill/barcode.gif?height=25&content=$dtos$ttos'>";
 print "<font color=red>$Config{control} </font> \n" ;
}
print "$Form{GroupName}";
print "</td></tr></table>\n";
print "<div align=left>\n" ;
if($Form{CorporInd} eq "CorpTeam"){
 print"<font color=red size=+1>Team Contact Information</font><br>\n";
 print "COMPANY $Form{CompanyName}<br>";
 print " TEAM $Form{TeamName}";
}
print "<table border=0 cellpadding=0 align=center width=600>";
print "<tr><td width=399>" ;
print "<font size=+1>" ;
print "<pre>" ;
print "    NAME: <b>$Form{lastname}, $Form{firstname} $Form{initial}</b>\n" ;
print " ADDRESS: <b>$Form{address}</b>\n" ;
print "          <b>$Form{city},  $Form{state}  $Form{zip}</b>\n" ;
#print  "\n";
print " COUNTRY: <B>$Form{country}</b>\n"; 
print " PHONE #: <b>$Phone</b>" ;
print "</td><td valign=top width=199>\n";
 if($CatName ne ''){print "<div align=\"right\"><b>&nbsp; &nbsp; $CatName</b></div>";}
if ($STYLE eq "RACE" && $Form{CorporInd} ne "CorpTeam") {
  print "<pre><font size=+1>" ;
  if($Form{EventIDNum} ne ""){print " NUM: <b>$Form{EventIDNum}</b>\n" ;}
  print "      <b>$Form{nationality}</b>\n";
  print "      <b>$Form{gender}</b>";
  print " AGE: <b><font color=red>$Form{RaceDayAge}</font></b>\n" ;
  print "      (on race day)\n" ;
  print " DOB: <b>$Form{DOBmonth}\/$Form{DOBday}\/$Form{DOByear}</b>\n";
  
  if($ChipRace eq "YES" && $AskForChip eq "YES"){
   print "CHIP: <font color=red>$Form{chipcode}";
   if ($Form{chipcode} eq "" ) { print "NONE" ; }
  }
  print "</font></pre>";
}
print "</td></tr>\n";
print "<tr><td colspan=2>";
print "<font size=+1>" ;
print "<pre>   EMAIL: <b>$Config{email}</b>\n</pre>" ;
print "</font>" ;
print "</td></tr>\n";
print "<tr><td>";
#print "$Form{category}\n";
#print "\n<b>$CatName</b>\n";
print "</td><td align=right>\n";
if ($STYLE eq "RACE" && $Form{shirt_size} ne "none") {
print "<b>Shirt Size: $Form{shirt_size}</b><br>\n";
}
print "</td></tr></table>\n";
print "</pre>" ;
#print "<font size=-1>" ;
#print "(If you own your own Champion-Chip and plan to use it at the race, put the code in the spaces below.)</font>\n" ;
print "<font size=+1>" ;
&ExtraItems;
if ($Form{clyfil}) {
  print " I am a ";
  if ($Form{gender} eq "MALE" ) { print "Clydesdale - man 190"; 
    }else {print "Filly - women 140";}
  print " pounds or above.<br>\n";
 }
if ($UseTeams eq "checked") {
 print "<b>Team Name</b> ";
 print "<font color=red>";
 print "$Form{team_name} $Form{other_team_name}" ;
 if ($Form{team_name} eq "") { print "NONE" ; }
 print "</font><br>";
}
if ($Form{KidsNames} ne "" || $KidsNames ne ""){
# print "<br>Kids Fun Run \n";
 print "<br>Additional Signups <br>\n";
# (@Kids)=split(/\r\n/,$Form{KidsNames});
 $TotalKids = 0;
 foreach (@Kids){print sprintf("%-30s %5.2f <br>\n",$_,$funrunfee); ++$TotalKids;}
 #print sprintf("Total Additional Fees \$%5.2f <br>\n",$funrunfee*$TotalKids);
}
if ($Form{TeamMembers} ne ""){
 $TotalTeam = 0;
 print "<br>Team Members \n";
 print "<table><tr><td>Name(first and last)</td><td>Gender</td><td>Age</td><td align=center>Town/State</td>\n";
 if ($FreeShirt) {print "<td>Shirt</td>"; }
 if($ChipRace eq "YES"){ print "<td>Chip</td>";}
 print " </tr><tr>";
# (@TeamMember)=split(/\r\n/,$Form{TeamMembers});
 foreach (@TeamMember){
 ++$TotalTeam ;
 (@tmemberinfo) = split(/\^/,$_);
 print "</tr>";
 print "<td>$tmemberinfo[0] $tmemberinfo[1]</td>\n";
 print "<td Align=center>$tmemberinfo[2]</td>\n";
 print "<td>$tmemberinfo[3]</td>\n";
 print "<td>$tmemberinfo[4], $tmemberinfo[5]</td>\n";
 if ($FreeShirt) {print "<td align=center>$tmemberinfo[6]</td>"; }
 if($ChipRace eq "YES"){ print "<td>$tmemberinfo[7]</td>";}
 print "</tr>";
 sprintf("%-50s %5.2f <br>\n",$tmemberinfo[0],$FEE); }
 print "</table>\n";
if($TotalTeam < $MinTeamSize){ 
     &error('missing_fields',"You must have at least $MinTeamSize members.");
}
}
print "</font>";
print "</font>";
print "</pre>\n" ;
$tbpalign='';
if($STYLE eq "TICKETS"){
print "<center><font color=blue><b>$RACE</b></font></center><br>\n" ;
$tbpalign='align=right';
}
print "<table border=0 $tbpalign>\n" ;
if($STYLE ne "TICKETS"){
  print "<tr><td nowrap align=center><font color=blue><b>$RACE\n" ;
  print "</b></font></td>\n" ;
  print "<td width=200 align=right><font color=green><b> ";
  if($TotalTeam == 0 && $FEE > 0){print sprintf ("\$<b>%5.2f<b>",$FEE);}
  print "</b></font></td></tr>\n" ;
}
if ($TotalTeam > 0) {print "<tr><td align=center><b>Team Members Fees ($TotalTeam)</b></td>\n";
 print "<td align=right><b>";
 print sprintf("%7.2f<b></td></tr>",$TotalTeam*($FEE+0));
 $TOTAL = 0;
 $TOTAL = $TOTAL + ($TotalTeam*($FEE+0));
 $FEE = $TOTAL;
}
if ($TotalKids > 0) {print "<tr><td align=center><b>Total Additional Fees</b></td>\n";
 print "<td align=right><b>";
 print sprintf("%7.2f<b></td></tr>",$TotalKids*$funrunfee);
 $TOTAL = $TOTAL + ($TotalKids*$funrunfee);
 $FEE = $TOTAL;
}
#(@Series)=split(/\, /,$Form{SeriesEvent});
foreach (@Series){
 (@SerEV)=split(/\|/,$_);
 $TOTAL = $TOTAL +$SerEV[2];
 $SeriesAdd = $SeriesAdd +1;
 print "\n<tr><td><font color=blue>$SerEV[3]</font></td>";
 print "<td align=right><font color=green>";
 print sprintf ("\$<b>%5.2f</b></font>",$SerEV[2]);
}
&ForSaleItems;
if ($TYPE eq "ON-LINE") {
 $ServiceCharge = 0 ;
 $Adjustment =~ tr [a-z] [A-Z] ;
 if ($Adjustment eq "ADD"){ 
   $Add1 =$FlatAdd +$SeriesAdd;
   if ($TotalTeam > 0) {$Add1 =($TotalTeam)*($TeamMemberAdd/1) ;}
   $ServiceCharge = (($TOTAL +$Add1) * $PercentAdd)+$Add1 ;
   if ($CompanyID eq "prponh" && $ServiceCharge == 2.05){
#    $ServiceCharge = 2.00  ;
   }
   if($ServiceCharge < $MinServiceCharge) {$ServiceCharge = $MinServiceCharge;}
  }
   if ($TOTAL > $FEE){
     print "<tr><td align=right><b>SUB TOTAL:</b></td><td align=right><b>\$";
     print sprintf ("%9.2f",$TOTAL);
     print "</b></td></tr>";
   }
   if ($ServiceCharge > 0 ) {
    print "<tr><td align=right><b>Service Charge:</b></td><td align=right><b>\$";
    print sprintf ("%9.2f",$ServiceCharge);
    print "</b></td></tr>";
   }
  }
#if ($Form{firstname} eq "BENNY"){print "-ADJ- $Adjustment SC $ServiceCharge";}
#if ($TOTAL+$ServiceCharge > $FEE){
  if($Action eq "FINISH"){$due_paid = 'PAID';}else{$due_paid = 'DUE';}
  print "<tr><td align=right><b>TOTAL $due_paid:</b></td><td align=right><b>\$";
  print sprintf ("%9.2f",$TOTAL+$ServiceCharge);
  print "</b></td></tr>";
#  if ($Action eq "FINISH" && $BarCodeIt eq "YES") {
#    print "<tr><td colspan=2 align=left>";
#    print "<p><img align=right src=/cgi-bin/dabarcode.cgi?Code39_$Config{control}><br></p>";
#    print "</td></tr>";
#  }
#  }
$XTYPE = $TYPE ;
print "</table><br>\n" ;
  if ($TYPE eq "ON-LINE" && $Action eq "FINISH" && $last4 ne "Not displayed on re-prints") {
   print "<p><b><center>";
   print "<font color=green>\n" ;
   print " Card:</font> $Form{ccType} $last4 ";
   print "<font color=green>\n" ;
   print "Expire:</font> $cardexpire ";
  print " <br>Your Credit Card statement will show a charge from<br>";
  print "Computer Comfort Signups";
  print " or Event/Race Signups";
   print "</center></b>";
  }
  print "</div>\n" ;
  if ($TYPE eq "MAIL-IN" && $Step == 4) { &checkto ; }
  if ($MODE1 eq "DISPLAY" && $TYPE eq "MAIL-IN" && $Step == 3) {
   &waiverok ;
   &signature ; 
  }
print "</pre>" ;
#print "</td></tr></table>\n";
if ($Action eq "FINISH"){
if($CompanyID eq "cigna"){
print "<br><h1><a href=http://www.elliothospital.org/cignaroadrace/supportanddonations.htm>I wish to support the Elliot with a donation.</a></h1><br>";
}
 &show_waiver;
  if($BarCodeIt eq "YES"){
#    print "<br><img align=right src=/cgi-bin/dabarcode.cgi?Code39_$Config{control}><br>";
  }
  print "<p>PLEASE PRINT THIS PAGE FOR YOUR RECORDS<br>"; 
 if($STYLE eq "PAYMENT"){ 
print "<a href=http://www.comfortsignups.com/cgi-bin/$scriptname> Again </a><br>";
print "<form action=http://www.comfortsignups.com/cgi-bin/$scriptname> ";
print "<input type=hidden name=ProfileID value=$Form{ProfileID}>";
print "<input type=hidden name=ProfilePassword value=$Form{ProfilePassword}>";
print "<input type=hidden name=Step value=1>";
print "<input type=submit name=Action value=Continue>";
print "</form>";
  }
 }
}

sub print_file(file_name){
open (FE,"$incdir/$Event/$filename") ;
 while (<FE>) { print "$_"; }
 close (FE) ;
}

sub ExtraItems {
 $Ex_match = "_"."$S_match";
#print "1 $Ex_match<br>";
 if($S_match == 0){$Ex_match = "";}
 foreach $exitm (`$GREP "Extra[1-9]=" $optfile`){
   ($xtr,$edata) =split(/\=/,$exitm);
   ($etype,$etext,$evalue,$extra) = split(/\|/,$edata);
   if ($$xtra ne ""){
     if ($etype eq "text"){print "$etsxt ";}
     print "<input type=$etype name=\"$$xtr\" "; 
     if ($etype eq "text"){print "size=$evalue ";}
     print ">";
     print "--$$xtr<br>";
     print "$exitm";
    
  }
}
if($Extra01 ne "") {
 ($etype,$evalue,$esize,$eextra2)= split(/\|/,$Extra1);
 if($Form{other_1} eq "" && $Step == 2) { 
 print "<input type=$etype size=$esize name=other_1 value=\"$evalue\"> $evalue";
 }
 if($Form{other_1} ne "" && $Step > 2) { print "<center><b>$evalue</b></center><br><br>";}
 }

 print "\n";
if (1 == 2) { 
if($Extra1 ne "") {
 ($etype,$evalue,$esize,$eextra)= split(/\|/,$Extra1);
 $esize = $esize +1;
 if($Form{other_1} eq "" && $Step == 2) { 
  if($etype eq "text"){print "$evalue ";}
  print "<input type=$etype size=$esize name=other_1$Ex_match ";
  if($etype eq "checkbox"){print " value=\"$evalue\" ";}
  print ">";
  if($etype eq "checkbox"){print " $evalue";}
  print "<font size=-1> $eextra </font>\n";
 }
 if($Form{other_1} ne "" && $Step > 2) { 
   print "<br><center>";
   if ($etype eq "text") {
     print "<font color=red>$evalue</font> ";
   }
   print "<b>$Form{other_1}</b></center><br><br>";
 }
   print "<br>";
 }
if($Extra2 ne "") {
 ($etype,$evalue,$esize,$eextra)= split(/\|/,$Extra2);
 $esize = $esize +1;
 if($Form{other_2} eq "" && $Step == 2) { 
  print "<br>\n";
  if($etype eq "text"){print "$evalue ";}
  print "<input type=$etype size=$esize name=other_2$Ex_match ";
  if($etype eq "checkbox"){print "value=\"$evalue\" ";}
  print ">";
  print "<font size=-1> $eextra </font>";
 }
 if($Form{other_2} ne "" && $Step > 2) { 
   print "<br><center>";
   if ($etype eq "text") {print "<font color=red>$evalue</font> ";}
   print "<b>$Form{other_2}</b></center><br><br>";
 }
   print "<br>";
 }
 
} #1==2
if($Extra1 ne "") {&ExtraItems2("other_1",$Extra1);}
if($Extra2 ne "") {&ExtraItems2("other_2",$Extra2);}
if($Extra3 ne "") {&ExtraItems2("other_3",$Extra3);}
if($Extra4 ne "") {&ExtraItems2("other_4",$Extra4);}

} # end of sub ExtraItems
sub ExtraItems2
{
 $ThisVal = $_[0];
 $ExtraData = $_[1];
 ($etype,$evalue,$esize,$eextra)= split(/\|/,$_[1]);
 $esize = $esize +1;
 if($Form{$_[0]} eq "" && $Step == 2) { 
  print "<br>";
  if ($etype eq "text" || $etype eq "checkbox"){
   if($etype eq "text"){print "$evalue ";}
   print "<input type=$etype size=$esize name=$_[0]$Ex_match ";
   if($etype eq "checkbox"){print "value=\"$evalue\" ";}
   print ">";
   if($etype eq "checkbox"){print "$evalue";}
   print "<font size=-1> $eextra </font>";
  } else{
   print "$ExtraData<br>";
  }
 }
 if($Form{$_[0]} ne "" && $Step > 2) { 
  print "<br><center>";
  if ($etype eq "text") {print "<font color=red>$evalue</font> ";}
  print "<b>$Form{$_[0]}</b></center><br><br>";
 }
#  print "<br>\n";
} # end of sub ExtraItems2

sub ForSaleItems {
$ForSaleTotal = 0 ;
# for ($i=1;$i<=$FORSALEITEMS;$i++){
 for ($i=1;$i<=10;$i++){
  $x = "ForSale$i" ;
  if ($Form{$x}) {&additem("$Form{$x}","ForSale$i"); }
  }
}
sub additem {
  $qty = $_[0];
  $itemx =$_[1];
  $ITEM = `$GREP $itemx $optfile` ;
  $ITEM = "$$itemx" ;
  ($ctrl,$itm,$price)  = split (/\|/,$ITEM);
#  $price = $_[2];
#  print "$ForSale1 $qty, $itm, $price -<br>";
  $itmtotal = $price*$qty;
  $TOTAL = $TOTAL + $itmtotal;
  $ForSaleTotal = $ForSaleTotal + $itmtotal;
  if($STYLE eq "TICKETS"){$fntAdj='+0';}else{$fntAdj='-1'}
  print "<tr><td align=right><font size=$fntAdj>$qty - $itm </font></td>";
  print "<td align=right><font size=$fntAdj>\$ ";
  print sprintf("%5.2f",$price*$qty);
  print "</font></td></tr>";
}
sub checkto {
  print "<br>Please make checks payable to: <b>$checkto</b><br>" ;
  if ($XTYPE eq "RACE DAY") {
  print "<center><font size=+1 color=red>Please bring this form to sign up at the event</font></center>";
  } else { 
   print "Send check and signed form to:<br><b>$sendto</b><br>";
  }
  print "<br>\n" ;
}
sub get_waiver_ok {
 if (-s "$waiverfile"){
  &show_waiver ;
 print "<p align=center><font color=red size=+1>\n";
 print "<input type=checkbox name=agreement> I agree to conditions of waiver.";
 print "<br>";
 $require = "agreement";
 }
}
sub show_waiver {
  print "</center>"; #<font size=-1>";
  open (WAIV,"$waiverfile") ;
   while (<WAIV>) {print "$_" ; }
  close (WAIV);
  print "</font>";
}

sub waiverok {
  print "<font size=-1>" ;
  &show_waiver;
  if ($TYPE eq "ON-LINE") {
   print "<br><font color=red>[ X ] Yes!</font>" ;
    }else { print "<br><br>(please check here)[ &nbsp ] " ;
   }
  print "<font color=red>I agree to waiver";
  if ($TYPE eq "ON-LINE") { print " and to have my credit card charged" ; }
  print ".</font></center>\n";
  print "</font>" ;
}

sub send_ack_old {
  if($Form{PaperEntry} eq "YES" && $SkipEmail eq "checked") {return;}

  open(MAIL,"|$mailprog -f$EventxEmail");
  $MODE1 = "MAIL" ;
  select MAIL ;
  print "To: $Config{'email'}\n";
  print "From: $Config{'email'}\n";
  print "Subject: $racetitle1\n\n" ;
  print "$Form{firstname} " ;
  print "$Form{lastname}\n" ;
  print "$Form{address}\n" ;
  print "$Form{city}, " ;
  print "$Form{state} " ;
  print "$Form{zip}\n" ;
  print "$racetitle1\n" ;
  print "Confirmation # $Config{control}\n\n" ;
  print "$EventDesc\n\n";
  if ($ChipRace eq "YES"){&pr_chipinfo;}
  if($TYPE eq "ON-LINE") {
   print "Your Credit Card Statemnet will show a charge from\n";
   print "COMPUTER COMFORT SIGNUPS.\n\n";
  }
  print "Event contact:\n";
  print "$EventContact\n";
  print "$EventxEmail\n";
  print "$EventPhone\n";

  if ($TYPE eq "ON-LINE") {
   open (NOTICE, "$incdir/$Event/ccnotice");
  } else {
   open (NOTICE, "$incdir/$Event/mailnotice");
  }
  while (<NOTICE>) { print MAIL $_; }
  close (NOTICE);
  close (MAIL);
  $MODE1 = "DISPLAY" ;
  select STDOUT ;
}

sub send_ack {
  if($Form{PaperEntry} eq "YES" && $SkipEmail eq "checked") {return;}
  $email_text = '';
  $email_cc = '';
  $email_file = '';
  $email_to = "$Config{'email'}";
  $email_from = "$Config{'email'}";
  $email_subject= "$racetitle1" ;
  $email_text .= "$Form{firstname} $Form{lastname}\n" ;
  $email_text .= "$Form{address}\n" ;
  $email_text .= "$Form{city}, $Form{state} $Form{zip}\n" ;
  $email_text .= "$racetitle1\n" ;
  $email_text .= "Confirmation # $Config{control}\n\n" ;
  $email_text .= "$EventDesc\n\n";

  $MODE1 = "MAIL" ;
  if ($ChipRace eq "YES"){&pr_chipinfo;$email_text .= $chipinfotext;}
  $MODE1 = "DISPLAY" ;

  if($TYPE eq "ON-LINE") {
   $email_text .= "Your Credit Card Statemnet will show a charge from\n";
   $email_text .= "COMPUTER COMFORT SIGNUPS.\n\n";
  }

  $email_text .= "Event contact:\n";
  $email_text .= "$EventContact\n";
  $email_text .= "$EventxEmail\n";
  $email_text .= "$EventPhone\n";

  if ($TYPE eq "ON-LINE") {
   open (NOTICE, "$incdir/$Event/ccnotice");
  } else {
   open (NOTICE, "$incdir/$Event/mailnotice");
  }
  while (<NOTICE>) { $email_text .=  $_; }
  close (NOTICE);
}

sub signature {
   open (SIG,"/var/www/custom/html/signature.txt") ;
   while (<SIG>) {print "$_" ; }
   close (SIG);
}
sub do_alert {
  print "<script>\n<!--\n";
   print "alert(\"please print this form your records";
  if ($TYPE eq "MAIL-IN") {
   print " and send it with your payment.";
  }
  print "\\nYou have been sent an E-mail with more information.\")\n";
  print "\n-->\n";
  print "</script>\n";
}

sub ccgetinfo{
 open (CCI, "/var/www/custom/html/ccgetinfo1.htm");
 while (<CCI>) { print "$_";}
 close (CCI) ;
}

sub teamnames {
#print"<br>";
#print " <center>";
#print "</center>If you are a member of a team select here<br>\n";
#print "<option>------------------------------</option>\n";
 if($USATFTeams eq "" ){ $TEAMNAMES ="";}
 if(-f "$incdir/$Event/TeamList.csv"){$TEAMNAMES.=" $incdir/$Event/TeamList.csv";}
#  if($CompanyID eq "cigna"){$TEAMNAMES.=" $incdir/Corporate/TeamList.csv";}
  @TheTeams = `$SORT -u -f $TeamFile $TEAMNAMES `;
 if($TheTeams[0] ne "") {
  print "<br />$TeamTitle<br />";
  print "<select name=team_name size=5>\n";
  print "<option></option>\n";
}
  foreach (@TheTeams){
   chop ;
   $_ =~ tr [a-z] [A-Z];
   if($_ ne ""){print "<option>$_</option>\n";}
   }
  print "</select>";
if($AskOtherTeam ne ""){
print "<br>Other<input type=text name=other_team_name size=40>(if not in above list)";
if($CorpSignup ne ""){
print "<br>You will be designated as Team/Group contact for the new team.";
}
}
#print " <a href=javascript:ShowHelp('teams.htm')>more info</a>\n";
print"</center><br>";
}

sub pr_prices {

if ($SeriesID eq "") {
# $ServiceCharge = sprintf ("%9.2f",($EventFee+$FlatAdd) * $PercentAdd) ;
 print "<tr><td colspan=2>";
  if ($Adjustment ne "INCLUDED") { 
 $ServiceCharge = sprintf ("%9.2f",($EventFee+$FlatAdd) * $PercentAdd) ;
  if ($CompanyID eq "prponh" && $ServiceCharge == 2.05){
#   $ServiceCharge = 2.00  ;
}
  print "<font size=-1>Service Charges Will Be Added In Next Step:</td></td></tr>\n";
  }
  if($EvCatCnt == 0){
   $TOTAL = sprintf("%9.2f",$FEE+$ServiceCharge);
#   print "<tr><td>TOTAL:</td><td align=right>\$$TOTAL</td></tr>\n";
  }
 }
 print " </table></center><br>\n";

}
sub pr_form_fields {
  foreach $field (@Field_Order) {
    local $fname = &clean_html($field);
#    &FORM_input("hidden","$fname","$Form{$field}");
    print "<input type=hidden name=\"$fname\" value=\"$Form{$field}\">\n";
  }
}

sub pr_chipinfo {
#if ($MODE1 eq "DISPLAY") { $chipinfotext .= "<center>";}
$chipinfotext = '';
if($SeriesID){ $chipinfotext .= " One or more of these events";}else{$chipinfotext .= "This event";}
$chipinfotext .= " uses the ChampionChip timing and scoring system.\n";
if ($MODE1 eq "DISPLAY") { 
 $chipinfotext .= "</b>";
 if ($AskForChip eq "YES"){
 $chipinfotext .= "<br><font size=-1 color=#008080>"; 
 $chipinfotext .= "If you own your own ChampionChip and plan to use it at this event, put the code in the space below.";
 $chipinfotext .= "</font><br>"; 
 $chipinfotext .= "<input type=text size=8 maxlength=7 name=chipcode value=\"$ChipCode\"><br>";
 $chipinfotext .= "<font size=-1>";
}
 $chipinfotext .= "If you do not have your own Chip, you will be given one when you register and receive your race bib.\n";
}
 if ($MODE1 eq "DISPLAY") { $chipinfotext .= "<br>"; }
$chipinfotext .= "THE CHIP IS MANDATORY IN ORDER TO BE TIMED.\n";
 if ($MODE1 eq "DISPLAY") { $chipinfotext .= "<br><font color=red>"; }
$chipinfotext .=  "The Chip you receive MUST BE RETURNED or you will charged with a \$35.00 replacement fee.\n\n";
 if ($MODE1 eq "DISPLAY") { print "$chipinfotext</font><br>"; }
}

sub help_script {
print <<"(OPTIONS)";
</font>
<script>
<!--
 function ShowHelp(helpfile)
  {
    window.open(helpfile,null,"height=300,width=400,scrollbars=yes,toolbar=no,menubar=no,location=no");   }
--> 
</script>
(OPTIONS)
}
sub confirm {
 print "<input type=hidden name=type value=\"$TYPE\">\n" ;
 print "<input type=hidden name=EventName value=\"$RACE\">\n" ;
 print "<input type=hidden name=EventFee value=\"$FEE\">\n" ;
 print "<input type=hidden name=lastname value=\"$Form{lastname}\">\n" ;
 print "<input type=hidden name=firstname value=\"$Form{firstname}\">\n" ;
 print "<input type=hidden name=address value=\"$Form{address}\">\n" ;
 print "<input type=hidden name=DLnumber value=\"$Form{DLnumber}\">\n" ;
 print "<input type=hidden name=city value=\"$Form{city}\">\n" ;
 print "<input type=hidden name=state value=\"$Form{state}\">\n" ;
 print "<input type=hidden name=zip value=\"$Form{zip}\">\n" ;
 print "<input type=hidden name=Phone value=\"$Phone\">\n" ;
 print "<input type=hidden name=phone1 value=\"$Form{phone1}\">\n" ;
 print "<input type=hidden name=phone2 value=\"$Form{phone2}\">\n" ;
 print "<input type=hidden name=phone3 value=\"$Form{phone3}\">\n" ;
 print "<input type=hidden name=email value=\"$Config{email}\">\n" ;
 print "<input type=hidden name=email2 value=\"$Config{email2}\">\n" ;
 print "<input type=hidden name=gender value=\"$Form{gender}\">\n" ;
 print "<input type=hidden name=clyfil value=\"$Form{clyfil}\">\n" ;
 print "<input type=hidden name=RaceDayAge value=\"$Form{RaceDayAge}\">\n" ;
 print "<input type=hidden name=DOBmonth value=\"$Form{DOBmonth}\">\n" ;
 print "<input type=hidden name=DOBday value=\"$Form{DOBday}\">\n" ;
 print "<input type=hidden name=DOByear value=\"$Form{DOByear}\">\n" ;
 print "<input type=hidden name=chipcode value=\"$Form{chipcode}\">\n" ;
 print "<input type=hidden name=shirt_size value=\"$Form{shirt_size}\">\n" ;
 print "<input type=hidden name=team_name value=\"$Form{team_name}\">\n" ;
 print "<input type=hidden name=other_team_name value=\"$Form{other_team_name}\">\n" ;
 print "<input type=hidden name=category value=\"$Form{category}\">\n" ;
 print "<input type=hidden name=CompanyName value=\"$Form{CompanyName}\">\n" ;
 print "<input type=hidden name=CompanyTeam value=\"$Form{CompanyTeam}\">\n" ;
 print "<input type=hidden name=KidsNames value=\"$Form{KidsNames}\">\n" ;
 print "<input type=hidden name=other_1 value=\"$Form{other_1}\">\n" ;
 print "<input type=hidden name=other_2 value=\"$Form{other_2}\">\n" ;
 print "<input type=hidden name=other_3 value=\"$Form{other_3}\">\n" ;
 print "<input type=hidden name=other_4 value=\"$Form{other_4}\">\n" ;
 print "<input type=hidden name=other_5 value=\"$Form{other_5}\">\n" ;
 print "<input type=hidden name=country value=\"$Form{country}\">\n" ;
 print "<input type=hidden name=nationality value=\"$Form{nationality}\">\n" ;
 print "<input type=hidden name=ForSale1 value=\"$Form{ForSale1}\">\n" ;
 print "<input type=hidden name=ForSale2 value=\"$Form{ForSale2}\">\n" ;
 print "<input type=hidden name=ForSale3 value=\"$Form{ForSale3}\">\n" ;
 print "<input type=hidden name=ForSale4 value=\"$Form{ForSale4}\">\n" ;
 print "<input type=hidden name=ForSale5 value=\"$Form{ForSale5}\">\n" ;
 print "<input type=hidden name=ForSale6 value=\"$Form{ForSale6}\">\n" ;
 print "<input type=hidden name=GroupID value=\"$Form{GroupID}\">\n";
 print "<input type=hidden name=isProfile value=\"$Form{isProfile}\">\n";
 print "<input type=hidden name=PaperEntry value=\"$Form{PaperEntry}\">\n";
 print "<input type=hidden name=CorporInd value=\"$Form{CorporInd}\">\n";
 print "<input type=hidden name=TeamMembers value=\"$Form{TeamMembers}\">\n";
 print "<input type=hidden name=ForcePrice value=\"$Form{ForcePrice}\">\n";
 print "<input type=hidden name=EventIDNum value=\"$Form{EventIDNum}\">\n";
 (@Series)=split(/\, /,$Form{SeriesEvent});
 $S_matchadd = 0;
 foreach (@Series){
 ++$S_matchadd ;
 $S_shirt = "shirt_size$S_matchadd";
 $S_extra1 = "other_1_$S_matchadd";
 $S_extra2 = "other_2_$S_matchadd";
 $S_extra3 = "other_3_$S_matchadd";
 $S_extra4 = "other_4_$S_matchadd";
 $S_extra5 = "other_5_$S_matchadd";
 $S_extra = "|$Form{$S_shirt}|$Form{$S_extra1}|$Form{$S_extra2}|";
 $S_extra .="$Form{$S_extra3}|$Form{$S_extra4}|$Form{$S_extra5}|";
#$ $S_Shirt =
 #print "1 $S_shirt 2 $S_extra<br>";
 print "<input type=hidden name=SeriesEvent value=\"$_$S_extra\">\n";}
 if ($Step > 3) {
  print "<input type=hidden name=control value=\"$Config{control}\">\n";
 }

}

sub confirm2 {
 print "<input type=hidden name=credit_card_type value=\"$Form{credit_card_type}\">\n";
 print "<input type=hidden name=credit_card_number value=\"$Form{credit_card_.number}\">\n";
 print "<input type=hidden name=card_expire_month value=\"$Form{card_expire_month}\">\n";
 print "<input type=hidden name=card_expire_year value=\"$Form{card_expire_year}\">\n";
 print "<input type=hidden name=cardholdername value=\"$Form{cardholdername}\">\n";
 print "<input type=hidden name=cardaddress value=\"$Form{cardaddress}\">\n";
 print "<input type=hidden name=cardzip value=\"$Form{cardzip}\">\n";
 print "<input type=hidden name=cardemail value=\"$Form{cardemail}\">\n";
 print "<input type=hidden name=ProcessMethod value=\"$Form{ProcessMethod}\">\n";
}

sub make_filedata {
 $dtos = substr($Config{control},0,8);
 $ttos = substr($Config{control},9,6);
 $GENDER = substr($Form{gender},0,1);
 $ccinvoice = "$dtos$ttos";
 $csvline = "\"" ;
 #$csvline = $csvline. "$dtos\",\"" ;
 $csvline = $csvline. "$today\",\"" ;
 $csvline = $csvline. "$ttos\",\"" ;
 $csvline = $csvline. "$RACE\",\"" ;
 $csvline = $csvline. "$TYPE\",\"" ;
 $csvline = $csvline. "$Form{EventIDNum}\",\"" ;
 #$csvline = $csvline. "$TOTAL+$ServiceCharge \",\"" ;
 $csvline = $csvline. "$Form{chipcode}\",\"" ;
 $csvline = $csvline. "$Form{lastname}\",\"" ;
 $csvline = $csvline. "$Form{firstname}\",\"" ;
 $csvline = $csvline. "$Form{address}\",\"" ;
 $csvline = $csvline. "$Form{city}\",\"" ;
 $csvline = $csvline. "$Form{state}\",\"" ;
 $csvline = $csvline. "$Form{zip}\",\"" ;
 $csvline = $csvline. "$Phone\",\"" ;
 $csvline = $csvline. "$Config{email}\",\"" ;
 $csvline = $csvline. "$GENDER\",\"" ;
 $csvline = $csvline. "$Form{RaceDayAge}\",\"" ;
 $csvline = $csvline. "$Form{DOBmonth}\",\"" ;
 $csvline = $csvline. "$Form{DOBday}\",\"" ;
 $csvline = $csvline. "$Form{DOByear}\",\"" ;
 $csvline = $csvline. "$Form{clyfil}\",\"" ;
 $csvline = $csvline. "$Form{shirt_size}\",\"" ;
 $csvline = $csvline. "$Form{team_name}\",\"" ;
 $csvline = $csvline. "$Form{CompanyName}\",\"" ;
 $csvline = $csvline. "$Form{GroupID}\",\"" ;
 $csvline = $csvline. "$Form{DLnumber}\",\"" ;
 $csvline = $csvline. "$Form{other_1}\",\"" ;
 $csvline = $csvline. "$Form{other_2}\",\"" ;
 $csvline = $csvline. "$Form{other_3}\",\"" ;
 $csvline = $csvline. "$Form{other_4}\",\"" ;
 $csvline = $csvline. "$Form{other_5}\",\"" ;
 $csvline = $csvline. "$Form{other_6}\",\"" ;
 $csvline = $csvline. "$Form{other_7}\",\"" ;
 $csvline = $csvline. "$Form{other_8}\",\"" ;
 $csvline = $csvline. "$Form{other_9}\",\"" ;
 $csvline = $csvline. "$Form{other_10}\",\"" ;
 $csvline = $csvline. "$Form{phone2}\",\"" ;
 $csvline = $csvline. "$Form{country}\",\"" ;
 $csvline = $csvline. "$Form{nationality}\",\"" ;
 $csvline = $csvline. "$TOTAL\",\"" ;
 $csvline = $csvline. "$ServiceCharge\",\"" ;
 $csvline = $csvline. "$ForSaleTotal\",\"" ;
 $csvline = $csvline. "$Form{ForSale1}\",\"" ;
 $csvline = $csvline. "$Form{ForSale2}\",\"" ;
 $csvline = $csvline. "$Form{ForSale3}\",\"" ;
 $csvline = $csvline. "$Form{ForSale4}\",\"" ;
 $csvline = $csvline. "$Form{ForSale5}\",\"" ;
 $csvline = $csvline. "$Form{ForSale6}\",\"" ;
 $csvline = $csvline. "$Form{ForSale7}\",\"" ;
 $csvline = $csvline. "$Form{ForSale8}\",\"" ;
 $csvline = $csvline. "$Form{ForSale9}\",\"" ;
 $csvline = $csvline. "$Form{ForSale10}\",\"" ;
 $csvline = $csvline. "$T_COMPANY\",\"" ;
 $csvline = $csvline. "$T_ADDRESS\",\"" ;
 $csvline = $csvline. "$T_CSZ\",\"" ;
 $csvline = $csvline. "$T_CONTACT\",\"" ;
 $csvline = $csvline. "$T_EMAIL\",\"" ;
 $csvline = $csvline. "$T_PHONE\",\"" ;
 $csvline = $csvline. "$Config{control}\",\"";
 $csvline = $csvline. "$CatCode\",\"" ;
 $csvline = $csvline. "$CatName\",\"" ;
 $csvline = $csvline. "\"" ;
}

sub make_additional_data_line {
 $dtos = substr($Config{control},0,8);
 $ttos = substr($Config{control},9,6);
 $Add_csvline = "\"" ;
 $Add_csvline .= "$today\",\"" ;
 $Add_csvline .= "$ttos\",\"" ;
 $Add_csvline .= "$RACE\",\"" ;
 $Add_csvline .= "$TYPE\",\"" ;
 $Add_csvline .= "$KEventIDNum\",\"" ;
 #$Add_csvline .= "$TOTAL+$ServiceCharge \",\"" ;
 $Add_csvline .= "$Kchipcode\",\"" ;
 $Add_csvline .= "$KLast\",\"" ;
 $Add_csvline .= "$KFirst\",\"" ;
 $Add_csvline .= "$Form{address}\",\"" ;
 $Add_csvline .= "$Form{city}\",\"" ;
 $Add_csvline .= "$Form{state}\",\"" ;
 $Add_csvline .= "$Form{zip}\",\"" ;
 $Add_csvline .= "$Phone\",\"" ;
 $Add_csvline .= "$Config{email}\",\"" ;
 $Add_csvline .= "$KGender\",\"" ;
 $Add_csvline .= "$KAge\",\"" ;
 $Add_csvline .= "\",\"" ;
 $Add_csvline .= "\",\"" ;
 $Add_csvline .= "\",\"" ;
 $Add_csvline .= "\",\"" ;
 $Add_csvline .= "$KShirt\",\"" ;
 $Add_csvline .= "\",\"" ;
 $Add_csvline .= "\",\"" ;
 $Add_csvline .= "\",\"" ;
 $Add_csvline .= "\",\"" ;
 $Add_csvline .= "$Kother_1\",\"" ;
 $Add_csvline .= "$Kother_2\",\"" ;
 $Add_csvline .= "$Kother_3\",\"" ;
 $Add_csvline .= "$Kother_4\",\"" ;
 $Add_csvline .= "$Kother_5\",\"" ;
 $Add_csvline .= "$Kother_6\",\"" ;
 $Add_csvline .= "$Kother_7\",\"" ;
 $Add_csvline .= "$Kother_8\",\"" ;
 $Add_csvline .= "$Kother_9\",\"" ;
 $Add_csvline .= "$Kother_10\",\"" ;
 $Add_csvline .= "$Form{phone2}\",\"" ;
 $Add_csvline .= "$Form{country}\",\"" ;
 $Add_csvline .= "$Form{nationality}\",\"" ;
 $Add_csvline .= "$funrunfee\",\"" ;
 $Add_csvline .= "$KServicecharge\",\"" ;
 $Add_csvline .= "\",\"" ;
 $Add_csvline .= "\",\"" ;
 $Add_csvline .= "\",\"" ;
 $Add_csvline .= "\",\"" ;
 $Add_csvline .= "\",\"" ;
 $Add_csvline .= "\",\"" ;
 $Add_csvline .= "\",\"" ;
 $Add_csvline .= "\",\"" ;
 $Add_csvline .= "\",\"" ;
 $Add_csvline .= "\",\"" ;
 $Add_csvline .= "\",\"" ;
 $Add_csvline .= "\",\"" ;
 $Add_csvline .= "$T_ADDRESS\",\"" ;
 $Add_csvline .= "$T_CSZ\",\"" ;
 $Add_csvline .= "$T_CONTACT\",\"" ;
 $Add_csvline .= "$T_EMAIL\",\"" ;
 $Add_csvline .= "$T_PHONE\",\"" ;
 $Add_csvline .= "$Config{control}-$KidCount\",\"";
 $Add_csvline .= "$Kcategory\",\"" ;
 $Add_csvline .= "\"" ;
}

sub add_to_file {
	if ($CompanyID eq 'nedin'){ print " T5 ";}
 if ($Group eq "YES" || $Form{signup_type} eq "GROUP") {
 open (CSV, ">>$incdir/$Event/GRPDATA.csv");
	if ($CompanyID eq 'nedin'){ print " T6 ";}
 }else{
	if ($CompanyID eq 'nedin'){ print " T7 ";}
 open (CSV, ">>$incdir/$Event/REGDATA.pending");
 }
	if ($CompanyID eq 'nedin'){ print " T8 $incdir/$Event / $csvline ";}
 print CSV "$csvline\n" ;
 close (CSV);
 if ($TotalKids > 0) {&add_to_funrun_file ; }
 #if ($TYPE eq "ON-LINE") { &add_to_cc_file ; }
}

sub add_to_funrun_file {
 $dtos = substr($Config{control},0,8);
 $ttos = substr($Config{control},9,6);
 open (CSV, ">>$incdir/$Event/FUNRUN.pending");
 $KidCount = 1;
foreach (@Kids){
 $KidCount++ ;
 ($expm,$expy)= split(/\//,$cardexpire) ;
 ($KLast,$KFirst,$KAge,$KGender,$KShirt) = split( /\,/,$_);
if (1==2){
 print CSV "\"$dtos\",\"" ;
 print CSV "$ttos\",\"" ;
# print CSV "\"$Config{control}\",\"" ;
 print CSV "$RACE\",\"" ;
 print CSV "$TYPE\",\"" ;
 print CSV "$KLast\",\"" ;
 print CSV "$KFirst\",\"" ;
 print CSV "$KAge\",\"" ;
 print CSV "$KGender\",\"" ;
 print CSV "$funrunfee\",\"" ;
 print CSV "$Form{lastname}\",\"" ;
 print CSV "$Form{firstname}\",\"" ;
 print CSV "$Form{address}\",\"" ;
 print CSV "$Form{city}\",\"" ;
 print CSV "$Form{state}\",\"" ;
 print CSV "$Form{zip}\",\"" ;
 print CSV "$Phone\",\"" ;
 print CSV "$Config{email}\",\"" ;
 print CSV "$KShirt\"" ;
 print CSV "\n";
}
 &make_additional_data_line;
# open (r_CSV, ">>$incdir/$Event/REGDATA.csv");
 print CSV "$Add_csvline\n" ;
# close (r_CSV);

 }
 close (CSV);
}

sub make_ccdata {
 $CCFEE = sprintf ("%9.2f",$TOTAL+$ServiceCharge);
 $CCFEE =~ s/ //g;
 $cardexpire = substr($cardexpire,0,3) . substr($cardexpire,5,2) ; 
 $cardnumber =$Form{credit_card_number} ;
 $namepart1 = $Form{firstname};
 $namepart2 = $Form{lastname};
 ($expm,$expy)= split(/\//,$cardexpire) ;
  if (length($expm) == 1) {$expm = "0".$expm ;}
  if (length($expm) == 1) {$expm = "0".$expm ;}
 $expyr = substr($expy,length($expy),2) ;
 $cardnumber =~ s/ //g ;
 $cardnumber =~ s/_//g ;
 $cardnumber =~ s/-//g ;
 $ec_cardnumber = encode2($cardnumber);
 $dtos = substr($Config{control},0,8);
 $ttos = substr($Config{control},9,6);
 $ccinvoice = "$dtos$ttos";
 $infodata = "$EventID|$Form{cardholdername}|$Form{cardemail}|$Phone";
 $infopart1 =substr($infodata,0,17); 
 $infopart2 = substr($infodata,17,10);
 $infopart3 = substr($infodata,27,20);
 $infopart4 = substr($infodata,47,20);
# $ccinfoline = $ccinfoline."\$Form{cardholdername}\",";"
 $ccinfoline = $ccinfoline."\"$ccinvoice\",";
 $ccinfoline = $ccinfoline."\"$RACE\",";
 $ccinfoline = $ccinfoline."\"$CCFEE\",";
 $ccinfoline = $ccinfoline."\"CC\",";
 $ccinfoline = $ccinfoline."\"\",";
 $ccinfoline = $ccinfoline."\"$Approval_Code\",";
 $ccinfoline = $ccinfoline."\"$Transaction_ID\",";
 $ccinfoline = $ccinfoline."\"$last4\",";
 $ccinfoline = $ccinfoline."\"$cardexpire\",";
 $ccinfoline = $ccinfoline."\"$Config{control}\",";
 $ccinfoline = $ccinfoline."\"$Form{cardfirstname}\"," ;
 $ccinfoline = $ccinfoline."\"$Form{cardlastname}\"," ;
 $ccinfoline = $ccinfoline."\"$Form{firstname} $Form{lastname}\"," ;
 $ccinfoline = $ccinfoline."\"$Form{cardaddress}\",";
 $ccinfoline = $ccinfoline."\"$Form{cardcity}\"," ;
 $ccinfoline = $ccinfoline."\"$Form{cardstate}\"," ;
 $ccinfoline = $ccinfoline."\"$Form{cardzip}\",";
 $ccinfoline = $ccinfoline."\"$Phone\"," ;
 $ccinfoline = $ccinfoline."\"$Form{cardemail}\",";
 $ccinfoline = $ccinfoline."\"$Form{ccvcode}\",";
 $ccinfoline = $ccinfoline."\"$TOTAL+$ServiceCharge \"," ;
 $ccinfoline = $ccinfoline."\"$Transaction_ID\",";
 $ccinfoline = $ccinfoline."\"$Approval_Code\",";
 $ccinfoline = $ccinfoline."\"$TOTAL\"," ;
 $ccinfoline = $ccinfoline."\"$ServiceCharge \"," ;
 $ccinfoline = $ccinfoline."\"$Address_VCode\",";
 $ccinfoline = $ccinfoline."\"-$First_4--$Last_4-\",";
 $ccinfoline = $ccinfoline."\"\"";
}
sub add_to_cc_file {
 open (CC, ">>$incdir/$Event/CCDATA.csv");
  print CC "$ccinfoline\n";
 close (CC) ;
}
sub SaveMe {
 print "saving profile $PID - $ttos<br>\n";
 $PID = substr($Form{firstname},0,3).substr($Form{lastname},0,3).$Form{state} ;
 $csvline = "|$PID|$ttos" ;
 $csvline = $csvline. "|$Form{lastname}" ;
 $csvline = $csvline. "|$Form{firstname}" ;
 $csvline = $csvline. "|$Form{address}" ;
 $csvline = $csvline. "|$Form{city}" ;
 $csvline = $csvline. "|$Form{state}" ;
 $csvline = $csvline. "|$Form{zip}" ;
 $csvline = $csvline. "|$Phone" ;
 $csvline = $csvline. "|$Config{email}" ;
 $csvline = $csvline. "|$Form{gender}" ;
 $csvline = $csvline. "|$Form{DOBmonth}" ;
 $csvline = $csvline. "|$Form{DOBday}" ;
 $csvline = $csvline. "|$Form{DOByear}" ;
 $csvline = $csvline. "|$Form{chipcode}";
 $csvline = $csvline. "||||" ;
#open (SM, ">>/var/race/members/Pprofiles");
open (SM, ">/var/race/members/PROFILES/$PID-$ttos");
 print SM "$csvline\n";
close (SM) ;
open(MAIL,"|$mailprog1");
  print MAIL "To: $Config{'email'}\n";
  print MAIL "From: Signups\@comfortinc.com (Comfort Signups)\n";
  print MAIL "Subject: $racetitle1\n\n" ;
 print MAIL "Comfort Signups Profile\n\n";
 print MAIL "Profile ID: $PID\n";
 print MAIL "Password: $ttos\n" ;
  print MAIL "$Form{firstname} " ;
  print MAIL "$Form{lastname}\n" ;
  print MAIL "$Form{address}\n" ;
  print MAIL "$Form{city}, " ;
  print MAIL "$Form{state} " ;
  print MAIL "$Form{zip}\n" ;
  print MAIL "$Phone\n" ;
  print MAIL "$Config{email}\n" ;
  print MAIL "$Form{gender}\n" ;
  print MAIL "$Form{DOBmonth}/" ;
  print MAIL "$Form{DOBday}/" ;
  print MAIL "$Form{DOByear}\n" ;
  print MAIL "\n";
  print MAIL "Thank you for creating a Comfort Signups profile.\n\n";
close (MAIL);
}

sub save_data {
#$TYPE = "ON-LINE";
 if ($TYPE eq "ON-LINExxx") { 
 $CC1FILE = "$incdir/$Event/ccfiles/$Config{control}";
 open (CC, ">>$CC1FILE");
	if ($CompanyID eq 'nedin'){ print " CC1 ";}
#  print CC "\"S\",\"$cardnumber\",\"$cardexpire\",\"$CCFEE\",";
  print CC "$ccinfoline,$csvline\n";
 close (CC) ;
} 
 if ($Group eq "YES" || $Form{signup_type} eq "GROUP") {
 $CSVFILE = "$incdir/$Event/grpfiles/$Config{control}";
 }else{
 $CSVFILE = "$incdir/$Event/regfiles/$Config{control}";
  if ($CompanyID eq 'nedin'){ print " RG1=$CSVFILE<br>";}
 }
 $CSVFILE = "$incdir/$Event/pending/$Config{control}";
 open (CSV, ">> $CSVFILE")   ;
 print CSV "$csvline\n" ;
 close (CSV);
#print "CC $CC1FILE<br>CSV $CSVFILE<br>";
if (!-f "$CSVFILE" && !-f "$CCFILE"){&error("There was a problem saving your information!<p>Please contact Support at 603 865-7352 ASAP</p>$CCFILE<br>$CSVFILE");}
}
sub Free_Shirt {
    $FreeShirt = "$tsizeC$tsizeS$tsizeM$tsizeL$tsizeXL$tsizesXXL" ;
    print "<center><font size=+1>\n";
    if($SeriesID ne "") { 
     print "<br><font color=red>Not all of the above events may offer ";
     print "free shirts, for thoose that do...</font><br>";
#     $ShirtSizes = "S M L XL XXL ";
    }else{
    $ShirtSizes = "";
    if ($tsizeS) { $ShirtSizes .= "S "; }
    if ($tsizeM) { $ShirtSizes .= "M "; }
    if ($tsizeL) { $ShirtSizes .= "L "; }
    if ($tsizeXL) { $ShirtSizes .= "XL "; }
    if ($tsizeXXL) { $ShirtSizes .= "XXL "; }
    if ($tsizeC) { $ShirtSizes .= "Child"; }
    }
    if ($ShirtSizes  ne "" ) {
     &PickShirt;
      }else{
      &FORM_input("hidden","shirt_size","none");
     }
  print "</center><br>\n";
}

sub PickShirt {
 if($S_match == 0){$S_match = "";}
 $RButton = "<input type=radio name=shirt_size$S_match";
 $PickShirt = "$tsizeC$tsizeS$tsizeM$tsizeL$tsizeXL$tsizesXXL" ;
 if($SeriesID ne ""){ print "&nbsp $Shirt ";}
 if($PickShirt ne ""){
 print " </font>Please Select Your Free Shirt Size For This Event.<br>" ;
 $PickShirt = "";
 }else{print "<br>no shirt <input type=hidden name=shirt_size$S_match value=none>";}
 print "<table><tr><td width=20\%>";
 if($tsizeS ||$SeriesID1 ne ""){print "$RButton value=Small>Small\n"; } 
 print "</td><td width=20\%>";
 if($tsizeM ||$SeriesID1 ne ""){print "$RButton value=Medium>Medium\n"; }
 print "</td><td width=20\%>";
 if($tsizeL ||$SeriesID1 ne ""){print "$RButton value=Large>Large\n"; }
 print "</td><td width=20\%>";
 if($tsizeXL ||$SeriesID1 ne ""){print "$RButton value=X-Large>XLarge\n"; }
 print "</td><td width=20\%>";
 if($tsizeXXL ||$SeriesID1 ne ""){print"$RButton value=XX-Large>XXLarge\n"; }
 if ($ShirtNote ne ""){print "</tr></td><tr><td colspan=\"5\">$ShirtNote\n";}
print "</td></tr></table>";
 if($tsizeC){print "$RButton value=Child>Child\n"; }
if($Form{PaperEntry} eq "YES" && $NoShirt eq "checked") {
 print"$RButton value=no-shirt checked>No Shirt\n"; 
}
#  print "<br>\n";
}
sub AskSigningUp {
 &New_Table("Ask Signup") ;
 print "I am signing up ....<br> ";
 print "<input type=radio name=CorporInd value=Single checked>Just Me\n";
 print "<br>\n";
 print "<input type=radio name=CorporInd value=CorpTeam>Complete Team.";
# print "<br>";
 &teamnames;
}
sub CorpSignup {
 print "</center>";
 print " <font color=blue>The above information is the team contact.</font> <br>\n";
 print "<Font color=green>";
 print "Please Enter <br><font color=red>";
 print "First Name, Last Name, Gender, Age, Town, State";
 if ($FreeShirt) {print ", Shirt Size"; }
 print "</font>";
 if($ChipRace eq "YES"){ print "<font color=red>, Chip Code </font><font size=-1>(if member has his/her own ChampionChip)</font>";}
 print  "<br>of each team member<br>\n";
 print "<br>\n";
 print " example: <font color=blue>Benn,Boulton,M,35,Newport,NH";
 if ($FreeShirt) {print ",L </font>"; 
 if($ChipRace eq "YES"){ print "<font color=blue>,??????? </font> (???????= 7 diget chip code)";}
 print "<br>Shirt sizes are $ShirtSizes"; }
 print "</font></center>";
 print "<font color=red>";
 print "If person above is also on the team, please re-enter below.<br>";
 print "</font>";
 print "Minimun Team Members is $MinTeamSize<br>\n";
 print "<textarea name=TeamMembers rows=8 cols=60 wrap=off></textarea><br>\n";

}

sub ShowSeries {
$RACELIST ="/var/race/RACE_LIST" ;
$SeriesList = `$GREP "$SeriesID|" /var/race/SeriesList`;
($SerID,$SeriesName,$SeriesOff,$SeriesRule) = split(/\|/,$SeriesList);
if($dtos > $SeriesOff){$SeriesID=""; return;}
$X_Comp = "xx$CompanyID";
$X_Event = $EventID;
$S_match=0;
foreach (`$SORT -t '|' -k 5.1 $RACELIST |$GREP "|ACTIVE|$SeriesID|" `){
 ($S_Comp,$S_Event,$S_Name,$S_Town,$S_State,$S_EVDate,$REST) = split(/\|/,$_);
if($dtos < $S_EVDate) {
#if($X_Comp ne $S_Comp && $X_Event ne $S_Event) {
if ($S_match == 0) {
print "</font>This Event is part of the ";
print "<font color=blue size=+1>$SeriesName</font>.<br>";
print "You may also sign up for...<br>\n";
print "$SeriesRule<br>";
print "<table border>\n";
#$S_match=1;
}
++$S_match;
&getraceopts("$incdir/$S_Comp/$S_Event/raceopts") ;
$SeriesID=$SerID;
$S_Date = "$months[$EventMonth-1] $EventDay, $EventYear";
 print "<tr><td nowrap>\n";
if($X_Comp ne $S_Comp && $X_Event ne $S_Event) {
 print "<input type=checkbox name=SeriesEvent value=\"$S_Comp|$S_Event|$EventFee|$EventName|$Adjustment\">";
 }else { print "-X-"; --$S_match;
 print "<input type=hidden name=XSeriesEvent value=\"||||\">";
 #print "<input type=hidden name=SeriesEvent value=\"$S_Comp|$S_Event|$EventFee|$EventName|$Adjustment\">";
 }
 print "</td><td nowrap> $EventName";
 &PickShirt;
 &ExtraItems;
 print "</td><td> $S_Town, ";
 print "$S_State</td> ";
 print "<td align=right>$S_Date</td> ";
 print sprintf("<td nowrap><font color=green>\$%9.2f </font></td>",$EventFee );
 print "<td nowrap>";
# print "$S_match";
# &PickShirt;
# &Free_Shirt;
 print "</td></tr>\n";
#}
}
}
 print "</table>\n";
&getraceopts("$optfile") ;
if($Form{ForcePrice} ne ""){$EventFee=$Form{ForcePrice} ;}
}

sub send_the_email{
  $mwrrmessage=$mwrrmessage."New Signup\n\n$csvline";
  $mwrrto = "Racetime\@gsrs.com";
  $mwrrfrom = "Signups\@comfortsignups.com";
  $mwrrsubject = "Late/Free MWRR SIGNUP";
  $mwrrfile = "$CSVFILE";
  $mwrrfilename= "$outputfile";
#  $mailparms = "-f$mwrrfrom";

  $boundaryline= "NextPart_csvfile";

use MIME::Lite;

my $msg = MIME::Lite->new(
    From    => "$email_from",
    To      => "$email_to",
    Cc      => "$email_cc",
    Subject => "$email_subject",
    Type    => 'multipart/mixed'
);

$msg->attach(
    Type     => 'TEXT',
    Data     => "$email_text"
);

if($email_file ne '') {
$msg->attach(
    Type     => 'application/octet-stream',
    Path     => "$email_file",
    Filename => "$email_filename"
);
}

$msg->send;
}

sub _show_reprint_form {
  &form_begin("reprint","$FORM1");
print "<br> To re-print a Confirmation page, fill in your Registration Code and click Re-Print.<br>";
print "<input type=\"text\" name=\"RegCode\" size=\"20\">";
print "<input type=\"hidden\" name=\"RePrintIt\" value=\"ReIndexIt\">";
&form_end("1","Re-Print");
}
