<html><head>
<?php

global $vars;

function initVars ()
{
    global $vars;
    $vars = array(
        "name" => "",
	"address" => "",
	"kinname" => "",
	"kinaddress" => "",
	"phone" => "",
	"kinphone" => "",
	"email" => "",
	"kinemail" => "",
	"relation" => "",
	"veggie" => "no",
	"student" => "yes",
	"insurance" => "BCRA",
	"bcranum" => "",
	"leave_uk" => "",
	"length_of_stay" => "",
	"transport_ok" => "yes",
	"transport_out" => "need",
	"transport_back" => "need",
	"from_uk" => "",
	"returning_uk" => "",
	"tent" => "no",
	"top_tent_cap" => "",
	"btent" => "no",
	"base_tent_cap" => "",
	"tent_share" => "yes",
	"share_condition" => "",
	"medic_info" => "",
	"extra_info" => "",
	"aims" => "",
	"lifts_out" => "yes",
	"lifts_back" => "yes",
	"no_out" => "2",
	"no_back" => "2",
	"take_out" => "",
	"take_back" => "",
	"transport_info" => "",
	"medication" => "",
	"allergies" => "",
	"bivvy" => ""
    );
}

function fillVars ()
{
    initVars ();

    global $vars;
    $vars[name] = stripslashes($_POST[name]);
    $vars[address] = stripslashes($_POST[address]);
    $vars[phone] = stripslashes($_POST[phone]);
    $vars[kinname] = stripslashes($_POST[kinname]);
    $vars[kinaddress] = stripslashes($_POST[kinaddress]);
    $vars[phone] = stripslashes($_POST[phone]);
    $vars[kinphone] = stripslashes($_POST[kinphone]);
    $vars[email] = stripslashes($_POST[email]);
    $vars[kinemail] = stripslashes($_POST[kinemail]);
    $vars[relation] = stripslashes($_POST[relation]);
    $vars[veggie] = stripslashes($_POST[veggie]);
    $vars[student] = stripslashes($_POST[student]);
    $vars[transport_ok] = stripslashes($_POST[transport_ok]);
    $vars[from_uk] = stripslashes($_POST[from_uk]);
    $vars[returning_uk] = stripslashes($_POST[returning_uk]);
    $vars[tent] = stripslashes($_POST[tent]);
    $vars[top_tent_cap] = stripslashes($_POST[top_tent_cap]);
    $vars[btent] = stripslashes($_POST[btent]);
    $vars[base_tent_cap] = stripslashes($_POST[base_tent_cap]);
    $vars[medic_info] = stripslashes($_POST[medic_info]);
    $vars[extra_info] = stripslashes($_POST[extra_info]);
    $vars[aims] = stripslashes($_POST[aims]);
    $vars[transport_info] = stripslashes($_POST[transport_info]);
    $vars[medication] = stripslashes($_POST[medication]);
    $vars[allergies] = stripslashes($_POST[allergies]);
    $vars[bivvy] = stripslashes($_POST[bivvy]);
}

function errorPage ($err)
{
    echo <<<END
    <title>Error</title>
    </head>
END;
    echo <<<END
    <h1>Sorry</h1>
    <p><b>$err</b>
END;
}

function securityChecks ()
{
    global $vars;

    $error = "";
    
    if ($vars[name] == "") $error = "You must specify your full name.";
    elseif ($vars[address] == "") $error = "You must specify your address.";
    elseif ($vars[kinname] == "") $error = "You must specify the name of your next-of-kin.";
    elseif ($vars[kinaddress] == "") $error = "You must specify your next-of-kin's address.";
    elseif ($vars[kinphone] == "") $error = "You must specify your next-of-kin's phone number.";
    elseif ($vars[email] != "" && !ereg( '^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'. '@'. '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.' . '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', $vars[email])) $error = "Invalid email address (leave blank if you don't have one).";
    elseif ($vars[kinemail] != "" && !ereg( '^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'. '@'. '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.' . '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', $vars[kinemail])) $error = "Invalid next-of-kin email address (leave blank if they don't have one).";
    elseif ($vars[veggie] != "yes" && $vars[veggie] != "no" && $vars[veggie] != "mostly") $error = "Invalid veggie specification.";
    elseif ($vars[student] != "yes" && $vars[student] != "no") $error = "Invalid student specification.";
    elseif ($vars[transport_ok] != "yes" && $vars[transport_ok] != "no") $error = "Invalid transport specification given.";
    elseif ($vars[bivvy] != "" && $vars[bivvy] != "yes") $error = "Invalid bivvy specification.";
    elseif ($vars[tent] != "" && $vars[tent] != "yes") $error = "Invalid Top Camp tent specification.";
    elseif ($vars[btent] != "" && $vars[btent] != "yes") $error = "Invalid Base Camp tent specification.";
    elseif ($vars[tent] == "yes" && ($vars[top_tent_cap] < 1 || $vars[top_tent_cap] > 9)) $error = "Invalid Top Camp tent capacity given.";
    elseif ($vars[btent] == "yes" && ($vars[base_tent_cap] < 1 || $vars[base_tent_cap] > 9)) $error = "Invalid Base Camp tent capacity given.";
    
    if ($error != "") {
        echo "<title>Expo 2004 Application Form Error -- Cambridge University Caving Club</title>";
        echo "</head>";
	
        echo "<FORM ACTION=\"http://cucc.survex.com/expo/signup/submit.php?action=commit\" METHOD=\"POST\">";
	printHidden ();
    
        echo <<<END
        <H1>Expo 2004 Application Form Error</H1>
        <p><b>Sorry, but you have made a mistake on your form:</b><center>
        $error
        </center>
        <p>Please click the button below and try again.
        <center><INPUT TYPE="submit" name="submit" VALUE="Modify form."></center>
	</form>
END;

	return false;
    }

    return true;
}

