/// <summary>
        /// Called when the "open" button is clicked on this form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpen_Click(object sender, EventArgs e)
        {
            FileInfo currentDB = this.getSelectedDatabase();
            if (currentDB == null)
                return;

            MainForm parent = this.MdiParent as MainForm;


            BaseInfoDAO dao = new BaseInfoDAO(currentDB.FullName);
            BaseInfo baseInfo = dao.GetBaseInfo();
            if (!baseInfo.Password.Equals(""))
            {
                PasswordPrompt prompt = new PasswordPrompt(baseInfo.Password);
                if (prompt.ShowDialog() != DialogResult.OK)
                    return;
            }   

            MainForm form = (MainForm)this.MdiParent;
            form.CurrentDatabase = currentDB;
            this.Close();

            foreach (Form f in parent.MdiChildren)
                f.Close();

            ViewBooksForm booksForm = new ViewBooksForm(Constants.LibraryMode.LIBRARY);
            booksForm.MdiParent = form;
            booksForm.Show();

        }
        /// <summary>
        /// called when OK button is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            int errors = 0;
            //check and make sure everything is filled out
            errors += UIUtils.ValidationHelper(this.txtOwner.Text.Length == 0,this.errorProvider1,this.txtOwner,ErrorMessages.Common.REQUIRED_FIELD);
            errors += UIUtils.ValidationHelper(this.txtLibrary.Text.Length == 0,this.errorProvider1,this.txtLibrary,ErrorMessages.Common.REQUIRED_FIELD);
            errors += UIUtils.ValidationHelper(this.chkProtected.Checked == true && this.txtPassword.Text.Length == 0,this.errorProvider1,this.txtPassword,ErrorMessages.NewDBForm.PASSWORD);

            if (errors > 0)
                return;

            //copy the empty database and rename it
            FileInfo emptyDB = new FileInfo("emdata");

            if(!emptyDB.Exists)
            {
                MessageBox.Show(emptyDB.FullName);
                MessageBox.Show(ErrorMessages.NewDBForm.EMPTY_DB_MISSING,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                this.Close();
                return;
            }
            

            string newFileName = this.txtLibrary.Text.Replace(" ","_") + ".plb";
            //check to make sure we're not writing over anything
            FileInfo newFile = new FileInfo(newFileName);

            if (newFile.Exists)
            {
                MessageBox.Show(ErrorMessages.NewDBForm.DB_ALREADY_EXISTS, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            try
            {
                newFile = emptyDB.CopyTo(newFileName);
            }
            catch (Exception e1)
            {
                ExceptionHandler.HandleException(e1);
                this.Close();
                return;
            }
            
            
            if (!newFile.Exists)
            {
                MessageBox.Show(ErrorMessages.NewDBForm.DB_NOT_CREATED, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //if we reached here, we have a database, now initialize it
            BaseInfoDAO dao = new BaseInfoDAO(newFileName);
            bool success = dao.InitializeDatabase(this.txtOwner.Text, this.txtLibrary.Text, this.txtPassword.Text);
           
            if (!success)
            {
                MessageBox.Show(ErrorMessages.NewDBForm.DB_NOT_CREATED, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                newFile.Delete();
            }
            else
            {
                if (DialogResult.Yes == MessageBox.Show(Messages.NewDBForm.DB_CREATED, "Success", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                {
                    MainForm form = (MainForm)this.MdiParent;
                    form.CurrentDatabase = newFile;

                    //check and see if there is a ViewDB window open, if so, close it
                    Form[] children = form.MdiChildren;
                    foreach (Form f in children)
                    {
                        if (f is ViewDatabasesForm)
                            f.Close();
                    }

                    ViewBooksForm f2 = new ViewBooksForm(Constants.LibraryMode.LIBRARY);
                    f2.MdiParent = this.MdiParent;
                    f2.Show();
                }
                else
                {
                    //check for a view database window, and refresh it
                    MainForm form = (MainForm)this.MdiParent;
                    Form[] children = form.MdiChildren;
                    foreach (Form f in children)
                    {
                        if (f is ViewDatabasesForm)
                            f.Refresh();
                    }
                }
            }
            this.Close();

           
        }
        /// <summary>
        /// Called when Tools -> Import is Called
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void importToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Library files (*.plb)|*.plb";
            dialog.RestoreDirectory = true;
            dialog.Multiselect = true;
            bool errors = false;

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                foreach (string file in dialog.FileNames)
                {
                    FileInfo info = new FileInfo(file);
                    if (info.Exists)
                    {
                        BaseInfoDAO dao = new BaseInfoDAO(info.FullName);
                        string version = dao.getVersionNumber();
                        if (version == null)
                        {
                            MessageBox.Show("The database you selected is not supported under this version of the application", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            continue;
                        }
                        else if (version == "1.0")
                        {
							info.CopyTo(info.Name,true);
							dao = new BaseInfoDAO(info.Name);
							StreamReader sr = File.OpenText("db10_to_12.sql");
							while(!sr.EndOfStream)
							{
								string line = sr.ReadToEnd();
								foreach(string command in line.Split(';'))
								{
									if(!dao.ExecuteNonQuery(command.Trim()))
										errors = true;
								}
							}

							MessageBox.Show("Database successfully imported and updated","Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
							continue;
                        }
                        else if (version == "1.1")
                        {
							info.CopyTo(info.Name,true);
							dao = new BaseInfoDAO(info.Name);
							StreamReader sr = File.OpenText("db11_to_12.sql");
							while(!sr.EndOfStream)
							{
								string line = sr.ReadToEnd();
								foreach(string command in line.Split(';'))
								{
									string trimmedCommand = command.Trim();
									if(trimmedCommand != "" && !dao.ExecuteNonQuery(trimmedCommand))
										errors = true;
								}
							}

							MessageBox.Show("Database successfully imported and updated","Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
							continue;
                        }
                        else if(version.Equals("1.2"))
                        {
                            //copy the file directly
                            info.CopyTo(info.Name);
                            MessageBox.Show("Database imported successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            continue;
                        }
                        else
                        {
                            MessageBox.Show("The database you selected is not supported under this version of the application", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            continue;  	
                        }
                    }
                }
            }
            
            if(errors == false)
            {
            	foreach(Form f in this.MdiChildren)
            	{
            		if(f is ViewDatabasesForm)
            			(f as ViewDatabasesForm).Refresh();
            	}
            }
        }
        private void refreshGrid()
        {
        	MainForm parent = this.MdiParent as MainForm;
            //look in the datadirectory, get a list of its contents
            this.grdDBList.Rows.Clear();
            
            DirectoryInfo info = new DirectoryInfo(Directory.GetCurrentDirectory());
            this.filesInfo = info.GetFiles("*.plb");

            //iterate over all files in the directory
            for (int i = 0; i < this.filesInfo.Length; i++)
            {
                BaseInfoDAO dao = new BaseInfoDAO(this.filesInfo[i].FullName);
                BaseInfo baseInfo = dao.GetBaseInfo();
                if (baseInfo != null)
                {
                    int index = this.grdDBList.Rows.Add();
                    DataGridViewRow row = this.grdDBList.Rows[index];
                    row.Cells[0].Value = baseInfo.DBName;
                    row.Cells[1].Value = baseInfo.Owner;
                    row.Cells[2].Value = baseInfo.DateCreated;
                    row.Cells[3].Value = baseInfo.LastAccessed;
                    string s1 = parent.AppSettings.AppSettings.Settings[Constants.Settings.DEFAULT_DB].Value;
                    string s2 = this.filesInfo[i].Name;
                    if (parent.AppSettings.AppSettings.Settings[Constants.Settings.DEFAULT_DB].Value == this.filesInfo[i].Name)
                        row.Cells[4].Value = "true";
                    else
                        row.Cells[4].Value = "false";
                }
            }

            //select the first row
            if (this.grdDBList.Rows.Count > 0)
                this.grdDBList.Rows[0].Selected = true;
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            FileInfo currentDB = this.getSelectedDatabase();
            //make sure there isn't a password in this database
            BaseInfoDAO dao = new BaseInfoDAO(currentDB.FullName);
            BaseInfo baseInfo = dao.GetBaseInfo();
            if (!baseInfo.Password.Equals(""))
            {
                PasswordPrompt prompt = new PasswordPrompt(baseInfo.Password);
                if (prompt.ShowDialog() != DialogResult.OK)
                    return;
            }
            if (MessageBox.Show(Messages.ViewDBForm.DB_DELETE_CONFIRM, "Delete This Library?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
            {
                currentDB.Delete();
                if (currentDB.Exists)
                    MessageBox.Show(ErrorMessages.Common.COULD_NOT_DELETE_DB, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                    MessageBox.Show(Messages.Common.DB_DELETED, "Library Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information);

                MainForm form = (MainForm)this.MdiParent;
                form.CurrentDatabase = null;
            }
            this.refreshGrid();
        }