<?php
session_start();

if(!$_SESSION['isadmin'])
{
	header("Location: login.php?ref=edit.php?pageid=1");
	exit;
}

include_once("../functions.php");
$conn = dbconnect();
include "header.php";
include "leftbar.php";

if($_POST['register_club']) { 
			$golfclub = mysqli_real_escape_string($conn,$_POST['golfclub']);
			$businessname = mysqli_real_escape_string($conn,$_POST['businessname']);
			$country = mysqli_real_escape_string($conn,$_POST['country']);
			$address = mysqli_real_escape_string($conn,$_POST['address']);
			$contactname = mysqli_real_escape_string($conn,$_POST['contactname']);
			$phone = mysqli_real_escape_string($conn,$_POST['phone']);
			$email = mysqli_real_escape_string($conn,$_POST['email']);
			$password = md5($_POST['password']);
			$rep_password = md5($_POST['rep_password']);
			
			mysqli_query($conn,"INSERT INTO club_registrations VALUES(0,'".$golfclub."','".$businessname."','".$country."','".$address."','".$contactname."','".$phone."','".$email."','".$password."','','".date('Y-m-d H:i:s')."','')");
			$registration_id = mysqli_insert_id($conn);
			
			//make entry in club password.			
			mysqli_query($conn,"INSERT INTO reports_password VALUES (0,$registration_id,'".$rep_password."','".date('Y-m-d H:i:s')."','0000-00-00 00:00:00')");
			
			$idmd5 = md5($_POST['email'].$registration_id);
			
			mysqli_query($conn,"UPDATE club_registrations SET idmd5 = '".$idmd5."' WHERE id=".$registration_id);
}
?>

    <div class="content">
        
        <div class="header">
            
            <h1 class="page-title">Add a club</h1>
        </div>
        
                <ul class="breadcrumb">
            <li><a href="index.php">Home</a> <span class="divider">/</span></li>
            <li class="active">Add a club</li>
        </ul>

<?php if($_POST['register_club']) {?>
<div style="padding: 0.1em; width:96%; margin-left:20px;" class="ui-state-highlight ui-corner-all">
   <p style="padding-top:10px;"><span style="float: left; margin-right: 0.3em;" class="ui-icon ui-icon-info"></span>Club registered successfully.</p>
</div>
<br />
<?php }?>  

        <div class="container-fluid">
            <div class="row-fluid">
		
<div class="well">


	<form action="add-a-club.php" method="POST" id="club_reg">
	<table>
	<tr><td>Golf Club</td><td style="padding-left:20px;"><input type="text" name="golfclub" size="25" class="alpha" value="<?php echo $_POST['golfclub']?>"></td></tr>
	<tr><td>Business Name</td><td style="padding-left:20px;"><input type="text" name="businessname"  size="25" class="alpha" value="<?php echo $_POST['businessname']?>"></td></tr>
	
	<tr><td>Country</td><td style="padding-left:20px;">
	<?php $get_countries = mysqli_query($conn,"SELECT name FROM country");?>
	
	<select name="country">
	<?php while($country = mysqli_fetch_array($get_countries)) {?>
	<option value="<?php echo $country['name']?>" <?php if($_POST['country']==$country['name']){?> selected="selected" <?php }?>><?php echo $country['name']?></option>
	<?php }?>
	</select>
	
	</td></tr>
	<tr><td>Postal Address</td><td style="padding-left:20px;"><textarea name="address" rows="4" cols="27"><?php echo $_POST['address']?></textarea></td></tr>
	<tr><td>Contact Name</td><td style="padding-left:20px;"><input type="text" name="contactname" class="alpha"  size="25" value="<?php echo $_POST['contactname']?>"></td></tr>
	<tr><td>Contact Telephone</td><td style="padding-left:20px;"><input type="text" name="phone"  size="25" class="phone" value="<?php echo $_POST['phone']?>"></td></tr>
	<tr><td>Email</td><td style="padding-left:20px;"><input type="text" name="email" id="email" size="25" value="<?php echo $_POST['email']?>"></td></tr>
	<tr><td>New Password</td><td style="padding-left:20px;"><input type="password" name="password"  size="25" value="<?php echo $_POST['password']?>"></td></tr>
	<tr><td>Reports Password</td><td style="padding-left:20px;"><input type="password" name="rep_password"  size="25" value="<?php echo $_POST['rep_password']?>"></td></tr>
	
	<tr><td></td><td><input type="submit" name="register_club" value="Register" class="regBtn" style="float:left; margin-left:20px;"></td></tr>
	
	</table>
	</form>

</div>

<script src="../js/jquery.validate.js"></script>
<script src="../js/jquery.alphanumeric.pack.js"></script>

<script>

/*$('.an').alphanumeric();

$('.name').alpha();

$('.suburb').alpha({allow:" "});

$('.anandspace').alphanumeric({allow:"#., "});

$('.numeric').numeric({allow:"+ "});*/

$('.alpha').alpha({allow:" "});
$('.phone').numeric({allow:" -+"});

	
	$().ready(function() {
		// validate signup form on keyup and submit
		$("#club_reg").validate({
			rules: {
				
				golfclub: {
					required: true
				},
				
				businessname: {
					required: true
				},
				
				address: {
					required: true
				},
				
				contactname: {
					required: true
				},
				phone: {
					required: true
				},
				email: {
					required: true,
					email: true,
					remote: "../check-email.php"
				},
				password: {
					required: true,
					minlength: 5
				},
				
			},
			messages: {
				golfclub:"Please enter golf club name",
				businessname:"Please enter business name",
				address:"Please enter postal address",
				contactname:"Please enter contact name",
				phone:"Please enter phone number",
				email: {required: "Please enter a valid email address",remote: "This email id is already in use"},
				password: { required: "Please enter password", minlength:"Minimum 6 chars required" }
			}
		});

	});
</script>

<?php include "footer.php";?>