//Insert
 public void Remove(addressitem value)
 {
     List.Remove(value);
 }
 public void verifysingledocument(addressitem ai, string filename, string template, bool startuploadwhendone = false)
 {
     _sourcefilename = filename;
     _CurrentTemplate = _path + "\\" + template;
     _aiSingle = ai;
     _startuploadwhendone = startuploadwhendone;
     CurrentPage = 1;
 }
 //Add
 public int IndexOf(addressitem value)
 {
     return List.IndexOf(value);
 }
 //IndexOf
 public void Insert(int index, addressitem value)
 {
     List.Insert(index, value);
 }
 public int Add(addressitem value)
 {
     return List.Add(value);
 }
        private void standardizeaddress(ref addressitem ai, bool bypassvalidate = false)
        {
            if (bypassvalidate == false) {
                if ( ai.validatedStatus == "false") {
                    return;
                }
            }

            if (!string.IsNullOrEmpty(ai.Address1)) {
                ai.Address1 = ai.Address1.Replace( "#", " APT ").Replace( "  ", " ");
                ai.Address1 = ai.Address1.ToUpper().Replace( " APT APT ", " APT ");
            }
            if (!string.IsNullOrEmpty(ai.Address2)) {
                ai.Address2 = ai.Address2.Replace( "#", " APT ").Replace( "  ", " ");
                ai.Address2 = ai.Address2.ToUpper().Replace(" APT APT ", " APT ");
            }

            HttpWebRequest request = null;
            HttpWebResponse response = null;
            StreamReader reader = null;
            Uri address = null;
            Stream postStream = null;

            var _url = "http://production.shippingapis.com/ShippingAPITest.dll?API=Verify&XML=";
            XmlDocument xm = new XmlDocument();
            XmlNode xadr = xm.CreateElement("AddressValidateRequest");
            XmlAttribute attr = xm.CreateAttribute("USERID");
            attr.Value = "264NPWU04230";
            xadr.Attributes.Append(attr);

            XmlNode xad = xm.CreateElement("Address");
            XmlNode xad1 = xm.CreateElement("Address1");
            xad1.InnerText = ai.Address1;

            XmlNode xad2 = xm.CreateElement("Address2");
            xad2.InnerText = ai.Address2;

            XmlNode xcity = xm.CreateElement("City");
            xcity.InnerText = ai.city;
            XmlNode xstate = xm.CreateElement("State");
            xstate.InnerText = ai.state;
            XmlNode xzip = xm.CreateElement("Zip5");
            xzip.InnerText = ai.zip5;
            XmlNode xzip4 = xm.CreateElement("Zip4");

            xzip.InnerText = Strings.Left(ai.zip5, 5);
            xad.AppendChild(xad1);
            xad.AppendChild(xad2);
            xad.AppendChild(xcity);
            xad.AppendChild(xstate);
            xad.AppendChild(xzip);
            xad.AppendChild(xzip4);

            xadr.AppendChild(xad);
            xm.AppendChild(xadr);

            Console.WriteLine(_url + xm.OuterXml);
            address = new Uri(_url + xm.OuterXml);

            // Create the web request
            request = (HttpWebRequest)WebRequest.Create(address);
            request.Method = "GET";
            request.ContentType = "text/xml";

            //data = New StringBuilder()
            //data.Append("test=1")
            //data.Append("&layout=" + WebUtility.UrlEncode("Address on Separate Page"))
            //data.Append("&productionTime=" + WebUtility.UrlEncode("Next Day"))
            //data.Append("&envelope=" + WebUtility.UrlEncode("#10 Double Window"))
            //data.Append("&color=" + WebUtility.UrlEncode("Black and White"))
            //data.Append("&paperType=" + WebUtility.UrlEncode("White 24#"))
            //data.Append("&printOption=" + WebUtility.UrlEncode("Printing One side"))
            //data.Append("&documentId=" + WebUtility.UrlEncode(c2m.documentid))
            //data.Append("&addressId=" + WebUtility.UrlEncode(c2m.addressid))
            //Console.Write(data.ToString)
            //byteData = UTF8Encoding.UTF8.GetBytes(Data.ToString())

            //        postStream = request.GetRequestStream()
            //       postStream.Write(byteData, 0, byteData.Length)

            try {
                response = (HttpWebResponse) request.GetResponse();
            } catch (WebException wex) {
                // This exception will be raised if the server didn't return 200 - OK
                // Try to retrieve more information about the network error
                if ((wex.Response != null)) {
                    HttpWebResponse errorResponse = null;
                    try {
                        errorResponse = (HttpWebResponse)wex.Response;
                        Console.WriteLine("The server returned '{0}' with the status code {1} ({2:d}).", errorResponse.StatusDescription, errorResponse.StatusCode, errorResponse.StatusCode);
                    } finally {
                        if ((errorResponse != null))
                            errorResponse.Close();
                    }
                }
            } finally {
                if ((postStream != null))
                    postStream.Close();
            }
            //

            try {
                reader = new StreamReader(response.GetResponseStream());

                // Console application output
                string s = reader.ReadToEnd();
                reader.Close();
                if (string.IsNullOrEmpty(parsexml(s, "City"))) {
                    ai.uspsStatus = "BAD ADDRESS";
                    return;

                } else {
                    ai.Address1 = parsexml(s, "Address1");
                    ai.Address2 = parsexml(s, "Address2");
                    ai.city = parsexml(s, "City");
                    ai.state = parsexml(s, "State");
                    ai.zip5 = parsexml(s, "Zip5");
                    ai.zip4 = parsexml(s, "Zip4");
                    ai.returntext = parsexml(s, "ReturnText");
                    ai.uspsStatus = "1";
                    if (!string.IsNullOrEmpty(ai.returntext)) {
                        ai.uspsStatus = "2";
                    }

                }
                //Return parsexml(s, "id")
                //    c2m.StatusPick.jobStatus = parsexml(s, "status")
                //MsgBox(s)

            } finally {
                // If c2m.jobid = 0 Then
                //            c2m.StatusPick.jobStatus = 99
                //End If
                //If Not response Is Nothing Then response.Close()
            }
        }
        private bool parseaddresssingle_GeneratedPDF(ref addressitem ai, ref XmlNode BatchNode)
        {
            int startpage = 1;

            //Validate Text

            //It is a first page, but now lets see if there is a valid address
            //If address1 validates
            //

            if (BatchNode == null)
            {

                XMLDOC = new System.Xml.XmlDocument();
                System.Xml.XmlNode docNode = XMLDOC.CreateXmlDeclaration("1.0", "UTF-8", null);
                XMLDOC.AppendChild(docNode);
                BatchNode = XMLDOC.CreateElement("batch");
                System.Xml.XmlNode ns = null;
                ns = XMLDOC.CreateAttribute("xmlns", "xsi", "http://www.w3.org/2000/xmlns/");
                ns.Value = "http://www.w3.org/2001/XMLSchema-instance";
                //BatchNode.Attributes.Append(ns);
                XMLDOC.AppendChild(BatchNode);

                System.Xml.XmlNode un = XMLDOC.CreateElement("username");
                BatchNode.AppendChild(un);
                un.InnerText = (string) _dtt.Select("setting = true and fieldname = 'username'")[0]["misc"];

                System.Xml.XmlNode pw = XMLDOC.CreateElement("password");
                pw.InnerText = Decrypt((string) _dtt.Select("setting = true and fieldname = 'password'")[0]["misc"]);
                BatchNode.AppendChild(pw);
                System.Xml.XmlNode fn = XMLDOC.CreateElement("filename");
                fn.InnerText = _sourcefilename;
                BatchNode.AppendChild(fn);
                System.Xml.XmlNode as1 = XMLDOC.CreateElement("appSignature");
                BatchNode.AppendChild(as1);
                as1.InnerText = (string) _dtt.Select("setting = true and fieldname = 'appSignature'")[0]["misc"];
            }

            // ai = New addressitem

            System.Xml.XmlNode job = XMLDOC.CreateElement("job");
            BatchNode.AppendChild(job);

            System.Xml.XmlNode startingpage = XMLDOC.CreateElement("startingPage");
            job.AppendChild(startingpage);
            startingpage.InnerText = Convert.ToString( ai.startpage);
            System.Xml.XmlNode endpage = XMLDOC.CreateElement("endingPage");
            endpage.InnerText = Convert.ToString(ai.endpage);
            job.AppendChild(endpage);

            System.Xml.XmlNode printProductionOptions = XMLDOC.CreateElement("printProductionOptions");
            job.AppendChild(printProductionOptions);
            System.Xml.XmlNode docclass = XMLDOC.CreateElement("documentClass");
            docclass.InnerText = (string) _dtt.Select("setting = true and fieldname = 'poDocumentClass'")[0]["misc"];
            printProductionOptions.AppendChild(docclass);

            System.Xml.XmlNode layout = XMLDOC.CreateElement("layout");
            layout.InnerText = (string) _dtt.Select("setting = true and fieldname = 'poLayout'")[0]["misc"];
            printProductionOptions.AppendChild(layout);

            System.Xml.XmlNode productiontime = XMLDOC.CreateElement("productionTime");
            productiontime.InnerText = (string) _dtt.Select("setting = true and fieldname = 'poProductionTime'")[0]["misc"];
            printProductionOptions.AppendChild(productiontime);

            System.Xml.XmlNode envelope = XMLDOC.CreateElement("envelope");
            envelope.InnerText = (string) _dtt.Select("setting = true and fieldname = 'poEnvelope'")[0]["misc"];

            if ("Printing One side" == (string) _dtt.Select("setting = true and fieldname = 'poPrintOption'")[0]["misc"])
            {
                if ((ai.endpage - ai.startpage) + 1 > 5)
                {
                    envelope.InnerText = oversized;
                }

            }
            else
            {
                if ((ai.endpage - ai.startpage) + 1 > 10)
                {
                    envelope.InnerText = oversized;
                }

            }

            printProductionOptions.AppendChild(envelope);

            System.Xml.XmlNode color = XMLDOC.CreateElement("color");
            color.InnerText = (string)_dtt.Select("setting = true and fieldname = 'poColor'")[0]["misc"];
            printProductionOptions.AppendChild(color);

            System.Xml.XmlNode papertype = XMLDOC.CreateElement("paperType");
            papertype.InnerText = (string)_dtt.Select("setting = true and fieldname = 'poPaperType'")[0]["misc"];
            printProductionOptions.AppendChild(papertype);

            System.Xml.XmlNode printoption = XMLDOC.CreateElement("printOption");
            printoption.InnerText = (string)_dtt.Select("setting = true and fieldname = 'poPrintOption'")[0]["misc"];
            printProductionOptions.AppendChild(printoption);

            System.Xml.XmlNode mailclass = XMLDOC.CreateElement("mailClass");
            mailclass.InnerText = (string)_dtt.Select("setting = true and fieldname = 'poMailClass'")[0]["misc"];
            printProductionOptions.AppendChild(mailclass);

            if (!string.IsNullOrEmpty((string)_dtt.Select("setting = true and fieldname = 'raName'")[0]["misc"]) | !string.IsNullOrEmpty((string)_dtt.Select("setting = true and fieldname = 'raOrganization'")[0]["misc"]))
            {
                System.Xml.XmlNode returnAddress = XMLDOC.CreateElement("returnAddress");
                job.AppendChild(returnAddress);

                System.Xml.XmlNode raname = XMLDOC.CreateElement("name");
                raname.InnerText = (string)_dtt.Select("setting = true and fieldname = 'raName'")[0]["misc"];
                returnAddress.AppendChild(raname);

                System.Xml.XmlNode raorg = XMLDOC.CreateElement("organization");
                raorg.InnerText = (string)_dtt.Select("setting = true and fieldname = 'raOrganization'")[0]["misc"];
                returnAddress.AppendChild(raorg);

                System.Xml.XmlNode raaddress1 = XMLDOC.CreateElement("address1");
                raaddress1.InnerText = (string)_dtt.Select("setting = true and fieldname = 'raAddress1'")[0]["misc"];
                returnAddress.AppendChild(raaddress1);

                System.Xml.XmlNode raaddress2 = XMLDOC.CreateElement("address2");
                raaddress2.InnerText = (string)_dtt.Select("setting = true and fieldname = 'raAddress2'")[0]["misc"];
                returnAddress.AppendChild(raaddress2);

                System.Xml.XmlNode racity = XMLDOC.CreateElement("city");
                racity.InnerText = (string)_dtt.Select("setting = true and fieldname = 'raCity'")[0]["misc"];
                returnAddress.AppendChild(racity);

                System.Xml.XmlNode rastate = XMLDOC.CreateElement("state");
                rastate.InnerText = (string)_dtt.Select("setting = true and fieldname = 'raState'")[0]["misc"];
                returnAddress.AppendChild(rastate);

                System.Xml.XmlNode rapost = XMLDOC.CreateElement("postalCode");
                rapost.InnerText = (string)_dtt.Select("setting = true and fieldname = 'raPostalCode'")[0]["misc"];
                returnAddress.AppendChild(rapost);
            }

            System.Xml.XmlNode recipients = XMLDOC.CreateElement("recipients");
            job.AppendChild(recipients);

            //VARIABLES TO HOLD INDIVIDUAL PARTS

            //SHOW RESULT
            //  MessageBox.Show("Name: " & AddressName)
            //MessageBox.Show("Address1: " & Address1)
            //MessageBox.Show("Address2: " & Address2)
            //MessageBox.Show("Address3: " & Address3)
            //MessageBox.Show("City: " & City)
            //MessageBox.Show("State: " & State)
            //MessageBox.Show("Zip: " & Zip)

            System.Xml.XmlNode address = XMLDOC.CreateElement("address");

            System.Xml.XmlNode xname = null;
            System.Xml.XmlNode xorginization = null;
            System.Xml.XmlNode xAddress1 = null;
            System.Xml.XmlNode xAddress2 = null;
            System.Xml.XmlNode xAddress3 = null;
            System.Xml.XmlNode xCity = null;
            System.Xml.XmlNode xState = null;
            System.Xml.XmlNode xZip = null;
            System.Xml.XmlNode xcountry = null;

            // Private _nName As String
            //        Private _norganization As String
            //Private _nAddress3 As String 'This is not used except for single item
            //        Private _Address1 As String
            //Private _Address2 As String
            //       Private _City As String
            //      Private _State As String
            //     Private _Zip5 As String
            //    Private _Zip4 As String

            xname = XMLDOC.CreateElement("name");
            xname.InnerText = ai.nname;
            xorginization = XMLDOC.CreateElement("organization");
            xorginization.InnerText = ai.norganization;
            xAddress1 = XMLDOC.CreateElement("address1");
            xAddress1.InnerText = Strings.Trim(ai.Address1);
            xAddress2 = XMLDOC.CreateElement("address2");
            xAddress2.InnerText = Strings.Trim(ai.Address2);
            xAddress3 = XMLDOC.CreateElement("address3");
            xAddress3.InnerText = Strings.Trim(ai.nAddress3);
            xCity = XMLDOC.CreateElement("city");
            xCity.InnerText = Strings.Trim(ai.city);
            xState = XMLDOC.CreateElement("state");
            xState.InnerText = Strings.Trim(ai.state);
            xZip = XMLDOC.CreateElement("postalCode");
            xZip.InnerText = Strings.Trim(ai.zip5 + "-" + ai.zip4);
            xcountry = XMLDOC.CreateElement("country");

            address.AppendChild(xname);
            address.AppendChild(xorginization);

            address.AppendChild(xAddress1);
            address.AppendChild(xAddress2);
            address.AppendChild(xAddress3);
            address.AppendChild(xCity);
            address.AppendChild(xState);
            address.AppendChild(xZip);
            address.AppendChild(xcountry);
            recipients.AppendChild(address);
            return true;
        }
        private bool parseaddresssingledoctomultiple(ref addressitem ai, ref XmlNode batchnode, string s, int startpage, int endingpage, int row, ref XmlNode recipients )
        {
            string[] parts = s.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries );
            ai = new addressitem();
            //It is a first page, but now lets see if there is a valid address
            //If address1 validates
            //
            ai.row = row;
            if (batchnode == null) {
                XMLDOC = new System.Xml.XmlDocument();

                System.Xml.XmlNode docNode = XMLDOC.CreateXmlDeclaration("1.0", "UTF-8", null);
                XMLDOC.AppendChild(docNode);
                batchnode = XMLDOC.CreateElement("batch");
                System.Xml.XmlNode ns = null;
                ns = XMLDOC.CreateAttribute("xmlns", "xsi", "http://www.w3.org/2000/xmlns/");
                ns.Value = "http://www.w3.org/2001/XMLSchema-instance";
                //batchnode.Attributes.Append(ns);
                XMLDOC.AppendChild(batchnode);

                System.Xml.XmlNode un = XMLDOC.CreateElement("username");
                batchnode.AppendChild(un);
                un.InnerText = (string) _dtt.Select("setting = true and fieldname = 'username'")[0]["misc"];

                System.Xml.XmlNode pw = XMLDOC.CreateElement("password");
                pw.InnerText = Decrypt((string) _dtt.Select("setting = true and fieldname = 'password'")[0]["misc"]);
                batchnode.AppendChild(pw);
                System.Xml.XmlNode fn = XMLDOC.CreateElement("filename");
                fn.InnerText = _sourcefilename;
                batchnode.AppendChild(fn);
                System.Xml.XmlNode as1 = XMLDOC.CreateElement("appSignature");
                batchnode.AppendChild(as1);
                as1.InnerText = (string) _dtt.Select("setting = true and fieldname = 'appSignature'")[0]["misc"];

                System.Xml.XmlNode job = XMLDOC.CreateElement("job");
                batchnode.AppendChild(job);

                System.Xml.XmlNode startingpage = XMLDOC.CreateElement("startingPage");
                job.AppendChild(startingpage);
                startingpage.InnerText = Convert.ToString(startpage);
                XmlNode endpage = XMLDOC.CreateElement("endingPage");
                endpage.InnerText = Convert.ToString(endingpage);
                job.AppendChild(endpage);

                System.Xml.XmlNode printProductionOptions = XMLDOC.CreateElement("printProductionOptions");
                job.AppendChild(printProductionOptions);
                System.Xml.XmlNode docclass = XMLDOC.CreateElement("documentClass");
                docclass.InnerText = (string)_dtt.Select("setting = true and fieldname = 'poDocumentClass'")[0]["misc"];
                printProductionOptions.AppendChild(docclass);

                System.Xml.XmlNode layout = XMLDOC.CreateElement("layout");
                layout.InnerText = (string)_dtt.Select("setting = true and fieldname = 'poLayout'")[0]["misc"];
                printProductionOptions.AppendChild(layout);

                System.Xml.XmlNode productiontime = XMLDOC.CreateElement("productionTime");
                productiontime.InnerText = (string)_dtt.Select("setting = true and fieldname = 'poProductionTime'")[0]["misc"];
                printProductionOptions.AppendChild(productiontime);

                System.Xml.XmlNode envelope = XMLDOC.CreateElement("envelope");
                envelope.InnerText = (string)_dtt.Select("setting = true and fieldname = 'poEnvelope'")[0]["misc"];
                printProductionOptions.AppendChild(envelope);

                if ("Printing One side" == _dtt.Select("setting = true and fieldname = 'poPrintOption'")[0]["misc"])
                {
                    if (endingpage > 5)
                    {
                        envelope.InnerText = oversized;
                    }

                }
                else
                {
                    if (endingpage > 10)
                    {
                        envelope.InnerText = oversized;
                    }

                }

                System.Xml.XmlNode color = XMLDOC.CreateElement("color");
                color.InnerText = (string)_dtt.Select("setting = true and fieldname = 'poColor'")[0]["misc"];
                printProductionOptions.AppendChild(color);

                System.Xml.XmlNode papertype = XMLDOC.CreateElement("paperType");
                papertype.InnerText = (string)_dtt.Select("setting = true and fieldname = 'poPaperType'")[0]["misc"];
                printProductionOptions.AppendChild(papertype);

                System.Xml.XmlNode printoption = XMLDOC.CreateElement("printOption");
                printoption.InnerText = (string)_dtt.Select("setting = true and fieldname = 'poPrintOption'")[0]["misc"];
                printProductionOptions.AppendChild(printoption);

                System.Xml.XmlNode mailclass = XMLDOC.CreateElement("mailClass");
                mailclass.InnerText = (string)_dtt.Select("setting = true and fieldname = 'poMailClass'")[0]["misc"];
                printProductionOptions.AppendChild(mailclass);

                if (!string.IsNullOrEmpty((string)_dtt.Select("setting = true and fieldname = 'raName'")[0]["misc"]) | !string.IsNullOrEmpty((string)_dtt.Select("setting = true and fieldname = 'raOrganization'")[0]["misc"]))
                {
                    System.Xml.XmlNode returnAddress = XMLDOC.CreateElement("returnAddress");
                    job.AppendChild(returnAddress);

                    System.Xml.XmlNode raname = XMLDOC.CreateElement("name");
                    raname.InnerText = (string)_dtt.Select("setting = true and fieldname = 'raName'")[0]["misc"];
                    returnAddress.AppendChild(raname);

                    System.Xml.XmlNode raorg = XMLDOC.CreateElement("organization");
                    raorg.InnerText = (string)_dtt.Select("setting = true and fieldname = 'raOrganization'")[0]["misc"];
                    returnAddress.AppendChild(raorg);

                    System.Xml.XmlNode raaddress1 = XMLDOC.CreateElement("address1");
                    raaddress1.InnerText = (string)_dtt.Select("setting = true and fieldname = 'raAddress1'")[0]["misc"];
                    returnAddress.AppendChild(raaddress1);

                    System.Xml.XmlNode raaddress2 = XMLDOC.CreateElement("address2");
                    raaddress2.InnerText = (string)_dtt.Select("setting = true and fieldname = 'raAddress2'")[0]["misc"];
                    returnAddress.AppendChild(raaddress2);

                    System.Xml.XmlNode racity = XMLDOC.CreateElement("city");
                    racity.InnerText = (string)_dtt.Select("setting = true and fieldname = 'raCity'")[0]["misc"];
                    returnAddress.AppendChild(racity);

                    System.Xml.XmlNode rastate = XMLDOC.CreateElement("state");
                    rastate.InnerText = (string)_dtt.Select("setting = true and fieldname = 'raState'")[0]["misc"];
                    returnAddress.AppendChild(rastate);

                    System.Xml.XmlNode rapost = XMLDOC.CreateElement("postalCode");
                    rapost.InnerText = (string)_dtt.Select("setting = true and fieldname = 'raPostalCode'")[0]["misc"];
                    returnAddress.AppendChild(rapost);
                }

                recipients = XMLDOC.CreateElement("recipients");
                job.AppendChild(recipients);

            }

            ai = new addressitem();
            ai.startpage = startpage;
            ai.row = row;
            ai.validatedStatus = "false";
            ai.uspsStatus = "Not Processed";
            ai.endpage = endingpage;

            //   If startpage = 8 Then
            //Console.Write(Trim(parts(parts.Length - 1)))
            //End If
            Regex rp = new Regex("((?:\\w|\\s)+),\\s(AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY)(|.(\\d{5}(-\\d{4}|\\d{4}|$)))$");

            if (!rp.IsMatch(Strings.Trim(parts[parts.Length - 1]))) {
                if (Convert.ToBoolean( _dtt.Select("setting = true and fieldname = 'omitNonValidated'")[0]["misc"]) == true) {
                    Console.WriteLine(startpage + " is not a valid address.");
                    badaddress += "Address on row " + row + " is not a valid address, this was omitted." + "\r\n";
                    ai.ommitted = true;
                    return true;
                } else {
                    ai.validatedStatus = "false";
                }
            } else {
                ai.validatedStatus = "true";
            }

            string AddressName = parts[0];
            string Organization = string.Empty;
            string Address1 = string.Empty;
            string Address2 = string.Empty;
            string Address3 = string.Empty;
            string City = string.Empty;
            string State = string.Empty;
            string Zip = string.Empty;
            ai.nname = AddressName;
            try {
                string[] Words = Strings.Trim(parts[parts.Length - 1]).Split(' ');

                //GRAB ZIP (ALWAYS LAST IN THE ARRAY)
                Zip = Words[Words.Length - 1];
                //GRAB STATE ABBR (ALWAYS SECOND TO LAST IN ARRAY)
                State = Words[Words.Length - 2];
                //LOOP REMAINING ARRAY ELEMENT TO FORM CITY NAME
                //(THIS WORKS FOR ANY NUMBER OF WORDS IN CITY NAME)
                for (int i = 0; i <= Words.Length - 3; i++) {
                    City += Words[i] + " ";
                    //ADD SPACE BACK IN BETWEEN WORDS
                }
                City = Strings.Replace(City, ",", "");
            } catch {
            }

            bool checkstandard = Convert.ToBoolean( _dtt.Select("setting = true and fieldname = 'omitNonStandard'")[0]["misc"]);
            bool checkstandardwarning = Convert.ToBoolean( _dtt.Select("setting = true and fieldname = 'omitNonStandardWarning'")[0]["misc"]);

            if (parts.Length >= 5) {
                Address1 = Strings.Trim(parts[1]);
                Address2 = Strings.Trim(parts[2]);
                Address3 = Strings.Trim(parts[3]);

                if (string.IsNullOrEmpty(Address3) & string.IsNullOrEmpty(Address2)) {
                    ai.Address1 = Address1;
                } else if (!string.IsNullOrEmpty(Address3) & string.IsNullOrEmpty(Address2)) {
                    ai.Address1 = Address1;
                    ai.Address2 = Address3;
                } else if (!string.IsNullOrEmpty(Address3) & !string.IsNullOrEmpty(Address2)) {
                    ai.Address1 = Address2;
                    ai.Address2 = Address3;
                } else {
                    ai.Address1 = Address1;
                    ai.Address2 = Address2;
                }

                ai.city = City;
                ai.state = State;
                ai.zip5 = Zip;
                standardizeaddress(ref ai);

                if (!(ai.uspsStatus == "1")) {
                    //Omit for non standard
                    if (ai.uspsStatus == "2" & checkstandardwarning == true) {
                        Console.WriteLine(startpage + " is not a valid address.");
                        badaddress += "Address on row " + row + " IS Valid VIA THE USPS, But this was omitted due to warning." + "\r\n";
                        ai.ommitted = true;
                        return true;
                    } else if (ai.uspsStatus == "2") {
                        Console.WriteLine(startpage + " is allowed through even though there is a warning.");
                    } else if (checkstandard == true) {
                        Console.WriteLine(startpage + " is not a valid address.");
                        badaddress += "Address on row " + row + " is not a valid address VIA THE USPS, this was omitted." + "\r\n";
                        ai.ommitted = true;
                        return true;
                    }
                } else {
                    Organization = Address1;
                    Address1 = ai.Address1;
                    Address2 = ai.Address2;
                    City = ai.city;
                    State = ai.state;
                    Zip = ai.zip5 + "-" + ai.zip4;
                }

            } else if (parts.Length == 4) {
                Address1 = Strings.Trim(parts[1]);
                Address2 = Strings.Trim(parts[2]);

                ai.Address1 = Address1;
                ai.Address2 = Address2;
                ai.city = City;
                ai.state = State;
                ai.zip5 = Zip;
                standardizeaddress(ref ai);
                if (!(ai.uspsStatus == "1")) {
                    //Omit for non standard
                    if (ai.uspsStatus == "2" & checkstandardwarning == true) {
                        Console.WriteLine(startpage + " is not a valid address.");
                        badaddress += "Address on row " + row + " IS Valid VIA THE USPS, But this was omitted due to warning." + "\r\n";
                        ai.ommitted = true;
                        return true;
                    } else if (ai.uspsStatus == "2") {
                        Console.WriteLine(startpage + " is allowed through even though there is a warning.");
                    } else if (checkstandard == true) {
                        Console.WriteLine(startpage + " is not a valid address.");
                        badaddress += "Address on row " + row + " is not a valid address VIA THE USPS, this was omitted." + "\r\n";
                        ai.ommitted = true;
                        return true;
                    }
                } else {
                    Address2 = ai.Address2;
                    Address1 = ai.Address1;
                    City = ai.city;
                    State = ai.state;
                    Zip = ai.zip5 + "-" + ai.zip4;
                }
            } else if (parts.Length >= 3) {
                Address1 = Strings.Trim(parts[1]);
                ai.Address1 = Address1;
                ai.city = City;
                ai.state = State;
                ai.zip5 = Zip;

                standardizeaddress(ref ai);
                if (!(ai.uspsStatus == "1")) {
                    //Omit for non standard
                    if (ai.uspsStatus == "2" & checkstandardwarning == true) {
                        Console.WriteLine(startpage + " is not a valid address.");
                        badaddress += "Address on row " + row + " IS Valid VIA THE USPS, But this was omitted due to warning." + "\r\n";
                        ai.ommitted = true;
                        return true;
                    } else if (ai.uspsStatus == "2") {
                        Console.WriteLine(startpage + " is allowed through even though there is a warning.");
                    } else if (checkstandard == true) {
                        Console.WriteLine(startpage + " is not a valid address.");
                        badaddress += "Address on row " + row + " is not a valid address VIA THE USPS, this was omitted." + "\r\n";
                        ai.ommitted = true;
                        return true;
                    }
                } else {
                    Address1 = ai.Address1;
                    Address2 = ai.Address2;
                    City = ai.city;
                    State = ai.state;
                    Zip = ai.zip5 + "-" + ai.zip4;
                }
            }

            //VARIABLES TO HOLD INDIVIDUAL PARTS

            //SHOW RESULT
            //  MessageBox.Show("Name: " & AddressName)
            //MessageBox.Show("Address1: " & Address1)
            //MessageBox.Show("Address2: " & Address2)
            //MessageBox.Show("Address3: " & Address3)
            //MessageBox.Show("City: " & City)
            //MessageBox.Show("State: " & State)
            //MessageBox.Show("Zip: " & Zip)

            System.Xml.XmlNode address = XMLDOC.CreateElement("address");

            System.Xml.XmlNode xname = null;
            System.Xml.XmlNode xorginization = null;
            System.Xml.XmlNode xAddress1 = null;
            System.Xml.XmlNode xAddress2 = null;
            System.Xml.XmlNode xAddress3 = null;
            System.Xml.XmlNode xCity = null;
            System.Xml.XmlNode xState = null;
            System.Xml.XmlNode xZip = null;
            System.Xml.XmlNode xcountry = null;

            xname = XMLDOC.CreateElement("name");
            xname.InnerText = AddressName;
            xorginization = XMLDOC.CreateElement("organization");
            xorginization.InnerText = Organization;
            xAddress1 = XMLDOC.CreateElement("address1");
            xAddress1.InnerText = Strings.Trim(Address1);
            xAddress2 = XMLDOC.CreateElement("address2");
            xAddress2.InnerText = Strings.Trim(Address2);
            xAddress3 = XMLDOC.CreateElement("address3");
            xAddress3.InnerText = Strings.Trim(Address3);
            xCity = XMLDOC.CreateElement("city");
            xCity.InnerText = Strings.Trim(City);
            xState = XMLDOC.CreateElement("state");
            xState.InnerText = Strings.Trim(State);
            xZip = XMLDOC.CreateElement("postalCode");
            xZip.InnerText = Strings.Trim(Zip);
            xcountry = XMLDOC.CreateElement("country");

            address.AppendChild(xname);
            address.AppendChild(xorginization);

            address.AppendChild(xAddress1);
            address.AppendChild(xAddress2);
            address.AppendChild(xAddress3);
            address.AppendChild(xCity);
            address.AppendChild(xState);
            address.AppendChild(xZip);
            address.AppendChild(xcountry);
            recipients.AppendChild(address);
            return true;
        }
        private bool parseaddresssingle(ref addressitem ai, int totalpages)
        {
            int startpage = 1;
            XmlNode batchnode = null;

            //Validate Text

            //It is a first page, but now lets see if there is a valid address
            //If address1 validates
            //

            XMLDOC = new System.Xml.XmlDocument();
            System.Xml.XmlNode docNode = XMLDOC.CreateXmlDeclaration("1.0", "UTF-8", null);
            XMLDOC.AppendChild(docNode);
            batchnode = XMLDOC.CreateElement("batch");
            System.Xml.XmlNode ns = null;
            ns = XMLDOC.CreateAttribute("xmlns", "xsi", "http://www.w3.org/2000/xmlns/");
            ns.Value = "http://www.w3.org/2001/XMLSchema-instance";
            //batchnode.Attributes.Append(ns);
            XMLDOC.AppendChild(batchnode);

            System.Xml.XmlNode un = XMLDOC.CreateElement("username");
            batchnode.AppendChild(un);
            un.InnerText = (string) _dtt.Select("setting = true and fieldname = 'username'")[0]["misc"];

            System.Xml.XmlNode pw = XMLDOC.CreateElement("password");
            pw.InnerText = Decrypt((string) _dtt.Select("setting = true and fieldname = 'password'")[0]["misc"]);
            batchnode.AppendChild(pw);
            System.Xml.XmlNode fn = XMLDOC.CreateElement("filename");
            fn.InnerText = _sourcefilename;
            batchnode.AppendChild(fn);
            System.Xml.XmlNode as1 = XMLDOC.CreateElement("appSignature");
            batchnode.AppendChild(as1);
            as1.InnerText = (string) _dtt.Select("setting = true and fieldname = 'appSignature'")[0]["misc"];

            // ai = New addressitem
            ai.startpage = 1;
            ai.row = startpage;

            ai.validatedStatus = "false";
            ai.uspsStatus = "Not Processed";

            Regex rp = new Regex("((?:\\w|\\s)+),\\s(AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY)(|.(\\d{5}(-\\d{4}|\\d{4}|$)))$");

            if (!rp.IsMatch(ai.city + ", " + ai.state + " " + ai.zip5)) {
                if (Convert.ToBoolean( _dtt.Select("setting = true and fieldname = 'omitNonValidated'")[0]["misc"]) == true) {
                    Console.WriteLine(1 + " is not a valid address.");
                    badaddress += "Document starting on page " + 1 + " is not a valid address, this was omitted." + "\r\n";
                    ai.ommitted = true;
                    return true;
                } else {
                    ai.validatedStatus = "false";
                }
            } else {
                ai.validatedStatus = "true";
            }

            string AddressName = ai.nname;
            string Organization = string.Empty;
            string Address1 = ai.Address1;
            string Address2 = ai.Address2;
            string Address3 = ai.nAddress3;
            string City = ai.city;
            string State = ai.state;
            string Zip = ai.zip5;

            bool checkstandard = Convert.ToBoolean( _dtt.Select("setting = true and fieldname = 'omitNonStandard'")[0]["misc"]);
            bool checkstandardwarning = Convert.ToBoolean(_dtt.Select("setting = true and fieldname = 'omitNonStandardWarning'")[0]["misc"]);

            if (!string.IsNullOrEmpty(Address3)) {
                Address1 = ai.Address1;
                Address2 = ai.Address2;
                Address3 = ai.nAddress3;

                if (string.IsNullOrEmpty(Address3) & string.IsNullOrEmpty(Address2)) {
                    ai.Address1 = Address1;
                } else if (!string.IsNullOrEmpty(Address3) & string.IsNullOrEmpty(Address2)) {
                    ai.Address1 = Address1;
                    ai.Address2 = Address3;
                } else if (!string.IsNullOrEmpty(Address3) & !string.IsNullOrEmpty(Address2)) {
                    ai.Address1 = Address2;
                    ai.Address2 = Address3;
                } else {
                    ai.Address1 = Address1;
                    ai.Address2 = Address2;
                }

                ai.city = City;
                ai.state = State;
                ai.zip5 = Zip;
                standardizeaddress(ref ai);

                if (!(ai.uspsStatus == "1")) {
                    //Omit for non standard
                    if (ai.uspsStatus == "2" & checkstandardwarning == true) {
                        Console.WriteLine(1 + " is not a valid address.");
                        badaddress += "Document starting on page " + 1 + " IS Valid VIA THE USPS, But this was omitted due to warning." + "\r\n";
                        ai.ommitted = true;
                        return true;
                    } else if (ai.uspsStatus == "2") {
                        Console.WriteLine(1 + " is allowed through even though there is a warning.");
                    } else if (checkstandard == true) {
                        Console.WriteLine(1 + " is not a valid address.");
                        badaddress += "Document starting on page " + 1 + " is not a valid address VIA THE USPS, this was omitted." + "\r\n";
                        ai.ommitted = true;
                        return true;
                    }
                } else {
                    Organization = Address1;
                    Address1 = ai.Address1;
                    Address2 = ai.Address2;
                    City = ai.city;
                    State = ai.state;
                    Zip = ai.zip5 + "-" + ai.zip4;
                }

            } else {
                standardizeaddress(ref ai);
                if (!(ai.uspsStatus == "1")) {
                    //Omit for non standard
                    if (ai.uspsStatus == "2" & checkstandardwarning == true) {
                        Console.WriteLine(startpage + " is not a valid address.");
                        badaddress += "Document starting on page " + startpage + " IS Valid VIA THE USPS, But this was omitted due to warning." + "\r\n";
                        ai.ommitted = true;
                        return true;
                    } else if (ai.uspsStatus == "2") {
                        Console.WriteLine(startpage + " is allowed through even though there is a warning.");
                    } else if (checkstandard == true) {
                        Console.WriteLine(startpage + " is not a valid address.");
                        badaddress += "Document starting on page " + startpage + " is not a valid address VIA THE USPS, this was omitted." + "\r\n";
                        ai.ommitted = true;
                        return true;
                    }
                } else {
                    Address2 = ai.Address2;
                    Address1 = ai.Address1;
                    City = ai.city;
                    State = ai.state;
                    Zip = ai.zip5 + "-" + ai.zip4;
                }

            }

            System.Xml.XmlNode job = XMLDOC.CreateElement("job");
            batchnode.AppendChild(job);

            System.Xml.XmlNode startingpage = XMLDOC.CreateElement("startingPage");
            job.AppendChild(startingpage);
            startingpage.InnerText =Convert.ToString(startpage);
            System.Xml.XmlNode endpage = XMLDOC.CreateElement("endingPage");
            endpage.InnerText = Convert.ToString(totalpages);
            job.AppendChild(endpage);

            System.Xml.XmlNode printProductionOptions = XMLDOC.CreateElement("printProductionOptions");
            job.AppendChild(printProductionOptions);
            System.Xml.XmlNode docclass = XMLDOC.CreateElement("documentClass");
            docclass.InnerText = (string) _dtt.Select("setting = true and fieldname = 'poDocumentClass'")[0]["misc"];
            printProductionOptions.AppendChild(docclass);

            System.Xml.XmlNode layout = XMLDOC.CreateElement("layout");
            layout.InnerText = (string) _dtt.Select("setting = true and fieldname = 'poLayout'")[0]["misc"];
            printProductionOptions.AppendChild(layout);

            System.Xml.XmlNode productiontime = XMLDOC.CreateElement("productionTime");
            productiontime.InnerText = (string) _dtt.Select("setting = true and fieldname = 'poProductionTime'")[0]["misc"];
            printProductionOptions.AppendChild(productiontime);

            System.Xml.XmlNode envelope = XMLDOC.CreateElement("envelope");
            envelope.InnerText = (string) _dtt.Select("setting = true and fieldname = 'poEnvelope'")[0]["misc"];

            if ("Printing One side" == _dtt.Select("setting = true and fieldname = 'poPrintOption'")[0]["misc"]) {
                if (totalpages > 5) {
                    envelope.InnerText = oversized;
                }

            } else {
                if (totalpages > 10) {
                    envelope.InnerText = oversized;
                }

            }

            printProductionOptions.AppendChild(envelope);

            System.Xml.XmlNode color = XMLDOC.CreateElement("color");
            color.InnerText = (string) _dtt.Select("setting = true and fieldname = 'poColor'")[0]["misc"];
            printProductionOptions.AppendChild(color);

            System.Xml.XmlNode papertype = XMLDOC.CreateElement("paperType");
            papertype.InnerText = (string) _dtt.Select("setting = true and fieldname = 'poPaperType'")[0]["misc"];
            printProductionOptions.AppendChild(papertype);

            System.Xml.XmlNode printoption = XMLDOC.CreateElement("printOption");
            printoption.InnerText = (string) _dtt.Select("setting = true and fieldname = 'poPrintOption'")[0]["misc"];
            printProductionOptions.AppendChild(printoption);

            System.Xml.XmlNode mailclass = XMLDOC.CreateElement("mailClass");
            mailclass.InnerText = (string) _dtt.Select("setting = true and fieldname = 'poMailClass'")[0]["misc"];
            printProductionOptions.AppendChild(mailclass);

            if (!string.IsNullOrEmpty((string) _dtt.Select("setting = true and fieldname = 'raName'")[0]["misc"]) | !string.IsNullOrEmpty((string) _dtt.Select("setting = true and fieldname = 'raOrganization'")[0]["misc"])) {
                System.Xml.XmlNode returnAddress = XMLDOC.CreateElement("returnAddress");
                job.AppendChild(returnAddress);

                System.Xml.XmlNode raname = XMLDOC.CreateElement("name");
                raname.InnerText = (string) _dtt.Select("setting = true and fieldname = 'raName'")[0]["misc"];
                returnAddress.AppendChild(raname);

                System.Xml.XmlNode raorg = XMLDOC.CreateElement("organization");
                raorg.InnerText = (string) _dtt.Select("setting = true and fieldname = 'raOrganization'")[0]["misc"];
                returnAddress.AppendChild(raorg);

                System.Xml.XmlNode raaddress1 = XMLDOC.CreateElement("address1");
                raaddress1.InnerText = (string) _dtt.Select("setting = true and fieldname = 'raAddress1'")[0]["misc"];
                returnAddress.AppendChild(raaddress1);

                System.Xml.XmlNode raaddress2 = XMLDOC.CreateElement("address2");
                raaddress2.InnerText = (string) _dtt.Select("setting = true and fieldname = 'raAddress2'")[0]["misc"];
                returnAddress.AppendChild(raaddress2);

                System.Xml.XmlNode racity = XMLDOC.CreateElement("city");
                racity.InnerText = (string) _dtt.Select("setting = true and fieldname = 'raCity'")[0]["misc"];
                returnAddress.AppendChild(racity);

                System.Xml.XmlNode rastate = XMLDOC.CreateElement("state");
                rastate.InnerText = (string) _dtt.Select("setting = true and fieldname = 'raState'")[0]["misc"];
                returnAddress.AppendChild(rastate);

                System.Xml.XmlNode rapost = XMLDOC.CreateElement("postalCode");
                rapost.InnerText = (string) _dtt.Select("setting = true and fieldname = 'raPostalCode'")[0]["misc"];
                returnAddress.AppendChild(rapost);
            }

            System.Xml.XmlNode recipients = XMLDOC.CreateElement("recipients");
            job.AppendChild(recipients);

            //VARIABLES TO HOLD INDIVIDUAL PARTS

            //SHOW RESULT
            //  MessageBox.Show("Name: " & AddressName)
            //MessageBox.Show("Address1: " & Address1)
            //MessageBox.Show("Address2: " & Address2)
            //MessageBox.Show("Address3: " & Address3)
            //MessageBox.Show("City: " & City)
            //MessageBox.Show("State: " & State)
            //MessageBox.Show("Zip: " & Zip)

            System.Xml.XmlNode address = XMLDOC.CreateElement("address");

            System.Xml.XmlNode xname = null;
            System.Xml.XmlNode xorginization = null;
            System.Xml.XmlNode xAddress1 = null;
            System.Xml.XmlNode xAddress2 = null;
            System.Xml.XmlNode xAddress3 = null;
            System.Xml.XmlNode xCity = null;
            System.Xml.XmlNode xState = null;
            System.Xml.XmlNode xZip = null;
            System.Xml.XmlNode xcountry = null;

            xname = XMLDOC.CreateElement("name");
            xname.InnerText = AddressName;
            xorginization = XMLDOC.CreateElement("organization");
            xorginization.InnerText = Organization;
            xAddress1 = XMLDOC.CreateElement("address1");
            xAddress1.InnerText = Strings.Trim(Address1);
            xAddress2 = XMLDOC.CreateElement("address2");
            xAddress2.InnerText = Strings.Trim(Address2);
            xAddress3 = XMLDOC.CreateElement("address3");
            xAddress3.InnerText = Strings.Trim(Address3);
            xCity = XMLDOC.CreateElement("city");
            xCity.InnerText = Strings.Trim(City);
            xState = XMLDOC.CreateElement("state");
            xState.InnerText = Strings.Trim(State);
            xZip = XMLDOC.CreateElement("postalCode");
            xZip.InnerText = Strings.Trim(Zip);
            xcountry = XMLDOC.CreateElement("country");

            address.AppendChild(xname);
            address.AppendChild(xorginization);

            address.AppendChild(xAddress1);
            address.AppendChild(xAddress2);
            address.AppendChild(xAddress3);
            address.AppendChild(xCity);
            address.AppendChild(xState);
            address.AppendChild(xZip);
            address.AppendChild(xcountry);
            recipients.AppendChild(address);
            return true;
        }