public static void _SA_OpenCompany(string dbPath)
        {
            if (!OpenCompany.repo.SelfInfo.Exists())
            {
                OpenCompany._Invoke();

                SimplyMessage._SA_HandleMessage(SimplyMessage.repo.Yes, SimplyMessage.repo._Msg_AreYouSureYouAreFinishedWithThisCompany);

                if (SimplyMessage.repo._Msg_DoyouwanttobackupthiscompanybeforeclosingInfo.Exists())
                {
                    SimplyMessage.repo.NoRadioBtn.Click();
                    SimplyMessage.repo.DoNotAskMeAgain.Check();
                    SimplyMessage.repo.OK.Click();
                }
            }

            OpenCompany.repo.FileName.TextValue = dbPath;
            OpenCompany.repo.Open.Click();

            // if necessary convert db, then handle messages to get to home window
            Simply._SA_GotoHomeWindow();

            // set flavor
            if ((!Functions.GoodData(Variables.bAcctEd)) || (Variables.bAcctEd))
            {
                Simply._SA_SetFlavorVariables();
            }

            // if we don't do this it will populate with previous db
            Settings._SA_GetCompanyInformation();
        }
        public static void _SA_StartProgram(bool bUseSample, string CompanyPath)
        {
            // or hard wait?
            while (!SelectCompany.repo.SelfInfo.Exists(Variables.iExistWaitTime))
            {
                Thread.Sleep(1000);
            }

            if (!SelectCompany.repo.Self.Visible)
            {
                _SA_StartSage50();

//                // handle the PU download dialog
//                if (DownloadUpdate.Instance.Window.Exists())
//                {
//                    DownloadUpdate.Instance.Window.SetActive();
//                    DownloadUpdate.Instance.DownloadLater.Click();
//                }

                // check if restart message shows
                // SimplyMessage._SA_HandleMessage(SimplyMessage.repo.No);
            }

            // we have to check this when the product is not yet registered
            // the below window only shows when company is already registered, therefore we need it to skip everything with registration window shows
            while (!SelectCompany.repo.Self.Visible)
            {
                Thread.Sleep(1000);
            }

            SelectCompany.repo.Self.Activate();

            if (Functions.GoodData(CompanyPath))
            {
                SelectCompany.repo.SelectAnExistingCompany.Click();
                SelectCompany.repo.OK.Click();

                OpenCompany.repo.Self.Activate();
                Simply._SA_OpenCompany(CompanyPath);
            }
            else
            {
                if (bUseSample)
                {
                    SelectCompany.repo.OpenSampleCompany.Click();
                }
                else    // defaults to last company opened
                {
                    // A Recently used company radio button and Last company you worked on are the same
                    if (SelectCompany.repo.OpenARecentlyUsedCompanyInfo.Exists())
                    {
                        SelectCompany.repo.OpenARecentlyUsedCompany.Click();
                    }
                    else        // log error
                    {
                        Functions.Verify(false, true, "The radio list item to open the last company used is found");
                    }
                }
                SelectCompany.repo.Self.Activate();
                SelectCompany.repo.OK.Click();

                // If necessary wait for CM service to start
                // Handle CM error, in main machine CM service is not started when opening sample db
                while (!SimplyMessage.repo.CmMessageTextInfo.Exists())
                {
                    if (UpgradeCompany.repo.SelfInfo.Exists() || SimplyMessage.repo.SelfInfo.Exists())
                    {
                        break;
                    }
                }

                if (SimplyMessage.repo.CmMessageTextInfo.Exists())                      // Ranorex see select company as SimplyMessage as well
                {
                    if (SimplyMessage.repo.CmMessageText.TextValue.Contains(SimplyMessage.sCmNotRunningMsg) || SimplyMessage.repo.CmMessageText.TextValue.Contains(SimplyMessage.sCmNotFoundMsg))
                    {
                        try
                        {
                            iCmTry++;
                            // Maximum 7 tries
                            if (iCmTry > 8)
                            {
                                Functions.Verify(false, true, "CM service started");
                            }
                            else
                            {
                                // wait
                                System.Threading.Thread.Sleep(10000);
                                SimplyMessage.repo.Self.Activate();
                                // SimplyMessage.repo.TryAgain.Click();
                                SimplyMessage.repo.Close.Click();                                       // if tryagain doesn't work
                                // Try from select company dialog again
                                _SA_StartProgram(bUseSample, CompanyPath);
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                    }
                }
                else
                {
                    // for beta
                    // SimplyMessage.Instance._SA_HandleMessage(SimplyMessage.OK_LOC);

                    // handle messages and stuff to get to home window
                    Simply._SA_GotoHomeWindow();

                    // get the build number
                    //binfo.sBuildNumber =GetSimplyBuildNumber( + Simply.Instance._SA_GetProgramPath () + "simplyaccounting.exe"); NC

                    // set flavor
                    Simply._SA_SetFlavorVariables();

                    // get version Cdn or US
                    // Simply._SA_getVersion (true);
                }
            }
        }
        public static void _SA_Create(COMPANY Company)
        {
            Simply.repo.Self.Activate();
            Simply.repo.File.Click();
            Simply.repo.NewCompany.Click();

            SimplyMessage._SA_HandleMessage(SimplyMessage.repo.Yes, SimplyMessage.repo._Msg_AreYouSureYouAreFinishedWithThisCompany);

            NewCompanyWizard.repo.Next.Click();

            if (NewCompanyWizard.repo.QuantumInfo.Exists())
            {
                switch ((int)Company.edition)
                {
                case 1:
                    NewCompanyWizard.repo.First.Click();
                    break;

                case 2:
                    NewCompanyWizard.repo.Pro.Click();
                    break;

                case 3:
                    NewCompanyWizard.repo.Premium.Click();
                    break;

                case 4:
                    NewCompanyWizard.repo.Quantum.Click();
                    break;

                default:
                    NewCompanyWizard.repo.Premium.Click();
                    break;
                }

                NewCompanyWizard.repo.Next.Click();
            }

            NewCompanyWizard.repo.Name.TextValue = Company.companyInformation.companyName;
            if (Functions.GoodData(Company.companyInformation.Address) && Functions.GoodData(Company.companyInformation.Address.street1))
            {
                NewCompanyWizard.repo.Street1.TextValue = Company.companyInformation.Address.street1;
            }
            if (Functions.GoodData(Company.companyInformation.Address) && Functions.GoodData(Company.companyInformation.Address.street2))
            {
                NewCompanyWizard.repo.Street2.TextValue = Company.companyInformation.Address.street2;
            }
            if (Functions.GoodData(Company.companyInformation.Address) && Functions.GoodData(Company.companyInformation.Address.city))
            {
                NewCompanyWizard.repo.City.TextValue = Company.companyInformation.Address.city;
            }
//			if  (Variables.productVersion ==  "Canadian")
//			{
            if (Functions.GoodData(Company.companyInformation.Address) && Functions.GoodData(Company.companyInformation.Address.provinceCode))
            {
                NewCompanyWizard.repo.Province.Select(Company.companyInformation.Address.provinceCode);
            }
            if (Functions.GoodData(Company.companyInformation.Address) && Functions.GoodData(Company.companyInformation.Address.province))
            {
                NewCompanyWizard.repo.ProvinceName.TextValue = Company.companyInformation.Address.province;
            }
//			}
//			else
//			{
//				if (Functions.GoodData (Company.companyInformation.Address) && Functions.GoodData (Company.companyInformation.Address.State))
//				{
//					NewCompanyWizard.Instance.State.SetText (Company.companyInformation.Address.State);
//				}
//			}
            if (Functions.GoodData(Company.companyInformation.Address) && Functions.GoodData(Company.companyInformation.Address.country))
            {
                NewCompanyWizard.repo.Country.TextValue = Company.companyInformation.Address.country;
            }
            if (Functions.GoodData(Company.companyInformation.Address) && Functions.GoodData(Company.companyInformation.Address.postalCode))
            {
                NewCompanyWizard.repo.Postal.TextValue = Company.companyInformation.Address.postalCode;
            }
            if (Functions.GoodData(Company.companyInformation.Address) && Functions.GoodData(Company.companyInformation.Address.phone1))
            {
                NewCompanyWizard.repo.Phone1.TextValue = Company.companyInformation.Address.phone1;
            }
            if (Functions.GoodData(Company.companyInformation.Address) && Functions.GoodData(Company.companyInformation.Address.phone2))
            {
                NewCompanyWizard.repo.Phone2.TextValue = Company.companyInformation.Address.phone2;
            }
            if (Functions.GoodData(Company.companyInformation.Address) && Functions.GoodData(Company.companyInformation.Address.fax))
            {
                NewCompanyWizard.repo.Fax.TextValue = Company.companyInformation.Address.fax;
            }
            NewCompanyWizard.repo.Next.Click();

            if (!(Functions.GoodData(Company.companyInformation.fiscalStart)))
            {
                Company.companyInformation.fiscalStart = "1/1/" + Variables.sLongYear + "";
            }
            NewCompanyWizard.repo.FiscalStart.TextValue = Company.companyInformation.fiscalStart;
            if (Functions.GoodData(Company.companyInformation.earliestTransaction))
            {
                NewCompanyWizard.repo.EarliestTransaction.TextValue = Company.companyInformation.earliestTransaction;
            }
            else
            {
                Company.companyInformation.earliestTransaction = NewCompanyWizard.repo.EarliestTransaction.TextValue;
            }
            if (Functions.GoodData(Company.companyInformation.fiscalEnd))
            {
                NewCompanyWizard.repo.FiscalEnd.TextValue = Company.companyInformation.fiscalEnd;
            }
            else
            {
                Company.companyInformation.fiscalEnd = NewCompanyWizard.repo.FiscalEnd.TextValue;
            }
            NewCompanyWizard.repo.Next.Click();

            // Select default the list of accounts
            NewCompanyWizard.repo.Next.Click();

            if (Company.ownership != 0)
            {
                NewCompanyWizard.repo.Ownership.Select(Company.ownership.ToString());
            }
            else
            {
                Company.ownership = (OWNERSHIP)NewCompanyWizard.repo.Ownership.SelectedItemIndex;
            }
            if (Company.industryType != 0)
            {
                NewCompanyWizard.repo.IndustryType.Select(Company.industryType.ToString());
            }
            else
            {
                Company.industryType = (INDUSTRY_TYPE)NewCompanyWizard.repo.IndustryType.SelectedItemIndex;
            }
            if (Functions.GoodData(Company.companyType))
            {
                NewCompanyWizard.repo.CompanyType.SelectListItem(Company.companyType);
            }
            else
            {
                Company.companyType = NewCompanyWizard.repo.CompanyType.SelectedItemText;
            }

            // Account ranges
            if (Company.AccountDetails.accountNumberDigits != 0)  // temp using till good data is properly updated
            {
                NewCompanyWizard.repo.AccountRanges.Click();

                if (Functions.GoodData(Company.AccountDetails.accountNumberDigits))
                {
                    AccountInformation.repo.Digits.Select(Company.AccountDetails.accountNumberDigits.ToString());
                }
                else
                {
                    Company.AccountDetails.accountNumberDigits = 4;
                }

                AccountInformation.repo.AccountContainer.MoveRight();
                if (Functions.GoodData(Company.AccountDetails.startingAssetAccountNumber))
                {
                    AccountInformation.repo.AccountContainer.SetText(Company.AccountDetails.startingAssetAccountNumber);
                }
                AccountInformation.repo.AccountContainer.MoveRight();
                if (Functions.GoodData(Company.AccountDetails.endingAssetAccountNumber))
                {
                    AccountInformation.repo.AccountContainer.SetText(Company.AccountDetails.endingAssetAccountNumber);
                }
                AccountInformation.repo.AccountContainer.MoveRight();
                if (Functions.GoodData(Company.AccountDetails.startingLiabilityAccountNumber))
                {
                    AccountInformation.repo.AccountContainer.SetText(Company.AccountDetails.startingLiabilityAccountNumber);
                }
                AccountInformation.repo.AccountContainer.MoveRight();
                if (Functions.GoodData(Company.AccountDetails.endingLiabilityAccountNumber))
                {
                    AccountInformation.repo.AccountContainer.SetText(Company.AccountDetails.endingLiabilityAccountNumber);
                }
                AccountInformation.repo.AccountContainer.MoveRight();
                if (Functions.GoodData(Company.AccountDetails.startingEquityAccountNumber))
                {
                    AccountInformation.repo.AccountContainer.SetText(Company.AccountDetails.startingEquityAccountNumber);
                }
                AccountInformation.repo.AccountContainer.MoveRight();
                if (Functions.GoodData(Company.AccountDetails.endingEquityAccountNumber))
                {
                    AccountInformation.repo.AccountContainer.SetText(Company.AccountDetails.endingEquityAccountNumber);
                }
                AccountInformation.repo.AccountContainer.MoveRight();
                if (Functions.GoodData(Company.AccountDetails.startingRevenueAccountNumber))
                {
                    AccountInformation.repo.AccountContainer.SetText(Company.AccountDetails.startingRevenueAccountNumber);
                }
                AccountInformation.repo.AccountContainer.MoveRight();
                if (Functions.GoodData(Company.AccountDetails.endingRevenueAccountNumber))
                {
                    AccountInformation.repo.AccountContainer.SetText(Company.AccountDetails.endingRevenueAccountNumber);
                }
                AccountInformation.repo.AccountContainer.MoveRight();
                if (Functions.GoodData(Company.AccountDetails.startingExpenseAccountNumber))
                {
                    AccountInformation.repo.AccountContainer.SetText(Company.AccountDetails.startingExpenseAccountNumber);
                }
                AccountInformation.repo.AccountContainer.MoveRight();
                if (Functions.GoodData(Company.AccountDetails.endingExpenseAccountNumber))
                {
                    AccountInformation.repo.AccountContainer.SetText(Company.AccountDetails.endingExpenseAccountNumber);
                }
                AccountInformation.repo.AccountContainer.MoveRight();
                AccountInformation.repo.OK.Click();
            }
            else
            {
                Company.AccountDetails.accountNumberDigits = 4;
            }
            NewCompanyWizard.repo.Next.Click();

            if (Functions.GoodData(Company.companyNameFile))
            {
                NewCompanyWizard.repo.CompanyName.TextValue = Company.companyNameFile;
            }
            else
            {
                Company.companyNameFile = NewCompanyWizard.repo.CompanyName.TextValue;
            }
            if (Functions.GoodData(Company.companyFileLocation))
            {
                NewCompanyWizard.repo.Location.TextValue = Company.companyFileLocation;
            }
            else
            {
                Company.companyFileLocation = NewCompanyWizard.repo.Location.TextValue;
            }
            NewCompanyWizard.repo.Next.Click();

            //SimplyMessage._SA_HandleMessage(SimplyMessage.Yes, SimplyMessage._Msg_TheFolderAndFileDoNotExist)
//			//SimplyMessage._SA_HandleMessage (SimplyMessage.Yes, SimplyMessage._Msg_ReplaceExistingFile)
            if (SimplyMessage.repo.YesInfo.Exists())
            {
                SimplyMessage.repo.Yes.Click();
            }

            NewCompanyWizard.repo.Finish.Click();

            while (!NewCompanyWizard.repo.CloseInfo.Exists())
            {
                Thread.Sleep(1000);
            }

            NewCompanyWizard.repo.Close.Click();

            // Getting started always exists when a new company is created
            while (!GettingStarted.repo.SelfInfo.Exists())
            {
                Thread.Sleep(500);
            }
            GettingStarted.repo.Show.Uncheck();
            GettingStarted.repo.Close.Click();


            Simply.isEnhancedView();
//            Simply.Instance.SwitchViewLink.Click();
            // FunctionsLib.WUEn (Simply.Instance.SwitchToEnhancedViewLink);DW

            if ((!Functions.GoodData(Variables.bAcctEd)) || (Variables.bAcctEd))
            {
                Simply._SA_SetFlavorVariables();
            }

            Settings._SA_SetToGenericValues();                  // need to do this after getting a new, clean company
            Variables.bHistoryMode = true;
        }