<?php

/*
=====================================================
 ExpressionEngine - by pMachine
-----------------------------------------------------
 http://www.pmachine.com/
-----------------------------------------------------
 Copyright (c) 2003 - 2007 pMachine, Inc.
=====================================================
 THIS IS COPYRIGHTED SOFTWARE
 PLEASE READ THE LICENSE AGREEMENT
 http://eedocs.pmachine.com/license.html
=====================================================
 File: cp.display.php
-----------------------------------------------------
 Purpose: This class provides all the HTML dispaly
 elements used in the control panel.
=====================================================
*/

if ( ! defined('EXT'))
{
    exit('Invalid file request');
}


class Display {
 
 
    var $publish_nav	= 'click';  // The PUBLISH tab drop down menu behavior. Either 'click' or 'hover'
    var $title      	= '';    // Page title
    var $body       	= '';    // Main content area
    var $crumb      	= '';    // Breadcrumb.
    var $rcrumb     	= '';    // Right side breadcrumb
    var $crumbline  	= FALSE;  // Assigns whether to show the line below the breadcrumb
    var $show_crumb 	= TRUE;  // Assigns whether to show the breadcrumb
    var $crumb_ov   	= FALSE; // Crumb Override. Will prevent the "M" variable from getting auto-linked
    var $refresh    	= FALSE; // If set to a URL, the header will contain a <meta> refresh
    var $ref_rate   	= 0;     // Rate of refresh
    var	$url_append		= '';	 // This variable lets us globally append something onto URLs
    var	$body_props		= '';    // Code that can be addded the the <body> tag
    var	$initial_body	= '';    // We can manually add things just after the <body> tag.
    var $extra_css		= '';    // Additional CSS that we can fetch from a different file.  It gets added to the main CSS request.
    var $manual_css		= '';    // Additional CSS that we can generate manually.  It gets added to the main CSS request.
    var $extra_header	= '';    // Additional headers we can add manually
    var $rcrumb_css		= 'breadcrumbRight';	 // The default CSS used in the right breadcrumb
    var $padding_tabs	= 'clear';	// on/off/clear  -  The navigation tabs have an extra cell on the left and right side to provide padding.  This determis how it should be displayed.  It interacts with this variable, which is placed in the CSS file:  {padding_tabs ="clear"}
    var $empty_menu		= FALSE;	// Is the Publish weblog menu empty?
    
    /** -------------------------------------
    /**  Constructor
    /** -------------------------------------*/
    
    function Display()
    {
    	global $PREFS;
    
  		if ( ! defined('AMP')) define('AMP', '&amp;');
        if ( ! defined('BR'))  define('BR',  '<br />');
        if ( ! defined('NL'))  define('NL',  "\n");
        if ( ! defined('NBS')) define('NBS', "&nbsp;");	
        
        $this->publish_nav = (in_array($PREFS->ini('publish_tab_behavior'), array('click', 'hover', 'none'))) ? $PREFS->ini('publish_tab_behavior') : 'hover';
    }
    /* END */
   
  
    /** -------------------------------------
    /**  Set return data
    /** -------------------------------------*/
  
    function set_return_data($title = '', $body = '', $crumb = '',  $rcrumb = '')
    {
        $this->title  = $title;
        $this->body   = $body;        
        $this->crumb  = $crumb;
        $this->rcrumb = $rcrumb;
    }  
    /* END */
 
 
    /** -------------------------------------
    /**  Group Access Verification
    /** -------------------------------------*/

    function allowed_group($which = '')
    {
        global $SESS;
        
        if ($which == '')
            return false;
            
        // Super Admins always have access
                    
        if ($SESS->userdata['group_id'] == 1)
        {
			return true;
        }

        if ($SESS->userdata[$which] !== 'y')
            return false;
        else
            return true;
    }
    /* END */
  

    /** -------------------------------------
    /**  Control panel
    /** -------------------------------------*/

    function show_full_control_panel()
    {
        global $OUT, $EXT, $FNS;
        
        // -------------------------------------------
        // 'show_full_control_panel_start' hook.
        //  - Full Control over CP
        //  - Modify any $DSP class variable (JS, headers, etc.)
        //  - Override any $DSP method and use their own
        //
        	$edata = $EXT->call_extension('show_full_control_panel_start');
        	if ($EXT->end_script === TRUE) return;
        //
        // -------------------------------------------
                    
		$out =	$this->html_header()
				.$this->page_header()
				.$this->page_navigation()
				.$this->breadcrumb()
				.$this->content()
				.$this->content_close()
				.$this->copyright()
				.$this->html_footer();
				
		$out = $FNS->insert_action_ids($out);
				
		// -------------------------------------------
        // 'show_full_control_panel_end' hook.
        //  - Rewrite CP's HTML
        //	- Find/Replace Stuff, etc.
        //
        	if ($EXT->active_hook('show_full_control_panel_end') === TRUE)
        	{
        		$out = $EXT->call_extension('show_full_control_panel_end', $out);
        		if ($EXT->end_script === TRUE) return;
        	}
        //
        // -------------------------------------------
                   
        $OUT->build_queue($out);
    }
    /* END */



    /** -------------------------------------
    /**  Show restricted version of CP
    /** -------------------------------------*/

    function show_restricted_control_panel()
    {
        global $IN, $OUT, $SESS, $FNS;
        
        $r = $this->html_header();
        
        // We treat the bookmarklet as a special case
        // and show the navigation links in the top right
        // side of the page
        
        if ($IN->GBL('BK') AND $SESS->userdata['admin_sess'] == 1)
        {
            $r .= $this->page_header(0);
        }
        else
        {
            $r .= $this->simple_header('helpLinksLeft');
        }
        
        $r .= $this->content(TRUE);
        $r .= $this->content_close();
		$r .= $this->html_footer();
		
		$r = $FNS->insert_action_ids($r);
    
        $OUT->build_queue($r);
    }
    /* END */


    /** -------------------------------------
    /**  Show "login" version of CP
    /** -------------------------------------*/

    function show_login_control_panel()
    {
        global $IN, $OUT, $SESS;
        
        $this->secure_hash();
        
        $r  = $this->html_header()
		.$this->simple_header()
        .$this->body
        .$this->copyright()
        .$this->html_footer();
    
        $OUT->build_queue($r);
    }
    /* END */


    
    
    /** -------------------------------------
    /**  HTML Header
    /** -------------------------------------*/

    function html_header($title = '')
    {
        global $PREFS;
        
        if ($title == '')
            $title = $this->title;
                
        $header =

        "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n".
        "<html>\n".
        "<head>\n".
        "<title>".APP_NAME." | $title</title>\n\n".        
        "<meta http-equiv='content-type' content='text/html; charset=".$PREFS->ini('charset')."' >\n".
        "<meta http-equiv='expires' content='-1' >\n".
        "<meta http-equiv='expires' content='Mon, 01 Jan 1970 23:59:59 GMT' >\n".        
        "<meta http-equiv='pragma' content='no-cache' >\n";
        
        if ($this->refresh !== FALSE)
        {
            $header .= "<meta http-equiv=\"refresh\" content=\"".$this->ref_rate."; url=".$this->refresh."\" >\n";
        }
        
        $header .=
     
        "<style type='text/css'>\n".
        $this->fetch_stylesheet()."\n\n".
        $this->manual_css.
        "</style>\n\n".
        $this->_fetch_publish_menu().
        $this->_global_javascript().
        $this->extra_header.
        "</head>\n\n".
        "<body{$this->body_props}>\n".
        $this->initial_body."\n";
        
        return $header;
    }
    /* END */
    
        
   
    /** -------------------------------------
    /**  Fetch CSS Stylesheet
    /** -------------------------------------*/

    function fetch_stylesheet()
    {
        global $PREFS, $OUT, $SESS, $LANG;    
                
        $cp_theme = (! isset($SESS->userdata['cp_theme']) || $SESS->userdata['cp_theme'] == '') ? $PREFS->ini('cp_theme') : $SESS->userdata['cp_theme']; 
        
		$path = ( ! is_dir('./cp_themes/')) ? PATH_CP_THEME : './cp_themes/';
		
		if ( ! $theme = $this->file_open($path.$cp_theme.'/'.$cp_theme.'.css'))
		{
			if ( ! $theme = $this->file_open($path.'default/default.css'))
			{
				return '';
			}
		}
        
        if ($this->extra_css != '')
        {
			if ($extra = $this->file_open($this->extra_css))
			{
        		$theme .= NL.NL.$extra;
        	}
        }
        
        // Set the value of the "padding tabs" based on
        // a variable (that might be) contained in the CSS file
        
        if (preg_match("/\{padding_tabs\s*=\s*['|\"](.+?)['|\"]\}/", $theme, $match))
        {
        	$this->padding_tabs = $match['1'];
        	$theme = str_replace($match['0'], '', $theme);
        }        
        
        // Remove comments and spaces from CSS file
        $theme = preg_replace("/\/\*.*?\*\//s", '', $theme);
        $theme = preg_replace("/\}\s+/s", "}\n", $theme);
        
        // Replace the {path:image_url} variable. 
        
        $img_path = $PREFS->ini('theme_folder_url', 1).'cp_themes/'.$cp_theme.'/images/';       
		$theme = str_replace('{path:image_url}', $img_path, $theme);      

        return $theme;    
    }
    /* END */
   
   
    /** -------------------------------------
    /**  File Opener
    /** -------------------------------------*/

   function file_open($file)
   {
        if ( ! $fp = @fopen($file, 'rb'))
        {
			return FALSE;
        }
            
		flock($fp, LOCK_SH);
		
		$f = '';
		
		if (filesize($file) > 0) 
		{
			$f = fread($fp, filesize($file)); 
		}

		flock($fp, LOCK_UN);
        fclose($fp); 
   
   		return $f;
   }
   /* END */
   
   

    /** -------------------------------------
    /**  Page Header
    /** -------------------------------------*/

