Index: uc_multiprice.module
===================================================================
--- uc_multiprice.module	(revision 380)
+++ uc_multiprice.module	(working copy)
@@ -105,7 +105,7 @@
   }
   
   // Checkout form
-  if ($form_id=='uc_cart_checkout_form') {
+  if ($form_id == 'uc_cart_checkout_form') {
     $id = uc_multiprice_country_id();
     $form['panes']['delivery']['delivery_country']['#default_value'] = $id;
     $form['panes']['delivery']['delivery_country']['#attributes'] = array('disabled' => 'disabled');  
@@ -114,6 +114,122 @@
 }
 
 /**
+ * Implementation of hook_cart_pane().
+ */
+function uc_multiprice_cart_pane($items) {
+  drupal_add_css(drupal_get_path('module', 'uc_multiprice') .'/uc_multiprice.css');
+  $panes[] = array(
+    'id' => 'multiprice_country_cart_form',
+    'title' => t('Multiprice - country switcher'),
+    'enabled' => FALSE,
+    'weight' => 0,
+    'body' => !is_null($items) ? '<div id="multiprice-country-cart-form-pane">'. drupal_get_form('uc_multiprice_region_form', TRUE) .'</div>': '',
+  );
+  return $panes;
+}
+
+/**
+ * Implementation of hook_order().
+ */
+function uc_multiprice_order($op, &$arg1, $arg2) {
+  switch ($op) {
+    case 'save':
+      uc_multiprice_save_country_to_order($arg1->order_id);
+      break;
+  }
+}
+
+function uc_multiprice_form_uc_country_formats_form_alter(&$form, &$form_state) {
+  $currencies = array();
+  $result = db_query("SELECT * FROM {uc_multiprice_currencies}");
+  while ($mpc = db_fetch_array($result)) {
+    $currencies[$mpc['country_id']] = $mpc;
+  }
+
+  $defaults = array(
+    'currency_code' => variable_get('uc_currency_code', 'USD'),
+    'sign' => variable_get('uc_currency_sign', '$'),
+    'sign_after' => variable_get('uc_sign_after_amount', FALSE),
+    'prec' => variable_get('uc_currency_prec', 2),
+    'deci' => variable_get('uc_currency_dec', '.'),
+    'thou' => variable_get('uc_currency_thou', ','),
+    'label' => TRUE,
+  );
+
+  foreach (element_children($form['countries']) as $c) {
+    $country = &$form['countries'][$c];
+
+    $currency = (array)$currencies[$c] + $defaults;
+
+    $country['currency'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Currency format'),
+      '#summary callback' => 'summarize_form',
+      '#collapsible' => TRUE,
+      '#collapsed' => TRUE,
+    );
+    $country['currency']['currency_code'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Default currency'),
+      '#description' => t('While not used directly in formatting, the currency code is used by other modules as the primary currency for your site.  Enter here your three character <a href="!url">ISO 4217</a> currency code.', array('!url' => 'http://en.wikipedia.org/wiki/ISO_4217#Active_codes')),
+      '#default_value' => $currency['currency_code'],
+      '#maxlength' => 3,
+      '#size' => 5,
+    );
+
+    $context = array(
+      'revision' => 'formatted-original',
+      'location' => 'currency-format-form',
+    );
+    $country['currency']['example'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Current format'),
+      '#value' => uc_price(1000.1234, $context, $currency),
+      '#summary' => t('Currency format: @format', array('@format' => uc_price(1000.1234, $context, $currency))),
+      '#disabled' => TRUE,
+      '#size' => 10,
+    );
+    $country['currency']['sign'] = uc_textfield(t('Currency Sign'), $currency['sign'], FALSE, NULL, 10, 10);
+    $country['currency']['sign']['#summary callback'] = 'summarize_null';
+
+    $country['currency']['sign_after'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Display currency sign after amount.'),
+      '#summary callback' => 'summarize_null',
+      '#default_value' => $currency['sign_after'],
+    );
+
+    $country['currency']['thou'] = uc_textfield(t('Thousands Marker'), $currency['thou'], FALSE, NULL, 10, 10);
+    $country['currency']['thou']['#summary callback'] = 'summarize_null';
+
+    $country['currency']['deci'] = uc_textfield(t('Decimal Marker'), $currency['deci'], FALSE, NULL, 10, 10);
+    $country['currency']['deci']['#summary callback'] = 'summarize_null';
+
+    $country['currency']['prec'] = array(
+      '#type' => 'select',
+      '#title' => t('Number of decimal places'),
+      '#options' => drupal_map_assoc(array(0, 1, 2)),
+      '#summary callback' => 'summarize_null',
+      '#default_value' => $currency['prec'],
+    );
+  }
+
+  $form['#submit'][] = 'uc_multiprice_currencies_submit';
+}
+
+function uc_multiprice_currencies_submit($form, &$form_state) {
+  foreach ($form_state['values']['countries'] as $id => $country) {
+    $currency = $country['currency'];
+    $currency['country_id'] = $id;
+
+    drupal_write_record('uc_multiprice_currencies', $currency, 'country_id');
+    if (!db_affected_rows()) {
+      drupal_write_record('uc_multiprice_currencies', $currency);
+    }
+  }
+}
+
+/**
  * Implementation of hook_nodeapi().
  */
 function uc_multiprice_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
@@ -183,6 +299,26 @@
   }
 }
 