function showForm ()
{
    global $vars;
    
    echo <<<END
    <title>Expo 2004 Application Form -- Cambridge University Caving Club</title>
    </head>
END;
    echo <<<END
    
    <H1>Expo 2004 Application Form</H1>
    
    <P>This is the application form for Expo 2004, which runs from 10th July until 14th August.
    
    <P>This form is not secure and by submitting
    this form you consent to its contents being used for Expo purposes and
    being included in the Bier Book on Expo.</P>
    
    <HR>
    <FORM ACTION="http://cucc.survex.com/expo/signup/submit.php?action=preview" METHOD="POST">
    
    <H2>Section A: Personal information<h2>
    
    <h3>General</h3>
    
    <table border=0>
    
    <tr><td colspan=2><b>Your details:</b></td><td colspan=2>
    <b>Next of kin:</b></td></tr>
    
    <tr><td align=right><i>Full</i> name:</td><td><input name="name" value="$vars[name]" tabindex=1></td>
    <td align=right>Name:</td><td><input name="kinname" value="$vars[kinname]" tabindex=4></td></tr>
    
    <tr><td align=right>Address:</td><td>
    <textarea name="address" rows=7 cols=20 tabindex=1>$vars[address]</textarea></td>
    <td align=right>Address:</td><td>
    <textarea name="kinaddress" rows=7 cols=20 tabindex=5>$vars[kinaddress]</textarea></td>
    </tr>
    
    <tr>
    <td align=right>Phone:</td><td><input name="phone" tabindex=2 value="$vars[phone]"></td>
    <td align=right>Phone:</td><td><input name="kinphone" tabindex=6 value="$vars[kinphone]"></td>
    </tr>
    
    <tr>
    <td align=right>Email:</td><td><input name=email tabindex=3 value="$vars[email]"></td>
    <td align=right>Email:</td><td><input name=kinemail tabindex=7 value="$vars[kinemail]"></td>
    </tr>
    
    <tr>
    <td colspan=2></td>
    <td align=right>Relation to you:</td><td><input name=relation tabindex=8 value="$vars[relation]"></td>
    </tr>
    
    </table>
    
    <table width=100%>
    <tr><td><b>Are you a vegetarian?</b></td>
    <td><b>Are you a student/unwaged?</b></td></tr>
    <tr><td><INPUT TYPE="radio" NAME="veggie" VALUE="yes" tabindex=9
END;
    if ($vars[veggie] == "yes") echo " checked";
    echo <<<END
    >Yes</td><td><INPUT TYPE="radio" NAME="student" VALUE="yes" tabindex=12
END;
    if ($vars[student] == "yes") echo " checked";
    echo <<<END
    >Yes</td></tr>
    <tr><td><INPUT TYPE="radio" NAME="veggie" VALUE="mostly" tabindex=10
END;
    if ($vars[veggie] == "mostly") echo " checked";
    echo <<<END
    >Mostly</td><td><INPUT TYPE="radio" NAME="student" VALUE="no" tabindex=13
END;
    if ($vars[student] <> "yes") echo " checked";
    echo <<<END
    >No</td></tr><tr><td><INPUT TYPE="radio" NAME="veggie" VALUE="no" tabindex=11
END;
    if ($vars[veggie] <> "yes" && $vars[veggie] <> "mostly") echo " checked";
    echo <<<END
    >No</td><td></td></tr></table>
    
    <h3>Transport</h2>

    <p>Please look now at the <a target="_blank" href="http://cucc.survex.com/expo/years/2004/travel.html">transport arrangements page</a> (will open in a new window).  Then select one of the following.

    <p><input type="radio" name="transport_ok" value="yes" tabindex=38
END;
    if ($vars[transport_ok] == "yes") echo " checked";
    echo <<<END
    >The transport page shows my arrangements correctly.</input>
    
    <br><input type="radio" name="transport_ok" value="no" tabindex=39
END;
    if ($vars[transport_ok] == "no") echo " checked";
    echo <<<END
    >The transport page does not show my arrangements correctly.</input>

    <p>If your arrangements are shown incorrectly, then please state the
    corrected version in the box below.  This will automatically be
    emailed to the transport co-ordinator.
    
    <br><TEXTAREA NAME="transport_info" ROWS=6 COLS=80 tabindex=40>$vars[transport_info]</TEXTAREA></p>
    
    <h3>Tents</h3>

    <p>There will be two high camps in 2004:
    <ul><li>the stone bridge bivvy site;
    <li>the old Top Camp site, to be used for exploration of
    Eisl&uuml;fth&ouml;hle.
    </ul>

    <p>There is space for three people in a Club tent at Base Camp.
    There is space for five or six people in a Club tent at Top Camp.
    At the stone bridge, a bivvy bag is not essential but useful if
    you have one.
    
    <P>Will you be taking any of the following (please tick/complete as appropriate):<BR>
    <INPUT TYPE="checkbox" NAME="bivvy" VALUE="yes" tabindex=53
END;
    if ($vars[bivvy] == "yes") echo " checked";
    echo <<<END
    > A bivvy bag suitable for the stone bridge.<br>
    <INPUT TYPE="checkbox" NAME="tent" VALUE="yes" tabindex=50
END;
    if ($vars[tent] == "yes") echo " checked";
    echo <<<END
    > A <INPUT NAME="top_tent_cap" SIZE=1 MAXSIZE=1 value="$vars[top_tent_cap]" tabindex=51> person
        tent for the Top Camp site.<BR>
    <INPUT TYPE="checkbox" NAME="btent" VALUE="yes" tabindex=52
END;
    if ($vars[btent] == "yes") echo " checked";
    echo <<<END
    > A <INPUT NAME="base_tent_cap" SIZE=1 MAXSIZE=1 value="$vars[base_tent_cap]" tabindex=53> person
        tent for Base Camp.<br>

    <p>Leave the boxes unchecked if you require Club tent space at a
    particular site.
    
    <h3>Medical</h3>

    <p>The medical information entered here will appear in the Bier Book.
    Extra medical information may be communicated to
    <a href="mailto:mjg54&#64cam.ac.uk">Martin Green</a>; this will be
    placed in sealed envelopes inside the large first aid kits for use in
    case of emergency.  Please ensure that you communicate such information
    in good time.
    
    <P>Please list any allergies which you have:
    <TEXTAREA NAME="allergies" ROWS=2 COLS=80 tabindex=68>$vars[allergies]</TEXTAREA></P>
    
    <P>Please list any medication which you may be taking in Austria:
    <TEXTAREA NAME="medication" ROWS=2 COLS=80 tabindex=69>$vars[medication]</TEXTAREA></P>

    <P>Please list any other medical conditions you have that the expedition should be aware of:<BR>
    <TEXTAREA NAME="medic_info" ROWS=5 COLS=80 tabindex=70>$vars[medic_info]</TEXTAREA></P>

    
    <h3>Other</h3>
    
    <P>Any other information, including any skills e.g. first aid, languages, etc. that you have:<BR>
    <TEXTAREA NAME="extra_info" ROWS=15 COLS=80 tabindex=71>$vars[extra_info]</TEXTAREA></P>
    
    <h2>Section B - Caving/surface work projects</h2>
    
    <p>To assist organisation of gear, it would be helpful to know
    people's possible aims on Expo.  Thus if you have a particular project in
    mind, please state it below:

    <br>
    <textarea name="aims" rows=15 cols=80 tabindex=77>$vars[aims]</textarea>
    
    </table>
    
    <hr>
    
    <h2>All done?</h2>
    
    <p>Click the <b>Preview</b> button below to review your submission.
    
    <p><INPUT TYPE="submit" VALUE="Preview form submission">
    
    </FORM>

    <hr>
    <p>Please report any problems with this form to the
    <a href="mailto:Mark.Shinwell@cl.cam.ac.uk">webmaster</a>.</p>
END;
}

