using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.Net;
using System.Timers;
using System.Threading;

using TrackingController;       //controller 
//using SerialPort;          // serial port communication API

namespace client
{
	/// <summary>
	/// Summary description for ClientForm.
	/// </summary>
	
	// Delegate declaration.
	public delegate void MessageHandler(SerialPortEventArgs e);
	
	public class ClientForm : System.Windows.Forms.Form
	{
		private	TrackeeManagerServant m_manager = null;
		private TcpClientChannel m_clientChannel = null;
		
		private System.IO.StreamReader m_file = null;
		private SerialPort m_serialPort =null;
		private string m_channelName;
		private Thread m_thread=null;         //a thread listens serial port    
		public bool m_stop = true;     
       
		// Create an event 
		// It has the form public event delegateName eventName
		public event MessageHandler MessageReadyEvent;

		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.TextBox portTextBox;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox addressTextBox;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.CheckBox httpCheckBox;
		private System.Windows.Forms.CheckBox tcpCheckBox;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Button startbutton;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.RadioButton disconnectButton;
		private System.Windows.Forms.RadioButton connectButton;
		private System.Windows.Forms.Button stopbutton;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.ComboBox comboBox1;
		private System.Windows.Forms.ComboBox comboBox2;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.ComboBox comboBox3;
		private System.Windows.Forms.ComboBox comboBox4;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.ComboBox comboBox5;
		private System.ComponentModel.IContainer components;