    function page_header($header = TRUE)
    {
        global $IN, $LANG, $SESS, $FNS, $PREFS;
                
		$qm = ($PREFS->ini('force_query_string') == 'y') ? '' : '?';
        
        $r = "<div id='topBar'>\n"
             .$this->table('', '', '0', '100%')
             .$this->tr()
             .$this->td('helpLinks')
             .$this->div('helpLinksLeft')
             .$this->anchor($FNS->fetch_site_index().$qm.'URL=http://www.pmachine.com/', APP_NAME.$this->nbs(2).'v '.APP_VER)
             .$this->div_c()
             .$this->td_c()
             .$this->td('helpLinks');
		
		$r .= $this->anchor(BASE.AMP.'C=myaccount'.AMP.'M=quicklinks'.AMP.'id='.$SESS->userdata('member_id'),
							"<img src='".PATH_CP_IMG."edit_quicklinks.png' border='0' width='16' height='16' style='vertical-align: bottom;' alt='".$LANG->line('edit_quicklinks')."' />")
							.$this->nbs(3);
		
        $r .= $this->fetch_quicklinks();   

        $doc_path = $PREFS->ini('doc_url');
                
		if ( ! ereg("/$", $doc_path)) 
			$doc_path .= '/';
                
        $r .= $this->anchor(BASE, $LANG->line('main_menu')).$this->nbs(3).'|'.$this->nbs(3)
             ."<a href='".$doc_path."' target='_blank'>".$LANG->line('user_guide').'</a>'.$this->nbs(3).'|'.$this->nbs(3)
             .$this->anchor(BASE.AMP.'C=logout', $LANG->line('logout')).$this->nbs(3).'|'.$this->nbs(3);
             		
		$r .= $this->anchor(BASE.AMP.'C=myaccount'.AMP.'M=tab_manager'.$this->generate_quick_tab(), $LANG->line('new_tab'))
             .$this->td_c()
             .$this->tr_c()
             .$this->table_c()
             .$this->div_c();
            
       if ($header != 0)
			$r .= "<div id='header'></div>\n";

        return $r;
    }
    /* END */
  
  
    /** -------------------------------------
    /**  Quicklinks
    /** -------------------------------------*/

    function fetch_quicklinks()
    {
        global $SESS, $FNS, $PREFS;
            
        if ( ! isset($SESS->userdata['quick_links']) || $SESS->userdata['quick_links'] == '')
        {
            return '';
        }
        
        $r = '';
                 
        foreach (explode("\n", $SESS->userdata['quick_links']) as $row)
        {                
            $x = explode('|', $row);
            
            $title = (isset($x['0'])) ? $x['0'] : '';
            $link  = (isset($x['1'])) ? $x['1'] : '';
            
			$qm = ($PREFS->ini('force_query_string') == 'y') ? '' : '?';
    
            $r .= $this->anchor($FNS->fetch_site_index().$qm.'URL='.$link, $title, '', 1).$this->nbs(3).'|'.$this->nbs(3);
    
        }
            
        return $r;
    }  
    /* END */



    /** -------------------------------------
    /**  Quck Tabs
    /** -------------------------------------*/

    function fetch_quicktabs()
    {
        global $SESS, $FNS, $PREFS;
        
        $tabs = array();
            
        if ( ! isset($SESS->userdata['quick_tabs']) || $SESS->userdata['quick_tabs'] == '')
        {
            return $tabs;
        }
        
        foreach (explode("\n", $SESS->userdata['quick_tabs']) as $row)
        {                
            $x = explode('|', $row);
            
            $title = (isset($x['0'])) ? $x['0'] : '';
            $link  = (isset($x['1'])) ? $x['1'] : '';
            
			$tabs[] = array($title, $link);    
        }
            
        return $tabs;
    }  
    /* END */



    /** -------------------------------------
    /**  Create the "quick add" link
    /** -------------------------------------*/
      
	function generate_quick_tab()
	{
		global $IN, $SESS;
		
        $link  = '';
        $linkt = '';
        if ($IN->GBL('M') != 'tab_manager' AND $IN->GBL('M') != '')
        {
			foreach ($_GET as $key => $val)
			{
				if ($key == 'S')
					continue;
			
				$link .= $key.'--'.$val.'/';
			}
			
			$link = substr($link, 0, -1);
		}
		
		// Does the link already exist as a tab?
		// If so, we'll make the link blank so that the
		// tab manager won't let the user create another tab.
		
		$show_link = TRUE;
				
        if (isset($SESS->userdata['quick_tabs']) AND $SESS->userdata['quick_tabs'] != '')
        {
			$newlink = '|'.str_replace('/', '&', str_replace('--', '=', $link)).'|';
			
			if (strpos($SESS->userdata['quick_tabs'], $newlink))
			{
				$show_link = FALSE;
			}
        }
		
		// We do not normally allow semicolons in GET variables, so we protect it
		// in this rare instance.
		$tablink = ($link != '' AND $show_link == TRUE) ? AMP.'link='.$link.AMP.'linkt='.base64_encode($this->title) : '';
	
		return $tablink;
	}
	/* END */
	
	

    /** -------------------------------------
    /**  Simple version of the Header
    /** -------------------------------------*/

    function simple_header($class ='loginLogo')
    {
        global $LANG, $PREFS;
         
        return
               
        "<div id='topBar'>\n"
        .$this->table('', '', '0', '100%')
        .$this->table_qrow('helpLinks', $this->qdiv($class, $this->nbs(2).APP_NAME.$this->nbs(2).'v '.APP_VER))
        .$this->table_c()
        .$this->div_c()
        ."<div id='simpleHeader'></div>\n";
    }
    /* END */



    /** -------------------------------------
    /**  Equalize Text
    /** -------------------------------------*/

	// This function lets us "equalize" the text length by adding non-breaking spaces
	// before/after each line so that they all match.  This enables the
	// navigation buttons to have the same length.  The function must be passed an 
	// associative array
	
	function equalize_text($text = array())
	{
         $longest = 0;   
        
         foreach ($text as $val)
         {
            $val = strlen($val);
         
            if ($val > $longest)
                $longest = $val;
         }
                
         foreach ($text as $key => $val)
         {
            $i = $longest - strlen($val);
            
            $i = ceil($i/2);
                            
            $val = $this->nbs($i).$val.$this->nbs($i);
                    
            $text[$key] = $val;
         }
	
		return $text;
	}
	/* END */
	

    /** -------------------------------------
    /**  Main control panel navigation
    /** -------------------------------------*/

