public DBBuilder()
 {
     InitializeComponent();
     LoadSettings();
     _writer = new SWATWikiGenerator();
 }
		//Begin recording. Opens a new browser window and connects to browser events.
		public void Record(ContextMenuStrip cmsMenu)
		{
			this.InitializeContextMenu(cmsMenu);
			_writer = new SWATWikiGenerator();
			_writer.Initialize(BrowserType.InternetExplorer, true);
			_recording = true;
			_inetExplorer = new WebBrowserEvents("NewWindow", null, ref _writer);
			RecordJScriptBoxSetup();
		}
		public void Record(ContextMenuStrip cmsMenu, ArrayList browserNames, ArrayList indexes, System.Windows.Forms.CheckedListBox.CheckedIndexCollection checkedIndices)
		{
			this.InitializeContextMenu(cmsMenu);
			_writer = new SWATWikiGenerator();
			_writer.Initialize(BrowserType.InternetExplorer, false);
			_recording = true;

			RecordJScriptBoxSetup();

			foreach (int a in checkedIndices)
			{
				string titleContents = (String)browserNames[a];
				int _index = (int)indexes[a];
				int indexCount = 0;
				ShellWindows m_IEFoundBrowsers = new ShellWindowsClass();
				foreach (InternetExplorer Browser in m_IEFoundBrowsers)
				{
                    try
                    {
                        if (Browser.LocationName != "" && Browser.Document is mshtml.HTMLDocument)
                        {
                            if ((((mshtml.HTMLDocument)Browser.Document).title).Equals(titleContents))
                            {
                                if (indexCount == _index)
                                {
                                    _inetExplorer = new WebBrowserEvents("AttachToWindow", Browser, ref _writer);
                                    break;
                                }
                                else
                                    indexCount++;
                            }
                        }
                    }
                    catch (System.Runtime.InteropServices.COMException) { continue; }
				}
			}
		}
		//public WebBrowserEvents(InternetExplorer _intExp, ref SWATWikiGenerator _write)
		//{
		//    _writer = _write;

		//    Random rand = new Random();
		//    browserId = rand.Next(100000);

		//    if (_intExp == null)
		//    {
		//        manualNavigation = true;
		//        ie = new InternetExplorerClass();
		//        //add a property for identification
		//        ie.PutProperty("ID", browserId);
		//        ie.Visible = true;
		//        object url = "about:blank";
		//        object nullObj = String.Empty;
		//        ConnectWindow(ie);
		//        ie.Navigate2(ref url, ref nullObj, ref nullObj, ref nullObj, ref nullObj);
		//        HTMLEvents.lastBrowser = browserId;
		//    }
		//    else
		//    {
		//        manualNavigation = false;
		//        ie = _intExp;
		//        ie.PutProperty("ID", browserId);
		//        ConnectWindow(ie);
		//    }


		//}


		public WebBrowserEvents(String flag, SHDocVw.InternetExplorer _intExp, ref SWATWikiGenerator _write)
		{
			_writer = _write;
			Random rand = new Random();
			browserId = rand.Next(100000);

			if (flag.Equals("AttachToWindow"))
			{
				init = false;
				manualNavigation = false;
				ie = _intExp;
				ie.PutProperty("ID", browserId);
				ConnectWindow(ie);
				//Debug.WriteLine((((HTMLDocument)ie.Document).frames).length);
				try
				{
					FramesCollection frames = (((HTMLDocument)ie.Document).frames);

					for (int i = 0; i < frames.length; i++)
					{
						object refIndex = i;
						try
						{
							HTMLDocument doc = (HTMLDocument)((HTMLWindow2Class)frames.item(ref refIndex)).document;
							if (doc.Equals((HTMLDocument)ie.Document)) continue;
							HTMLEvents aEvent = new HTMLEvents(_writer, doc, ie);
							_eventsList.Add(aEvent);
						}
						catch (UnauthorizedAccessException) { }
					}
				}
				catch (Exception) { }
				_eventsList.Insert(0, new HTMLEvents(_writer, ie.Document, ie));
				checkAttach();
			}
			else if (flag.Equals("NewWindow"))
			{
				manualNavigation = true;
				ie = new InternetExplorerClass();
				//add a property for identification
				ie.PutProperty("ID", browserId);
				ie.Visible = true;
				object url = "about:blank";
				object nullObj = String.Empty;
				ConnectWindow(ie);
				ie.Navigate2(ref url, ref nullObj, ref nullObj, ref nullObj, ref nullObj);
				HTMLEvents.lastBrowser = browserId;
			}
			else if (flag.Equals("PopUp"))
			{
				manualNavigation = false;
				ie = _intExp;
				ie.PutProperty("ID", browserId);
				ConnectWindow(ie);
			}
			else if (flag.Equals("AttachToModalWindow"))
			{
				IntPtr dialogHwnd = IntPtr.Zero;
				//int indexCount = NativeMethods.GetWindowWithSubstring("content.html", 0, 0, ref dialogHwnd);
				dialogHwnd = NativeMethods.GetForegroundWindow();


				if (dialogHwnd != IntPtr.Zero)
				{
					// This block of code SHOULD take a window handle and return an IHTMLDocument object to dialogDoc
					// Then it will assign that object to _doc. It follows an example in the MSDN library and WatiN uses it as well.
					// If it performs correctly, then we should be attached to the Dialog.
					dialogHwnd = NativeMethods.GetChildWindowHwnd(dialogHwnd, "Internet Explorer_Server");
					//dialogHwnd = NativeMethods.GetForegroundWindow();

					Int32 result = 0;
					Int32 dialog;
					Int32 message;

					message = NativeMethods.RegisterWindowMessage("WM_HTML_GETOBJECT");
					NativeMethods.SendMessageTimeout(dialogHwnd, message, 0, 0, NativeMethods.SMTO_ABORTIFHUNG, 1000, ref result);

					//NativeMethods.SendMessage(dialogHwnd, NativeMethods.WM_CLOSE, 0, 0);

					IHTMLDocument2 dialogDoc = null;
					System.Guid dialogID = typeof(mshtml.HTMLDocument).GUID;
					dialog = NativeMethods.ObjectFromLresult(result, ref dialogID, 0, ref dialogDoc);

					//HTMLFrameBase dialogDoc = null;
					//System.Guid dialogID = typeof(mshtml.HTMLFrameBase).GUID;
					//dialog = NativeMethods.ObjectFromLresult(result, ref dialogID, 0, ref dialogDoc);

					//dialogDoc.title = "Hello?";

					//IHTMLDialog dialogDoc = null;
					//System.Guid dialogID = typeof(mshtml.HTMLDialog).GUID;
					//dialog = NativeMethods.ObjectFromLresult(result, ref dialogID, 0, ref dialogDoc);




					//IHTMLWindow2 dialogDoc = null;
					//System.Guid dialogID = typeof(IHTMLWindow2).GUID;
					//dialog = NativeMethods.ObjectFromLresult(result, ref dialogID, 0, ref dialogDoc);

					//dialogDoc.close();

					//Console.WriteLine("URL: " + dialogDoc.close + "\nTitle: " + dialogDoc.title);

					//ie = (InternetExplorer)dialogDoc;
					//ie.PutProperty("ID", browserId);
					//ConnectWindow(ie);

					//doc = (HTMLDocument)dialogDoc;



				}
			}
			_browserList.Add(this);

		}