protected void TextBoxStranicaVrijemePrikazivanja_TextChanged(object sender, EventArgs e)
        {
            SemaforiDataContext db = new SemaforiDataContext();
            var stranica = (from tblStranica in db.tblStranicas where tblStranica.Id == Convert.ToInt32(LabelStranicaID.Text) select tblStranica).Single();

            try {
                stranica.refreshPeriod = Convert.ToInt32(TextBoxStranicaVrijemePrikazivanja.Text);
                Session["periodOsvjezavanjaStranice"] = TextBoxStranicaVrijemePrikazivanja.Text;

                try {
                    db.SubmitChanges();
                }
                catch (Exception e1) {
                    Console.WriteLine(e1);
                }

                ListViewStranice.DataBind();
                oznaciTabStranice(stranica.Id);
            }
            catch (Exception e2) {
                Console.Write(e2);
            }
        }
        protected void textBoxSemaforNaziv_TextChanged(object sender, EventArgs e)
        {
            SemaforiDataContext db = new SemaforiDataContext();
            var sem = (from s in db.tblSemafors where s.Id == Convert.ToInt32(labelSemaforID.Text) select s).Single();
            sem.naziv = textBoxSemaforNaziv.Text;

            try {
                db.SubmitChanges();
            }
            catch (Exception e1) {
                Console.WriteLine(e1);
            }

            ListViewSemafori.DataBind();
            //menuUpdatePanel.Update();
            //Response.Redirect(HttpContext.Current.Request.Path);
        }
        protected void TextBoxStranicaNaziv_TextChanged(object sender, EventArgs e)
        {
            SemaforiDataContext db = new SemaforiDataContext();
            var stranica = (from tblStranica in db.tblStranicas where tblStranica.Id == Convert.ToInt32(LabelStranicaID.Text) select tblStranica).Single();

            stranica.naziv = TextBoxStranicaNaziv.Text;

            try {
                db.SubmitChanges();
            }
            catch (Exception e1) {
                Console.WriteLine(e1);
            }

            ListViewStranice.DataBind();
            oznaciTabStranice(stranica.Id);
        }
        protected void LinkButtonBrisiStranicu_Click(object sender, EventArgs e)
        {
            // brisanje stranice, redova i celija

            int stranicaID = Convert.ToInt32(LabelStranicaID.Text);

            SemaforiDataContext db = new SemaforiDataContext();

            // pripazi na kaskadno brisanje

            var stranica = (from tblStranica in db.tblStranicas where tblStranica.Id == stranicaID select tblStranica).Single();

            var brisiRedove = from tblRedak in db.tblRedaks where tblRedak.stranicaID == stranicaID select tblRedak;

            foreach (var redak in brisiRedove) {
                // pronađi da li ima celija i ako ima, obriši ih
                var brisiCelije = from tblCelija in db.tblCelijas where tblCelija.redakID == redak.id select tblCelija;
                foreach (var celija in brisiCelije) {
                    db.tblCelijas.DeleteOnSubmit(celija);
                }
                // obriši redak
                db.tblRedaks.DeleteOnSubmit(redak);

            }

            // obrisi stranicu
            db.tblStranicas.DeleteOnSubmit(stranica);

            db.SubmitChanges();

            // pronađi ID prethodne stranice i nju prikaži
            stranica = (from tblStranica in db.tblStranicas where tblStranica.Id < stranicaID select tblStranica).OrderByDescending(x => x.Id).First();

            prikaziStranicu(stranica.Id);
            menuUpdatePanel.Update();
            contentUpdatePanel.Update();
        }
        protected void LinkButtonDodajStranicu_Click(object sender, EventArgs e)
        {
            //upisi stranicu semafora
            SemaforiDataContext db = new SemaforiDataContext();

            tblStranica stranica = new tblStranica();
            tblRedak redak = new tblRedak();

            stranica.semaforID = Convert.ToInt32(labelSemaforID.Text);
            stranica.naziv = "Privremeni naziv stranice";
            stranica.refreshPeriod = 10000;
            stranica.brojRedova = 1;
            db.tblStranicas.InsertOnSubmit(stranica);
            db.SubmitChanges();

            //upisi redove
            redak.stranicaID = stranica.Id;
            db.tblRedaks.InsertOnSubmit(redak);
            db.SubmitChanges();

            //po defaultu nam graID i upitID ne postoje, pa nas onda strga u null vrijednosti kod prikaza !!!

            //upisi celije
            for (int i = 0; i < 3; ++i) {
                tblCelija celija = new tblCelija();
                celija.redakID = redak.id;
                celija.upitID = 9999;
                celija.grafID = 9999;
                db.tblCelijas.InsertOnSubmit(celija);
                db.SubmitChanges();
            }

            prikaziSemafor(stranica.semaforID);
            prikaziStranicu(stranica.Id);
            oznaciTabStranice(stranica.Id);
        }
        protected void LinkButtonBrisiSemafor_Click(object sender, EventArgs e)
        {
            int semaforID = Convert.ToInt32(labelSemaforID.Text);

            SemaforiDataContext db = new SemaforiDataContext();

            // pripazi na kaskadno brisanje

            var brisiSemafor = (from tblSemafor in db.tblSemafors where tblSemafor.Id == semaforID select tblSemafor).Single();

            var brisiStranice = (from tblStranica in db.tblStranicas where tblStranica.semaforID == semaforID select tblStranica);

            foreach (tblStranica stranica in brisiStranice) {

                var brisiRedove = from tblRedak in db.tblRedaks where tblRedak.stranicaID == stranica.Id select tblRedak;

                foreach (var redak in brisiRedove) {
                    // pronađi da li ima celija i ako ima, obriši ih
                    var brisiCelije = from tblCelija in db.tblCelijas where tblCelija.redakID == redak.id select tblCelija;
                    foreach (var celija in brisiCelije) {
                        db.tblCelijas.DeleteOnSubmit(celija);
                    }
                    // obriši redak
                    db.tblRedaks.DeleteOnSubmit(redak);

                }

                // obrisi stranicu
                db.tblStranicas.DeleteOnSubmit(stranica);
            }
            // obrisi semafor
            db.tblSemafors.DeleteOnSubmit(brisiSemafor);

            db.SubmitChanges();
            ListViewSemafori.DataBind();
            menuUpdatePanel.Update();
            contentUpdatePanel.Update();
            editSemafor.Visible = false;
        }
        protected void LinkButtonBrisiServerDatasource_Click(object sender, EventArgs e)
        {
            // brisemo sada taj datasource
            SemaforiDataContext db = new SemaforiDataContext();

            tblServer server = (from s in db.tblServers where s.id == Convert.ToInt32(labelServerID.Text) select s).Single();
            tblDB dbase = (from d in db.tblDBs where d.serverid == server.id select d).Single();

            db.tblServers.DeleteOnSubmit(server);
            db.tblDBs.DeleteOnSubmit(dbase);
            db.SubmitChanges();

            ListViewServeri.DataBind();
            prikaziContentDiv("nista");
        }
        protected void DropDownListStranicaBrojRedova_SelectedIndexChanged(object sender, EventArgs e)
        {
            //ovdje dodajemo ili brišemo redove i njihove pripadne ćelije

            SemaforiDataContext db = new SemaforiDataContext();
            var stranica = (from tblStranica in db.tblStranicas where tblStranica.Id == Convert.ToInt32(LabelStranicaID.Text) select tblStranica).Single();

            stranica.brojRedova = Convert.ToInt32(DropDownListStranicaBrojRedova.SelectedValue);

            try {
                db.SubmitChanges();
            }
            catch (Exception e1) {
                Console.WriteLine(e1);
            }

            // tu trebamo dodati ili brisati redove, zavisi da li ih ima više ili manje. Također treba dodavati ili brisati i njihove ćelije

            switch (stranica.brojRedova) {
                case 1:
                    // bili smo na 2, briši zadnji red
                    // prvo provjeri koja dva imamo pokupi redakID iz celije
                    IEnumerable<tblRedak> redovi = (from tblRedak in db.tblRedaks where tblRedak.stranicaID == Convert.ToInt32(LabelStranicaID.Text) select tblRedak).OrderByDescending(x => x.id);

                    tblRedak redZaBrisanje = redovi.First();
                    if (redovi.Count() > 1) {
                        db.tblRedaks.DeleteOnSubmit(redZaBrisanje);

                        IEnumerable<tblCelija> celije = (from tblCelija in db.tblCelijas where tblCelija.redakID == redZaBrisanje.id select tblCelija);
                        db.tblCelijas.DeleteAllOnSubmit(celije);
                        db.SubmitChanges();
                    }

                    break;
                case 2:
                    // trebamo dodati red
                    tblRedak redak = new tblRedak();
                    redak.stranicaID = stranica.Id;
                    db.tblRedaks.InsertOnSubmit(redak);
                    db.SubmitChanges();

                    // dodajemo i njegove celije
                    for (int i = 0; i < 3; ++i) {
                        tblCelija celija = new tblCelija();
                        celija.redakID = redak.id;
                        celija.upitID = 9999;
                        celija.grafID = 9999;
                        db.tblCelijas.InsertOnSubmit(celija);
                        db.SubmitChanges();
                    }

                    break;

            }

            ListViewStranice.DataBind();
            ListViewRedovi.DataBind();

            contentUpdatePanel.Update();
            oznaciTabStranice(stranica.Id);
        }
        protected void DropDownListOdabirUpita_SelectedIndexChanged(object sender, EventArgs e)
        {
            // upiši u tablicu celije koji upit smo odabrali

            // celijaID nam je zapisana kao custom atribut za svaki dropdown

            int celijaID =Convert.ToInt32((sender as DropDownList).Attributes["celijaID"]);

            SemaforiDataContext db = new SemaforiDataContext();
            tblCelija celija = (from c in db.tblCelijas where c.id == celijaID select c).Single();
            celija.upitID = Convert.ToInt32((sender as DropDownList).SelectedValue);

            db.SubmitChanges();
        }
        protected void DodajSemafor_Click(object sender, EventArgs e)
        {
            /*
            dodajemo semafor
            Svaki semafor odmah dobije jednu stranicu i jedan red
            dignemo prozor sa nazivom semafora, upišemo ga i prikažemo sve na desnoj strani
            */

            SemaforiDataContext db = new SemaforiDataContext();
            string privremeniNaslovSemafora = "Novi Semafor";

            tblSemafor semafor = new tblSemafor();
            tblStranica stranica = new tblStranica();
            tblRedak redak = new tblRedak();

            // upisi semafor
            semafor.naziv = privremeniNaslovSemafora;
            db.tblSemafors.InsertOnSubmit(semafor);
            db.SubmitChanges();

            //upisi stranicu semafora
            stranica.semaforID = semafor.Id;
            stranica.naziv = "Privremeni naziv stranice";
            stranica.refreshPeriod = 10000;
            stranica.brojRedova = 1;
            db.tblStranicas.InsertOnSubmit(stranica);
            db.SubmitChanges();

            //upisi redove
            redak.stranicaID = stranica.Id;
            db.tblRedaks.InsertOnSubmit(redak);
            db.SubmitChanges();

            //dodaj retku 3 celije
            for (int i = 0; i < 3; ++i) {
                tblCelija celija = new tblCelija();
                celija.redakID = redak.id;
                celija.grafID = 9999;              //samo privremeno da ne bude problema kasnije !!!
                celija.upitID = 9999;
                db.tblCelijas.InsertOnSubmit(celija);
                db.SubmitChanges();
            }

            prikaziSemafor(semafor.Id);
        }
        protected void buttonUpitSnimi_Click(object sender, EventArgs e)
        {
            SemaforiDataContext db = new SemaforiDataContext();

            // slično kao i kod servera; ako je Id=-1, onda je dodavanje novog, inače je edit

            if (Convert.ToInt32(labelUpitID.Text)>0) {              //edit postojećeg

                tblUpit upit = (from u in db.tblUpits where u.id == Convert.ToInt32(labelUpitID.Text) select u).Single();
                upit.naziv = textBoxUpitNaziv.Text;
                upit.upit = textBoxUpitDefinicija.Text;
                upit.serverID = Convert.ToInt32(dropDownListUpitOdaberiDatasource.SelectedValue);

                // petljanje, ali kad se mora.

                tblDB dbase = (from d in db.tblDBs where d.serverid == upit.serverID select d).Single();
                upit.dbID = dbase.id;

                db.SubmitChanges();

            }
            else {                              //dodajemo novi

                tblUpit upit = new tblUpit();

                upit.naziv = textBoxUpitNaziv.Text;
                upit.upit = textBoxUpitDefinicija.Text;
                upit.serverID = Convert.ToInt32(dropDownListUpitOdaberiDatasource.SelectedValue);

                // petljanje, ali kad se mora.
                tblDB dbase = (from d in db.tblDBs where d.serverid == upit.serverID select d).Single();
                upit.dbID = dbase.id;

                db.tblUpits.InsertOnSubmit(upit);
                db.SubmitChanges();

            }

            ListViewUpiti.DataBind();
            menuUpdatePanel.Update();
        }
        protected void buttonServerSnimiPostavke_Click(object sender, EventArgs e)
        {
            SemaforiDataContext db = new SemaforiDataContext();
            SimplerAES saes = new SimplerAES();

            if (Convert.ToInt32(labelServerID.Text) > 0) {                  // ako imamo ID, znači da mijenjamo postojeći
                tblServer server = (from tblServer in db.tblServers where tblServer.id == Convert.ToInt32(labelServerID.Text) select tblServer).Single();

                server.naziv = textBoxServerNaziv.Text;
                server.serverString = textBoxServerServerString.Text;

                db.SubmitChanges();

                tblDB dbase = (from tblDB in db.tblDBs where tblDB.serverid == server.id select tblDB).Single();

                dbase.naziv = textBoxServerDatabaseName.Text;
                dbase.serverid = server.id;
                dbase.integratedAuth = checkBoxServerDatabaseIntegratedAuth.Checked;
                dbase.korisnik = textBoxServerDatabaseKorisnik.Text;
                dbase.dbAttachString = textBoxServerDatabaseAttachString.Text;

                // kriptiramo lozinku da je baš ne spremimo u clear text u bazu

                 // ako nešto piše u TXt, to je nova lozinka. Spremi u H atribut
                if (textBoxServerDatabaseLozinka.Text.Length>0) {
                    textBoxServerDatabaseLozinka.Attributes["h"] = saes.Encrypt(textBoxServerDatabaseLozinka.Text);
                }

                dbase.lozinka = textBoxServerDatabaseLozinka.Attributes["h"];

                db.SubmitChanges();

                (sender as Button).Enabled = false;
                labelServerProvjeraStringa.Text = "";

            }
            else {      //nemamo ID, upisujemo novi

                tblServer server = new tblServer();
                tblDB dbase = new tblDB();

                server.naziv = textBoxServerNaziv.Text;
                server.serverString = textBoxServerServerString.Text;

                db.tblServers.InsertOnSubmit(server);
                db.SubmitChanges();

                dbase.naziv = textBoxServerDatabaseName.Text;
                dbase.serverid = server.id;
                dbase.integratedAuth = checkBoxServerDatabaseIntegratedAuth.Checked;
                dbase.korisnik = textBoxServerDatabaseKorisnik.Text;
                dbase.dbAttachString = textBoxServerDatabaseAttachString.Text;

                // kriptiramo password prije nego ga upišemo u bazu

                textBoxServerDatabaseLozinka.Attributes["h"] = saes.Encrypt(textBoxServerDatabaseLozinka.Text);
                dbase.lozinka = saes.Encrypt(textBoxServerDatabaseLozinka.Text);

                db.tblDBs.InsertOnSubmit(dbase);
                db.SubmitChanges();

                (sender as Button).Enabled = false;
                labelServerProvjeraStringa.Text = "";

            }

            ListViewServeri.DataBind();
            menuUpdatePanel.Update();
        }