    function page_navigation()
    {
        global $IN, $DB, $SESS, $LANG, $EXT;
        
        /* -------------------------------------------
        /* 'cp_display_page_navigation' hook.
        /*  - Take control of the Control Panel's top navigation
        /*  - Added 1.5.0
        */
        	$r = $EXT->call_extension('cp_display_page_navigation', $this);
        	if ($EXT->end_script === TRUE) return $r;
        /*
        // -------------------------------------------*/
        
        
		$C = ($IN->GBL('class_override', 'GET') == '') ? $IN->GBL('C') : $IN->GBL('class_override', 'GET') ;        
                 
        // First we'll gather the navigation menu text in the selected language.
                
        $text = array(  'publish'     => $LANG->line('publish'),
                        'edit'        => $LANG->line('edit'),
                        'design'      => $LANG->line('design'),
                        'communicate' => $LANG->line('communicate'),
                        'modules'     => $LANG->line('modules'),
                        'my_account'  => $LANG->line('my_account'),
                        'admin'       => $LANG->line('admin')        
                      );
                           
         
		// Fetch the custom tabs if there are any
		
		$quicktabs = $this->fetch_quicktabs();
         
        // Set access flags
        
        $cells = array(
							'p_lock' => 'can_access_publish',
							'e_lock' => 'can_access_edit',
							'd_lock' => 'can_access_design',
							'c_lock' => 'can_access_comm',
							'm_lock' => 'can_access_modules',
							'a_lock' => 'can_access_admin'
        				);
        
       // Dynamically set the table width based on the number
       // of tabs that will be shown.
       
        $tab_total	= 7 + count($quicktabs); // Total possible tabs
		$width_base	= floor(80/$tab_total); // Width of each tab        
		$width_pad	= 98;

        foreach ($cells as $key => $val)
        {
			if ( ! $this->allowed_group($val))
			{
				$$key = 1;
				$width_pad -= $width_base;
				$tab_total--;
			}
			else
			{
				$$key = 0;
			}
        }
         
        if ($tab_total < 6)
        {
			$width 		= ceil($width_pad/$tab_total);
			$width_pad	= floor(100-$width_pad);
        }
        else
        {
			$width 		= ceil(96/$tab_total);
        	$width_pad	= 0;
        }
                  
		/*
		
		Does a custom tab need to be highlighted?
        Since users can have custom tabs we need to highlight them when the page is
        accessed.  However, when we do, we need to prevent any of the default tabs
        from being highlighted.  Say, for example, that someone creates a tab pointing
        to the Photo Gallery.  When that tab is accessed it needs to be highlighted (obviously)
        but we don't want the MODULES tab to also be highlighted or it'll look funny.
        Since the Photo Gallery is within the MODULES tab it'll hightlight itself automatically.
        So... we'll use a variable called:  $highlight_override
        When set to TRUE, this variable turns off all default tabs.
        The following code blasts thorough the GET variables to see if we have
        a custom tab to show.  If we do, we'll highlight it, and turn off
        all the other tabs.
        */
  
        $highlight_override = FALSE;
        
        $tabs = '';
        $tabct = 1;
        if (count($quicktabs) > 0)
        {
        	foreach ($quicktabs as $val)
        	{
        		$gkey = '';
        		$gval = '';
        		if (strpos($val['1'], '&'))
        		{
        			$x = explode('&', $val['1']);
        			
        			$i = 1;
        			foreach ($x as $v)
        			{ 
        				$z = explode('=', $v);

        				if (isset($_GET[$z['0']]))
        				{
        					if ($_GET[$z['0']] != $z['1'])
        					{
								$gkey = '';
								$gval = '';
								break;
        					}
        					elseif (count($x)+1 == count($_GET))
        					{ 
								$gkey = $z['0'];
								$gval = $z['1'];
							}
        				}
        			}
        		}
        		elseif (strpos($val['1'], '='))
        		{
					$z = explode('=', $v);
				
					if (isset($_GET[$z['0']]))
					{
						$gkey = $z['0'];
						$gval = $z['1']; 
					}        		
        		}
        		
        		$tab_nav_on = FALSE;
       
        		if (isset($_GET[$gkey]) AND $_GET[$gkey] == $gval)
        		{
        			$highlight_override = TRUE;
        			$tab_nav_on = TRUE;
        		}
        		
        		$linktext = ( ! isset($text[$val['0']])) ? $val['0'] : $text[$val['0']];
				$linktext = $this->clean_tab_text($linktext);
				
				$tabid = 'tab'.$tabct;
				$tabct ++;
												
				if ($tab_nav_on == TRUE) 
				{
					$js = ' onClick="navjump(\''.BASE.AMP.$val['1'].'\');"';
					$tabs .= "<td class='navCell' width='".$width."%' ".$js.">";  				
					$div = $this->qdiv('cpNavOn', $linktext);
				}
				else
				{
					$js = ' onClick="navjump(\''.BASE.AMP.$val['1'].'\');"  onMouseover="navTabOn(\''.$tabid.'\');" onMouseout="navTabOff(\''.$tabid.'\');" ';
					$tabs .= "<td class='navCell' width='".$width."%' ".$js.">";  				
					$div = $this->div('cpNavOff', '', $tabid).$linktext.$this->div_c();
				}
						
				$tabs .= $this->anchor(BASE.AMP.$val['1'], $div);
				$tabs .= $this->td_c();				
        	}
        } 
        
        
        $r = '';        
                  
        // Define which nav item to show based on the group 
        // permission settings and render the finalized navigaion  
        
        if ($this->publish_nav == 'click')
        {
        	$r .= '<div id="publishdropmenu" onMouseover="clearhidemenu()"></div>';
        }
        elseif($this->publish_nav == 'none')
        {
        	$r .= '<div id="publishdropmenu"></div>';
        }
        else
        {
        	$r .= '<div id="publishdropmenu" onMouseover="clearhidemenu()" onMouseout="dynamichide(event)"></div>';
        }
        
                 
        $r .= $this->table('', '0', '0', '100%')
             .$this->tr();
		
		if ($this->padding_tabs != 'off')
		{
			$r .= $this->td('navCell');
				 
			if ($this->padding_tabs == 'clear')
			{
				$r .= $this->nbs();
			}
			else
			{
				$r .= $this->div('cpNavOff')
					 .$this->nbs()
					 .$this->div_c();
			}
		
			$r .= $this->td_c();
 		}
            
            
        if ($p_lock == 0)
        {                            	
        	if ($C == 'publish' AND $highlight_override == FALSE) 
        	{
        		if ($this->publish_nav == 'click')
        		{
        			if ($this->empty_menu === TRUE)
        			{
        				$js = ' onclick="navjump(\''.BASE.AMP.'C=publish'.'\');" onmouseout="navTabOff(\'publish\');" onmouseover="navTabOn(\'publish\');"';
        			}
        			else
        			{
        				$js = ' onclick="dropdownmenu(\'publishdropmenu\', this, event, menu, \''.($width * 2).'%\');return false;" onmouseout="navTabOff(\'publish\');" onmouseover="navTabOn(\'publish\');"';
        			}
        		}
        		elseif($this->publish_nav == 'none')
        		{
					$js = ' onClick="navjump(\''.BASE.AMP.'C=publish'.'\');" onMouseover="navTabOn(\'publish\');"';
				}
				else
        		{
					$js = ' onClick="navjump(\''.BASE.AMP.'C=publish'.'\');" onMouseover="dropdownmenu(\'publishdropmenu\', this, event, menu, \''.($width * 2).'%\');navTabOn(\'publish\');"';
				}
				
				$r .= "<td class='navCell' width='".$width."%' ".$js.">";  
        		$div = $this->qdiv('cpNavOn', $this->clean_tab_text($text['publish']));
        	}
        	else
        	{
        		if ($this->publish_nav == 'click')
        		{
        			if ($this->empty_menu === TRUE)
        			{
        				$js = ' onclick="navjump(\''.BASE.AMP.'C=publish'.'\');" onmouseover="navTabOn(\'publish\');" onMouseout="navTabOff(\'publish\');" ';
        			}
        			else
        			{
        				$js = ' onclick="dropdownmenu(\'publishdropmenu\', this, event, menu, \''.($width * 2).'%\');return false;" onmouseover="navTabOn(\'publish\');" onMouseout="navTabOff(\'publish\');" ';
					}
				}
				elseif($this->publish_nav == 'none')
        		{
        			$js = ' onClick="navjump(\''.BASE.AMP.'C=publish'.'\');" onMouseover="navTabOn(\'publish\');" onMouseout="navTabOff(\'publish\');" ';
				}
				else
				{
        			$js = ' onClick="navjump(\''.BASE.AMP.'C=publish'.'\');" onMouseover="dropdownmenu(\'publishdropmenu\', this, event, menu, \''.($width * 2).'%\');navTabOn(\'publish\');" onMouseout="navTabOff(\'publish\');delayhidemenu();" ';
				}
				
				$r .= "<td class='navCell' width='".$width."%' ".$js.">";  
        		$div = $this->div('cpNavOff', '', 'publish').$this->clean_tab_text($text['publish']).$this->div_c();
        	}
        	      
			$r .= $this->anchor(BASE.AMP.'C=publish', $div);
			$r .= $this->td_c();
        }
            
        if ($e_lock == 0)
        {              	
        	if ($C == 'edit' AND $highlight_override == FALSE) 
        	{
				$js = ' onClick="navjump(\''.BASE.AMP.'C=edit'.'\');"'; 
				$r .= "<td class='navCell' width='".$width."%' ".$js.">";  
        		$div = $this->qdiv('cpNavOn', $this->clean_tab_text($text['edit']));
        	}
        	else
        	{
				$js = ' onClick="navjump(\''.BASE.AMP.'C=edit'.'\');" onMouseover="navTabOn(\'edit\');" onMouseout="navTabOff(\'edit\');" ';                
				$r .= "<td class='navCell' width='".$width."%' ".$js.">";  
        		$div = $this->div('cpNavOff', '', 'edit').$this->clean_tab_text($text['edit']).$this->div_c();
        	}
        	        	
			$r .= $this->anchor(BASE.AMP.'C=edit', $div);
			$r .= $this->td_c();
        }
        
        
        // Custom Tabs
		$r .= $tabs;                
            
        if ($d_lock == 0)
        {                           	
        	if ($C == 'templates' AND $highlight_override == FALSE) 
        	{
				$js = ' onClick="navjump(\''.BASE.AMP.'C=templates'.'\');"';
				$r .= "<td class='navCell' width='".$width."%' ".$js.">";  
        		$div = $this->qdiv('cpNavOn', $this->clean_tab_text($text['design']));
        	}
        	else
        	{
				$js = ' onClick="navjump(\''.BASE.AMP.'C=templates'.'\');" onMouseover="navTabOn(\'design\');" onMouseout="navTabOff(\'design\');" ';
				$r .= "<td class='navCell' width='".$width."%' ".$js.">";  
        		$div = $this->div('cpNavOff', '', 'design').$this->clean_tab_text($text['design']).$this->div_c();
        	}
        	        	
			$r .= $this->anchor(BASE.AMP.'C=templates', $div);
			$r .= $this->td_c();
        }
        
        if ($c_lock == 0)
        {                            	
        	if ($C == 'communicate' AND $highlight_override == FALSE) 
        	{
				$js = ' onClick="navjump(\''.BASE.AMP.'C=communicate'.'\');"';
				$r .= "<td class='navCell' width='".$width."%' ".$js.">";  
        		$div = $this->qdiv('cpNavOn', $this->clean_tab_text($text['communicate']));
        	}
        	else
        	{
				$js = ' onClick="navjump(\''.BASE.AMP.'C=communicate'.'\');" onMouseover="navTabOn(\'communicate\');" onMouseout="navTabOff(\'communicate\');" ';
				$r .= "<td class='navCell' width='".$width."%' ".$js.">";  
        		$div = $this->div('cpNavOff', '', 'communicate').$this->clean_tab_text($text['communicate']).$this->div_c();
        	}
        	        	
            $r .= $this->anchor(BASE.AMP.'C=communicate', $div);
			$r .= $this->td_c();
        }
       
        if ($m_lock == 0)
        {                          	
        	if ($C == 'modules' AND $highlight_override == FALSE) 
        	{
				$js = ' onClick="navjump(\''.BASE.AMP.'C=modules'.'\');"';
				$r .= "<td class='navCell' width='".$width."%' ".$js.">";  
        		$div = $this->qdiv('cpNavOn', $this->clean_tab_text($text['modules']));
        	}
        	else
        	{
				$js = ' onClick="navjump(\''.BASE.AMP.'C=modules'.'\');" onMouseover="navTabOn(\'modules\');" onMouseout="navTabOff(\'modules\');" ';
				$r .= "<td class='navCell' width='".$width."%' ".$js.">";  
        		$div = $this->div('cpNavOff', '', 'modules').$this->clean_tab_text($text['modules']).$this->div_c();
        	}
        	        	
            $r .= $this->anchor(BASE.AMP.'C=modules', $div);
			$r .= $this->td_c();
        }
        
        
        // We only want the "MY ACCOUNT" tab highlighted if
        // the profile being viewed belongs to the logged in user
        
		$tab = $this->div('cpNavOff');

        if ($C == 'myaccount')
        {
            $id = ( ! $IN->GBL('id', 'GP')) ? $SESS->userdata('member_id') : $IN->GBL('id', 'GP');
                        
            if ($id != $SESS->userdata('member_id'))
            {
                $tab = $this->div('cpNavOff');
            }
            else
            {
            	if ($highlight_override == FALSE)
                	$tab = $this->div('cpNavOn');
            }
        }
        		
		if ($C == 'myaccount' AND $highlight_override == FALSE) 
		{
			$js = ' onClick="navjump(\''.BASE.AMP.'C=myaccount'.'\');"';
			$r .= "<td class='navCell' width='".$width."%' ".$js.">";  
			$div = $this->qdiv('cpNavOn', $this->clean_tab_text($text['my_account']));
		}
		else
		{
			$js = ' onClick="navjump(\''.BASE.AMP.'C=myaccount'.'\');" onMouseover="navTabOn(\'my_account\');" onMouseout="navTabOff(\'my_account\');" ';
			$r .= "<td class='navCell' width='".$width."%' ".$js.">";  
			$div = $this->div('cpNavOff', '', 'my_account').$this->clean_tab_text($text['my_account']).$this->div_c();
		}
					
		$r .= $this->anchor(BASE.AMP.'C=myaccount', $div);
		$r .= $this->td_c();
        
            
        if ($a_lock == 0)
        { 					
			if ($C == 'admin' AND $highlight_override == FALSE) 
			{
				$js = ' onClick="navjump(\''.BASE.AMP.'C=admin'.'\');"';
				$r .= "<td class='navCell' width='".$width."%' ".$js.">";  
				$div = $this->qdiv('cpNavOn', $this->clean_tab_text($text['admin']));
			}
			else
			{
				$js = ' onClick="navjump(\''.BASE.AMP.'C=admin'.'\');" onMouseover="navTabOn(\'admin\');" onMouseout="navTabOff(\'admin\');" ';
				$r .= "<td class='navCell' width='".$width."%' ".$js.">";  
				$div = $this->div('cpNavOff', '', 'admin').$this->clean_tab_text($text['admin']).$this->div_c();
			}
						
			$r .= $this->anchor(BASE.AMP.'C=admin', $div);
			$r .= $this->td_c();
        }
        
		if ($this->padding_tabs != 'off')
		{
        	$r .= $this->td('navCell', (($width_pad <= 2) ? '': $width_pad.'%'));
				
			if ($this->padding_tabs == 'clear')
			{
				$r .= $this->nbs();
			}
			else
			{
				$r .= $this->div('cpNavOff')
					 .$this->nbs()
					 .$this->div_c();
			}
		
			$r .= $this->td_c();
 		}
        
		$r .= $this->tr_c().
              $this->table_c().
              $this->nl(2);
            
        return $r;
    }
    /* END */


