<?php	
final class Reg
	{
	
	private $sql;
	var $cleaner = '<div style="clear: both;"></div>';
	
	public function __construct()
		{ 
		global $sql;
		$this->sql = &$sql;
		}

	public function return_heading_text($text)
		{
		echo '<h3>' . $text . '</h3>';
		}

	/*** REGISTRATION: START ***/

	public function reg_check_enque($nick)
		{
		$this->sql->sql_connect('login');
			$sql_check = "SELECT nick FROM users WHERE LOWER(nick)='" . strtolower($nick) . "'";
			$result_check = mysql_query($sql_check);
			if ( mysql_num_rows($result_check) > 0 )
				{
				return false;
				}
			else
				{
				return true;
				}
		$this->sql->sql_free_result($result_check);
		$this->sql->sql_close();
		}
	
	public function return_reg_form()
		{
		if ( $this->reg_check_enque(trim(strip_tags($_POST['nick']))) ) {$enque = '';}
		else {$enque = '<h3 style="color: red;">You have to change your nick. ' . $_POST['nick'] . ' is used here yet.</h3>';}
			
		if	(
			isset($_POST['nick']) && (trim(strip_tags($_POST['nick']))!='') &&
			isset($_POST['pass']) && (trim(strip_tags($_POST['pass']))!='') &&
			isset($_POST['repass']) && (trim(strip_tags($_POST['repass']))!='') &&
			$_POST['pass'] == $_POST['repass'] &&
			isset($_POST['first_name']) && (trim(strip_tags($_POST['first_name']))!='') &&
			isset($_POST['last_name']) && (trim(strip_tags($_POST['last_name']))!='') &&
			isset($_POST['mail']) && (trim(strip_tags($_POST['mail']))!='') && empty($enque)
			)
			{
			$this->execute_reg( strip_tags($_POST['nick']), strip_tags($_POST['pass']), strip_tags($_POST['first_name']), strip_tags($_POST['last_name']), strip_tags($_POST['mail']) );
			}
		else
			{
			$this->return_heading_text('You can register here:');
			echo $enque;
		?>
			<form class="registration" action="<?=str_replace('&','&amp;',$_SERVER['REQUEST_URI'])?>" method="post">
				<table>
					<tr>
						<td>nick:</td>
						<td><input type="text" name="nick" value="<?=$_POST['nick']?>" /></td>
					</tr>
					<tr>
						<td>password:</td>
						<td><input type="password" name="pass" value="<?=$_POST['pass']?>" /></td>
					</tr>
					<tr>
						<td>re-password:</td>
						<td><input type="password" name="repass" value="<?=$_POST['repass']?>" /></td>
					</tr>
					<tr>
						<td>first name:</td>
						<td><input type="text" name="first_name" value="<?=$_POST['first_name']?>" /></td>
					</tr>
					<tr>
						<td>last name:</td>
						<td><input type="text" name="last_name" value="<?=$_POST['last_name']?>" /></td>
					</tr>
					<tr>
						<td>e-mail:</td>
						<td><input type="text" name="mail" value="<?=$_POST['mail']?>" /></td>
					</tr>
					<tr>
						<td colspan="2"><div class="submit"><input type="submit" value="Register"  /></div></td>
					</tr>
				</table>
			</form>
		<?
			}
		}
		
	public function execute_reg($nick, $pass, $first_name, $last_name, $mail)
		{
		$this->sql->sql_connect('login');
			$sql_reg = "INSERT INTO users (nick, pass, first_name, last_name, mail) VALUES 
			('" . $nick . "', '" . $pass . "', '" . $first_name . "', '" . $last_name . "', '" . $mail . "');";
			$result_reg = mysql_query($sql_reg);
			if ( $result_reg )
				{
				$this->return_heading_text('Your registration was successfully completed.');
				}
			else
				{
				$this->return_heading_text('<span style="color: red;">Unexpected error. Your registration wasn\'t successfully completed.</span>');
				}
		$this->sql->sql_close();
		}

	/*** REGISTRATION: END ***/

	}
?>