<?php

function getCurrency($order_id)
{
$db = new ps_DB;

$q = "SELECT order_currency FROM #__vm_orders where order_id = " . $order_id;
$db->query($q);
	if ($db->next_record()) {
		$currency_code = $db->f("order_currency");
	}
return $currency_code;
}

function getCountryCode($userCountryCode3)
{
$db = new ps_DB;

$q = "SELECT country_2_code FROM #__vm_country WHERE country_3_code='".$userCountryCode3."' ORDER BY country_2_code ASC";
$db->query($q);
	if ($db->next_record()) {
		$country_code = $db->f("country_2_code");
	}
return $country_code;
}

function getLanguageCode($country_code)
{
	$country_code = strtoupper($country_code);
	switch ($country_code) {
		case 'CZ':
			return 'cs';
		case 'DK':
			return 'da';
		case 'DE':
			return 'de';
		case 'GB':
			return 'en';
		case 'ES':
			return 'es';
		case 'FI':
			return 'fi';
		case 'FR':
			return 'fr';
		case 'IT':
			return 'it';
		case 'NL':
			return 'nl';
		case 'NO':
			return 'no';
		case 'PL':
			return 'pl';
		case 'PT':
			return 'pt';
		case 'SE':
			return 'sv';
		case 'TR':
			return 'tr';
		default:
			return 'en';
	}
}

// Get user shipping information
$dbst =& new ps_DB;
$qt = "SELECT * FROM #__vm_user_info WHERE user_info_id='".$db->f("user_info_id")."' AND address_type='ST'";
$dbst->query($qt);

if(!$dbst->next_record()) {
	$dbst = $dbbt;
}


$url = CAB_PREMIUM_URL;

$total_price = $db->f('order_total');
$cab_price = round( $total_price, 2) * 100;

if($cab_price < 100) {	
	if($cab_price < 10)  $cab_price = "00".$cab_price;
	else $cab_price = "0".$cab_price;
}

$currency_code = getCurrency($order_id);

$cb_billing_FirstName = utf8_decode($dbbt->f('first_name'));
$cb_billing_LastName = utf8_decode($dbbt->f('last_name'));
$cb_billing_Street = utf8_decode($dbbt->f('address_1'));
$cb_billing_City = utf8_decode($dbbt->f('city'));
$cb_billing_ZIP = $dbbt->f('zip');
$country_code_billing = getCountryCode($user->country);
$language_code = getLanguageCode($country_code_billing);

$cb_shipping_FirstName = utf8_decode($dbst->f("first_name"));
$cb_shipping_LastName = utf8_decode($dbst->f("last_name"));
$cb_shipping_Street = utf8_decode($dbst->f("address_1"));
$cb_shipping_City = utf8_decode($dbst->f("city"));
$cb_shipping_ZIP = $dbst->f("zip");
$country_code_shipping = getCountryCode($dbst->f("country"));

$post_variables = Array(
	"price"=>$cab_price,
	"cb_currency"=>$currency_code,
	"cb_content_name_utf"=>$vendor_store_name,
	"cb_content_info_utf"=>"Order ". $db->f("order_id"),
	"externalBDRID"=>$db->f("order_number"),
	"cb_billing_Nation"=>$country_code_billing,
	"lang"=>$language_code,
	//Account detail
	"Email"=>$dbbt->f('user_email'),
	//Billing information
	"cb_billing_FirstName"=>$cb_billing_FirstName,
	"cb_billing_LastName"=>$cb_billing_LastName,
	"cb_billing_Street"=>$cb_billing_Street,
	"cb_billing_City"=>$cb_billing_City,
	"cb_billing_ZIP"=>$cb_billing_ZIP,
	//Shipping information
	"cb_shipping_FirstName"=>$cb_shipping_FirstName,
	"cb_shipping_LastName"=>$cb_shipping_LastName,
	"cb_shipping_Street"=>$cb_shipping_Street,
	"cb_shipping_City"=>$cb_shipping_City,
	"cb_shipping_ZIP"=>$cb_shipping_ZIP,
	"cb_shipping_Nation"=>$country_code_shipping
);

if ($cb_billing_Nation == "GB") $cb_billing_Nation = "UK";
if ($cb_shipping_Nation == "GB") $cb_shipping_Nation = "UK";

if( $page == "checkout.thankyou" ) {
$query_string = "?";
foreach( $post_variables as $name => $value ) {
$query_string .= $name. "=" . urlencode($value) ."&";
}
vmRedirect( $url . $query_string );
} else {
echo '<form action="'.$url.'" method="post" target="_blank">';
echo '<input type="image" name="submit" src="https://eu.clickandbuy.com/images/all/logos/powredby_logo_en.gif" border="0" alt="Click to pay with ClickandBuy - it is fast, free and secure!" />';

foreach( $post_variables as $name => $value ) {
echo '<input type="hidden" name="'.$name.'" value="'.htmlspecialchars($value).'" />';
}
echo '</form>';

}
?>