<?php require_once('Connections/oprmConn.php');

if(isset($_POST['fullname'])) {

mysql_select_db($database_oprmConn, $oprmConn);
$query_getProj = "select * FROM weby_users where email_address = '$_POST[emailaddress]' and status in ('A','P')";
$getProj = mysql_query($query_getProj, $oprmConn) or die(mysql_error());
$row_getProj = mysql_fetch_assoc($getProj);
$totalRows_getProj = mysql_num_rows($getProj);

if($totalRows_getProj<1) {
$date = date('Y-m-d H:i:s');
$t = rand(12100,99999);
mysql_select_db($database_oprmConn, $oprmConn);
$query_getProj1 = "INSERT INTO weby_users (`user_id`, `email_address`, `password`, `user_role`, `token`, `status`, `created_by`, `caeated_date`, `updated_by`, `updated_date`) VALUES (NULL, '$_POST[emailaddress]', '', '2', '$t', 'P', '', '$date', '', CURRENT_TIMESTAMP)";
$getProj1 = mysql_query($query_getProj1, $oprmConn) or die(mysql_error());

mysql_select_db($database_oprmConn, $oprmConn);
$query_getProj2 = "select max(user_id) as uid FROM weby_users";
$getProj2 = mysql_query($query_getProj2, $oprmConn) or die(mysql_error());
$row_getProj2 = mysql_fetch_assoc($getProj2);
$totalRows_getProj2 = mysql_num_rows($getProj2);

mysql_select_db($database_oprmConn, $oprmConn);
$query_getProj22 = "select email_address FROM weby_users where user_role = 1";
$getProj22 = mysql_query($query_getProj22, $oprmConn) or die(mysql_error());
$row_getProj22 = mysql_fetch_assoc($getProj22);
$totalRows_getProj22 = mysql_num_rows($getProj22);

$q = "INSERT INTO weby_profiles (`full_name`, `job_title`, `skill_set`, `profile_pic`, `cv_file`, `passport_file`, `holiday_sheet`, `other_docs`, `other_info`, `bank_account`, `bank_sortcode`, `bank_address`, `passport_number`, `citizen_of`, `work_permit`, `contact_phone`, `contact_address`, `hr_rate`, `created_by`, `created_date`, `updated_by`, `updated_date`, `user_id`) VALUES ( '$_POST[fullname]', '$_POST[title]', '$_POST[message]', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '$row_getProj2[uid]', '', '', CURRENT_TIMESTAMP, '$row_getProj2[uid]')";

mysql_select_db($database_oprmConn, $oprmConn);
$query_getProj3 = $q;
$getProj3 = mysql_query($query_getProj3, $oprmConn) or die(mysql_error());


$credate = date('Y-m-d');
mysql_select_db($database_oprmConn, $oprmConn);
$query_getasspro = "INSERT INTO weby_users_projects (`project_id`, `user_id`, `status`, `created_date`) VALUES ('10', '$row_getProj2[uid]', 'A', '$credate')";
$getasspro = mysql_query($query_getasspro, $oprmConn) or die(mysql_error());
mysql_select_db($database_oprmConn, $oprmConn);
$query_getasspro = "INSERT INTO weby_users_projects (`project_id`, `user_id`, `status`, `created_date`) VALUES ('11', '$row_getProj2[uid]', 'A', '$credate')";
$getasspro = mysql_query($query_getasspro, $oprmConn) or die(mysql_error());


mysql_select_db($database_oprmConn, $oprmConn);
$query_getPa = "SELECT weby_profiles.full_name, weby_users.user_id, weby_users.email_address FROM weby_users, weby_profiles WHERE weby_users.email_address = '$_REQUEST[emailaddress]' AND weby_profiles.user_id = weby_users.user_id";
$getPa = mysql_query($query_getPa, $oprmConn) or die(mysql_error());
$row_getPa = mysql_fetch_assoc($getPa);
$totalRows_getPa = mysql_num_rows($getPa);

if($totalRows_getPa>0) {


$l = 'http://'.$_SERVER[HTTP_HOST].'/RM/change-password?token='.$t.'&user='.$row_getProj2['uid'];
$ll = 'http://'.$_SERVER[HTTP_HOST].'/RM/index';

//mysql_select_db($database_oprmConn, $oprmConn);
//$query_getProj11 = "update weby_users set token = '$t' where email_address = '$_POST[emailaddress]'";
//$getProj11 = mysql_query($query_getProj11, $oprmConn) or die(mysql_error());

// send email to new user along with registration details.
$user_email1 = $row_getProj22['email_address'];
	$user_email = $_POST['emailaddress'];
    $headers = 'MIME-Version: 1.0' . "\r\n";
       $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
       $headers .= "From: $SERVER_EMAIL" . "\r\n" ;
	   $subject1 = "SS WEB TECHNOLOGIES - New Resource Account Created.";
	$subject = "SS WEB TECHNOLOGIES - New Account Information.";
	$body = "Hi $_POST[fullname],<br>Your account has been successfully created with us.<br>Please click the below link to setup your password. <br>
	<br>
	<a href='$l'>Password Setup link </a><br>
	<br>
	Copy paste the below link in your browser addressbar if above link is not working.<br>
	$l
	<br>
	<i>Your account is currently InActive. You will receive mail communication once your account is activated by admin</i>
	Thanks,<br>
	SS WEB TECHNOLOGIES";
	$body1 = "Hi Admin,<br>New Resource account has been successfully created.<br>Please login into admin area to activate the user. <br>
	<br>
	<a href='$ll'>admin login </a><br>
	<br>
	
	<br>
	<i>Resource account is currently InActive.</i>
	Thanks,<br>
	SS WEB TECHNOLOGIES";
	mail($user_email, $subject, $body, $headers);
	mail($ADMIN_EMAIL, $subject1, $body1, $headers);
	
	//$ll = 'http://'.$_SERVER[HTTP_HOST].'/signin';
	
	// send email to new user along with registration details.
	
   /* $headers = 'MIME-Version: 1.0' . "\r\n";
       $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
       $headers .= "From: Indef Systems <DO NOT REPLAY>" . "\r\n" ;
	$subject = "Indef Systems - New Resource Account Created.";
	$body = "Hi Admin,<br>New Resource account has been successfully created.<br>Please login into admin area to activate the user. <br>
	<br>
	<a href='$ll'>admin login </a><br>
	<br>
	
	<br>
	<i>Resource account is currently InActive.</i>
	Thanks,<br>
	Indef Systems.";
	mail($user_email, $subject, $body, $headers);*/

}

$msg = "You have successfully registered with Us, Please check your email and activate your account";

header("Location: registration?msg=s");
 
} else 
{
	$msg = "Email Already Exists."; 
	header("Location: registration?errmsg=$msg");
}
} 


