function cas_register_form($errors) {
	global $cas_text, $cas_textcount, $cas_myurl, $cas_imgheight, $cas_imgwidth, $cas_limitcolor, $cas_borderclr, $cas_message, $cas_displaytext, $cas_table, $cas_count, $cas_wav, $wpdb;

	// Insert a row into the count database to generate an auto_increment number
	$wpdb->query('INSERT INTO ' . $cas_count . ' (id) VALUES (NULL)');

	// Get the id of the inserted count to feed to the word table and image generator
	$cas_rowid = $wpdb->get_var('SELECT last_insert_id()');

	// Pick a random number
	$cas_antiselect = rand( 1, $cas_textcount ); // 0 is for invalid, so don't select it

	// Put the word corresponding to the random number into the database
	$wpdb->query('INSERT INTO ' . $cas_table . ' (id, createtime, word) VALUES (' . $cas_rowid . ', ' . time() . ', \'' . $cas_text[$cas_antiselect] . '\')');
	
	// Delete the row from the count table
	$wpdb->query('DELETE FROM ' . $cas_count . ' WHERE id = ' . $cas_rowid);

	// Do some table admin while we can :D
	if (strlen($cas_rowid) == 10) {

		// Delete all rows from the count table
		$wpdb->query('DELETE FROM ' . $cas_count);

		// Reset the table's auto increment if it's getting too huge
		$wpdb->query('ALTER TABLE ' . $cas_count . ' AUTO_INCREMENT=1');

		// Delete any anti-spam words more than a day old
		$wpdb->query('DELETE FROM ' . $cas_table . ' WHERE ' . time() . ' > createtime + 86400');
	}

	// echo( "\t\t\t".'<div style="display:block;" id="secureimgdiv">'."\n\t\t\t\t" );
	if ( $errors->get_error_message('cas_register') ) {
		echo '<tr class="error">';
	} else {
		echo '<tr>';
	}
	echo ('<th valign="top">');
	echo ( "<label for=\"securitycode\">{$cas_displaytext['label']}</label><span style=\"color:#FF0000;\">*</span><br />\n\t\t\t\t" );
	echo ( $cas_message . "<br />\n\t\t\t\t" );
	echo ('</th><td>');
	if ( $errmsg = $errors->get_error_message('cas_register') ) {
		?><p><strong><?php echo $errmsg ?></strong></p><?php
	}
	echo( '<input type="text" name="securitycode" id="securitycode" size="30" />'."\n\t\t\t\t" );
	echo( '<input type="hidden" name="matchthis" value="' . $cas_rowid . "\" />\n\t\t\t\t" );
	if ($cas_wav) echo( '<a href="' . $cas_myurl . '?audioselect=' . $cas_rowid . '" title="' . $cas_displaytext['alt_tag'] . '">' );
		echo( '<img src="' . $cas_myurl . '?antiselect=' . $cas_rowid . "\"\n\t\t\t\t" );
		echo( 'alt="' . $cas_displaytext['alt_tag'] . '" ' );
		echo( 'style="border:1px solid ' . $cas_borderclr . ';vertical-align:top;' );
		echo( 'height:' . $cas_imgheight .'px;width:' . $cas_imgwidth . 'px;" />' );
		if ($cas_wav) echo( '</a>');
		echo( "</td></tr>\n\t\t\t" );
}

function cas_register_post($result) {
	global $_POST, $cas_text, $cas_textcount, $user_ID, $cas_displaytext, $cas_table, $wpdb, $cas_reg_blacklist;

		// Validate the form input values
		if( isset( $_POST['securitycode'] ) )
		{
			// Consider only the first 50 characters in the posted word
			$securitycode = substr( strval( $_POST['securitycode'] ), 0, 50 );

			// Remove all spaces and hyphens to give the commenter a break!
			$securitycode = str_replace(' ', '', $securitycode);
			$securitycode = str_replace('-', '', $securitycode);

		} else {
			$securitycode = '';
		}
		if( isset( $_POST['matchthis'] ) )
		{
			$matchnum = intval( $_POST['matchthis'] );
		} else {
			$matchnum = 0;
		}

		if ( $securitycode == '' )
		{			
			$result['errors']->add('cas_register', $cas_displaytext['emptyfield']);
			return $result;
		}
		else {
			// Get the anti-spam word from the database
			$matchthis = $wpdb->get_var('SELECT word FROM ' . $cas_table . ' WHERE id = ' . $matchnum);

			// If this row doesn't exist, say something
			if (is_null($matchthis)) {
				$result['errors']->add('cas_register', $cas_displaytext['register_alreadyused']);
				return $result;
			}

			else {

				// Remove all spaces and hyphens, since we removed them from what the commenter entered
				$matchthis = str_replace(' ', '', $matchthis);
				$matchthis = str_replace('-', '', $matchthis);

				// Check what was entered against what the code should be
				if ( strtolower( $matchthis ) != strtolower( $securitycode ) ) {
					$result['errors']->add('cas_register', $cas_displaytext['register_wrongfield']);
					return $result;
				}

				else {
					// The word matched, so delete the row for the anti-spam word so that it cannot be used again
					$wpdb->query('DELETE FROM ' . $cas_table . ' WHERE id = ' . $matchnum);
					unset( $matchthis );

					// Do some more table admin while we can :D
					// Delete any anti-spam words more than a day old
					$wpdb->query('DELETE FROM ' . $cas_table . ' WHERE ' . time() . ' > createtime + 86400');
				}
			}
		}
		foreach ($cas_reg_blacklist as $cas_blacklist) {
			if (stristr($_POST['user_email'], $cas_blacklist)) {
				$result['errors']->add('cas_register', $cas_displaytext['register_blocked']);
				return $result;
				break;
			}
		}
		// All checks passed!
		return $result;
	}

// Add registration protection to the appropriate hooks only if it has been enabled in this plugin's settings
if ($cas_reg_protection) {
	add_action('signup_extra_fields', 'cas_register_form');
	add_action('wpmu_validate_user_signup', 'cas_register_post');
	}
}
?>