<?php session_start();
include("header.php");
require_once('dbcon.php');
?>
<head>
<!-- Bootstrap -->
    <link href="vendors/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
	</head>
	
<div class="container">
	 <div id="loginbox" style="display:block;margin-top:150px;" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">                    
            <div class="panel panel-info" >
                    <div class="panel-heading" style="background-color:#ea7d09;color:#fff;font-weight:bold;text-align:center;" >
                        <div class="panel-title">Reset Password</div>
                       
                    </div>     

                    <div style="padding-top:30px" class="panel-body" >

                        <div style="display:none" id="login-alert" class="alert alert-danger col-sm-12"></div>
                            
                         <form  class="form-horizontal" role="form" method="post" action="">
                            
                                    
                            <div style="margin-bottom: 25px" class="input-group">
                                        <span class="input-group-addon"><i class="glyphicon glyphicon-message"></i></span>
                                        <input id="username" type="email" class="form-control" name="usernm" placeholder="Your Email" required>                                        
                            </div>
                                
                            <div style="margin-bottom: 25px" class="input-group">
                                      <p> By Clicking <b>Reset Password, </b> a new password will be sent to your email address immediately. </p>
                            </div>

                                <div style="margin-top:10px;margin-bottom:20px;" class="form-group">
                                    <!-- Button -->

                                    <div class="col-sm-12 controls" style="margin-left:150px;"  >
                                        <input type="submit" name="submitx"  value="Reset Password" id="btn-login"  class="btn btn-success pull-left"  />
                                    </div>
                                </div>
								
<hr/>
                                <div class="form-group">
                                    <div class="col-md-12 control">
                                        <div style="border-top: 1px solid#888; padding-top:15px;font-size:85%" >    
                                        <a style="margin-left:150px;background-color:#ea7d09;color:black;font-weight:bold;" class="btn btn-" rel="tooltip" title="Go to Home"  href="index.php"  >  &nbsp;&nbsp; <b><< Back Home </b></a>
										
                                        </div>
                                    </div>
                                </div>    
                            </form>     
	


                        </div>                     
                    </div>  
        </div>
		
								<?php

     
    if(isset($_POST['submitx'])){
			
		 $usr = $_POST['usernm'];   
		 
		 $query = mysql_query("select * from airtime_customer where email = '$usr' and user_type='normal'  ") or die (mysql_error());
		   $nums = mysql_num_rows($query);

			  if ($nums > 0) {
				  
					$rws = mysql_fetch_array($query);
					$company = $rws['company'];
					$fname = $rws['fname'];
					$lname = $rws['lname'];
					$user = ucwords($fname." ".$lname);
					
					$email =  $usr;
					
				  //generate password
					$string = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
						function randomPassword($string) {
							$alphabet = $string;
							$pass = array(); //remember to declare pass as an array
							$alphaLength = strlen($alphabet) - 1;  //put the length -1 in cache
						
								for ($i = 0; $i < 8; $i++) {
									$n = rand(0, $alphaLength);
									$pass[] = $alphabet[$n];
								}
									
									return implode($pass); //turn the array into a string
						}
								
						$pword = randomPassword($string);
						//echo $pword;
								
						// update password
							$queryx = mysql_query("update airtime_customer set password = '$pword' where email = '$email' ");
				  
							  if($queryx){
									
										// send email notification		
										require 'admin/phpmail/PHPMailerAutoload.php';

										//Create a new PHPMailer instance
										$mail = new PHPMailer;
										// Set PHPMailer to use the sendmail transport
										$mail->isSendmail();
										//Set who the message is to be sent from
										$mail->setFrom('info@mobisky.biz', 'Airtime Portal Team');
										//Set an alternative reply-to address
										$mail->addReplyTo('pnjoroge@mobisky.biz', 'Airtime Portal Team');
										//Set who the message is to be sent to
										$mail->addAddress($email, 'Airtime Portal System User Account');
										//Set the subject line
										$mail->Subject = 'Airtime Portal Management Information System';

										$mail->Body = '

										Dear '.$user.', <br>
										<br>
										Your new password is <b>'.$pword.'</b> <br>
										<br>
										Please sign-in to your Airtime Portal User Account and go to your Profile page on the top right drop-down menu to change password.<br>
										<br>
										Warm Regards,<br>
										<br>
										 Airtime Portal Team.
										<br>
										<br>
										<br>
										<p align="center"><small>You received this E-mail beacause you agreed to our Airtime Portal System terms and Conditions</p>
										';
									
										//Replace the plain text body with one created manually
										$mail->AltBody = 'Empty plain-text message body';
										//Attach an image file
										$mail->addAttachment('admin/phpmail/images/phpmailer_mini.png');

										//send the message, check for errors
										if (!$mail->send()) {
											//echo "Mailer Error: " . $mail->ErrorInfo;
											?>
												<script>
													alert("Error Sending Email! Please try again.");
													window.location="resetpword.php";
												</script>
											<?php
										} else {
											// echo "Message sent!";
											?>
												<script>
													alert("Email Notification Sent. If you do not find the email in your inbox, find it in the spam's folder.");
													window.location="login.php";
												</script>
											<?php
										}
									
								 }else{
									?>
										<script>
											alert("Error updating Password! Please try again.");
											window.location="resetpword.php";
										</script>
									<?php
								 }
			}
			else {
				 ?>
				<script>
					alert("Error! Invalid Email Address.");
				</script>
			<?php
			  /* echo ' <div class="col-md-offset-3 col-md-9">
                      <button id="btn-signup" type="button" class="btn btn-error"><i class="icon-hand-right"></i>Invalid Username or Password! </button>                  
                    </div>'; */
			}

	}

?>
</div>

   <!--?php include("footerlinks.php"); ?-->
	 
	  <script src="vendors/jquery/dist/jquery.min.js"></script>
    <!-- Bootstrap -->
    <script src="vendors/bootstrap/dist/js/bootstrap.min.js"></script>