<?php

/**@manasera
 */

require_once 'DB_Functions.php';
$db = new DB_Functions();

// json response array
$response = array("error" => FALSE);

if (isset($_POST['id']) && isset($_POST['password'])) {

    // receiving the post params
	$password = $_POST['password'];
	
	$user_id= $_POST['id'];
	$table= $_POST['who'];
	
  

    // get the user by email and password
    $user = $db->getUserByEmailAndPassword($user_id,$password,$table);

    if ($user != false && $table=='parent') {
        // use is found
        $response["error"] = FALSE;
	
		$response["user"]["firstname"] = $user["firstname"];
        $response["user"]["lastname"] = $user["lastname"];
        $response["user"]["phone"] = $user["phone"];
        $response["user"]["sex"] = $user["sex"];
        $response["user"]["user_id"] = $user["user_id"];
		$response["user"]["month"] = $user["month"];
		$response["user"]["day"] = $user["day"];
		echo json_encode($response);
	}else if ($user != false ){
		
        // use is found
        $response["error"] = FALSE;
       // $response["uid"] = $user["firstname"];
        $response["user"]["firstname"] = $user["firstname"];
        $response["user"]["lastname"] = $user["lastname"];
        $response["user"]["level"] = $user["level"];
        $response["user"]["sex"] = $user["sex"];
		$response["user"]["faculty"] = $user["faculty"];
		$response["user"]["department"] = $user["department"];
		$response["user"]["user_id"] = $user["user_id"];
		$response["user"]["month"] = $user["month"];
		$response["user"]["day"] = $user["day"];
        echo json_encode($response);
    } else {
        // user is not found with the credentials
        $response["error"] = TRUE;
        $response["error_msg"] = "Login credentials are wrong. Please try again!";
        echo json_encode($response);
    }
} else {
    // required post params is missing
    $response["error"] = TRUE;
    $response["error_msg"] = "Required parameters user_id or password is missing!";
    echo json_encode($response);
}
?>

