diff --git a/include/class.attachment.php b/include/class.attachment.php
index 8644d671c4986f6b5541e6b9b592fa06fde91652..442c9865e495d8e6d32e380f712524b66c5ef0e6 100644
--- a/include/class.attachment.php
+++ b/include/class.attachment.php
@@ -93,10 +93,12 @@ class Attachment {
         return db_result(db_query($sql));
     }
 
-    function lookup($id,$tid=0) {
-        $id=is_numeric($id)?$id:self::getIdByFileHash($hash,$tid);
+    function lookup($var,$tid=0) {
+        $id=is_numeric($var)?$var:self::getIdByFileHash($var,$tid);
 
-        return ($id && is_numeric($id) && ($attach = new Attachment($id,$tid)) && $attach->getId()==$id)?$attach:null;
+        return ($id && is_numeric($id)
+            && ($attach = new Attachment($id,$tid))
+            && $attach->getId()==$id)?$attach:null;
     }
 
 }
diff --git a/include/class.canned.php b/include/class.canned.php
index 6c6c1ac1c75bcbc25b7c288c2e49909e30b4876a..51d39ca48024410bdefecba03f20b7b030f30649 100644
--- a/include/class.canned.php
+++ b/include/class.canned.php
@@ -130,6 +130,7 @@ class Canned {
     */
     function uploadAttachments($files) {
 
+        $i=0;
         foreach($files as $file) {
             if(($fileId=is_numeric($file)?$file:AttachmentFile::upload($file)) && is_numeric($fileId)) {
                 $sql ='INSERT INTO '.CANNED_ATTACHMENT_TABLE
@@ -174,7 +175,7 @@ class Canned {
         return self::save(0,$vars,$errors);
     }
 
-    function getIdByTitle($titke) {
+    function getIdByTitle($title) {
         $sql='SELECT canned_id FROM '.CANNED_TABLE.' WHERE title='.db_input($title);
         if(($res=db_query($sql)) && db_num_rows($res))
             list($id)=db_fetch_row($res);
diff --git a/include/class.faq.php b/include/class.faq.php
index 7e23803dc458272ed7f81184c190213ddabd0e85..3e8bcf7c191d560cb8b9213e1c0a5a14a06a5352 100644
--- a/include/class.faq.php
+++ b/include/class.faq.php
@@ -204,6 +204,7 @@ class FAQ {
     
     function uploadAttachments($files) {
 
+        $i=0;
         foreach($files as $file) {
             if(($fileId=is_numeric($file)?$file:AttachmentFile::upload($file)) && is_numeric($fileId)) {
                 $sql ='INSERT INTO '.FAQ_ATTACHMENT_TABLE
diff --git a/include/class.filter.php b/include/class.filter.php
index 4f18e96514c307077b7300a02674f8ad1698039e..da8e6797df0466cab45781a44d9a842013527bc6 100644
--- a/include/class.filter.php
+++ b/include/class.filter.php
@@ -419,6 +419,7 @@ class Filter {
         if($errors || !$id) return false;
 
         //Success with update/create...save the rules. We can't recover from any errors at this point.
+        # Don't care about errors stashed in $xerrors
         self::save_rules($id,$vars,$xerrors);
       
         return true;
diff --git a/include/class.staff.php b/include/class.staff.php
index 878c0333efc1cc5ad8416dba744c02ebc0c379f5..d3b2584e9b331877ac1b12f16bf733767c55cffc 100644
--- a/include/class.staff.php
+++ b/include/class.staff.php
@@ -495,7 +495,7 @@ class Staff {
     }
 
     function login($username, $passwd, &$errors, $strike=true) {
-        global $cfg;
+        global $cfg, $session;
 
 
         if($_SESSION['_staff']['laststrike']) {
@@ -552,7 +552,7 @@ class Staff {
     }
 
     function create($vars, &$errors) {
-        if(($id=self::save(0, $vars, $errors)) && $vars['teams'] && ($self=Staff::lookup($id)))
+        if(($id=self::save(0, $vars, $errors)) && $vars['teams'] && ($staff=Staff::lookup($id)))
             $staff->updateTeams($vars['teams']);
 
         return $id;
diff --git a/include/class.ticket.php b/include/class.ticket.php
index b0f54d2c761461ba987b0e4f91f86ad276d0290e..8eca918fbb101b71f02276b930e62b1bde35a3b1 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -188,7 +188,8 @@ class Ticket{
         if(!strcasecmp($client->getEmail(),$this->getEmail()))
             return true;
 
-        return ($cfg && $cfg->showRelatedTickets() && $client->getTicketId()==$ticket->getExtId());
+        return ($cfg && $cfg->showRelatedTickets() 
+            && $client->getTicketId()==$this->getExtId());
     }
 
     //Getters
@@ -633,7 +634,7 @@ class Ticket{
             .', priority_id='.db_input($priorityId)
             .' WHERE ticket_id='.db_input($this->getId());
 
-        return (db_query($sql) && db_affected_rows($res));
+        return (($res=db_query($sql)) && db_affected_rows($res));
     }
 
     //DeptId can NOT be 0. No orphans please!
@@ -959,7 +960,7 @@ class Ticket{
     }
 
     function onAssign($note, $alert=true) {
-        global $cfg;
+        global $cfg, $thisstaff;
 
         if($this->isClosed()) $this->reopen(); //Assigned tickets must be open - otherwise why assign?
 
@@ -1016,7 +1017,7 @@ class Ticket{
         return true;
     }
 
-    function onOverdue($whine=true) {
+   function onOverdue($whine=true, $comments="") {
         global $cfg;
 
         if($whine && ($sla=$this->getSLA()) && !$sla->alertOnOverdue())
@@ -1026,8 +1027,9 @@ class Ticket{
         if(!$whine || !$cfg->alertONOverdueTicket())
             return true;
 
-        //Get template.
-        if(!($tpl = $dept->getTemplate()))
+        $dept = $this->getDept();
+        //Get department-defined or default template.
+        if(!$dept || !($tpl = $dept->getTemplate()))
             $tpl= $cfg->getDefaultTemplate();
 
         //Email to use!
@@ -1776,7 +1778,7 @@ class Ticket{
             if($cfg->getMaxOpenTickets()>0 && strcasecmp($origin,'staff') 
                     && ($client=Client::lookupByEmail($vars['email']))
                     && ($openTickets=$client->getNumOpenTickets())
-                    && ($opentickets>=$cfg->getMaxOpenTickets()) ) {
+                    && ($openTickets>=$cfg->getMaxOpenTickets()) ) {
 
                 $errors['err']="You've reached the maximum open tickets allowed.";
                 Sys::log(LOG_WARNING, 'Ticket denied -'.$vars['email'], 
@@ -1976,8 +1978,8 @@ class Ticket{
         else
             $vars['message']=$vars['issue'];
 
-        if($var['source'] && !in_array(strtolower($var['source']),array('email','phone','other')))
-            $errors['source']='Invalid source - '.Format::htmlchars($var['source']);
+        if($vars['source'] && !in_array(strtolower($vars['source']),array('email','phone','other')))
+            $errors['source']='Invalid source - '.Format::htmlchars($vars['source']);
 
         if(!($ticket=Ticket::create($vars, $errors, 'staff', false, (!$vars['assignId']))))
             return false;
@@ -1995,7 +1997,7 @@ class Ticket{
             }
         }
         //Post Internal note
-        if($var['assignId'] && $thisstaff->canAssignTickets()) { //Assign ticket to staff or team.
+        if($vars['assignId'] && $thisstaff->canAssignTickets()) { //Assign ticket to staff or team.
             $ticket->assign($vars['assignId'],$vars['note']);
         } elseif($vars['note']) { //Not assigned...save optional note if any
             $ticket->postNote('New Ticket',$vars['note'],false);
diff --git a/include/mysql.php b/include/mysql.php
index 192859af970722c9127e1c57c7596085e98290f5..4f50ee6d2cc49d08eabc98a38584048d8c57661b 100644
--- a/include/mysql.php
+++ b/include/mysql.php
@@ -46,7 +46,8 @@
     function db_version(){
 
         $version=0;
-        if(preg_match('/(\d{1,2}\.\d{1,2}\.\d{1,2})/', mysql_result(db_query('SELECT VERSION()'),0,0),$matches))
+        if(preg_match('/(\d{1,2}\.\d{1,2}\.\d{1,2})/', 
+                mysql_result(db_query('SELECT VERSION()'),0,0),$matches))
             $version=$matches[1];
 
         return $version;
diff --git a/include/pear/Mail/mimeDecode.php b/include/pear/Mail/mimeDecode.php
index b7984d681e38a30e5e546d2846019b93b38f3c33..59b6e1923c32d5ea5a3816e5c14821b1913fd3bf 100644
--- a/include/pear/Mail/mimeDecode.php
+++ b/include/pear/Mail/mimeDecode.php
@@ -721,6 +721,7 @@ class Mail_mimeDecode extends PEAR
         $this->_decode_headers = FALSE;
         $headerlist =$this->_parseHeaders($this->_header);
         $to = "";
+        $header = array();
         if (!$headerlist) {
             return $this->raiseError("Message did not contain headers");
         }