<?php require_once('Connections/oprmConn.php'); 
/*if($_SESSION['role_id']!='') {
if($_SESSION['role_id']==1) {
header("Location: admin/index");
}
if($_SESSION['role_id']==2) {
header("Location: resource/index");
}
}*/
if(isset($_SESSION['user_id'])!=''){
if($_SESSION['role_id']==2) {
header("Location: resource/index");
}
if($_SESSION['role_id']==1) {
header("Location: admin/index");
}
}
if(isset($_POST['username'])) {
	$pw = md5($_POST['password']); 
$email = $_POST['username'];

mysql_select_db($database_oprmConn, $oprmConn);
$query_getUser = "SELECT * FROM weby_users WHERE email_address = '$email' AND password = '$pw' AND status = 'A'";
$getUser = mysql_query($query_getUser, $oprmConn) or die(mysql_error());
$row_getUser = mysql_fetch_assoc($getUser);
$totalRows_getUser = mysql_num_rows($getUser);

if($totalRows_getUser == 0) {
	
$msg = "Your Email address or Password has not been recognised.  Please try again.";
header("Location: index.php?msg=$msg");
}

if($totalRows_getUser > 0) {
	$_SESSION['role_id']= $row_getUser['user_role'];
$_SESSION['user_id']=$row_getUser['user_id'];

if($_SESSION['role_id']==1) {
header("Location: admin/index");
}
if($_SESSION['role_id']==2) {
header("Location: resource/index");
}
}else {
$msg = "<font color='red'>Invalid Login ! Please try again.</font>";
}


}
if (isset($_SERVER['PHP_SELF'])) /* this is means that the page get redirected from .htaccess */
{
$parts = explode('/', $_SERVER['PHP_SELF']);
array_shift($parts);
$url_name = $parts[0];
$url_name = str_replace(".php", "", $url_name);

}

?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title> SS WEB TECHNOLOGIES- Login</title>
<link href="/images/favicon.png" rel="shortcut icon" type="image/x-icon">
<link href="css/style.css" type="text/css" rel="stylesheet"/>
<script language = "Javascript">

function validateForm() {
	
	var emailID=document.getElementById('username');
	
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please entere your email address")
		emailID.focus()
		return false
	}
	
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}



	
 var address = document.getElementById('password').value;
	if (address == "") {
        //message = message+"Address be filled out\n";
		alert("Please enter your Password")
       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("Your email address is incorrectly formatted")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Your email address is incorrectly formatted")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Your email address is incorrectly formatted")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Your email address is incorrectly formatted")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Your email address is incorrectly formatted")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Your email address is incorrectly formatted")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Your email address is incorrectly formatted")
		    return false
		 }

 		 return true					
	}
</script>
<?php if(isset($_GET['msg'])) { ?>
 <script>alert("<?php echo $_GET['msg']; ?>");</script>
<?php } ?>
</head>

<body>
<div class="wrapper">
  <?php  require_once('includes/header.php'); ?> 
  <article class="banner_container"><img src="/RM/images/login-image.jpg"/></article>
    <article class="content_container">
            <div class="content_wrapper clear_fix">
                <div class="tools_wrapper">
                    <form action="" method="post" class="login_form" onsubmit="return validateForm()">
                      <div class="login_wrap">
                        <h3>Signin</h3>
                        <div class="field_item clear_fix">
                          <div class="label"><label>User Name</label></div>
                          <div class="input"><input name="username" id="username" class="u_name" type="text" size="35"></div>
                        </div>
                        <div class="field_item clear_fix">
                          <div class="label"><label>Password</label></div>
                          <div class="input"><input name="password" id="password" type="password" size="35"></div>
                        </div>
                        <div class="action_butt">
                            <input name="submit" id="submit" type="submit" class="loginbutton" value="Signin">
                            <a href="forgot-password">Forgot Password?</a> <br>
                            <a href="registration">No account yet? Sign Up Now</a>
                        </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>
                </div>
      
  </article>
        <?php  require_once('includes/footer.php'); ?>
</div>
</body>
</html>