function insertSlashes ()
{
}

function printHidden ()
{
    global $vars;
    while (list ($key, $value) = each($vars))
        echo "<input type=hidden name=$key value=\"$value\">";
}

function previewForm ()
{
    global $vars;

    echo <<<END
    <title>Expo 2004 Application Form Preview -- Cambridge University Caving Club</title>
    </head>
END;
    echo <<<END
    <h1>Expo 2004 Application Form Preview</h1>
    <P><b>Please check the details below and then select one of the options at the
    bottom of the page.</b>
    <hr>
    <H2>Contact details</H2>
    <h3>Your details</h3>
    <p>
    <FORM ACTION="http://cucc.survex.com/expo/signup/submit.php?action=commit" METHOD="POST">
END;

    printHidden ();
    
    $address1 = rtrim(ereg_replace(",,", ",", ereg_replace("(\r\n|\n|\r)", ", ", $vars[address])), " \t\n\r\0\x0b.,");
    $kinaddress1 = rtrim(ereg_replace(",,", ",", ereg_replace("(\r\n|\n|\r)", ", ", $vars[kinaddress])), " \t\n\r\0\x0b.,");

    echo "<b>Full name: </b>$vars[name].  <br><b>Address: </b> $address1.<br>";
    echo "<b>Phone: </b> $vars[phone].<br><b>Email: </b> ";
    if ($vars[email] == "") echo "none"; else echo $vars[email];

    echo "<h3>Details for your $vars[relation]</h3><p>";

    echo "<b>Full name: </b>$vars[kinname].  <br><b>Address: </b> $kinaddress1.<br>";
    echo "<b>Phone: </b> $vars[kinphone].<br><b>Email: </b> ";
    if ($vars[kinemail] == "") echo "none"; else echo $vars[kinemail];

    echo "<h2>Miscellany</h2> <p><ul><li>";

    echo "You are";
    switch ($vars[veggie]) {
        case "no": echo "n't"; break;
	case "mostly": echo " mostly"; break;
	default: break;
    }
    echo " a vegetarian.<li>You are";
    if ($vars[student] == "no") echo "n't";
    echo " a student/unwaged person.";

    if ($vars[bivvy] == "yes")
        echo "<li>You're taking a bivvy bag suitable for the stone bridge.";
    if ($vars[tent] == "yes")
        echo "<li>You're taking a $vars[top_tent_cap] person tent suitable for Top Camp.";
    if ($vars[btent] == "yes")
        echo "<li>You're taking a $vars[base_tent_cap] person tent suitable for Base Camp.";

    echo "</ul><h2>Medical conditions</h2><ul>";

    echo "<li>Allergies: ";
    if ($vars[allergies] == "")
        echo "None.";
    else
        echo nl2br($vars[allergies]);

    echo "<li>Medication to be taken in Austria: ";
    if ($vars[medication] == "")
        echo "None.";
    else
        echo nl2br($vars[medication]);
	
    echo "<li>Other medical conditions: ";
    if ($vars[medic_info] == "")
        echo "None.";
    else
        echo nl2br($vars[medic_info]);

    echo "</ul>";

    if ($vars[aims] != "") {
        echo "<h2>Your aims for Expo</h3><p>";
	echo nl2br($vars[aims]);
    }

    if ($vars[extra_info] != "") {
        echo "<h2>Other information</h2><p>";
	echo nl2br($vars[extra_info]);
    }

    echo "<h2>Transport</h2><ul>";

    if ($vars[transport_ok] == "yes")
        echo "<li>The transport page shows your arrangements correctly.";
    else {
        echo "<li>The transport page does not show your arrangements correctly.<br>";
	echo "The corrected version is: ";
	echo nl2br($vars[transport_info]);
    }
    echo "</ul>";

    if ($vars[transport_info] != "") {
        echo "<h3>Other transport information</h3><p>";
	echo nl2br($vars[transport_info]);
    }

    echo <<<END
    <hr>
    
    <h2>If the above details are correct...</h2>
    
    <p><b>Only click once to avoid submitting duplicate forms.</b>
    
    <center><INPUT TYPE="submit" name="submit" VALUE="I confirm that all details are correct."></center>
    
    <h2>If you want to change something...</h2>
    
    <p>If there is an error in the above details, press the button below to change them.
    <center><INPUT TYPE="submit" name="submit" VALUE="Modify form."></center>

    </form>
END;
}