    /** -------------------------------------
    /**  This keeps the quick tab text OK
    /** -------------------------------------*/

	function clean_tab_text($str = '')
	{
		if ($str == '')
			return '';
			
		$str = str_replace(' ', NBS, $str);
		$str = str_replace('"', '&quot;', $str);
		$str = str_replace("'", "&#39;", $str);
		
		return $str;
	}
	/* END */
	

    /** -------------------------------------
    /**  Content
    /** -------------------------------------*/

    function content($padding = FALSE)
    { 
    	$this->secure_hash();
    	
    	if ($padding === TRUE)
    	{
    		$this->body = $this->qdiv('itemWrapperTop', $this->body);
    	}
    
        if ($this->crumbline == FALSE)
        {   
			return NL."<div id='contentNB'>".$this->nl(2).$this->body.$this->nl(2);
        }
        else
        {
 			return NL."<div id='content'>".$this->nl(2).$this->body.$this->nl(2);
 		}
    }
    /* END */
    
    
    /** -------------------------------------
    /**  Secure Hash
    /** -------------------------------------*/

    function secure_hash($str = '')
    { 
    	global $IN, $FNS, $DB, $PREFS;
    	
    	$check = ($str != '') ? $str : $this->body;
    	
    	if ($PREFS->ini('secure_forms') == 'y' && preg_match_all("/<form.*?>/", $check, $matches))  // <?php  fixex BBEdit display bug
    	{
    		$sql = "INSERT INTO exp_security_hashes (date, ip_address, hash) VALUES ";
    		
    		for($i=0, $s=sizeof($matches['0']); $i < $s; ++$i)
    		{
    			$hash = $FNS->random('encrypt');
    			$sql .= "(UNIX_TIMESTAMP(), '".$IN->IP."', '".$hash."'),";
    			
    			$check = str_replace($matches['0'][$i], $matches['0'][$i].NL.$this->input_hidden('XID', $hash), $check);
    		}
    		
    		$check = str_replace('{XID_SECURE_HASH}', $hash, $check);
    		
    		$DB->query(substr($sql,0,-1));
    	}
    	
    	if ($str != '')
    	{
    		return $check;
    	}
    	else
    	{
    		$this->body = $check;
    	}
    }
    /* END */


    /** -------------------------------------
    /**  Crumb Builder
    /** -------------------------------------*/
    
    // This function lets us build crumbs.  It can receive either a string or an array.
    // If you pass it an array  the key must be the name of the crumb and the value 
    // must be the URL where the crumb points.  If the value is blank only the text will appear.
    // EXAMPLE:
    /*
    	$crumbs = array(	
    						'Forum'			=> BASE.AMP.'C=modules'.AMP.'M=forum',
    						'Forum Manager	=> BASE.AMP.'C=modules'.AMP.'M=forum'.AMP.'P=forum_manager',
    						'Categories'	=> ''
						);
    
    
    	$DSP->crumb = $DSP->build_crumb($crumbs);
    	
    	The above would produce:
    	
    	<a href="bla...">Forum</a> > <a href="bla..">Forum Manager</a> > Cateogories
    */

    function build_crumb($crumbs = '')
    { 
    	if ($crumbs == '')
    	{
    		return '';
    	}
    
    	if ( ! is_array($crumbs))
    	{
			return $this->crumb_item($crumbs);
    	}
    	    
		if (count($crumbs) == 0)
			return '';
		
		$str = '';
		
		foreach ($crumbs as $key => $val)
		{
			if ($val == '')
			{
				$str .= $this->crumb_item($key);
			}
			else
			{
				$str .= $this->crumb_item($this->anchor($val, $key));
			}
		}	

		return $str;
    }
    /* END */
    
     
    /** -------------------------------------
    /**  Breadcrumb
    /** -------------------------------------*/

    function breadcrumb()
    {
        global $IN, $PREFS, $SESS, $LANG;
        
        if ($this->show_crumb == FALSE)
        {   
            return;
        }
        
        $link = '';
        
        if ($C = $IN->GBL('C'))
        {
            $link .= $this->anchor(BASE, $LANG->line('main_menu'));
        }
        
        if ($IN->GBL('class_override', 'GET') != '')
        {
        	$C = $IN->GBL('class_override', 'GET') ;
        }
        
        // If the "M" variable exists in the GET query string, turn 
        // the variable into the next segment of the breadcrumb
        
        if ($IN->GBL('M') AND $this->crumb_ov == FALSE)
        {            
            // The $special variable let's us add additional data to the query string
            // There are a few occasions where this is necessary
            
            $special = '';
                        
            if ($IN->GBL('weblog_id', 'POST'))
            {
                $special = AMP.'weblog_id='.$IN->GBL('weblog_id');
            }
            
            // Build the link
         
            $name = $C;
            
            if ($C == 'myaccount')
            {
                if ($id = $IN->GBL('id', 'GP'))
                {
                    if ($id != $SESS->userdata('member_id'))
                    {
                        $name = $LANG->line('user_account');
                        
                        $special = AMP.'id='.$id;
                    }
                    else
                    {
                        $name = $LANG->line('my_account');
                    }
                }
            }
         
            $link .= $this->nbs(2)."&#8250;".$this->nbs(2).$this->anchor(BASE.AMP.'C='.$C.$special, ucfirst($name));        
        }
        
        // $this->crumb indicates the page being currently viewed.
        // It does not need to be a link.
        
        if ($this->crumb != '')
        {
            $link .= $this->nbs(2)."&#8250;".$this->nbs(2).$this->crumb;
        }

        // This is the right side of the breadcrumb area.

        $data = ($this->rcrumb == '') ? "&nbsp;" : $this->rcrumb;
            
        if ($data == 'OFF')
        {
            $link = '&nbsp;';
            $data = '&nbsp;';
        }
        
        if ($link == '')
        	$link = '&nbsp;';
        
        // Define the breadcrump CSS.  On all but the PUBLISH page we use the
        // version of the breadcrumb that has a bottom border
        
        if ($this->crumbline == TRUE)        
        {
            $ret = "<div id='breadcrumb'>";
        }
        else
        {
            $ret = "<div id='breadcrumbNoLine'>";
        }
                        
        $ret .= $this->table('', '0', '0', '100%');
        $ret .= $this->tr();
        $ret .= $this->table_qcell('crumbPad', $this->span('crumblinks').$link.$this->span_c());
        $ret .= $this->table_qcell($this->rcrumb_css, $data, '270px', 'bottom', 'right');
        $ret .= $this->tr_c();
        $ret .= $this->table_c();
        $ret .= $this->div_c();
        
        return $ret;
    }
    /* END */

 
    /** ---------------------------------------
    /**  Right Side Crumb
    /** ---------------------------------------*/
 
	function right_crumb($title, $url = '', $extra = '', $pop = FALSE)
	{
		$nj = '';
		if ($url != '')
		{
			if ($pop === FALSE)
			{
				$nj = ' onclick="navjump(\''.$url.'\');" ';
			}
			else
			{
				$nj = ' onclick="navjump(\''.$url.'\', \'true\');" ';
			}		
		}

		$js = $nj.$extra.' onmouseover="navCrumbOn();" onMouseout="navCrumbOff();" ';
		
		if ($url != '')
		{
			$this->rcrumb = $this->anchor($url, '<div class="crumblinksR" id="rcrumb" '.$js.'>'.$title.'</div>');
		}
		else
		{
			$this->rcrumb = '<div class="crumblinksR" id="rcrumb" '.$js.'>'.$title.'</div>';
		}
	}
	/* END */
	
 
    /** ---------------------------------------
    /**  Adds "breadcrum" formatting to an item
    /** ---------------------------------------*/
 
    function crumb_item($item)
    {
        return $this->nbs(2)."&#8250;".$this->nbs(2).$item;
    } 
 

    /** -------------------------------------
    /**  Required field indicator
    /** -------------------------------------*/

    function required($blurb = '')
    {
        global $LANG;
        
        if ($blurb == 1)
        {
            $blurb = "<span class='default'>".$this->nbs(2).$LANG->line('required_fields').'</span>';
        }
        elseif($blurb != '')
        {
            $blurb = "<span class='default'>".$this->nbs(2).$blurb.'</span>';
        }
    
        return "<span class='alert'>*</span>".$blurb.NL;
    }
    /* END */


    /** -------------------------------------
    /**  Content closing </div> tag
    /** -------------------------------------*/

    function content_close()
    {    
        return "</div>".NL;
    }
    /* END */



    /** -------------------------------------
    /**  Copyright
    /** -------------------------------------*/

    function copyright()
    {
        global $LANG, $PREFS, $FNS, $DB;
             
		$qm = ($PREFS->ini('force_query_string') == 'y') ? '' : '?';

		$logo = '<img src="'.PATH_CP_IMG.'ee_logo_sm.gif" border="0"  width="20" height="12" alt="ExpressionEngine" />';

		$core = '';
		$buyit = '';
		if ( ! file_exists(PATH_MOD.'member/mod.member'.EXT))
		{
			$qm = ($PREFS->ini('force_query_string') == 'y') ? '' : '?';		
		
			$core = ' Core';
			$buyit = 'Love EE Core? Consider '.$this->anchor($FNS->fetch_site_index().$qm.'URL=http://www.pmachine.com/', 'buying').' a personal license!<br />';
		}

        return

        "<div class='copyright'>". $logo.$this->nl(2).$this->br().$this->nl().
         $this->anchor($FNS->fetch_site_index().$qm.'URL=http://www.pmachine.com/', APP_NAME.$core." ".APP_VER)." - &#169; ".$LANG->line('copyright')." 2003 - 2007 - pMachine, Inc.".BR.NL.
         $buyit.
         str_replace("%x", "{cp:elapsed_time}", $LANG->line('page_rendered')).$this->nbs(3).
         str_replace("%x", $DB->q_count, $LANG->line('queries_executed')).$this->br().
		 $LANG->line('build').$this->nbs(2).APP_BUILD.$this->nl(2).
        "</div>".NL;
    }
    /* END */
        


    /** -------------------------------------
    /**  HTML Footer
    /** -------------------------------------*/

    function html_footer()
    {
        return NL.'</body>'.NL.'</html>';
    }
    /* END */


    /** -------------------------------------
    /**  Error Message
    /** -------------------------------------*/

