<?php require_once('Connections/oprmConn.php'); 
if(isset($_POST['emailaddress'])) {
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 and weby_users.status in ('P','A')";
$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) {
$t = rand(12100,99999);

$l = "http://$_SERVER[HTTP_HOST]/RM/change-password?token=$t&user=$row_getPa[user_id]";

mysql_select_db($database_oprmConn, $oprmConn);
$query_getProj1 = "update weby_users set token = '$t' where user_id = $row_getPa[user_id] and status in ('P','A')";
$getProj1 = mysql_query($query_getProj1, $oprmConn) or die(mysql_error());

// send email to new user along with registration details.
	$user_email = $row_getPa['email_address'];
    $headers = 'MIME-Version: 1.0' . "\r\n";
       $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
       $headers .= "From: $SERVER_EMAIL" . "\r\n" ;
	$subject = "5S Store Ltd- Password Reset.";
	$body = "Hi $row_getPa[full_name],<br>Please click the below link to Reset your password. <br>
	==================================================================<br>
	<a href='$l'>password reset link </a><br>
	==================================================================<br>
	Copy paste the below link in your browser addressbar if above link is not working.<br>
	$l
	<br>
	Thanks,<br>
	5S Store Ltd.";
	mail($user_email, $subject, $body, $headers);
	
	$msg = "Password reset link has been sent to your email. Please check your email and follow the instructions given in the email.";
header("Location: forgot-password?success=success");
} else {
$msg = "Invalid Email Id.";
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>5S Store Ltd- Forgot Password</title>
<link href="css/style.css" type="text/css" rel="stylesheet"/>
<script type="text/javascript">
<!--
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }
//-->
    </script>
    <script>
$(document).ready(function(){
     if($("#pTag").val() != "") {
       $("#forgot-pwd-block").show();
     } else {
      $("#forgot-pwd-block").hide();
    }
  });
</script>
</head>

<body>
	<div class="wrapper">
   	  <?php  require_once('includes/header.php'); ?> 
        <div class="content_container">
            <div class="content_wrapper">
                <div class="tools_wrapper">
                <h3>Forgot Password</h3>
                <?php if(!isset($_GET['success'])){?>
                    <form  action="#" method="post" id="forgot-pwd-block" name="forgot-pwd-block">
                    <div id="login">
                      <p> Provide your email address</br>
                        <input name="emailaddress" id="emailaddress" type="text" placeholder="john@websiteaddress.com" size="35" />
                        <br/>                        <input name="Button" type="submit" class="loginbutton" onClick="MM_validateForm('emailaddress','','RisEmail');return document.MM_returnValue" value="Submit" />
                        <input name="cancel" id="cancel" type="button" value="Cancel" onclick="window.location= 'index'" />
                    </div>
                  </form>
                  <?php } if(isset($_GET['success'])){?>
                  <p id="pTag">A password rest link will be sent to your email address.</p>
                  <?php } ?>
                  <div id="errormsg" style="display:none">
                    <p> Invalid email address. Please retry or contact Us </p>
                    <p><a href="index">Login Here</a> </p>
                  </div>
                </div>
            </div>
        </div>
    </div>
    <?php  require_once('includes/footer.php'); ?> 
</body>
</html>
