<?php
$path_to_root = "../..";
include($path_to_root . "/includes/session.inc");
include($path_to_root . "/includes/ui.inc");
//include($path_to_root . "/includes/current_user.inc");
include_once($path_to_root . "/includes/db/connect_db.inc");

	$arr = array();
	
	/*if(!empty($_POST['selectedVal1']) && !empty($_POST['selectedVal2']) )//search from both
	{
		$myrow2 = get_student_by_both($_POST['selectedVal1'],$_POST['selectedVal2']);
		//print_r($myrow2);
		
		//$arr[0] = $myrow2[0];
		$res = $myrow2[0];
		$res = intval($res);
		
	}*/
	
	/*if(!empty($_POST['selectedVal1']) )//search from std reg num
	{
		$myrow2 = get_student_by_std_reg_num($_POST['selectedVal1']);
		//print_r($myrow2);
		
		//$arr[0] = $myrow2[0];
		$res = $myrow2[0];
		$res = intval($res);
	}
	elseif(!empty($_POST['selectedVal2']))//search from uwe reg num
	{
		$myrow2 = get_student_by_uwe_reg_num($_POST['selectedVal2']);
		//print_r($myrow2);
		
		//$arr[0] = $myrow2[0];
		$res = $myrow2[0];
		$res = intval($res);
	}
	elseif(!empty($_POST['selectedVal3']))//search from nic num
	{
		$myrow2 = get_student_by_nic($_POST['selectedVal3']);
		//print_r($myrow2);
		
		//$arr[0] = $myrow2[0];
		$res = $myrow2[0];
		$res = intval($res);
	}
	
	elseif(empty($_POST['selectedVal1']) && empty($_POST['selectedVal2']) && empty($_POST['selectedVal3']) )//none filled
	{
		$res = "New Student";
	}*/

	
	if($_POST['selectedVal1'] == 1)//search from std reg num
	{
		$myrow2 = get_student_by_std_reg_num($_POST['selectedVal2']);
		//print_r($myrow2);
		
		//$arr[0] = $myrow2[0];
		$res = $myrow2[0];
		$res = intval($res);
	}
	elseif($_POST['selectedVal1'] == 2)//search from uwe reg num
	{
		$myrow2 = get_student_by_uwe_reg_num($_POST['selectedVal2']);
		//print_r($myrow2);
		
		//$arr[0] = $myrow2[0];
		$res = $myrow2[0];
		$res = intval($res);
	}
	elseif($_POST['selectedVal1'] == 3)//search from nic num
	{
		$myrow2 = get_student_by_nic($_POST['selectedVal2']);
		//print_r($myrow2);
		
		//$arr[0] = $myrow2[0];
		$res = $myrow2[0];
		$res = intval($res);
	}
	elseif($_POST['selectedVal2'] == '')
	{
		$res = "New Student";
	}
	else
	{
		$res = "New Student";
	}
	

	/*if(is_int($res))
	{
		echo "Yes Int".$res;
	}
	else
	{
		echo "Not Int".$res;
	}*/
	
	echo json_encode($res);
	exit();

	function get_student_by_std_reg_num($stdregnum)
	{
	  $sql = "SELECT debtor_no,debtor_ref inactive FROM ".TB_PREF."debtors_master WHERE isStudent = 1 AND debtor_ref ='".$stdregnum."' ";
	  $result = db_query($sql, "could not get student details check student Reg. No. ");
	  return db_fetch($result);
	}
	
	
	function get_student_by_uwe_reg_num($uweregnum)
	{
	  $sql = "SELECT debtor_no,debtor_ref inactive FROM ".TB_PREF."debtors_master WHERE isStudent = 1 AND uweRegNumber ='".$uweregnum."' ";
	  $result = db_query($sql, "could not get student details");
	  return db_fetch($result);
	}
	
	/*function get_student_by_both($stdregnum,$uweregnum)
	{
	  $sql = "SELECT debtor_no,debtor_ref inactive FROM ".TB_PREF."debtors_master WHERE isStudent = 1 AND debtor_ref ='".$stdregnum."' AND uweRegNumber ='".$uweregnum."' ";
	  $result = db_query($sql, "could not get student details");
	  return db_fetch($result);
	}*/
	
	function get_student_by_nic($nic)
	{
	  $sql = "SELECT debtor_no,stdRegNo inactive FROM ".TB_PREF."debtors_master WHERE isStudent = 1 AND nic ='".$nic."' ";
	  $result = db_query($sql, "could not get student details");
	  return db_fetch($result);
	}
	
	/*echo "course short name= ".$myrow2["course_short_name"];
	echo "course id= ".$myrow2["course_id"];
	echo "batch year= ".$myrow2["batch_year"];
	echo "batch month= ".$myrow2["batch_month"];
	echo "batch entrM= ".$myrow2["batch_entrM"];
	echo "tuition Fee= ".$myrow2["tuitionFee"];
	echo "uwe Fee= ".$myrow2["uweFee"];
	echo "tot fee= ".$myrow2["totalFee"];*/

?>
