<?php

/**
* @version 2.6.7
* @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 Payment {

  var $module ;
	var $component_tasks =  array('registeration'=>'payment','duepay'=>'due_payment','change'=>'paymentchanges','cancel'=>'paymentchanges');
	var $retryFields = array();
	function Payment($method,$params=array()){
		
	}
	
	function generateconfirmNum(){
	   
	     global $confirm_number_type , $confirm_number_prefix , $confirm_number_start ;
	   
	   if($confirm_number_type=='random'){
	       $chars = "0123456789";

		   srand((double)microtime()*1000000);

			for($i=0; $i<7; $i++){
	
				$x_invoice_num1 .= $chars[rand()%strlen($chars)];
	
			}

	   }else{
	       $x_invoice_num1 = $confirm_number_start+1 ;
	   }
	   
	   return $this->confirmNum = $confirm_number_prefix.$x_invoice_num1 ;
       
	}
	
	function billingform(){
	     ob_start();
	     if($this->paymentType!='registeration' || $_SESSION['register']['memtot']){ // $dt_user->userType=="I" 

				?>

		        <tr><td colspan="2" ><strong><?php echo JText::_( 'DT_PAYMENT_INFORMATION' );?></strong></td></tr>

                <tr><td colspan="2" >&nbsp;</td></tr>

                <tr>

                	<td><?php echo JText::_( 'DT_SAME_BILLING_INFO' );?></td>

                	<td><input type="checkbox" class="inputbox" name="same" id="same" value="1" /></td>

                </tr>

		        <tr>

                	<td width="31%"  ><?php echo JText::_( 'DT_CARD_HOLDER_FIRSTNAME' ); ?>:<span class="required">  *  </span></td>

                    <td width="69%" align="left" > <input id="billing_firstname"  class="inputbox" type="text" name="billing_firstname" value="<?php echo isset($this->billing_firstname)?$this->billing_firstname:'' ?>" /> </td>

                 </tr>

                   <tr>

                	<td width="31%"  ><?php echo JText::_( 'DT_CARD_HOLDER_LASTNAME' ); ?>:<span class="required">  *  </span></td>

                    <td width="69%" align="left" > <input id="billing_lastname" class="inputbox" type="text" name="billing_lastname" value="<?php echo isset($this->billing_lastname)?$this->billing_lastname:'' ?>" /> </td>

                 </tr>

                   <tr>

                	<td width="31%"  ><?php echo JText::_( 'DT_BILLING_ADDRESS' ); ?>:<span class="required">  *  </span></td>

                    <td width="69%" align="left" > <input id="billing_address" class="inputbox" type="text" name="billing_address" value="<?php echo isset($dt_user->billing_address)?$dt_user->billing_address:'' ?>" /> </td>

                 </tr>

                   <tr>

                	<td width="31%"  ><?php echo JText::_( 'DT_CITY' ); ?>:<span class="required">  *  </span></td>

                    <td width="69%" align="left" ><input id="billing_city" class="inputbox" type="text" name="billing_city" value="<?php echo isset($this->billing_city)?$this->billing_city:'' ?>" />  </td>

                 </tr>

                   <tr>

                	<td width="31%"  ><?php echo JText::_( 'DT_STATE' ); ?>:<span class="required">  *  </span></td>

                    <td width="69%" align="left" > <input  id="billing_state" class="inputbox" type="text" name="billing_state" value="<?php echo isset($this->billing_state)?$this->billing_state:'' ?>" /> </td>

                 </tr>

                  <tr>

                	<td width="31%"  ><?php echo JText::_( 'DT_ZIPCODE' ); ?>:<span class="required">  *  </span></td>

                    <td width="69%" align="left" > <input id="billing_zipcode" class="inputbox" type="text" name="billing_zipcode" value="<?php echo isset($this->billing_zipcode)?$this->billing_zipcode:'' ?>" /> </td>

                 </tr>

                   <tr>

                	<td width="31%"  ><?php echo JText::_( 'DT_EMAIL' ); ?>:<span class="required">  *  </span></td>

                    <td width="69%" align="left" > <input id="billing_email" class="inputbox" type="text" name="billing_email" value="<?php echo isset($this->billing_email)?$this->billing_email:'' ?>" /> </td>

                 </tr>

                <script src="<?php echo Juri::root();  ?>/components/com_dtregister/assets/js/jquery.js" type="text/javascript"></script>

				<script language="javascript" type="text/javascript">
                    //<![CDATA[
					
          var billing_firstname = "<?php echo $this->firstname ; ?>";

					var billing_lastname = "<?php echo $this->lastname; ?>";

					var billing_address = "<?php echo $this->address; ?>";

					var billing_city = "<?php echo $this->city; ?>";

					var billing_state = "<?php echo $this->state; ?>";

					var billing_zipcode = "<?php echo $this->zip; ?>";

					var billing_email = "<?php echo $this->email; ?>";
					DTjQuery(function(){

						DTjQuery("#same").click(function(){

							if(this.checked){

								DTjQuery("#billing_firstname").val(billing_firstname);

								DTjQuery("#billing_lastname").val(billing_lastname);

								DTjQuery("#billing_address").val(billing_address);

								DTjQuery("#billing_city").val(billing_city);

								DTjQuery("#billing_state").val(billing_state);

								DTjQuery("#billing_zipcode").val(billing_zipcode);

								DTjQuery("#billing_email").val(billing_email);

							}else{

								DTjQuery("#billing_firstname").val('');

								DTjQuery("#billing_lastname").val('');

								DTjQuery("#billing_address").val('');

								DTjQuery("#billing_city").val('');

								DTjQuery("#billing_state").val('');

								DTjQuery("#billing_zipcode").val('');

								DTjQuery("#billing_email").val('');

							}

						});

					});

                     //]]>
                   </script>

                <?php 

				}else{

				?>

          <input id="billing_firstname" type="hidden" name="billing_firstname" value="<?php echo isset($this->firstname)?$this->firstname :'' ?>" /> 

         <input id="billing_lastname" type="hidden" name="billing_lastname" value="<?php echo isset($this->lastname)?$this->lastname :'' ?>" /> 

            <input id="billing_address" type="hidden" name="billing_address" value="<?php echo isset($this->address )?$this->address :'' ?>" /> 

             <input id="billing_city" type="hidden" name="billing_city" value="<?php echo isset($this->city )?$this->city :'' ?>" /> 

<input  id="billing_state"type="hidden" name="billing_state" value="<?php echo isset($this->state )?$this->state :'' ?>" /> 

<input id="billing_zipcode" type="hidden" name="billing_zipcode" value="<?php echo isset($this->zip )?$this->zip :'' ?>" />  

<input id="billing_email" type="hidden" name="billing_email" value="<?php echo isset($this->email )?$this->email :'' ?>" /> 

                <?php

				}
	   
	    return ob_get_clean();
	}
	
	function getLoginUserData(){
	   
	   global $cb_integrated ,$ptc ;
	   $database = &JFactory::getDBO();
	   $user = &JFactory::getUser();
	   $this->credircardNo = "" ;
	   $this->credircardExp = "" ;
	   $this->cardtype = "";
	   
	   if($cb_integrated ==1 ){
	       $userId=$user->id;
		   if ($userId){
		     
						//Get the information from cb

						$sql="Select * From #__comprofiler where user_id=$userId";

						$database->setQuery($sql);

						$rowProfile = $database->loadObject();

						if($rowProfile)

						{

					      $ptc->ToDecode=$rowProfile->cb_creditcardnumber;

							$ptc->Decode();

							if($ptc->Decoded!=-1)

								$this->credircardNo=$ptc->Decoded;

							$ptc->ToDecode=$rowProfile->cb_expdate;

							$ptc->Decode();

							if($ptc->Decoded!=-1)

								$this->credircardExp=$ptc->Decoded;
								
							$this->cardtype = $rowProfile->cb_cardtype ;

						}

			}
	   }
	   
	}
	
	function setBillinginfo($info){
	    
		foreach($info as $key => $value){
		    $this->$key =  $value ;
		}
		
	}
	
	function setData($data= array()){
	    
		foreach($data as $key => $value){
		   
		   $this->$key = $value ;
		   
		}
		
	}
	
	function loadmodule($method,$params=array()){
	   
	   include(JPATH_SITE.'/components/com_dtregister/lib/payment/class.'.$method.'.php');
	   
	   $this->module =  new $method();
	   call_user_func_array(array(&$this->module, $method), $params );
	   
	}
	
	function showform($options){
	    $this->setformData();
	  return  $this->drawform($options);
	   
	}
	
	function add_field($name,$value){
	  
	   $this->add_field($name,$value);
	   
	}
	
	function addItem($name, $description, $quantity, $price){
	     
		 $this->addItem($name, $description, $quantity, $price);
		 
	}
	
	function saveSession(){
	   $database = &JFactory::getDBO();
	   $sql = "INSERT INTO #__dtregister_session (`session_id` ,`data`, `user_id`)VALUES ( ".$database->Quote(session_id()).",".$database->Quote(serialize($_SESSION)).",'0')" ;

		 $database->setQuery($sql);

				$database->query();
            echo $database->getErrorMsg();
				return $database->insertid();
	}
	
	function setPaymentType($type){
	    $this->paymentType = $type ;
	}

    function beforepayment(){
	   return true ;
	}
	
	 function afterpayment(){
	   return true ;
	}
	
	function tryagain(){
	    global $Itemid ;
		
		ob_start();
		?>
         <form id="frmBack" name="frmBack" method="POST" action="index.php">

	<input type="hidden" name="try_again" value="1" />
   <?php
   if($this->paymentType=="registeration"){
   ?>
	<input type="hidden" name="eventId" value="<?php echo $this->eventId?>" />
   
   <?php } ?>

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

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

	<input type="hidden" name="task" value="<?php echo $this->component_tasks[$this->paymentType]; ?>" />

	<input type="hidden" name="type" value="<?php echo get_class($this); ?>" />

		<!-- Other field in bank account info-->
      <?php
      foreach($this->retryFields as $key=>$value){
	      
		  ?>
          <input type="hidden" name="<?php echo $key; ?> value="<?php echo $value; ?>" /> 
          <?php
		  
	  }
	  ?>
		<input type="submit" value="<?php echo JText::_( 'DT_TRY_AGAIN' ); ?>" />

	</form>
    
		<?php
		return ob_get_clean();
		
	}

}
?>