    function error_message($message = "", $n = 1)
    {
        global $LANG;
        
        $this->title = $LANG->line('error');
        
        if (is_array($message))
        {
			$message = implode(BR, $message);
        }
        
        $this->crumbline = FALSE;
                
        $this->body = $this->qdiv('alertHeadingCenter', $LANG->line('error'))
				.$this->div('box')
				.$this->div('defaultCenter')
				.$this->qdiv('defaultBold', $message);
                    
       if ($n != 0)
           $this->body .= BR.$this->nl(2)."<a href='javascript:history.go(-".$n.")' style='text-transform:uppercase;'>&#171; <b>".$LANG->line('back')."</b></a>";
            
        $this->body .= BR.BR.$this->div_c().$this->div_c();            
    }
    /* END */


    /** -------------------------------------
    /**  Unauthorized access message
    /** -------------------------------------*/

    function no_access_message($message = '')
    {
        global $LANG;
        
        $this->title = $LANG->line('unauthorized');
        
        $msg = ($message == '') ? $LANG->line('unauthorized_access') : $message;
        
        $this->body = $this->qdiv('highlight', BR.$msg);    
    }
    /* END */



    /** -------------------------------------
    /**  Global Javascript
    /** -------------------------------------*/

	function _global_javascript()
	{
		ob_start();
		?>
			<script type="text/javascript"> 
			<!--
			
			var browser = "Unknown";
			var version = "Unknown";
			var OS 		= "Unknown";
		
			var info = navigator.userAgent.toLowerCase();
							
			var browsers = new Array();
				browsers['safari']		= "Safari";
				browsers['omniweb']		= "OmniWeb";
				browsers['opera']		= "Opera"; 
				browsers['webtv']		= "WebTV"; 
				browsers['icab']		= "iCab";
				browsers['konqueror']	= "Konqueror";
				browsers['msie']		= "IE";
				browsers['mozilla']		= "Mozilla";
				
			for (b in browsers)
			{
				pos = info.indexOf(b) + 1;
				if (pos != false)
				{
					browser = browsers[b];
					version = info.charAt(pos + b.length);
					break;
				}	
			}
			
			var systems = new Array();
				systems['linux']	= "Linux";
				systems['x11'] 		= "Unix";
				systems['mac'] 		= "Mac";
				systems['win'] 		= "Win";

		
			for (s in systems)
			{
				pos = info.indexOf(s) + 1;
				if (pos != false)
				{
					OS = systems[s];
					break;
				}	
			}
			
			
			function navCrumbOn()
			{
				if (document.getElementById('rcrumb').className == 'crumblinksR')
				{
					document.getElementById('rcrumb').className = 'crumblinksRHover';
				}
			}
			
			function navCrumbOff()
			{
				if (document.getElementById('rcrumb').className == 'crumblinksRHover')
				{
					document.getElementById('rcrumb').className = 'crumblinksR';
				}
			}
	
			function navTabOn(link, idoff, idhover)
			{
				if ( ! idoff)
					idoff = 'cpNavOff';
					
				if ( ! idhover)
					idhover = 'cpNavHover';
			
				if (document.getElementById(link))
				{
					if (document.getElementById(link).className == idoff)
					{
						document.getElementById(link).className = idhover;
					}
				}
			}
		
			function navTabOff(link, idoff, idhover)
			{  
				if ( ! idoff)
					idoff = 'cpNavOff';
					
				if ( ! idhover)
					idhover = 'cpNavHover';		
			
				if (document.getElementById(link).className == idhover)
				{
					document.getElementById(link).className = idoff;
				}
			}
			
			function navjump(where, pop)
			{			
				if (browser != 'IE')
					return false;
			
				if (pop == 'true')
				{
					window.open(where, '_blank');
				}
				else
				{
					window.location=where;
				}
			}			
			//-->
			</script>
			
			<!--[if lt IE 7]>
			<script language="JavaScript">
			/*
			/* Fix for PNG alpha transparency for Internet Explorer
			/* Solution courtesy Bob Osola
			/* http://homepage.ntlworld.com/bobosola/index.htm
			*/
			function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
			{
			   var arVersion = navigator.appVersion.split("MSIE")
			   var version = parseFloat(arVersion[1])
			   if ((version >= 5.5) && (document.body.filters)) 
			   {
			      for(var i=0; i<document.images.length; i++)
			      {
			         var img = document.images[i]
			         var imgName = img.src.toUpperCase()
			         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
			         {
			            var imgID = (img.id) ? "id='" + img.id + "' " : ""
			            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
			            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
			            var imgStyle = "display:inline-block;" + img.style.cssText 
			            if (img.align == "left") imgStyle = "float:left;" + imgStyle
			            if (img.align == "right") imgStyle = "float:right;" + imgStyle
			            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
			            var strNewHTML = "<span " + imgID + imgClass + imgTitle
			            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
			            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
			            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
			            img.outerHTML = strNewHTML
			            i = i-1
			         }
			      }
			   }    
			}
			window.attachEvent("onload", correctPNG);
			</script>
			<![endif]-->
		<?php
		
		$out = ob_get_contents();
		ob_end_clean();
		return $out;
	}
	/* END */
	

    /** -------------------------------------
    /**  Place-holder menu JS
    /** -------------------------------------*/
    
    // Since we only show the PUBLISH drop-down menu if there are
    // multiple blogs (and thus, only generate the javascript for the menu), 
    // this keeps the links from triggering errors.  It's a placebo 
    // js function

	function _empty_menu_js()
	{
		$this->empty_menu = TRUE;
	
		ob_start();
		?>
			<script type="text/javascript"> 
			<!--
			var menu = new Array();
			function dropdownmenu(){
				return false;
			}
			
			function delayhidemenu(){
				return false;
			}
			
			//-->
			</script>
			
		<?php
		
		$out = ob_get_contents();
		ob_end_clean();
		return $out;
	}
	/* END */
	

    /** -------------------------------------
    /**  Javascript list of blogs for publish menu
    /** -------------------------------------*/

	function _fetch_publish_menu()
	{
		global $IN, $DB, $FNS, $SESS;
		
		if ( ! $this->allowed_group('can_access_publish'))
		{
			return $this->_empty_menu_js();
		}

		if ($SESS->userdata['group_id'] == 1) 
		{ 
			$sql = "SELECT weblog_id, blog_title FROM exp_weblogs WHERE is_user_blog = 'n' ORDER BY blog_title";
		}
		else
		{
			$allowed_blogs = $FNS->fetch_assigned_weblogs();
						
			if (count($allowed_blogs) == 0)
			{
				return $this->_empty_menu_js();
			}
		
			// Build query
			
			$sql = "SELECT weblog_id, blog_title FROM exp_weblogs WHERE weblog_id IN (";
		
			foreach ($allowed_blogs as $val)
			{
				$sql .= "'".$val."',"; 
			}
			
			$sql = substr($sql, 0, -1).') ORDER BY blog_title';
		}
		
		
		$query = $DB->query($sql);
		
		if ($query->num_rows < 2)
		{
			return $this->_empty_menu_js();
		}
	
		$links = array();
	
	
		foreach ($query->result as $row)
		{ 
			$row['blog_title'] = str_replace("'", "&#39;", $row['blog_title']);
		
			$links[] = '<a href="'.BASE.AMP.'C=publish'.AMP.'M=entry_form'.AMP.'weblog_id='.$row['weblog_id'].'">'.$row['blog_title']."<\/a>";
		}
		
		if (count($links) < 1)
		{
			return $this->_empty_menu_js();
		}
		
		ob_start();
		?>
			<script type="text/javascript"> 
			<!--
						
			/***********************************************************
			* AnyLink Drop Down Menu- Dynamic Drive (www.dynamicdrive.com)
			* Visit http://www.dynamicdrive.com/ for full source code
			*************************************************************/
			
			var menu = new Array();
			<?php
				
				$i = 0;
				foreach ($links as $val)
				{
					echo "menu[".$i."] = '<div class=\"publishdropmenuinner\">".$val."<\/div>';\n";
					$i++;
				}
			
			?>			
			var ie4=document.all
			var ns6=document.getElementById&&!document.all			
			
			function getposOffset(what, offsettype)
			{
				var totaloffset = (offsettype == "left") ? what.offsetLeft : what.offsetTop;
				var parentEl = what.offsetParent;
				
				while (parentEl!=null)
				{
					totaloffset = (offsettype=="left") ? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
					parentEl = parentEl.offsetParent;
				}
				return totaloffset;
			}
			
			function showhide(obj, e, visible, hidden, menuwidth)
			{
				if (ie4||ns6)
					dropmenuobj.style.left = dropmenuobj.style.top='-500px';
					
				if (menuwidth != "")
				{
					dropmenuobj.widthobj = dropmenuobj.style;
					dropmenuobj.widthobj.width = menuwidth;
				}
					
				if (e.type=="click" && obj.visibility!=visible || e.type=="mouseover")
					obj.visibility=visible
				else if (e.type=="click")
					obj.visibility=hidden
			}
			
			function iecompattest()
			{
				return (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
			}
			
			function clearbrowseredge(obj, whichedge)
			{
				var edgeoffset=0
				
				if (whichedge=="rightedge")
				{
					var windowedge=ie4 && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
					dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
					
					if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
						edgeoffset=dropmenuobj.contentmeasure-obj.offsetWidth
				}
				else
				{
					var topedge=ie4 && !window.opera? iecompattest().scrollTop : window.pageYOffset
					var windowedge=ie4 && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
					dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
					
					if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure)
					{ 
						//move up?
						edgeoffset=dropmenuobj.contentmeasure+obj.offsetHeight
						
						if ((dropmenuobj.y-topedge)<dropmenuobj.contentmeasure) //up no good either?
							edgeoffset=dropmenuobj.y+obj.offsetHeight-topedge
					}
				}
				
				return edgeoffset
			}
			
			function populatemenu(what)
			{
				if (ie4||ns6)
					dropmenuobj.innerHTML=what.join("");
			}
			
			function dropdownmenu(cssid, obj, e, menucontents, menuwidth)
			{
				if (OS == 'Mac' && browser == 'IE')
				{
					return false;
				}
			
				if (window.event) 
					event.cancelBubble=true;
				else if (e.stopPropagation) 
					e.stopPropagation();
				
				clearhidemenu();
				
				dropmenuobj = document.getElementById ? document.getElementById(cssid) : cssid;
				
				populatemenu(menucontents);
				
				if (ie4||ns6)
				{
					showhide(dropmenuobj.style, e, "visible", "hidden", menuwidth);
					
					dropmenuobj.x = getposOffset(obj, "left") - 1;
					dropmenuobj.y = getposOffset(obj, "top");
					dropmenuobj.style.left = dropmenuobj.x - clearbrowseredge(obj, "rightedge")+"px";
					dropmenuobj.style.top = dropmenuobj.y - clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px";
				}
				
				return clickreturnvalue();
			}
			
			function clickreturnvalue()
			{
				return true;
			}
			
			function contains_ns6(a, b) 
			{
				while (b.parentNode)
					if ((b = b.parentNode) == a)
						return true;
				return false;
			}
			
			function dynamichide(e)
			{
				if (ie4&&!dropmenuobj.contains(e.toElement))
					delayhidemenu();
				else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
					delayhidemenu();
			}
			
			function hidemenu(e)
			{
				if (typeof dropmenuobj!="undefined")
				{
					if (ie4||ns6)
						dropmenuobj.style.visibility="hidden";
				}
			}
			
			function delayhidemenu()
			{
				if (ie4||ns6)
					delayhide=setTimeout("hidemenu()", 200);
			}
			
			function clearhidemenu()
			{
				if (typeof delayhide!="undefined")
					clearTimeout(delayhide);
			}
			
			//-->
			</script>
			
		<?php
		
		$out = ob_get_contents();
		ob_end_clean(); 
		
		return NL.NL.$out.NL.NL;
	}
	/* END */


