<?php require_once('Connections/courier.php'); 

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
?>
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}


$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}
mysql_select_db($database_courier, $courier);
if (isset($_POST['username'])) {
  $loginUsername=$_POST['username'];
  $password= md5($_POST['password']);
  $MM_fldUserAuthorization = "";
  $MM_redirectLoginSuccess = "index.php";
  $MM_redirectLoginFailed = "index.php?error";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_courier, $courier);
  
  $LoginRS__query="SELECT * FROM users WHERE username='$loginUsername' AND password='$password' AND account_status='Active'";
  $LoginRS = mysql_query($LoginRS__query, $courier) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  $row_LoginRS = mysql_fetch_assoc($LoginRS);
  if ($loginFoundUser) {
     $loginStrGroup = "";
    
    $_SESSION['myMM_Username'] = $loginUsername;
    $_SESSION['myMM_UserGroup'] = $loginStrGroup;
    $_SESSION['myMM_Fullname'] = $row_LoginRS['title'].' '.$row_LoginRS['firstnames'].' '.$row_LoginRS['surname'];
    $_SESSION['myMM_Userid'] = $row_LoginRS['id'];
    $_SESSION['security'] = $row_LoginRS['security'];
    $_SESSION['securitystaff'] = $row_LoginRS['securitystaff'];
    $_SESSION['staffid'] = $row_LoginRS['staffid'];
    $security=$_SESSION['security'];	
    
    //CHECK IF BRANCH IS HEAD QUARTERS
    // $branch_info = mysql_fetch_assoc(mysql_query("SELECT * FROM branches WHERE active_branch = 'active'"));
    $branch_info = mysql_fetch_assoc(mysql_query("SELECT * FROM branches WHERE id = '".$row_LoginRS['active_branch_id']."'"));
    $_SESSION['main_branch'] = $branch_info['head_branch'];
    $_SESSION['branch_name'] = $branch_info['branch_name'];
    $_SESSION['change_price'] = $branch_info['change_price'];
    $_SESSION['branch_code'] = $branch_info['branch_code'];
    $_SESSION['branch_type'] = $branch_info['type'];
    $_SESSION['name_code'] = $branch_info['name_code'];
    $_SESSION['branch_id'] = $branch_info['id'];

    $branch_info = mysql_fetch_assoc(mysql_query("SELECT * FROM branches WHERE id = '".$row_LoginRS['branch_id']."'"));
    $_SESSION['branch_id_user'] = $branch_info['id'];
	
    $provider_id = $row_LoginRS['provider_id'];
    $query_rscarmodel11 = "SELECT * FROM provider_api where provider_id='$provider_id'";
    $rscarmodel11 = mysql_query($query_rscarmodel11, $courier) or die(mysql_error());
    $row_rscarmodel11 = mysql_fetch_assoc($rscarmodel11);

    $_SESSION['provider_id'] = $row_rscarmodel11['provider_id'];
    $_SESSION['provider_short_code'] = $row_rscarmodel11['short_code'];
    $_SESSION['provider_id_master'] = $row_rscarmodel11['provider_id_master'];
    $_SESSION['vvk'] = $row_rscarmodel11['apikey'];
    $_SESSION['facility_number'] = $row_rscarmodel11['facility_number'];
    $_SESSION['facility_name']= $row_rscarmodel11['facility_name'];
	
    if (isset($_SESSION['PrevUrl']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];	
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>
