def search(text, title, author, ID, loanStatus):
    """Searches for books with data similar to user's input information:
    
    title - the list of titles
    author - the list of authors
    loanStauts - the list of loan statuses
    ID - the list of ID's."""
    text = text.strip()
    numberOfBooks = booksNumber()
    searchValue = [0] * numberOfBooks
    if (len(title) < numberOfBooks):
        title = []
        author = []
        ID = []
        loanStatus = []
        title, author, ID, loanStatus = readList()
    for i in range(numberOfBooks):
        if (text.strip().lower() == title[i].strip().lower() or 
            text.strip().lower() ==
            author[i].strip().lower() or text.strip().lower() == 
            ID[i].strip().lower() or
            text.strip().lower() == loanStatus[i].strip().lower()):
            searchValue[i] = len(text) * 1.1 + len(text.strip().split(" ")) * 0.3
        else:
            searchValue[i] = compare(text, title[i], searchValue[i])
            searchValue[i] = compare(text, author[i], searchValue[i])
    return searchValue
def delBook(ind):
    """Deletes a book from the table and from the database.txt file:

    ind - the index of a book."""
    global form
    numberOfBooks = booksNumber()
    title, author, loanStatus, ID = readList()
    searchValue = search(searchBox.get(), title, author, ID, loanStatus)
    searchValue, title, author, loanStatus, ID = quickSort(0, numberOfBooks, 
                                       searchValue, title, author, loanStatus, ID)
    if (messagebox.askokcancel("Confirm Box","Are you sure you want to remove \
the following book?\n"
 + title[ind] + " by " + author[ind] + " having ID-" + ID[ind], parent=form)):
        del title[ind]
        del author[ind]
        del loanStatus[ind]
        del ID[ind]
        numberOfBooks = booksNumber() - 1
        title, author, loanStatus, ID = quickSortID(0, numberOfBooks, title, 
                                                    author, loanStatus, ID)
        writeToDatabase(title, author, loanStatus, ID, numberOfBooks)
        updateList()
def cleanup(IDs):
    """Creates and returns the list of indices of the books which are suggested 
    to be removed:
    
    IDs - IDs of books."""
    title, author, loanStatus, ID = readList()
    ID = IDs
    numberOfBooks = booksNumber()
    sugg = {}
    r = open("logfile.txt", 'r')
    for line in r:
        id, year, month, day, status = splitLine(line)
        sugg = isRecommended(year, month, day, status, sugg, id)
    return getList(numberOfBooks, sugg, ID)
def returnBook():
    """Checks if data is valid and, if it is, marks a book as returned."""
    global bookEntry
    global error
    numberOfBooks = booksNumber()
    title, author, loanStatus, ID = readList()
    found = False
    if (type(error) == Label):
        error.grid_remove()
    excpt, id = invalidInput()
    if (not excpt):
        found, excpt = isTaken(numberOfBooks, id, title, author, ID,
                               loanStatus)
        if (not found and not excpt):
            notFound()
def delete():
    """Deletes the book having the user's input ID, otherwise, displays an error 
    message."""
    global error
    numberOfBooks = booksNumber()
    title, author, loanStatus, ID = readList()
    found = False
    error.grid_forget()
    excpt = False
    try:
        id = int(bookEntry.get())
    except ValueError:
        excpt = invalidID()
    if (not excpt):
        found = findBook(numberOfBooks, title, author, loanStatus, ID, id)
    if (not found and not excpt):
        notFound()
def updateList():
    """Updates the table in the form."""
    global searchBox
    title, author, loanStatus, ID = readList()
    numberOfBooks = booksNumber()
    booksFound = False
    if (searchBox.get() == ""):
        booksFound = searchBoxEmpty()
    else:
        clearEmptyLabel()
        searchValueNew, titleNew, authorNew, loanStatusNew, IDNew = \
        getSearchResult(title, author, loanStatus, ID, numberOfBooks)
        booksFound = \
        displayResults(title, author, loanStatus, ID, numberOfBooks, searchValueNew)
        showSuggested(ID, searchValueNew)
    booksNotFound(booksFound)
    frameBackground.update_idletasks()
    background.config(scrollregion=background.bbox("all"))
def delBook2(ind):
    """Deletes a book when a remove button is clicked in the form, where the 
    books, suggested to be removed are displayed:
    
    ind - the index of a book."""
    global form
    title, author, loanStatus, ID = readList()
    if (messagebox.askokcancel("Confirm Box","Are you sure you want to remove the \
following book?\n"
 + title[ind] + " by " + author[ind] + " having ID-" + ID[ind], parent=form)):
        del title[ind]
        del author[ind]
        del loanStatus[ind]
        del ID[ind]
        numberOfBooks = booksNumber() - 1
        title, author, loanStatus, ID = quickSortID(0, numberOfBooks, title, 
                                                    author, loanStatus, ID)
        writeToDatabase(title, author, loanStatus, ID, numberOfBooks)
        form.destroy()
        booksToRemove()
def changeRemoveColour(i, searchValueNew, toBeRemoved):
    """Changes colours of all remove buttons, not associated with a book suggested 
    to be removed, to the default colour:
    
    i - index of a line
    searchValueNew - the list of values, showing each book's similarity to user's 
    input
    toBeRemoved - the list of indices of books, suggested to be removed."""
    global remove
    if (i == 0):
        for z in range(0, toBeRemoved[0]):
            if (searchValueNew[z] > 0):
                remove[z].config(bg = bgColour)
    if (i > 0 and i < len(toBeRemoved) - 1):
        for z in range(toBeRemoved[i - 1] + 1, toBeRemoved[i]):
            if (searchValueNew[z] > 0):
                remove[z].config(bg = bgColour)
    if (i == len(toBeRemoved) - 1):
        for z in range(toBeRemoved[i] + 1, booksNumber()):
            if (searchValueNew[z] > 0):
                remove[z].config(bg = bgColour)
def chngBookStatus(ind):
    """Changes the status of a book from checked out to returned or otherwise:

    ind - the index of a book."""
    global form, searchBox
    numberOfBooks = booksNumber()
    title, author, loanStatus, ID = readList()
    searchValue = search(searchBox.get(), title, author, ID, loanStatus)
    searchValue, title, author, loanStatus, ID = quickSort(0, numberOfBooks, 
                                       searchValue, title, author, loanStatus, ID)
    if not (int(loanStatus[ind]) == 0):
        if (messagebox.askokcancel("Confirmation Box","Are you sure you want to \
mark the following book as returned?\n"
 + title[ind] + " by " + author[ind] + " having ID-" + ID[ind], parent=form)):
            loanStatus[ind] = 0
            title, author, loanStatus, ID = quickSortID(0, numberOfBooks, title, 
                                                        author, loanStatus, ID)
            writeToDatabase(title, author, loanStatus, ID, numberOfBooks)
            bookreturn.writeToLogfileRet(ID[ind])
            updateList()
    else:
        checkOutParticular(title, author,loanStatus, ID, ID[ind])
        updateList()