    /** -------------------------------------
    /**  Paginate 
    /** -------------------------------------*/

    function pager($base_url = '', $total_count = '', $per_page = '', $cur_page = '', $qstr_var = '')
    {
        global $LANG;
        
        // Instantiate the "paginate" class.
  
		if ( ! class_exists('Paginate'))
		{
        	require PATH_CORE.'core.paginate'.EXT;
        }
        
        $PGR = new Paginate();
        
        $PGR->base_url     = $base_url;
        $PGR->total_count  = $total_count;
        $PGR->per_page     = $per_page;
        $PGR->cur_page     = $cur_page;
        $PGR->qstr_var     = $qstr_var;
        
        return $PGR->show_links();
    }
    /* END */


    /** -------------------------------------
    /**  Delete Confirmation Wrapper
    /** -------------------------------------*/

	// Creates a standardized confirmation message used whenever
	// something needs to be deleted.  The prototype for this form is:
	/*
		$r = $DSP->delete_confirmation(
										array(
												'url'		=> 'C=modules'.AMP.'P=delete_module_confirm',
												'heading'	=> 'delete_module_heading',
												'message'	=> 'delete_module_message,
												'item'		=> $module_name,
												'extra'		=> '',
												'hidden'	=> array('module_id' => $module_id)
											)
										);	
	
	*/
	
	function delete_confirmation($data = array())
	{
		global $LANG;
		
		$vals = array('url', 'heading', 'message', 'item', 'hidden', 'extra');
		
		foreach ($vals as $val)
		{
			if ( ! isset($data[$val]))
			{
				$data[$val] = '';
			}
		}
		
		$r = $this->form_open(array('action' => $data['url']));
		
		if (is_array($data['hidden']))
		{
			foreach ($data['hidden'] as $key => $val)
			{
				$r .= $this->input_hidden($key, $val);
			}
		}
			
		$this->crumbline = FALSE;
		
		$r	.= 	 $this->qdiv('alertHeading', $LANG->line($data['heading']))
				.$this->div('box')
				.$this->qdiv('itemWrapper', '<b>'.$LANG->line($data['message']).'</b>')			
				.$this->qdiv('itemWrapper', $this->qdiv('highlight_alt', $data['item']));
		
		if ($data['extra'] != '')
		{
			$r .= $this->qdiv('itemWrapper', '<b>'.$LANG->line($data['extra']).'</b>');
		}
				
		$r .=	 $this->qdiv('itemWrapper', $this->qdiv('alert', $LANG->line('action_can_not_be_undone')))
				.$this->qdiv('itemWrapperTop', $this->input_submit($LANG->line('delete')))
				.$this->div_c()
				.$this->form_close();
	
		return $r;
	}
	/* END */
	

    /** -------------------------------------
    /**  Div
    /** -------------------------------------*/

    function div($style='default', $align = '', $id = '', $name = '', $extra='')
    {
        if ($align != '')
            $align = " align='{$align}'";
        if ($id != '')
        	$id = " id='{$id}' ";
        if ($name != '')
        	$name = " name='{$name}' ";
        	
        $extra = ' '.trim($extra);
    
        return NL."<div class='{$style}'{$id}{$name}{$align}{$extra}>".NL;
    }
    /* END */


    /** -------------------------------------
    /**  Div close
    /** -------------------------------------*/

    function div_c()
    {
        return NL."</div>".NL;
    }
    /* END */


    /** -------------------------------------
    /**  Quick div
    /** -------------------------------------*/

    function qdiv($style='', $data = '', $id = '', $extra = '')
    {
        if ($style == '')
            $style = 'default';
        if ($id != '')
        	$id = " id='{$id}' ";
        	
        $extra = ' '.trim($extra);
    
        return NL."<div class='{$style}'{$id}{$extra}>".$data.'</div>'.NL;
    }
    /* END */



    /** -------------------------------------
    /**  Span
    /** -------------------------------------*/

    function span($style='default')
    {    
        return "<span class='{$style}'>".NL;
    }
    /* END */


    /** -------------------------------------
    /**  Span close
    /** -------------------------------------*/

    function span_c($style='default')
    {
        return NL."</span>".NL;
    }
    /* END */


    /** -------------------------------------
    /**  Quick span
    /** -------------------------------------*/

    function qspan($style='', $data = '', $id = '')
    {
        if ($style == '')
            $style = 'default';
        if ($id != '')
        	$id = " name = '{$id}' id='{$id}' ";
    
        return NL."<span class='{$style}'{$id}>".$data.'</span>'.NL;
    }
    /* END */


    /** -------------------------------------
    /**  Heading
    /** -------------------------------------*/

    function heading($data = '', $h = '1')
    {
        return NL."<h".$h.">".$data."</h".$h.">".NL;
    }
    /* END */


    /** -------------------------------------------
    /**  Anchor Tag
    /** -------------------------------------------*/
    
    function anchor($url, $name = "", $extra = '', $pop = FALSE)
    {
        if ($name == "" || $url == "")
            return false;
            
        if ($pop != FALSE)
        {
            $pop = " target=\"_blank\"";
        }
        
        $url .= $this->url_append;
    
        return "<a href='{$url}' ".$extra.$pop.">$name</a>";
    }
    /* END */
    

    /** -------------------------------------------
    /**  Anchor - pop-up version
    /** -------------------------------------------*/
    
    function anchorpop($url, $name, $width='500', $height='480')
    {    
        return "<a href='javascript:nullo();' onclick=\"window.open('{$url}', '_blank', 'width={$width},height={$height},scrollbars=yes,status=yes,screenx=0,screeny=0,resizable=yes'); return false;\">$name</a>";
    }
    /* END */
    
    
    /** -------------------------------------------
    /**  Anchor - pop-up version - full page
    /** -------------------------------------------*/
    
    function pagepop($url, $name)
    {    
        return "<a href='#' onclick=\"window.open('{$url}', '_blank');\">$name</a>";
    }
    /* END */

    
    /** -------------------------------------------
    /**  Mailto Tag
    /** -------------------------------------------*/
    
    function mailto($email, $name = "")
    {
        if ($name == "") $name = $email;

        return "<a href='mailto:{$email}'>$name</a>";
    }
    /* END */


    /** -------------------------------------------
    /**  <br /> Tags
    /** -------------------------------------------*/
    
    function br($num = 1)
    {
        return str_repeat("<br />\n", $num);
    }
    /* END */
    
    
    /** -------------------------------------------
    /**  "quick" <br /> tag with <div>
    /** -------------------------------------------*/
    
    function qbr($num = 1)
    {
        return NL.'<div>'.str_repeat("<br />\n", $num).'</div>'.NL;
    }
    /* END */


    /** -------------------------------------------
    /**  Item group
    /** -------------------------------------------*/
    
    function itemgroup($top = '', $bottom = '')
    {
        return $this->div('itemWrapper').
               $this->qdiv('itemTitle', $top).
               $bottom.
               $this->div_c();
    }
    /* END */


    /** -------------------------------------------
    /**  Newline characters
    /** -------------------------------------------*/
    
    function nl($num = 1)
    {
        return str_repeat("\n", $num);
    }
    /* END */

    
    /** -------------------------------------------
    /**  &nbsp; entity
    /** -------------------------------------------*/
    
    function nbs($num = 1)
    {
        return str_repeat("&nbsp;", $num);    
    }
    /* END */
    

    /** -------------------------------------------
    /**  Table open
    /** -------------------------------------------*/
        
    function table_open($props = array())
    {
		$str = '';
		
		foreach ($props as $key => $val)
		{
			if ($key == 'width')
			{
				$str .= " style='width:{$val};' ";
			}
			else
			{
				$str .= " {$key}='{$val}' ";
			}
		}
		
		$required = array('cellspacing' => '0', 'cellpadding' => '0', 'border' => '0');

		foreach ($required as $key => $val)
		{
			if ( ! isset($props[$key]))
			{
				$str .= " {$key}='{$val}' ";
			}
		}
        
        return "<table{$str}>".NL;
    }
    /* END */


    /** -------------------------------------------
    /**  Table Row
    /** -------------------------------------------*/
    
    function table_row($array = array())
    {
		$params		= '';
		$content	= '';		
		$end_row 	= FALSE; 
	
		$str = "<tr>".NL;
    
		foreach($array as $key => $val)
		{	
			if (is_array($val))
			{
				$params		= '';
				$content	= '';		
			
				foreach($val as $k => $v)
				{				
					if ($k == 'width')
					{
						$params .= " style='width:{$v};'";
					}
					else
					{
						if ($k == 'text')
						{
							$content = $v;
						}
						else
						{
							$params .= " {$k}='{$v}'";
						}
					}
				}

				$str .= "<td".$params.">";
				$str .= $content;
				$str .= "</td>".NL;				
			}
			else
			{ 
				$end_row = TRUE;
			
				if ($key == 'width')
				{
					$params .= " style='width:{$val};'";
				}
				else
				{
					if ($key == 'text')
					{
						$content .= $val;
					}
					else
					{
						$params .= " {$key}='{$val}'";
					}
				}
			}			
		}
		
		if ($end_row == TRUE)
		{
			$str .= "<td".$params.">";
			$str .= $content;
			$str .= "</td>".NL;				
		}
		
		$str .= "</tr>".NL;

		return $str;
    }
    /* END */


    /** -------------------------------------------
    /**  Table close
    /** -------------------------------------------*/
    
    function table_close($padding = FALSE)
    {
    	$r = '</table>'.NL;
        	
    	if ($padding !== FALSE)
    	{
			$r .= $this->qdiv('defaultSmall', $padding);
    	}
    
        return $r;
    }
    /* END */


    /** -------------------------------------------
    /**  Form declaration - new version
    /** -------------------------------------------*/
    
