<?php

/**
* @version 2.7.16
* @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 DtregisterControllerValidate extends DtrController {

   var $name = "Validate";

    function __construct($config = array()){

		 parent::__construct($config);

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

	}

   function discountcode(){
	   
	   $eventId = JRequest::getVar('eventId',DT_Session::get('Setting.eventId'));
	   $event = & DtrTable::getInstance('Event','Table');
	   $event->load($eventId );
	   $error = true;
	   if(isset($_REQUEST['discount_code']) && $_REQUEST['discount_code'] != ""){
			
			 $discount_code_id = $event->validate_code($_REQUEST['discount_code']); 
			if($discount_code_id !== false){
			   $error = false;	
			  // DT_Session::set('register.User.'.$userIndex.'.discount_code_id', $discount_code_id );
			}else{
			  // DT_Session::set('register.User.'.$userIndex.'.discount_code_id', false );
			  $error = true;	
			  if($event->TableEventdiscountcode->TableDiscountcode->error == "")
			  {
			  	$event->TableEventdiscountcode->TableDiscountcode->error = JText::_('DT_CODE_INCORRECT');
			  }
			  $this->view->assign('discountCodeError',$event->TableEventdiscountcode->TableDiscountcode->error );
			    $message = $event->TableEventdiscountcode->TableDiscountcode->error;
			}
			
		}
		///ob_clean();
		echo ($error)?$message:'true';
		die;
	      
   }

	function captcha(){

	    $database = &JFactory::getDBO();

		$code=JRequest::getVar('captcha','');

		$userIp=$_SERVER['REMOTE_ADDR'];

		//Check database

		$sql="Select count(*) From #__dtregister_captcha where user_ip='$userIp' and code='$code'";

		$database->setQuery($sql);

		$total=$database->loadResult();

		//$_SESSION['register']['secCheck'] = $total;

		if($total){

			echo "true";

		}else{

			echo "false";

		}

die;

	}

	function email(){

	    global $usercreation;

		$database = &JFactory::getDBO();

		ob_clean();
		
		$eventId=$_GET['eventId'];
		
		$sql="Select config_value From #__dtregister_config where config_key = 'prevent_duplication'";

		$database->setQuery($sql);

		$prevent_duplication=$database->loadResult();

		$global_prevent_duplication = $prevent_duplication;

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

		$eventTable->load($eventId);
        $prevent_duplication = $eventTable->prevent_duplication;
		$eventTable->overrideGlobal($eventId);
		
		$usercreation = $eventTable->usercreation;
		$my = &JFactory::getUser();
		
		$field_id = key($_REQUEST['Field']);
		$email=array_pop($_REQUEST['Field']);
		
		if(!$my->id &&  $usercreation > 0 && !isset($_GET['member_check'])){

			$sql="SELECT COUNT(*) FROM #__users WHERE email='$email' ";

			$database->setQuery($sql);

			$total=$database->loadResult();

			if($total){

				echo "\"".JText::_( 'DT_JOOMLA_EMAIL_EXISTS' )."\"";

			}else{

			    echo "true";	

			}
			
			die;
		}
		
		if($prevent_duplication){

		  $prevent_duplication = $global_prevent_duplication;

		}else{

		  $prevent_duplication = 0;

		}
		
		if(!$prevent_duplication || isset($_REQUEST['dup_check'])){

			echo "true";	

		}else{
			 $user_con = "";
            if(isset($_GET['userId'])) {
				$userid = $_GET['userId'];
				$user_con = " and u.userId <> $userid ";
				
			}
			$sql="SELECT COUNT(*) FROM #__dtregister_user u inner join #__dtregister_user_field_values v on u.userId  = v.user_id   WHERE eventId='$eventId'  AND v.field_id = $field_id  and v.`value` = '$email' and u.cancel <> 1 and u.type = 'I' and u.status in(0,1) $user_con";

			$database->setQuery($sql);

			$total=$database->loadResult();
			$member_con = "";
			 if(isset($_GET['member_id'])) {
				$memberid = $_GET['member_id'];
				$member_con = " and m.groupMemberId <> $memberid ";
				
			}
		$sql="SELECT COUNT(*) FROM #__dtregister_user u inner join #__dtregister_group_member m on m.groupUserId = u.userId inner join  #__dtregister_member_field_values v on m.groupMemberId   = v.member_id   WHERE eventId='$eventId'  AND v.field_id = $field_id  and v.`value` = '$email' and u.cancel <> 1 and u.type = 'G' and u.status in(0,1) $member_con";

			$database->setQuery($sql);

			$total2=$database->loadResult();
       
			if($total || $total2){

				$clean = ob_get_clean();
				echo "\"".JText::_('DT_ALREADY_REGISTERED')."\"";die;

			}else{

				echo "true";

			}

		}

die;

    }

	function uniqueUser(){

	    $my = &JFactory::getUser();

		$database = &JFactory::getDBO();

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

		   $username = $_GET['username'];

		   $where = " username='".$username."' ";

		}else{

		   $email=array_pop($_GET['Field']);

		   $where = " email='".$email."' ";

		}

		$sql="SELECT COUNT(*) FROM #__users WHERE $where ";

		$database->setQuery($sql);

		$total=$database->loadResult();

		if($total){

			echo "\"".JText::_( 'DT_JOOMLA_USERNAME_EXISTS' )."\"";

		}else{

		   echo "true";

		}
	
die;

	}	

}

?>