+function uc_multiprice_uc_price_handler() {
+  return array(
+    'alter' => array(
+      'title' => t('Multiprice currency handler'),
+      'description' => t('Changes the currency of the displayed price based on its multiprice setting.'),
+      'callback' => 'uc_multiprice_price_handler_alter',
+    ),
+  );
+}
+
+function uc_multiprice_price_handler_alter(&$price, &$context, &$options) {
+  $cid = uc_multiprice_country_id();
+  $result = db_query("SELECT * FROM {uc_multiprice_currencies} WHERE country_id = %d", $cid);
+  if ($currency = db_fetch_array($result)) {
+    $currency['dec'] = $currency['deci'];
+    $options = $currency + $options;
+    unset($options['deci']);
+  }
+}
+
 /*******************************************************************************
  * Helpers and Callbacks
  ******************************************************************************/
@@ -194,6 +330,10 @@
 function uc_multiprice_country_id($country_id = FALSE) {
   if ($country_id) {
     $_SESSION['country_id'] = $country_id;
+    // If we have an order, update the country associated with it.
+    if (($order_id = intval($_SESSION['cart_order'])) > 0) {
+      uc_multiprice_save_country_to_order($order_id);
+    }
   }
   
   // Get Country by IP if not set
@@ -282,7 +422,6 @@
 
 function uc_multiprice_js() {
   if (!$_POST) {
-    print "fake";
     exit;
   }
   
@@ -316,17 +455,18 @@
   exit;
 }
 
-function uc_multiprice_region_form($form_state) {
+function uc_multiprice_region_form($form_state, $show_label = FALSE) {
   $result = db_query("SELECT * FROM {uc_countries} WHERE version > 0 ORDER BY country_name ASC");
   while ($country = db_fetch_object($result)) {
-  $countries[$country->country_id] = $country->country_name;
+    $countries[$country->country_id] = $country->country_name;
   }
 
   $form['country_id'] = array(
-  '#type' => 'select',
-  '#default_value' => uc_multiprice_country_id(),
-  '#options' => $countries,
-  '#attributes' => array('onchange' => 'javascript:this.form.submit();'),
+    '#type' => 'select',
+    '#title' => ($show_label ? t('Choose country') : ''),
+    '#default_value' => uc_multiprice_country_id(),
+    '#options' => $countries,
+    '#attributes' => array('onchange' => 'javascript:this.form.submit();'),
   );
   
   // hide the submit button
@@ -337,4 +477,19 @@
 // save selection to session
 function uc_multiprice_region_form_submit($form, &$form_state) {
   uc_multiprice_country_id($form_state['values']['country_id']);
-}
\ No newline at end of file
+}
+
+/**
+ * Save selected country id to the order.
+ */
+function uc_multiprice_save_country_to_order($order_id) {
+  // Load up the existing data array.
+  $data = db_result(db_query("SELECT data FROM {uc_orders} WHERE order_id = %d", $order_id));
+  $data = unserialize($data);
+
+  // Set the country.
+  $data['country_id'] = uc_multiprice_country_id();
+
+  // Save it again.
+  db_query("UPDATE {uc_orders} SET data = '%s' WHERE order_id = %d", serialize($data), $order_id);
+}
Index: uc_multiprice.install
===================================================================
--- uc_multiprice.install	(revision 380)
+++ uc_multiprice.install	(working copy)
@@ -8,7 +8,7 @@
 
 function uc_multiprice_schema() {
   $schema = array();
-  
+
   $schema['uc_multiprice'] = array(
     'fields' => array(
       'nid' => array(
@@ -49,7 +49,57 @@
       ),
     ),
   );
-  
+
+  $schema['uc_multiprice_currencies'] = array(
+    'fields' => array(
+      'country_id' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+      ),
+      'currency_code' => array(
+        'type' => 'char',
+        'length' => 3,
+        'not null' => TRUE,
+        'default' => '   ',
+      ),
+      'sign' => array(
+        'type' => 'varchar',
+        'length' => 15,
+        'not null',
+        'default' => '$',
+      ),
+      'sign_after' => array(
+        'type' => 'int',
+        'size' => 'tiny',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+      ),
+      'prec' => array(
+        'type' => 'int',
+        'size' => 'tiny',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 2,
+      ),
+      'deci' => array(
+        'type' => 'char',
+        'length' => 1,
+        'not null' => TRUE,
+        'default' => '.',
+      ),
+      'thou' => array(
+        'type' => 'char',
+        'length' => 1,
+        'not null' => TRUE,
+        'default' => ',',
+      ),
+    ),
+    'primary keys' => array('country_id'),
+  );
+
   return $schema;
 }
 
@@ -65,4 +115,64 @@
  */
 function uc_multiprice_uninstall() {
   drupal_uninstall_schema('uc_multiprice');
-}
\ No newline at end of file
+}
+
+function uc_multiprice_update_6001() {
+  $ret = array();
+
+  $schema = array(
+    'fields' => array(
+      'country_id' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+      ),
+      'currency_code' => array(
+        'type' => 'char',
+        'length' => 3,
+        'not null' => TRUE,
+        'default' => '   ',
+      ),
+      'sign' => array(
+        'type' => 'varchar',
+        'length' => 15,
+        'not null',
+        'default' => '$',
+      ),
+      'sign_after' => array(
+        'type' => 'int',
+        'size' => 'tiny',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+      ),
+      'prec' => array(
+        'type' => 'int',
+        'size' => 'tiny',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 2,
+      ),
+      'deci' => array(
+        'type' => 'char',
+        'length' => 1,
+        'not null' => TRUE,
+        'default' => '.',
+      ),
+      'thou' => array(
+        'type' => 'char',
+        'length' => 1,
+        'not null' => TRUE,
+        'default' => ',',
+      ),
+    ),
+    'primary keys' => array('country_id'),
+  );
+
+  db_create_table($ret, 'uc_multiprice_currencies', $schema);
+  $ret[] = update_sql("INSERT INTO {uc_multiprice_currencies} (country_id, currency_code) VALUES (840, 'USD')");
+
+  return $ret;
+}
+
Index: uc_multiprice.css
===================================================================
--- uc_multiprice.css	(revision 0)
+++ uc_multiprice.css	(revision 0)
@@ -0,0 +1,13 @@
+/* $Id$ */
+
+#multiprice-country-cart-form-pane
+{
+  border: 1px solid #bbbbbb;
+  padding: 0.5em;
+}
+
+#multiprice-country-cart-form-pane label
+{
+  margin-right: 1em;
+  display: inline;
+}
