<?php



/**

* @version 2.7.19

* @package Joomla 1.5

* @subpackage DT Register

* @copyright Copyright (C) 2006 DTH Development

* @copyright contact dthdev@dthdevelopment.com

* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL

*/



class DtregisterControllerEvent extends DtrController {

    

	var $name ='event';

	

	function __construct($config = array()){

		  parent::__construct($config);

		  $this->view = & $this->getView( 'event', 'html' );

		  $this->view->setModel($this->getModel('event'),true);

		  $this->view->setModel($this->getModel( 'location' ));

		  $this->view->setModel($this->getModel( 'user' ));

		  $this->view->setModel($this->getModel( 'member' ));

		  $this->view->setModel($this->getModel( 'field' ));

		  $this->view->setModel($this->getModel( 'fieldtype' ));

		  $this->view->setModel($this->getModel( 'discountcode' ),false);

		  $this->view->setModel($this->getModel( 'file' ),false);

		  $this->view->setModel($this->getModel( 'category' ),false);

		  $this->view->setModel($this->getModel( 'section' ),false);

		  $this->registerTask( 'new', 'add' );

		  $this->registerTask( 'category', 'events' );

		  $this->registerDefaultTask("events");

		  

	}

	function checkpermission(){

		

		true;	

	}

	function events(){

		global $disable_cart;

		/*if(DT_Session::get('Setting')){

			DT_Session::set('Setting');

			DT_Session::set('Setting.process','register');

		} else {

			DT_Session::set('Setting');

		}*/

	    $this->view->setModel($this->getModel('category'));

	    $this->view->setLayout('list');

		$this->view->display();

	}

	

	function cut_off(){

	   

	   $this->display();

	   	

	}

	

	function check_waiting() {

		ob_clean();

		$eventId = JRequest::getVar('eventId',DT_Session::get('Setting.eventId'));

	   

	    $event = & DtrTable::getInstance('Event','Table');

	    $event->load($eventId );

		$event->registered = $event->getTotalregistered($event->slabId);

		$registering = $event->count_registering();

		$total = $event->registered + $registering; 

		if($event->max_registrations == "0" || $event->max_registrations > $total) {

			echo "regular";

		} else if($event->max_registrations <= $total && $event->waiting_list > 0) {

			echo "waiting";

		} else {

			echo "full";

		}

		die;

		echo $event->is_waiting_check()?"false":"true";

		die;

		

	}

	function register(){

	   

	   global $mainframe,$xhtml,$Itemid,$now;

	   global $disable_cart;

		

	    if(DT_Session::get('Setting')){

			DT_Session::set('Setting');

			DT_Session::set('Setting.process','register');

		} else {

			DT_Session::set('Setting');

		}

		if($disable_cart ==0 ) {

			DT_Session::clear('register.User');

		}

	   DT_Session::set('Setting.reg_status','start');

	   $type = JRequest::getVar('type');

	   $eventId = JRequest::getVar('eventId',0);

	   $tEvent = $this->getModel('event')->table;

	   $tUser = $this->getModel('user')->table;

	   $tEvent->load($eventId);

	   $tEvent->is_registerable($tUser);

	   if($type=="options"){
sleep_till_session_written();
	       $mainframe->redirect(JRoute::_("index.php?option=com_dtregister&controller=event&task=options&Itemid=".$Itemid."&eventId=".$eventId,$xhtml));
exit ;
	   }else{
sleep_till_session_written();
	       $mainframe->redirect(JRoute::_("index.php?option=com_dtregister&controller=event&task=options&Itemid=".$Itemid."&eventId=".$eventId,$xhtml));
exit ;
	   }

	   $event = $this->getModel('event')->table;

	   $event->load($eventId);

	   $this->view->assign('header_eventId',$eventId);



	}

	

	function options(){

	  

	  global $mainframe,$Itemid,$xhtml;

	  

	  $eventId = JRequest::getVar('eventId',0);

	  DT_Session::set('Setting.eventId', $eventId);

	  DT_Session::set('Setting.process', 'register');

	  

	  if(false === DT_Session::get('Setting.eventId') || DT_Session::get('Setting.eventId') == 0){

		   

		   $mainframe->redirect("index.php?option=com_dtregister&Itemid=".$Itemid);

	  }

	  

	 

	  $tEvent = $this->getModel('event')->table;

	  

	  $tEvent->load($eventId);

	  $my = &JFactory::getUser();

	

	  $tUser = $this->getModel('user')->table;

		

	  $tEvent->is_registerable($tUser);

	

	   DT_Session::set('Setting.User.eventId',$eventId);

	   

	   $registered = $tEvent->getTotalregistered($tEvent->slabId);



	   if (isset($tEvent->max_registrations) && $tEvent->max_registrations > 0) {

		if($tEvent->max_registrations <= $registered){

		   	if($tEvent->waiting_list){

			     DT_Session::set('Setting.User.status',-2);

			}

		}

	    }



	   if($tEvent->registration_type=="individual"){

		     if(isset($_REQUEST['back']) && DT_Session::get('Setting.Editindex')=== false){

			    $mainframe->redirect('index.php?option=com_dtregister&Itemid='.$Itemid);

			 }else{
sleep_till_session_written();
		        $mainframe->redirect('index.php?option=com_dtregister&Itemid='.$Itemid.'&eventId='.$eventId.'&controller=event&task=individualRegister');

			 }

	   }elseif($tEvent->registration_type=="group"){

		   
sleep_till_session_written();
 $mainframe->redirect('index.php?option=com_dtregister&Itemid='.$Itemid.'&eventId='.$eventId.'&controller=event&task=groupNum');

	   }

	   $this->view->assign('header_eventId',$eventId);

	   $this->view->assign('tEvent',$tEvent);

	   $this->view->setLayout('options');

	   $this->view->display();

	    

	}

	