function updateDatabase ()
{
    global $vars;

    $temp = "";

    $address1 = rtrim(ereg_replace(",,", ",", ereg_replace("(\r\n|\n|\r)", ", ", $vars[address])), " \t\n\r\0\x0b.,");
    $kinaddress1 = rtrim(ereg_replace(",,", ",", ereg_replace("(\r\n|\n|\r)", ", ", $vars[kinaddress])), " \t\n\r\0\x0b.,");

    $temp = $temp . "Form submission from:\n";
    $temp = $temp . "    $vars[name]\n";
    $temp = $temp . "    $address1\n";
    $temp = $temp . "    Phone: $vars[phone]\n";
    $temp = $temp . "    Email: ";
    if ($vars[email] == "") $temp = $temp . "none";
    else $temp = $temp . "$vars[email]";

    $temp = $temp . "\n\n";
    $temp = $temp . "Next of kin:\n";
    $temp = $temp . "    $vars[kinname]\n";
    $temp = $temp . "    $kinaddress1\n";
    $temp = $temp . "    Phone: $vars[kinphone]\n";
    $temp = $temp . "    Email: ";
    if ($vars[kinemail] == "") $temp = $temp . "none";
    else $temp = $temp . "$vars[kinemail]";

    $temp = $temp . "\n\n";
    $temp = $temp . "Vegetarian? $vars[veggie]\n";
    $temp = $temp . "Student/unwaged? $vars[student]\n\n";

    if ($vars[transport_ok] == "yes") {
        $temp = $temp . "Transport arrangements page is correct for this person.\n";
    }
    else {
        $temp = $temp . "Transport arrangements need changing to:\n";
	$temp = $temp . "$vars[transport_info]\n";
	$temp = $temp . "(Mark has been emailed and will update the page.)\n";
    }

    $temp = $temp . "\nTaking a bivvy bag? $vars[bivvy]\n";
    $temp = $temp . "\nTaking a Top Camp tent? $vars[tent]";
    if ($vars[tent] == "yes")
        $temp = $temp . " (capacity $vars[top_tent_cap])\n";
    else $temp = $temp . "\n";
    $temp = $temp . "\nTaking a Base Camp tent? $vars[btent]";
    if ($vars[btent] == "yes")
        $temp = $temp . " (capacity $vars[base_tent_cap])\n";
    else $temp = $temp . "\n";

    $temp = $temp . "\nAllergies: $vars[allergies]\n";
    $temp = $temp . "Medication to be taken in Austria: $vars[medication]\n";
    $temp = $temp . "Other medical conditions: $vars[medic_info]\n\n";

    $temp = $temp . "Aims for Expo: $vars[aims]\n\n";

    $temp = $temp . "Other information: $vars[extra_info]\n";

    mail ("David Loeffler <dl267@cam.ac.uk>\n",
          "Expo form from $vars[name]", $temp,
          "From: Expo forms <mark@three-tuns.net>\n");
	
    if ($vars[transport_ok] == "no") {
        $temp = "Transport page needs updating for $vars[name]:\n\n";
	$temp = $temp . $vars[transport_info] . "\n";
	mail ("Mark Shinwell <mark@three-tuns.net>",
	      "Transport update from $vars[name]", $temp,
	      "From: Expo forms <mark@three-tuns.net>\n");
    }
}

