Index: Dev/trunk/classes/Loader.php
===================================================================
--- Dev/trunk/classes/Loader.php	(revision 115)
+++ Dev/trunk/classes/Loader.php	(revision 116)
@@ -2,5 +2,5 @@
 
 /**
- * Helper class to load stuff
+ * Helper class to load arrays of objects from the database.
  *
  * @author fpvanagthoven
Index: Dev/trunk/classes/QuestionCreationTool.php
===================================================================
--- Dev/trunk/classes/QuestionCreationTool.php	(revision 115)
+++ Dev/trunk/classes/QuestionCreationTool.php	(revision 116)
@@ -12,7 +12,19 @@
     public function __construct($questions) {
         $this->questions = $questions;
+
+        $this->loadQuestionsToJavascript();
         $this->javascript();
         $this->displayList();
         $this->displayEditPanel();
+    }
+    
+    /**
+     * Loads the questions that PHP retrieved from database
+     * into javascript. This is done so that the page does not need
+     * to refresh in order to load an entire question in to the form.
+     */
+    private function loadQuestionsToJavascript()
+    {
+        
     }
 
@@ -24,8 +36,8 @@
                 var type = select.valueOf().value;
                 clearSpecifications();
-                                                                                
+                                                                                                        
                 /* set answerCount for options */
                 document.getElementById("answerSpecifications").answerCount = 1;
-                                                                                
+                                                                                                        
                 switch (type) {
                     case "text":
@@ -44,10 +56,10 @@
                         break;
                     default:
-                                                                                        
+                                                                                                                
                         break;
                 }
 
             }
-                                                                            
+                                                                                                    
             function clearSpecifications()
             {
@@ -56,5 +68,5 @@
                 specs.clicked = null;
                 specs.lastAnswer = null;
-                                                                
+                                                                                        
                 var questionType = document.getElementById("questionType");
                 var buttons = document.getElementById("questionButtons");
@@ -62,18 +74,18 @@
                     questionType.removeChild(buttons);
             }
-                                                                            
+                                                                                                    
             function addOption()
             {                       
                 var specs = document.getElementById("answerSpecifications");
-                                                                                
+                                                                                                        
                 addAddRemoveOptionButtonsOnce(specs);
                 addAnswerInput(specs);
-                                                                                
-            }
-                                                                            
+                                                                                                        
+            }
+                                                                                                    
             function removeOption()
             {
                 var specs = document.getElementById("answerSpecifications");
-                                                                                
+                                                                                                        
                 if(specs.lastAnswer.prev != null)
                 {
@@ -83,12 +95,12 @@
                 }
             }
-                                                                            
+                                                                                                    
             function addAnswerInput(specs)
             {
                 var answerCount = specs.answerCount;
-                                                                                
+                                                                                                        
                 /* set value for option textbox */
                 var optionStr = "Option " + answerCount;
-                                                                                
+                                                                                                        
                 /* the input textbox */
                 var answerInput = document.createElement("input");
@@ -97,14 +109,14 @@
                 answerInput.setAttribute("value", optionStr);
                 answerInput.className = "answerInput";
-                                                                                
+                                                                                                        
                 specs.appendChild(answerInput);
-                                                                                
+                                                                                                        
                 /* Singly linked list */
                 answerInput.prev = specs.lastAnswer; // remember last one
                 specs.lastAnswer = answerInput; // new lastAnswer
-                                                                                
+                                                                                                        
                 specs.answerCount++;               
             }
-                                                                            
+                                                                                                    
             function addAddRemoveOptionButtonsOnce(specs)
             {
@@ -116,12 +128,12 @@
                     var typeBox = document.getElementById("questionType");
                     typeBox.appendChild(buttonsDiv);
-                                                                    
+                                                                                            
                     addAddOptionButton(buttonsDiv);                                  
                     addRemoveOptionButton(buttonsDiv);
-                                                                                    
+                                                                                                            
                     specs.clicked = true;
                 }
             }
-                                                                            
+                                                                                                    
             function addAddOptionButton(buttonsDiv)
             {    
@@ -134,5 +146,5 @@
                 buttonsDiv.appendChild(addOpt);                                  
             }
-                                                                            
+                                                                                                    
             function addRemoveOptionButton(buttonsDiv)
             {
@@ -144,5 +156,5 @@
                 buttonsDiv.appendChild(removeOpt);
             }
