 
 class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
 
+    // MARK: Global core variables
+    
     var personInfo: String = ""
     var items = [String]()
-    var updatedDatabase = false
+    
+    // MARK: App utilities
     
     @IBOutlet var textField: UITextField!
     
     @IBOutlet var textFieldPerson: UITextField!
     
     @IBOutlet var tableView: UITableView!
-    
-    @IBAction func addButton(sender: UIButton) {
-        
-        let newItem = textField.text
-        let newInfo = textFieldPerson.text
-        
-        personInfo = newInfo!
-        
-        let dataString = addUnderscores(newItem!)
-        
-        //if the data that the user inputted is correct
-        if (dataString != ""){
-            
-            updateDatabase(dataString, rowData: personInfo)
-            
-            if (updatedDatabase == true) {
-            
-                items.insert(newItem! + " " + getDate(), atIndex: 0)
-                textField.resignFirstResponder()
-            
-                textField.text = ""
-                tableView.reloadData()
-            
-                saveCoreVariables()
-                
-                updatedDatabase = false
-            
-            } else {
-                
-                alert("Connection", alertMessage: "Could not update database")
-                
-            }
-            
-        }
-        
-    }
     
     override func viewDidLoad() {
         super.viewDidLoad()

        
    }
    
    //update the Parse database
    func updateDatabase(columnName: String,rowData: String){
        
        let SIObject = PFObject(className: "SI_Sign_in")
        SIObject[columnName] = rowData
        SIObject.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in self.updatedDatabaseTrue() }
        
    }
    
    func updatedDatabaseTrue() {
        updatedDatabase = true
    }
    
    //Create an alert with a title, message, and dismiss button
    func alert(alertTitle: String,alertMessage: String){
        
        let alertController = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertControllerStyle.Alert)
        
        alertController.addAction(UIAlertAction(title: "dismiss", style: UIAlertActionStyle.Default,handler: nil))
        
        self.presentViewController(alertController, animated: true, completion: nil)
        
    }
    
    //So we can convert "Bob Smith" to "bob_smith"
    func addUnderscores(var inputString: String) -> String{
        
        var returnString = ""
        
        //Check if the string contains a date with forward slashes
        if inputString != "" {
            
            //split string by the spaces, then join it by underscore
            let newItemArr = inputString.componentsSeparatedByString(" ")
            inputString = newItemArr.joinWithSeparator("_")
            
            returnString = inputString.lowercaseString
            
        } else {
            
            alert("Invalid entry", alertMessage: "Empty input")
        
        }
        
        return returnString
        
    }
    
    //Saves the variables we need to remember
    func saveCoreVariables(){

            textFieldPerson.text = personInfo
        }
        tableView.reloadData()
        
    }
    
    // MARK: Table View functions
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        
        return items.count
        
    }
    
    //load the table view based off the items array
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
        cell.textLabel?.text = items[indexPath.row]
        
        return cell
        
    }
    
    //When the user clicks on a tableview cell the SI name in that cell is put in the SI name textfield
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        let autoCompleteString = items[indexPath.row]
        let returnString = autoCompleteString.componentsSeparatedByString(" ")
        
        textField.text = returnString[0] + " " + returnString[1]
        
        tableView.reloadData()
        
    }
    
    // MARK: Tools
    
    //Create an alert with a title, message, and dismiss button
    func alert(alertTitle: String,alertMessage: String){
        
        let alertController = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertControllerStyle.Alert)
        
        alertController.addAction(UIAlertAction(title: "dismiss", style: UIAlertActionStyle.Default,handler: nil))
        
        self.presentViewController(alertController, animated: true, completion: nil)
        
    }
    