	function confirm(){

		global $mainframe,$Itemid,$xhtml,$member_confirmnum,$partial_payment_group_reg;

		

		if(false === DT_Session::get('Setting.eventId') || DT_Session::get('Setting.eventId') == 0){

		   

		   //$mainframe->redirect("index.php?option=com_dtregister&Itemid=".$Itemid);

	   }

	   global $Itemid,$mainframe,$xhtml_url,$offline_zero_credit,$paymentmethod;

	   $this->view->setModel($this->getModel( 'field' ));

	   $this->view->setModel($this->getModel( 'fieldtype' ));

	  	  

	   $step = JRequest::getVar('step',0);

	   $my = &JFactory::getUser();

	   

	   $eventId = JRequest::getVar('eventId',DT_Session::get('Setting.eventId'));

	   

	   $event = & DtrTable::getInstance('Event','Table');

	   $event->load($eventId );

	   

	   if($step ==="confirm"){

          

			// pr(DT_Session::get('register.User')); prd('exit here');

		   $memtot = DT_Session::get('Setting.User.memtot');

		   $paying_amount = JRequest::getVar('paying_amount',0);

           if($event->partial_payment > 0 && $partial_payment_group_reg == 'eachmember') {

		   		

				if(DT_Session::get('Setting.User.type')=='G'){ 



					if( $memtot > 1 ){

						

						$paying_amount *= $memtot;

						

					}

				

				}

				

		   }

		   $TableUser =& DtrTable::getInstance('Duser','Table');

	   $TableUser->create(DT_Session::get('Setting.User'));

	   $feeObj = new DT_Fee($event,$TableUser);

	   $feeObj->getFee($my->id);

	  

	       DT_Session::set('Setting.User.fee',DTrCommon::objectToArray($feeObj->getObjectdata()));



		   DT_Session::set('Setting.User.fee.paid_amount',$paying_amount);

		   DT_Session::set('Setting.User.fee.paying_amount',$paying_amount); 



		   if($my->id){

			    DT_Session::set('Setting.User.user_id', $my->id);  

		   }

		   $tableUser = $this->getModel('user')->table;

		   if(DT_Session::get('Setting.User.type')=='G' && $member_confirmnum){

			  

			   $members = DT_Session::get('Setting.User.members');

			   $i = 1;

			   if($members)

			   foreach($members as $key => $member){

				   $confirmnum = $tableUser->generateconfirmNum();

				  

				   DT_Session::set('Setting.User.members.'.$key.'.confirmnum',$confirmnum );

				   if($i == 1) {

					  DT_Session::set('Setting.User.confirmNum',$confirmnum);

				   }

				   $i++;

			   }

			   

		   } else {

				  DT_Session::set('Setting.User.confirmNum',$tableUser->generateconfirmNum());

		   }

		   $event->add_registering($memtot);

		   $status = DT_Session::get('Setting.User.status');

		   $cart = new DT_Cart();

		   $cart_amount = $cart->getAmount();

		   

		   if($status == -2){ // free or waiting $paying_amount <= 0 

			   if($status == -2){

				   DT_Session::set('Setting.User.fee.paid_amount',0);

		           DT_Session::set('Setting.User.fee.paying_amount',0);

				   $task = "waiting_msg";

			   }else{

				   $task = "freethanks";

			   }

			   

			    if($cart_amount > 0 || ($offline_zero_credit && in_array('offline_payment',$paymentmethod ))) {

					$users = DT_Session::get('register.User');

					$users[] = DT_Session::get('Setting.User');

					DT_Session::set('register.User',$users);

					DT_Session::clear('Setting.User');
sleep_till_session_written();
			   		$mainframe->redirect("index.php?option=com_dtregister&controller=event&task=viewCart&Itemid=".$Itemid);

			   } else {

			   		$tableUser->register(DT_Session::get('Setting.User'));

			   

			   		DT_Session::set('Setting.User.userId',$tableUser->userId);
sleep_till_session_written();
			   		$mainframe->redirect("index.php?option=com_dtregister&controller=message&task=$task&Itemid=".$Itemid);

			   }

		   }else{

			    $users = DT_Session::get('register.User');

			   

			   

			   $index = $_REQUEST['Index'];

			   DT_Session::set('Setting.User.confirmed',1);

			   DT_Session::set('Setting.User.Index',$index) ;

			   $users[$index] = DT_Session::get('Setting.User');

			   DT_Session::set('register.User',$users);

			  // pr(count($users));

			   //prd($users);

			   DT_Session::clear('Setting.User');

			   DT_Session::set('Setting.reg_status','confirmed') ;

			   sleep_till_session_written();

			   $mainframe->redirect("index.php?option=com_dtregister&controller=event&task=viewCart&Itemid=".$Itemid);

		   }



	   }

	   

	  /* if(isset($_REQUEST['Field'])){

	     // pr('register.User.'.$userIndex.'.fields');

	     DT_Session::set('Setting.User.fields', $_REQUEST['Field']);

		 if(isset($_REQUEST['username'])){

		   DT_Session::set('Setting.User.username', $_REQUEST['username']);

		   DT_Session::set('Setting.User.password', $_REQUEST['password']);

		 }

		  if(DT_Session::get('Setting.User.fee')) {

		 	DT_Session::clear('Setting.User.fee');

			$data = DT_Session::get('register.User');

	        $data = end($data);

			DT_Session::set('Setting.User.memtot',$data['memtot']);

			DT_Session::set('Setting.User.type',$data['type']);

		 }

		if(isset($_REQUEST['discount_code']) && $_REQUEST['discount_code'] != ""){

			

			$discount_code_id = $event->validate_code($_REQUEST['discount_code']); 

			if($discount_code_id !== false){

			   DT_Session::set('Setting.User.discount_code_id', $discount_code_id );

			}else{

			   DT_Session::set('Setting.User.discount_code_id', false );

			  $this->view->assign('discountCodeError',$event->TableEventdiscountcode->TableDiscountcode->error );

			  

			}

			

		}

		  $userdata = DT_Session::get('Setting.User');

	   }else {

		 DT_Session::set('Setting.User',DT_Session::get('register.User.0'));

	     $userdata = DT_Session::get('register.User.0');

		 

	   }*/

	   if(DT_Session::get('Setting.User') === false) {

	      $this->load_from_last_reg();

	   } 

	   

	   $userdata = DT_Session::get('Setting.User');

	  

	   

	   DT_Session::set('Setting.User.eventId', $eventId);

	   $fieldtype =$this->getModel( 'fieldtype' );

	   $viewMemFields = "";

	   if(DT_Session::get('Setting.User.type')=='G'){

	      $back = JRoute::_("index.php?option=com_dtregister&Itemid=".$Itemid."&eventId=".$eventId."&controller=event&task=billinginfo&back=1", $xhtml_url);

		  $type = 'B';

		   $members = DT_Session::get('Setting.User.members');

		   if($members)

		   foreach($members as $key => $member){

		      $viewMemFields .= "<tr><td colspan='2'>&nbsp;</td></tr><tr><td colspan='3'><u>".JText::_( 'DT_MEMBER' ).($key+1)." </u></td></tr>";

			  if(DT_Session::get('Setting.User.members.'.$key) != false){ 

			  	  $member_data = DT_Session::get('Setting.User.members.'.$key);

			  } else {

			  	  $member_data = DT_Session::get('register.User.0.members.'.$key);

			  }

		      $viewMemFields .= $event->viewFields('M',$member_data,false,'frmcart',false);

		   }

	   }else{

	      $back = JRoute::_("index.php?option=com_dtregister&Itemid=".$Itemid."&eventId=".$eventId."&controller=event&task=individualRegister&back=1", $xhtml_url);

		  $type = 'I';

	   }

	   

	   $TableUser =& DtrTable::getInstance('Duser','Table');

	   $TableUser->create($userdata);

	   $feeObj = new DT_Fee($event,$TableUser);

	   $feeObj->getFee($my->id);

	   

	   if($event->is_waiting()){

	      

		  DT_Session::set('Setting.User.status',-2);

		  $this->view->assign('paying_amount',0);

		  

	   }else{

		     

		   $this->view->assign('paying_amount',$feeObj->paid_fee);

	   }

	   

	   DT_Session::set('Setting.User.fee',DTrCommon::objectToArray($feeObj->getObjectdata()));

	  

	   $this->view->assign('currentfee',$feeObj->currentfee);

	   $this->view->assign('back',$back);

	   $this->view->assign('eventId',$eventId);

	   $this->view->assign('header_eventId',$eventId);

	   //$event->load($eventId);

	  

	   $this->view->assign( 'viewFields' ,$event->viewFields($type,DT_Session::get('Setting.User'),false,'frmcart',false));

	   $this->view->assign( 'viewMemFields',$viewMemFields);

	   $this->view->assign( 'event',$event);

	   $this->view->setLayout('confirm');

	    if(isset($_REQUEST['Index'])) {

	       $this->view->assign('Index',$_REQUEST['Index']);

	   } else {

	   	   $this->set_User_index();

	   }

	   $this->view->display();

	  // $db = &JFactory::getDBO();

	  // $db->profileData = array();



	}

	

