<?php

/**
* @version 2.8.16
* @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
*/

global $Itemid,$mainframe,$depend_on_field,$field_options,$paylater_field,$paylater_field_options,$xhtml_url;

$document =& JFactory::getDocument();

$document->addScript( JURI::root(true)."/components/com_dtregister/assets/js/dt_jquery.js");

$document->addScript( JURI::root(true)."/components/com_dtregister/assets/js/validate.js");

$document->addScript( JURI::root(true)."/components/com_dtregister/assets/js/validationmethods.js");
if(DT_Session::get('register.User.process')){
 // include(JPATH_SITE.DS.'components'.DS.'com_dtregister'.DS.'views'.DS.'user'.DS.'tmpl'.DS.'event_header.php');
  include(DTreg::get_template_path('user'.DS.'tmpl'.DS.'event_header.php'));
}else{
  ?>
  <h2><?php echo JText::_('DT_EVENT_REGISTRATION')?>: <?php echo JText::_( 'DT_PAYMENT' );?></h2>
  <?php	
}
$action = JRoute::_("index.php?option=com_dtregister&controller=payment&Itemid=".$Itemid,false);
?>

<form name="frmcart" method="post" action="<?php echo $action ?>" enctype="multipart/form-data">

<!--form name="frmcart" method="post" action="http://www.joomlaeventregistration.com/j25session/components/com_dtregister/jump.php" enctype="multipart/form-data" -->

<div id="dtpay_options">

<?php

global $Itemid,$xhtml,$paymentmethod;

$paymentmhd = $this->getModel('paymentmethod');

$paymentmhds = $paymentmhd->getMethods();
$paymentmethods = $paymentmethod;
$pay_images = $paymentmhd->images;

$paylateroptions = DtHtml::options($paymentmhd->paylater->getOptions(),JText::_( 'DT_SELECT_PAY_OPTIONS' ));
$display_offline = false;
if($depend_on_field != "" && DT_Session::get('register.User') != false) {
	
	foreach(DT_Session::get('register.User') as $user) {
		
	if(isset($user['fields'][$paylater_field])) {
			
			if(is_array($user['fields'][$paylater_field])){
			   
			   foreach($user['fields'][$paylater_field] as $field_value){
			      
				  if(in_array($field_value , $paylater_field_options)) { // display offline 
				        $display_paylater = true;
				     break;
				   } else{
					continue;
				   }
			      
			   }
			   if($display_paylater){
			      break ;
			   }
			}else{
			   
			   
			   if(in_array($user['fields'][$paylater_field] , $paylater_field_options)) { // display offline 
				  $display_paylater = true;
				   break;
				} else{
					continue;
				}
			  
			}
			
		} else {
			continue;
		}
		
		
	}
} else{
	$display_offline = true; 
}

if(!$display_offline) {
	
	$key = array_search('offline_payment',$paymentmethods);
	if($key !== false) {
		
		unset($paymentmethods[$key]);
	}
	
}

$display_paylater = false;
if($paylater_field != "") {
	
	foreach(DT_Session::get('register.User')  as $user) {
		
		if(isset($user['fields'][$paylater_field])) {
			if(in_array($user['fields'][$paylater_field] , $paylater_field_options)) { // display offline 
				$display_paylater = true;
				break;
			} else{
				continue;
			}
		} else {
			continue;
		}
		
	}
} else{
	$display_paylater = true;
}

if(!$display_paylater) {
	
	$key = array_search('pay_later',$paymentmethods);
	if($key !== false) {
		
		unset($paymentmethods[$key]);
	}
	
}

$i=1;

$paymcnt = 0;
foreach($paymentmethods as $key=>$method){
	
	if($method == "pay_later" && DT_Session::get('Setting.process') !="register"){
		unset($method);
		$method = $last_method;
		continue;
    }
	
	$last_method = $method;
	$paymcnt++;

  ?>

	 <dl> 

            <dt style="padding: 0 5px 0 0">
                    <img src="<?php echo $pay_images[$method]; ?>" alt="" width="149" height="100" align="middle" />
			</dt>

             <dd><input type="radio" class="required" id="paymentmethod" name="paymentmethod" value="<?php echo $method; ?>" />&nbsp;&nbsp;<?php echo $paymentmhds[$method]; ?>

			 <?php 

			  if($method == "pay_later"){

				   echo JHTML::_('select.genericlist', $paylateroptions,'pay_later_option','onchange=check_value(this.value)','value','text');

			  }

			   ?></dd>

           </dl>

   	<?php

	$i++;

}

if($paymcnt == 1){
	if($method == "pay_later"){
		
		$paymethod_options = $paymentmhd->paylater->getOptions();
		
	   if(count($paymethod_options) == 1){
		    foreach($paymethod_options as $paylateroption=>$pay){
				
			}
	   		$mainframe->redirect(JRoute::_("index.php?option=com_dtregister&controller=payment&task=form&paymentmethod=$method&Itemid=$Itemid&pay_later_option=$paylateroption",false));
			exit ;
	   }
	}else{
		
		$mainframe->redirect(JRoute::_("index.php?option=com_dtregister&controller=payment&task=form&paymentmethod=$method&Itemid=$Itemid",false));
		exit ;
	}
  	
}

?>

<div style="clear:both">

   <label for="paymentmethod" style="display:none" class="error" generated="true"></label>

</div>

<div style="clear:both">
   <?php 
   if(DT_Session::get('Setting.process') == 'register') {
   ?>
   <input type="button" name="back" id="back" value="<?php echo JText::_( 'DT_BACK' ); ?>" onclick="window.location='<?php echo  JRoute::_("index.php?option=com_dtregister&controller=event&Itemid=$Itemid&task=viewCart&back=1",$xhtml_url); ?>'" />
   <?php } else {
	?> 
	   <input type="button" name="back" id="back" value="<?php echo JText::_( 'DT_BACK' ); ?>" onclick="window.location='<?php echo  JRoute::_("index.php?option=com_dtregister&controller=user&Itemid=$Itemid&task=confirm&back=1",$xhtml_url); ?>'" />
	  
     <?php } ?>
   <input type="submit" name="submitpayment" id="next" value="<?php echo JText::_( 'DT_NEXT_BUTTON' ); ?>" />

</div>

 <input type="hidden" name="option" value="com_dtregister" />

 <input type="hidden" name="controller" value="payment" />

 <input type="hidden" name="Itemid" value="<?php echo $Itemid; ?>" />

 <input type="hidden" name="task" value="form" />

</div>

</form>
<script type="text/javascript">
	function check_value(value) {
		if(value != "") {
			DTjQuery(":radio[value=pay_later]").attr('checked',true);
		} else {
			DTjQuery(":radio[value=pay_later]").attr('checked',false);
		}
	}
</script>
<script type="text/javascript">

 DTjQuery(function(){

   //DTjQuery.validator.messages.required = " ";

    DTjQuery(document.frmcart).validate({

	        success: function(label) {

				label.addClass("success");

			}

	});

	 DTjQuery('#pay_later_option').rules('add',{required: function(){
		  					
		                      return ( DTjQuery('input[id="paymentmethod"]:checked').val() == 'pay_later');          

		                   }

                  });

 })

</script>