    /*	EXAMPLE:
    
    	The first parameter is an array containing the "action" and any other items that
    	are desired in the form opening.  The second optional parameter is an array of hidden fields
    
		$r = $DSP->form_open(	
								array(
										'action'	=> 'C=modules'.AMP.'M=forum', 
										'method'	=> 'post',
										'name'		=> 'entryform',
										'id'		=> 'entryform'
									 ),
								array(
										'member_id' => $mod_forum_id,
										'status'	=> $status
									)
							 );
    
    	The above code will produce:
    	
    	<form action="C=modules&M=forum" method="post" name="entryform" id="entryform" />
    	<input type="hidden" name="member_id" value="23" />
    	<input type="hidden" name="status" value="open" />
    	
    	Notes:  
    			The method in the first parameter is not required.  It ommited it'll be set to "post".
    			
    			If the first parameter does not contain an array it is assumed that it contains
    			the "action" and will be treated as such.
    */
    
    function form_open($data = '', $hidden = array())
    {
    	global $REGX;
    	
    	if ( ! is_array($data))
    	{
    		$data = array('action' => $data);
    	}
    	
		if ( ! isset($data['action']))
		{
			$data['action'] = '';
		}
		
    	if ( ! isset($data['method']))
    	{
    		$data['method'] = 'post';
    	}
		
    	$str = '';
    	foreach ($data as $key => $val)
    	{
    		if ($key == 'action')
    		{
    			$str .= " {$key}='".BASE.AMP.$val.$this->url_append."'";
    		}
    		else
    		{
				$str .= " {$key}='{$val}'";  
			}
    	}
                
        $form = NL."<form{$str}>".NL;
        
        if (count($hidden > 0))
        {
        	foreach ($hidden as $key => $val)
        	{
        		$form .= "<div class='hidden'><input type='hidden' name='{$key}' value='".$REGX->form_prep($val)."' /></div>".NL;
        	}
        }
        
        return $form;
    }
    /* END */
    

    /** -----------------------------------
    /**  Form close
    /** -----------------------------------*/
    
    function form_close()
    {
        return "</form>".NL;
    }
    /* END */

    

    /** -------------------------------------------
    /**  Input - hidden
    /** -------------------------------------------*/
    
    function input_hidden($name, $value = '')
    {
        global $REGX;
        
        if ( ! is_array($name))
        {
			return "<div class='hidden'><input type='hidden' name='{$name}' value='".$REGX->form_prep($value)."' /></div>".NL;
    	}
    
    	$form = '';
    	
		foreach ($name as $key => $val)
		{
			$form .= "<div class='hidden'><input type='hidden' name='{$key}' value='".$REGX->form_prep($val)."' /></div>".NL;
		}
    	
    	return $form;
    }
    /* END */


    
    /** -------------------------------------------
    /**  Input - text
    /** -------------------------------------------*/
    
    function input_text($name, $value='', $size = '90', $maxl = '100', $style='input', $width='100%', $extra = '', $convert = FALSE, $text_direction = 'ltr')
    {
        global $REGX;
        
        $text_direction = ($text_direction == 'rtl') ? " dir='rtl' " : " dir='ltr' ";
        
        $value = ($convert == FALSE) ? $REGX->form_prep($value) : $REGX->form_prep($REGX->entities_to_ascii($value, FALSE));
        
        $id = (stristr($extra, 'id=')) ? '' : "id='".str_replace(array('[',']'), '', $name)."'";
                    
        return "<input {$text_direction} style='width:{$width}' type='text' name='{$name}' {$id} value='".$value."' size='{$size}' maxlength='{$maxl}' class='{$style}' $extra />".NL;
    }
    /* END */
 
    /** -------------------------------------------
    /**  Input - password
    /** -------------------------------------------*/
    
    function input_pass($name, $value='', $size = '20', $maxl = '100', $style='input', $width='100%', $text_direction = 'ltr')
    {        
    	$text_direction = ($text_direction == 'rtl') ? " dir='rtl' " : " dir='ltr' ";
    	
    	$id = "id='".str_replace(array('[',']'), '', $name)."'";
    	
        return "<input {$text_direction} style='width:{$width}' type='password' name='{$name}' {$id} value='{$value}' size='{$size}' maxlength='{$maxl}' class='{$style}' />".NL;
    }
    /* END */


    /** -------------------------------------------
    /**  Input - textarea
    /** -------------------------------------------*/
    
    function input_textarea($name, $value='', $rows = '20', $style='textarea', $width='100%', $extra = '', $convert = FALSE, $text_direction = 'ltr')
    {
        global $REGX;
        
        $text_direction = ($text_direction == 'rtl') ? " dir='rtl' " : " dir='ltr' ";

        $value = ($convert == FALSE) ? $REGX->form_prep($value) : $REGX->form_prep($REGX->entities_to_ascii($value, FALSE));
        
        $id = (stristr($extra, 'id=')) ? '' : "id='".str_replace(array('[',']'), '', $name)."'";

        return "<textarea {$text_direction} style='width:{$width};' name='{$name}' {$id} cols='90' rows='{$rows}' class='{$style}' $extra>".$value."</textarea>".NL;
    }
    /* END */


    /** -------------------------------------------
    /**  Input - pulldown - header
    /** -------------------------------------------*/
    
    function input_select_header($name, $multi = '', $size=3, $width='')
    {
        if ($multi != '')
            $multi = " size='".$size."' multiple='multiple'";
            
        if ($multi == '')
        {
            $class = 'select';
        }
        else
        {
            $class = 'multiselect';  
            
            if ($width == '')
            {
            	$width = '45%';
            }
    	}
    	
    	if ($width != '')
    	{
    		$width = "style='width:".$width."'";
    	}
    	
        return NL."<select name='{$name}' class='{$class}'{$multi} {$width}>".NL;
    }
    /* END */

    /** -------------------------------------------
    /**  Input - pulldown 
    /** -------------------------------------------*/
    
    function input_select_option($value, $item, $selected = '', $extra='')
    {    
        global $REGX;        
    
        $selected = ($selected != '') ? " selected='selected'" : '';
        $extra    = ($extra != '') ? " ".trim($extra)." " : '';
    
        return "<option value='".$value."'".$selected.$extra.">".$item."</option>".NL;
    }
    /* END */


    /** -------------------------------------------
    /**  Input - pulldown - footer
    /** -------------------------------------------*/
    
    function input_select_footer()
    {    
        return "</select>".NL;
    }
    /* END */


    /** -------------------------------------------
    /**  Input - checkbox
    /** -------------------------------------------*/
    
    function input_checkbox($name, $value='', $checked = '', $extra = '')
    {
        $checked = ($checked == '' || $checked == 'n') ? '' : "checked='checked'";
        
        return "<input class='checkbox' type='checkbox' name='{$name}' value='{$value}' {$checked}{$extra} />".NL;
    }
    /* END */


    /** -------------------------------------------
    /**  Input - radio buttons
    /** -------------------------------------------*/
    
    function input_radio($name, $value='', $checked = 0, $extra = '')
    {
        $checked = ($checked == 0) ? '' : "checked='checked'";
    
        return "<input class='radio' type='radio' name='{$name}' value='{$value}' {$checked}{$extra} />".NL;
    }
    /* END */


    /** -------------------------------------------
    /**  Input - submit
    /** -------------------------------------------*/
    
    function input_submit($value='', $name = '', $extra='')
    {    
        global $LANG;
        
        $value = ($value == '') ? $LANG->line('submit') : $value;
        $name  = ($name == '') ? '' : "name='".$name."'";
        
        if ($extra != '')
            $extra = ' '.$extra.' ';
    
        return NL."<input $name type='submit' class='submit' value='{$value}' {$extra} />".NL;
    }
    /* END */
    
    /** -------------------------------------------
    /**  Magic Checkboxes for Rows
    /** -------------------------------------------*/
    
    function magic_checkboxes()
    {
    	ob_start();
    	
    	?>

<script type="text/javascript"> 
<!--

var lastChecked = '';

function magic_check()
{
	var listTable = document.getElementById('target').getElementsByTagName("table")[0];
	var listTRs = listTable.getElementsByTagName("tr");
	
	for (var j = 0; j < listTRs.length; j++)
	{
		var elements = listTRs[j].getElementsByTagName("td");
		
		for ( var i = 0; i < elements.length; i++ )
		{
			elements[i].onclick = function (e) {
			
									e = (e) ? e : ((window.event) ? window.event : "")
									var element = e.target || e.srcElement;
									var tag = element.tagName ? element.tagName.toLowerCase() : null;
									
									// Last chance
									if (tag == null)
									{
										element = element.parentNode;
										tag = element.tagName ? element.tagName.toLowerCase() : null;
									}
			
									if (tag != 'a' && tag != null)
									{
										while (element.tagName.toLowerCase() != 'tr')
										{
											element = element.parentNode;
											if (element.tagName.toLowerCase() == 'a') return;
										}
										
										var theTDs = element.getElementsByTagName("td");
										var theInputs = element.getElementsByTagName("input");
										var entryID = false;
										var toggleFlag = false;
										
										for ( var k = 0; k < theInputs.length; k++ )
										{
											if (theInputs[k].type == "checkbox")
											{
												if (theInputs[k].name == 'toggleflag')
												{
													toggleFlag = true;
												}
												else
												{
													entryID = theInputs[k].id;
												}
												
												break;
											}
										}
										
										if (entryID == false && toggleFlag == false) return;
										
										// Select All Checkbox
										if (toggleFlag == true)
										{
											if (tag != 'input')
											{
												return;
											}
											
											var listTable = document.getElementById('target').getElementsByTagName("table")[0];
											var listTRs = listTable.getElementsByTagName("tr");
	
											for (var j = 1; j < listTRs.length; j++)
											{
												var elements = listTRs[j].getElementsByTagName("td");
		
												for ( var t = 0; t < elements.length; t++ )
												{
													if (theInputs[k].checked == true)
													{
														elements[t].className = (elements[t].className == 'tableCellOne') ? 'tableCellTwoHover' : 'tableCellOneHover';
													}
													else
													{
														elements[t].className = (elements[t].className == 'tableCellOneHover') ? 'tableCellOne' : 'tableCellTwo';
													}
												}
											}
										}
										else
										{
											if (tag != 'input')
											{
												document.getElementById(entryID).checked = (document.getElementById(entryID).checked ? false : true);
											}
										
											// Unselect any selected text on screen
											// Safari does not have this ability, which sucks
											// so I just did a focus();
											if (document.getSelection) { window.getSelection().removeAllRanges(); }
											else if (document.selection) { document.selection.empty(); }
											else { document.getElementById(entryID).focus(); }
											
											for ( var t = 0; t < theTDs.length; t++ )
											{
												if (document.getElementById(entryID).checked == true)
												{
													theTDs[t].className = (theTDs[t].className == 'tableCellOne') ? 'tableCellTwoHover' : 'tableCellOneHover';
												}
												else
												{
													theTDs[t].className = (theTDs[t].className == 'tableCellOneHover') ? 'tableCellOne' : 'tableCellTwo';
												}
											}
										
											if (e.shiftKey && lastChecked != '')
											{
												shift_magic_check(document.getElementById(entryID).checked, lastChecked, element);
											}
										
											lastChecked = element;
										}
									}
								}
		}
	}
}	


function shift_magic_check(whatSet, lastChecked, current)
{
	var outerElement = current.parentNode;
	var outerTag = outerElement.tagName ? outerElement.tagName.toLowerCase() : null;
	
	if (outerTag == null)
	{
		outerElement = outerElement.parentNode;
		outerTag = outerElement.tagName ? outerElement.tagName.toLowerCase() : null;
	}
	
	if (outerTag != null)
	{
		while (outerElement.tagName.toLowerCase() != 'table')
		{
			outerElement = outerElement.parentNode;
		}
		
		var listTRs = outerElement.getElementsByTagName("tr");
	
		var start = false;
	
		for (var j = 1; j < listTRs.length; j++)
		{
			if (start == false && listTRs[j] != lastChecked && listTRs[j] != current)
			{
				continue;
			}
			
			var listTDs = listTRs[j].getElementsByTagName("td");
			var listInputs = listTRs[j].getElementsByTagName("input");
			var entryID = false;
			
			for ( var k = 0; k < listInputs.length; k++ )
			{
				if (listInputs[k].type == "checkbox")
				{
					entryID = listInputs[k].id;
				}
			}
										
			if (entryID == false || entryID == '') return;
			
			document.getElementById(entryID).checked = whatSet;
			
			for ( var t = 0; t < listTDs.length; t++ )
			{
				if (whatSet == true)
				{
					listTDs[t].className = (listTDs[t].className == 'tableCellOne') ? 'tableCellTwoHover' : 'tableCellOneHover';
				}
				else
				{
					listTDs[t].className = (listTDs[t].className == 'tableCellOneHover') ? 'tableCellOne' : 'tableCellTwo';
				}
			}
			
			if (listTRs[j] == lastChecked || listTRs[j] == current)
			{
				if (start == true) break;
				if (start == false) start = true;
			}
		}
	}
}
   
//-->
</script>
        <?php
    
        $buffer = ob_get_contents();
                
        ob_end_clean(); 
        
        return $buffer;
    } 
    /* END */
       
    
    