	function confirmsave() {

	   

	    global $mainframe,$Itemid,$xhtml,$member_confirmnum,$partial_payment_group_reg;

		

		if(false === DT_Session::get('Setting.eventId') || DT_Session::get('Setting.eventId') == 0){

		   

		   $mainframe->redirect("index.php?option=com_dtregister&Itemid=".$Itemid);

	   }

	   global $Itemid,$mainframe,$xhtml_url,$offline_zero_credit,$paymentmethod;

	   $this->view->setModel($this->getModel( 'field' ));

	   $this->view->setModel($this->getModel( 'fieldtype' ));

	  	  

	   $step = JRequest::getVar('step',0);

	   $my = &JFactory::getUser();

	   

	   $eventId = JRequest::getVar('eventId',DT_Session::get('Setting.eventId'));

	   

	   $event = & DtrTable::getInstance('Event','Table');

	   $event->load($eventId );

	   

	   if(isset($_REQUEST['Field'])){

	     // pr('register.User.'.$userIndex.'.fields');

	     DT_Session::set('Setting.User.fields', $_REQUEST['Field']);

		 if(isset($_REQUEST['username'])){

		   DT_Session::set('Setting.User.username', $_REQUEST['username']);

		   DT_Session::set('Setting.User.password', $_REQUEST['password']);

		 }

		  if(DT_Session::get('Setting.User.fee')) {

		 	DT_Session::clear('Setting.User.fee');

			$data = DT_Session::get('register.User');

			if($data !== false) {

	        $data = end($data);

			if(DT_Session::get('Setting.User.memtot') ==  false && isset($data['memtot']) && $data['memtot'] != "" ) {

			   DT_Session::set('Setting.User.memtot',$data['memtot']);

			}

			if(DT_Session::get('Setting.User.type') ==  false) {

			   DT_Session::set('Setting.User.type',$data['type']);

			}

			if(DT_Session::get('Setting.User.eventId') ==  false) {

			

				DT_Session::set('Setting.User.eventId',$data['eventId']);

		   }

		 }

		 }

		if(isset($_REQUEST['discount_code']) && $_REQUEST['discount_code'] != ""){

			

			$discount_code_id = $event->validate_code($_REQUEST['discount_code']); 

			if($discount_code_id !== false){

			   DT_Session::set('Setting.User.discount_code_id', $discount_code_id );

			}else{

			   DT_Session::set('Setting.User.discount_code_id', false );

			  $this->view->assign('discountCodeError',$event->TableEventdiscountcode->TableDiscountcode->error );

			  

			}

			

		}

		  $userdata = DT_Session::get('Setting.User');

	   }

	    if(isset($_REQUEST['mailchimp_register'])) {

	      DT_Session::set('Setting.User.mailchimp_register', $_REQUEST['mailchimp_register']);

	   }

	   sleep_till_session_written();

	   $mainframe->redirect('index.php?option=com_dtregister&controller=event&task=confirm&Itemid='.$Itemid);

	   

	}

	

