--- phone.cr.inc	1969-12-31 18:00:00.000000000 -0600
+++ phone.cr.inc	2008-10-05 14:43:36.000000000 -0600
@@ -0,0 +1,64 @@
+<?php
+// $Id: phone.cr.inc $
+
+/**
+ * Verifies that $phonenumber is a valid eight-digit Costa Rican phone number
+ *
+ * @param string $phonenumber
+ * @return boolean Returns boolean FALSE if the phone number is not valid. 
+ */
+function valid_cr_phone_number($phonenumber) {
+
+  //$phonenumber = trim($phonenumber);
+
+  // define regular expression
+    $regex = "/(00)?[\s|-]?((\+)?[\s|-]?[0-9]{3})?[\s|-]?([0-9]{2})[\s|-]?([0-9]{2})[\s|-]?([0-9]{2})[\s|-]?([0-9]{2})[\s|-]?/";
+  	
+  // return true if valid, false otherwise
+  return (bool) preg_match($regex, $phonenumber); 
+} 
+
+/**
+ * Convert a valid Costa Rican phone number into standard (+506) 5555 55 55 format
+ * 
+ * @param $phonenumber must be a valid eight-digit number (with optional international prefix)
+ * 
+ */
+
+  /*
+    Accepts:
+        +506 88798857
+ 		+506 88-79-88-57
+        00506 88798857
+        00506 88-79-88-57
+    Rejects:
+        +506 8 8798857
+ 		+506 8 8-79-88-57
+        00506 8 8798857
+        00506 8 8-79-88-57  */
+
+function format_cr_phone_number($phonenumber) {
+
+  // define regular expression 
+    $regex = "/(00)?[\s|-]?((\+)?[\s|-]?[0-9]{3})?[\s|-]?([0-9]{2})[\s|-]?([0-9]{2})[\s|-]?([0-9]{2})[\s|-]?([0-9]{2})[\s|-]?/";
+    
+  // get digits of phone number
+  //dprint_r($matches);
+  preg_match($regex, $phonenumber, $matches);
+  
+  // construct eight-digit phone number
+  
+  //dprint_r($matches);
+  $phonenumber = $matches[4] . '-' . $matches[5] . '-' . $matches[6] . '-' . $matches[7];
+  
+  if($matches[2]){
+  	if($matches[1])
+  		$phonenumber =  "+" . $matches[2] . " " . $phonenumber;
+  	else
+  		$phonenumber =  $matches[2] . " " . $phonenumber;
+  }	
+  
+  
+  return $phonenumber;
+}
+
--- phone.module	2008-05-28 16:45:34.000000000 -0600
+++ phone.module	2008-10-05 14:43:27.000000000 -0600
@@ -21,7 +21,8 @@
     'ru_phone' => array('label' => t('Russian Phone Numbers')),
     'es_phone' => array('label' => t('Spanish Phone Numbers')),
     'au_phone' => array('label' => t('Australian Phone Numbers')),
-    'cs_phone' => array('label' => t('Czech Phone Numbers'))    
+    'cs_phone' => array('label' => t('Czech Phone Numbers')),
+    'cr_phone' => array('label' => t('Costa Rican Phone Numbers')),    
    );
 }
 
@@ -67,6 +68,7 @@
        || $field['type'] == 'es_phone'
        || $field['type'] == 'au_phone'
        || $field['type'] == 'cs_phone'
+       || $field['type'] == 'cr_phone'
        ){ 
       	$columns = array(
         	'value' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
@@ -114,7 +116,8 @@
       			'ru_phone',
       			'es_phone',
       			'au_phone',
-      			'cs_phone'      			
+      			'cs_phone',
+    			'cr_phone'        			
       			),
     ),
   );
@@ -154,7 +157,8 @@
       			'ru_phone',
       			'es_phone',
       			'au_phone',
-      			'cs_phone'      			
+      			'cs_phone',
+      			'cr_phone'        			
       			),
     ),
   );
@@ -245,7 +249,11 @@
 	        }
     	        if ($field['type'] == 'cs_phone') { 
 	          $node_field[0]['value'] = format_phone_number('cs', $node_field[0]['value'], $field);
+	        }
+    	        if ($field['type'] == 'cr_phone') { 
+	          $node_field[0]['value'] = format_phone_number('cr', $node_field[0]['value'], $field);
 	        }	        
+	        
 	      }
 	    }
 	  }
@@ -280,7 +288,10 @@
               }   
               if ($field['type'] == 'cs_phone' && !valid_phone_number('cs', $item['value'])) {
                 form_set_error($field['field_name'],t('"%value" is not a valid Czech phone number!<br>Czech phone numbers should contain only numbers and spaces be like 999 999 999 with an optional prefix of "+420" or "00420".', array('%value' => $item['value'])));
-              }                 
+              }
+	          if ($field['type'] == 'cr_phone' && !valid_phone_number('cr', $item['value'])) {
+                form_set_error($field['field_name'],t('"%value" is not a valid Costa Rican phone number!<br>Costa Rican phone numbers should contain only numbers and spaces be like 99 99 99 99 with an optional prefix of "+506" or "00506".', array('%value' => $item['value'])));
+              }                               
             }
           }
       }      
@@ -307,7 +318,8 @@
   	|| $countrycode == 'ru'
   	|| $countrycode == 'es'
   	|| $countrycode == 'au'
-  	|| $countrycode == 'cs'  	
+  	|| $countrycode == 'cs'
+  	|| $countrycode == 'cr'  	  	
   	) { 
 	
         //drupal_set_message('langue = ' . $countrycode, 'error');
@@ -347,7 +359,8 @@
   	|| $countrycode == 'ru'
   	|| $countrycode == 'es'
   	|| $countrycode == 'au'
-   	|| $countrycode == 'cs' 	
+   	|| $countrycode == 'cs'
+   	|| $countrycode == 'cr'   	 	
   	) { 
 	
         //drupal_set_message('langue = ' . $countrycode, 'error');       
--- README.txt	2008-05-28 16:45:50.000000000 -0600
+++ README.txt	2008-10-05 11:23:55.000000000 -0600
@@ -18,7 +18,8 @@
 	+ Spanish,
 	+ Czech, 
 	+ US 
-	and Canadian phone numbers 
+	and Canadian phone numbers,
+	+ Costa Rican 
 * Formating of phone numbers 
 * Option for internationalization phone numbers  
 * IPhone support 
@@ -50,7 +51,7 @@
 
 Author
 ------
-Thierry Guégan (http://www.arvoriad.com)
+Thierry Guï¿½gan (http://www.arvoriad.com)
 
 If you use this module, find it useful, and want to send the author
 a thank you note, then use the Feedback/Contact page at the URL above.
