//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include <iostream>
#include <vector>
#include "Unit4.h"
#include "Unit1.h"
#include "Math.h"
#include<math.hpp>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

//nastaveni hodnot paremetrů jednotlivych komponent
//vlakna
double g652B[]={-0.35,-0.21,-0.23,3.5,18,22,0.2}; //a 1310,a 1550,a 1625,CD 1310,CD 1550,CD 1625,PMD FutureGuide®-SM
double g652D[]={-0.35,-0.21,-0.23,3.5,18,22,0.2};   //FutureGuide®-SR15E
double g653[]={-0.22,3.5,0.3}; //a1550, CD 1550,PMD   FutureGuide®-DS
double g655[]={-0.22,-0.25,4.3,6.5,0.1}; //a 1550,a 1625,CD 1550,CD 1625, PMD  FutureGuide®-SS
double g656[]={-0.23,-0.26,5.0,5.5,0.1}; //a 1550,a 1625,CD 1550,CD 1625, PMD  FutureGuide®-USS
double sg652[]= {-0.34,-0.21,-0.24,3.0,17.5,22,0.1}; //a 1310,a 1550,a 1625,CD 1310,CD 1550,CD 1625,PMD samsung -SMF
double dcf1550[]={-0.15,-80.0,0.03};//a 1550,cd 1550,pmd  Z-DCFM-XP-168

//konektory
double LC[]={-0.1};
double FC[]={-0.15};
double SC[]={-0.15};
double ST[]={-0.25};
double svar[]={-0.05};

//atenuatory
double at1[]= {-1.0};
double at2[]= {-2.0};
double at3[]= {-3.0};
double at5[]= {-5.0};
double at10[]= {-10.0};
double at15[]= {-15.0};
double at20[]= {-20.0};
double at30[]= {-30.0};

// zesilovace
double pdfa20[]= {20.0}; // AMP-FL8611-OB-20
double pdfa30[]= {30.0}; // AMP-FL8610-OB-30
double edfa10[]= {10.0}; // KPS-OEM-10-S
double edfa15[]= {15.0}; // KPS-OEM-15-S
double edfa23[]= {23.0}; // KPS-OEM-23-HP
double edfa30[]= {30.0}; // KPS-OEM-30-HP
double raman30[]= {10.0}; // KPS-OEM-RFL
double raman33[]= {15.0}; // KPS-OEM-RFL

//zdroje
double zdroj1310_0[]= {0.0};
double zdroj1310_10[]= {10.0};
double zdroj1550_0[]= {0.0};
double zdroj1550_10[]= {10.0};
double zdroj1625_0[]= {0.0};
double zdroj1625_10[]= {10.0};
double detektor10[]= {-10.0};
double detektor20[]= {-20.0};
double detektor30[]= {-30.0};

//ostatni promenne
double uroven=0.0;
double uroven_prijimaci=0.0;
double utlum=0.0;
double utlum_celkove=0.0;
double CD=0.0;
double PMD=0.0;
double delka_celkove=0.0;
int i=1;
int pocet_prvku=0;
int lambda =0;
bool detektor =0;

TForm4 *Form4;
//---------------------------------------------------------------------------
__fastcall TForm4::TForm4(TComponent* Owner)
        : TForm(Owner)
{
}
typedef struct {            //vytvoreni struktury pro ukladani vlaken
        double delka;
        double PMD;
        double CD;
} VLAKNO;

