<?php
	ob_start();
	session_start();
	
	//Included Files
	include("../class/dbConnection.php");
    include("../class/admin.php");
	
	//Declaring Variables
	$adminId;
	$currentPassword;
	$newPassword;
	$dbPassword;
	$passwordResult;
	
	//Retrieving Session Variable
	if(isset($_SESSION["adminId"]))
	{
		$adminId = $_SESSION["adminId"];
		//echo $instructorId;
	}
	else
	{
		header('Location:index.php?message=Unauthorized%20Access%20Prohobited');
	}
	
	//Retrieving Form Fields
	$currentPassword = $_REQUEST["pasCurrentPassword1"];
	$newPassword = $_REQUEST["pasNewPassword1"];
	//echo $currentPassword . "<br/>" . $newPassword;
		
	//Creating 'dbConnection' Object
	$dbObject = new dbConnection();
	$con = $dbObject->getConnection();
	//echo $con;
	
	if($con)
	{
		//MySQL Connection Established
		$dbResult = $dbObject->selectDatabase();
		if($dbResult)
		{
			//Creating 'admin' Object
			$adminObj = new admin();
			$recordSet = $adminObj->getAdminDetails($adminId,$con);
			while($row = mysql_fetch_array($recordSet))
			{
				$dbPassword = $row["password"];
			}
			 if($dbPassword == $currentPassword)
			{
				$passwordResult = $adminObj->setPassword($adminId,$newPassword,$con);
				//echo $passwordResult;
				if($passwordResult)
				{
					header('Location:changePassword.php?success=Password%20Updated%20Successfully');
				}
				else
				{
					header('Location:changePassword.php?failure=Unable%20To%20Update%20Password');
				}
			} 
			else
			{
				header('Location:changePassword.php?failure=Password%20Mismatch');
			}
		}
		else
		{
			echo mysql_errno() . "<br/>" . mysql_error();
		}		
	}
	else
	{
		echo mysql_errno() . "<br/>" . mysql_error();
	}
?>