<?php

session_start();

require_once '../includes/configuration.php';




include 'includes/functions.php';


include('sms.php');


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




//check if submitted

  if($_POST['submit'] == 'APPLY')
  {
	//validate the fullname
	if(!empty($_POST['fullname']))
	{
	   $fullname = mysqli_real_escape_string($dbc, $_POST['fullname']);
	   
	   //collect first name to fill the static form
	   $_SESSION['fullname'] = $fullname;
	 
	}
	else
	{
	   $errors[] = '<p> * </p>';
	   
	   
	}
	
	
	//
//	//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 = mysqli_real_escape_string($dbc, $_POST['phone_number']);
//	$_SESSION['phone_number'] = $phone_number;
	
	//validate the  phone_number
	if((!empty($_POST['phone_number'] )) && (is_numeric($_POST['phone_number'])) && (strlen($_POST['phone_number']) == 11) )
	{
	   $phone_number = mysqli_real_escape_string($dbc, $_POST['phone_number']);
	   
	   $_SESSION['phone_number'] = $phone_number;
	 
	}
	else
	{
	   $errors['error_ph'] = 'Please insert a valid phone number';
	   header("Location: generate_epin.php?error=".urlencode($errors['error'])."");
	}
	
	
	
	
	//validate the 
	if(!empty($_POST['email'] ) && preg_match ('/^[\w.-]+@[\w.-]+\.[AZa-z]{2,6}$/', $_POST['email']))
	{
	   $email = mysqli_real_escape_string($dbc, $_POST['email']);
	   
	   $_SESSION['e'] = $email;
	 
	}
	else
	{
	   $errors['error'] = 'Please insert a valid email';
	   header("Location: generate_epin.php?error=".urlencode($errors['error'])."");
	}
	
	
	//validate the programme
	  if($_POST['programme'] == '0')
	  {
		   $_SESSION['prog'] = $_POST['programme']; //pas to the parent page for feed back
		   $errors['prog'] = '<p> * </p>';
		  
		 
	   
	  }
	  else
	  {
		  $programme= mysqli_real_escape_string($dbc, $_POST['programme']);
		  $_SESSION['prog'] = $prog;
		
	  }
	  
	  
	  
	//get department id of the programme chosen
	$dept_id = get_department_id_from_programmes($programme);



	//get school id of department id
	$school_id = get_school_id_from_departments($dept_id);
	
	
	
	
	
	//cart the category of student
	$cart = 2; //for 1 for regular  2  lvsp 
	
	
	//prgramme type appplied for
	$pt = 2; /// 1 - prence 2 - nce 3 - deree
	
	
	$sess = get_current_session($id);
	//generate application number
	$appno = generate_application_number_for_applicants($cart, $pt, $sess);
	
	

	 //collect time
	 //$time = time();
	 
	 //collect the expiry time
	 //$expiry_time = time() + 777600;
	 
	
	
	
	
	
	
	
	if(empty($errors))
	{
		//destroy session if there is no error
		unset($_SESSION['fullname']);
		unset($_SESSION['phone_number']);
		unset($_SESSION['e']);
		unset($_SESSION['prog']);
		
		
		 //check if the query is for adding den add
		 $q = "INSERT INTO `applicants_nce`(`id`, `other_names`, `phone_number`, `email`,`cart_id`, `application_number`, `programme_type_applied_for`,
		 	  `school_applied_for`,`department_applied_for`,`programme_applied_for`, `user_status`, `date_added`,session) 
			  
			  
			 VALUES (NULL,'$fullname','$phone_number','$email','$cart','$appno','$pt','$school_id','$dept_id','$programme','1',NOW(),'$sess')";
								
								
		$r = mysqli_query($dbc,$q);
		
		if(mysqli_affected_rows($dbc))
		{
			
	
			//attach sms message
			//$sms_msg = 'Dear '.$fullname.', 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 logging to continue your application process!';
			header("Location: ../applications_2/?is=".urlencode($info_success['success'])."&appnNSec=".$appno."&m=".md5(10000).sha1('ah')); //is - info success
			
		}
		else
		{
			$errors['error'] =  'System error! We apologize for any inconvenience.';
											
			header("Location: apply_IntoPTD.php?error=".urlencode($errors['error'])."");
		}
		
	}
	else
	{//stay back in generate epin form
	
	
		$errors['error'] =  'Fill the form appropriately, take note of <span class="ast">*</span>.';
											
		header("Location: apply_IntoPTD.php?error=".urlencode($errors['error'])." ".urlencode($errors['error_ph'])."");
	
	}
  
  
  }//end of if submitted





?>