<?php
session_start();
/* All Right Reserved (C) 2010;
  MyTickets;
  Writed By Fahad S. Al-ghudaier;
  faha1ad2@hotmail.com;
  www.phpx3.com;
*/
	include('start.php');
	include('include/system/arrays.php');


	
	// if he is already a member
	if($member['member'] == 1){
		header("Location: control.php");
		die();
	}
	
	// page information
	$page['title']			= $language['lang_2'];
	$page['allow_metas']	= 2;
	$page['other_tags']		= '<script type="text/javascript" language="javascript" src="'.$options['site_url'].'/include/js/tooltip.js"></script>';
	$page['show_header']	= 1;
	include('content_head.php');
	
	// if the registration is closed
	if($options['open_registration'] == 2){
		$smarty->assign('msg',$language['lang_42']);
		$smarty->display('error_msg.htm');
		include('content_foot.php');
		die();
	}

	$dos	= array('check');
	$do		= $_GET['do'];
	if(! in_array($do,$dos)){
		$do	= 'main';
	}
	
	switch($do){
	// the first step of registration
	case 'main':
		if($language['menus'] == 1){
			countrys_menu($posted['country'],1);
		}else{
			countrys_menu($posted['country'],2);
		}
		$smarty->assign('countrys',$array['country']);
		$smarty->display('signup.htm');
	break;
	case 'check':
	
		
	$posted['username']	= input_handler($_POST['username']);
	$posted['password']	= input_handler($_POST['password']);
	$posted['email']	= $_POST['email'];
	$posted['country']	= input_handler($_POST['country']);
	$posted['website']	= input_handler($_POST['website']);
	$posted['captcha']	= $_POST['captcha'];
	
	if($language['menus'] == 1){
		countrys_menu($posted['country'],1);
	}else{
		countrys_menu($posted['country'],2);
	}

	if(empty($posted['username']) OR empty($posted['password']) OR empty($posted['email'])){
		$error['error']	= 1;
		$error['msg']	= $language['lang_3'];
		$smarty->assign('error',$error);
		$smarty->assign('countrys',$array['country']);
		$smarty->assign('posted',$posted);
		$smarty->display('signup.htm');
		include('content_foot.php');
		die();
	}
	
	if(check($posted['username'],'count',3) == false OR check($posted['password'],'count',5) == false){
		$error['error']	= 1;
		$error['msg']	= $language['lang_28'];
		$smarty->assign('error',$error);
		$smarty->assign('countrys',$array['country']);
		$smarty->assign('posted',$posted);
		$smarty->display('signup.htm');
		include('content_foot.php');
		die();
	}
	
	if(check($posted['email'],'email') == false){
		$error['error']	= 1;
		$error['msg']	= $language['lang_4'];
		$smarty->assign('error',$error);
		$smarty->assign('countrys',$array['country']);
		$smarty->assign('posted',$posted);
		$smarty->display('signup.htm');
		include('content_foot.php');
		die();
	}
	if(mysql_num_rows(mysql_query("SELECT `id` FROM `members` WHERE `email`='".$posted['email']."'")) > 0){
		$error['error']	= 1;
		$error['msg']	= $language['lang_7'];
		$smarty->assign('error',$error);
		$smarty->assign('countrys',$array['country']);
		$smarty->assign('posted',$posted);
		$smarty->display('signup.htm');
		include('content_foot.php');
		die();
	
	}
	if(! $posted['website'] == '' AND check($posted['website'],'url') == false){
		$error['error']	= 1;
		$error['msg']	= $language['lang_5'];
		$smarty->assign('error',$error);
		$smarty->assign('countrys',$array['country']);
		$smarty->assign('posted',$posted);
		$smarty->display('signup.htm');
		include('content_foot.php');
		die();
	}
	
	$posted['password']	= md5($posted['password']);
	
	if($options['allow_captcha'] == 1){
		if($posted['captcha'] != $_SESSION['captchacode'] OR $_SESSION["captchacode"]=='') {
			$error['error']	= 1;
			$error['msg']	= $language['lang_6'];
			$smarty->assign('error',$error);
			$smarty->assign('countrys',$array['country']);
			$smarty->assign('posted',$posted);
			$smarty->display('signup.htm');
			include('content_foot.php');
			die();
		}
	}
	
	
	$insert	= mysql_query("INSERT INTO `members` (`username`,`password`,`country`,`email`,`website`,`last_login`,`ip`,`reg_date`,`language`,`group_id`)
	VALUES (
	'".$posted['username']."','".$posted['password']."','".$posted['country']."','".$posted['email']."',
	'".$posted['website']."','".date('d/m/Y')."','".$_SERVER['REMOTE_ADDR']."','".date('d/m/Y')."',
	'".$Language_ID."','".$options['main_reg_group']."'
	)");
	
	if($insert){
		$msg	= $language['lang_9'];
		$smarty->assign('msg',$msg);
		$smarty->display('ok_msg.htm');
		redirect('log.php?do=login');
	}else{
		$error['error']	= 1;
		$error['msg']	= $language['lang_8'];
		$smarty->assign('error',$error);
		$smarty->assign('countrys',$array['country']);
		$smarty->assign('posted',$posted);
		$smarty->display('signup.htm');
		include('content_foot.php');
		die();
	}

	break;
	}
	
	include('content_foot.php');
?>