-                                                                           
+                                                                                                   
             function minMax(min, max)
             {
@@ -151,16 +163,16 @@
                 if (max == null)
                     var max = '';
-                                                                                                                                                                                                                                                                                                        
+                                                                                                                                                                                                                                                                                                                                
                 var specs = document.getElementById("answerSpecifications");
-                                                                                                                                                                                                                                                                                                                                                                                                                                               
+                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
                 var answerDiv = document.createElement("div");
-                                                                                                                                                                                                                                                                                                                                                                                                                
+                                                                                                                                                                                                                                                                                                                                                                                                                                        
                 answerDiv.className = "answerDiv";
                 answerDiv.innerHTML = "<label for='min'>Min</label><input type='text' class='intBox' onchange='checkInt(this)' value='" + min + "' name='ans1' />" +
                     "<label for='max'>Max</label><input type='text' class='intBox' onchange='checkInt(this)' value='" + max + "' name='ans2' />";
-                                                                                                                                                                                                                                                                                                                                                                                                                                           
+                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
                 specs.appendChild(answerDiv);
             }      
-                                                    
+                                                                            
             function checkInt(input)
             {
@@ -178,5 +190,5 @@
                 }
             }
-                                    
+                                                            
             function minMaxIncr(left, right, incr)
             {
@@ -191,10 +203,10 @@
                 if (incr == null)
                     var incr = '';
-                                                                                                                                                                                                                                
+                                                                                                                                                                                                                                                        
                 var specs = document.getElementById("answerSpecifications");
-                                                                                                                                                                                                                                                                                                                                                                                                                       
+                                                                                                                                                                                                                                                                                                                                                                                                                                               
                 var answerDiv = document.createElement("div");
                 answerDiv.className = "answerDiv";
-                                                                                                                                                       
+                                                                                                                                                                               
                 var leftLabel = document.createElement("label");
                 var rightLabel = document.createElement("label");
@@ -203,5 +215,5 @@
                 rightLabel.innerHTML = "Right label";
                 scaleLabel.innerHTML = "Scale count";
-                                                                                                                                                        
+                                                                                                                                                                                
                 var leftInput = document.createElement("input");
                 leftInput.type = "text";
@@ -209,5 +221,5 @@
                 leftInput.name= "ans1";
                 leftInput.setAttribute("onchange", "handleAnswerChange()");
-                                                                                                                                                        
+                                                                                                                                                                                
                 var rightInput = document.createElement("input");
                 rightInput.type = "text";
@@ -215,5 +227,5 @@
                 rightInput.name = "ans2";
                 rightInput.setAttribute("onchange", "handleAnswerChange()");
-                                                                                                                                                        
+                                                                                                                                                                                
                 var scaleInput = document.createElement("input");
                 scaleInput.type = "text";
@@ -223,5 +235,5 @@
                 scaleInput.setAttribute("onchange", "");
                 scaleInput.name = "ans3";
-                                                                                                                                                        
+                                                                                                                                                                                
                 answerDiv.appendChild(leftLabel);
                 answerDiv.appendChild(leftInput);
@@ -230,19 +242,19 @@
                 answerDiv.appendChild(scaleLabel);
                 answerDiv.appendChild(scaleInput);
-                                                                                                                                                                                                                                                                                                                                                                                                   
+                                                                                                                                                                                                                                                                                                                                                                                                                           
                 specs.appendChild(answerDiv);
             }   
-                    
+                                            
             function newCategory()
             {
                 var categoryBox = document.getElementById("categoryBox");
                 categoryBox.innerHTML = "";
-                        
+                                                
                 var categoryInput = document.createElement("input");
                 categoryInput.name = "questionCategory";
-                        
+                                                
                 categoryBox.appendChild(categoryInput);
             }
-                               
+                                                       
             function clearFields()
             {
@@ -253,14 +265,14 @@
                 clearType();
             }
-                    
+                                            
             function clearCategory()
             {
                 var categoryBox = document.getElementById("categoryBox");
                 categoryBox.innerHTML = "";
-                        
+                                                
                 var questionCategorySelect = document.createElement("select");
                 questionCategorySelect.setAttribute("name", "questionCategory");
                 questionCategorySelect.id = "questionCategorySelect";
-                        
+                                                
                 var newCategoryButton = document.createElement("input");
                 newCategoryButton.setAttribute("type", "button");
@@ -268,9 +280,9 @@
                 newCategoryButton.setAttribute("value", "New Category");
                 newCategoryButton.className = "surveyButton";
-                        
+                                                
                 categoryBox.appendChild(questionCategorySelect);
                 categoryBox.appendChild(newCategoryButton);
             }
-                    
+                                            
             function clearCode()
             {
@@ -278,5 +290,5 @@
                 codeInput.value = "";
             }
