<?php

/**
* @version 2.8.17
* @package Joomla 2.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 DTreg {
    
	public static function getPaymentInstance($name="paypal"){
	    if($name == ""){
			echo "<pre>";
		   	debug_print_backtrace();
			die;
		}
		require_once( JPATH_SITE.'/components/com_dtregister/lib/payment/'.$name.'.php');
		$payment = new $name();
		if(is_callable(array($payment, 'getPaymentInstance'),false)){
		    return $payment->getPaymentInstance();
		} else {
		    return $payment;
		}	
		
	}
	
	function get_template_path($path){
	   
	   if(file_exists(str_replace("tmpl/","" ,JPATH_THEMES . '/' . JFactory::getApplication()->getTemplate() . '/html/com_dtregister/'.$path))) {
	      return str_replace("tmpl/","" ,JPATH_THEMES . '/' . JFactory::getApplication()->getTemplate() . '/html/com_dtregister/'.$path);
	   } else {
	      
	      return JPATH_SITE.DS.'components/com_dtregister'.DS.'views'.DS.$path;
	   }
	}
	
	function getcomItemId($option){
		
		global $mainframe;
				
        $database = &JFactory::getDBO();
		$query = " SELECT m.id
					FROM #__extensions c
					INNER JOIN #__menu m ON c.extension_id  = m.component_id
					WHERE c.name = '$option' 
					AND m.parent_id= 0
					LIMIT 1 ";
	
		$database->setQuery( $query );
	    
		 $Itemid = $database->loadResult();
		 return $Itemid; 

    }
	
	function check_com_menu_ItemId($Itemid){
		
		global $mainframe;
				
        $database = &JFactory::getDBO();
		 $query = " SELECT m.id
					FROM #__extensions c
					INNER JOIN #__menu m ON c.extension_id  = m.component_id
					WHERE 
  c.name = 'dtregister' AND
  LOCATE('task=register',m.link) > 0
    AND m.id = '$Itemid'
    
   LIMIT 1";
	
		$database->setQuery( $query );
	    
		 $Itemid = $database->loadResult();
		 
		 return $Itemid; 

    }
	
	function register_link_small($row,$task,$class,$color,$front_link_type=0){
	     // prd('reached');
		 if($row->future_event=='y' ){ 

		 if($front_link_type){

		   	 $register = '<img class="event_button" src="'.JURI::root(true).'/components/com_dtregister/assets/images/'.$color.'/closed_62x14.png" border="0" alt="'.JText::_('DT_CLOSED').'" />';

			 $separater = " ";

             return $register;

		   }else{

	    		$register = JText::_( 'DT_CLOSED');

                return $register;

		   }

   }elseif(($row->cut_off=='y') && ($row->cut_off_date!='0000-00-00')){

		 if($front_link_type){

		   	 $register = '<img class="event_button" src="'.JURI::root(true).'/components/com_dtregister/assets/images/'.$color.'/closed_62x14.png" border="0" alt="'.JText::_('DT_CLOSED').'" />';

			 $separater = " ";

		   }else{

	    		$register = JText::_( 'DT_REGISTER');

		   }
           
	}elseif(($row->registered>=$row->max_registrations)&&($row->max_registrations)&&($row->waiting_list=='0')){	

		if($front_link_type){

		   	 $register = '<img class="event_button" src="'.JURI::root(true).'/components/com_dtregister/assets/images/'.$color.'/full_62x14.png" border="0" alt="'.JText::_('DT_FULL').'" />';

			 $separater = " ";

		   }else{

	    		$register = JText::_( 'DT_FULL');

		   }
           return $register;
	}elseif(($row->registered >= $row->max_registrations)&&($row->max_registrations)&&($row->waiting_list > 0)){

		if($front_link_type){

		   	 $register = '<img class="event_button" src="'.JURI::root(true).'/components/com_dtregister/assets/images/'.$color.'/full_62x14.png" border="0" alt="'.JText::_('DT_FULL').'" />';

			 $separater = " ";

		   }else{

	    		$register = JText::_( 'DT_FULL');

		   }

	}else{

		if($front_link_type){

		   	 $register = '<img class="event_button" src="'.JURI::root(true).'/components/com_dtregister/assets/images/'.$color.'/register_62x14.png" border="0" alt="'.JText::_('DT_REGISTER').'" />';

			 $separater = " ";

		   }else{

	    		$register = JText::_( 'DT_REGISTER');

		   }

	}
     $reglink = self::register_href($row,$task);
	 return $text ='<a '.$class.' href="'.$reglink.'">'.$register.'</a>';
		 
	}
	
	function register_href($row,$task){
	    global $xhtml_url,$Itemid;
		$eventId = $row->slabId;
		
		return JRoute::_("index.php?option=com_dtregister&controller=event&eventId=$eventId&Itemid=$Itemid&task=register",$xhtml_url);
		
		switch ($row->registration_type) {

         case "individual" :
  		      $reglink=JRoute::_("index.php?option=com_dtregister&controller=event&eventId=$eventId&Itemid=$Itemid&task=$task&type=individual",$xhtml_url);

          	  break;

        case "group" :
              $reglink=JRoute::_("index.php?option=com_dtregister&controller=event&eventId=$eventId&Itemid=$Itemid&task=$task&type=group_num",$xhtml_url);

      		  break;

        case "both" :

            $reglink=JRoute::_("index.php?option=com_dtregister&controller=event&eventId=$eventId&Itemid=$Itemid&task=$task&type=options",$xhtml_url);

    	    break;

      }
		return $reglink;
		
	}
	
	function currency_symbol($currency_code){
	    $arrCode=array(

     		'USD'=>'$',

     		'BRL'=>'R$',

     		'ZAR'=>'R',

     		'GBP'=>'&#163;',

     		'EUR'=>'&#128;',

     		'JPY'=>'&#165;'

     	);

		foreach($arrCode as $arKey=>$arValue){

			if ($arKey === $currency_code){

				$currency_symbol = $arValue;

				break;

			} else {
	
				$currency_symbol = "";
	
			}

		}
		
		return $currency_symbol;
	}
	
	function eventStartDateDisplay($row){
	    
		global $displaytime,$timeformat,$dateformat; // config variables 
		
		if($row->dtstart == $row->dtend){
		   
		}
		
		return '<span style="float:left">'.$row->dtstart.'<br />&nbsp;'.$row->dtstarttime.'</span>';
		
	}
	
	function eventEndDateDisplay($row){
	   
	   return '<span style="float:right">'.' - ' .$row->dtend.'<br />&nbsp;&nbsp;&nbsp;'.$row->dtendtime.'</span>';
	   
	}
	
	function displayRate($value,$code="USD"){
	   global $currency_code;
	   if($code == ""){
	       $code = "USD";
		   
	   }
	    if(trim($value) == ""){
	   	 $value = 0;
	   }
	   $currency_symbol = self::currency_symbol($code);
	   $currency_symbol = ($currency_symbol=="")?$currency_symbol:$currency_symbol." ";
	   $code = ($currency_symbol=='')?' '.$code:'';
	   return $currency_symbol.self::numberFormat($value,2).$code;
	    
	}
	
	function showprice($value=0){
		
		return self::numberFormat($value,2);
	}
	
	function displayEventTitle($event=null){
	   
	   return $event->title;	
	}
    
	function showDate($date='now'){
	   
	   	return $date;
	}
	
    function numberFormat($value,$precision=2){
      global $currency_separator;
      $number_value = $value;
	  if($value == "") { return "0.00";}
      $padString = "";

      $value = $number_value;
	  $value = str_replace(",",".",$value);
      $sep_array =  array(0=>".",1=>",",2=>".");
	  $formatedValue=number_format($value,$precision,$sep_array[$currency_separator],"");

	  $pointIndex=strrpos($formatedValue,".");

	  if($pointIndex===false){

      if($currency_separator == 1){

		  $formatedValue = str_replace(".",",",$formatedValue);

		}
        if($currency_separator == 0){
			   $formatedValue = round($formatedValue);
		}
		return $formatedValue;

	  }else{
        
		//Cut the string and show it here

		$pointIndex=strrpos($formatedValue,".");

		if($pointIndex===false)

		{

			if($currency_separator == 1){

			  $formatedValue = str_replace(".",",",$formatedValue);

			}
            if($currency_separator == 0){
			   $formatedValue = round($formatedValue);
			}
			return $formatedValue;

		}

		else

		{

			$newLength=$pointIndex+1+$precision;

			for($i=strlen($formatedValue);$i<$newLength;$i++)

			{

				$padString.="0";

			}

        if($currency_separator == 1){

		     $formatedValue = str_replace(".",",",$formatedValue.$padString);

		    }else{

			  $formatedValue = $formatedValue.$padString;

			}
            if($currency_separator == 0){
			   $formatedValue = round($formatedValue);
			}
			
			
			return $formatedValue;

		}

	}

  }
	
}
?>