	function viewCart(){

		

		global $mainframe,$Itemid,$disable_cart,$minimum_cart_event;

		global $xhtml;

		if(false === DT_Session::get('Setting.eventId') || DT_Session::get('Setting.eventId') == 0){

		   

		   $mainframe->redirect("index.php?option=com_dtregister&Itemid=".$Itemid);

	   }

	   global $mainframe,$Itemid,$disable_cart;

	    if ($disable_cart == 0) {

		    if(isset($_REQUEST['back'])) {
sleep_till_session_written();
			   $mainframe->redirect('index.php?option=com_dtregister&controller=event&task=confirm&Itemid='.$Itemid);

			} else {
sleep_till_session_written();
			   $mainframe->redirect('index.php?option=com_dtregister&controller=payment&task=methods&Itemid='.$Itemid);

			}

			

	   }

	   DT_Session::clear('Setting.cart'); 

	   $this->view->setLayout('viewcart');

	   $this->view->display();

	   

	}

	

	

	function memberdata(){

	   global $mainframe,$Itemid,$xhtml;

		if(false === DT_Session::get('Setting.eventId') || DT_Session::get('Setting.eventId') == 0){

		   

		   $mainframe->redirect("index.php?option=com_dtregister&Itemid=".$Itemid);

	   }

	   if(DT_Session::get('Setting.User.type') === false) {

	   		DT_Session::set('Setting.User.type','G');

	   }

	   $this->view->setModel($this->getModel( 'field' ));

	   $this->view->setModel($this->getModel( 'fieldtype' ));

	   $field = $this->getModel( 'field' )->table;

	   $eventId = JRequest::getVar('eventId',DT_Session::get('Setting.eventId'));

	   $fieldtype =$this->getModel( 'fieldtype' );

	   $event = $this->getModel('event')->table;

	   $event->load($eventId);

	   

	   if(isset($_POST['member_data'])){

		   DT_Session::set('Setting.Member.fields', $_REQUEST['Field']);

	      $members = DT_Session::get('Setting.User.members')?DT_Session::get('Setting.User.members'):array();

		$index = $_REQUEST['Index'];

		DT_Session::set('Setting.Member.Index',$index);

		DT_Session::clear('Setting.Member.Add');

		$members[$index] = DT_Session::get('Setting.Member');

		

		DT_Session::set('Setting.User.members',$members);

		DT_Session::clear('Setting.Member');

	     if($index < (DT_Session::get('Setting.User.memtot')-1)) {

	     	 $task = 'memberdata';

		 } else {

		 		$task = 'billinginfo'; 

		 }

		 global $xhtml_url;

		 // prd(JRoute::_('index.php?option=com_dtregister&controller=event&task='.$task.'&Itemid='.$Itemid,$xhtml_url));
sleep_till_session_written();
		    $mainframe->redirect(JRoute::_('index.php?option=com_dtregister&eventid='.$eventId.'&controller=event&task='.$task.'&Itemid='.$Itemid,$xhtml_url));

	     

	   }

	   

	  if(DT_Session::get('Setting.User.memtot')) {

		   $memtot = DT_Session::get('Setting.User.memtot');

	      

	   } 

	  

	   if(DT_Session::get('Setting.Member.Index') !== false) {

	   		

			$index = DT_Session::get('Setting.Member.Index') ;

			

	   } else {

		      if(DT_Session::get('Setting.User.members') != false) {

			     $index = count( DT_Session::get('Setting.User.members'));

			  }  else {

		      	 $index = 0 ;

			  }

			  

	   		

			

	   }

	   

	   if($index > 1) {

	     $back =  JRoute::_("index.php?option=com_dtregister&Itemid=".$Itemid."&eventId=".$eventId."&controller=event&task=editlastmember");

	   } else {

	   	  $back = JRoute::_("index.php?option=com_dtregister&Itemid=".$Itemid."&eventId=".$eventId."&controller=event&task=options&back=1");

	   }

	  

	   $this->view->assign('back',$back);

	  

	   $this->view->assign('memberIndex',$index);

	   $memtot = DT_Session::get('Setting.User.memtot');

	   $this->view->assign( 'form' ,$event->form('M',(array)DT_Session::get('Setting.Member'),false,'frmcart',false,$memtot));

	   $this->view->setLayout('memberdata');

	   $this->view->assign('header_eventId',$eventId);

	   $this->view->assign('tEvent',$event);

	   $this->view->display();



	}

