<?php 
$form_name = "Change Password";
$table_name = "lime_users";
$primary_key = "usr_id";

$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : "";
	
include_once("includes/config.php");
include_once("includes/common_functions.php");

$common = new General_Functions();
$css_class = "x-gray";
// $common->SetCssClass($css_class);

$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : "";
$act_type = isset($_REQUEST['act_type']) ? $_REQUEST['act_type'] : "";
$cancel_link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
$action = $_SERVER['SCRIPT_NAME']."?page=".$_REQUEST['page']."&mode=submit";

switch($mode)
{
	case "submit":
                $status = isset($_REQUEST['status']) ? $_REQUEST['status'] : 0;
                
                if($_REQUEST['id'] != '')
                    $whr = ' AND '.$primary_key." != '".$_REQUEST['id']."'";
                else
                    $whr = '';
                
                $qry = "SELECT * FROM ".$table_name." WHERE users_name = '".$_REQUEST['usr_name']."' OR email = '".$_REQUEST['usr_email']."'".$whr;
                $common->sql->query($qry, SQL_ALL, SQL_ASSOC);
                $rec = $common->sql->record;
                
                if(count($rec) == 0)
                {
                    if($_REQUEST['id'] != '')
                    {
						if(md5($rec[0]['password']) == $_REQUEST['usr_pwd'])
							$_REQUEST['usr_pwd'] = $_REQUEST['usr_pwd'];
						else
							$_REQUEST['usr_pwd'] = md5($_REQUEST['usr_pwd']);
						
                        $_query = "UPDATE ".$table_name." SET   password        	= '".$_REQUEST['usr_pwd']."',
                                                                update_time         = now(),
                                                                update_user         = '".USR."'
                                                          WHERE ".$primary_key."    = '".$_REQUEST['id']."'";
                        $rec = $common->sql->exeQuery($_query);
						$common->UserLog($form_name, $table_name, $_REQUEST['id'], 'Updated');
                        $_SESSION['msg'] = 'Password Updated Successfully';
                    }
                }
                else
                {
                    $_SESSION['msg'] = 'User Name / Email Already Exsists';
                }
		$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
		header("location:".$link);
	break;
        
        case "delete":
                if(isset($_REQUEST[$prefix.'id']))
                        $id = $_REQUEST[$prefix.'id'];
                else
                        $id = implode("', '", $_REQUEST[$primary_key]);

                $query = "DELETE FROM ".$table_name." WHERE ".$primary_key." IN ('".$id."')";

                $rec = $common->sql->exeQuery($query);

                if($rec)
                {
                        $_SESSION['msg'] = 'Selected record has been deleted successfully';
                        if(isset($_REQUEST['id']))
                                $common->UserLog($form_name, $table_name, $_REQUEST['id'], 'Delete');
                        else
                                for($i=0; $i<count($_REQUEST[$primary_key]); $i++)
                                        $common->UserLog($form_name, $table_name, $_REQUEST[$primary_key][$i], 'Delete');
                }

                $query_string = str_replace("&mode=delete","",$_SERVER['QUERY_STRING']);
                $link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
                header("location:".$link);
        break;
	
	default:
		$qry = "SELECT * FROM ".$table_name." WHERE ".$primary_key." = '".$_SESSION['TER']["user"]."'";
		$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
		
		$usr_id = $rec[0]['usr_id'];
		$users_name = $rec[0]['users_name'];
		$password = $rec[0]['password'];
		$full_name = $rec[0]['full_name'];
		$email = $rec[0]['email'];
		$status = $rec[0]['status'];
		
		echo '<div class="content_bg">';
			include("add.php");
		echo '</div>';
	break;
}
?>