<?php
/*
  www.oscGold.com (C) Kamelia-Net Jacek Krysiak
  modul oscGold wer 4.0
*/

  class payment {
    var $modules, $selected_module;

// class constructor
    function payment($module = '') {
      global $payment, $language, $PHP_SELF, $shipping;

      if (defined('MODULE_PAYMENT_INSTALLED') && tep_not_null(MODULE_PAYMENT_INSTALLED)) {
      require(DIR_WS_CLASSES . 'ship2pay.php');

      $my_ship2pay = new ship2pay;
      $arrship=explode('_',$shipping['id']);
      $ship2pay_mods = $my_ship2pay->get_pay_modules($arrship[0]);

      if (tep_not_null($ship2pay_mods)){
        $this->modules = explode(';', $ship2pay_mods);
      }else{
        $this->modules = explode(';', MODULE_PAYMENT_INSTALLED);
      }

        $include_modules = array();

        if ( (tep_not_null($module)) && (in_array($module . '.' . substr($PHP_SELF, (strrpos($PHP_SELF, '.')+1)), $this->modules)) ) {
          $this->selected_module = $module;

          $include_modules[] = array('class' => $module, 'file' => $module . '.php');
        } else {
          reset($this->modules);
          while (list(, $value) = each($this->modules)) {
            $class = substr($value, 0, strrpos($value, '.'));
            $include_modules[] = array('class' => $class, 'file' => $value);
          }
        }

        for ($i=0, $n=sizeof($include_modules); $i<$n; $i++) {
          include(DIR_WS_LANGUAGES . $language . '/modules/payment/' . $include_modules[$i]['file']);
          include(DIR_WS_MODULES . 'payment/' . $include_modules[$i]['file']);

          $GLOBALS[$include_modules[$i]['class']] = new $include_modules[$i]['class'];
        }

// if there is only one payment method, select it as default because in
// checkout_confirmation.php the $payment variable is being assigned the
// $HTTP_POST_VARS['payment'] value which will be empty (no radio button selection possible)
        if ( (tep_count_payment_modules() == 1) && (!isset($GLOBALS[$payment]) || (isset($GLOBALS[$payment]) && !is_object($GLOBALS[$payment]))) ) {
          $payment = $include_modules[0]['class'];
        }

        if ( (tep_not_null($module)) && (in_array($module, $this->modules)) && (isset($GLOBALS[$module]->form_action_url)) ) {
          $this->form_action_url = $GLOBALS[$module]->form_action_url;
        }
      }
    }

// class methods
/* The following method is needed in the checkout_confirmation.php page
   due to a chicken and egg problem with the payment class and order class.
   The payment modules needs the order destination data for the dynamic status
   feature, and the order class needs the payment module title.
   The following method is a work-around to implementing the method in all
   payment modules available which would break the modules in the contributions
   section. This should be looked into again post 2.2.
*/   
    function update_status() {
      if (is_array($this->modules)) {
        if (is_object($GLOBALS[$this->selected_module])) {
          if (function_exists('method_exists')) {
            if (method_exists($GLOBALS[$this->selected_module], 'update_status')) {
              $GLOBALS[$this->selected_module]->update_status();
            }
          } else { // PHP3 compatibility
            @call_user_method('update_status', $GLOBALS[$this->selected_module]);
          }
        }
      }
    }

    function javascript_validation() {
      $js = '';
      if (is_array($this->modules)) {
	    $liczba = rand(1,10000);
        $js = '<script language="javascript"><!-- ' . "\n" .
			'function close_div' . $liczba . '() {' . "\n" .			
		    'document.getElementById(\'pop_window' . $liczba . '\').innerHTML = \'\';' . "\n" .			
			'document.getElementById(\'pop_overlay\').style.display = \'none\';' . "\n" .
			'document.getElementById(\'blad\').style.display = \'none\';' . "\n" .
			'document.getElementById(\'ekran\').style.display = \'none\';' . "\n" .			
			'}' . "\n" .			
              'function check_form() {' . "\n" .
              '  var error = 0;' . "\n" .
              '  var error_message = "' . JS_ERROR . '";' . "\n" .
              '  var payment_value = null;' . "\n" .
              '  if (document.checkout_address.payment.length) {' . "\n" .
              '    for (var i=0; i<document.checkout_address.payment.length; i++) {' . "\n" .
              '      if (document.checkout_address.payment[i].checked) {' . "\n" .
              '        payment_value = document.checkout_address.payment[i].value;' . "\n" .
              '      }' . "\n" .
              '    }' . "\n" .
              '  } else if (document.checkout_address.payment.checked) {' . "\n" .
              '    payment_value = document.checkout_address.payment.value;' . "\n" .
              '  } else if (document.checkout_address.payment.value) {' . "\n" .
              '    payment_value = document.checkout_address.payment.value;' . "\n" .
              '  }' . "\n\n";

        reset($this->modules);
        while (list(, $value) = each($this->modules)) {
          $class = substr($value, 0, strrpos($value, '.'));
          if ($GLOBALS[$class]->enabled) {
            $js .= $GLOBALS[$class]->javascript_validation();
          }
        }

        $js .= "\n" . '  if (payment_value == null) {' . "\n" .
               '    error_message = error_message + "<br>' . str_replace('*','',JS_ERROR_NO_PAYMENT_MODULE_SELECTED) . '";' . "\n" .
               '    error = 1;' . "\n" .
               '  }' . "\n\n";
		
		if (CZY_PARAGON == '0' && defined('CZY_PARAGON')) {
		$js .= "\n" . '  if (document.checkout_address.paragon.disabled == true) {' . "\n" .
               '    error_message = error_message + "<br>'. str_replace(')','',str_replace('(','',FAKTURA_NIP)) .'";' . "\n" .
               '    error = 1;' . "\n" .
               '  }' . "\n\n";
		}
		
		if ((USE_POINTS_SYSTEM == 'true') && (USE_REDEEM_SYSTEM == 'true')) {
			$js .= '
			if (submitter == 1 && error == 1) {
			    wartosc_punktow = parseFloat(document.checkout_address.points_kwota.value);
				suma_zamowienia = parseFloat(document.checkout_address.suma.value);
				if (wartosc_punktow < suma_zamowienia) {
					error_message = error_message + "'.REDEEM_SYSTEM_ERROR_POINTS_NOT.'";
					error = 1;
				}' ."\n" .'
			}';
			if (tep_not_null(USE_REFERRAL_SYSTEM) && (tep_count_customer_orders() == 0)) {
			    $js .= 'reg = /^[a-zA-Z0-9ąćęłńóśżźĄĆĘŁŃÓŚŻŹ]{1,30}@[a-zA-Z0-9ąćęłńóśżźĄĆĘŁŃÓŚŻŹ]+(\.[a-zA-Z0-9ąćęłńóśżźĄĆĘŁŃÓŚŻŹ]+)+$/;' ."\n".
					 'wyn = document.checkout_address.customer_referred.value.match(reg);' ."\n".
					 'if (document.checkout_address.customer_referred.value != "") {' ."\n".
					 'if (wyn == null) { error_message = error_message + "'.REFERRAL_ERROR_NOT_VALID.'";error = 1 }	}' ."\n";
			    $js .= 'mail = document.checkout_address.mail_adres.value;' ."\n".
					 'wyn = document.checkout_address.customer_referred.value;' ."\n".
					 'if (wyn == mail) {' ."\n".
					 'error_message = error_message + "'.REFERRAL_ERROR_SELF.'";error = 1 }' ."\n";					 
			}
		}
		
	    $okno = '
			document.getElementById(\'blad\').style.display=\'block\';
			document.getElementById(\'pop_overlay\').style.display = \'block\';
			document.getElementById(\'ekran\').style.display = \'block\';
			document.getElementById(\'ekran\').innerHTML += unescape("<div id=\'pop_window' . $liczba . '\' style=\'display:block;position:absolute; top:210px; left:50%; margin-left:-175px; font-size:20px; z-index:9999;\'>"+
			   "<table style=\'background-color:#ffffff;border:1px solid #000000;\' width=\'350px\' cellpadding=\'5\' cellspacing=\'0\'><tr><td style=\'background-color: #333344;border-bottom: 2px solid #000000;border-top: 1px solid #78A3F2;border-left: 1px solid #78A3F2;border-right: 1px solid #204095;\' align=\'center\'>"+
			   "<table width=\'98%\' border=\'0\' cellpadding=\'0\' cellspacing=\'0\'><tr><td width=\'50%\' align=\'left\' style=\'font: bold 13px tahoma,arial, sans-serif; color:#ffffff\'>'.TEXT_UWAGA.'<%2Ftd><td width=\'50%\' align=\'right\'>"+
			   "<img src=\'popupbox/close.gif\' border=\'0\' style=\'cursor:pointer\' onclick=\'javascript:close_div' . $liczba . '();\'><%2Ftd><%2Ftr><%2Ftable><%2Ftd><%2Ftr><tr><td>"+				
			   "<center><p style=\'font: bold 17px tahoma,arial, sans-serif; color:#FF0000\'>'.TEXT_KOMUNIKAT.'<%2Fp><%2Fcenter>"+				
			   "<center><p style=\'font: normal 11px tahoma,arial, sans-serif; color:#616161\'>"+error_message+"<%2Fp><%2Fcenter><br>&nbsp;"+			
			   "<input type=\'button\' value=\''.TEXT_ZAMKNIJ.'\' onclick=\'javascript:close_div' . $liczba . '();\'>"+
			   "<%2Ftd><%2Ftr><%2Ftable><%2Fdiv>")';		
		
        //$js .= '  if (error == 1 && submitter != 1) {' . "\n" .
		$js .= '  if (error == 1) {' . "\n" .
               '    '.$okno. "\n" .
               '    return false;' . "\n" .
               '  } else {' . "\n" .
               '    return true;' . "\n" .
               '  }' . "\n" .
               '}' . "\n" .
               '//--></script>' . "\n";
      }

      return $js;
    }

    function checkout_initialization_method() {
      $initialize_array = array();

      if (is_array($this->modules)) {
        reset($this->modules);
        while (list(, $value) = each($this->modules)) {
          $class = substr($value, 0, strrpos($value, '.'));
          if ($GLOBALS[$class]->enabled && method_exists($GLOBALS[$class], 'checkout_initialization_method')) {
            $initialize_array[] = $GLOBALS[$class]->checkout_initialization_method();
          }
        }
      }

      return $initialize_array;
    }

		function selection() {
      $selection_array = array();

      if (is_array($this->modules)) {
        reset($this->modules);
        while (list(, $value) = each($this->modules)) {
          $class = substr($value, 0, strrpos($value, '.'));
          if ($GLOBALS[$class]->enabled) {
            $selection = $GLOBALS[$class]->selection();
            if (is_array($selection)) $selection_array[] = $selection;
          }
        }
      }

      return $selection_array;
    }

    function pre_confirmation_check() {
      if (is_array($this->modules)) {
        if (is_object($GLOBALS[$this->selected_module]) && ($GLOBALS[$this->selected_module]->enabled) ) {
          $GLOBALS[$this->selected_module]->pre_confirmation_check();
        }
      }
    }

    function confirmation() {
      if (is_array($this->modules)) {
        if (is_object($GLOBALS[$this->selected_module]) && ($GLOBALS[$this->selected_module]->enabled) ) {
          return $GLOBALS[$this->selected_module]->confirmation();
        }
      }
    }

    function process_button() {
      if (is_array($this->modules)) {
        if (is_object($GLOBALS[$this->selected_module]) && ($GLOBALS[$this->selected_module]->enabled) ) {
          return $GLOBALS[$this->selected_module]->process_button();
        }
      }
    }

    function before_process() {
      if (is_array($this->modules)) {
        if (is_object($GLOBALS[$this->selected_module]) && ($GLOBALS[$this->selected_module]->enabled) ) {
          return $GLOBALS[$this->selected_module]->before_process();
        }
      }
    }

    function after_process() {
      if (is_array($this->modules)) {
        if (is_object($GLOBALS[$this->selected_module]) && ($GLOBALS[$this->selected_module]->enabled) ) {
          return $GLOBALS[$this->selected_module]->after_process();
        }
      }
    }

    function get_error() {
      if (is_array($this->modules)) {
        if (is_object($GLOBALS[$this->selected_module]) && ($GLOBALS[$this->selected_module]->enabled) ) {
          return $GLOBALS[$this->selected_module]->get_error();
        }
      }
    }
  }
?>