    /** -------------------------------------------
    /**  JavaScript checkbox toggle code
    /** -------------------------------------------*/
    
    // This lets us check/uncheck all checkboxes in a series

    function toggle()
    {
        ob_start();
    
        ?>
        <script type="text/javascript"> 
        <!--
    
        function toggle(thebutton)
        {
            if (thebutton.checked) 
            {
               val = true;
            }
            else
            {
               val = false;
            }
                        
            var len = document.target.elements.length;
        
            for (var i = 0; i < len; i++) 
            {
                var button = document.target.elements[i];
                
                var name_array = button.name.split("["); 
                
                if (name_array[0] == "toggle") 
                {
                    button.checked = val;
                }
            }
            
            document.target.toggleflag.checked = val;
        }
        
        //-->
        </script>
        <?php
    
        $buffer = ob_get_contents();
                
        ob_end_clean(); 
        
        return $buffer;
    } 
    /* END */
        
        
    
    // DEPRECATED DISPLAY FUNCTIONS
    // ------------------------------------------------------------------------    
    // ------------------------------------------------------------------------        
    
    // At present we're still using these so they have to stay here.
    // Once we've gone throgh the entire control panel and replaced these
    // function calls with the new versions we can kill them.
    
        
    function table($style='', $cellspacing='0', $cellpadding='0', $width='100%', $border='0', $align='')
    {
        $style   = ($style != '') ? " class='{$style}' " : '';
        $width   = ($width != '') ? " style='width:{$width};' " : '';
        $align   = ($align != '') ? " align='{$align}' " : '';
                                
        if ($border == '')      $border = 0;
        if ($cellspacing == '') $cellspacing = 0;
        if ($cellpadding == '') $cellpadding = 0;
        
        return NL."<table border='{$border}'  cellspacing='{$cellspacing}' cellpadding='{$cellpadding}'{$width}{$style}{$align}>".NL;
    }
    /* END */

    /** -------------------------------------------
    /**  Table "quick" row
    /** -------------------------------------------*/
    
    function table_qrow($style='', $data = '', $auto_width = FALSE)
    {
    	$width = '';
        $style = ($style != '') ? " class='{$style}' " : '';
    
        if (is_array($data))
        {
        	if ($auto_width != FALSE AND count($data) > 1)
        	{
        		$width = floor(100/count($data)).'%';
        	}
        	
			$width = ($width != '') ? " style='width:{$width};' " : '';
        
            $r = "<tr>";
            
            foreach($data as $val)
            {
                $r .=  "<td".$style.$width.">".
                       $val.
                       '</td>'.NL;    
            }
            
            $r .= "</tr>".NL;
            
            return $r;      
        }
        else
        {
            return
            
                "<tr>".
                "<td".$style.$width.">".
                $data.
                '</td>'.NL.
                "</tr>".NL;   
        }
    }
    /* END */


    /** -------------------------------------------
    /**  Table "quick" cell
    /** -------------------------------------------*/

    function table_qcell($style = '', $data = '', $width = '', $valign = '', $align = '')
    {    
        if (is_array($data))
        {
            $r = '';
                        
            foreach($data as $val)
            {
                $r .=  $this->td($style, $width, '', '', $valign, $align).
                       $val.
                       $this->td_c();    
            }
            
            return $r;      
        }
        else
        {
            return
            
                $this->td($style, $width, '', '', $valign, $align).
                $data.
                $this->td_c();    
        }
    }
    /* END */


    /** -------------------------------------------
    /**  Table row start
    /** -------------------------------------------*/
    
    function tr($style='')
    {
        return "<tr>";
    }
    /* END */
    
    /** -------------------------------------------
    /**  Table data cell
    /** -------------------------------------------*/
    
    function td($style='', $width='', $colspan='', $rowspan='', $valign = '', $align = '')
    {
        if ($style  == '') 
            $style = 'default';
        
        if ($style != 'none')
        {
        	$style = " class='".$style."' ";
        }
        
        $width   = ($width   != '') ? " style='width:{$width};'" : '';
        $colspan = ($colspan != '') ? " colspan='{$colspan}'"   : '';
        $rowspan = ($rowspan != '') ? " rowspan='{$rowspan}'"   : '';
        $valign  = ($valign  != '') ? " valign='{$valign}'"     : '';
        $align   = ($align  != '') 	? " align='{$align}'"       : '';
        
        return $this->nl()."<td ".$style.$width.$colspan.$rowspan.$valign.$align.">".$this->nl();
    }
    /* END */

    /** -------------------------------------------
    /**  Table cell close
    /** -------------------------------------------*/
    
    function td_c()
    {
        return $this->nl().'</td>';
    }
    /* END */
    
    /** -------------------------------------------
    /**  Table row close
    /** -------------------------------------------*/
    
    function tr_c()
    {
        return $this->nl().'</tr>'.$this->nl();
    }
    /* END */
    
    
    /** -------------------------------------------
    /**  Table close
    /** -------------------------------------------*/
    
    function table_c()
    {
    	return '</table>'.$this->nl(2);
    }
    /* END */

    /** -------------------------------------------
    /**  Form declaration -- old version
    /** -------------------------------------------*/
    
    function form($action, $name = '', $method = 'post', $extras = '')
    {
        if ($name != '')
            $name = " name='{$name}' id='{$name}' ";
            
        if ($method != '')
            $method = 'post';
                
        return $this->nl()."<form method='{$method}' ".$name." action='".BASE.AMP.$action.$this->url_append."' $extras>".$this->nl();
    }
    /* END */
    
    
    /** -------------------------------------------
    /**  Form close
    /** -------------------------------------------*/
    
    function form_c()
    {
        return "</form>".NL;
    }


	// END DEPRECATED DISPLAY FUNCTIONS    
    // ------------------------------------------------------------------------    
    // ------------------------------------------------------------------------    
    
    
	/* -------------------------------------
	/*  Validate Weblog and Member Custom Fields
	/*  
	/*  Yes this method doesn't strictly have anything to do with
	/*  displaying anything, but here it will be available for use
	/*  in the control panel by anyone who needs it.
	/* -------------------------------------*/
	
	function invalid_custom_field_names()
	{
		$weblog_vars = array(
								'aol_im', 'author', 'author_id', 'avatar_image_height',
								'avatar_image_width', 'avatar_url', 'comment_auto_path',
								'comment_entry_id_auto_path', 'comment_tb_total',
								'comment_total', 'comment_url_title_path', 'count',
								'edit_date', 'email', 'entry_date', 'entry_id',
								'entry_id_path', 'expiration_date', 'forum_topic_id',
								'gmt_edit_date', 'gmt_entry_date', 'icq', 'interests',
								'ip_address', 'location', 'member_search_path', 'msn_im',
								'occupation', 'permalink', 'photo_image_height',
								'photo_image_width', 'photo_url', 'profile_path',
								'recent_comment_date', 'relative_date', 'relative_url',
								'screen_name', 'signature', 'signature_image_height',
								'signature_image_url', 'signature_image_width', 'status',
								'switch', 'title', 'title_permalink', 'total_results',
								'trackback_total', 'trimmed_url', 'url',
								'url_as_email_as_link', 'url_or_email', 'url_or_email_as_author',
								'url_title', 'url_title_path', 'username', 'weblog',
								'weblog_id', 'yahoo_im', 
							);
							
		$global_vars = array(
								'app_version', 'captcha', 'charset', 'current_time',
								'debug_mode', 'elapsed_time', 'email', 'embed', 'encode',
								'group_description', 'group_id', 'gzip_mode', 'hits',
								'homepage', 'ip_address', 'ip_hostname', 'lang', 'location',
								'member_group', 'member_id', 'member_profile_link', 'path',
								'private_messages', 'screen_name', 'site_index', 'site_name',
								'site_url', 'stylesheet', 'total_comments', 'total_entries',
								'total_forum_posts', 'total_forum_topics', 'total_queries',
								'username', 'webmaster_email', 'version'
							);
		
		$orderby_vars = array(
								'comment_total', 'date', 'edit_date', 'expiration_date',
								'most_recent_comment', 'random', 'screen_name', 'title',
								'url_title', 'username', 'view_count_four', 'view_count_one',
								'view_count_three', 'view_count_two'
						 	 );
						
		return array_unique(array_merge($weblog_vars, $global_vars, $orderby_vars));
	}
	/* END */
	
    
    
}
// END CLASS
?>