void pmdcd_graf(void)   //fce pro vypocet grafu cd a pmd
{
Form1->Series3->Clear();                //smazani akt dat v grafech
Form1->Series2->Clear();
Form1->Series2->AddXY(0,0,0, clBlue ) ; //graf zacina v 0-0
Form1->Series3->AddXY(0,0,0, clMaroon ) ; //graf zacina v 0-0
std::vector<VLAKNO> trasa(0);
double celk_delka = 0.0;
        for(int x = 0; x<Form1->grid->RowCount;x+=2) {    //pro vsechny komponenty v gridu
                if(Form1->grid->Cells[1][x]=="Vlákno") {     //pokud je prvek vlákno

                switch (lambda) {                            // rozhodovani o ulozeni hodnot vlakna do vektoru podle vlnove delky zdroje
                        case 1310: {VLAKNO vl = {Form1->grid->Cells[9][x+1].ToDouble(), Form1->grid->Cells[8][x+1].ToDouble(),\
                                   Form1->grid->Cells[5][x+1].ToDouble()};
                                   celk_delka+=Form1->grid->Cells[9][x+1].ToDouble();
                                   trasa.push_back(vl);}
                        break;
                        case 1550: {VLAKNO vl = {Form1->grid->Cells[9][x+1].ToDouble(), Form1->grid->Cells[8][x+1].ToDouble(),Form1->grid->Cells[6][x+1].ToDouble()};
                                   celk_delka+=Form1->grid->Cells[9][x+1].ToDouble();
                                   trasa.push_back(vl);}
                        break;
                        case 1625: {VLAKNO vl = {Form1->grid->Cells[9][x+1].ToDouble(), Form1->grid->Cells[8][x+1].ToDouble(),Form1->grid->Cells[7][x+1].ToDouble()};
                                   celk_delka+=Form1->grid->Cells[9][x+1].ToDouble();
                                   trasa.push_back(vl); }
                        break;

                }
        }
}

double krok = celk_delka/100.0;   //krok pro vypocet
double akt_delka = krok;
double akt_pmd = 0.0;
unsigned int segment = 0;
double  prev_pmd = 0.0;
double prev_delka = 0.0;
double celk_cd = 0.0;
double celkova_delka = 0.0;
do {                             //vypocet po cele delce trasy
        if (celk_delka ==0 ){
                akt_delka=1;}

        else {
                akt_pmd = sqrt(pow(prev_pmd,2) + pow(trasa[segment].PMD*sqrt(akt_delka-prev_delka),2));  //vypocet PMD
                Form1->Series3->AddXY(RoundTo(akt_delka,-4), akt_pmd , RoundTo(akt_delka,-4),clMaroon ) ; //vyneseni do grafu
                akt_delka+=krok;

                if(akt_delka > (prev_delka + trasa[segment].delka)) {
                        prev_pmd=sqrt(pow(prev_pmd,2.0)+pow(trasa[segment].PMD*sqrt(trasa[segment].delka),2.0));
                        prev_delka+=trasa[segment].delka;
                        celk_cd+= trasa[segment].CD* trasa[segment].delka;   //vypocet CD
                        celkova_delka+=trasa[segment].delka;
                        Form1->Series2->AddXY(celkova_delka,celk_cd , celkova_delka,clBlue ) ; //vyneseni hodnoty CD
                        if(segment<trasa.size())segment++;
                }
                }

         }
         while(akt_delka<=celk_delka);
}
void rezerva (void)     //zobrazeni vykonove rezervy (usecka akt. hodnota urovne - hodnota vstup. vykonu detektoru  )
{
if (detektor == 1){
      Form1->Series5->Clear();
      Form1->Series5->AddXY(delka_celkove,uroven_prijimaci,delka_celkove,clGreen);
      Form1->Series5->AddXY(delka_celkove,utlum_celkove,delka_celkove,clGreen);
      }
}

