public List<object> GetRateFromUsps(WareHouseAddress originAddress, DestinationAddress destination, ArrayList plist, int providerId, int storeId, int portalId)
        {
            var lists = new List<object>();
            List<USPSPackage> list = new List<USPSPackage>(plist.Count);
            list.AddRange(plist.Cast<USPSPackage>());
            try
            {
                if (originAddress.Country == "US")
                {
                    List<USPSRateResponse> rateResponses;
                    if (originAddress.Country.ToLower() == destination.ToCountry.ToLower())
                    {
                        rateResponses = GetRate(originAddress, destination, list, true, providerId, storeId, portalId);
                    }
                    else
                    {
                        rateResponses = GetRate(originAddress, destination, list, false, providerId, storeId, portalId);
                    }


                    foreach (var item in rateResponses)
                    {
                        if (string.IsNullOrEmpty(item.ErrorMessage))
                        {
                            if (item.IsDomestic)
                            {
                                lists.AddRange(item.Postage.Cast<object>());
                            }
                            else
                            {
                                lists.AddRange(item.IntRateList.Cast<object>());
                            }
                        }
                        else
                        {
                            lists.Add(item);
                        }

                    }




                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

            return lists;
        }
        public List<USPSRateResponse> GetRate(WareHouseAddress originAddress, DestinationAddress destination, List<USPSPackage> plist, bool isDomestic, int providerId, int storeId, int portalId)
        {

            //GetOriginAddress()
            _originAddress = originAddress;
            List<USPSRateResponse> response;
            LoadConfig(providerId, storeId, portalId);
            XmlDocument xx = new XmlDocument();
            string api = ""; //RateV4Request
            XmlNode wrap;
            XmlNode userId;

            if (isDomestic)
            {
                api = "RateV4";
                wrap = xx.CreateNode(XmlNodeType.Element, "RateV4Request", "");
                userId = xx.CreateNode(XmlNodeType.Attribute, "USERID", "");
                userId.Value = this._userId;
                wrap.Attributes.SetNamedItem(userId);
            }
            else
            {
                if (_isItemWiseCalculation)
                {
                    api = "IntlRateV2";
                    wrap = xx.CreateNode(XmlNodeType.Element, "IntlRateV2Request", "");
                    userId = xx.CreateNode(XmlNodeType.Attribute, "USERID", "");
                    userId.Value = this._userId;
                    wrap.Attributes.SetNamedItem(userId);
                }
                else
                {
                    api = "IntlRate";
                    wrap = xx.CreateNode(XmlNodeType.Element, "IntlRateRequest", "");
                    userId = xx.CreateNode(XmlNodeType.Attribute, "USERID", "");
                    userId.Value = this._userId;
                    wrap.Attributes.SetNamedItem(userId);
                }



            }
            int i = 1;

            foreach (var package in plist)
            {
                XmlNode root = xx.CreateNode(XmlNodeType.Element, "Package", "");
                XmlNode ids = xx.CreateNode(XmlNodeType.Attribute, "ID", "");
                ids.Value = i.ToString();
                root.Attributes.SetNamedItem(ids); //save attribute value

                if (isDomestic)
                {

                    decimal totalWeight = 0;
                    totalWeight = package.Quantity * package.WeightValue;

                    XmlNode node = xx.CreateNode(XmlNodeType.Element, "Service", "");
                    root.AppendChild(node); // append node to root 
                    root.SelectSingleNode("Service").InnerXml = package.ServiceType.ToString();

                    //if  servicetype=all then no mailtype specification
                    //  root.AppendChild(xx.CreateNode(XmlNodeType.Element, "MailType", ""));
                    //  root.SelectSingleNode("MailType").InnerXml = package.ServiceType.ToString();

                    root.AppendChild(xx.CreateNode(XmlNodeType.Element, "ZipOrigination", ""));
                    root.SelectSingleNode("ZipOrigination").InnerXml = _originAddress.PostalCode; //package.OriginZipcode.ToString();

                    root.AppendChild(xx.CreateNode(XmlNodeType.Element, "ZipDestination", ""));
                    root.SelectSingleNode("ZipDestination").InnerXml = destination.ToPostalCode;
                    if (package.WeightUnit.ToLower() == WeightUnits.POUNDS.ToString().ToLower() ||
                            package.WeightUnit.ToLower() == "pound" ||
                            package.WeightUnit.ToLower() == "lb" ||
                          package.WeightUnit.ToLower() == WeightUnits.LBS.ToString().ToLower())
                    {
                        //pound to ounce conversion
                        root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Pounds", ""));
                        root.SelectSingleNode("Pounds").InnerXml = totalWeight.ToString();

                        root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Ounces", ""));
                        root.SelectSingleNode("Ounces").InnerXml = "0";// (package.WeightValue * 16).ToString();
                    }
                    else if (package.WeightUnit.ToLower() == WeightUnits.KGS.ToString().ToLower() ||
                            package.WeightUnit.ToLower() == "kg")
                    {
                        //pound to ounce conversion
                        root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Pounds", ""));
                        root.SelectSingleNode("Pounds").InnerXml =
                            (double.Parse(package.WeightValue.ToString()) * 2.20462262185).ToString();

                        root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Ounces", ""));
                        root.SelectSingleNode("Ounces").InnerXml = "0";
                        // (double.Parse(package.WeightValue.ToString()) * 2.20462262185 * 16).ToString();
                    }
                    root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Container", ""));
                    if (package.Container.ToString().ToLower() != "none")
                        root.SelectSingleNode("Container").InnerXml = package.Container.ToString();

                    root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Size", ""));
                    root.SelectSingleNode("Size").InnerXml = package.PackageSize.ToString();

                    root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Machinable", ""));
                    root.SelectSingleNode("Machinable").InnerXml = package.Machinable.ToString();

                    if (package.Container.ToString().ToLower() == "none")
                    {

                        //root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Width", ""));
                        //root.SelectSingleNode("Width").InnerXml = package.Width.ToString();

                        //root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Length", ""));
                        //root.SelectSingleNode("Length").InnerXml = package.Length.ToString();

                        //root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Height", ""));
                        //root.SelectSingleNode("Height").InnerXml = package.Height.ToString();

                        ////for international rate
                        //// The "girth" of the package as measured in inches rounded
                        ////to the nearest whole inch. Required to obtain GXG pricing
                        ////when pricing and when Size=”LARGE” and
                        ////Container=”NONRECTANGULAR”.
                        ////For example: <Girth>15</Girth>
                        //root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Girth", ""));
                        //root.SelectSingleNode("Girth").InnerXml = package.Girth.ToString();
                    }

                    // root.AppendChild(node);//append node to root 

                }
                else
                {
                    if (_isItemWiseCalculation)
                    {
                        decimal totalWeight = 0;
                        totalWeight = package.Quantity * package.WeightValue;
                        if (package.WeightUnit.ToLower() == WeightUnits.POUNDS.ToString().ToLower() ||
                            package.WeightUnit.ToLower() == "pound" ||
                            package.WeightUnit.ToLower() == "lb" ||
                          package.WeightUnit.ToLower() == WeightUnits.LBS.ToString().ToLower())
                        {
                            //pound to ounce conversion
                            root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Pounds", ""));
                            root.SelectSingleNode("Pounds").InnerXml = totalWeight.ToString();

                            root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Ounces", ""));
                            root.SelectSingleNode("Ounces").InnerXml = "0";//(totalWeight * 16).ToString();
                        }
                        else if (package.WeightUnit.ToLower() == WeightUnits.KGS.ToString().ToLower() ||
                            package.WeightUnit.ToLower() == "kg")
                        {
                            //pound to ounce conversion
                            root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Pounds", ""));
                            root.SelectSingleNode("Pounds").InnerXml =
                                (double.Parse(totalWeight.ToString()) * 2.20462262185).ToString();

                            root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Ounces", ""));
                            root.SelectSingleNode("Ounces").InnerXml = "0";
                            // (double.Parse(totalWeight.ToString())*2.20462262185*16).ToString();
                        }
                        root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Machinable", ""));
                        root.SelectSingleNode("Machinable").InnerXml = package.Machinable.ToString();

                        //if  servicetype=all then no mailtype specification
                        root.AppendChild(xx.CreateNode(XmlNodeType.Element, "MailType", ""));
                        root.SelectSingleNode("MailType").InnerXml = package.MailType.ToString();

                        XmlNode gxg = xx.CreateNode(XmlNodeType.Element, "GXG", "");
                        XmlNode poboxFlag = xx.CreateNode(XmlNodeType.Element, "POBoxFlag", "");
                        gxg.AppendChild(poboxFlag);
                        XmlNode giftFlag = xx.CreateNode(XmlNodeType.Element, "GiftFlag", "");
                        gxg.AppendChild(giftFlag);
                        gxg.SelectSingleNode("POBoxFlag").InnerXml = package.PoBoxFlag.ToString();
                        gxg.SelectSingleNode("GiftFlag").InnerXml = package.GiftFlag.ToString();

                        root.AppendChild(xx.CreateNode(XmlNodeType.Element, "ValueOfContents", ""));
                        root.SelectSingleNode("ValueOfContents").InnerXml = package.ValueOfContents.ToString();
                        root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Country", ""));
                        root.SelectSingleNode("Country").InnerXml = destination.ToCountryName;
                        root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Container", ""));
                        root.SelectSingleNode("Container").InnerXml = package.Container.ToString();
                        root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Size", ""));
                        root.SelectSingleNode("Size").InnerXml = package.Size.ToString();
                        root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Width", ""));
                        root.SelectSingleNode("Width").InnerXml = package.Width.ToString();
                        root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Length", ""));
                        root.SelectSingleNode("Length").InnerXml = package.Length.ToString();
                        root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Height", ""));
                        root.SelectSingleNode("Height").InnerXml = package.Height.ToString();
                        root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Girth", ""));
                        root.SelectSingleNode("Girth").InnerXml = package.Girth.ToString();
                        root.AppendChild(xx.CreateNode(XmlNodeType.Element, "CommercialFlag", ""));
                        root.SelectSingleNode("CommercialFlag").InnerXml = package.CommercialFlag.ToString();
                    }
                    else
                    {

                        decimal totalWeight = plist.Sum(item => item.Quantity * item.WeightValue);// plist.Sum(packageweight => packageweight.WeightValue);

                        if (package.WeightUnit.ToLower() == WeightUnits.POUNDS.ToString().ToLower() ||
                            package.WeightUnit.ToLower() == "pound" ||
                            package.WeightUnit.ToLower() == "lb" ||
                          package.WeightUnit.ToLower() == WeightUnits.LBS.ToString().ToLower())
                        {
                            //pound to ounce conversion
                            root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Pounds", ""));
                            root.SelectSingleNode("Pounds").InnerXml = totalWeight.ToString();

                            //applying only one weight is ok 
                            root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Ounces", ""));
                            root.SelectSingleNode("Ounces").InnerXml = "0";// (totalWeight * 16).ToString();
                        }
                        else if (package.WeightUnit.ToLower() == WeightUnits.KGS.ToString().ToLower() ||
                            package.WeightUnit.ToLower() == "kg")
                        {
                            //pound to ounce conversion
                            root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Pounds", ""));
                            root.SelectSingleNode("Pounds").InnerXml =
                                (double.Parse(totalWeight.ToString()) * 2.20462262185).ToString();

                            root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Ounces", ""));
                            root.SelectSingleNode("Ounces").InnerXml = "0";
                            // (double.Parse(totalWeight.ToString()) * 2.20462262185 * 16).ToString();
                        }
                        root.AppendChild(xx.CreateNode(XmlNodeType.Element, "MailType", ""));
                        root.SelectSingleNode("MailType").InnerXml = package.MailType.ToString();
                        root.AppendChild(xx.CreateNode(XmlNodeType.Element, "Country", ""));
                        root.SelectSingleNode("Country").InnerXml = destination.ToCountryName;
                        wrap.AppendChild(root);
                        xx.AppendChild(wrap);

                        break;
                    }

                }


                //  root.AppendChild(node);
                wrap.AppendChild(root);
                xx.AppendChild(wrap);

                i++;
            }
            XmlDocument rateResponse = SendRequestToProvider(xx, api);
            //calling parsefunction
            if (!isDomestic)
            {
                if (_isItemWiseCalculation)
                    response = ParseRateInterNationalV2(rateResponse);
                else
                    response = ParseRateInterNational(rateResponse);
            }
            else
            {

                response = ParseRate(rateResponse);
            }

            return response;
        }
    private void BindShippingAddress(DestinationAddress shippingAddress)
    {
        StringBuilder shippingAdr = new StringBuilder();
        shippingAdr.Append("<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\"><tr>");
        shippingAdr.Append("<td>Customer Name:</td>");
        shippingAdr.Append("<td>" + shippingAddress.FirstName + " " + shippingAddress.LastName + "</td></tr><tr>");
        shippingAdr.Append("<td>Address:</td>");
        shippingAdr.Append("<td>" + shippingAddress.ToAddress + "</td></tr><tr>");
        if (!string.IsNullOrEmpty(shippingAddress.ToAddress2))
        {
            shippingAdr.Append("<td>Address2:</td>");
            shippingAdr.Append("<td>" + shippingAddress.ToAddress2 + "</td></tr><tr>");
        }
        shippingAdr.Append("<td>Country:</td>");
        shippingAdr.Append("<td>" + shippingAddress.ToCountry + "</td></tr><tr>");
        shippingAdr.Append("<td>City:</td>");
        shippingAdr.Append("<td>" + shippingAddress.ToCity + "</td></tr><tr>");
        shippingAdr.Append("<td>State:</td>");
        shippingAdr.Append("<td>" + shippingAddress.ToState + "</td></tr><tr>");
        shippingAdr.Append("<td>ZipCode:</td>");
        shippingAdr.Append("<td>" + shippingAddress.Zip + "</td></tr><tr>");
        shippingAdr.Append("<td>Email Address:</td>");
        shippingAdr.Append("<td>" + shippingAddress.Email + "</td></tr><tr>");
        shippingAdr.Append("<td>Phone No:</td>");
        shippingAdr.Append("<td>" + shippingAddress.Phone + "</td></tr>");
        shippingAdr.Append("</table>");
        ltShippingAddress.Text = shippingAdr.ToString();

        _toAddress = shippingAddress;
        _toAddress.ToCountryName = "";
        Session["sl_toAddress"] = _toAddress;

    }