?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>SS WEB TECHNOLOGIES- Signup</title>
<link href="css/style.css" type="text/css" rel="stylesheet"/>
<script type="text/javascript">
function validateForm2() {	
	var fullname = document.getElementById('fullname').value;
	if((fullname == "")||(fullname == null)){
        alert("Please enter full name");
		return false;
	}	
	var emailaddress = document.getElementById('emailaddress').value;
	if((emailaddress == "")||(emailaddress == null)){
        alert("Please enter email address");	
	return false;
	}
	
	if (echeck(emailaddress)==false){
		return false;
	}

	var message = document.getElementById('message').value;
	if((message == "")||(message == null)){
	 alert("Please enter message");
	return false;
	}	
	var question = document.getElementById('question').value;
	if((question == "")||(question == null)){
	 alert("Please enter the sum of given two digits");
	return false;
	}
	var ans = document.getElementById('ans').value;
	if(question != ans){
	 alert("Wrong Answer. Please try one more time");
	return false;
	}
}

function echeck(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		    alert("Incorrect email address");
		   return false
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		    alert("Incorrect email address");
		   return false
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Incorrect email address");
		    return false
		}
		 if (str.indexOf(at,(lat+1))!=-1){
		     alert("Incorrect email address");
		    return false
		 }
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Incorrect email address");
		    return false
		 }
		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Incorrect email address");
		    return false
		 }		
		 if (str.indexOf(" ")!=-1){
		    alert("Incorrect email address");
		    return false
		 }					
	}
	
</script>
<?php if(isset($_GET['errmsg'])) { ?>
 <script>alert("<?php echo $_GET['errmsg']; ?>");</script>
<?php } ?>

<style>

.content_container .content_wrapper {
    width: 100%;
    max-width: 950px;
    min-height: 300px;
    background: #fff;
    margin: 51px auto;
    padding: 25px;
}</style>
</head>

<body>
	<div class="wrapper">
   	  <?php  require_once('includes/header.php'); ?> 
        <div class="content_container">
            <div class="content_wrapper">
                <div class="tools_wrapper">
                    <?php if(isset($_REQUEST['msg'])) { if($_REQUEST['msg']=="s") echo "<div><font color='#428bca'>You have successfully registered with Us, Please check your email and activate your account.</font></div><br><br><a href='index'>Click here</a> to login"; } else { ?>
    <form method="post" class="login_form"  onSubmit="return validateForm2();">
	<?php echo $msg; ?>
      <div class="signup_wrap">
        <h3>Signup</h3>
        <div class="field_item clear_fix">
          <div class="label"><label >Full Name</label></div>
          <div class="input"><input name="fullname" id="fullname" class="fullname" type="text" size="35" /></div>
        </div>
        <div class="field_item clear_fix">
          <div class="label"><label>Email Address</label></div>
          <div class="input"><input name="emailaddress" id="emailaddress" type="text"  size="35" /></div>
        </div>
        <div class="field_item clear_fix">
          <div class="label"><label >Skill Set</label></div>
          <div class="input"><textarea rows="5" cols="27"  name="message" id="message" class="required"></textarea></div>
        </div>
       
        <div class="question">
				<?php $n1 = rand(1,10); $n2 = rand(1,10); echo $n1." + ".$n2; ?> = <input name="question" id="question" type="text"  size="8" />
				<input type="hidden" value="<?php echo $n1+$n2 ?>" name="ans" id="ans" /></div>
        <div class="action_butt">
            <input name="submit" id="submit" type="submit" class="signupbutton" value="Signup"  />
            <input name="cancel" id="cancel" type="button" value="Cancel" onclick="window.location= 'index'" />
            <span class="paragraph clear_fix">After verifying your details activation link and password setup link will be sent to your email address</span>
        </div>
        <!--<div id="errormsg" style="display:none">
          <p> Please check your user name and password. </p>
          <p><a href="signin">Retry Here</a> </p>
        </div>-->
      </div>
    </form>
   <?php  } ?>
                </div>
            </div>
        </div>
    </div>
    <?php  require_once('includes/footer.php'); ?> 
</body>
</html>