function submitForm ()
{
    global $vars;

    updateDatabase ();

    echo <<<END
    <title>Submission successful!</title>
    </head>
END;
    echo <<<END
    <h1>Expo 2004 Application</h1>

    <p><b>Submission successful!</b>

    <p>Thank you for your application.  Please address any questions
    to the Expo Leader, <a href="mailto:oghm2&#64cam.ac.uk">Olly Madge</a>.

    <p><a href="http://cucc.survex.com/expo/">Return to the main Expo page</a>.
END;
}

virtual("main.css");

switch ($_GET[action]) {
    case "blank":
        initVars ();
        showForm ();
	break;

    case "edit":
        fillVars ();
        showForm ();
	break;

    case "preview":
        fillVars ();
        if (securityChecks ()) {
            previewForm ();
	}
	break;

    case "commit":
        switch ($_POST[submit]) {
            case "I confirm that all details are correct.":
        	fillVars ();
        	if (securityChecks ()) {
		    insertSlashes ();
        	    submitForm ();
		}
		break;

	    case "Modify form.";
	        fillVars ();
		showForm ();
		break;

	    default:
	        errorPage ("Invalid script action.");
	}
	break;

    default:
        errorPage ("Invalid script action.");
	break;
}

?>

</td></tr></table></body></html>