<?
function HTML_header(){
global
$antwoord,$cvsfile,$tournooi,$sluiting,$workdir,$workfile,$delfile;
echo
'
<html>
<head>
<title>'
,$antwoord,' Inschrijf formulier</title>
<style>
body {font-size:12pt;}
a {text-decoration:none}
.post {font-family:Courier,monospace}
.posting {color:blue;background:#FFFFCC}
.pre {page-break-after:always}
</style>
</head>
<body link=#0033CC vlink=#0033CC alink=#0033CC>
<form action=E'
,$workfile,'.php method=POST>
<input type=hidden name=work value='
,$workdir,'>
<input type=hidden name=prog value="Icmd2csv '
,$delfile,'.csv">
<table align=center border=1 cellspacing=0 cellpadding=1 width=100% bgcolor=FFFFCC>
<caption align=center><h1>Inschrijfformulier '
,$tournooi,'</h1>
<br>Sluitingsdatum is '
,$sluiting,'<br>&nbsp;</caption>';
}

function
organisatie(){
global
$org;
echo
'
<tr><td colspan=8 align=center><b><u>Organisatie gegevens</u></b></td></tr>
<tr>
<td colspan=4>Sch/Ver. <input class=post type=text name=org value="'
,$org[0],'" size=20></td>
<td colspan=1>Aansluitnr. <input class=post type=text name=aansl value="'
,$org[1],'" size=4></td>
<td colspan=3>Telefoon <input class=post type=text name=tel value="'
,$org[2],'" size=11>
&nbsp Fax <input class=post type=text name=fax value="'
,$org[3],'" size=11></td>
</tr>
<tr>
<td colspan=4>Kontaktpersoon <input class=post type=text name=wie value="'
,$org[4],'" size=10></td>
<td colspan=4>eMail adres <input class=post type=text name=mail value="'
,$org[5],'" size=40></td>
</tr>
<tr>
<td colspan=4>Straat <input class=post type=text name=straat value="'
,$org[6],'" size=20></td>
<td colspan=1>Postcode <input class=post type=text name=postcode value="'
,$org[7],'" size=6></td>
<td colspan=3>Plaats <input class=post type=text name=plaats value="'
,$org[8],'" size=14></td>
</tr>
<tr><td colspan=8 align=center><font color=red>Maak de text <b>niet langer</b> dan het textvenster groot is !</font></td></tr>'
;
}

function
deelnemer() {
global
$deelname,$toelichting;
echo
'
<tr><td colspan=8 align=center><b><u>Deelnemers</u></b></td></tr>
<tr><td colspan=8 ><b>Toelichting:</b>'
,$toelichting,'</td></tr>
<tr><th width=07%>Nr:</th><th width=08%>J/M</th><th width=10%>Geboorte-datum</th>
     <th width=10%>Gewicht<br>(in Kg)</th><th width=20%>Roepnaam</th><th width=30%>Achternaam</th>
     <th width=05%>Band</th><th width=10%>Paspoort-nummer</th>
</tr> '
;

$deelnemer=array('geslacht','geboren','gewicht','roepnaam','achternaam','band','pasno');
$deelsize=array(1,6,3,10,20,2,6);
/* genereer 20 deelnemers */
for ($g=1;$g<=20;$g++) {
   
/* iedere deelnemers heeft een variabelen reeks van 0..6 */
   
echo '<tr><td align=right>',$g,'</td>'; /* deelnemer nummer */
   
for ($i=0;$i<count($deelnemer);$i++) {
     echo
'<td align=center><input class=post type=text name=',$deelnemer[$i].($g*10+$i),
       
' value="',$deelname[$g-1][$i],
       
'" size=',$deelsize[$i],'>';
       if (
$i==2) echo 'Kg.';
       echo
'</td>';
   }
   echo
'</tr>';
}
}

function
toelichting_antwoord() {
global
$aantaldeelnemers,$deelname,$deelnemerfout,$deelname_error;
global
$org,$orgfout,$org_error;
    echo
'
     <tr><td colspan=8 ><b> Antwoord Toelichting:</b>
     <br>'
;
    if (
$deelnemerfout+$orgfout==0) {
     echo
'
     <br>Zo te zien is het goed ingevuld . U hebt '
,$aantaldeelnemers,' deelnemers opgegeven .
     <br>Met <font color=blue>verzenden</font> wordt Uw opgave verstuurd . Met <font color=blue>wijzigen</font> kunt U de huidige gegevens wijzigen .
     <br>&nbsp;
     <br>Maak een <b>afdruk</b> van deze pagina voor U deze verstuurd .'
;
//     if (strlen($org[5])!=0) {/* eMail */
//    echo '<br>Als U deze inschrijfing <b>niet</b> per eMail terug krijgt , is het eMail adres fout .';
//     }
     
echo '</td>';
    } else {
    if (
$orgfout!=0) {/* fout analyse */
               
echo '<br><font color=red><b>Fout</b></font> in Sch/Ver. gegevens er is geen ',$org_error[$orgfout-1],' gegeven .';
               if (
$orgfout==2) echo ' Dit nummer moet 4 cyfers bevatten .<br>Indien U het echt niet weet geef 9900 . (If unknown give 9900)';
               echo
'<br>';
    }
        if (
$deelnemerfout!=0) {
          if (
$aantaldeelnemers) {
        if (
$aantaldeelnemers==1) {
                echo
'<br>U hebt ',$aantaldeelnemers,' deelnemer opgegeven . ';
            echo
'Er is ',$deelnemerfout,' <font color=red><b>fout</b></font> gezien .';
        } else {
                echo
'<br>U hebt ',$aantaldeelnemers,' deelnemers opgegeven . ';
            echo
'zijn ',$deelnemerfout,' <font color=red><b>fouten</b></font> gezien .';
        }
            
/* fout analyse */
            
for  ($i=0;$i<20;$i++) {
                 if (
$deelname[$i][7]!=0) { /* some error */
                 
echo '<br>Fout in deelnemer : ',$i+1,' is ',$deelname_error[$deelname[$i][7]-1],' .';
                 }
            }
          } else {
            echo
'<br><b>Onzin</b> , U hebt helemaal geen deelnemers opgegeven !!';
          }
        }
        echo
'
        <br>Met <font color=blue>wijzigen</font> kunt U de gewijzigde gegevens opnieuw versturen .
        <br>&nbsp;</td>'
;
    }
     echo
'</tr>';
}

    
// seed with microseconds
function make_seed() {
    list(
$usec, $sec) = explode(' ', microtime());
    return (float)
$sec + ((float) $usec * 100000);
}

function
tocvs($file){
global
$org,$deelname,$aantaldeelnemers,$datestamp,$IDstamp;
    
$outfile=fopen($file.'.csv','a');
    
flock($outfile,2);

    
mt_srand(make_seed());

    
$IDstamp=$file.mt_rand(1000,9999);/* betalings ID (mutual excluded area ?)*/
    
$datestamp=date('dmy,His');
    for (
$i=0;$i<9;$i++){
       
fwrite($outfile,$org[$i].',');
    }
    
fwrite($outfile,$datestamp.','.$IDstamp."\n");
    for (
$i=0;$i<$aantaldeelnemers;$i++){
       for (
$j=0;$j<6;$j++){
           
fwrite($outfile,$deelname[$i][$j].',');
       }
       
fwrite($outfile,$deelname[$i][6]."\n");
    }
    
flock($outfile,3);
    
fclose($outfile);
}
/* gobals */

$org=array('','','','','','','','','');
$deelname=array(array('','','','','','','',0),
                array(
'','','','','','','',0),
                array(
'','','','','','','',0),
                array(
'','','','','','','',0),
                array(
'','','','','','','',0),
                array(
'','','','','','','',0),
                array(
'','','','','','','',0),
                array(
'','','','','','','',0),
                array(
'','','','','','','',0),
                array(
'','','','','','','',0),
                array(
'','','','','','','',0),
                array(
'','','','','','','',0),
                array(
'','','','','','','',0),
                array(
'','','','','','','',0),
                array(
'','','','','','','',0),
                array(
'','','','','','','',0),
                array(
'','','','','','','',0),
                array(
'','','','','','','',0),
                array(
'','','','','','','',0),
                array(
'','','','','','','',0),
                array(
'','','','','','','',0));
                
$aantaldeelnemers=0;
$deelnemerfout=0;
$datestamp='oef fout';
$IDstamp='0000';

/* main program */

/* is PHP gestart van af de command line of via CGI */
//print_r($_SERVER);  echo strlen($_SERVER['PHP_SELF']),'<br>';
if ( (strlen($_SERVER['PHP_SELF'])==0) )
     { include
$_ENV['include_path'].'loadpost.php'; /* loading $_POST from file ; this run offline with PEU server */
       
$workfile=$_SERVER['argv'][0]; }
else
/* moet ook de dir/dir/file.php er uit gesloopen*/
     
{ $workfile=strrchr($_SERVER['PHP_SELF'],'/');
       if (
$workfile) {$workfile=substr($workfile,1);}
       else {
$workfile=$_SERVER['PHP_SELF'];}
     }

/* sloop de .ext uit de file naam */
$workfile=substr($workfile,1,strpos($workfile,'.')-1);
//echo $workfile,'<br>';

if (count($_POST)==0) {              /* inschrijving */
    
$antwoord='Invoer ';
    
HTML_header();
    
organisatie();
    
deelnemer();
} else {
    
error_check();                   /* antwoord */
    
if (($_POST['verstuur']!='versturen') || ($deelnemerfout+$orgfout!=0)) {
           
$antwoord='Wijzig ';      /* wijzigingen */
           
HTML_header();
           
toelichting_antwoord();
           
organisatie();
           
deelnemer();
    } else {
$antwoord='Verstuurd '; /* verstuur */
           
HTML_header();
       
$_POST=array_slice($_POST,0,$aantaldeelnemers*7+11);/* verwijderd alle lege deelnemers */
       /* append to csv file */
       
tocvs($delfile);
       
/* maak een begeleidend briefje */
       
$letter='bijgaand het inschrijfformulier van '.$org[0].". \nVerstuurd : ".$datestamp.'. ID='.$IDstamp;
       
$letter=$letter."\n\nBevat ".$aantaldeelnemers." deelnemers \n";
           for (
$i=0;$i<$aantaldeelnemers;$i++){
              for (
$j=0;$j<6;$j++){
              
$letter=$letter.$deelname[$i][$j].' , ';
              }
              
$letter=$letter."\n";
           }
           
$letter=$letter."\n\nAfz : ".$org[4]."\n     ".$org[6]."\n     ".$org[7].' '.$org[8]."\nTel :".$org[2];
       
/* put date,time,ID into POST */
       
$_POST=array_slice($_POST,0,11)+
              array(
'date'=>substr($datestamp,0,strpos($datestamp,',')),'time'=>substr($datestamp,1+strpos($datestamp,',')),'ID'=>$IDstamp)+
          
array_slice($_POST,11,$aantaldeelnemers*7) ;
       
/* send eMail to */
       
include $_ENV['include_path'].'sendmail.php';
       
$attach_files= array($delfile.'.csv','POSTDATA.ATT');
       if (!
is_array($orgmail)) { $orgmail=array($orgmail); }
       for (
$i=0;$i<count($orgmail);$i++) {
            
SendMail($orgmail[$i],$_POST['mail'],'inschrijving '.$org[0],$letter,$attach_files,$attach_files);
       }
       echo
'<tr><td colspan=8 align=center>Uw inschrijfgegevens zijn opgeslagen en doorgestuurd . Timestamp = '.$datestamp.'</tr></td>';
    }
}
echo
'</table>';

/* betalingen */
if (($antwoord=='Verstuurd ') && ($deelnemerfout+$orgfout==0) && ($inschrijfgeld!=0)) {
   echo
'<br>
   <center>U dient '
,$inschrijfgeld*$aantaldeelnemers,' Euro te storten op rekening ',$rekening,' .
   <br>Onder vermelding van <b>'
,$IDstamp,'</b></center>';
}

/* HTML footer */
echo '<p><table align=center border=1 cellspacing=0 cellpadding=0 width=90% bgcolor=FFFFCC><tr>';
if (
count($_POST)==0) {
  echo
'<td align=center width=50%><input class=posting type=submit name=verstuur value=inschrijven></td>
        <td align=center width=50%><input class=posting type=reset value=opnieuw></td></tr>'
;
} elseif ((
$antwoord=='Verstuurd ') && ($deelnemerfout+$orgfout==0)) {
  echo
'<td align=center><a href=',$home_page,'>back-home</a></td>';
} else {
  echo
'<td align=center width=50%><input class=posting type=submit name=verstuur value=wijzigen></td>
        <td align=center width=50%><input class=posting type=submit name=verstuur value=versturen></td>
    <tr align=center><td colspan=2>'
,$afzender,'</td></tr>
    </table>
    </form>
        </body>
        </html>'
;
}
?>