<?php
ob_start();

	//starting session if not already started
	if (!session_start()) {
	    session_start();
	}

	//when user already attemped registration
	if ($_SESSION['reg_app'] == 1) {
		$errors['error'] = 'System error! Please re-fill and submit the form.'.PHP_EOL;
	 	header("Location: apply_IntoSOH_new.php?error=".urlencode($errors['error'])."");
   		exit();
	}
	//initializing attempt
	$_SESSION['reg_app'] = 1;

include_once 'includes/functions.php';


//include_once('sms.php');


$errors = array();
$info = array();
$info_success = array();




//check if submitted

if(isset($_POST['submit']) == 'APPLY' && isset($_POST['token']))
{
	//validate the first_name
	if(!empty($_POST['fname']))
	{
	   $fname = clean_data( $_POST['fname']);

	   //collect first name to fill the static form
	   $_SESSION['fname'] = $fname;

	}
	else
	{
	   $errors[] = ' * ';

	}

	//validate the middle name
	if(isset($_POST['mid_name']) && strlen($_POST['mid_name']) > 0)
	{

	   $mid_name = clean_data( $_POST['mid_name']);

		if (strlen($mid_name) < 3){
			$errors['error'] =  'Middle name should be more than two(2) letters.'.PHP_EOL;
				//.$prog .' '.$dept_id;

			header("Location: apply_IntoSOH_new.php?error=".urlencode($errors['error'])."");
			exit();

		}


	   //collect first name to fill the static form
	   $_SESSION['mid_name'] = $mid_name;


	}
	else
	{
	    $mid_name = '';

	}


	//validate the first_name
	if(!empty($_POST['lname']))
	{
	   $lname = clean_data( $_POST['lname']);

	   //collect first name to fill the static form
	   $_SESSION['lname'] = $lname;

	}
	else
	{
	   $errors[] = ' * ';

	}

	//
	//	//eliminate every char except 0-9
	//	$justNums = preg_replace("/[^0-9]/", '', $string);
	//
	//	//eliminate leading 1 if its there
	//	if (strlen($justNums) == 11) $justNums = preg_replace("/^1/", '',$justNums);
	//
	//	//if we have 10 digits left, it's probably valid.
	//	if (strlen($justNums) == 10) $isPhoneNum = true;
	//
	//
	//	 if (preg_match("/^\+(09|63)-?[\d]{9}$/m", $mobile)) {
	//            // valid mobile number
	//            echo $mobile;
	//     }
	//
	//
	//	//collect the phone_number
	//	$phone_number = clean_data( $_POST['phone_number']);
	//	$_SESSION['phone_number'] = $phone_number;

	//validate the
	//$pre_no = '';
	$jamb_no = '';
	//$jamb_score = '';

	//validate the Programme type
  if($_POST['programme_type'] =='0' || $_POST['programme_type'] =='' )
  {

	   $_SESSION['pt'] =  clean_data($_POST['programme_type']);
	   $errors['error'] = ' Please choose one of the Mode of Entry'.PHP_EOL;
	 	header("Location: apply_IntoSOH_new.php?error=".urlencode($errors['error'])."");
	 	unset($_SESSION['pt']);
   		exit();
  }
  else
  {


  	$pt =  clean_data($_POST['programme_type']);
  	//when transiting
  	if ($pt == '1' || $pt == '2' )
		{


  		//validate PRE-NCE Reg. Number
  		isset($_POST['jamb_no']) ? $jamb_no = clean_data($_POST['jamb_no']) : $jamb_no = '' ;   //pre_no

  		$_SESSION['jamb_no'] =  $jamb_no;

  		//when empty
  		if($jamb_no == '')
			{
  			$errors['error'] = 'Please enter DE Reg. Number '.PHP_EOL;
		 		header("Location: apply_IntoSOH_new.php?error=".urlencode($errors['error'])."");
		 		unset($_SESSION['jamb_no']);
	   		exit();
  		}


  	//validating UTME Number and Score
  	}

		// elseif ($pt == '3' ){
  	// 	//
  	// 	isset($_POST['jamb_no']) ? $jamb_no = clean_data($_POST['jamb_no']) : $jamb_no = '' ;
		//
  	// 	$_SESSION['jamb_no'] =  $jamb_no;
		//
		// isset($_POST['jamb_score']) ? $jamb_score = clean_data($_POST['jamb_score']) : $jamb_score = '' ;
		//
  	// 	$_SESSION['jamb_score'] =  $jamb_score;
		//
  	// 	//when UTME NO is empty
  	// 	if($jamb_no == '' && $pt == '3'){
		//
  	// 		$errors['error'] = 'Please enter UTME Reg. Number '.PHP_EOL;
		//  	header("Location: apply_IntoSOH_new.php?error=".urlencode($errors['error'])."");
		//  	unset($_SESSION['jamb_no']);
	  //  		exit();
  	// 	}
  	// 	//when UTME Score is empty
  		// if($jamb_score == '' && $pt == '3'){
			//
  		// 	$errors['error'] = 'Please enter UTME Reg. Number '.PHP_EOL;
		 	// header("Location: apply_IntoSOH_new.php?error=".urlencode($errors['error'])."");
		 	// unset($_SESSION['jamb_score']);
	   	// 	exit();
  		// }
			//
	   	// //UTME SCORE MUST BE NUMBER
  		// if(!is_numeric($jamb_score)){
			//
  		// 	$errors['error'] = 'UTME Score Should only contain Numbers '.PHP_EOL;
		 	// header("Location: apply_IntoSOH_new.php?error=".urlencode($errors['error'])."");
		 	// unset($_SESSION['jamb_score']);
	   	// 	exit();
  		// }

  		//compare score
  		// if($jamb_score < 100 ){
			//
  		// 	$errors['error'] = 'Sorry, Your UTME Score is below the Minimum Required Score. '.PHP_EOL;
		 	// header("Location: apply_IntoSOH_new.php?error=".urlencode($errors['error'])."");
		 	// unset($_SESSION['jamb_score']);
	   	// 	exit();
  		// }

  //	}


	  $prg_type = db_escape($pt);
	  $_SESSION['pt'] = $prg_type;


  }


	/*echo $pt;
	exit();*/

 // validate the programme
	// if($_POST['programme'] == '')
	// {
 	// 	$_SESSION['prog'] = $_POST['programme']; //pas to the parent page for feed back
	// 	$errors['prog'] = ' Please choose one of the Courses '.PHP_EOL;
	// 	 header("Location: apply_IntoSOH_new.php?error=".urlencode($errors['prog'])."");
	// 	 unset($_SESSION['prog']);
	// 	 exit();
	// }
	// else
	// {
	// 	$prog = db_escape( $_POST['programme']);
	// 	$_SESSION['prog'] = $prog;
	//
	// }
	//





	//get department id of the courses chosen
	//$dept_id = get_department_id_from_courses($prog);
	//$entry_mode = get_mode_of_entry_from_courses($prog);

	//get entry mode code
	$pt_code = get_pt_code_prg_type($pt);

	///////$dept_id = get_department_id_from_programmes($prog );

	//get school id of department id
	///////$school_id = get_school_id_from_departments($dept_id);

	/*echo $prog . '<br>';
	echo $dept_id;

	exit();
	/*
		$app = 'COE/TR/0001';
	  echo ;
	*/
	 //current active session
	//$ses = get_current_session($id= 1);
	
	$sess = db_escape($ses = 2);

	//generate application number
	$appno = generate_application_number_for_applicants($sess);


	$appno = $pt_code.$appno;



	 //collect time
	 //$time = time();

	 //collect the expiry time
	 //$expiry_time = time() + 777600;


		if(empty($errors))
		{
			//destroy session if there is no error
			unset($_SESSION['fname']);
			unset($_SESSION['mid_name']);
			unset($_SESSION['lname']);
			unset($_SESSION['pt']);
			//unset($_SESSION['pre_no']);
			unset($_SESSION['jamb_no']);
			//unset($_SESSION['jamb_score']);
			//unset($_SESSION['prog']);


			//sanitizing before using with any query
			$fname = db_escape(strtoupper($fname));
			$mid_name = db_escape(strtoupper($mid_name));
			$lname = db_escape(strtoupper($lname));
			//$entry_mode = db_escape($entry_mode);
			$app_num = db_escape($appno);
			//$pre_no = db_escape($pre_no);
			$jamb_no = db_escape($jamb_no);
			//$jamb_score = db_escape($jamb_score);
			//$dept_id = db_escape($dept_id);



			//school_applied_for, department_applied_for, programme_applied_for,
			// mode_of_entry, prence_no,	jamb_score,


			$q = "INSERT INTO applicants_nce_(
			 		application_number, session, first_name, middle_name,
			 		other_names, programme_type_applied_for,jamb_no, user_status,date_added)

				 VALUES ( $app_num, $sess, $fname, $mid_name, $lname,  $pt,  $jamb_no,  1, NOW())";

//$school_id, $ptaf, $dept_id,$prog,$pre_no,$jamb_score,

			//inserting and getting last inserted id by this query
			$r = mysqli_query(dbc(), $q);
			//$id = mysqli_insert_id(dbc());
			//;
			if ($r){

				//echo $ls_id;
				//update pin table by adding user id to used pin
				//$r2 = update_user_id_in_pin($ls_id, $mid_name, $lname);
				//attach sms message
				//$sms_msg = 'Dear '.$first_name.', your Application No. is '.$tid.'.  Please login with valid credentials to continue your application process.-';
				//sms($phone_number,$sms_msg.' '); //send sms to user
				$info_success['success'] = 'Please login to continue your application process! '.PHP_EOL;
				header("Location: ../b_/?is=".urlencode($info_success['success'])."&appnNSec=".$appno."&m=".md5(rand(1,10000)).sha1('ah')); //is - info success


			}
			else
			{
				$errors['error'] =  'System error! We apologize for any inconvenience.'.PHP_EOL;
				//.$prog .' '.$dept_id;

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

			}


		}
		else
		{//stay back in generate epin form


			$errors['error'] =  'Fill the form appropriately, take note of *.'.PHP_EOL;

			header("Location: apply_IntoSOH_new.php?error=".urlencode($errors['error'])." ".urlencode($errors['error_ph'])."");

		}


	}


	else{

		$errors['error'] =  'Fill the form appropriately, and submit  again.'.PHP_EOL;
		header("Location: apply_IntoSOH_new.php?error=".urlencode($errors['error'])." ".urlencode($errors['error_ph'])."");






	}//end of if submitted





?>
