//TimeZoneInfo _logTimeZone = TimeZoneInfo.FindSystemTimeZoneById(Settings.Default.APP_LogTimeZoneCultureInfo);

        public MaxCredible_Export()
        {
            InitializeComponent();

            TxtSavedSearch1.Text = Settings.Default.NS_MaxCredible_Savedsearch;

            DTPickerTo.Value = System.DateTime.Now.Date.AddDays(Settings.Default.APP_DefaultDateOffset);
            double delDayOffset = Settings.Default.APP_DefaultDateRange + Settings.Default.APP_DefaultDateOffset;
            DTPickerFrom.Value = System.DateTime.Now.Date.AddDays(delDayOffset);

            if (!Settings.Default.APP_DatesEnabled)
            {
                DTPickerFrom.Visible = false;
                lblDTFrom.Visible = false;
                DTPickerTo.Visible = false;
                lblDTTo.Visible = false;
            }

            _NSservice = new NetSuiteService();
            _NSFileLog.deleteLog();

            _MaxCredibleData = new HTTP_POSTS();
            _MaxCredibleService = new Invoer_mc();

            string environment = "";
            #if useNSSandbox // override with live URLs if set in config file
            environment = "SANDBOX/STAGING";
            if (Settings.Default.APP_UseLiveWebServices)
            {
                environment = "LIVE";
                _NSservice.Url = Settings.Default.NS_com_netsuite_webservices_NetSuiteService;
                _MaxCredibleService.Url = Settings.Default.MaxCredibleExport_com_maxcredible_secure5_Invoer_mc;
            }
            #else // live build so invert the web services URLs logic
            environment = "LIVE";
            if (!Settings.Default.APP_UseLiveWebServices)
            {
                environment = "SANDBOX/STAGING";
                _NSservice.Url = Settings.Default.NS_com_netsuite_sandbox_webservices_NetSuiteService;
                _MaxCredibleService.Url = Settings.Default.MaxCredibleExport_com_maxcredible_secure6_Invoer_mc;
            }
            #endif

            GrpNSSearch.Text += " " + environment;
            GrpMaxCredible.Text += " " + environment;

            _NSservice.CookieContainer = new System.Net.CookieContainer();
            setupNetSuiteObjects(_NSservice);

            //Set up defaults for processing flow
            if (Settings.Default.APP_XMLFileOnly) ChkFileXMLOnly.Checked = true;
            if (Settings.Default.APP_AutoFinish) ChkAutoFinish.Checked = true;
            if (Settings.Default.APP_AutoUpload) ChkAutoUpload.Checked = true;

            if (!ChkFileXMLOnly.Checked)// Read back in and upload to MaxCredible
            {
                try
                {
                    System.Net.ServicePointManager.CertificatePolicy = new MaxCrediblePolicy();
                    X509Certificate Cert = X509Certificate.CreateFromCertFile(Settings.Default.MC_CertificateFile); //Defined in config file
                    _MaxCredibleService.ClientCertificates.Add(Cert);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("MaxCredible Security / Certificate Error :\nCertificate File :" + Settings.Default.MC_CertificateFile + "\n" + ex.Message);
                    ChkFileXMLOnly.Checked = true;
                    ChkFileXMLOnly.Enabled = false;
                }
            }

            if (Settings.Default.APP_AutoSearch) btnGet_Click(null, EventArgs.Empty);
        }
        public void uploadXMLMaxCredible(NetSuiteService service)
        {
            //Start processing for MaxCredible upload
            int currentMCRecord= 0;
            int currentMCChunk = 1;
            HTTP_POSTS theseHTTPSPOSTS = new HTTP_POSTS();
            theseHTTPSPOSTS.NR_RECORDS = _SSRECORDS.Length.ToString();
            int MCchunkSize = Convert.ToInt32(_MaxCredibleData.CHUNK_SIZE);
            int MCchunkQty = Convert.ToInt32(TxtChunkQty.Text);
            ProgressMCUpload.Maximum = _SSRECORDS.Length;
            string MCBatchName = Settings.Default.MC_XMLOutputPrefix + System.DateTime.Now.ToString("ddMMyyHHmmss");
            for (int i = 0; i < _SSRECORDS.Length; i++)
            {
                TransactionSearchRow TSR = (TransactionSearchRow)_SSRECORDS[i];
                theseHTTPSPOSTS.CHUNK_NR = currentMCChunk.ToString();
                currentMCRecord = i + 1;
                HTTP_POST thisHTTPPOST = createHTTP_POST(TSR);
                thisHTTPPOST.RECORD_NR = currentMCRecord.ToString();
                theseHTTPSPOSTS.HTTP_POST.Add(thisHTTPPOST);
                AppendTextBoxLine(TxtMaxCredibleUpload, "  Invoice " + currentMCRecord.ToString("0000") + "\t" + thisHTTPPOST.FACTUURNUMMER, true);

                if ((currentMCRecord % MCchunkSize == 0 && i != 0) || currentMCRecord == _SSRECORDS.Length)
                {
                    XmlSerializer XMLData = new XmlSerializer(theseHTTPSPOSTS.GetType());
                    if (System.IO.Directory.Exists(Settings.Default.MC_XMLOutputDir))
                    {
                        StreamWriter XMLfile = new StreamWriter(Settings.Default.MC_XMLOutputDir + MCBatchName + currentMCChunk.ToString("0000") + ".xml", false);
                        XMLData.Serialize(XMLfile, theseHTTPSPOSTS);
                        XMLfile.Close();
                        AppendTextBoxLine(TxtMaxCredibleUpload, "Chunk " + currentMCChunk + " of " + MCchunkQty + " created", true);
                    }

                    if (!ChkFileXMLOnly.Checked)// Read back in and upload to MaxCredible
                    {
                        try
                        {
                            XmlNode MCResponseNode;
                            XmlDocument MCRequestDoc = new XmlDocument();
                            XmlTextReader XMLTextReader = new XmlTextReader(Settings.Default.MC_XMLOutputDir + MCBatchName + currentMCChunk.ToString("0000") + ".xml");
                            MCRequestDoc.Load(XMLTextReader);
                            string MCXML = MCRequestDoc.OuterXml.Replace("<HTTP_POST><HTTP_POST>", "<HTTP_POST>").Replace("</HTTP_POST></HTTP_POST>", "</HTTP_POST>"); //Remove outer list node as MaxCredible does not implement
                            //This really needs more elegant XML node / attribute transform handling but just quick string replacements at the moment ...
                            //Before - <HTTP_POSTS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                            //After - <HTTP_POSTS xmlns="http://www.MaxCredible.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="Posten_Batch.xsd">
                            MCXML = MCXML.Replace("<HTTP_POSTS ", "<HTTP_POSTS xmlns=\"http://www.MaxCredible.com/\"  xsi:schemaLocation=\"Posten_Batch.xsd\" ");
                            MCXML = MCXML.Replace(" encoding=\"utf-8\"", ""); //Breaks if encoding present
                            System.IO.File.WriteAllText(Settings.Default.MC_XMLOutputDir + "Request_" + currentMCChunk.ToString("0000") + ".xml", MCXML);
                            MCResponseNode = _MaxCredibleService.XMLBatchReceive(Settings.Default.MC_UserName, Settings.Default.MC_Password, MCBatchName, MCXML);
                            AppendTextBoxLine(TxtMaxCredibleUpload, "MC Response Chunk " + currentMCChunk.ToString("0000") + " : " + MCResponseNode.OuterXml, true);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("MaxCredible Web Services Error :\n" + ex.Message);
                        }
                    }

                    currentMCChunk++;
                    ProgressMCUpload.Value = currentMCRecord;
                    theseHTTPSPOSTS.HTTP_POST.Clear(); //Ready for next chunk
                }                
            }

            //Write textbox contents to log file - deprecated as now done as text box appended
            //if (Settings.Default.APP_LogFileName != "")
            //{
            //    System.IO.File.WriteAllText(Settings.Default.MC_XMLOutputDir + Settings.Default.APP_LogFileName, TxtMaxCredibleUpload.Text);
            //    AppendTextBoxLine(TxtMaxCredibleUpload, "Log file created : " + Settings.Default.APP_LogFileName, false);
            //}
        }