/// <summary>
        /// Converts a HTMLFormElementClass to a GB HtmlFormTag.
        /// </summary>
        /// <param name="formElement"> The HTMLFormElementClass to convert.</param>
        /// <param name="currentUri"> The current uri.</param>
        /// <returns> A HtmlFormTag.</returns>
        public HtmlFormTag ConvertToHtmlFormTag(HTMLFormElementClass formElement, Uri currentUri)
        {
            IHTMLElement el = (IHTMLElement)formElement.elements;
            ArrayList elements = new ArrayList();
            // Converts the element tag to an array list
            elements = ParseTags(elements,(IHTMLElementCollection)el.children);

            HtmlFormTag formTag = new HtmlFormTag();

            #region Set Form Properties
            if ( formElement.action == null )
            {
                formTag.Action = currentUri.Scheme + "://" + currentUri.Authority + currentUri.AbsolutePath;
            }
            else
            {
                formTag.Action = formElement.action;
            }

            formTag.Class = formElement.className;

            if ( formElement.method == null )
            {
                formTag.Method = "GET";
            } else {
                formTag.Method = formElement.method;
            }
            if ( formElement.encoding == null )
            {
                formTag.Enctype = "";
            }
            else
            {
                formTag.Enctype = formElement.encoding;
            }

            // formTag.FormIndex = formElement.
            formTag.Id = formElement.id;

            if ( formElement.name != null )
            {
                formTag.Name = formElement.name;
            }
            else
            {
                if ( formElement.id != null )
                {
                    formTag.Name = formElement.id;
                }
                else
                {
                    formTag.Name = formElement.uniqueID;
                    formTag.Id = formElement.uniqueID;
                }
            }
            formTag.OnSubmit = string.Empty;
            formTag.Style = string.Empty;
            formTag.Title = formElement.title;
            #endregion
            #region Add Tags
            foreach (object obj in elements)
            {
                // The last check is the most significant type

                #region Button Element
                if (obj is mshtml.HTMLInputButtonElementClass)
                {
                    HtmlButtonTag button = CreateHtmlButtonTag((HTMLInputButtonElementClass)obj);

                    if ( formTag.ContainsKey(button.Name) )
                    {
                        // just add the value
                        HtmlTagBaseList array = ((HtmlTagBaseList)formTag[button.Name]);
                        array.Add(button);
                    }
                    else
                    {
                        HtmlTagBaseList list = new HtmlTagBaseList();
                        list.Add(button);
                        formTag.Add(button.Name,list);
                    }
                    continue;
                }
                #endregion

                #region Select Element
                if (obj is mshtml.HTMLSelectElementClass)
                {
                    HtmlSelectTag select = CreateHtmlSelectTag((HTMLSelectElementClass)obj);

                    if ( formTag.ContainsKey(select.Name) )
                    {
                        // just add the value
                        HtmlTagBaseList array = ((HtmlTagBaseList)formTag[select.Name]);
                        array.Add(select);
                    }
                    else
                    {
                        HtmlTagBaseList list = new HtmlTagBaseList();
                        list.Add(select);
                        formTag.Add(select.Name,list);
                    }
                    continue;
                }
                #endregion

                #region Textarea Element
                if (obj is mshtml.HTMLTextAreaElementClass)
                {
                    HtmlTextAreaTag textarea=CreateHtmlTextAreaTag((HTMLTextAreaElementClass)obj);
                    if ( formTag.ContainsKey(textarea.Name) )
                    {
                        // just add the value
                        HtmlTagBaseList array = ((HtmlTagBaseList)formTag[textarea.Name]);
                        array.Add(textarea);
                    }
                    else
                    {
                        HtmlTagBaseList list = new HtmlTagBaseList();
                        list.Add(textarea);
                        formTag.Add(textarea.Name,list);
                    }
                    continue;
                }
                #endregion

                #region Input Element
                if (obj is mshtml.HTMLInputElementClass)
                {
                    HtmlInputTag input = CreateHtmlInputTag((HTMLInputElementClass)obj);
                    if ( formTag.ContainsKey(input.Name) )
                    {
                        // just add the value
                        HtmlTagBaseList array = ((HtmlTagBaseList)formTag[input.Name]);
                        array.Add(input);
                    }
                    else
                    {
                        HtmlTagBaseList list = new HtmlTagBaseList();
                        list.Add(input);
                        formTag.Add(input.Name,list);
                    }
                    continue;
                }
                #endregion

            }
            #endregion

            return formTag;
        }
        /// <summary>
        /// Creates a form from a query string.
        /// </summary>
        /// <param name="url"> The complete url.</param>
        /// <returns> A HtmlFormTag.</returns>
        private HtmlFormTag CreateFormFromQueryString(Uri url)
        {
            string action = url.Scheme + "://" + url.Host + url.AbsolutePath;
            FormConverter converter = new FormConverter();
            PostDataCollection postData = converter.GetPostDataCollection(url.Query.TrimStart('?'));

            HtmlFormTag formTag = new HtmlFormTag();
            formTag.Action = action;
            formTag.Name = "docform";
            formTag.Method = base.WebRequest.RequestType.ToString();

            foreach ( string key in postData.Keys )
            {
                ArrayList items = postData[key];

                HtmlTagBaseList list = new HtmlTagBaseList();
                formTag.Add(key, list);
                foreach ( string value in items )
                {
                    HtmlInputTag hiddenField = new HtmlInputTag();
                    hiddenField.Type = HtmlInputType.Hidden;
                    hiddenField.Name = key;
                    hiddenField.Value = value.Trim().TrimEnd('\0');
                    list.Add(hiddenField);
                }
            }

            return formTag;
        }
        /// <summary>
        /// Loads the forms into a HtmlFormTagCollection.
        /// </summary>
        /// <param name="html"> The parsed HTML content.</param>
        /// <returns> Returns a HtmlFormTagCollection with the forms contained in the HTML.</returns>
        public HtmlFormTagCollection LoadForm(string html)
        {
            HtmlFormTagCollection forms;
            try
            {
                forms = new HtmlFormTagCollection();
                XPathDocument doc;

                if ( HasForms(html) )
                {
                    doc = this.DocumentCache;

                    XPathNavigator nav = doc.CreateNavigator();
                    #region "Set namespaces"
                    if ( this.NamespaceCache == null)
                    {
                        //create prefix<->namespace mappings (if any)
                        XmlNamespaceManager nsMgr = new XmlNamespaceManager(nav.NameTable);

                        // resolve namespaces before loading document
                        Hashtable values = ResolveNamespaces(new XmlTextReader(new StringReader(html)));

                        foreach (DictionaryEntry de in values)
                        {
                            nsMgr.AddNamespace((string)de.Key,(string)de.Value);
                        }

                        this.NamespaceCache = nsMgr;
                    }
                    #endregion
                    int i=0;
                    XPathExpression expr = nav.Compile("//form");
                    expr.SetContext(this.NamespaceCache);
                    // select all form elements
                    XPathNodeIterator nodes = nav.Select(expr);

                    #region Build Form Tag
                    while (nodes.MoveNext())
                    {
                        HtmlFormTag f = new HtmlFormTag();

                        f.FormIndex = i;
                        f.Id=nodes.Current.GetAttribute("id",nodes.Current.NamespaceURI);
                        f.Style=nodes.Current.GetAttribute("style",nodes.Current.NamespaceURI);
                        f.Enctype=nodes.Current.GetAttribute("enctype",nodes.Current.NamespaceURI);
                        f.Class=nodes.Current.GetAttribute("class",nodes.Current.NamespaceURI);
                        f.Name=nodes.Current.GetAttribute("name",nodes.Current.NamespaceURI);
                        f.Action=nodes.Current.GetAttribute("action",nodes.Current.NamespaceURI);
                        f.Method=nodes.Current.GetAttribute("method",nodes.Current.NamespaceURI);
                        f.Id=nodes.Current.GetAttribute("id",nodes.Current.NamespaceURI);
                        f.OnSubmit=nodes.Current.GetAttribute("onsubmit",nodes.Current.NamespaceURI);

                        if (f.Action.Length == 0 )
                        {
                            // add dummy action
                            f.Action = "dummyAction";
                        }

                        if ( f.Method.Length==0 )
                        {
                            f.Method="get";
                        }

                        if ( f.Id!=String.Empty )
                        {
                            f.Name = f.Id;
                        }
                        if ( forms.ContainsKey(f.Name) )
                        {
                            forms.Add("_" + f.Name,f);
                        }
                        else
                        {
                            if ( f.Name!=String.Empty )
                            {
                                forms.Add(f.Name,f);
                            }
                            if (f.Id==String.Empty && f.Name == String.Empty )
                            {
                                f.Id = "Form " + i;
                                f.Name = "Form " + i;
                                forms.Add(f.Name,f);
                            }
                        }

                        #region " Loop thru descendants from Form"
                        // Select descendants, childs
                        XPathNodeIterator items = nodes.Current.SelectDescendants(XPathNodeType.Element,true);

                        int autoId = 0;

                        while ( items.MoveNext() )
                        {

                            // if exists, add to same HtmlTagBaseList type
                            // else create new
                            string name = items.Current.GetAttribute("name",items.Current.NamespaceURI);
                            string id = items.Current.GetAttribute("id",items.Current.NamespaceURI);
                            string onclick = items.Current.GetAttribute("onclick",items.Current.NamespaceURI);

                            if ( onclick == String.Empty )
                            {
                                onclick = items.Current.GetAttribute("onClick",items.Current.NamespaceURI);
                            }

                            // prioritize name use
                            // else use id
                            if ( name == String.Empty )
                            {
                                name = id;
                                // if no id, generate one
                                if ( name == String.Empty )
                                {
                                    id = autoId.ToString();
                                    name = id;
                                }
                            }

                            switch ( items.Current.Name )
                            {
                                case "div":
                                    if ( onclick != String.Empty )
                                    {
                                        AddCommonTag(f,onclick,id,name);
                                    }
                                    break;
                                case "span":
                                    if ( onclick != String.Empty )
                                    {
                                        AddCommonTag(f,onclick,id,name);
                                    }
                                    break;
                                case "a":
                                    HtmlALinkTag a = CreateLinkTag(items.Current);
                                    if ( f.ContainsKey(name) )
                                    {
                                        // just add the value
                                        HtmlTagBaseList array = ((HtmlTagBaseList)f[name]);
                                        array.Add(a);
                                    }
                                    else
                                    {
                                        HtmlTagBaseList list = new HtmlTagBaseList();
                                        list.Add(a);
                                        f.Add("a" + autoId.ToString(),list);
                                    }
                                    break;
                                case "input":
                                    // if exists, add to same HtmlTagBaseList type
                                    // else create new
                                    // verify by name and type
                                    HtmlInputTag input = FillInputTag(items.Current);
                                    input.Name = name;

                                    if ( f.ContainsKey(name) )
                                    {
                                        // just add the value
                                        HtmlTagBaseList array = ((HtmlTagBaseList)f[name]);
                                        array.Add(input);
                                    }
                                    else
                                    {
                                        HtmlTagBaseList list = new HtmlTagBaseList();
                                        //HtmlInputTag input = FillInputTag(items.Current);
                                        list.Add(input);
                                        f.Add(input.Name,list);
                                    }
                                    break;
                                case "button":
                                    // if exists, add to same HtmlTagBaseList type
                                    // else create new
                                    // verify by name
                                    HtmlButtonTag button = FillButtonTag(items.Current);
                                    button.Name = name;

                                    if ( f.ContainsKey(name) )
                                    {
                                        // just add the value
                                        HtmlTagBaseList array = ((HtmlTagBaseList)f[name]);
                                        //HtmlButtonTag button = FillButtonTag(items.Current);
                                        array.Add(button);
                                    }
                                    else
                                    {
                                        HtmlTagBaseList buttonList = new HtmlTagBaseList();
                                        buttonList.Add(button);
                                        f.Add(button.Name,buttonList);
                                    }

                                    break;
                                case "select":
                                    // if exists, add to same HtmlTagBaseList type
                                    // else create new
                                    // verify by name
                                    HtmlSelectTag select = CreateSelectTag(items.Current);
                                    select.Name = name;

                                    if ( f.ContainsKey(name) )
                                    {
                                        HtmlTagBaseList array = ((HtmlTagBaseList)f[name]);
                                        array.Add(select);
                                    }
                                    else
                                    {
                                        HtmlTagBaseList selectList = new HtmlTagBaseList();
                                        //HtmlSelectTag select = FillSelectTag(items.Current);
                                        selectList.Add(select);
                                        f.Add(select.Name,selectList);
                                    }
                                    break;
                                case "textarea":
                                    // if exists, add to same HtmlTagBaseList type
                                    // else create new
                                    // verify by name
                                    HtmlTextAreaTag textarea = FillTextAreaTag(items.Current);
                                    textarea.Name = name;

                                    if ( f.ContainsKey(name) )
                                    {
                                        HtmlTagBaseList array = ((HtmlTagBaseList)f[name]);
                                        //HtmlTextAreaTag textarea = FillTextAreaTag(items.Current);
                                        array.Add(textarea);
                                    }
                                    else
                                    {
                                        HtmlTagBaseList textAreaList = new HtmlTagBaseList();
                                        textAreaList.Add(textarea);
                                        f.Add(textarea.Name,textAreaList);
                                    }
                                    break;
                            }

                            // increase
                            autoId++;
                        }
                        i++;
                        #endregion
                    }
                    #endregion
                }
            }
            catch
            {
                throw;
            }

            return forms;
        }
        /// <summary>
        /// Writes the HtmlFormTagXml to a HtmlFormTag.
        /// </summary>
        /// <returns>A new cloned HtmlFormTag.</returns>
        public HtmlFormTag WriteHtmlFormTag()
        {
            HtmlFormTag form = new HtmlFormTag();
            form.Action = this.Action;
            form.Enctype = this.Enctype;
            form.FormIndex = this.FormIndex;
            form.Method = this.Method;
            form.Name = this.Name;
            form.OnSubmit = this.OnSubmit;

            foreach ( HtmlTagListXml listXml in Elements )
            {
                HtmlTagBaseList list = new HtmlTagBaseList();

                list.AddRange(listXml.Tags);
                form.Add(listXml.Name, list);
            }

            return form;
        }
        /// <summary>
        /// Adds a common tag.
        /// </summary>
        /// <param name="form"> The HtmlFormTag.</param>
        /// <param name="click"> The click event source code.</param>
        /// <param name="id"> The element id.</param>
        /// <param name="name"> The element name.</param>
        private void AddCommonTag(HtmlFormTag form,string click,string id, string name)
        {
            //add
            HtmlTagBase tag = new HtmlTagBase();
            tag.Id = id;
            tag.OnClick = click;

            if ( form.ContainsKey(name) )
            {
                // just add the value
                HtmlTagBaseList array = ((HtmlTagBaseList)form[name]);
                array.Add(tag);
            }
            else
            {
                HtmlTagBaseList list = new HtmlTagBaseList();
                list.Add(tag);
                form.Add(tag.Id,list);
            }
        }