<?php
	$cookie_timeout = 60 * 30; // in seconds
	session_set_cookie_params($cookie_timeout);

	// include our primary functions and other includes - only needed once per
	// session
	require_once('includes/reuapp.inc');

	// check to see if there is a session continue on if so, else start one
	if (!(session_started())) {	session_start(); }
	
	// before everything else check to see if the reset request has been set
	// if so clear all data before continuing
	if(isset($_REQUEST['reset']) && trim($_REQUEST['reset'] != ""))
	{
		clearVars($data);
		writeReuApp($data);
	}
	
	// if reset hasn't been triggered next check for data submit and process,
	// else continue
	elseif(isset($_REQUEST['dataChk']) && trim($_REQUEST['dataChk'] == "Submit"))
	{
		// first set $data with submitted
		$data = setData($data);
		
		// check data 
		$data = checkData($data);

		// check file/resume upload
		$resFile = setFiles();
		if ($resFile == FALSE)
		{
			$data['validData'] = FALSE;
		}
	
		// once data is set/checked check to see if it has been set correctly
		// and is valid for submittal
		if ( (isset($data['validData'])) && ($data['validData'] == TRUE) )
		{
			// prep and validate the data for mysql
			$data = prepData($data);
			
			// include our local function to connect to our database
			include('includes/dbconnect.inc');
			
			// build up our query to inject the data
			$query = createNewQuery($data, $resFile);
			
			// mysql_query will return a success or fail return result so we 
			// submit our query/injection via that process and determine next
			// setp from that result
			if(mysql_query($query, $dblink))
			{
				// pull the data back out
				$retQuery = returnDataQuery($data);
				$rowReturn = mysql_fetch_row(mysql_query($retQuery));

				writeDataSuccess($data,$resFile,$rowReturn);
				mysql_close($dblink);
			}
			else
			{
				writeDataFailed();
				mysql_close($dblink);				
			}
		}
		
		// if we made it in here but we have invalid data for one reason or 
		// another, scrap any further checks and validation and rewrite the form
		// with the current data set and their valid/invalid flags
		else { writeReuApp($data, $resFile); }
	}
	// if we aren't reset or submitted then we must be a brand new page load, so
	// we need to write out the form so that the end user can submit their data
	else { writeReuApp($data, $resFile); }
?>