<?php
class Registration extends Controller {

   function Registration(){
      parent::Controller();
	$this->load->model('MRegister');
    session_start();
	$this->lang->load('public');
		#check to see if the default lang_id exists, and if not set it
	if($this->session->userdata('lang_id')==null){
		$ld='1';
		$this->session->set_userdata('lang_id',$ld);
	}else{
		$ld=$this->session->userdata('lang_id');
	}
   }

	function index() {
		$data['liID']=$this->uri->segment(1);
		$id= $this->session->userdata('session_id');
	    $this->load->library('validation');
		
		$rules['fname'] = "trim|required|min_length[2]|xss_clean";
		$rules['lname'] = "trim|required|min_length[5]|xss_clean";
	  	$rules['email'] = "trim|required|valid_email";
		$rules['password']	= "required|matches[password_retype]";
		$rules['password_retype'] = "required";
		$rules['sex'] = "required";
		$rules['birthday']="required";
        $this->validation->set_rules($rules);
 
		$fields['fname'] = 'First Name';
		$fields['lname'] = 'Last Name';
		$fields['email'] = 'Email';
		$fields['password'] = 'Password';
		$fields['password_retype'] = 'Retype Password';
		$fields['sex'] = 'Sex';
		$fields['birthday'] = 'Birthday';
        $this->validation->set_fields($fields);
		
		
//		$data['email'] = array('name' => 'email', 'id' => 'email');
//		$data['fname'] = array('name' => 'fname', 'id' => 'fname');
//		$data['lname'] = array('name' => 'lname', 'id' => 'lname');
//		$data['password'] = array('name' => 'password', 'id' => 'password');
//		$data['password_retype'] = array('name' => 'password_retype', 'id' => 'password_retype');
//		$data['sex'] = array('name' => 'sex', 'id' => 'sex');
//		$data['birthday'] = array('name' => 'birthday', 'id' => 'birthday');
			
		$data['title'] = "Sing up to myFriendsOnBoard | myFOB";
		$data['page_title'] = $this->lang->line('sing_up');
  		$data['header'] = 'header';
  		$data['nav'] = 'nav';
  		$data['main_sr'] = 'sing_up';
  		$data['footer'] = 'footer';
	
		$this->load->view('template_master_sr', $data);
	}

    function checkRegister(){
		$this->validation->set_error_delimiters('<div class="warning">', '</div>');
		$data['liID']=$this->uri->segment(1);
		$id= $this->session->userdata('session_id');
		
        $this->load->library('validation');
		
		$rules['fname'] = "trim|required|min_length[2]|xss_clean";
		$rules['lname'] = "trim|required|min_length[5]|xss_clean";
	  	$rules['email'] = "trim|required|valid_email";
		$rules['password']	= "required|matches[password_retype]";
		$rules['password_retype'] = "required";
		$rules['sex'] = "required";
		$rules['birthday']="required";
        $this->validation->set_rules($rules);
 
		$fields['fname'] = 'First Name';
		$fields['lname'] = 'Last Name';
		$fields['email'] = 'Email';
		$fields['password'] = 'Password';
		$fields['password_retype'] = 'Retype Password';
		$fields['sex'] = 'Sex';
		$fields['birthday'] = 'Birthday';
        $this->validation->set_fields($fields);
 
        if ($this->validation->run()) {
            $email= $this->validation->email;
			$userBirth=$this->validation->birthday;
			//check to see if the email is within a real domain
			if(!$this->MRegister->validEmail($email)){
				$this->session->set_flashdata('message','You must enter a valid email, in order to continue!');
				redirect('registration','refresh');
			}
			//check to see if the user is already register
			if($this->MRegister->checkUserEmail($email,"users")){
				$this->session->set_flashdata('message','The e-mail you entered is already registered to our database');
				redirect('page/login','refresh');
			}
			//check to see if the user is under 18
			$age = $this->MRegister->birthday($userBirth);
			if(($age<18) || ($age<0)){
				$this->session->set_flashdata('message','You must be over 18th to join. Have you read the Terms &amp; Contitions?');
				redirect('registration','refresh');
			}
			
			//if all the validation rules passed then redirect the user to the success page, and
			//make an insertion to tha database. Also sends to the user and admin the verification email
			$this->session->set_flashdata('message', 'Welcome to myFOB!!! Have fun!!!');
			$details=$this->MRegister->insertUser();
			$this->MUsers->UserLogin($details['id']);
			$ses_user_id = $details['id'];
			$this->session->set_userdata("ses_user_id",$ses_user_id);
			//$_SESSION['userid'] = $details['id'];
			redirect('user','refresh');
        } else {
			$data['email'] = array('name' => 'email', 'id' => 'email');
			$data['fname'] = array('name' => 'fname', 'id' => 'fname');
			$data['lname'] = array('name' => 'lname', 'id' => 'lname');
			$data['email'] = array('name' => 'email', 'id' => 'email');
			$data['sex'] = array('name' => 'sex', 'id' => 'sex');
			$data['birthday'] = array('name' => 'birthday', 'id' => 'birthday');
			
			$data['title'] = "Sing up to myFriendsOnBoard | myFOB";
			$data['page_title'] = $this->lang->line('sing_up');
  			$data['header'] = 'header';
  			$data['nav'] = 'nav';
  			$data['main_sr'] = 'sing_up';
  			$data['footer'] = 'footer';
	
			$this->load->view('template_master_sr', $data);
        }
 
        //$output = str_replace("\r", "", $output);
        //$output = str_replace("\n", "", $output);
 
        //redirect('user','refresh');
    }



		
}
?>