diff --git a/include/class.dept.php b/include/class.dept.php
index 5516646129b348de5eba208d005c8ab87d2f4145..f11a264ec677e0b2109e83d4f5465c51e68b1288 100644
--- a/include/class.dept.php
+++ b/include/class.dept.php
@@ -24,7 +24,7 @@ class Dept {
 
     var $ht;
   
-    function Dept($id){
+    function Dept($id) {
         $this->id=0;
         $this->load($id);
     }
@@ -55,24 +55,24 @@ class Dept {
         return true;
     }
 
-    function reload(){
+    function reload() {
         return $this->load();
     }
 
-    function getId(){
+    function getId() {
         return $this->id;
     }
     
-    function getName(){
+    function getName() {
         return $this->ht['name'];
     }
 
         
-    function getEmailId(){
+    function getEmailId() {
         return $this->ht['email_id'];
     }
 
-    function getEmail(){
+    function getEmail() {
         
         if(!$this->email && $this->getEmailId())
             $this->email=Email::lookup($this->getEmailId());
@@ -80,16 +80,16 @@ class Dept {
         return $this->email;
     }
 
-    function getNumStaff(){
+    function getNumStaff() {
         return $this->ht['users'];
     }
 
      
-    function getNumUsers(){
+    function getNumUsers() {
         return $this->getNumStaff();
     }
 
-    function getNumMembers(){
+    function getNumMembers() {
         return count($this->getMembers());
     }
 
@@ -117,11 +117,11 @@ class Dept {
     }
 
 
-    function getSLAId(){
+    function getSLAId() {
         return $this->ht['sla_id'];
     }
 
-    function getSLA(){
+    function getSLA() {
 
         if(!$this->sla && $this->getSLAId())
             $this->sla=SLA::lookup($this->getSLAId());
@@ -164,11 +164,11 @@ class Dept {
         return ($this->getSignature() && $this->isPublic());
     }
     
-    function getManagerId(){
+    function getManagerId() {
         return $this->ht['manager_id'];
     }
 
-    function getManager(){
+    function getManager() {
      
         if(!$this->manager && $this->getManagerId())
             $this->manager=Staff::lookup($this->getManagerId());
@@ -176,19 +176,19 @@ class Dept {
         return $this->manager;
     }
 
-    function isPublic(){
+    function isPublic() {
          return ($this->ht['ispublic']);
     }
     
-    function autoRespONNewTicket(){
+    function autoRespONNewTicket() {
         return ($this->ht['ticket_auto_response']);
     }
         
-    function autoRespONNewMessage(){
+    function autoRespONNewMessage() {
         return ($this->ht['message_auto_response']);
     }
 
-    function noreplyAutoResp(){
+    function noreplyAutoResp() {
          return ($this->ht['noreply_autoresp']);
     }
 
@@ -201,7 +201,7 @@ class Dept {
         return $this->ht;
     }
 
-    function getInfo(){
+    function getInfo() {
         return $this->getHashtable();
     }
 
@@ -243,9 +243,9 @@ class Dept {
 
     }
 
-    function update($vars,&$errors){
+    function update($vars, &$errors) {
 
-        if(!$this->save($this->getId(),$vars,$errors))
+        if(!$this->save($this->getId(), $vars, $errors))
             return false;
 
         $this->updateAllowedGroups($vars['groups']);
@@ -262,7 +262,7 @@ class Dept {
 
         $id=$this->getId();
         $sql='DELETE FROM '.DEPT_TABLE.' WHERE dept_id='.db_input($id).' LIMIT 1';
-        if(db_query($sql) && ($num=db_affected_rows())){
+        if(db_query($sql) && ($num=db_affected_rows())) {
             // DO SOME HOUSE CLEANING
             //Move tickets to default Dept. TODO: Move one ticket at a time and send alerts + log notes.
             db_query('UPDATE '.TICKET_TABLE.' SET dept_id='.db_input($cfg->getDefaultDeptId()).' WHERE dept_id='.db_input($id));
@@ -277,6 +277,23 @@ class Dept {
         return $num;
     }
 
+    //Replace staff's variables
+    function replaceVariables($text, $prefix='dept') {
+
+        $manager = $this->getManager();
+
+        $prefix = rtrim($prefix, '.');
+        $search = array("/%$prefix.name/", "/%$prefix.manager/", "/%$prefix.signature/");
+        $replace = array($this->getName(),
+                         ($manager?$manager->getName():''),
+                         $this->getSignature());
+        while ($text != ($T = preg_replace($search, $replace, $text))) {
+            $text = $T;
+        }
+
+        return $text;
+    }
+
     /*----Static functions-------*/
 	function getIdByName($name) {
         $id=0;
@@ -287,7 +304,7 @@ class Dept {
         return $id;
     }
 
-    function lookup($id){
+    function lookup($id) {
         return ($id && is_numeric($id) && ($dept = new Dept($id)) && $dept->getId()==$id)?$dept:null;
     }
 
@@ -323,14 +340,14 @@ class Dept {
         return self::getDepartments(true);
     }
 
-    function create($vars,&$errors) {
+    function create($vars, &$errors) {
         if(($id=self::save(0, $vars, $errors)) && ($dept=self::lookup($id)))
             $dept->updateAllowedGroups($vars['groups']);
 
         return $id;
     }
 
-    function save($id,$vars,&$errors) {
+    function save($id, $vars, &$errors) {
         global $cfg;
                 
         if($id && $id!=$vars['id'])
@@ -342,11 +359,11 @@ class Dept {
         if(!is_numeric($vars['tpl_id']))
             $errors['tpl_id']='Template selection required';
 
-        if(!$vars['name']){
+        if(!$vars['name']) {
             $errors['name']='Name required';
-        }elseif(strlen($vars['name'])<4) {
+        } elseif(strlen($vars['name'])<4) {
             $errors['name']='Name is too short.';
-        }elseif(($did=Dept::getIdByName($vars['name'])) && $did!=$id){
+        } elseif(($did=Dept::getIdByName($vars['name'])) && $did!=$id) {
             $errors['name']='Department already exist';
         }
         
@@ -377,7 +394,7 @@ class Dept {
             
             $errors['err']='Unable to update '.Format::htmlchars($vars['name']).' Dept. Error occurred';
            
-        }else{
+        } else {
             $sql='INSERT INTO '.DEPT_TABLE.' '.$sql.',created=NOW()';
             if(db_query($sql) && ($id=db_insert_id()))
                 return $id;
diff --git a/include/class.staff.php b/include/class.staff.php
index 83c9ce2e58da2905c4716a28ff159dfb870074b1..672782dad0d44b96c87125475039767adb272849 100644
--- a/include/class.staff.php
+++ b/include/class.staff.php
@@ -502,6 +502,26 @@ class Staff {
         return $num;
     }
 
+    //Replace staff's variables
+    function replaceVariables($text, $prefix='staff') {
+
+        $prefix = rtrim($prefix, '.');
+        $search = array("/%$prefix.username/", "/%$prefix.firstname/", "/%$prefix.lastname/", 
+                        "/%$prefix.name/", "/%$prefix.email/", "/%$prefix.signature/");
+        $replace = array($this->getUserName(),
+                         $this->getFirstName(),
+                         $this->getLastName(),
+                         $this->getName(),
+                         $this->getEmail(),
+                         $this->getSignature());
+
+        while ($text != ($T = preg_replace($search,$replace,$text))) {
+            $text = $T;
+        }
+
+        return $text;
+    }
+
     /**** Static functions ********/
 
     function getStaffMembers($availableonly=false) {
diff --git a/include/class.team.php b/include/class.team.php
index cdf4cffcd3148fab082b2dd69f24733481aaeca0..60301e26712543a024e7176b7b8b272774d2f348 100644
--- a/include/class.team.php
+++ b/include/class.team.php
@@ -21,7 +21,7 @@ class Team {
 
     var $members;
 
-    function Team($id){
+    function Team($id) {
 
         return $this->load($id);
     }
@@ -47,30 +47,30 @@ class Team {
         return $this->id;
     }
 
-    function reload(){
+    function reload() {
         return $this->load($this->getId());
     }
 
-    function getId(){
+    function getId() {
         return $this->id;
     }
 
-    function getName(){
+    function getName() {
         return $this->ht['name'];
     }
 
-    function getNumMembers(){
+    function getNumMembers() {
         return $this->ht['members'];
     }
 
-    function getMembers(){
+    function getMembers() {
 
-        if(!$this->members && $this->getNumMembers()){
+        if(!$this->members && $this->getNumMembers()) {
             $sql='SELECT m.staff_id FROM '.TEAM_MEMBER_TABLE.' m '
                 .'LEFT JOIN '.STAFF_TABLE.' s USING(staff_id) '
                 .'WHERE m.team_id='.db_input($this->getId()).' AND s.staff_id IS NOT NULL '
                 .'ORDER BY s.lastname, s.firstname';
-            if(($res=db_query($sql)) && db_num_rows($res)){
+            if(($res=db_query($sql)) && db_num_rows($res)) {
                 while(list($id)=db_fetch_row($res))
                     if(($staff= Staff::lookup($id)))
                         $this->members[]= $staff;
@@ -87,18 +87,18 @@ class Team {
             .'   AND staff_id='.db_input($staff->getId())) !== 0;
     }
 
-    function getLeadId(){
+    function getLeadId() {
         return $this->ht['lead_id'];
     }
 
-    function getTeamLead(){
+    function getTeamLead() {
         if(!$this->lead && $this->getLeadId())
             $this->lead=Staff::lookup($this->getLeadId());
 
         return $this->lead;
     }
 
-    function getLead(){
+    function getLead() {
         return $this->getTeamLead();
     }
 
@@ -106,27 +106,27 @@ class Team {
         return $this->ht;
     }
 
-    function getInfo(){
+    function getInfo() {
         return  $this->getHashtable();
     }
 
-    function isEnabled(){
+    function isEnabled() {
         return ($this->ht['isenabled']);
     }
 
-    function isActive(){
+    function isActive() {
         return $this->isEnabled();
     }
 
-    function update($vars,&$errors) {
+    function update($vars, &$errors) {
 
         //reset team lead if they're being deleted
         if($this->getLeadId()==$vars['lead_id'] 
-                && $vars['remove'] && in_array($this->getLeadId(),$vars['remove']))
+                && $vars['remove'] && in_array($this->getLeadId(), $vars['remove']))
             $vars['lead_id']=0;
 
         //Save the changes...
-        if(!Team::save($this->getId(),$vars,$errors))
+        if(!Team::save($this->getId(), $vars, $errors))
             return false;
 
         //Delete staff marked for removal...
@@ -169,13 +169,28 @@ class Team {
         return true;
     }
 
+    //Replace staff's variables
+    function replaceVariables($text, $prefix='team') {
+
+        $lead = $this->getLead();
+
+        $prefix = rtrim($prefix, '.');
+        $search = array("/%$prefix.name/", "/%$prefix.lead/");
+        $replace = array($this->getName(), ($lead?$lead->getName():''));
+        while ($text != ($T = preg_replace($search, $replace, $text))) {
+            $text = $T;
+        }
+
+        return $text;
+    }
+
     /* ----------- Static function ------------------*/
-    function lookup($id){
+    function lookup($id) {
         return ($id && is_numeric($id) && ($team= new Team($id)) && $team->getId()==$id)?$team:null;
     }
 
 
-    function getIdbyName($name){
+    function getIdbyName($name) {
 
         $sql='SELECT team_id FROM '.TEAM_TABLE.' WHERE name='.db_input($name);
         if(($res=db_query($sql)) && db_num_rows($res))
@@ -201,7 +216,7 @@ class Team {
                 .' ORDER by t.name ';
         }
         if(($res=db_query($sql)) && db_num_rows($res)) {
-            while(list($id,$name)=db_fetch_row($res))
+            while(list($id, $name)=db_fetch_row($res))
                 $teams[$id] = $name;
         }
 
@@ -212,20 +227,20 @@ class Team {
         return self::getTeams(true);
     }
 
-    function create($vars,&$errors) { 
-        return self::save(0,$vars,$errors);
+    function create($vars, &$errors) { 
+        return self::save(0, $vars, $errors);
     }
 
-    function save($id,$vars,&$errors) {
+    function save($id, $vars, &$errors) {
 
         if($id && $vars['id']!=$id)
             $errors['err']='Missing or invalid team';
             
         if(!$vars['name']) {
             $errors['name']='Team name required';
-        }elseif(strlen($vars['name'])<3) {
+        } elseif(strlen($vars['name'])<3) {
             $errors['name']='Team name must be at least 3 chars.';
-        }elseif(($tid=Team::getIdByName($vars['name'])) && $tid!=$id){
+        } elseif(($tid=Team::getIdByName($vars['name'])) && $tid!=$id) {
             $errors['name']='Team name already exists';
         }
         
@@ -242,7 +257,7 @@ class Team {
                 return true;
                     
             $errors['err']='Unable to update the team. Internal error';
-        }else{
+        } else {
             $sql='INSERT INTO '.TEAM_TABLE.' '.$sql.',created=NOW()';
             if(db_query($sql) && ($id=db_insert_id()))
                 return $id;