-                    
+                                            
             function clearTitle()
             {
@@ -284,5 +296,5 @@
                 titleInput.value ="";
             }
-                    
+                                            
             function clearDescription()
             {
@@ -290,5 +302,5 @@
                 descriptionInput.value = "";
             }
-                    
+                                            
             function clearType()
             {
@@ -297,5 +309,5 @@
                 clearSpecifications();
             }
-                    
+                                            
         </script>
         <?php
@@ -306,5 +318,5 @@
         <div id="questionListWrapper">
             <select id="questionsList" size="9999">
-        <?php $this->populateList(); ?>
+                <?php $this->populateList(); ?>
             </select>
         </div>
@@ -315,5 +327,5 @@
         ?>
         <div id="questionCreation" class="floatLeft">
-        <?php $this->displayForm(); ?>
+            <?php $this->displayForm(); ?>
         </div>
         <?php
@@ -327,5 +339,5 @@
                     <td><label for="questionCategory">Category</label></td>
                     <td id="categoryBox"><!-- Select should be filled with existing categories-->
-        <?php $this->questionCategorySelect(); ?>
+                        <?php $this->questionCategorySelect(); ?>
                     </td>
                 </tr>
@@ -372,35 +384,75 @@
         <?php
     }
-    
-    private function generateCategoryOptions()
-    {
+
+    private function generateCategoryOptions() {
         $categories = array();
-        
+
         foreach ($this->questions as $question) {
             $category = $question->category;
-            if (! array_search($category, $categories)) // if not in categories
-            {
-                array_push($categories, $category);
-            }
+            array_push($categories, $category);
         }
-        
+        $categories = array_unique($categories);
+
         // You might want to escape quotes and stuff here
         foreach ($categories as $category) {
             ?>
-        <option value="<?php echo $category;?>"><?php echo $category; ?></option> 
-        <?php
+            <option value="<?php echo $category; ?>"><?php echo $category; ?></option> 
+            <?php
         }
     }
 
+    /**
+     * Populates the question list. Sorts the list first by question category.
+     */
     private function populateList() {
+
+        $this->questions = $this->sortByCategory($this->questions);
+
+        $currentCategory = null;
+
         foreach ($this->questions as $question) {
+            if (is_null($currentCategory)) {
+                ?> <optgroup label="<?php echo $question->category; ?>"><?php
+                $currentCategory = $question->category;
+            } else if ($currentCategory != $question->category) {
+                /* Next category */
+                ?></optgroup><?php
+                $currentCategory = $question->category;
+                ?> <optgroup label="<?php echo $question->category; ?>"><?php
+            }
             ?>
-            <option value="<?php echo $question->code; ?>">
-            <?php echo $question->code . " - " . $question->title; ?>
-            </option>
+                /* function loadQuestionIn() should still be made */
+                <option value="<?php echo $question->code; ?>" onfocus="loadQuestionIn()">
+                    <?php echo "[" . $question->code . "] " . $question->title; ?>
+                </option>
+
                 <?php
             }
-        }
-
-    }
+            ?></optgroup><?php
+    }
+
+    /**
+     *
+     * @param type $questions 
+     */
+    private function sortByCategory($questions) {
+        usort($questions, "cmp");
+        return $questions;
+    }
+
+}
+
+/* This function is placed outside the class on purpose.
+ * It's only used as callback function.
+ */
+
+function cmp($q1, $q2) {
+    if ($q1->category == $q2->category) {
+        if($q1->code < $q2->code)
+            return -1;
+        else
+            return 1;
+    }
+    return ($q1->category < $q2->category) ? -1 : 1;
+}
     ?>
Index: Dev/trunk/mainmenu.php
===================================================================
--- Dev/trunk/mainmenu.php	(revision 115)
+++ Dev/trunk/mainmenu.php	(revision 116)
@@ -2,6 +2,4 @@
 require 'classes/master.php'; //should be at top of every page   
      
-//$_SESSION['username'] = 'hai'; //temp, until bug is fixed
-
 if (isset($_POST['login'])) {
     if ($_POST['username'] == '')
Index: Dev/trunk/questioncreation.php
===================================================================
--- Dev/trunk/questioncreation.php	(revision 115)
+++ Dev/trunk/questioncreation.php	(revision 116)
@@ -26,5 +26,5 @@
 
             <div id="content">
-                <?php var_dump($_POST); ?>
+                <?php var_dump($_POST);      ?>
                 <?php
                     new QuestionCreationTool($questions);