		public ClientForm()
		{
			InitializeComponent();

			//get the IPaddress from the DNS
			IPHostEntry hostInfo = Dns.GetHostByName(Dns.GetHostName());
			string ipaddress = null;
			for (int i=0 ; i < hostInfo.AddressList.Length; i++)
				if (ipaddress != null)
					ipaddress+=";"+hostInfo.AddressList[i].ToString();
				else
					ipaddress = hostInfo.AddressList[i].ToString();
			//set the ip address Textbox
			this.addressTextBox.Text = ipaddress;
			MessageReadyEvent = new MessageHandler(OnSerialMessageReady);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ClientForm));
			this.startbutton = new System.Windows.Forms.Button();
			this.panel1 = new System.Windows.Forms.Panel();
			this.disconnectButton = new System.Windows.Forms.RadioButton();
			this.connectButton = new System.Windows.Forms.RadioButton();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.httpCheckBox = new System.Windows.Forms.CheckBox();
			this.tcpCheckBox = new System.Windows.Forms.CheckBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.portTextBox = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.addressTextBox = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.comboBox5 = new System.Windows.Forms.ComboBox();
			this.label9 = new System.Windows.Forms.Label();
			this.comboBox4 = new System.Windows.Forms.ComboBox();
			this.comboBox3 = new System.Windows.Forms.ComboBox();
			this.label8 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.comboBox2 = new System.Windows.Forms.ComboBox();
			this.comboBox1 = new System.Windows.Forms.ComboBox();
			this.label6 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.panel2 = new System.Windows.Forms.Panel();
			this.label7 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.stopbutton = new System.Windows.Forms.Button();
			this.panel1.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.panel2.SuspendLayout();
			this.SuspendLayout();
			// 
			// startbutton
			// 
			this.startbutton.Enabled = false;
			this.startbutton.Location = new System.Drawing.Point(552, 144);
			this.startbutton.Name = "startbutton";
			this.startbutton.Size = new System.Drawing.Size(104, 23);
			this.startbutton.TabIndex = 7;
			this.startbutton.Text = "Start Collection";
			this.startbutton.Click += new System.EventHandler(this.startbutton_Click);
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.disconnectButton);
			this.panel1.Controls.Add(this.connectButton);
			this.panel1.Controls.Add(this.pictureBox1);
			this.panel1.Controls.Add(this.groupBox1);
			this.panel1.Controls.Add(this.groupBox2);
			this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
			this.panel1.Location = new System.Drawing.Point(0, 0);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(680, 120);
			this.panel1.TabIndex = 8;
			// 
			// disconnectButton
			// 
			this.disconnectButton.Checked = true;
			this.disconnectButton.Location = new System.Drawing.Point(528, 72);
			this.disconnectButton.Name = "disconnectButton";
			this.disconnectButton.Size = new System.Drawing.Size(144, 24);
			this.disconnectButton.TabIndex = 13;
			this.disconnectButton.TabStop = true;
			this.disconnectButton.Text = "Disconnect from Server";
			this.disconnectButton.CheckedChanged += new System.EventHandler(this.disconnectButton_CheckedChanged);
			// 
			// connectButton
			// 
			this.connectButton.Location = new System.Drawing.Point(528, 32);
			this.connectButton.Name = "connectButton";
			this.connectButton.Size = new System.Drawing.Size(120, 24);
			this.connectButton.TabIndex = 12;
			this.connectButton.Text = "Connect to Server";
			this.connectButton.CheckedChanged += new System.EventHandler(this.connectButton_CheckedChanged);
			// 
			// pictureBox1
			// 
			this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
			this.pictureBox1.Location = new System.Drawing.Point(488, 24);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(24, 24);
			this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
			this.pictureBox1.TabIndex = 8;
			this.pictureBox1.TabStop = false;
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.httpCheckBox);
			this.groupBox1.Controls.Add(this.tcpCheckBox);
			this.groupBox1.Location = new System.Drawing.Point(344, 16);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(128, 88);
			this.groupBox1.TabIndex = 7;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Channle Type";
			// 
			// httpCheckBox
			// 
			this.httpCheckBox.Location = new System.Drawing.Point(16, 56);
			this.httpCheckBox.Name = "httpCheckBox";
			this.httpCheckBox.Size = new System.Drawing.Size(104, 16);
			this.httpCheckBox.TabIndex = 1;
			this.httpCheckBox.Text = "HTTP Channle";
			// 
			// tcpCheckBox
			// 
			this.tcpCheckBox.Checked = true;
			this.tcpCheckBox.CheckState = System.Windows.Forms.CheckState.Checked;
			this.tcpCheckBox.Location = new System.Drawing.Point(16, 24);
			this.tcpCheckBox.Name = "tcpCheckBox";
			this.tcpCheckBox.Size = new System.Drawing.Size(96, 16);
			this.tcpCheckBox.TabIndex = 0;
			this.tcpCheckBox.Text = "TCP Channel";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.portTextBox);
			this.groupBox2.Controls.Add(this.label2);
			this.groupBox2.Controls.Add(this.addressTextBox);
			this.groupBox2.Controls.Add(this.label1);
			this.groupBox2.Location = new System.Drawing.Point(8, 16);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(320, 88);
			this.groupBox2.TabIndex = 6;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Server";
			// 
			// portTextBox
			// 
			this.portTextBox.Location = new System.Drawing.Point(72, 56);
			this.portTextBox.Name = "portTextBox";
			this.portTextBox.Size = new System.Drawing.Size(232, 20);
			this.portTextBox.TabIndex = 3;
			this.portTextBox.Text = "4000";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 56);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(32, 23);
			this.label2.TabIndex = 2;
			this.label2.Text = "Port:";
			// 
			// addressTextBox
			// 
			this.addressTextBox.Location = new System.Drawing.Point(72, 24);
			this.addressTextBox.Name = "addressTextBox";
			this.addressTextBox.Size = new System.Drawing.Size(232, 20);
			this.addressTextBox.TabIndex = 1;
			this.addressTextBox.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(56, 23);
			this.label1.TabIndex = 0;
			this.label1.Text = "Address:";
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.Add(this.comboBox5);
			this.groupBox3.Controls.Add(this.label9);
			this.groupBox3.Controls.Add(this.comboBox4);
			this.groupBox3.Controls.Add(this.comboBox3);
			this.groupBox3.Controls.Add(this.label8);
			this.groupBox3.Controls.Add(this.label5);
			this.groupBox3.Controls.Add(this.comboBox2);
			this.groupBox3.Controls.Add(this.comboBox1);
			this.groupBox3.Controls.Add(this.label6);
			this.groupBox3.Controls.Add(this.label3);
			this.groupBox3.Location = new System.Drawing.Point(8, 136);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(528, 144);
			this.groupBox3.TabIndex = 9;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "Serial Port:";
			// 
			// comboBox5
			// 
			this.comboBox5.Items.AddRange(new object[] {
														   "1",
														   "1.5",
														   "2"});
			this.comboBox5.Location = new System.Drawing.Point(152, 112);
			this.comboBox5.Name = "comboBox5";
			this.comboBox5.Size = new System.Drawing.Size(96, 21);
			this.comboBox5.Sorted = true;
			this.comboBox5.TabIndex = 17;
			this.comboBox5.Text = "1";
			// 
			// label9
			// 
			this.label9.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label9.Location = new System.Drawing.Point(24, 112);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(80, 23);
			this.label9.TabIndex = 16;
			this.label9.Text = "StopBits:";
			// 
			// comboBox4
			// 
			this.comboBox4.Items.AddRange(new object[] {
														   "Even",
														   "Mark",
														   "None",
														   "Odd",
														   "Space"});
			this.comboBox4.Location = new System.Drawing.Point(400, 72);
			this.comboBox4.Name = "comboBox4";
			this.comboBox4.Size = new System.Drawing.Size(96, 21);
			this.comboBox4.Sorted = true;
			this.comboBox4.TabIndex = 15;
			this.comboBox4.Text = "None";
			// 
			// comboBox3
			// 
			this.comboBox3.Items.AddRange(new object[] {
														   "7",
														   "8"});
			this.comboBox3.Location = new System.Drawing.Point(400, 32);
			this.comboBox3.Name = "comboBox3";
			this.comboBox3.Size = new System.Drawing.Size(96, 21);
			this.comboBox3.Sorted = true;
			this.comboBox3.TabIndex = 14;
			this.comboBox3.Text = "8";
			// 
			// label8
			// 
			this.label8.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label8.Location = new System.Drawing.Point(328, 72);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(64, 23);
			this.label8.TabIndex = 13;
			this.label8.Text = "Parity:";
			// 
			// label5
			// 
			this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label5.Location = new System.Drawing.Point(328, 32);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(64, 23);
			this.label5.TabIndex = 12;
			this.label5.Text = "ByteSize:";
			// 
			// comboBox2
			// 
			this.comboBox2.Items.AddRange(new object[] {
														   "1200",
														   "19200",
														   "2400",
														   "4800",
														   "9600"});
			this.comboBox2.Location = new System.Drawing.Point(152, 72);
			this.comboBox2.Name = "comboBox2";
			this.comboBox2.Size = new System.Drawing.Size(96, 21);
			this.comboBox2.Sorted = true;
			this.comboBox2.TabIndex = 11;
			this.comboBox2.Text = "9600";
			// 
			// comboBox1
			// 
			this.comboBox1.Items.AddRange(new object[] {
														   "COM1",
														   "COM2",
														   "COM3",
														   "COM4",
														   "COM5"});
			this.comboBox1.Location = new System.Drawing.Point(152, 32);
			this.comboBox1.Name = "comboBox1";
			this.comboBox1.Size = new System.Drawing.Size(96, 21);
			this.comboBox1.Sorted = true;
			this.comboBox1.TabIndex = 10;
			this.comboBox1.Text = "COM1";
			// 
			// label6
			// 
			this.label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label6.Location = new System.Drawing.Point(24, 72);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(120, 23);
			this.label6.TabIndex = 8;
			this.label6.Text = "Speed (BaudRate):";
			// 
			// label3
			// 
			this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label3.Location = new System.Drawing.Point(24, 32);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(128, 23);
			this.label3.TabIndex = 2;
			this.label3.Text = "Serial Port Number:";
			// 
			// panel2
			// 
			this.panel2.Controls.Add(this.label7);
			this.panel2.Controls.Add(this.label4);
			this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.panel2.Location = new System.Drawing.Point(0, 293);
			this.panel2.Name = "panel2";
			this.panel2.Size = new System.Drawing.Size(680, 64);
			this.panel2.TabIndex = 10;
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(88, 16);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(576, 40);
			this.label7.TabIndex = 2;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(16, 16);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(64, 16);
			this.label4.TabIndex = 1;
			this.label4.Text = "Message:";
			// 
			// stopbutton
			// 
			this.stopbutton.Enabled = false;
			this.stopbutton.Location = new System.Drawing.Point(552, 176);
			this.stopbutton.Name = "stopbutton";
			this.stopbutton.Size = new System.Drawing.Size(104, 23);
			this.stopbutton.TabIndex = 11;
			this.stopbutton.Text = "Stop Collection";
			this.stopbutton.Click += new System.EventHandler(this.stopbutton_Click);
			// 
			// ClientForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(680, 357);
			this.Controls.Add(this.stopbutton);
			this.Controls.Add(this.panel2);
			this.Controls.Add(this.groupBox3);
			this.Controls.Add(this.panel1);
			this.Controls.Add(this.startbutton);
			this.Name = "ClientForm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Tracking Client ";
			this.panel1.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.panel2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new ClientForm());
		}

		private void startbutton_Click(object sender, System.EventArgs e)
		{
			if (m_serialPort ==null)
				m_serialPort =  new SerialPort();
			if (m_serialPort.PortInitialize(comboBox1.Text,Convert.ToInt16(comboBox2.Text),
				Convert.ToByte(Convert.ToInt16(comboBox3.Text)),
				Convert.ToByte(comboBox4.SelectedIndex),
				Convert.ToByte(comboBox5.SelectedIndex)))
			{
              	m_stop = false;
				m_thread = new Thread(new ThreadStart(serialPortListening));
				m_thread.Start();      // Start the thread.				
				
				//serialPortListening();
				stopbutton.Enabled = true;
				startbutton.Enabled = false;
			}
		}

		private void connectButton_CheckedChanged(object sender, System.EventArgs e)
		{
			if (connectButton.Checked)
			{
				try
				{
					//register client channel
					m_clientChannel = new TcpClientChannel();
					ChannelServices.RegisterChannel(m_clientChannel);
				
					//Obtaining Proxy for TrcakeeManager, using activator.GetObject
					string temp;
					if (tcpCheckBox.Checked)
						temp = "tcp://";
					else
						temp = "http://";
					m_channelName= temp + addressTextBox.Text+":"+portTextBox.Text+"/";
					temp= m_channelName + "TrackeeManager";
				
					m_manager = (TrackeeManagerServant) Activator.GetObject(typeof(TrackeeManagerServant),temp);
					startbutton.Enabled = true;
					stopbutton.Enabled = false;
				}
				catch(Exception e1)
				{
					MessageBox.Show(e1.ToString());
				}
			}
		}

		private void disconnectButton_CheckedChanged(object sender, System.EventArgs e)
		{
			if (disconnectButton.Checked)
			{
				try
				{
					//delete Proxy of the TrcakeeManager
					m_manager = null;
					//disregister client channel
					ChannelServices.UnregisterChannel(m_clientChannel);
					if (m_file != null)
					{
						m_file.Close();
						m_file = null;
					}
					startbutton.Enabled = false;
					stopbutton.Enabled = false;
					label7.Text = "";
				}
				catch(Exception e1)
				{
					MessageBox.Show(e1.ToString());
				}
			}
		}

		private void stopbutton_Click(object sender, System.EventArgs e)
		{
			stopbutton.Enabled = false;
			startbutton.Enabled = true;
			m_stop = true;
			if (m_thread.IsAlive)
			{
                m_serialPort.Close();
				m_thread.Abort();
			}
			label7.Text = "";
			m_serialPort = null;
		}

		private void serialPortListening()
		{
            m_serialPort.monitorSerialPort(this);
		}

		public void receiveFromComPort(string aMessage)
		{
			//
			if (MessageReadyEvent != null)
			{
				label7.Text = aMessage;
			
				//Invokes the delegates.
				MessageReadyEvent(new SerialPortEventArgs(aMessage)); 
			}
		}

		void OnSerialMessageReady(SerialPortEventArgs e)
		{
			Callsign callsign;
		    string line;

			if ((line = e.getText()) != null)
			{
				callsign = Callsign.StringToObject(line);
				if ( callsign == null||callsign.Name==null)
					return ;

				try
				{
					m_manager.setPosition(callsign.Name,callsign.Pos.Latitude,callsign.Pos.Longitude);
				}
				catch(Exception e1)
				{
					string temp = e1.ToString();
					//MessageBox.Show(callsign.Name);
				}
			}
		}
	}

	public class SerialPortEventArgs : EventArgs 
	{  
		private readonly string m_message;
		private readonly byte[ ] m_bytes;
      
		//Constructor.
		public SerialPortEventArgs(string aMessage) 
		{
			this.m_message =aMessage;
		}
		//Constructor.
		public SerialPortEventArgs(byte[] aMessage) 
		{
			this.m_bytes = aMessage;
		}

		public string getText() 
		{
			return this.m_message;
		}
	}

	public class Callsign
	{
		private string m_name;
		private Position m_pos;
        

		public Callsign(string callsignName, Position aPos)
		{
			m_name = callsignName;
			m_pos = aPos;
		}

		public string Name 
		{
			get {	return m_name; 	}
			set {	m_name = value; 	}
		}
		
		public Position Pos 
		{
			get {	return m_pos; 	}
			set {	m_pos = value; 	}
		}
		
		//method create object from a message
		//token the packet is following format:
		//deplay message:    :}callsignmae>path ....
		//callsignname>path:=DDMM.mmN/DDDMM.mmW 
		//callsignname>path:@timehDDMM.mmN/DDMM.mmW  
		//callsignname>path:!DDMM.mmN/DDDMM.mmW 
		//callsignname>path:$GPRMC,time,A,DDMM.mm,N,DDDMM.mm,W,)
		//RTTY: DDMM.mm'/DDDMM.mm) '--N ,--S (--E )--W

		public static Callsign StringToObject(String m)
		{
			string message,callsign,temp;
			string latStr,lonStr;
			int index,index1,index2;
			Position aPos;
 			
			message =m;
			//token the callsign name from the message
			
			index = message.IndexOf(":}",0);
			if (index >-1) //delay message format: get the real message
			{
				message = message.Substring(index+2,message.Length-index-2);
			}
			index = message.IndexOf(">",0);
			if (index <0)
			{
				return null;						
			}
			callsign = message.Substring(0,index);
			
			if (callsign.Length >10)
				return null;						

			index = message.IndexOf(":!",0);
			if (index <0)
			{
				index = message.IndexOf(":=",0);
			}
			if (index <0)
			{
				index = message.IndexOf(":@",0);
			}
			if (index <0)
			{
				return null;						
			}
			
			//token position from the message
			temp =  message.Substring(index+1,message.Length-index-1);
			index1 = temp.IndexOf("N",0);
			if (index1 <0)
				index1 = temp.IndexOf("S",0);
			
			index2 = temp.IndexOf("W",0);
			if (index2 <0)
				index2 = temp.IndexOf("E",0);
			
			if (index1<0 || index2 <0 || index2!= index1+10) //not position message
				return null;

			latStr = temp.Substring(index1-7,8);
			lonStr = temp.Substring(index2-8,9);
			aPos = Position.StringToObject(latStr,lonStr);

			if (callsign !=null && aPos != null)
				return new Callsign(callsign,aPos);
			else
				return null;
		} //end method StringToObject

	} //end callsign class
}