	function add_member() {

		global $mainframe,$Itemid;

		DT_Session::clear('Setting.Member' );

		//DT_Session::set('Setting.Member.Index' ,(count(DT_Session::get('Setting.User.members')) + 1) );

		DT_Session::set('Setting.Member.Add','yes' );
sleep_till_session_written();
		$mainframe->redirect('index.php?option=com_dtregister&controller=event&task=memberdata&Itemid='.$Itemid);

	}

	

	function member_cancel() {

		

		global $mainframe,$Itemid;

		DT_Session::clear('Setting.Member' );

		$memtot = count(DT_Session::get('Setting.User.members'));

		DT_Session::set('Setting.User.memtot',$memtot);

		//DT_Session::set('Setting.Member.Index' ,(count(DT_Session::get('Setting.User.members')) + 1) );
sleep_till_session_written();
		$mainframe->redirect('index.php?option=com_dtregister&controller=event&task=billinginfo&Itemid='.$Itemid);

		

	}

	function billinginfo(){

	   global $mainframe,$Itemid,$xhtml;

		if(false === DT_Session::get('Setting.eventId') || DT_Session::get('Setting.eventId') == 0){

		   

		   $mainframe->redirect("index.php?option=com_dtregister&Itemid=".$Itemid);

	   }

	   

	   $eventId = JRequest::getVar('eventId',DT_Session::get('Setting.User.eventId'));

	   $fieldtype =$this->getModel( 'fieldtype' );

	   $event = $this->getModel('event')->table;

	   $event->load($eventId);

	   $this->loadprofile();

	   $memtot = DT_Session::get('Setting.User.memtot');

	   $this->view->assign( 'form' ,$event->form('B',(array)DT_Session::get('Setting.User'),false,'frmcart',false,$memtot));

	   

	   // pr($_SESSION['DTregister']);

	   

	   $this->view->setLayout('individualRegister');

	   if($event->group_registration_type !="detail"){

		    $back =  JRoute::_('index.php?option=com_dtregister&Itemid='.$Itemid.'&eventId='.$eventId.'&controller=event&task=groupNum');

	   }else{

		    $back =  JRoute::_("index.php?option=com_dtregister&Itemid=".$Itemid."&eventId=".$eventId."&controller=event&task=memberdata&back=1&memberIndex=".DT_Session::get('Setting.current.memberIndex'));

	   }

	   $this->view->assign('pageTile',JText::_( 'DT_BILLING_INFORMATION'));

	   $this->view->assign('task','confirmsave');

	   $this->view->assign('back',$back);

	   $this->view->assign('tEvent',$event);

	   $this->view->assign('eventId',$eventId);

	   $this->view->assign('header_eventId',$eventId);

	   $this->set_User_index();

	   $this->view->display();

	   

	}

	

	function terms(){

		$eventId = JRequest::getVar('eventId',0);

		$event = $this->getModel('event')->table;

	    $event->load($eventId);

		if($event->terms_conditions_set == 1){

			$terms = $event->terms_conditions_msg;

		}else{

			$terms = $this->config->getGlobal('terms_conditions_msg','');

		}

		$this->view->assign('terms',$terms);

		$this->view->setLayout('terms');

		$this->view->display();

		

	}

	

