Index: /trunk/contactcenter/inc/class.ui_data.inc.php
===================================================================
--- /trunk/contactcenter/inc/class.ui_data.inc.php	(revision 258)
+++ /trunk/contactcenter/inc/class.ui_data.inc.php	(revision 276)
@@ -182,11 +182,17 @@
 			$GLOBALS['phpgw']->template->set_var('cc_msg_expresso_default', lang('Expresso (default)'));
 			$GLOBALS['phpgw']->template->set_var('cc_msg_choose_contacts_file', lang('Select the file that contains the contacts to be imported:'));
-			$GLOBALS['phpgw']->template->set_var('cc_msg_close', lang('Close'));
+			$GLOBALS['phpgw']->template->set_var('cc_msg_close_win', lang('Close'));
 			$GLOBALS['phpgw']->template->set_var('cc_msg_ie_personal', lang('Import / Export personal Contacts'));
 			$GLOBALS['phpgw']->template->set_var('cc_msg_import_fail', lang('The importation has failed. Verify the file format.'));
 			$GLOBALS['phpgw']->template->set_var('cc_msg_importing_contacts', lang('Importing Contacts...'));
 			$GLOBALS['phpgw']->template->set_var('cc_msg_import_finished', lang('The importation has finished.'));
-			$GLOBALS['phpgw']->template->set_var('cc_msg_choose_valid_csv', lang('Select a valid CSV file to import your contacts'));
+			$GLOBALS['phpgw']->template->set_var('cc_msg_new', lang(' new'));
+			$GLOBALS['phpgw']->template->set_var('cc_msg_failure', lang(' failed'));
+			$GLOBALS['phpgw']->template->set_var('cc_msg_exists', lang(' were existent'));
+			$GLOBALS['phpgw']->template->set_var('cc_msg_show_more_info', lang('show more info'));
+			$GLOBALS['phpgw']->template->set_var('cc_msg_clean', lang('Clean'));
+			$GLOBALS['phpgw']->template->set_var('cc_msg_invalid_csv', lang('Select a valid CSV file to import your contacts'));
 			$GLOBALS['phpgw']->template->set_var('cc_msg_export_csv', lang('Select the format type that you want to export your contacts'));
+			$GLOBALS['phpgw']->template->set_var('cc_msg_automatic', lang('Automatic'));
 			$GLOBALS['phpgw']->template->set_var('cc_msg_export_error', lang('An error has occurred while the exportation.'));
 			$GLOBALS['phpgw']->template->set_var('cc_msg_new_email', lang('New Email'));
@@ -2466,5 +2472,5 @@
 			echo serialize($return);
 		}	
