<?php

session_start();

include_once('../../../db_connect/db.php');
include ('../functions.php');

      $first_name = mysqli_real_escape_string($con, $_POST['f_name']);
      $middle_name = mysqli_real_escape_string($con, $_POST['m_name']);
      $last_name= mysqli_real_escape_string($con, $_POST['l_name']);
      $school_id = mysqli_real_escape_string($con, $_POST['school']);
      $programme = mysqli_real_escape_string($con, $_POST['programme']);
      $email = mysqli_real_escape_string($con, $_POST['email']);
      $phone= mysqli_real_escape_string($con, $_POST['phone']);
      $password= mysqli_real_escape_string($con, $_POST['password']);
      $p_password = md5($password);




//include('sms.php');

	//validate the programme
	  if($_POST['school'] == '0')
	  {
		   echo "select school";

	  }else if($_POST['programme'] == '0')
	  {
		   echo "select programme";

	  }




	//get department id of the programme chosen
  $dept_id = get_department_id_from_programmes($con, $programme);
  $school_code = get_school_code_by_id($con, $school_id);
  $academic_year = get_current_session($con);
  $get_institute_code = get_institute_code($con);
	$get_current_session = get_current_session($con);





	//generate application number
  $app = $school_code.'/APP/'.$get_institute_code.'/'.$get_current_session.'/';
	$appno = $app.generate_application_number_for_applicants($con, $school_id);



    //check if applicant already exist
    $chk = mysqli_query($con, "SELECT * FROM applicants WHERE email ='$email' ") or die(mysqli_error($con));
    if($chk){
			$chkregnorow=mysqli_num_rows($chk);
			if($chkregnorow > 0){
        echo 'Applicant already exist, please login';

      }else{
        //check if the query is for adding den add
   		 $q = "INSERT INTO `applicants`(`applicant_id`,`school_id`, `first_name`,`middle_name`,`last_name`, `phone_no`, `email`, `application_number`,`application_status`,
   		 	  `department_applied_for`,`programme_applied_for`,`password`, `user_status`, `date_added`)


   			 VALUES (NULL,'$school_id','$first_name','$middle_name','$last_name','$phone','$email','$appno','1','$dept_id','$programme','$p_password','1',NOW())";


   		$r = mysqli_query($con,$q) or die(mysqli_error($con));

   		if(mysqli_affected_rows($con))
   		{



   			echo 'Please login to continue your application process with this application number! &nbsp; <span style="font-weight:bold;color:red; font-size:18px;">'.$appno.'</span>';
   			//header("Location: ../index?".urlencode($info_success['success'])."&appnNSec=".$appno."&m=".md5(10000).sha1('ah')); //is - info success

   		}
   		else
   		{
   		echo  'System error! We apologize for any inconvenience.';

   			//header("Location: apply_IntoPTD.php?error=".urlencode($errors['error'])."");
   		 }

   	 }
      }











?>