	function cartRemove(){

	   global $mainframe,$Itemid;

	   $userIndex = JRequest::getVar('userIndex');

	   

	   // pr($userIndex);

	   

	   DT_Session::clear('register.User.'.$userIndex);

	   DT_Session::set('Setting.User',DT_Session::get('Setting.User'));

	   

	   foreach(DT_Session::get('Setting.User') as $key => $user) {

	   		DT_Session::set('Setting.User.'.$key.".Index" ,$key);

	   }

	   // prd(DT_Session::get('register.User'));

	   // prd($_SESSION);

	    if(count(DT_Session::get('register.User')) < 1) {

	   		DT_Session::clearAll('');

	   }
sleep_till_session_written();
	   $mainframe->redirect('index.php?option=com_dtregister&controller=event&task=viewCart&Itemid='.$Itemid);  

	}

	

	function remove_member(){

	   global $mainframe,$Itemid;

	   $index = JRequest::getVar('index');

	   

	   // pr($userIndex);

	   

	   DT_Session::clear('Setting.User.members.'.$index );

	   

	   // re arrange key index;

	   DT_Session::set('Setting.User.members',DT_Session::get('Setting.User.members'));

	   

	   foreach(DT_Session::get('Setting.User.members') as $key => $member) {

	   		DT_Session::set('Setting.User.members.'.$key.".Index" ,$key);

	   }

	   DT_Session::set('Setting.User.memtot',count(DT_Session::get('Setting.User.members')));

	  
sleep_till_session_written();
	   $mainframe->redirect('index.php?option=com_dtregister&controller=event&task=billinginfo&Itemid='.$Itemid);	   

	}

	

	function edit_member(){

	   global $mainframe,$Itemid;

	   $index = JRequest::getVar('index');

	   

	   // pr($userIndex);

	   

	   $memberdata = DT_Session::get('Setting.User.members.'.$index );

	   

	   DT_Session::set('Setting.Member',$memberdata);

	  
sleep_till_session_written();
	   $mainframe->redirect('index.php?option=com_dtregister&controller=event&task=memberdata&Itemid='.$Itemid);	   

	}

	

	function editlastmember() {

		global $mainframe,$Itemid;

	    //$index = end(DT_Session::get('Setting.User.members'));

	   

	   // pr($userIndex);

	   

	   $memberdata = end(DT_Session::get('Setting.User.members'));

	   

	   DT_Session::set('Setting.Member',$memberdata);

	  
sleep_till_session_written();
	   $mainframe->redirect('index.php?option=com_dtregister&controller=event&task=memberdata&Itemid='.$Itemid);

	}

	

	

	function edit(){

	   global $mainframe,$Itemid;

	   $userIndex = JRequest::getVar('userIndex');

	   

	   // pr($userIndex);

	   

	   $userdata = DT_Session::get('register.User.'.$userIndex);

	   

	   if($userdata['type'] == 'G') {

	       $task = "billinginfo";

	   } else {

	   	   $task = "individualRegister";

	   }

	    DT_Session::set('Setting.User',$userdata);

		DT_Session::set('Setting.Editindex',$userIndex);

		DT_Session::set('Setting.eventId', $userdata['eventId']);
sleep_till_session_written();
	   $mainframe->redirect('index.php?option=com_dtregister&controller=event&task='.$task.'&Itemid='.$Itemid);	   

	}

	

	function loadprofile(){

	   $user = JFactory::getUser();

	   $muser = $this->getModel('user');

       $tuser = $muser->table;

       $profile = $tuser->TableJUser;

       $user_id = $user->id;

       $userdata = $profile->getProfile($user_id);

	  

	   if (is_array($userdata) && count($userdata) && $user_id) { 

		 foreach($userdata as $field_id => $val){

			 if($field_id != "" && isset($userdata[$field_id])){

				   $currentval = DT_Session::get('Setting.User.fields.'.$field_id);

				   if($currentval === false){

					  DT_Session::set('Setting.User.fields.'.$field_id,$userdata[$field_id]);

				   }

		  

			 }

		 } 

	   } else {

		     

	   		$this->load_from_first_reg();

	   }

	   

	}