-
+		
 		function export_contacts($typeExport){
 
@@ -2558,5 +2564,5 @@
 				break;
 				
-				default:
+				case 'expresso':
 					$streamBuffer = "Nome,Email,Telefone\r\n";
 					foreach($all_contacts as $index => $object){
@@ -2579,9 +2585,37 @@
 		}
 
+		// Get the csv field and put into array, from php.net
+		function parse_line($input_text, $delimiter = ',', $text_qualifier = '"') {
+  			$text = trim($input_text);
+  			  if(is_string($delimiter) && is_string($text_qualifier)) {
+       			 $re_d = '\x' . dechex(ord($delimiter));            //format for regexp
+        		$re_tq = '\x' . dechex(ord($text_qualifier));    //format for regexp
+        		
+        		$fields = array();
+        		$field_num = 0;
+        		while(strlen($text) > 0) {
+            		if($text{0} == $text_qualifier) {
+                		preg_match('/^' . $re_tq . '((?:[^' . $re_tq . ']|(?<=\x5c)' . $re_tq . ')*)' . $re_tq . $re_d . '?(.*)$/', $text, $matches);
+		               
+        		        $value = str_replace('\\' . $text_qualifier, $text_qualifier, $matches[1]);
+             			$text = trim($matches[2]);
+               
+                		$fields[$field_num++] = $value;
+            		} else {
+                		preg_match('/^([^' . $re_d . ']*)' . $re_d . '?(.*)$/', $text, $matches);
+		               
+        		        $value = $matches[1];
+               			$text = trim($matches[2]);
+               
+              		  	$fields[$field_num++] = $value;
+            	}
+        	}
+        		return $fields;
+		    } else
+       			return false;
+		}
 
 		function import_contacts($typeImport){
-						
-			$delim 		= ($typeImport == 'outlook' ? ';' : ',');
-			
+
 			if($file = $_SESSION['contactcenter']['importCSV']) {				
 				unset($_SESSION['contactcenter']['importCSV']);				
@@ -2589,5 +2623,18 @@
 				$count = 0;
 				$return = array('error' => false, '_new' => 0, '_existing' => 0, '_failure' => 0);
-   				$handle = @fopen($file, "r") or die(serialize($return['error'] = true));   				   			
+   				$handle = @fopen($file, "r") or die(serialize($return['error'] = true));
+   				
+				$input_header = fgets($handle);
+				if ($typeImport == 'outlook')
+					$delim = ';';
+				else if ($typeImport == 'auto')
+				$delim = strstr($input_header,',') ? ',' : ';';
+				else  
+					$delim = ',';
+				$csv_header = $this->parse_line($input_header,$delim);
+				$firstContact = fgets($handle);
+				preg_match("/\"(.+)\"[,;]/sU",$firstContact,$matches); // yahoo csv
+   				rewind($handle);	   			
+
    				$header = @fgetcsv($handle, $len, $delim) or die(serialize($return['error'] = true));
    				if(count($header)  < 2 || count($header) > 100) {
@@ -2597,8 +2644,10 @@
    					return;
    				}
-
-				$boGroup = CreateObject('contactcenter.bo_group');
-				$boPeople = CreateObject('contactcenter.bo_people_catalog');
-			
+   				
+   				if ($matches[0][strlen($matches[0])-1] == ';')
+					$delim = ';';
+   				
+			$boGroup = CreateObject('contactcenter.bo_group');
+			$boPeople = CreateObject('contactcenter.bo_people_catalog');
 				switch($typeImport){
 					case 'outlook2000':
@@ -2617,12 +2666,39 @@
 						$phone_pos=7;
 						break;
-					default:
+					case 'expresso':
 						$name_pos=0;
 						$email_pos=1;
 						$phone_pos=2;
-				}
-				
+						break;
+					default:
+						foreach($csv_header as $index => $fieldName)
+						{
+							switch($fieldName){
+							case 'Name':
+							case 'Nome':
+							case 'First Name':
+								$name_pos = $index;
+								break;
+							case 'E-mail':
+							case 'Email':
+							case 'E-mail Address':
+							case 'Endereço de correio eletrônico':
+							case 'End. de email':
+								$email_pos = $index;
+								break;
+							case 'Home Phone':
+							case 'Phone':
+							case 'Telefone':
+							case 'Telefone principal':
+							case 'Telefone comercial':
+								$phone_pos = $index;
+								break;
+							}
+						}	
+						break;
+				}
+				
+
 				while (($data = fgetcsv($handle, $len, $delim))) {
-
        				foreach ($header as $key=>$heading)
                			$row[$heading]=(isset($data[$key])) ? $data[$key] : '';
@@ -2645,4 +2721,5 @@
 					$contact = $boGroup->verify_contact($email);
 
+					$line_iteration = $return['_failure'] + $return['_existing'] + $return['_new']; 
 					if($contact){
 						$return['_existing']++;
@@ -2650,7 +2727,9 @@
 					else if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
 						$return['_failure']++;
+						$return['_failure_status'] .= "Line: " . ($line_iteration + 2) . ", Invalid E-Mail address: " . $email ."<br>";
 					}
 					else if (!$sdata['given_names'] || !$email || !$boPeople ->quick_add($sdata)){						
 						$return['_failure']++;							
+						$return['_failure_status'] .= "Line: " . ($line_iteration + 2) . ", Invalid Name: " . $sdata['given_names'] ."<br>";
 					}
 					else
