<?php 
##################################################################
#  This is the basic that can be called  within the web application
#  Include this after the configuration file on every page
#  File name: functions.php
##################################################################

//Call the configuration file
include('../includes/configuration.php');


	//function get_user()
//	{
//		$user_id = $_SESSION['id'];
//		return $user_id;
//	}





//not functional
//Get Current User's ID fro sessionif i need t use this in startic manner
	function get_user_id()
	{
		$pn = $_COOKIE['listdnttt'];
		return $pn;
	}

	
	
	
	
//Function to fetch students Lat login date
	function get_last_login($user_id)
	{
		$result = mysql_query("SELECT DATE_FORMAT(last_login, '%b %e,%y  %l:%i:%p') as user_lastlogindate  FROM students WHERE id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$last_login = $row['user_lastlogindate'];
		
	return $last_login;
	}

//Functions to fetch user's Full name
	function get_user_fullname($user_id)
	{
		$result = mysql_query("SELECT * FROM students WHERE id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$first_name = $row['first_name'];
		$other_names = $row['other_names'];
		
		$user_fullname = $first_name . " " . $other_names;

	return $user_fullname;
	}
	
	
//Function to fetch user name
	function get_user_username($user_id)
	{
		$result = mysql_query("SELECT * FROM students WHERE id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$username = $row['username'];
		

	return $username;
	}
	
	
	
	
	
	
//Function to fetch email
	function get_user_email($user_id)
	{
		$result = mysql_query("SELECT email FROM students WHERE id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$email = $row['email'];

	return $email;
	}
	
	
	
	//Function to fetch phone number
	function get_user_phone_no($user_id)
	{
		$result = mysql_query("SELECT phone_no FROM students WHERE id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$ph = $row['phone_no'];

	return $ph;
	}
	
	
	//function dat get user security id  by id 
	function get_security_id($user_id)
	{
		$q = "SELECT security_id FROM students WHERE id = '$user_id' ";
		$r = mysql_query($q);
		$row = mysql_fetch_assoc($r);
		
		return $row['security_id'];
		
		
	}
	
	
	//function dat get user address 
	function get_user_address($user_id)
	{
		$q = "SELECT address FROM students WHERE id = '$user_id'";
		$r = mysql_query($q);
		$row = mysql_fetch_assoc($r);
		
		return $row['address'];
		
		
	}
	
	
	//function dat get user image by id 
	function get_user_image($user_id)
	{
	$q = "SELECT image FROM students WHERE id = '$user_id'";
	$r = mysql_query($q);
	$row = mysql_fetch_assoc($r);
	
	return $row['image'];
	
	
	}
	
	
	
	
	//Functions to fetch user's first name
	function get_user_first_name($user_id)
	{
		$result = mysql_query("SELECT first_name FROM students WHERE id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$first_name = $row['first_name'];
	

	return $first_name;
	}
	
	//Functions to fetch user's other names
	function get_user_other_names($user_id)
	{
		$result = mysql_query("SELECT other_names FROM students WHERE id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$on = $row['other_names'];
	

	return $on;
	}
	
	//Functions to fetch user's marital status
	function get_user_ms($user_id)
	{
		$result = mysql_query("SELECT marital_status FROM students WHERE id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$ms = $row['marital_status'];
	

	return $ms;
	}
	
	
		//Functions to fetch user's gender
	function get_user_gender($user_id)
	{
		$result = mysql_query("SELECT gender FROM students WHERE id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$gd = $row['gender'];
	

	return $gd;
	}
	
	
	
		//Functions to fetch user's religion
	function get_user_religion($user_id)
	{
		$result = mysql_query("SELECT religion FROM students WHERE id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$religion = $row['religion'];
	

	return $religion;
	}
	
	
	
	
	
	
//Function to fetch user level
	function get_user_level($id)
	{
		$result = mysql_query("SELECT level FROM students WHERE id = '$id'"); 
		$row = mysql_fetch_assoc($result);
		$level = $row['level'];
		
	return $level;
	}
	
	
	
	//function fetch user day
	function get_user_day($id)
	{
		$result = mysql_query("SELECT day FROM students WHERE id = '$id'");
		$row = mysql_fetch_assoc($result);
		$day = $row['day'];
		
		return $day;
	}
	
	
	
	
	//function fetch user month
	function get_user_month($id)
	{
		$result = mysql_query("SELECT month FROM students WHERE id = '$id'");
		$row = mysql_fetch_assoc($result);
		$month = $row['month'];
		
		return $month;
	}
	
	
	
	//function fetch user year
	function get_user_year($id)
	{
		$result = mysql_query("SELECT year FROM students WHERE id = '$id'");
		$row = mysql_fetch_assoc($result);
		$year = $row['year'];
		
		return $year;
	}
	
	
	//function fetch user nationality
	function get_user_nationality($id)
	{
		$result = mysql_query("SELECT nationality FROM students WHERE id = '$id'");
		$row = mysql_fetch_assoc($result);
		$n = $row['nationality'];
		
		return $n;
	}
	
	
	
	//function fetch user country
	function get_user_country_id($id)
	{
		$result = mysql_query("SELECT country_id FROM students WHERE id = '$id'");
		$row = mysql_fetch_assoc($result);
		$country_id = $row['country_id'];
		
		return $country_id;
	}
	
	
	//function fetch user state id
	function get_user_state_id($id)
	{
		$result = mysql_query("SELECT state_id FROM students WHERE id = '$id'");
		$row = mysql_fetch_assoc($result);
		$sid = $row['state_id'];
		
		return $sid;
	}
	
	//function fetch user lga id
	function get_user_lga_id($id)
	{
		$result = mysql_query("SELECT lga_id FROM students WHERE id = '$id'");
		$row = mysql_fetch_assoc($result);
		$lid = $row['lga_id'];
		
		return $lid;
	}
	
	
	
	
	//function get user permanent address
	
	function get_user_permanent_address($id)
	{
		$result = mysql_query("SELECT permanent_address FROM students WHERE id = '$id'");
		$row = mysql_fetch_assoc($result);
		$v = $row['permanent_address'];
		
		
		return $v;
	}
	
	
	
	
	
//function fetch user programme type
	function get_user_programme_type($id)
	{
		$result = mysql_query("SELECT type_id FROM students WHERE id = '$id'");
		$row = mysql_fetch_assoc($result);
		$v = $row['type_id'];
		
		
		return $v;
	}

	//function fetch user entry_year
	function get_user_entry_year($id)
	{
		$result = mysql_query("SELECT entry_year FROM students WHERE id = '$id'");
		$row = mysql_fetch_assoc($result);
		$v = $row['entry_year'];
		
		
		return $v;
	}
	
	
	
		//function fetch user get_highest qualififcation
	function get_user_highest_qualification($id)
	{
		$result = mysql_query("SELECT highest_qualification FROM students WHERE id = '$id'");
		$row = mysql_fetch_assoc($result);
		$v = $row['highest_qualification'];
		
		
		return $v;
	}
	
		//function fetch user get obtained_institution
	function get_user_institution_obtained($id)
	{
		$result = mysql_query("SELECT institution_obtained FROM students WHERE id = '$id'");
		$row = mysql_fetch_assoc($result);
		$v = $row['institution_obtained'];
		
		
		return $v;
	}
	
		//function fetch user get first_degree
	function get_user_first_degree($id)
	{
		$result = mysql_query("SELECT first_degree FROM students WHERE id = '$id'");
		$row = mysql_fetch_assoc($result);
		$v = $row['first_degree'];
		
		
		return $v;
	}
	
	
	
	
	
	//function fetch user credential
	function get_user_credential($id)
	{
		$result = mysql_query("SELECT pdf_file FROM students WHERE id = '$id'");
		$row = mysql_fetch_assoc($result);
		$v = $row['pdf_file'];
		
		return $v;
	}
	
	
	
//function select marital status 
function select_marital_status($st)
{
		
		if ($st == 'single')
		{
			$output = '<option value="0" > -- Please Select --</option>';
			$output .= '<option value="single" selected > Single</option>';
			$output .= '<option value="married" > Married</option>';
			$output .= '<option value="divorced" >Divorced</option>';
			$output .= '<option value="widowed" >Widowed</option>';
		
			
		}
		else if ($st == 'married')
		{
			$output = '<option value="0" > -- Please Select --</option>';
			$output .= '<option value="single"  > Single</option>';
			$output .= '<option value="married" selected> Married</option>';
			$output .= '<option value="divorced" >Divorced</option>';
			$output .= '<option value="widowed" >Widowed</option>';
		
			
		}
		else if ($st == 'divorced')
		{
			$output = '<option value="0" > -- Please Select --</option>';
			$output .= '<option value="single"  > Single</option>';
			$output .= '<option value="married" > Married</option>';
			$output .= '<option value="divorced" selected >Divorced</option>';
			$output .= '<option value="widowed" >Widowed</option>';
		
			
		}
		else if ($st == 'widowed')
		{
			$output = '<option value="0" > -- Please Select --</option>';
			$output .= '<option value="single"  > Single</option>';
			$output .= '<option value="married" > Married</option>';
			$output .= '<option value="divorced"  >Divorced</option>';
			$output .= '<option value="widowed" selected >Widowed</option>';
		
			
		}
		else
		{
		
			$output = '<option value="0" selected> -- Please Select --</option>';
			$output .= '<option value="single"  > Single</option>';
			$output .= '<option value="married" > Married</option>';
			$output .= '<option value="divorced" >Divorced</option>';
			$output .= '<option value="widowed" >Widowed</option>';
		
		}

	return $output;

}
//end of select marrital status



//function select gender
function select_gender($gd)
{
		
		if ($gd == 'M')
		{
			$output = '<option value="0" > -- Please Select --</option>';
			$output .= '<option value="M" selected > Male</option>';
			$output .= '<option value="F" > Female</option>';
		
			
		}
		else if ($gd == 'F')
		{
			$output = '<option value="0" > -- Please Select --</option>';
			$output .= '<option value="M"> Male</option>';
			$output .= '<option value="F" selected > Female</option>';
		
			
		}
		else
		{
		
			$output = '<option value="0"  selected  > -- Please Select --</option>';
			$output .= '<option value="M"> Male</option>';
			$output .= '<option value="F"> Female</option>';
		}

	return $output;

}
//end of select gender








//function select religion
function select_religion($r)
{
		
		if ($r == '1')
		{
			$output = '<option value="0" > -- Please Select --</option>';
			$output .= '<option value="1" selected > Islam</option>';
			$output .= '<option value="2" > Christianity</option>';
			$output .= '<option value="3" > Traditional</option>';
			$output .= '<option value="4" > Others</option>';
		
			
		}
		else if ($r == '2')
		{
			$output = '<option value="0" > -- Please Select --</option>';
			$output .= '<option value="1"  > Islam</option>';
			$output .= '<option value="2" selected > Christianity</option>';
			$output .= '<option value="3" > Traditional</option>';
			$output .= '<option value="4" > Others</option>';
		
			
		}
		else if ($r == '3')
		{
			$output = '<option value="0" > -- Please Select --</option>';
			$output .= '<option value="1"  > Islam</option>';
			$output .= '<option value="2" > Christianity</option>';
			$output .= '<option value="3" selected  > Traditional</option>';
			$output .= '<option value="4" > Others</option>';
		
			
		}
		else if ($r == '4')
		{
			$output = '<option value="0"> -- Please Select --</option>';
			$output .= '<option value="1"> Islam</option>';
			$output .= '<option value="2" > Christianity</option>';
			$output .= '<option value="3" > Traditional</option>';
			$output .= '<option value="4"  selected > Others</option>';
		
			
		}
		else
		{
		
			$output = '<option value="0"  selected> -- Please Select --</option>';
			$output .= '<option value="1"> Islam</option>';
			$output .= '<option value="2" > Christianity</option>';
			$output .= '<option value="3" > Traditional</option>';
			$output .= '<option value="4"  > Others</option>';
		}

	return $output;

}
//end of select religion


//function show religion
function show_religion($r)
{
		
		if ($r == '1')
		{
			
			$output .= 'Islam';
			
		}
		else if ($r == '2')
		{
			
			$output .= 'Christianity';
			
			
		}
		else if ($r == '3')
		{
			
			$output .= 'Traditional';
				
		}
		else if ($r == '4')
		{
			
			$output .= 'Others';
		
			
		}
		else
		{
		
			
			$output .= 'NIL';
		}

	return $output;

}
//end of show religion











//function select nationality
function select_nationality($n)
{
		
		if ($n == 'Nigerian')
		{
			$output = '<option value="0" > -- Please Select --</option>';
			$output .= '<option value="Nigerian" selected > Nigerian</option>';
			$output .= '<option value="Others" > NON Nigerian</option>';
		
			
		}
		else if ($n == 'Others')
		{
			$output = '<option value="0" > -- Please Select --</option>';
			$output .= '<option value="Nigerian" > Nigerian</option>';
			$output .= '<option value="Others"  selected>  NON Nigerian</option>';
		
			
		}
		else
		{
		
			$output = '<option value="0"  selected > -- Please Select --</option>';
			$output .= '<option value="Nigerian"> Nigerian</option>';
			$output .= '<option value="Others" > Others</option>';
		}

	return $output;

}

function show_nationality($n)
{
		
		if ($n == 'Nigerian')
		{
			$output = 'Nigerian';
		
		
			
		}
		else if ($n == 'Others')
		{
			$output = 'NON Nigerian';
		
			
		}
		else
		{
		
			$output = 'NIL';
		}

	return $output;

}




//select department
function select_department($department)
{
	$r = mysql_query("SELECT id, title  FROM departments WHERE real_ = '0' ORDER BY title");
	if(mysql_num_rows($r) > 0 )
	{
		while($row = mysql_fetch_array($r, MYSQL_ASSOC))
		{
			if($department == $row['id'])
			{
				$output = "<option value=\"$row[id]\" selected>";
				$output .=  $row['title'];
				$output .="</option>";
				
			}
			else
			{
				$output = "<option value=\"$row[id]\" >";
				$output .= $row['title'];
				$output .="</option>";
				
			}
			 
			
			echo $output;
		}
	}
	else
	{
		
			$output = "<option>";
			$output .="Not added yet";
			$output .="</option>"; 
			
			echo $output;
	}
}
//end of select department




//select programme
function select_programme($p)
{
	$r = mysql_query("SELECT id, title  FROM programmes WHERE real_ = '0' AND type_id = 2 AND  mode = 2 ORDER BY title");
	if(mysql_num_rows($r) > 0 )
	{
		while($row = mysql_fetch_array($r, MYSQL_ASSOC))
		{
			if($p == $row['id'])
			{
				$output = "<option value=\"$row[id]\" selected>";
				$output .=  $row['title'];
				$output .="</option>";
				
			}
			else
			{
				$output = "<option value=\"$row[id]\" >";
				$output .= $row['title'];
				$output .="</option>";
				
			}
			 
			
			echo $output;
		}
	}
	else
	{
		
			$output = "<option>";
			$output .="Not added yet";
			$output .="</option>"; 
			
			echo $output;
	}
}
//end of select programmes








	
	
	
	//Function to fetch current session
	function get_current_session($id)
	{
	    $id = 4;
		$result = mysql_query("SELECT id FROM sessions WHERE id = '$id'"); 
		$row = mysql_fetch_assoc($result);
		$id = $row['id'];
		
	return $id;
	}
	
	
	//Function to fetch  session
	function get_session_title($id)
	{
		$result = mysql_query("SELECT * FROM sessions WHERE id = '$id'"); 
		$row = mysql_fetch_assoc($result);
		$title = $row['title'];
		
	return $title;
	}
	
	
	
	



//function that filters the browser
function filter_browser($browser, $platform)
{
  
  $browser = $browser;
  $platform =$platform;
  
  
 //reject user if not firefox  
  if($browser != 'Mozilla' )
  {
	  header('location:autorize_message/'); // show authorize message
  }
  
  if($platform != 'Windows!')
  {
	   header('location:autorize_message/'); // show authorize message
	  
  }
    
  
  
} // end of browser / platform filter



//function that set asset action to switch it to last action
function set_asset($user_id , $act)
{
	$q = "SELECT * FROM assets WHERE user_id = '$user_id'";
	$r = mysql_query($q);
	
	if(mysql_num_rows($r)>0)
	{
		$query = "UPDATE assets SET action = '$act', date_added = NOW()";
		mysql_query($query);
	}
	else
	{
	
		$query = "INSERT INTO assets (id, user_id, action, date_added)
				VALUES (NULL, '$user_id', '$act', NOW())";
		mysql_query($query);
	}
}


//check if he has last action stored, get it 
function get_asset_action($user_id)
{
	$q = "SELECT * FROM assets WHERE user_id = '$user_id'";
	$r = mysql_query($q);
	
	$row = mysql_fetch_array($r, MYSQL_ASSOC);
	$at = $row['action'];
	
	return $at;
}





//function check if he jst assesed the site, if he did redirect the user base on timeout
function redirect_asset($user_id)
{
	
			
		 $at = get_asset_action($user_id); // get the last action and den delete 
		 
		 $query = "DELETE FROM assets  WHERE user_id = '$user_id'";
		 mysql_query($query);
			
			
			if( $at != '') //if $at not empty then redirect
			{
				header("Location:gateway.php?act=$at");
		 
				
			}
		 
	
	
}



//function select country
function select_country($c)
{
	$r = mysql_query("SELECT id, country_name  FROM countries  ORDER BY country_name ");
	if(mysql_num_rows($r) > 0 )
	{
		while($row = mysql_fetch_array($r, MYSQL_ASSOC))
		{
			if($c == $row['id'])
			{
				$output = "<option value=\"$row[id]\" selected>";
				$output .=  $row['country_name'];
				$output .="</option>";
				
			}
			else
			{
				$output = "<option value=\"$row[id]\" >";
				$output .= $row['country_name'];
				$output .="</option>";
				
			}
			 
			
			echo $output;
		}
	}
	else
	{
		
			$output = "<option>";
			$output .="Not added yet";
			$output .="</option>"; 
			
			echo $output;
	}
}
//end of select country_name




//function show country title
function show_country($c)
{
	$r = mysql_query("SELECT  country_name  FROM countries WHERE id = '$c'");
	$row = mysql_fetch_assoc($r);
		
    return $row['country_name'];
		
}
//end of show country_name






//function select state 
function select_state( $state)
{
	$r = mysql_query("SELECT id, title  FROM state  ORDER BY title ");
	if(mysql_num_rows($r) > 0 )
	{
		while($row = mysql_fetch_array($r, MYSQL_ASSOC))
		{
			if($state == $row['id'])
			{
				$output = "<option value=\"$row[id]\" selected>";
				$output .=  $row['title'];
				$output .="</option>";
				
			}
			else
			{
				$output = "<option value=\"$row[id]\" >";
				$output .= $row['title'];
				$output .="</option>";
				
			}
			 
			
			echo $output;
		}
	}
	else
	{
		
			$output = "<option>";
			$output .="Not added yet";
			$output .="</option>"; 
			
			echo $output;
	}
}
//end of select state


//function show state
function show_state($c)
{
	$r = mysql_query("SELECT  title  FROM state WHERE id = '$c'");
	$row = mysql_fetch_assoc($r);
		
    return $row['title'];
		
}
//end of show state



function select_lga( $lga)
{
	$r = mysql_query("SELECT id, title  FROM lga  ORDER BY title ");
	if(mysql_num_rows($r) > 0 )
	{
		while($row = mysql_fetch_array($r, MYSQL_ASSOC))
		{
			if($lga == $row['id'])
			{
				$output = "<option value=\"$row[id]\" selected>";
				$output .=  $row['title'];
				$output .="</option>";
				
			}
			else
			{
				$output = "<option value=\"$row[id]\" >";
				$output .= $row['title'];
				$output .="</option>";
				
			}
			 
			
			echo $output;
		}
	}
	else
	{
		
			$output = "<option>";
			$output .="Not added yet";
			$output .="</option>"; 
			
			echo $output;
	}
}
//end of select lga



//function show lga
function show_lga($c)
{
	$r = mysql_query("SELECT  title  FROM lga WHERE id = '$c'");
	$row = mysql_fetch_assoc($r);
		
    return $row['title'];
		
}
//end of show state






//function calendar months
function month($m)
{
	$m = $m;
	$months = range(1, 12);
	
	foreach ($months as $value) 
	{
		if($m == $value)
		{
	   		echo "<option value=\"$value\" selected>$value</option>\n";
		}
		else
		{
			echo "<option value=\"$value\">$value</option>\n";
		}
	}
	
	
}




//function calendardays
function day($d)
{
	$d = $d;
	$days = range(1, 31);
	
	foreach ($days as $value) 
	{
		if($d == $value)
		{
	   		echo "<option value=\"$value\" selected>$value</option>\n";
		}
		else
		{
			echo "<option value=\"$value\">$value</option>\n";
		}
	}
	
	
}


//get programme name
function get_programme_title($id)
{
	$result = mysql_query("SELECT title FROM programmes WHERE id = '$id'"); 
		$row = mysql_fetch_assoc($result);
		$title = $row['title'];

	return $title;
}
//

//get department name
function get_department_title($id)
{
	$result = mysql_query("SELECT title FROM departments WHERE id = '$id'"); 
	$row = mysql_fetch_assoc($result);
	$title = $row['title'];

	return $title;
}




//function that gets  department base on user id
function  get_department_id($user_id)
{	
	$result = mysql_query("SELECT department_id FROM students WHERE id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$department_id = $row['department_id'];

	return $department_id;
	
}


//function that gets programme_id base on user id
function  get_programme_id($user_id)
{	
	$result = mysql_query("SELECT programme_id FROM students WHERE id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$programme_id = $row['programme_id'];

	return $programme_id;
	
}


//function that gets programme duration
function  get_programme_duration($id)
{	
	$result = mysql_query("SELECT duration FROM programmes WHERE id = '$id'"); 
		$row = mysql_fetch_assoc($result);
		$v = $row['duration'];

	return $v;
	
}



//function that gets programme max duration
function  get_max_programme_duration($id)
{	
	$result = mysql_query("SELECT maximum_duration FROM programmes WHERE id = '$id'"); 
		$row = mysql_fetch_assoc($result);
		$v = $row['maximum_duration'];

	return $v;
	
}

//function that gets department id by user id
function  get_dept_id($user_id)
{	
	$result = mysql_query("SELECT department_id FROM students WHERE id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$course_id = $row['department_id'];

	return $course_id;
	
}

	
	//function to check if application or form sales is on // application is on 1
	function get_application_status()
	{
		$result = mysql_query("SELECT * FROM control WHERE id = 1");
		$row = mysql_fetch_assoc($result);
		$status = $row['status'];
		
		if($status == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
		
		
	}
	
	//function to check if school_resgistration status  1
	function get_school_registration_status()
	{
		$result = mysql_query("SELECT * FROM control WHERE id = '2'");
		$row = mysql_fetch_assoc($result);
		$status = $row['status'];
		
		
		if($status == 1)
		{
			return true;
		}
		else
		{
			return false;
		}	
	}
	
	//function to check if school fee payment is on 1
	function get_school_fee_status()
	{
		$result = mysql_query("SELECT * FROM control WHERE id = 3");
		$row = mysql_fetch_assoc($result);
		$status = $row['status'];
		
	
		if($status == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	









//function calendar yer
function year($y)
{
	$y = $y;
	$years = range(1980, 2017);
	
	foreach ($years as $value) 
	{
		if($y == $value)
		{
	   		echo "<option value=\"$value\" selected>$value</option>\n";
		}
		else
		{
			echo "<option value=\"$value\">$value</option>\n";
		}
	}
	
	
}





//get department id for  a programme
function get_department_id_from_programmes($p)
{
	$result = mysql_query("SELECT department_id FROM programmes WHERE id = '$p'"); 
	$row = mysql_fetch_assoc($result);
	$v = $row['department_id'];

	return $v;

	
}


//get school id from  department
function get_school_id_from_departments($p)
{
	$result = mysql_query("SELECT school_id FROM departments WHERE id = '$p'"); 
	$row = mysql_fetch_assoc($result);
	$v = $row['school_id'];

	return $v;

	
}


//get total applicants
	function get_total_num_applicants($ct, $pt,$sess)
	{
		//get total invoice
		
		
		$q = "SELECT COUNT(id) FROM applicants_nce WHERE cart_id = '$ct' AND programme_type_applied_for = '$pt' AND session = '$sess'";
		$r = mysql_query($q);
		$row = mysql_fetch_array($r,MYSQL_NUM);
		
		return $row[0];
		
	}
	
	
	//check if applicant no  already exist
	function applicant_no_exist($iid,$sess)
	{
		//check if invoice id generated exist
		$q1 = "SELECT application_number FROM applicants_nce WHERE application_number = '$iid' AND session ='$sess'";
		$r1 = mysql_query($q1);
		
		if(mysql_num_rows($r1)>0)
		{
			return true;
		}
		else
		{
			return false;
		}
	
		
	}
	


/// function taht generate app no
	function generate_application_number_for_applicants($ct,$pt,$sess)
	{
		
		$total_num = get_total_num_applicants($ct,$pt,$sess);
		
		
		//add 1 to $g_code and tek to d db
		$g_coded = $total_num + 1;
		
		
		$len_g_coded  = strlen($g_coded); //check lenght
		
		if($len_g_coded == '1')
		{
			$g_coded = '000'.$g_coded;
		}
		else if($len_g_coded == '2')
		{
			$g_coded = '00'.$g_coded;
		}
		else if($len_g_coded == '3')
		{
			$g_coded = '0'.$g_coded;
		}
		else
		{
			$g_coded = $g_coded; //add strings to invoice id
		}
		
		
		
		if(applicant_no_exist($g_coded,$sess))
		{
			$g_coded = $g_coded + 1; //if it exist add 1 to it
		
		}
		
		
		
		
		
		return 'COE/LVSP/'.$g_coded;
		
	
		
		
	}//end of generate apl no
	




?>