	function individualRegister(){

	   global $mainframe,$Itemid,$xhtml;

	   

	   $eventId = JRequest::getVar('eventId');

	   if (isset($eventId) && $eventId > 0) {

	    	DT_Session::set('Setting.eventId', $eventId);

			DT_Session::set('Setting.process', 'register');

	   }else {

	   	   $eventId = DT_Session::get('Setting.eventId');

	   }

	   if(false === DT_Session::get('Setting.eventId') || DT_Session::get('Setting.eventId') == 0){

		   

		   $mainframe->redirect("index.php?option=com_dtregister&Itemid=".$Itemid);

	   }

	   $tEvent = $this->getModel('event')->table;

	   $tUser = $this->getModel('user')->table;

	   $tEvent->load($eventId);

	   $tEvent->is_registerable($tUser);

	   

		if(false === DT_Session::get('Setting.eventId')){

		   

		   $mainframe->redirect("index.php?option=com_dtregister&Itemid=".$Itemid);

	   }

	   

	

	   if(DT_Session::get('Setting.User.type') == 'G') {

		   		DT_Session::clear('Setting.User');

				if(DT_Session::get('Setting.cart') === false && DT_Session::get('Setting.Editindex') === false) {



					DT_Session::clear('register');

				}



	   }

	

	   DT_Session::set('Setting.User.memtot',1);

	   DT_Session::set('Setting.User.type','I');

	   if(DT_Session::get('Setting.User.eventId') ==  false) {

			

				DT_Session::set('Setting.User.eventId',$eventId);

		   }

	   $this->view->setModel($this->getModel( 'field' ));

	   $this->view->setModel($this->getModel( 'fieldtype' ));

	   $field = $this->getModel( 'field' )->table;

	   $eventId = JRequest::getVar('eventId',DT_Session::get('Setting.User.eventId'));

	   $fieldtype =$this->getModel( 'fieldtype' );

	   $event = $this->getModel('event')->table;

	   $event->load($eventId);

	   

	   $tUser = $this->getModel('user')->table;

	   

	   $event->is_registerable($tUser);

	   $this->loadprofile();

	   $memtot = DT_Session::get('Setting.User.memtot');

	   $this->view->assign( 'form' ,$event->form('I',(array)DT_Session::get('Setting.User'),false,'frmcart',false,$memtot));

	   $this->view->setLayout('individualregister');

	  

	  //  pr((array)DT_Session::get('register.User.'.$userIndex));

	    //pr($_SESSION['DTregister']);

	 

	  $back =  JRoute::_("index.php?option=com_dtregister&Itemid=".$Itemid."&eventId=".$eventId."&controller=event&task=options&back=1");

	  $this->view->assign('pageTile', JText::_( 'USER_INFORMATION' ));

	  $this->view->assign('task','confirmsave');

	  $this->view->assign('back',$back);

	  $this->view->assign('tEvent',$event);

	  $this->view->assign('eventId',$eventId);

	  $this->view->assign('header_eventId',$eventId);

	  $this->set_User_index();

	  $this->view->display();

	   

	}

	

	function set_User_index() {

	   global $disable_cart;

	   $usertot = 0;

	   if(DT_Session::get('register.User')) {

		 

		 if(DT_Session::get('Setting.User.Index') !== false) {

			$usertot = count(DT_Session::get('register.User')); 

		 } else {

			$usertot = count(DT_Session::get('register.User'));

		 }

	      

	   }  else {

	   	  $usertot = 0;

	   }

	   

	   if(DT_Session::get('Setting.User.Index') !== false) {

	   		

			$index = DT_Session::get('Setting.User.Index');

			

	   } else {

	   		$index = $usertot;

	   }

	   if($disable_cart == 0) {

	   	  

		  $index = 0;

	   }

	   $this->view->assign('Index',$index);

		

	}

	

	function price_header(){

	$layout = JRequest::getVar('dttmpl','price_header');

	   if(isset($_REQUEST['Field'])){

		  

		   if(isset($_REQUEST['member_data'])){

			 

	          DT_Session::set('Setting.Member.fields', $_REQUEST['Field']);

		   }else{

			   

			   DT_Session::set('Setting.User.fields', $_REQUEST['Field']);

			   if(isset($_REQUEST['username'])){

				   DT_Session::set('Setting.User.username', $_REQUEST['username']);

				   DT_Session::set('Setting.User.password', $_REQUEST['password']);

			   }

			  if(isset($_REQUEST['discount_code']) && $_REQUEST['discount_code'] != ""){

				  $eventId = JRequest::getVar('eventId',DT_Session::get('Setting.eventId'));

				  $event = $this->getModel('event')->table;

	              $event->load($eventId);

				  $discount_code_id = $event->validate_code($_REQUEST['discount_code']); 

				  if($discount_code_id !== false){

					  DT_Session::set('Setting.User.discount_code_id', $discount_code_id );

				  }else{

					  DT_Session::set('Setting.User.discount_code_id', false );

					  $this->view->assign('discountCodeError',$event->TableEventdiscountcode->TableDiscountcode->error );

					

				  }

				  

			  }else{

				   DT_Session::set('Setting.User.discount_code_id', 0 );

			  }

		   }

	   } else if(DT_Session::get('Setting.User.fields') == false){

	   		

			$this->load_from_first_reg();

			

	   }

	   $this->view->setLayout($layout );

	   $this->view->display();

	   die;

	}

	

	function load_from_first_reg() {

	   $data = DT_Session::get('register.User.0');

	   if($data !== false) {

	   	  

		  if(DT_Session::get('Setting.reg_status') == 'start') {

		  	 

			 DT_Session::set('Setting.User.fields',$data['fields']);

			 if(DT_Session::get('Setting.User.memtot') ===  false) {

			   DT_Session::set('Setting.User.memtot',$data['memtot']);

			 }

			 if(DT_Session::get('Setting.User.type') ===  false) {

			   DT_Session::set('Setting.User.type',$data['type']);

			 }

			 if(DT_Session::get('Setting.User.eventId') ==  false) {

			   DT_Session::set('Setting.User.eventId',$data['eventId']);

			 }

			

			 

		  } else {

		  	 DT_Session::set('Setting.User', $data );

		  }

		  if(DT_Session::get('Setting.eventId') ==  false) {

			   DT_Session::set('Setting.eventId',DT_Session::get('Setting.User.eventId'));

			 }

		  

	   }

	   

	}

	

