context: using System;
  using System.IO;
  using System.Xml;
  using System.Collections.Generic;
  using System.Reflection;
  
  using log4net;
  using comexbase;
  
  namespace monosimbase
  {
  	
  	
  	public static partial class GlobalObjUI
  	{
  		
  		// Attributes
  		private static string simCommand = "";
  		private static string simResponse = "";
  		private static string simExpResponse = "";
  		private static bool simRespOk = false;
  
  		private static List<string> ADNrecords = new List<string>();
  		
  		
  		
  		// Events
  		public static event MonosimEventHandler MonosimEvent;
  		
  		
  		#region Public Methods
  		
  		
  		
  		
  		
  		
  		/// <summary>
  		/// Select sim contacts list.
  		/// </summary>
  		public static string SelectSimContactsList()
  		{
  			string retCmd = GetSimPinStatus();
  			
  			if (retCmd != "")
  			{
  				// error detected
  				return retCmd;
  			}
  			
  			if (GlobalObjUI.SimPin1Status)
  			{
  				// Pin1 enabled, should be disabled
  				return lMan.GetString("needpindisable");
  			}
  			
  			
  			retCmd = ReadIccId();
  			
  			if (retCmd != "")
  			{
  				// error detected
  				return retCmd;
  			}
  			
  			retCmd = ReadADN();
  			
  			if (retCmd != "")
  			{
  				// error detected
  				return retCmd;
  			}			
  			
  			
  			return "";
  		}
  		
  		
  		
  		
  		/// <summary>
  		/// Read sim contacts list.
  		/// </summary>
  		public static void ReadSimContactsList()
  		{
  			string emptyRecord = new string('F', SimADNRecordLen * 2) + "9000";
  			string expRecord = new string('?', SimADNRecordLen * 2) + "9000";
  			string retCmd = "";
  			
  			SimADNRecordNoEmpty = 0;
  			ADNrecords = new List<string>();
  			SimADNRecordEmptyID = new List<int>();
  			SimContacts = new Contacts();
  			
  			// loop for each ADN records
  			for (int l=1; l<=SimADNRecordCount; l++)
  			{
  				// Set record id
  				SimADNPosition = l;
  				
  				// Prepare sim command
  				simCommand = "A0B2" + l.ToString("X2") + "04" + SimADNRecordLen.ToString("X2");
  				simExpResponse = expRecord;
  				simResponse = "";
  				simRespOk = false;
  				retCmd = SendReceiveAdv(simCommand, ref simResponse, simExpResponse, ref simRespOk);
  				log.Debug("GlobalObjUI.Sim::ReadSimContactsList: READ ADN REC " + 
  					      l.ToString("d3") + " " + simResponse);
  				
  				if (retCmd != "")
  				{
  					// error detected
  					SimADNError = retCmd;
  					SimADNStatus = 3;
  					// send notify to gui
  					MonosimEvent(new object(), new EventArgs());
  					return;
  				}
  				
  				
  				if (!simRespOk)
  				{
  					// wrong response
  					SimADNError = "WRONG RESPONSE: [" + simExpResponse + "] - [" + simResponse + "]";
  					SimADNStatus = 3;
  					// send notify to gui
  					MonosimEvent(new object(), new EventArgs());
  					return;
  				}
  				
  				
  				// check for not empty records
  				if (simResponse != emptyRecord)
  				{
  					// increment counter of not empty records
  					SimADNRecordNoEmpty++;
  					
  					// update records list
  					ADNrecords.Add(simResponse.Substring(0, simResponse.Length-4));
  				}
  				else
  				{
  					// Add to Empty record list
  					SimADNRecordEmptyID.Add(l);
  				}
  				
  				// send notify to gui
  				MonosimEvent(new object(), new EventArgs());				
  			}
  			
  			
  			SimADNStatus = 2;
  			
  			// send notify to gui
  			MonosimEvent(new object(), new EventArgs());			
  			
  		}
  		
  		
  		
  		
  		
  		public static void WriteSimContactsList(Contacts contacts, bool isAppend)
  		{
  			string emptyRecord = new string('F', SimADNRecordLen * 2);
  			string expRecord = "9000";
  			string retCmd = "";
  			string retRecord = "";
  			int adnRec = 0;
  			
  			// loop for each ADN records
  			for (int cid=0; cid<contacts.SimContacts.Count; cid++)
  			{
  				if (!isAppend)
  				{
  					// first record
  					adnRec = cid + 1;
  				}
  				else
  				{
  					// first empty record
  					adnRec = SimADNRecordEmptyID[cid];
  				}
  				
  				// Set record id
  				SimADNPosition = adnRec;
  				
  				// Prepare sim command
  				simCommand = "A0DC" + adnRec.ToString("X2") + "04" + SimADNRecordLen.ToString("X2");
  					
  				retStr = PrepareRecord(contacts.SimContacts[cid], out retRecord);
  				if (retStr != "")
  				{
  					// error detected
  					SimADNError = retStr;
  					SimADNStatus = 3;
  					// send notify to gui
  					MonosimEvent(new object(), new EventArgs());
  					return;
  				}
  				
  				simCommand += retRecord;
  				simExpResponse = expRecord;
  				simResponse = "";
  				simRespOk = false;
  				retCmd = SendReceiveAdv(simCommand, ref simResponse, simExpResponse, ref simRespOk);
  				
  				log.Debug("GlobalObjUI.Sim::WriteSimContactsList: WRITE ADN REC " + 
  					      adnRec.ToString("d3") + " " + simCommand + " < " + simResponse);
  				
  				if (retCmd != "")
  				{
  					// error detected
  					SimADNError = retCmd;
  					SimADNStatus = 3;
  					// send notify to gui
  					MonosimEvent(new object(), new EventArgs());
  					return;
  				}
  				
  				
  				if (!simRespOk)
  				{
  					// wrong response
  					SimADNError = "WRONG RESPONSE: [" + simExpResponse + "] - [" + simResponse + "]";
  					SimADNStatus = 3;
  
  					// send notify to gui
  					MonosimEvent(new object(), new EventArgs());
  					return;
  				}
  				
  				// send notify to gui
  				MonosimEvent(new object(), new EventArgs());
  			}
  			
  			if(!isAppend)
  			{
  				// delete old other records
  				for (int rid=adnRec+1; rid<=GlobalObjUI.SimADNRecordCount; rid++)
  				{
  					if (!SimADNRecordEmptyID.Contains(rid))
  					{
  						// Set record id
  						SimADNPosition = rid;
  						
  						simCommand = "A0DC" + rid.ToString("X2") + "04" + SimADNRecordLen.ToString("X2") +
  							         emptyRecord;
  						simExpResponse = expRecord;
  						simResponse = "";
  						simRespOk = false;
  						retCmd = SendReceiveAdv(simCommand, ref simResponse, simExpResponse, ref simRespOk);
  						
  						log.Debug("GlobalObjUI.Sim::WriteSimContactsList: WRITE ADN REC " + 
  						      rid.ToString("d3") + " " + simCommand + " < " + simResponse);
  					
  
gt: 						if (retCmd != "")