//---------------------------------------------------------------------------
void __fastcall TForm4::combo_kompChange(TObject *Sender)        //    naplneni listbox_komp podle zvleneho prvku v combo_komp
{

if (combo_komp->Text == "Vlákno") {      // výber vlákna
                listbox_komp->Clear();
                listbox_komp->Items->Add("Fujikura FG-SM G.652B");
                listbox_komp->Items->Add("Fujikura FG-SR15E G.652D");
                listbox_komp->Items->Add("Fujikura FG-DS G.653");
                listbox_komp->Items->Add("Fujikura FG-SS G.655");
                listbox_komp->Items->Add("Fujikura FG-USS G.656");
                listbox_komp->Items->Add("Samsung SMF G.652B/C/D");
                listbox_komp->Items->Add("Sumitomo Z-DCFM-XP-168 ");
                edit_delka->Enabled=1;             //aktivace komponenty pro zadani delky vlakna
                button_test->Enabled=1;            //aktivace tlacitka pro test useku
                edit_delka->Text=10;               //delka prvku 10km
                utlum=0;

}
if (combo_komp->Text == "Spoj") {   //výber spojů
                listbox_komp->Clear();
                listbox_komp->Items->Add("Optokon LC konektor");
                listbox_komp->Items->Add("Optokon FC konektor");
                listbox_komp->Items->Add("Optokon SC konektor");
                listbox_komp->Items->Add("Optokon ST konektor");
                listbox_komp->Items->Add("Svár");

                edit_delka->Enabled=0;    //deaktivace komponenty pro zadani delky vlakna
                button_test->Enabled=0;   //adektivace tlacitka pro test useku
                edit_delka->Text=0;       //delka prvku 10km
                utlum=0;
}
if (combo_komp->Text == "Zesilovač") {  //výběr zesilovačů
                listbox_komp->Clear();
                listbox_komp->Items->Add("PDFA AMP-FL8611-OB");
                listbox_komp->Items->Add("PDFA AMP-FL8610-OB");
                listbox_komp->Items->Add("EDFA KPS-OEM-S");
                listbox_komp->Items->Add("EDFA KPS-OEM-S");
                listbox_komp->Items->Add("EDFA KPS-OEM-HP");
                listbox_komp->Items->Add("EDFA KPS-OEM-HP");
                listbox_komp->Items->Add("Raman KPS-OEM-RFL");
                listbox_komp->Items->Add("Raman KPS-OEM-RFL");

                edit_delka->Enabled=0;    //deaktivace komponenty pro zadani delky vlakna
                button_test->Enabled=0;   //adektivace tlacitka pro test useku
                edit_delka->Text=0;       //delka prvku 10km
                utlum=0;
}
if (combo_komp->Text == "Atenuátor") {   // vyber atenuatoru
                listbox_komp->Clear();
                listbox_komp->Items->Add("Optokon CAA-01");
                listbox_komp->Items->Add("Optokon CAA-02");
                listbox_komp->Items->Add("Optokon CAA-03");
                listbox_komp->Items->Add("Optokon CAA-05");
                listbox_komp->Items->Add("Optokon CAA-10");
                listbox_komp->Items->Add("Optokon CAA-15");
                listbox_komp->Items->Add("Optokon CAA-20");
                listbox_komp->Items->Add("Optokon CAA-30");
                edit_delka->Enabled=0;    //deaktivace komponenty pro zadani delky vlakna
                button_test->Enabled=0;   //adektivace tlacitka pro test useku
                edit_delka->Text=0;       //delka prvku 10km
                utlum=0;

}
if (combo_komp->Text == "Zdroj") {   // vyber zdroju
                listbox_komp->Clear();
                listbox_komp->Items->Add("1310 nm");
                listbox_komp->Items->Add("1310 nm");
                listbox_komp->Items->Add("1550 nm");
                listbox_komp->Items->Add("1550 nm");
                listbox_komp->Items->Add("1625 nm");
                listbox_komp->Items->Add("1625 nm");
                edit_delka->Enabled=0;    //deaktivace komponenty pro zadani delky vlakna
                button_test->Enabled=0;   //adektivace tlacitka pro test useku
                edit_delka->Text=0;       //delka prvku 10km
                utlum=0;

}
if (combo_komp->Text == "Detektor") { // vyber detektoru
                listbox_komp->Clear();
                listbox_komp->Items->Add("-10 dBm");
                listbox_komp->Items->Add("-20 dBm");
                listbox_komp->Items->Add("-30 dBm");
                edit_delka->Enabled=0;    //deaktivace komponenty pro zadani delky vlakna
                button_test->Enabled=0;   //adektivace tlacitka pro test useku
                edit_delka->Text=0;       //delka prvku 10km
                utlum=0;
}

}
//---------------------------------------------------------------------------
void __fastcall TForm4::listbox_kompClick(TObject *Sender)     //pri vyberu konkretniho prvku
{
Form4->button_vyber->Enabled=true;             //lze potvrdit vyber a prvek vlozit na trasu
if (combo_komp->Text == "Vlákno")             //vybrano vlakna
{
        switch     (listbox_komp->ItemIndex)  {       //ktery vlakno

                  case 0:  valuelist_hodnoty->Strings->Clear();       //FutureGuide®-SM G.652B
                            valuelist_hodnoty->Strings->Add("a (1310 nm)="+AnsiString(-g652B[0])); //pridani popisu do valuelist_hodnoty
                            valuelist_hodnoty->Strings->Add("a (1550 nm)="+AnsiString(-g652B[1]));
                            valuelist_hodnoty->Strings->Add("a (1625 nm)="+AnsiString(-g652B[2]));
                            valuelist_hodnoty->Strings->Add("D CD (1310nm)="+AnsiString(g652B[3]));
                            valuelist_hodnoty->Strings->Add("D CD (1550nm)="+AnsiString(g652B[4]));
                            valuelist_hodnoty->Strings->Add("D CD (1625nm)="+AnsiString(g652B[5]));
                            valuelist_hodnoty->Strings->Add("D PMD ="+AnsiString(g652B[6]));
                            PMD= g652B[6];

                            switch (lambda) {           //ulozeni hodnot promenych utlumu a CD podle vlnove delky
                            case 1310:
                                        utlum= g652B[0];
                                        CD= g652B[3];
                            break;

                            case 1550:
                                        utlum= g652B[1];
                                        CD= g652B[4];
                            break;

                            case 1625:
                                        utlum= g652B[2];
                                        CD= g652B[5];
                            break;
                            }
                   break;

                    case 1: valuelist_hodnoty->Strings->Clear();          //FutureGuide®-SR15E G.652D
                            valuelist_hodnoty->Strings->Add("a (1310 nm)="+AnsiString(-g652D[0]));
                            valuelist_hodnoty->Strings->Add("a (1550 nm)="+AnsiString(-g652D[1]));
                            valuelist_hodnoty->Strings->Add("a (1625 nm)="+AnsiString(-g652D[2]));
                            valuelist_hodnoty->Strings->Add("D CD (1310 nm)="+AnsiString(g652D[3]));
                            valuelist_hodnoty->Strings->Add("D CD (1550 nm)="+AnsiString(g652D[4]));
                            valuelist_hodnoty->Strings->Add("D CD (1625 nm)="+AnsiString(g652D[5]));
                            valuelist_hodnoty->Strings->Add("D PMD="+AnsiString(g652D[6]));
                            PMD= g652D[6];

                            switch (lambda) {
                            case 1310:
                                        utlum= g652D[0];
                                        CD= g652D[3];
                            break;

                            case 1550:
                                        utlum= g652D[1];
                                        CD= g652D[4];
                            break;

                            case 1625:
                                        utlum= g652D[2];
                                        CD= g652D[5];
                            break;
                            }



                    break;

                    case 2 : valuelist_hodnoty->Strings->Clear();         //"FutureGuide®-DS G.653"
                             valuelist_hodnoty->Strings->Add("a (1310 nm)=-");
                             valuelist_hodnoty->Strings->Add("a (1550 nm)="+AnsiString(-g653[0]));
                             valuelist_hodnoty->Strings->Add("a (1625 nm)=-");
                             valuelist_hodnoty->Strings->Add("D CD (1310 nm)=-");
                             valuelist_hodnoty->Strings->Add("D CD (1550 nm)="+AnsiString(g653[1]));
                             valuelist_hodnoty->Strings->Add("D CD (1625 nm)=-");
                             valuelist_hodnoty->Strings->Add("D PMD="+AnsiString(g653[2]));
                             PMD= g653[2];
                            switch (lambda) {
                            case 1310:
                                      Application->MessageBox("Vlákno lze použít pouze na 1550nm!", "Chyba",MB_OK| MB_ICONWARNING);   //chybova hlaska pri vybrani nevhod komponenty pro tuto vlnovou delku
                                      Form4->button_vyber->Enabled=false;   //nemoznost vlozit tento nevhodny prvek an trasu
                            break;

                            case 1550:
                                      utlum= g653[0];
                                      CD= g653[1];
                            break;

                            case 1625:
                                      Application->MessageBox("Vlákno lze použít pouze na 1550nm!", "Chyba",MB_OK| MB_ICONWARNING);
                                      Form4->button_vyber->Enabled=false;
                            break;
                            }


                    break;

                    case 3: valuelist_hodnoty->Strings->Clear();       //FutureGuide®-SS G.655
                            valuelist_hodnoty->Strings->Add("a (1310 nm)=-");
                            valuelist_hodnoty->Strings->Add("a (1550 nm)="+AnsiString(-g655[0]));
                            valuelist_hodnoty->Strings->Add("a (1625 nm)="+AnsiString(-g655[1]));
                            valuelist_hodnoty->Strings->Add("D CD (1310 nm)=-");
                            valuelist_hodnoty->Strings->Add("D CD (1550 nm)="+AnsiString(g655[2]));
                            valuelist_hodnoty->Strings->Add("D CD (1625 nm)="+AnsiString(g655[3]));
                            valuelist_hodnoty->Strings->Add("D PMD="+AnsiString(g655[4]));
                            PMD= g655[4];

                            switch (lambda) {
                            case 1310:
                                      Application->MessageBox("Vlákno nelze použít na 1310nm!", "Chyba",MB_OK| MB_ICONWARNING);
                                      Form4->button_vyber->Enabled=false;
                            break;

                            case 1550:
                                        utlum= g655[0];
                                        CD= g655[2];
                            break;

                            case 1625:
                                        utlum= g655[1];
                                        CD= g655[3];
                            break;
                            }
                   break;

                   case 4:  valuelist_hodnoty->Strings->Clear();       //FutureGuide®-USS G.656
                            valuelist_hodnoty->Strings->Add("a (1310 nm)=-");
                            valuelist_hodnoty->Strings->Add("a (1550 nm)="+AnsiString(-g656[0]));
                            valuelist_hodnoty->Strings->Add("a (1625 nm)="+AnsiString(-g656[1]));
                            valuelist_hodnoty->Strings->Add("D CD (1310 nm)=-");
                            valuelist_hodnoty->Strings->Add("D CD (1550 nm)="+AnsiString(g656[2]));
                            valuelist_hodnoty->Strings->Add("D CD (1625 nm)="+AnsiString(g656[3]));
                            valuelist_hodnoty->Strings->Add("D PMD="+AnsiString(g656[4]));
                            PMD= g656[4];

                            switch (lambda) {
                            case 1310:
                                      Application->MessageBox("Vlákno nelze použít na 1310nm!", "Chyba",MB_OK| MB_ICONWARNING);
                                      Form4->button_vyber->Enabled=false;
                            break;

                            case 1550:
                                        utlum= g656[0];
                                        CD= g656[2];
                            break;

                            case 1625:
                                        utlum= g656[1];
                                        CD= g656[3];
                            break;
                            }
                   break;

                   case 5:  valuelist_hodnoty->Strings->Clear();       //Samsung G.652B
                            valuelist_hodnoty->Strings->Add("a (1310 nm)="+AnsiString(-sg652[0]));
                            valuelist_hodnoty->Strings->Add("a (1550 nm)="+AnsiString(-sg652[1]));
                            valuelist_hodnoty->Strings->Add("a (1625 nm)="+AnsiString(-sg652[2]));
                            valuelist_hodnoty->Strings->Add("D CD (1310nm)="+AnsiString(sg652[3]));
                            valuelist_hodnoty->Strings->Add("D CD (1550nm)="+AnsiString(sg652[4]));
                            valuelist_hodnoty->Strings->Add("D CD (1625nm)="+AnsiString(sg652[5]));
                            valuelist_hodnoty->Strings->Add("D PMD ="+AnsiString(sg652[6]));
                            PMD= sg652[6];

                            switch (lambda) {
                            case 1310:
                                        utlum= sg652[0];
                                        CD= sg652[3];
                            break;

                            case 1550:
                                        utlum= sg652[1];
                                        CD= sg652[4];
                            break;

                            case 1625:
                                        utlum= sg652[2];
                                        CD= sg652[5];
                            break;
                            }
                   break;

                   case 6:  valuelist_hodnoty->Strings->Clear();       //Z-DCFM-XP-168
                            valuelist_hodnoty->Strings->Add("a (1310 nm)=-");
                            valuelist_hodnoty->Strings->Add("a (1550 nm)="+AnsiString(-dcf1550[0]));
                            valuelist_hodnoty->Strings->Add("a (1625 nm)=-");
                            valuelist_hodnoty->Strings->Add("D CD (1310 nm)=-");
                            valuelist_hodnoty->Strings->Add("D CD (1550 nm)="+AnsiString(dcf1550[1]));
                            valuelist_hodnoty->Strings->Add("D CD (1625 nm)=-");
                            valuelist_hodnoty->Strings->Add("D PMD="+AnsiString(dcf1550[2]));
                            PMD= dcf1550[2];

                            switch (lambda) {
                            case 1310:
                                      Application->MessageBox("Vlákno lze použít pouze na 1550 nm!", "Chyba",MB_OK| MB_ICONWARNING);
                                      Form4->button_vyber->Enabled=false;
                            break;

                            case 1550:
                                        utlum= dcf1550[0];
                                        CD= dcf1550[1];
                                      //  edit_delka->Text=40;
                                       // edit_delka->Enabled=0;
                            break;

                            case 1625:
                                        Application->MessageBox("Vlákno lze použít pouze na 1550 nm!", "Chyba",MB_OK| MB_ICONWARNING);
                                        Form4->button_vyber->Enabled=false;
                            break;
                            }
                   break;



       }
    }

if (combo_komp->Text == "Spoj")             //vybrano spoje
{
        switch     (listbox_komp->ItemIndex)  {
                  case 0:    valuelist_hodnoty->Strings->Clear();
                             valuelist_hodnoty->Strings->Add("A [dB]="+AnsiString(-LC[0]));
                             utlum=LC[0];

                   break;

                   case 1:   valuelist_hodnoty->Strings->Clear();
                             valuelist_hodnoty->Strings->Add("A [dB]="+AnsiString(-FC[0]));
                             utlum=FC[0];

                   break;

                   case 2:   valuelist_hodnoty->Strings->Clear();
                             valuelist_hodnoty->Strings->Add("A [dB]="+AnsiString(-SC[0]));
                             utlum=SC[0];

                   break;

                   case 3:   valuelist_hodnoty->Strings->Clear();
                             valuelist_hodnoty->Strings->Add("A [dB]="+AnsiString(-ST[0]));
                             utlum=ST[0];

                   break;

                    case 4:  valuelist_hodnoty->Strings->Clear();
                             valuelist_hodnoty->Strings->Add("A [dB]="+AnsiString(-svar[0]));
                             utlum=svar[0];

                    break;



       }
    }

if (combo_komp->Text == "Zesilovač")           //vybrano zesilovače
{
        switch     (listbox_komp->ItemIndex)  {

                   case 0:     valuelist_hodnoty->Strings->Clear(); //pdfa
                               valuelist_hodnoty->Strings->Add("Zesílení [dB]="+AnsiString(pdfa20[0]));
                            switch (lambda) {
                            case 1310:
                                      utlum=pdfa20[0];
                            break;

                            case 1550:
                                      Application->MessageBox("Zesilovač lze použít pouze na 1 310 nm!", "Chyba",MB_OK| MB_ICONWARNING);
                                      Form4->button_vyber->Enabled=false;
                            break;

                            case 1625:
                                      Application->MessageBox("Zesilovač lze použít pouze na 1 310 nm!", "Chyba",MB_OK| MB_ICONWARNING);
                                      Form4->button_vyber->Enabled=false;
                            break;
                             }
                   break;


                    case 1:    valuelist_hodnoty->Strings->Clear();    //pdfa
                               valuelist_hodnoty->Strings->Add("Zesílení [dB]="+AnsiString(pdfa30[0]));
                            switch (lambda) {
                            case 1310:
                                      utlum=pdfa30[0];
                            break;

                            case 1550:
                                      Application->MessageBox("Zesilovač lze použít pouze na 1 310 nm!", "Chyba",MB_OK| MB_ICONWARNING);
                                      Form4->button_vyber->Enabled=false;
                            break;

                            case 1625:
                                      Application->MessageBox("Zesilovač lze použít pouze na 1 310 nm!", "Chyba",MB_OK| MB_ICONWARNING);
                                      Form4->button_vyber->Enabled=false;
                            break;
                             }

                    break;

                   case 2:      valuelist_hodnoty->Strings->Clear(); //edfa
                                valuelist_hodnoty->Strings->Add("Zesílení [dB]="+AnsiString(edfa10[0]));
                            switch (lambda) {
                            case 1310:
                                      Application->MessageBox("Zesilovač nelze použít na 1 310 nm!", "Chyba",MB_OK| MB_ICONWARNING);
                                      Form4->button_vyber->Enabled=false;
                            break;

                            case 1550:
                                      utlum=edfa10[0];
                            break;

                            case 1625:
                                      utlum=edfa10[0];
                            break;
                             }


                   break;

                    case 3:     valuelist_hodnoty->Strings->Clear();    //efda
                                valuelist_hodnoty->Strings->Add("Zesílení [dB]="+AnsiString(edfa15[0]));
                                switch (lambda) {
                            case 1310:
                                      Application->MessageBox("Zesilovač nelze použít na 1 310 nm!", "Chyba",MB_OK| MB_ICONWARNING);
                                      Form4->button_vyber->Enabled=false;
                            break;

                            case 1550:
                                      utlum=edfa15[0];
                            break;

                            case 1625:
                                      utlum=edfa15[0];
                            break;
                             }


                    break;

                    case 4:     valuelist_hodnoty->Strings->Clear();    //efda
                                valuelist_hodnoty->Strings->Add("Zesílení [dB]="+AnsiString(edfa23[0]));
                            switch (lambda) {
                            case 1310:
                                      Application->MessageBox("Zesilovač nelze použít na 1 310 nm!", "Chyba",MB_OK| MB_ICONWARNING);
                                      Form4->button_vyber->Enabled=false;
                            break;

                            case 1550:
                                      utlum=edfa23[0];
                            break;

                            case 1625:
                                      utlum=edfa23[0];
                            break;
                             }


                    break;

                    case 5:     valuelist_hodnoty->Strings->Clear();    //efda
                                valuelist_hodnoty->Strings->Add("Zesílení [dB]="+AnsiString(edfa30[0]));
                            switch (lambda) {
                            case 1310:
                                      Application->MessageBox("Zesilovač nelze použít na 1 310 nm!", "Chyba",MB_OK| MB_ICONWARNING);
                                      Form4->button_vyber->Enabled=false;
                            break;

                            case 1550:
                                      utlum=edfa30[0];
                            break;

                            case 1625:
                                      utlum=edfa30[0];
                            break;
                             }


                    break;

                     case 6:     valuelist_hodnoty->Strings->Clear();    //raman
                                valuelist_hodnoty->Strings->Add("Zesílení [dB]="+AnsiString(raman30[0]));

                            switch (lambda) {
                            case 1310:
                                    utlum=raman30[0];
                            break;

                            case 1550:
                                      utlum=raman30[0];
                            break;

                            case 1625:
                                      utlum=raman30[0];
                            break;
                             }


                    break;

                      case 7:     valuelist_hodnoty->Strings->Clear();    //raman
                                valuelist_hodnoty->Strings->Add("Zesílení [dB]="+AnsiString(raman33[0]));

                            switch (lambda) {
                            case 1310:
                                    utlum=raman33[0];
                            break;

                            case 1550:
                                      utlum=raman33[0];
                            break;

                            case 1625:
                                      utlum=raman33[0];
                            break;
                             }


                    break;



       }
    }

if (combo_komp->Text == "Atenuátor")           //vybrano atenuátory
{
        switch     (listbox_komp->ItemIndex)  {
                  case 0:    valuelist_hodnoty->Strings->Clear();
                             valuelist_hodnoty->Strings->Add("A [dB]="+AnsiString(-at1[0]));
                             utlum=at1[0];
                   break;

                   case 1:   valuelist_hodnoty->Strings->Clear();
                             valuelist_hodnoty->Strings->Add("A [dB]="+AnsiString(-at2[0]));
                             utlum=at2[0];
                   break;

                   case 2:    valuelist_hodnoty->Strings->Clear();
                             valuelist_hodnoty->Strings->Add("A [dB]="+AnsiString(-at3[0]));
                             utlum=at3[0];
                   break;
                   case 3:    valuelist_hodnoty->Strings->Clear();
                             valuelist_hodnoty->Strings->Add("A [dB]="+AnsiString(-at5[0]));
                             utlum=at5[0];
                   break;
                   case 4:    valuelist_hodnoty->Strings->Clear();
                             valuelist_hodnoty->Strings->Add("A [dB]="+AnsiString(-at10[0]));
                             utlum=at10[0];
                   break;
                   case 5:    valuelist_hodnoty->Strings->Clear();
                             valuelist_hodnoty->Strings->Add("A [dB]="+AnsiString(-at15[0]));
                             utlum=at15[0];
                   break;
                   case 6:    valuelist_hodnoty->Strings->Clear();
                             valuelist_hodnoty->Strings->Add("A [dB]="+AnsiString(-at20[0]));
                             utlum=at20[0];
                   break;
                   case 7:    valuelist_hodnoty->Strings->Clear();
                             valuelist_hodnoty->Strings->Add("A [dB]="+AnsiString(-at30[0]));
                             utlum=at30[0];
                   break;


       }
    }

if (combo_komp->Text == "Zdroj")           //vybrano zdroj
{
        switch     (listbox_komp->ItemIndex)  {
                  case 0:    valuelist_hodnoty->Strings->Clear();
                             valuelist_hodnoty->Strings->Add("P out [dBm]="+AnsiString(zdroj1310_0[0]));
                             lambda = 1310;
                             uroven=zdroj1310_0[0];
                   break;

                  case 1:    valuelist_hodnoty->Strings->Clear();
                             valuelist_hodnoty->Strings->Add("P out [dBm]="+AnsiString(zdroj1310_10[0]));
                             lambda = 1310;
                             uroven=zdroj1310_10[0];
                   break;

                   case 2:    valuelist_hodnoty->Strings->Clear();
                             valuelist_hodnoty->Strings->Add("P out [dBm]="+AnsiString(zdroj1550_0[0]));
                             lambda = 1550;
                             uroven=zdroj1550_0[0];
                   break;

                  case 3:    valuelist_hodnoty->Strings->Clear();
                             valuelist_hodnoty->Strings->Add("P out [dBm]="+AnsiString(zdroj1550_10[0]));
                             lambda = 1550;
                             uroven=zdroj1550_10[0];
                   break;

                  case 4:    valuelist_hodnoty->Strings->Clear();
                             valuelist_hodnoty->Strings->Add("P out [dBm]="+AnsiString(zdroj1625_0[0]));
                             lambda = 1625;
                             uroven=zdroj1625_0[0];
                   break;

                  case 5:    valuelist_hodnoty->Strings->Clear();
                             valuelist_hodnoty->Strings->Add("P out [dBm]="+AnsiString(zdroj1625_10[0]));
                             lambda = 1625;
                             uroven=zdroj1625_10[0];
                   break;
       }
    }

if (combo_komp->Text == "Detektor")           //vybrano detektor
{       if (detektor==1) {

        Application->MessageBox("Na trase již detektor existuje! ", "Chyba",MB_OK| MB_ICONWARNING);
        Form4->button_vyber->Enabled=false;
        }
        switch     (listbox_komp->ItemIndex)  {
                  case 0:    valuelist_hodnoty->Strings->Clear();
                             valuelist_hodnoty->Strings->Add("P in [dBm]="+AnsiString(detektor10[0]));
                             uroven_prijimaci=detektor10[0];
                   break;

                    case 1:  valuelist_hodnoty->Strings->Clear();
                             valuelist_hodnoty->Strings->Add("P in [dBm]="+AnsiString(detektor20[0]));
                             uroven_prijimaci=detektor20[0];
                    break;

                    case 2 :  valuelist_hodnoty->Strings->Clear();
                              valuelist_hodnoty->Strings->Add("P in [dBm]="+AnsiString(detektor30[0]));
                              uroven_prijimaci=detektor30[0];
                   break;

       }
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm4::button_vyberClick(TObject *Sender)
{
try {                      //osetreni nevybrani zadne komponenty
listbox_komp->Items->Strings[listbox_komp->ItemIndex]==-1;
}
catch (...) {
Application->MessageBox("Vyberte prvek", "Chyba",MB_OK| MB_ICONWARNING);
return ;
}
if   (combo_komp->Text == "Detektor"){
        detektor=1;
}

int pocet_radku= valuelist_hodnoty->RowCount  ;   //      pocet radku ve valuelistu
Form1->grid->RowCount=i+1;
Form1->grid->Cells[0][i-1]="Vzdálenost [km]";       //na kterém km je vlozena komponenta
Form1->grid->Cells[0][i]=delka_celkove;

Form1->grid->Cells[1][i-1]=combo_komp->Items->Strings[combo_komp->ItemIndex];     //zapise na prvni misto typ komponenty z komba
Form1->grid->Cells[1][i]=listbox_komp->Items->Strings[listbox_komp->ItemIndex]; //zapise na prvni misto typ komponenty z listboxu
for (int k=0; k < pocet_radku-1; k++)  {
        Form1->grid->Cells[k+2][i-1]=valuelist_hodnoty->Cells[0][k+1] ;        //zapisuje prisusne radky z valuleistu do 1.řádku gridu
        Form1->grid->Cells[k+2][i]=valuelist_hodnoty->Cells[1][k+1];          //zapisuje prisusne radky z valuleistu do 2.řádku gridu
        }
if (combo_komp->Text =="Vlákno" )   {       //pokud vkladame vlakno
        Form1->grid->Cells[pocet_radku+1][i-1]="Délka vlákna [km]";  //do posledního radku zapise "delka vlakna"
        Form1->grid->Cells[pocet_radku+2][i-1]="Útlum úseku [dB]";  //tj. doplnujici info pro usek vlakna
        Form1->grid->Cells[pocet_radku+3][i-1]="CD úseku [ps]";
        Form1->grid->Cells[pocet_radku+4][i-1]="PMD úseku [ps]";

        Form1->grid->Cells[pocet_radku+1][i]=edit_delka->Text;    //na posledni radek zapise delku vlakna z editu
        Form1->grid->Cells[pocet_radku+2][i]=AnsiString(-utlum*edit_delka->Text.ToDouble()); //tj dpolnujici info pro usek vlakna
        Form1->grid->Cells[pocet_radku+3][i]=AnsiString(CD*edit_delka->Text.ToDouble());
        Form1->grid->Cells[pocet_radku+4][i]=AnsiString(RoundTo(PMD*sqrt(edit_delka->Text.ToDouble()),-4));

        utlum_celkove+= edit_delka->Text.ToDouble() * utlum;          //aktualizece celkoveho utlumu (delka * hodnota)
        delka_celkove+= edit_delka->Text.ToDouble();                 //aktualizace celkove delky
        Form1->Series1->AddXY(delka_celkove,utlum_celkove, delka_celkove, clRed ) ;  //graf utlumu

        if (uroven_prijimaci !=0 )Form1->Series4->AddXY(delka_celkove,uroven_prijimaci, delka_celkove, clGreen ) ;  //uroven detektoru
                rezerva();                              //zavolani fce zobrazeni vykonove rezervy
                pmdcd_graf();                      //zavolani fce vypoctu a zobrazeni CD a PMD
        }

        else {                           //kdyz je vybrany cokoliv jinyho nez vlakna
        utlum_celkove+= utlum;          // aktualizece celkoveho utlumu
        delka_celkove= delka_celkove;   //aktualizace celkove delky
        Form1->Series1->AddXY(delka_celkove,utlum_celkove, delka_celkove, clRed ) ;  //graf utlumu
        rezerva();                      //zavolani fce zobrazeni vykonove rezervy
}


if ( combo_komp->Text =="Zdroj") {   //kdyz jsou vybrany zdroje
      Form1->Series1->AddXY(0,uroven, 0, clRed ) ; //graf zacina v 0-0
      utlum_celkove+=uroven;         // aktualizece celkoveho utlumu
      combo_komp->Clear();           // vymazeni vyberu prvku
      listbox_komp->Clear();         // vymazeni vyberu konkretnich komponent
      valuelist_hodnoty->Strings->Clear();   // vymazeni vlastnosti konkretnich komponent
      combo_komp->Text="Zvolte...";           //naplneni combo_comp vsemi prkvy korme zdroju

      combo_komp->Items->Add("Vlákno")  ;              //pridani druhu do combo_komp
      combo_komp->Items->Add("Spoj")  ;
      combo_komp->Items->Add("Zesilovač")  ;
      combo_komp->Items->Add("Atenuátor")  ;
      combo_komp->Items->Add("Detektor")  ;

}

if ( combo_komp->Text =="Detektor") {   //kdyz sou vybrany detektory
      Form1->Series4->AddXY(0,uroven_prijimaci, 0, clGreen ) ;
      Form1->Series4->AddXY(delka_celkove,uroven_prijimaci, delka_celkove, clGreen ) ;  //uroven detektoru
      rezerva();
}

i=i+2;
Form4->Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm4::button_testClick(TObject *Sender)    //tlacítko zobrazení vlastnosti vybraneho useku vlakna
{

label_test->Caption="Zvolený úsek - útlum: "+ AnsiString(-utlum*edit_delka->Text.ToDouble())+"dB; CD: " +AnsiString(CD*edit_delka->Text.ToDouble())+" ps; PMD: "  +AnsiString(RoundTo(PMD*sqrt(edit_delka->Text.ToDouble()),-4))+ " ps.";

}
//---------------------------------------------------------------------------
void __fastcall TForm4::FormCreate(TObject *Sender)
{
combo_komp->Items->Add("Zdroj")  ;        //po spusteni lze vybrat pouze vlakna

edit_delka->Enabled=0;
button_test->Enabled=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm4::button_zpetClick(TObject *Sender)
{
Form4->Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm4::edit_delkaKeyPress(TObject *Sender, char &Key)   //osetreni vlozeni pouze cislic a des.carky do edit_delka
{
switch(Key)
   { case ',': return;
     case VK_BACK: return;
   }
   if (isdigit(Key)) return;
   Key=0;
}
//---------------------------------------------------------------------------

void __fastcall TForm4::valuelist_hodnotyDrawCell(TObject *Sender,     //vystredeni prvku v komponente valuelist
      int ACol, int ARow, TRect &Rect, TGridDrawState State)
{

int odsazeni= valuelist_hodnoty->DefaultColWidth/2-(valuelist_hodnoty->Canvas->TextWidth(valuelist_hodnoty->Cells[ACol][ARow])/2 );
valuelist_hodnoty->Canvas->TextRect(Rect,Rect.Left+odsazeni, Rect.Top+3,Form4->valuelist_hodnoty->Cells[ACol][ARow]);


}
//---------------------------------------------------------------------------

