<?php_track_vars?>
<?php

// Include common functions:

include ("common.inc");

// Function handles missing required fields
function output_missing ($aryMissing) {

 // Get HTML fragment filenames
 include("files.inc");

 // Print out html
 include($incHeader);
 include($incMissing);
 echo "Missing field(s):<P>\n";
 for ($p = 0 ; $p < count($aryMissing) ; $p++) {
    echo "<FONT COLOR=\"FF0000\">".$aryMissing[$p]."</FONT><BR>\n";
 } // endwhile
 echo "</FONT>\n";
 include($incFooter);

}   // endfunc


// Function handles bad address
function output_bad_address ($address) {

 // Get HTML fragment filenames
 include("files.inc");

 // Print out html
 include($incHeader);
 include($incBadfrom);
 echo "Bad Address: <FONT COLOR=\"FF0000\">";
 echo $address."\n";
 echo "</FONT>\n";
 include($incFooter);

}   // endfunc


// Function handles opening screen
function output_welcome ($selfname) {

 // Get HTML fragment filenames
 include("files.inc");

 // Print out html
 include($incHeader);
 include($incWelcome);
 echo "<FORM ACTION=\"$selfname\" METHOD=POST>\n";
 include($incForm);
 echo "<input type=\"submit\" name=\"submit\" value=\"Send!\">";
 echo "</FORM>\n";
 include($incFooter);

}   // endfunc


// Function handles thanks screen
function output_thanks ($selfname, $again = 0) {

 // Get HTML fragment filenames
 include("files.inc");

 // Print out html
 include($incHeader);
 include($incThanks);
 if ( $again ) {
    echo "<FORM ACTION=\"$selfname\" METHOD=POST>\n";
    include($incForm);
    echo "<input type=\"submit\" name=\"submit\" value=\"Send!\">";
    echo "</FORM>\n";
 }   // endif
 include($incFooter);

}   // endfunc



// <--- Main Program --->

if (!empty($submit)) {

 // Initialize array of all possible defaults
 $aryConfig = array("to","subject","from","autofrom","autoprefix",
                    "autosubject","sendextras","autoextras",
                    "template","autoreply","log","askagain",
                    "required","checkfrom","logbodies");

 // Set blank variables to their defaults
 for ($c = 0; $c < count($aryConfig); $c++) {
    ${$aryConfig[$c]} = default_ifempty ($aryConfig[$c],${$aryConfig[$c]});
    if (empty (${$aryConfig[$c]})) {
       unset (${$aryConfig[$c]});
    }   // endif
 }   // endfor

 // Start branching depending on values...
 
 if ( check_required($required,$aryMissing) ) {
    output_missing($aryMissing);
 } elseif (($checkfrom) and (strstr("$required",'from')) and (bad_address($from)) ) {
    output_bad_address($from);
 } else { 
    if ($log) {
       log_it ($to, $from, $subject, $body);
       $logged = 1;
    }   // endif
    if ((!empty ($to)) or (!empty ($autofrom))) {
       mail_it ();
       $mailed = 1;
    }   // endif
    $whoami = basename(getenv("PATH_INFO"));
    output_thanks($whoami,$askagain);
 }   // endif (submitted branch)
} else {
   $whoami = basename(getenv("PATH_INFO"));
   output_welcome($whoami);
}   // endif (main branch)

?>
