Index: uc_vat_number.module
===================================================================
--- uc_vat_number.module	(revision 14)
+++ uc_vat_number.module	(working copy)
@@ -19,7 +19,7 @@
  * Implementation of hook_perm()
  */
 function uc_vat_number_perm() {
-  return array('Have to fill VAT number');
+  return array('Have to fill VAT number', 'Is allowed to fill VAT number');
 }
 
 /**
@@ -50,7 +50,7 @@
   // Alter the checkout form to ask for the Customer VAT number
   if($form_id == 'uc_cart_checkout_form') {
     global $user;
-    if(!user_access('Have to fill VAT number')) {
+    if(!user_access('Have to fill VAT number') && !user_access('Is allowed to fill VAT number')) {
       return;
     }
     if(is_numeric($_SESSION['cart_order'])) {
@@ -58,21 +58,21 @@
       $order_data = unserialize($order_data);
     }
 
-    drupal_add_js(drupal_get_path('module', 'uc_vat_number') .'/uc_vat_number.js');
+    // DEBUG drupal_add_js(drupal_get_path('module', 'uc_vat_number') .'/uc_vat_number.js');
     // Trigger a fake change action to force VAT Number refresh
-    drupal_add_js('$(document).ready(function() { $("#edit-panes-billing-billing-country").change(); });', 'inline');
+    // DEBUG drupal_add_js('$(document).ready(function() { $("#edit-panes-billing-billing-country").change(); });', 'inline');
 
-    $form['panes']['billing']['billing_company']['#required'] = true;
+    $form['panes']['billing']['billing_company']['#required'] = user_access('Have to fill VAT number');
     $form['#validate'][] = 'uc_vat_number_checkout_validate';
 
     $form['panes']['billing']['billing_vat_number'] = array(
       '#type' => 'textfield',
       '#title' => t('VAT Number'),
-      '#description' => t('Required for professionnal customers in EU (eg: FR0123456789).'),
+      '#description' => t('Required for professional customers in EU (eg: BE0123456789).'),
       '#size' => 32,
       '#maxlength' => 32,
       '#weight' => 1,
-      '#required' => false,
+      '#required' => user_access('Have to fill VAT number'),
       '#default_value' => $order_data['vat_number'],
     );
   }
@@ -125,7 +125,7 @@
 			break;
 
     case 'uc_order':
-			if (user_access('Have to fill VAT number')) {
+			if (user_access('Have to fill VAT number') || user_access('Is allowed to fill VAT number')) {
       	$order = $object;
 				$values['order-vat-number'] = $order->vat_number['source'];
 			}
@@ -190,7 +190,8 @@
 	Slovaquie
 	Slovnie*/
 	// Skip validation if the customer is not in EU country.
-  $european_countries = array(276, 040, 056, 208, 724, 246, 250, 300, 372, 380, 442, 528, 620, 826, 752, 196, 233, 348, 428, 440, 470, 616, 203, 703, 705);
+
+  $european_countries = array(276, 40, 56, 208, 724, 246, 250, 300, 372, 380, 442, 528, 620, 826, 752, 196, 233, 348, 428, 440, 470, 616, 203, 703, 705);
   if(!in_array($form_state['values']['panes']['billing']['billing_country'], $european_countries)) {
    return;
   }
@@ -203,15 +204,25 @@
     $countryCode = substr($vat_number, 0, 2);
     $vatNumber = substr($vat_number, 2);
 
-    if(strlen($countryCode) != 2 or is_numeric(substr($countryCode, 0, 1)) or is_numeric(substr($countryCode, 1, 2)) or !is_numeric($vatNumber)) {
+    if(strlen($countryCode) != 2 or is_numeric(substr($countryCode, 0, 1)) or is_numeric(substr($countryCode, 1, 2))) {
       form_set_error('panes][billing][billing_vat_number', t('Your VAT Number syntax is not correct. You should have something like that : FR0123456789'));
       return;
     }
     $client = new SoapClient("http://ec.europa.eu/taxation_customs/vies/api/checkVatPort?wsdl");
-    $params = array('countryCode' => $countryCode, 'vatNumber' => $vatNumber);
-    $result = $client->checkVat($params);
-    if(!$result->valid) {
-      form_set_error('panes][billing][billing_vat_number', t('VAT Number verification failed, check it\'s validity on <a href="http://ec.europa.eu/taxation_customs/vies/vieshome.do">this website</a>'));
+    $params = array('countryCode' => drupal_strtoupper($countryCode), 'vatNumber' => $vatNumber);
+
+    try {
+      $result = $client->checkVat($params);
+      if(!$result->valid) {
+        form_set_error('panes][billing][billing_vat_number', t('VAT Number verification failed, check it\'s validity on <a href="http://ec.europa.eu/taxation_customs/vies/vieshome.do">this website</a>'));
+      }
+    } catch(Exception $e) {
+      if ($e->getMessage() == "{ 'INVALID_INPUT' }") {
+        form_set_error('panes][billing][billing_vat_number', t('VAT Number verification failed, check it\'s validity on <a href="http://ec.europa.eu/taxation_customs/vies/vieshome.do">this website</a>'));
+      }
+      else {
+        form_set_error('panes][billing][billing_vat_number', $e->getCode() . '::' . $e->getMessage());
+      }
     }
   }
 }
\ No newline at end of file