	function load_from_last_reg() {

	  

	   $data = DT_Session::get('register.User');

	   $data = end($data);

	   if($data !== false) {

	   	  

		  if(DT_Session::get('Setting.reg_status') == 'start') {

		  	 

			DT_Session::set('Setting.User.fields',$data['fields']);

			 if(DT_Session::get('Setting.User.memtot') ==  false) {

			   DT_Session::set('Setting.User.memtot',$data['memtot']);

			 }

			 if(DT_Session::get('Setting.User.type') ==  false) {

			   DT_Session::set('Setting.User.type',$data['type']);

			 }

			 if(DT_Session::get('Setting.User.eventId') ==  false) {

			   DT_Session::set('Setting.User.eventId',$data['eventId']);

			 }

			 

		  } else {

		  	 DT_Session::set('Setting.User', $data );

		  }

		   if(DT_Session::get('Setting.eventId') ==  false) {

			   DT_Session::set('Setting.eventId',DT_Session::get('Setting.User.eventId'));

			 }

	   }

	  

	}

	

	function registrant(){

		global $mainframe,$registrant_list;

	    $mUser = $this->getModel( 'user' );

		$order = JRequest::getVar('filter_order','name');

		$dir = JRequest::getVar('filter_order_Dir','asc');

		

		jimport('joomla.html.pagination');



	    $listLimit = $mainframe->getCfg( 'list_limit', 10 );

        $limit=JRequest::getInt('limit',$listLimit);



	    $limitstart = JRequest::getVar('limitstart', 0, '', 'int');

		

		$searchtxt = JRequest::getVar( 'search' ,'' );

		$users = $mUser->getUsers(array('eventId'=>JRequest::getVar('eventId'),'query'=>$searchtxt,'status'=>array(0,1)),$order,$dir,$limitstart,$limit);

		$total = $mUser->table->getLastCount($mUser->count_query);

		

		$pageNav = new DtPagination( $total, $limitstart, $limit );

		

		//if(!$registrant_list){

	

			$my = &JFactory::getUser();

	

			if($registrant_list <= $my->aid){

	

			}else{

	

				echo JText::_( 'DT_LOGIN_REQUIRED');

	

				return;

	

			}

	

		//}

		$this->view->assign('users',$users);

		$this->view->assign('pageNav',$pageNav);

		

		$this->display();

	}



	function getjevent(){

	   

	   $jevt = $this->getModel('event')->tableJevt;

	   $data = $jevt->find(' evdet_id='.JRequest::getVar('eventId',0));

	   $rep = $jevt->parserule(JRequest::getVar('eventId',0));

	  

	 //  prd($jevt->getObjData());

	   ob_clean();

	  	

	//   prd(json_encode($repjson));

	   if($data){

	      $data = $data[0];

	   if($rep)

	      $data->reprules = $jevt->getObjData();

	   echo json_encode($data);

	   

	   }else{

	      $array = array('error'=>'No event');

		  echo json_encode($array);

	   }

	   

	   die;

	}

	function continue_reg() {

		

		global $mainframe,$Itemid,$xhtml;

		

		//DT_Session::set('Setting.cart','yes');

		sleep_till_session_written();

		$mainframe->redirect(JRoute::_("index.php?option=com_dtregister&controller=event&Itemid=".$Itemid,$xhtml));

	}

	function groupNum(){

	   

	   global $mainframe,$Itemid,$xhtml;

	   $eventId = JRequest::getVar('eventId');

	

	   if(DT_Session::get('Setting.User.type') == 'I') {

		   		DT_Session::clear('Setting.User');

				if(DT_Session::get('Setting.cart') === false && DT_Session::get('Setting.Editindex') === false) {

					DT_Session::clear('register');

				}

		   }

	

	   if (isset($eventId) && $eventId > 0) {

	    	DT_Session::set('Setting.eventId', $eventId);

			DT_Session::set('Setting.User.eventId', $eventId);

			DT_Session::set('Setting.process', 'register');

	   }

		if(false === DT_Session::get('Setting.eventId') || DT_Session::get('Setting.eventId') == 0){

		   

		   $mainframe->redirect("index.php?option=com_dtregister&Itemid=".$Itemid);

	   }

	   

	   $eventId = JRequest::getVar('eventId',DT_Session::get('Setting.eventId'));

	   $tEvent = $this->getModel('event')->table;

	   $tUser = $this->getModel('user')->table;

	   $tEvent->load($eventId);

	   $tEvent->is_registerable($tUser);

	   if(isset($_POST['memtot'])){

	       DT_Session::clear('Setting.User.members');

		   DT_Session::set('Setting.User.memtot',$_POST['memtot']);

		   $eventTaable = $this->getModel('event')->table;

		   $eventTaable->load($eventId);

		   

		   if($eventTaable->group_registration_type !="detail"){

			  $mainframe->redirect('index.php?option=com_dtregister&controller=event&task=billinginfo&Itemid='.$Itemid);

		   }else{

		     $mainframe->redirect("index.php?option=com_dtregister&controller=event&task=memberdata&Itemid=".$Itemid);

		   }

	   }

	   

	   // echo '<pre>'; print_r($tEvent->max_group_size);

	   

	   DT_Session::set('Setting.User.type','G');

	   $this->view->assign('header_eventId',$eventId);

	   $this->view->setLayout('groupNum');

	   $this->view->assign('tEvent',$tEvent);

	   $this->view->display();

	   

	}



}

?>