Exemple #1
0
    function new_entry_form($which = 'new', $submission_error = '', $entry_id = '', $hidden = array())
    {
        global $DSP, $LANG, $LOC, $DB, $IN, $REGX, $FNS, $SESS, $PREFS, $EXT;
        $title = '';
        $url_title = '';
        $url_title_prefix = '';
        $default_entry_title = '';
        $status = '';
        $expiration_date = '';
        $comment_expiration_date = '';
        $entry_date = '';
        $sticky = '';
        $allow_trackbacks = '';
        $trackback_urls = '';
        $field_data = '';
        $allow_comments = '';
        $preview_text = '';
        $catlist = '';
        $author_id = '';
        $tb_url = '';
        $bookmarklet = FALSE;
        $version_id = $IN->GBL('version_id');
        $version_num = $IN->GBL('version_num');
        $dst_enabled = $SESS->userdata('daylight_savings');
        $weblog_id = '';
        if ($PREFS->ini('site_pages') !== FALSE) {
            $LANG->fetch_language_file('pages');
        }
        $publish_tabs = array('form' => $LANG->line('publish_form'), 'date' => $LANG->line('date'), 'cat' => $LANG->line('categories'), 'option' => $LANG->line('options'), 'tb' => $LANG->line('trackbacks'), 'ping' => $LANG->line('pings'), 'forum' => $LANG->line('forum'), 'revisions' => $LANG->line('revisions'), 'pages' => $LANG->line('pages_module_name'), 'show_all' => $LANG->line('show_all'));
        /** ------------------------------------------------------------------
            /**  We need to first determine which weblog to post the entry into.
            /** ------------------------------------------------------------------*/
        $assigned_weblogs = $FNS->fetch_assigned_weblogs();
        // if it's an edit, we just need the entry id and can figure out the rest
        if ($IN->GBL('entry_id', 'GET') !== FALSE and is_numeric($IN->GBL('entry_id', 'GET')) and $weblog_id == '') {
            $query = $DB->query("SELECT weblog_id FROM exp_weblog_titles WHERE entry_id = '" . $DB->escape_str($IN->GBL('entry_id', 'GET')) . "'");
            if ($query->num_rows == 1) {
                $weblog_id = $query->row['weblog_id'];
            }
        }
        if ($weblog_id == '' and !($weblog_id = $IN->GBL('weblog_id', 'GP'))) {
            // Does the user have their own blog?
            if ($SESS->userdata['weblog_id'] != 0) {
                $weblog_id = $SESS->userdata['weblog_id'];
            } elseif (sizeof($assigned_weblogs) == 1) {
                $weblog_id = $assigned_weblogs['0'];
            } else {
                $query = $DB->query("SELECT weblog_id from exp_weblogs WHERE is_user_blog = 'n'");
                if ($query->num_rows == 1) {
                    $weblog_id = $query->row['weblog_id'];
                } else {
                    return false;
                }
            }
        }
        if (!is_numeric($weblog_id)) {
            return FALSE;
        }
        /** ----------------------------------------------
            /**  Security check
            /** ---------------------------------------------*/
        if (!in_array($weblog_id, $assigned_weblogs)) {
            return $DSP->no_access_message($LANG->line('unauthorized_for_this_blog'));
        }
        // -------------------------------------------
        // 'publish_form_start' hook.
        //  - Allows complete rewrite of Publish page.
        //  - Added $hidden: 1.6.0
        //
        $edata = $EXT->call_extension('publish_form_start', $which, $submission_error, $entry_id, $hidden);
        if ($EXT->end_script === TRUE) {
            return;
        }
        //
        // -------------------------------------------
        // -------------------------------------------
        // 'publish_form_headers' hook.
        //  - Adds content to headers for Publish page.
        //  - Added $weblog_id: 1.6
        //  - Added $hidden: 1.6.0
        //
        $DSP->extra_header .= $EXT->call_extension('publish_form_headers', $which, $submission_error, $entry_id, $weblog_id, $hidden);
        if ($EXT->end_script === TRUE) {
            return;
        }
        //
        // -------------------------------------------
        // -------------------------------------------
        // 'publish_form_new_tabs' hook.
        //  - Allows adding of new tabs to submission form
        //  - Added: 1.4.1
        //  - Added $hidden: 1.6.0
        //
        if ($EXT->active_hook('publish_form_new_tabs') === TRUE) {
            $publish_tabs = $EXT->call_extension('publish_form_new_tabs', $publish_tabs, $weblog_id, $entry_id, $hidden);
        }
        //
        // -------------------------------------------
        /** ----------------------------------------------
        		/**  If Still Set, Show All Goes at the End
        		/** ---------------------------------------------*/
        if (isset($publish_tabs['show_all'])) {
            unset($publish_tabs['show_all']);
            $publish_tabs['show_all'] = $LANG->line('show_all');
        }
        /** ----------------------------------------------
            /**  Fetch weblog preferences
            /** ---------------------------------------------*/
        $query = $DB->query("SELECT * FROM  exp_weblogs WHERE weblog_id = '" . $DB->escape_str($weblog_id) . "'");
        if ($query->num_rows == 0) {
            return $DSP->error_message($LANG->line('no_weblog_exits'));
        }
        // -------------------------------------------
        // 'publish_form_weblog_preferences' hook.
        //  - Modify weblog preferences
        //  - Added: 1.4.1
        //
        if ($EXT->active_hook('publish_form_weblog_preferences') === TRUE) {
            $query->row = $EXT->call_extension('publish_form_weblog_preferences', $query->row);
        }
        //
        // -------------------------------------------
        foreach ($query->row as $key => $val) {
            ${$key} = $val;
        }
        /** ----------------------------------------------
            /**  Fetch Revision if Necessary
            /** ---------------------------------------------*/
        $show_revision_cluster = $enable_versioning == 'y' ? 'y' : 'n';
        if ($which == 'new') {
            $versioning_enabled = $enable_versioning == 'y' ? 'y' : 'n';
        } else {
            $versioning_enabled = isset($_POST['versioning_enabled']) ? 'y' : 'n';
        }
        if (is_numeric($version_id)) {
            $entry_id = $IN->GBL('entry_id');
            $revquery = $DB->query("SELECT version_data FROM exp_entry_versioning WHERE entry_id = '{$entry_id}' AND version_id = '{$version_id}'");
            if ($revquery->num_rows == 1) {
                $_POST = $REGX->array_stripslashes(@unserialize($revquery->row['version_data']));
                $_POST['entry_id'] = $entry_id;
                $which = 'preview';
            }
            unset($revquery);
        }
        /** ---------------------------------------
            /**  Insane Idea to Have Defaults and Prefixes
            /** ---------------------------------------*/
        if ($which == 'edit') {
            $url_title_prefix = '';
        } elseif ($which == 'new') {
            $title = $default_entry_title;
            $url_title = $url_title_prefix;
        }
        // --------------------------------------------------------------------
        // The $which variable determines what the page should show:
        //  If $which = 'new' we'll show a blank "new entry" page
        //  If $which = "preview", the user has clicked the "preview" button.
        //  If $which = "edit", we are editing an already existing entry.
        //  If $which = 'save', like a preview, but also an edit.
        // --------------------------------------------------------------------
        if ($which == 'edit') {
            if (!($entry_id = $IN->GBL('entry_id', 'GET'))) {
                return false;
            }
            // Fetch the weblog data
            $sql = "SELECT t.*, d.*\n                    FROM   exp_weblog_titles AS t, exp_weblog_data AS d\n                    WHERE  t.entry_id\t= '{$entry_id}'\n                    AND    t.weblog_id\t= '{$weblog_id}'\n                    AND    t.entry_id\t=  d.entry_id";
            $result = $DB->query($sql);
            if ($result->num_rows == 0) {
                return $DSP->error_message($LANG->line('no_weblog_exits'));
            }
            if ($result->row['author_id'] != $SESS->userdata('member_id')) {
                if (!$DSP->allowed_group('can_edit_other_entries')) {
                    return $DSP->no_access_message();
                }
            }
            // -------------------------------------------
            // 'publish_form_entry_data' hook.
            //  - Modify entry's data
            //  - Added: 1.4.1
            //
            if ($EXT->active_hook('publish_form_entry_data') === TRUE) {
                $result->row = $EXT->call_extension('publish_form_entry_data', $result->row);
            }
            //
            // -------------------------------------------
            foreach ($result->row as $key => $val) {
                ${$key} = $val;
            }
        }
        /** ---------------------------------------------
            /**  Assign page title based on type of request
            /** ---------------------------------------------*/
        switch ($which) {
            case 'edit':
                $DSP->title = $LANG->line('edit_entry');
                break;
            case 'save':
                $DSP->title = $LANG->line('edit_entry');
                break;
            case 'preview':
                $DSP->title = $LANG->line('preview');
                break;
            default:
                $DSP->title = $LANG->line('new_entry');
                break;
        }
        /** ----------------------------------------------
            /**  Assign breadcrumb
            /** ---------------------------------------------*/
        $DSP->crumb = $DSP->title . $DSP->crumb_item($blog_title);
        $activate_calendars = '"';
        if ($show_date_menu == 'y') {
            // Setup some onload items
            $activate_calendars = 'activate_calendars();" ';
            $DSP->extra_header .= '<script type="text/javascript">
			// depending on timezones, local settings and localization prefs, its possible for js to misinterpret the day, 
			// but the humanized time is correct, so we activate the humanized time to sync the calendar
		
			function activate_calendars() {
				update_calendar(\'entry_date\', document.getElementById(\'entry_date\').value);
				update_calendar(\'expiration_date\', document.getElementById(\'expiration_date\').value);';
            if ($comment_system_enabled == 'y') {
                $DSP->extra_header .= "\n\t\t\t\t" . 'update_calendar(\'comment_expiration_date\', document.getElementById(\'comment_expiration_date\').value);';
            }
            $DSP->extra_header .= "\n\t\t\t\t" . "current_month\t= '';\n\t\t\t\tcurrent_year\t= '';\n\t\t\t\tlast_date\t= '';";
            $DSP->extra_header .= "\n" . '}
			</script>';
        }
        /* -------------------------------------
        		/*  Publish Page Title Focus
        		/*  
        		/*  makes the title field gain focus when the page is loaded
        		/*  
        		/*  Hidden Configuration Variable
        		/*  - publish_page_title_focus => Set focus to the tile? (y/n)
        		/* -------------------------------------*/
        if ($which != 'edit' && $PREFS->ini('publish_page_title_focus') !== 'n') {
            $load_events = 'document.forms[0].title.focus();set_catlink();';
        } else {
            $load_events = 'set_catlink();';
        }
        $DSP->body_props .= ' onload="' . $load_events . $activate_calendars;
        // -------------------------------------------
        // 'publish_form_body_props' hook.
        //  - Allows setting of the body properties
        //
        $edata = $EXT->call_extension('publish_form_body_props');
        if ($EXT->end_script === TRUE) {
            return;
        }
        //
        // -------------------------------------------
        /** ----------------------------------------------
            /**  Are we using the bookmarklet?
            /** ---------------------------------------------*/
        if ($IN->GBL('BK', 'GP')) {
            $bookmarklet = TRUE;
            $tb_url = $IN->GBL('tb_url', 'GP');
        }
        /** ----------------------------------------------
            /**  Start building the page output
            /** ---------------------------------------------*/
        $r = '';
        /** ----------------------------------------------
            /**  Form header and hidden fields  
            /** ---------------------------------------------*/
        $BK = $bookmarklet == TRUE ? AMP . 'BK=1' . AMP . 'Z=1' : '';
        if ($IN->GBL('C') == 'publish') {
            $r .= $DSP->form_open(array('action' => 'C=publish' . AMP . 'M=new_entry' . $BK, 'name' => 'entryform', 'id' => 'entryform'));
        } else {
            $r .= $DSP->form_open(array('action' => 'C=edit' . AMP . 'M=new_entry' . $BK, 'name' => 'entryform', 'id' => 'entryform'));
        }
        $r .= $DSP->input_hidden('weblog_id', $weblog_id);
        foreach ($hidden as $key => $value) {
            $r .= $DSP->input_hidden($key, $value);
        }
        if ($IN->GBL('entry_id', 'POST')) {
            $entry_id = $IN->GBL('entry_id');
        }
        if (isset($entry_id)) {
            $r .= $DSP->input_hidden('entry_id', $entry_id);
        }
        if ($bookmarklet == TRUE) {
            $r .= $DSP->input_hidden('tb_url', $tb_url);
        }
        /** --------------------------------
            /**  Fetch Custom Fields
            /** --------------------------------*/
        // Even though we don't need this query until laters we'll run the
        // query here so that we can show previews in the proper order.
        // -------------------------------------------
        // 'publish_form_field_query' hook.
        //  - Allows control over the field query, controlling what fields will be displayed
        //
        if (isset($EXT->extensions['publish_form_field_query'])) {
            $field_query = $EXT->call_extension('publish_form_field_query', $this, $field_group);
        } else {
            $field_query = $DB->query("SELECT * FROM  exp_weblog_fields WHERE group_id = '{$field_group}' ORDER BY field_order");
        }
        //
        // -------------------------------------------
        /** ----------------------------------------------
            /**  Javascript stuff
            /** ---------------------------------------------*/
        $convert_ascii = $PREFS->ini('auto_convert_high_ascii') == 'y' ? TRUE : FALSE;
        // "title" input Field
        if ($IN->GBL('title', 'GET')) {
            $title = $this->bm_qstr_decode($IN->GBL('title', 'GET'));
        }
        $word_separator = $PREFS->ini('word_separator') != "dash" ? '_' : '-';
        if (!class_exists('Spellcheck')) {
            require PATH_CORE . 'core.spellcheck' . EXT;
        }
        $this->SPELL = new Spellcheck();
        $spellcheck_js = $this->SPELL->JavaScript(BASE . '&C=publish&M=spellcheck');
        /** -------------------------------------
           	/**  Create Foreign Character Conversion JS
           	/** -------------------------------------*/
        /* -------------------------------------
        		/*  'foreign_character_conversion_array' hook.
        		/*  - Allows you to use your own foreign character conversion array
        		/*  - Added 1.6.0
        		*/
        if (isset($EXT->extensions['foreign_character_conversion_array'])) {
            $foreign_characters = $EXT->call_extension('foreign_character_conversion_array');
        } else {
            $foreign_characters = array('223' => "ss", '224' => "a", '225' => "a", '226' => "a", '229' => "a", '227' => "ae", '230' => "ae", '228' => "ae", '231' => "c", '232' => "e", '233' => "e", '234' => "e", '235' => "e", '236' => "i", '237' => "i", '238' => "i", '239' => "i", '241' => "n", '242' => "o", '243' => "o", '244' => "o", '245' => "o", '246' => "oe", '249' => "u", '250' => "u", '251' => "u", '252' => "ue", '255' => "y", '257' => "aa", '269' => "ch", '275' => "ee", '291' => "gj", '299' => "ii", '311' => "kj", '316' => "lj", '326' => "nj", '353' => "sh", '363' => "uu", '382' => "zh", '256' => "aa", '268' => "ch", '274' => "ee", '290' => "gj", '298' => "ii", '310' => "kj", '315' => "lj", '325' => "nj", '352' => "sh", '362' => "uu", '381' => "zh");
        }
        /*
        		/* -------------------------------------*/
        $foreign_replace = '';
        foreach ($foreign_characters as $old => $new) {
            $foreign_replace .= "if (c == '{$old}') {NewTextTemp += '{$new}'; continue;}\n\t\t\t\t";
        }
        /** -------------------------------------
           	/**  Publish Tabs JavaScript
           	/** -------------------------------------*/
        $publish_tabs_javascript = "var blockarray\t= new Array(" . (sizeof($publish_tabs) - 1) . ")\n";
        $p = 0;
        foreach ($publish_tabs as $short => $long) {
            $publish_tabs_javascript .= "\t\t" . 'blockarray[' . $p . '] = "block' . $short . '"' . "\n";
            $p++;
        }
        $default_entry_title = $REGX->form_prep($default_entry_title);
        $r .= <<<EOT
        
        <script type="text/javascript"> 
        <!--
        
        /** ------------------------------------
        /**  Swap out categories
        /** -------------------------------------*/
     
     \t// This is used by the "edit categories" feature
     \t
     \tfunction set_catlink()
     \t{
     \t\tif (document.getElementById('cateditlink'))
     \t\t{
     \t\t\tif (browser == "IE" && OS == "Mac")  
\t\t\t\t{
\t\t\t\t\tdocument.getElementById('cateditlink').style.display = "none";
\t\t\t\t}
\t\t\t\telse
\t\t\t\t{
\t\t\t\t\tdocument.getElementById('cateditlink').style.display = "block";
\t\t\t\t}
\t\t\t}
     \t}
     \t
        function swap_categories(str)
        {
        \tdocument.getElementById('categorytree').innerHTML = str;\t
        }
        
        /** ------------------------------------
        /**  Array Helper Functions
        /** -------------------------------------*/

        function getarraysize(thearray)
        {
            for (i = 0; i < thearray.length; i++)
            {
                if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null))
                {
                    return i;
                }
            }
            
            return thearray.length;
        }        
        
        // Array push
        function arraypush(thearray, value)
        {
            thearray[getarraysize(thearray)] = value;
        }
        
        // Array pop
        function arraypop(thearray)
        {
            thearraysize = getarraysize(thearray);
            retval = thearray[thearraysize - 1];
            delete thearray[thearraysize - 1];
            return retval;
        }\t\t
\t\t
        /** ------------------------------------
        /**  Live URL Title Function
        /** -------------------------------------*/
        
        function liveUrlTitle()
        {
        \tvar defaultTitle = '{$default_entry_title}';
\t\t\tvar NewText = document.getElementById("title").value;
\t\t\t
\t\t\tif (defaultTitle != '')
\t\t\t{
\t\t\t\tif (NewText.substr(0, defaultTitle.length) == defaultTitle)
\t\t\t\t{
\t\t\t\t\tNewText = NewText.substr(defaultTitle.length);
\t\t\t\t}\t
\t\t\t}
\t\t\t
\t\t\tNewText = NewText.toLowerCase();
\t\t\tvar separator = "{$word_separator}";
\t\t\t\t
\t\t\t// Foreign Character Attempt
\t\t\t
\t\t\tvar NewTextTemp = '';
\t\t\tfor(var pos=0; pos<NewText.length; pos++)
\t\t\t{
\t\t\t\tvar c = NewText.charCodeAt(pos);
\t\t\t\t
\t\t\t\tif (c >= 32 && c < 128)
\t\t\t\t{
\t\t\t\t\tNewTextTemp += NewText.charAt(pos);
\t\t\t\t}
\t\t\t\telse
\t\t\t\t{
\t\t\t\t\t{$foreign_replace}
\t\t\t\t}
\t\t\t}
    \t\t
\t\t\tvar multiReg = new RegExp(separator + '{2,}', 'g');
\t\t\t
\t\t\tNewText = NewTextTemp;
\t\t\t
\t\t\tNewText = NewText.replace('/<(.*?)>/g', '');
\t\t\tNewText = NewText.replace(/\\s+/g, separator);
\t\t\tNewText = NewText.replace(/\\//g, separator);
\t\t\tNewText = NewText.replace(/[^a-z0-9\\-\\._]/g,'');
\t\t\tNewText = NewText.replace(/\\+/g, separator);
\t\t\tNewText = NewText.replace(multiReg, separator);
\t\t\tNewText = NewText.replace(/-\$/g,'');
\t\t\tNewText = NewText.replace(/_\$/g,'');
\t\t\tNewText = NewText.replace(/^_/g,'');
\t\t\tNewText = NewText.replace(/^-/g,'');
\t\t\tNewText = NewText.replace(/\\.+\$/g,'');
\t\t\t
\t\t\tif (document.getElementById("url_title"))
\t\t\t{
\t\t\t\tdocument.getElementById("url_title").value = "{$url_title_prefix}" + NewText;\t\t\t
\t\t\t}
\t\t\telse
\t\t\t{
\t\t\t\tdocument.forms['entryform'].elements['url_title'].value = "{$url_title_prefix}" + NewText; 
\t\t\t}\t\t
\t\t}

        /** ------------------------------------
        /**  Publish Option Tabs Open/Close
        /** -------------------------------------*/

\t\t{$publish_tabs_javascript}
\t\t
\t\tfunction showblock(which)
\t\t{\t\t\t\t\t
\t\t\tfor (i = 0 ; i < blockarray.length; i++ )
\t\t\t{\t\t\t
\t\t\t\tif (document.getElementById(blockarray[i]))
\t\t\t\t{
\t\t\t\t\tif (which == 'blockshow_all')
\t\t\t\t\t{
\t\t\t\t\t\tdocument.getElementById(blockarray[i]).style.display = "block";
\t\t\t\t\t}
\t\t\t\t\telse
\t\t\t\t\t{
\t\t\t\t\t\tdocument.getElementById(blockarray[i]).style.display = "none";
\t\t\t\t\t}
\t\t\t\t}
\t\t\t\t
\t\t\t\tvar menu = blockarray[i].substring(5) + 'menu';\t
\t\t\t\t
\t\t\t\tif (document.getElementById(menu))
\t\t\t\t{
\t\t\t\t\tdocument.getElementById(menu).style.display = "none";\t\t\t\t\t\t
\t\t\t\t}\t\t\t\t\t
\t\t\t}
\t\t\t
\t\t\tvar menu = which.substring(5) + 'menu';\t
\t\t\t\t\t\t\t\t
\t\t\tif (document.getElementById(which))
\t\t\t{\t\t\t
\t\t\t\tdocument.getElementById(which).style.display = "block";
\t\t\t\tdocument.getElementById(menu).style.display = "block";\t\t\t\t
\t\t\t}\t\t\t
\t\t}\t
\t\t
\t\tfunction styleswitch(link)
\t\t{                 
\t\t\tif (document.getElementById(link).className == 'publishTabs')
\t\t\t{
\t\t\t\tdocument.getElementById(link).className = 'publishTabsHover';
\t\t\t}
\t\t}
\t
\t\tfunction stylereset(link)
\t\t{                 
\t\t\tif (document.getElementById(link).className == 'publishTabsHover')
\t\t\t{
\t\t\t\tdocument.getElementById(link).className = 'publishTabs';
\t\t\t}
\t\t}
\t\t
        /** ------------------------------------
        /**  Glossary Item Insert
        /** -------------------------------------*/

        function glossaryInsert(item, id, tag)
        {        
\t\t\tselField = "field_id_" + id;\t
\t\t\ttaginsert('other', tag, '');      
        }
             
        /** ------------------------------------
        /**  Smiley Insert
        /** -------------------------------------*/
\t\t
        function add_smiley(smiley, id)
        {
\t\t\tselField = "field_id_" + id;\t
\t\t\ttaginsert('other', " " + smiley + " ", '');
\t\t\t
        \tshowhide_smileys(id);
        }
        
        
        {$spellcheck_js}
        
        
        /** ------------------------------------
        /**  Show/Hide Similey Pane
        /** -------------------------------------*/

        var open_panes = new Array();
        
        function showhide_smileys(id)
        {
        \tcid = 'smileys_' + id;
        \tgl = 'glossary_' + id;
        \tsp = 'spellcheck_field_id_' + id;
        \t
        \t
\t\t\tif (document.getElementById(cid))
\t\t\t{
\t\t\t\tif (document.getElementById(cid).style.display == "block")
\t\t\t\t{
                \thide_open_panes();
\t\t\t\t}
\t\t\t\telse
\t\t\t\t{\t\t\t\t
\t\t\t\t\tdocument.getElementById(cid).style.display = "block";
\t\t\t\t\tdocument.getElementById(gl).style.display = "none";
\t\t\t\t\t
\t\t\t\t\tif (document.getElementById(sp))
\t\t\t\t\t{
\t\t\t\t\t\tdocument.getElementById(sp).style.display = "none";
\t\t\t\t\t}
\t\t\t\t\t
\t\t\t\t\thide_open_panes();
\t\t\t\t\tarraypush(open_panes, cid);
\t\t\t\t}
\t\t\t}
        }
           
        /** ------------------------------------
        /**  Show/hide Glossary Pane
        /** -------------------------------------*/

        function showhide_glossary(id)
        {  
        \tcid = 'glossary_' + id;
        \tsm = 'smileys_' + id;
        \tsp = 'spellcheck_field_id_' + id;
        \t
        
\t\t\tif (document.getElementById(cid))
\t\t\t{
\t\t\t\tif (document.getElementById(cid).style.display == "block")
\t\t\t\t{
                \thide_open_panes();
\t\t\t\t}
\t\t\t\telse
\t\t\t\t{
\t\t\t\t\tdocument.getElementById(cid).style.display = "block";
\t\t\t\t\tdocument.getElementById(sm).style.display = "none";

\t\t\t\t\tif (document.getElementById(sp))
\t\t\t\t\t{
\t\t\t\t\t\tdocument.getElementById(sp).style.display = "none";
\t\t\t\t\t}
\t\t\t\t\t
\t\t\t\t\thide_open_panes();
\t\t\t\t\tarraypush(open_panes, cid);
\t\t\t\t}
\t\t\t}
        }
        
        
        /** ------------------------------------
        /**  Show/hide Spellcheck Pane
        /** -------------------------------------*/

        function showhide_spellcheck(id)
        {  
        \tcid = 'spellcheck_field_id_' + id;
        \tsm = 'smileys_' + id;
        \tgl = 'glossary_' + id;
        
\t\t\tif (document.getElementById(cid))
\t\t\t{
\t\t\t\tif (document.getElementById(cid).style.display == "block")
\t\t\t\t{
                \tSP_closeSpellCheck();
                \t
                \thide_open_panes();
\t\t\t\t}
\t\t\t\telse
\t\t\t\t{
\t\t\t\t\tdocument.getElementById(cid).style.display = "block";
\t\t\t\t\tdocument.getElementById(sm).style.display = "none";
\t\t\t\t\tdocument.getElementById(gl).style.display = "none";
\t\t\t\t
\t\t\t\t\teeSpell.getResults('field_id_'+id);
\t\t\t\t
\t\t\t\t\thide_open_panes();
\t\t\t\t\tarraypush(open_panes, cid);
\t\t\t\t}
\t\t\t}
        }
      
        /** ------------------------------------
        /**  Close Open Panes
        /** -------------------------------------*/

        function hide_open_panes()
        {
\t\t\tif (open_panes[0])
\t\t\t{
\t\t\t\twhile (open_panes[0])
\t\t\t\t{
\t\t\t\t\tclearState = arraypop(open_panes);
\t\t\t\t\tdocument.getElementById(clearState).style.display = "none";
\t\t\t\t}
\t\t\t}\t
   \t\t}


        /** ------------------------------------
        /**  Generic show/hide
        /** -------------------------------------*/

        function showhide_item(id)
        {
\t\t\tif (document.getElementById(id).style.display == "block")
\t\t\t{
\t\t\t\tdocument.getElementById(id).style.display = "none";
        \t}
        \telse
        \t{
\t\t\t\tdocument.getElementById(id).style.display = "block";
        \t}
        }
\t\t
\t\t
        /** ------------------------------------
        /**  Show/hide Fields
        /** -------------------------------------*/

        function showhide_field(id)
        {
        \tf_off = 'field_pane_off_' + id;
        \tf_on  = 'field_pane_on_' + id;
        
\t\t\tif (document.getElementById(f_off).style.display == "block")
\t\t\t{
\t\t\t\tdocument.getElementById(f_off).style.display = "none";
\t\t\t\tdocument.getElementById(f_on).style.display = "block";
        \t}
        \telse
        \t{
\t\t\t\tdocument.getElementById(f_off).style.display = "block";
\t\t\t\tdocument.getElementById(f_on).style.display = "none";
        \t}
        }

        // Remove the Preview from the DOM so it isn't added to submitted content
\t\tdocument.getElementById('entryform').onsubmit = function()
\t\t{
\t\t\tif (document.getElementById('entryform').hasChildNodes(document.getElementById('previewBox')) == true)
\t\t\t{
\t\t\t\tdocument.getElementById('entryform').removeChild(document.getElementById('previewBox'));
\t\t\t}
\t\t}

\t\t-->
\t\t</script>
EOT;
        $r .= NL . NL;
        if ($bookmarklet == TRUE) {
            $r .= $DSP->qdiv('defaultSmall', NBS);
        }
        /** ----------------------------------------------
            /**  Are we previewing an entry?
            /** ---------------------------------------------*/
        if ($which == 'preview') {
            /** ----------------------------------------
                /**  Instantiate Typography class
                /** ----------------------------------------*/
            if (!class_exists('Typography')) {
                require PATH_CORE . 'core.typography' . EXT;
            }
            $TYPE = new Typography();
            $TYPE->convert_curly = FALSE;
            $this->smileys = $TYPE->smiley_array;
            $preview = $version_id == FALSE ? $LANG->line('preview') : $LANG->line('version_preview');
            if (is_numeric($version_num)) {
                $preview = str_replace('%s', $version_num, $preview);
            }
            $prv_title = $submission_error == '' ? $preview : $DSP->qspan('alert', $LANG->line('error'));
            $r .= '<fieldset class="previewBox" id="previewBox">';
            $r .= '<legend class="previewItemTitle">&nbsp;' . $prv_title . '&nbsp;</legend>';
            if ($submission_error == '') {
                $r .= $DSP->heading($TYPE->format_characters(stripslashes($IN->GBL('title', 'POST'))));
            }
            // We need to grab each global array index and do a little formatting
            $preview_build = array();
            foreach ($_POST as $key => $val) {
                // Gather categories.  Since you can select as many categories as you want
                // they are submitted as an array.  The $_POST['category'] index
                // contains a sub-array as the value, therefore we need to loop through
                // it and assign discrete variables.
                if (is_array($val)) {
                    foreach ($val as $k => $v) {
                        $_POST[$k] = $v;
                    }
                    if ($key == 'category' or $key == 'ping') {
                        unset($_POST[$key]);
                    }
                } else {
                    if ($submission_error == '') {
                        if (strstr($key, 'field_id')) {
                            $expl = explode('field_id_', $key);
                            // Pass the entry data to the typography class
                            $txt_fmt = !isset($_POST['field_ft_' . $expl['1']]) ? 'xhtml' : $_POST['field_ft_' . $expl['1']];
                            $p_open = $txt_fmt != 'xhtml' ? '<p>' : '';
                            $p_close = $txt_fmt != 'xhtml' ? '</p>' : '';
                            $preview_build['field_id_' . $expl['1']] = $p_open . $TYPE->parse_type(stripslashes($val), array('text_format' => $txt_fmt, 'html_format' => $weblog_html_formatting, 'auto_links' => $weblog_auto_link_urls, 'allow_img_url' => $weblog_allow_img_urls)) . $p_close;
                            /** ----------------------------
                            				/**  Certain tags might cause havoc, so we remove them
                            				/** ----------------------------*/
                            $preview_build['field_id_' . $expl['1']] = preg_replace("#<script([^>]*)>.*?</script>#is", '', $preview_build['field_id_' . $expl['1']]);
                            $preview_build['field_id_' . $expl['1']] = preg_replace("#<form([^>]*)>(.*?)</form>#is", '\\2', $preview_build['field_id_' . $expl['1']]);
                        }
                    }
                    $val = stripslashes($val);
                    $_POST[$key] = $val;
                }
                ${$key} = $val;
            }
            // Show the preview.  We do it this way in order to honor
            // the custom field order since we can't guarantee that $_POST
            // data will be in the correct order
            if (count($preview_build) > 0) {
                foreach ($field_query->result as $row) {
                    if (isset($preview_build['field_id_' . $row['field_id']])) {
                        $r .= $preview_build['field_id_' . $row['field_id']];
                    }
                }
            }
            // Do we have a forum topic preview?
            if ($PREFS->ini('forum_is_installed') == "y") {
                if ($IN->GBL('forum_title') != '') {
                    $r .= $DSP->qdiv('itemWrapper', $DSP->qdiv('itemTitle', $LANG->line('forum_title', 'title')) . $DSP->qdiv('', $IN->GBL('forum_title')));
                }
                if ($IN->GBL('forum_body') != '') {
                    $forum_body = $TYPE->parse_type(stripslashes($IN->GBL('forum_body')), array('text_format' => 'xhtml', 'html_format' => 'safe', 'auto_links' => 'y', 'allow_img_url' => 'y'));
                    $r .= $DSP->qdiv('itemWrapper', $DSP->qdiv('itemTitle', $LANG->line('forum_body', 'title')) . $DSP->qdiv('', $forum_body));
                }
            }
            // -------------------------------------------
            // 'publish_form_preview_additions' hook.
            //  - Add content to preview
            //  - As this is a preview, content can be gotten from $_POST
            //  - Added: 1.4.1
            //
            if ($EXT->active_hook('publish_form_preview_additions') === TRUE) {
                $r .= $EXT->call_extension('publish_form_preview_additions');
            }
            //
            // -------------------------------------------
            // Are there any errors?
            if ($submission_error != '') {
                $r .= $DSP->qdiv('highlight', $submission_error);
            }
            $r .= '</fieldset>';
        }
        // END PREVIEW
        // QUICK SAVE:  THE PREVIEW PART
        if ($which == 'save') {
            foreach ($_POST as $key => $val) {
                if (is_array($val)) {
                    foreach ($val as $k => $v) {
                        $_POST[$k] = $v;
                    }
                    if ($key == 'category' or $key == 'ping') {
                        unset($_POST[$key]);
                    }
                } else {
                    $val = stripslashes($val);
                    $_POST[$key] = $val;
                }
                if ($key != 'entry_id') {
                    ${$key} = $val;
                }
                // we need to unset this or it will cause the forum tab to not display the existing connection
                unset($forum_topic_id);
            }
            $r .= '<fieldset class="previewBox" id="previewBox">';
            $r .= '<legend class="previewItemTitle">&nbsp;' . $LANG->line('quick_save') . '&nbsp;</legend></fieldset>';
        }
        // END SAVE
        /** --------------------------------
            /**  Weblog pull-down menu
            /** --------------------------------*/
        $menu_weblog = '';
        $show_weblog_menu = 'y';
        if ($show_weblog_menu == 'n') {
            $r .= $DSP->input_hidden('new_weblog', $weblog_id);
        } elseif ($which != 'new') {
            /** --------------------------------
            			/**  Create weblog menu
            			/** --------------------------------*/
            $query = $DB->query("SELECT weblog_id, blog_title FROM exp_weblogs \n\t\t\t\t\t\t\t\t WHERE status_group = '{$status_group}' \n\t\t\t\t\t\t\t\t AND cat_group = '" . $DB->escape_str($cat_group) . "'\n\t\t\t\t\t\t\t\t AND field_group = '{$field_group}'\n\t\t\t\t\t\t\t\t AND site_id = '" . $DB->escape_str($PREFS->ini('site_id')) . "'\n\t\t\t\t\t\t\t\t ORDER BY blog_title");
            if ($query->num_rows > 0) {
                foreach ($query->result as $row) {
                    if ($SESS->userdata['group_id'] == 1 or in_array($row['weblog_id'], $assigned_weblogs)) {
                        if (isset($_POST['new_weblog']) && is_numeric($_POST['new_weblog'])) {
                            $selected = $_POST['new_weblog'] == $row['weblog_id'] ? 1 : '';
                        } else {
                            $selected = $weblog_id == $row['weblog_id'] ? 1 : '';
                        }
                        $menu_weblog .= $DSP->input_select_option($row['weblog_id'], $REGX->form_prep($row['blog_title']), $selected);
                    }
                }
                if ($menu_weblog != '') {
                    $menu_weblog = $DSP->input_select_header('new_weblog') . $menu_weblog . $DSP->input_select_footer();
                }
            }
        }
        /** --------------------------------
            /**  Status pull-down menu
            /** --------------------------------*/
        $menu_status = '';
        if ($deft_status == '') {
            $deft_status = 'open';
        }
        if ($status == '') {
            $status = $deft_status;
        }
        if ($show_status_menu == 'n') {
            $r .= $DSP->input_hidden('status', $status);
        } else {
            $menu_status .= $DSP->input_select_header('status');
            /** --------------------------------
            			/**  Fetch disallowed statuses
            			/** --------------------------------*/
            $no_status_access = array();
            if ($SESS->userdata['group_id'] != 1) {
                $query = $DB->query("SELECT status_id FROM exp_status_no_access WHERE member_group = '" . $SESS->userdata['group_id'] . "'");
                if ($query->num_rows > 0) {
                    foreach ($query->result as $row) {
                        $no_status_access[] = $row['status_id'];
                    }
                }
            }
            /** --------------------------------
            			/**  Create status menu
            			/** --------------------------------*/
            $query = $DB->query("SELECT * FROM  exp_statuses WHERE group_id = '{$status_group}' order by status_order");
            if ($query->num_rows == 0) {
                // if there is no status group assigned, only Super Admins can create 'open' entries
                if ($SESS->userdata['group_id'] == 1) {
                    $menu_status .= $DSP->input_select_option('open', $LANG->line('open'), $status == 'open' ? 1 : '');
                }
                $menu_status .= $DSP->input_select_option('closed', $LANG->line('closed'), $status == 'closed' ? 1 : '');
            } else {
                $no_status_flag = TRUE;
                foreach ($query->result as $row) {
                    $selected = $status == $row['status'] ? 1 : '';
                    if (in_array($row['status_id'], $no_status_access)) {
                        continue;
                    }
                    $no_status_flag = FALSE;
                    $status_name = ($row['status'] == 'open' or $row['status'] == 'closed') ? $LANG->line($row['status']) : $row['status'];
                    $menu_status .= $DSP->input_select_option($REGX->form_prep($row['status']), $REGX->form_prep($status_name), $selected);
                }
                /** --------------------------------
                				/**  Were there no statuses?
                				/** --------------------------------*/
                // If the current user is not allowed to submit any statuses
                // we'll set the default to closed
                if ($no_status_flag == TRUE) {
                    $menu_status .= $DSP->input_select_option('closed', $LANG->line('closed'));
                }
            }
            $menu_status .= $DSP->input_select_footer();
        }
        /** --------------------------------
            /**  Author pull-down menu
            /** --------------------------------*/
        $menu_author = '';
        // First we'll assign the default author.
        if ($author_id == '') {
            $author_id = $SESS->userdata('member_id');
        }
        if ($show_author_menu == 'n') {
            $r .= $DSP->input_hidden('author_id', $author_id);
        } else {
            $menu_author .= $DSP->input_select_header('author_id');
            $query = $DB->query("SELECT username, screen_name FROM exp_members WHERE member_id = '{$author_id}'");
            $author = $query->row['screen_name'] == '' ? $query->row['username'] : $query->row['screen_name'];
            $menu_author .= $DSP->input_select_option($author_id, $author);
            // Next we'll gather all the authors that are allowed to be in this list
            /*
            // OLD VERSION OF THE QUERY... not so good
            $ss = "SELECT exp_members.member_id, exp_members.group_id, exp_members.username, exp_members.screen_name, exp_members.weblog_id,
            	exp_member_groups.*
            	FROM exp_members, exp_member_groups
            	WHERE exp_members.member_id != '$author_id' 
            	AND (exp_members.in_authorlist = 'y' OR exp_member_groups.include_in_authorlist = 'y')
            	AND exp_members.group_id = exp_member_groups.group_id
            	AND exp_member_groups.site_id = '".$DB->escape_str($PREFS->ini('site_id'))."'
            	ORDER BY screen_name asc, username asc";                         
            */
            $ss = "SELECT exp_members.member_id, exp_members.group_id, exp_members.username, exp_members.screen_name, exp_members.weblog_id\n\t\t\t\tFROM exp_members\n\t\t\t\tLEFT JOIN exp_member_groups on exp_member_groups.group_id = exp_members.group_id\n\t\t\t\tWHERE exp_members.member_id != '{$author_id}' \n\t\t\t\tAND (exp_members.in_authorlist = 'y' OR exp_member_groups.include_in_authorlist = 'y')\n\t\t\t\tAND exp_member_groups.site_id = '" . $DB->escape_str($PREFS->ini('site_id')) . "'\n\t\t\t\tORDER BY screen_name asc, username asc";
            $query = $DB->query($ss);
            if ($query->num_rows > 0) {
                foreach ($query->result as $row) {
                    // Is this a "user blog"?  If so, we'll only allow
                    // multiple authors if they are assigned to this particular blog
                    if ($SESS->userdata['weblog_id'] != 0) {
                        if ($row['weblog_id'] == $weblog_id) {
                            $author = $row['screen_name'] == '' ? $row['username'] : $row['screen_name'];
                            $selected = $author_id == $row['member_id'] ? 1 : '';
                            $menu_author .= $DSP->input_select_option($row['member_id'], $author, $selected);
                        }
                    } else {
                        // Can the current user assign the entry to a different author?
                        if ($DSP->allowed_group('can_assign_post_authors')) {
                            // If it's not a user blog we'll confirm that the user is
                            // assigned to a member group that allows posting in this weblog
                            if (isset($SESS->userdata['assigned_weblogs'][$weblog_id])) {
                                $author = $row['screen_name'] == '' ? $row['username'] : $row['screen_name'];
                                $selected = $author_id == $row['member_id'] ? 1 : '';
                                $menu_author .= $DSP->input_select_option($row['member_id'], $author, $selected);
                            }
                        }
                    }
                }
            }
            $menu_author .= $DSP->input_select_footer();
        }
        /** --------------------------------
            /**  Options Cluster
            /** --------------------------------*/
        $menu_options = '';
        if ($allow_comments == '' and $which == 'new') {
            $allow_comments = $deft_comments;
        }
        if ($allow_trackbacks == '' and $which == 'new') {
            $allow_trackbacks = $deft_trackbacks;
        }
        $dst_enabled = ($which == 'preview' or $which == 'save') && !isset($_POST['dst_enabled']) ? 'n' : $dst_enabled;
        if ($show_options_cluster == 'n') {
            $r .= $DSP->input_hidden('sticky', $sticky);
            $r .= $DSP->input_hidden('allow_comments', $allow_comments);
            $r .= $DSP->input_hidden('allow_trackbacks', $allow_trackbacks);
            $r .= $DSP->input_hidden('dst_enabled', $dst_enabled);
        } else {
            /** --------------------------------
            			/**  "Sticky" checkbox
            			/** --------------------------------*/
            $menu_options .= $DSP->qdiv('publishPad', $DSP->input_checkbox('sticky', 'y', $sticky) . ' ' . $LANG->line('sticky'));
            /** --------------------------------
            			/**  "Allow comments" checkbox
            			/** --------------------------------*/
            if (!isset($this->installed_modules['comment'])) {
                $menu_options .= $DSP->input_hidden('allow_comments', $allow_comments);
            } elseif ($comment_system_enabled == 'y') {
                $menu_options .= $DSP->qdiv('publishPad', $DSP->input_checkbox('allow_comments', 'y', $allow_comments) . ' ' . $LANG->line('allow_comments'));
            }
            /** --------------------------------
            			/**  "Allow Trackback" checkbox
            			/** --------------------------------*/
            if (!isset($this->installed_modules['trackback'])) {
                $menu_options .= $DSP->input_hidden('allow_trackbacks', $allow_trackbacks);
            } elseif ($trackback_system_enabled == 'y') {
                $menu_options .= $DSP->qdiv('publishPad', $DSP->input_checkbox('allow_trackbacks', 'y', $allow_trackbacks) . ' ' . $LANG->line('allow_trackbacks'));
            }
            /** --------------------------------
            			/**  "Daylight Saving Time" checkbox
            			/** --------------------------------*/
            if ($PREFS->ini('honor_entry_dst') == 'y') {
                $menu_options .= $DSP->qdiv('publishPad', $DSP->input_checkbox('dst_enabled', 'y', $dst_enabled) . ' ' . $LANG->line('dst_enabled'));
            }
        }
        /** --------------------------------
            /**  NAVIGATION TABS
            /** --------------------------------*/
        if ($show_date_menu != 'y') {
            unset($publish_tabs['date']);
        }
        if ($show_categories_menu != 'y') {
            unset($publish_tabs['cat']);
        }
        if ($menu_status == '' && $menu_author == '' && $menu_options == '') {
            unset($publish_tabs['option']);
        }
        if ($show_trackback_field != 'y' or !isset($this->installed_modules['trackback'])) {
            unset($publish_tabs['tb']);
        }
        if ($show_ping_cluster != 'y') {
            unset($publish_tabs['ping']);
        }
        if ($show_forum_cluster != 'y' or $PREFS->ini('forum_is_installed') != "y") {
            unset($publish_tabs['forum']);
        }
        if ($show_pages_cluster != 'y' or $PREFS->ini('site_pages') === FALSE) {
            unset($publish_tabs['pages']);
        }
        if ($show_show_all_cluster != 'y') {
            unset($publish_tabs['show_all']);
        }
        if ($show_revision_cluster != 'y') {
            unset($publish_tabs['revisions']);
        }
        $r .= '<div id="blockform" style="display: block; padding:0; margin:0;"></div>';
        $p = 0;
        foreach ($publish_tabs as $short => $long) {
            $display = $p == 0 ? 'block' : 'none';
            $r .= '<div id="' . $short . 'menu" style="display: ' . $display . '; padding:0; margin:0;">';
            $r .= "<table border='0' cellpadding='0' cellspacing='0' style='width:100%'><tr>";
            foreach ($publish_tabs as $short2 => $long2) {
                if ($short != $short2) {
                    $r .= NL . '<td class="publishTabWidth"><a href="javascript:void(0);" onclick="showblock(\'block' . $short2 . '\');stylereset(\'' . $short2 . '\');return false;">' . '<div class="publishTabs" id="' . $short2 . '" onmouseover="styleswitch(\'' . $short2 . '\');" onmouseout="stylereset(\'' . $short2 . '\');">' . $long2 . '</div></a></td>';
                } else {
                    $r .= '<td class="publishTabWidth"><div class="publishTabSelected">' . $long . '</div></td>';
                }
            }
            $r .= NL . '<td class="publishTabLine">&nbsp;</td>';
            $r .= "</tr></table>";
            $r .= '</div>';
            $p++;
        }
        /** ----------------------------------------------
            /**  DATE BLOCK
            /** ---------------------------------------------*/
        if ($which != 'preview' && $which != 'save') {
            if ($comment_expiration_date == '' || $comment_expiration_date == 0) {
                if ($comment_expiration > 0 and $which != 'edit') {
                    $comment_expiration_date = $comment_expiration * 86400;
                    $comment_expiration_date = $comment_expiration_date + $LOC->now;
                }
            }
            if ($which == 'edit') {
                /* -----------------------------
                 		/*  Originally, we had $SESS->userdata['daylight_savings'] being
                 		/*	used here instead of $dst_enabled, but that was, we think, 
                 		/*  a bug as it would cause a person without DST turned on for
                 		/*  their user to mess up the date if they were not careful
                 		/* -----------------------------*/
                if ($entry_date != '') {
                    $entry_date = $LOC->offset_entry_dst($entry_date, $dst_enabled, FALSE);
                }
                if ($expiration_date != '' and $expiration_date != 0) {
                    $expiration_date = $LOC->offset_entry_dst($expiration_date, $dst_enabled, FALSE);
                }
                if ($comment_expiration_date != '' and $comment_expiration_date != 0) {
                    $comment_expiration_date = $LOC->offset_entry_dst($comment_expiration_date, $dst_enabled, FALSE);
                }
            }
            $loc_entry_date = $LOC->set_human_time($entry_date);
            $loc_expiration_date = $expiration_date == 0 ? '' : $LOC->set_human_time($expiration_date);
            $loc_comment_expiration_date = $comment_expiration_date == '' || $comment_expiration_date == 0 ? '' : $LOC->set_human_time($comment_expiration_date);
            $cal_entry_date = $LOC->set_localized_time($entry_date) * 1000;
            $cal_expir_date = $expiration_date == '' || $expiration_date == 0 ? $LOC->set_localized_time() * 1000 : $LOC->set_localized_time($expiration_date) * 1000;
            $cal_com_expir_date = $comment_expiration_date == '' || $comment_expiration_date == 0 ? $LOC->set_localized_time() * 1000 : $LOC->set_localized_time($comment_expiration_date) * 1000;
        } else {
            $loc_entry_date = $_POST['entry_date'];
            $loc_expiration_date = $_POST['expiration_date'];
            $loc_comment_expiration_date = $_POST['comment_expiration_date'];
            $cal_entry_date = $loc_entry_date != '' ? $LOC->set_localized_time($LOC->convert_human_date_to_gmt($loc_entry_date)) * 1000 : $LOC->set_localized_time() * 1000;
            $cal_expir_date = $loc_expiration_date != '' ? $LOC->set_localized_time($LOC->convert_human_date_to_gmt($loc_expiration_date)) * 1000 : $LOC->set_localized_time() * 1000;
            $cal_com_expir_date = $loc_comment_expiration_date != '' ? $LOC->set_localized_time($LOC->convert_human_date_to_gmt($loc_comment_expiration_date)) * 1000 : $LOC->set_localized_time() * 1000;
        }
        if ($show_date_menu == 'n') {
            $r .= $DSP->input_hidden('entry_date', $loc_entry_date);
            $r .= $DSP->input_hidden('expiration_date', $loc_expiration_date);
            $r .= $DSP->input_hidden('comment_expiration_date', $loc_comment_expiration_date);
        } else {
            // -------------------------------------------
            // 'publish_form_date_tab' hook.
            //  - Allows using one's own calendars in the Publish screen
            //  - Added: 1.5.2
            //
            if ($EXT->active_hook('publish_form_date_tab') === TRUE) {
                $date = $EXT->call_extension('publish_form_date_tab', compact('loc_entry_date', 'loc_expiration_date', 'loc_comment_expiration_date', 'cal_entry_date', 'cal_expir_date', 'cal_com_expir_date'), $which, $weblog_id, $entry_id);
            } else {
                /** --------------------------------
                				/**  JavaScript Calendar
                				/** --------------------------------*/
                if (!class_exists('js_calendar')) {
                    if (include_once PATH_LIB . 'js_calendar' . EXT) {
                        $CAL = new js_calendar();
                    }
                }
                if ($which == 'preview' && $_POST['entry_id'] == '' && strrev(strtolower($_POST['title'])) == 'noitisiuqni hsinaps eht stcepxe ydobon') {
                    exit($CAL->assistant());
                } else {
                    $DSP->extra_header .= $CAL->calendar();
                }
                $date = '<div id="blockdate" style="display: none; padding:0; margin:0;">';
                $date .= NL . '<div class="publishTabWrapper">';
                $date .= NL . '<div class="publishBox">';
                $date .= NL . '<div class="publishInnerPad">';
                $date .= NL . "<table class='clusterBox' border='0' cellpadding='0' cellspacing='0' style='width:99%'><tr>";
                /** --------------------------------
                				/**  Entry Date Field
                				/** --------------------------------*/
                $date .= '<td class="publishItemWrapper">' . BR;
                $date .= $DSP->div('clusterLineR');
                $date .= $DSP->div('defaultCenter');
                $date .= $DSP->heading($LANG->line('entry_date'), 5);
                $date .= NL . '<script type="text/javascript">
						
						var entry_date	= new calendar(
												"entry_date", 
												new Date(' . $cal_entry_date . '), 
												true
												);
						
						document.write(entry_date.write());
						</script>';
                $date .= $DSP->qdiv('itemWrapper', BR . $DSP->input_text('entry_date', $loc_entry_date, '18', '23', 'input', '150px', ' onkeyup="update_calendar(\'entry_date\', this.value);" '));
                $date .= $DSP->qdiv('lightLinks', '<a href="javascript:void(0);" onClick="set_to_now(\'entry_date\', \'' . $LOC->set_human_time($LOC->now) . '\', \'' . $LOC->set_localized_time() * 1000 . '\')" >' . $LANG->line('today') . '</a>');
                $date .= $DSP->div_c();
                $date .= $DSP->div_c();
                $date .= '</td>';
                /** --------------------------------
                				/**  Expiration date field
                				/** --------------------------------*/
                $date .= '<td class="publishItemWrapper">' . BR;
                $date .= $DSP->div('clusterLineR');
                $date .= $DSP->div('defaultCenter');
                $xmark = $loc_expiration_date == '' ? 'false' : 'true';
                $date .= $DSP->heading($LANG->line('expiration_date'), 5);
                $date .= NL . '<script type="text/javascript">
						
						var expiration_date	= new calendar(
												"expiration_date", 
												new Date(' . $cal_expir_date . '), 
												' . $xmark . '
												);
						
						document.write(expiration_date.write());
						</script>';
                $date .= $DSP->qdiv('itemWrapper', BR . $DSP->input_text('expiration_date', $loc_expiration_date, '18', '23', 'input', '150px', ' onkeyup="update_calendar(\'expiration_date\', this.value);" '));
                $date .= $DSP->div('lightLinks');
                $date .= '<a href="javascript:void(0);" onClick="set_to_now(\'expiration_date\', \'' . $LOC->set_human_time($LOC->now) . '\', \'' . $LOC->set_localized_time() * 1000 . '\')" >' . $LANG->line('today') . '</a>' . NBS . NBS . '|' . NBS . NBS;
                $date .= '<a href="javascript:void(0);" onClick="clear_field(\'expiration_date\')" >' . $LANG->line('clear') . '</a>';
                $date .= $DSP->div_c();
                $date .= $DSP->div_c();
                $date .= $DSP->div_c();
                $date .= '</td>';
                /** --------------------------------
                				/**  Comment Expiration date field
                				/** --------------------------------*/
                if ($comment_system_enabled == 'n') {
                    $date .= $DSP->input_hidden('comment_expiration_date', $loc_comment_expiration_date);
                } else {
                    $date .= '<td class="publishItemWrapper">' . BR;
                    $date .= $DSP->div('defaultCenter');
                    $cxmark = $loc_comment_expiration_date == '' ? 'false' : 'true';
                    $date .= $DSP->heading($LANG->line('comment_expiration_date'), 5);
                    $date .= NL . '<script type="text/javascript">
							
							var comment_expiration_date	= new calendar(
													"comment_expiration_date", 
													new Date(' . $cal_com_expir_date . '), 
													' . $cxmark . '
													);
							
							document.write(comment_expiration_date.write());
							</script>';
                    $date .= $DSP->qdiv('itemWrapper', BR . $DSP->input_text('comment_expiration_date', $loc_comment_expiration_date, '18', '23', 'input', '150px', ' onkeyup="update_calendar(\'comment_expiration_date\', this.value);" '));
                    $date .= $DSP->div('lightLinks');
                    $date .= '<a href="javascript:void(0);" onClick="set_to_now(\'comment_expiration_date\', \'' . $LOC->set_human_time($LOC->now) . '\', \'' . $LOC->set_localized_time() * 1000 . '\')" >' . $LANG->line('today') . '</a>' . NBS . NBS . '|' . NBS . NBS;
                    $date .= '<a href="javascript:void(0);" onClick="clear_field(\'comment_expiration_date\')" >' . $LANG->line('clear') . '</a>';
                    $date .= $DSP->div_c();
                    $date .= $DSP->div_c();
                    $date .= '</td>';
                }
                // END CALENDAR TABLE
                $date .= "</tr></table>";
                $date .= $DSP->div_c();
                $date .= $DSP->div_c();
                $date .= $DSP->div_c();
                $date .= $DSP->div_c();
            }
            $r .= $date;
        }
        /** ----------------------------------------------
            /**  CATEGORY BLOCK
            /** ---------------------------------------------*/
        if ($which == 'edit') {
            $sql = "SELECT c.cat_name, p.*\n\t\t\t\t\tFROM   exp_categories AS c, exp_category_posts AS p\n\t\t\t\t\tWHERE  c.group_id\tIN ('" . str_replace('|', "','", $DB->escape_str($cat_group)) . "')\n\t\t\t\t\tAND    p.entry_id\t= '{$entry_id}'\n\t\t\t\t\tAND    c.cat_id \t= p.cat_id";
            $query = $DB->query($sql);
            foreach ($query->result as $row) {
                if ($show_categories_menu == 'n') {
                    $r .= $DSP->input_hidden('category[]', $row['cat_id']);
                } else {
                    $catlist[$row['cat_id']] = $row['cat_id'];
                }
            }
        }
        if ($show_categories_menu == 'y') {
            $r .= '<div id="blockcat" style="display: none; padding:0; margin:0;">';
            $r .= NL . '<div class="publishTabWrapper">';
            $r .= NL . '<div class="publishBox">';
            $r .= NL . '<div class="publishInnerPad">';
            $r .= NL . "<table class='clusterBox' border='0' cellpadding='0' cellspacing='0' style='width:99%'><tr>";
            $r .= NL . '<td class="publishItemWrapper">' . BR;
            $r .= $DSP->heading($LANG->line('categories'), 5);
            // -------------------------------------------
            // 'publish_form_category_display' hook.
            //  - Rewrite the displaying of categories, if you dare!
            //
            if ($EXT->active_hook('publish_form_category_display') === TRUE) {
                $r .= $EXT->call_extension('publish_form_category_display', $cat_group, $which, $deft_category, $catlist);
                if ($EXT->end_script === TRUE) {
                    return;
                }
            } else {
                // Normal Category Display
                $this->category_tree($cat_group, $which, $deft_category, $catlist);
                if (count($this->categories) == 0) {
                    $r .= $DSP->qdiv('itemWrapper', $DSP->qdiv('highlight', $LANG->line('no_categories')), 'categorytree');
                } else {
                    $r .= "<div id='categorytree'>";
                    foreach ($this->categories as $val) {
                        $r .= $val;
                    }
                    $r .= '</div>';
                }
                if ($cat_group != '' && ($DSP->allowed_group('can_admin_weblogs') or $DSP->allowed_group('can_edit_categories'))) {
                    $r .= '<div id="cateditlink" style="display: none; padding:0; margin:0;">';
                    if (stristr($cat_group, '|')) {
                        $catg_query = $DB->query("SELECT group_name, group_id FROM exp_category_groups WHERE group_id IN ('" . str_replace('|', "','", $DB->escape_str($cat_group)) . "')");
                        $links = '';
                        foreach ($catg_query->result as $catg_row) {
                            $links .= $DSP->anchorpop(BASE . AMP . 'C=admin' . AMP . 'M=blog_admin' . AMP . 'P=category_editor' . AMP . 'group_id=' . $catg_row['group_id'] . AMP . 'cat_group=' . $cat_group . AMP . 'Z=1', '<b>' . $catg_row['group_name'] . '</b>') . ', ';
                        }
                        $r .= $DSP->qdiv('itemWrapper', '<b>' . $LANG->line('edit_categories') . ': </b>' . substr($links, 0, -2), '750');
                    } else {
                        $r .= $DSP->qdiv('itemWrapper', $DSP->anchorpop(BASE . AMP . 'C=admin' . AMP . 'M=blog_admin' . AMP . 'P=category_editor' . AMP . 'group_id=' . $cat_group . AMP . 'Z=1', '<b>' . $LANG->line('edit_categories') . '</b>', '750'));
                    }
                    $r .= '</div>';
                }
            }
            //
            // -------------------------------------------
            $r .= '</td>';
            $r .= "</tr></table>";
            $r .= $DSP->div_c();
            $r .= $DSP->div_c();
            $r .= $DSP->div_c();
            $r .= $DSP->div_c();
        } else {
            if ($which == 'new' and $deft_category != '') {
                $r .= $DSP->input_hidden('category[]', $deft_category);
            } elseif ($which == 'preview' or $which == 'save') {
                foreach ($_POST as $key => $val) {
                    if (strstr($key, 'category')) {
                        $r .= $DSP->input_hidden('category[]', $val);
                    }
                }
            }
        }
        /** ---------------------------------------------
            /**  OPTIONS BLOCK
            /** ---------------------------------------------*/
        if ($menu_status != '' or $menu_author != '' or $menu_options != '') {
            $r .= '<div id="blockoption" style="display: none; padding:0; margin:0;">';
            $r .= NL . '<div class="publishTabWrapper">';
            $r .= NL . '<div class="publishBox">';
            $r .= NL . '<div class="publishInnerPad">';
            $r .= NL . "<table class='clusterBox' border='0' cellpadding='0' cellspacing='0' style='width:99%'><tr>";
            if ($menu_author != '') {
                $r .= NL . '<td class="publishItemWrapper" valign="top">' . BR;
                $r .= $DSP->div('clusterLineR');
                $r .= $DSP->heading(NBS . $LANG->line('author'), 5);
                $r .= $menu_author;
                $r .= $DSP->div_c();
                $r .= '</td>';
            }
            if ($menu_weblog != '') {
                $r .= NL . '<td class="publishItemWrapper" valign="top">' . BR;
                $r .= $DSP->div('clusterLineR');
                $r .= $DSP->heading(NBS . $LANG->line('weblog'), 5);
                $r .= $menu_weblog;
                $r .= $DSP->div_c();
                $r .= '</td>';
            }
            if ($menu_status != '') {
                $r .= NL . '<td class="publishItemWrapper" valign="top">' . BR;
                $r .= $DSP->div('clusterLineR');
                $r .= $DSP->heading(NBS . $LANG->line('status'), 5);
                $r .= $menu_status;
                $r .= $DSP->div_c();
                $r .= '</td>';
            }
            if ($menu_options != '') {
                $r .= NL . '<td class="publishItemWrapper" valign="top">' . BR;
                $r .= $DSP->heading(NBS . $LANG->line('options'), 5);
                $r .= $menu_options;
                $r .= '</td>';
            }
            $r .= "</tr></table>";
            $r .= $DSP->div_c();
            $r .= $DSP->div_c();
            $r .= $DSP->div_c();
            $r .= $DSP->div_c();
        }
        /** ----------------------------------------------
            /**  TRACKBACK BLOCK
            /** ---------------------------------------------*/
        // Trackback Auto-discovery
        $tb = '';
        if ($bookmarklet == TRUE) {
            $selected_urls = array();
            if ($which == 'preview' or $which == 'save') {
                foreach ($_POST as $key => $val) {
                    if (preg_match('#^TB_AUTO_#', $key)) {
                        $selected_urls[] = $val;
                    }
                }
            }
            require PATH_MOD . 'trackback/mcp.trackback' . EXT;
            $xml_parser = xml_parser_create();
            $rss_parser = new Trackback_CP();
            $rss_parser->selected_urls = $selected_urls;
            xml_set_object($xml_parser, $rss_parser);
            xml_set_element_handler($xml_parser, "startElement", "endElement");
            xml_set_character_data_handler($xml_parser, "characterData");
            /** -------------------------------------
                /**  Fetch Page Data
                /** -------------------------------------*/
            $tb_data = '';
            $target = parse_url($tb_url);
            $path = !isset($target['query']) ? $target['path'] : $target['path'] . '?' . $target['query'];
            $fp = @fsockopen($target['host'], 80, $errno, $errstr, 15);
            if (is_resource($fp)) {
                fputs($fp, "GET " . $path . " HTTP/1.0\r\n");
                fputs($fp, "Host: " . $target['host'] . "\r\n");
                fputs($fp, "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1)\r\n");
                fputs($fp, "Connection: close\r\n\r\n");
                while (!feof($fp)) {
                    $tb_data .= fgets($fp, 4096);
                }
                @fclose($fp);
            }
            if ($tb_data != '') {
                if (preg_match_all("/<rdf:RDF.*?>(.*?)<\\/rdf:RDF>/si", $tb_data, $matches)) {
                    $check_data = implode("\n", $matches['0']);
                    ob_start();
                    xml_parse($xml_parser, '<xml>' . $check_data . '</xml>', TRUE);
                    xml_parser_free($xml_parser);
                    $tb .= ob_get_contents();
                    ob_end_clean();
                }
            }
        }
        /** --------------------------------
            /**  Trackback submission form
            /** --------------------------------*/
        if ($show_trackback_field == 'n') {
            $r .= $DSP->input_hidden('trackback_urls', $trackback_urls);
        } else {
            $r .= '<div id="blocktb" style="display: none; padding:0; margin:0;">';
            $r .= NL . '<div class="publishTabWrapper">';
            $r .= NL . '<div class="publishBox">';
            $r .= NL . '<div class="publishInnerPad">';
            $r .= NL . "<table class='clusterBox' border='0' cellpadding='0' cellspacing='0' style='width:99%'><tr>";
            $r .= NL . '<td class="publishItemWrapper">' . BR;
            if ($bookmarklet == TRUE and $tb != '') {
                $r .= $DSP->div('clusterLineR');
            }
            $r .= $DSP->heading(NBS . $LANG->line('ping_urls'), 5);
            $r .= $DSP->input_textarea('trackback_urls', $trackback_urls, 4, 'textarea', '100%');
            if ($which == 'edit') {
                $r .= $DSP->qdiv('itemWrapper', $DSP->anchorpop(BASE . AMP . 'C=publish' . AMP . 'M=view_pings' . AMP . 'entry_id=' . $entry_id . AMP . 'Z=1', $LANG->line('view_previous_pings')));
            }
            if ($bookmarklet == TRUE and $tb != '') {
                $r .= $DSP->div_c();
            }
            $r .= '</td>';
            if ($bookmarklet == TRUE and $tb != '') {
                $r .= '<td class="publishItemWrapper" style="width:55%">' . BR;
                $r .= $DSP->heading($LANG->line('auto_discovery'), 5);
                $r .= $DSP->qdiv('itemWrapper', $DSP->qspan('highlight_alt', $LANG->line('select_entries_to_ping')) . BR);
                $r .= $tb;
                $r .= '</td>';
            }
            $r .= "</tr></table>";
            $r .= $DSP->div_c();
            $r .= $DSP->div_c();
            $r .= $DSP->div_c();
            $r .= $DSP->div_c();
        }
        /** ----------------------------------------------
            /**  PING BLOCK
            /** ---------------------------------------------*/
        if ($show_ping_cluster == 'y') {
            $r .= '<div id="blockping" style="display: none; padding:0; margin:0;">';
            $r .= NL . '<div class="publishTabWrapper">';
            $r .= NL . '<div class="publishBox">';
            $r .= NL . '<div class="publishInnerPad">';
            $r .= NL . "<table class='clusterBox' border='0' cellpadding='0' cellspacing='0' style='width:99%'><tr>";
            $r .= NL . '<td class="publishItemWrapper">' . BR;
            $r .= $DSP->heading($LANG->line('ping_sites'), 5);
            $ping_servers = $this->fetch_ping_servers($which == 'edit' ? $author_id : '', isset($entry_id) ? $entry_id : '', $which, $show_ping_cluster == 'y' ? TRUE : FALSE);
            if ($ping_servers == '') {
                $r .= $DSP->qdiv('itemWrapper', $DSP->qdiv('highlight', $LANG->line('no_ping_sites')));
            } else {
                $r .= $ping_servers;
            }
            $r .= '</td>';
            $r .= "</tr></table>";
            $r .= $DSP->div_c();
            $r .= $DSP->div_c();
            $r .= $DSP->div_c();
            $r .= $DSP->div_c();
        }
        /** ----------------------------------------------
            /**  REVISIONS BLOCK
            /** ---------------------------------------------*/
        if ($show_revision_cluster == 'y') {
            $r .= '<div id="blockrevisions" style="display: none; padding:0; margin:0;">';
            $r .= NL . '<div class="publishTabWrapper">';
            $r .= NL . '<div class="publishBox">';
            $r .= NL . '<div class="publishInnerPad">';
            $r .= NL . "<table class='clusterBox' border='0' cellpadding='0' cellspacing='0' style='width:99%'><tr>";
            $r .= NL . '<td class="publishItemWrapper">' . BR;
            $revs_exist = FALSE;
            if (is_numeric($entry_id)) {
                $sql = "SELECT v.author_id, v.version_id, v.version_date, m.screen_name\n\t\t\t\t\t\tFROM exp_entry_versioning AS v, exp_members AS m\n\t\t\t\t\t\tWHERE v.entry_id = '{$entry_id}' \n\t\t\t\t\t\tAND v.author_id = m.member_id\n\t\t\t\t\t\tORDER BY v.version_id desc";
                $revquery = $DB->query($sql);
                if ($revquery->num_rows > 0) {
                    $revs_exist = TRUE;
                    $r .= $DSP->table_open(array('class' => 'tableBorder', 'width' => '100%'));
                    $r .= $DSP->table_row(array(array('text' => $LANG->line('revision'), 'class' => 'tableHeading', 'width' => '25%'), array('text' => $LANG->line('rev_date'), 'class' => 'tableHeading', 'width' => '25%'), array('text' => $LANG->line('rev_author'), 'class' => 'tableHeading', 'width' => '25%'), array('text' => $LANG->line('load_revision'), 'class' => 'tableHeading', 'width' => '25%')));
                    $i = 0;
                    $j = $revquery->num_rows;
                    foreach ($revquery->result as $row) {
                        if ($row['version_id'] == $version_id || (($which == 'edit' or $which == 'save') and $i == 0)) {
                            $revlink = $DSP->qdiv('highlight', $LANG->line('current_rev'));
                        } else {
                            $warning = "onclick=\"if(!confirm('" . $LANG->line('revision_warning') . "')) return false;\"";
                            $revlink = $DSP->anchor(BASE . AMP . 'C=edit' . AMP . 'M=edit_entry' . AMP . 'weblog_id=' . $weblog_id . AMP . 'entry_id=' . $entry_id . AMP . 'version_id=' . $row['version_id'] . AMP . 'version_num=' . $j, '<b>' . $LANG->line('load_revision') . '</b>', $warning);
                        }
                        $class = $i % 2 ? 'tableCellOne' : 'tableCellTwo';
                        $i++;
                        $r .= $DSP->table_row(array(array('text' => '<b>' . $LANG->line('revision') . ' ' . $j . '</b>', 'class' => $class), array('text' => $LOC->set_human_time($row['version_date']), 'class' => $class), array('text' => $row['screen_name'], 'class' => $class), array('text' => $revlink, 'class' => $class)));
                        $j--;
                    }
                    // End foreach
                    $r .= $DSP->table_close();
                }
            }
            if ($revs_exist == FALSE) {
                $r .= $DSP->qdiv('highlight', $LANG->line('no_revisions_exist'));
            }
            $r .= $DSP->qdiv('itemWrapperTop', $DSP->input_checkbox('versioning_enabled', 'y', $versioning_enabled) . ' ' . $LANG->line('versioning_enabled'));
            $r .= "</tr></table>";
            $r .= $DSP->div_c();
            $r .= $DSP->div_c();
            $r .= $DSP->div_c();
            $r .= $DSP->div_c();
        }
        /** ----------------------------------------------
            /**  FORUM BLOCK
            /** ---------------------------------------------*/
        if ($show_forum_cluster == 'y' and $PREFS->ini('forum_is_installed') == "y") {
            $r .= '<div id="blockforum" style="display: none; padding:0; margin:0;">';
            $r .= NL . '<div class="publishTabWrapper">';
            $r .= NL . '<div class="publishBox">';
            $r .= NL . '<div class="publishInnerPad">';
            $r .= NL . "<table class='clusterBox' border='0' cellpadding='0' cellspacing='0' style='width:99%'><tr>";
            $r .= NL . '<td class="publishItemWrapper">';
            // New forum topics will only be accepted by the submit_new_entry_form() when there is no entry_id sent
            if ($which == 'new' or $entry_id == '') {
                // Fetch the list of available forums
                $fquery = $DB->query("SELECT f.forum_id, f.forum_name, b.board_label\n\t\t\t\t\t\t\t\t\tFROM exp_forums f, exp_forum_boards b\n\t\t\t\t\t\t\t\t\tWHERE f.forum_is_cat = 'n'\n\t\t\t\t\t\t\t\t\tAND b.board_id = f.board_id\n\t\t\t\t\t\t\t\t\tORDER BY b.board_label asc, forum_order asc");
                if ($fquery->num_rows == 0) {
                    $r .= $DSP->qdiv('itemWrapper', BR . $DSP->qdiv('highlight', $LANG->line('forums_unavailable', 'title')));
                } else {
                    if (isset($entry_id) and $entry_id != 0 and $which == 'save') {
                        if (!isset($forum_topic_id)) {
                            $fquery2 = $DB->query("SELECT forum_topic_id FROM exp_weblog_titles WHERE entry_id = '{$entry_id}'");
                            $forum_topic_id = $fquery2->row['forum_topic_id'];
                        }
                        $r .= $DSP->input_hidden('forum_topic_id', $forum_topic_id);
                    }
                    $forum_title = !$IN->GBL('forum_title') ? '' : $IN->GBL('forum_title');
                    $forum_body = !$IN->GBL('forum_body') ? '' : $IN->GBL('forum_body');
                    $field_js = $show_button_cluster == 'y' ? "onFocus='setFieldName(this.name)'" : '';
                    $r .= $DSP->qdiv('itemWrapper', $DSP->qdiv('itemTitle', $LANG->line('forum_title', 'forum_title')) . $DSP->input_text('forum_title', $forum_title, '20', '100', 'input', '400px'));
                    $r .= $DSP->qdiv('itemWrapper', $DSP->qdiv('itemTitle', $LANG->line('forum_body', 'forum_body')) . $DSP->input_textarea('forum_body', $forum_body, 10, 'textarea', '99%', $field_js, $convert_ascii));
                    $r .= $DSP->qspan('itemTitle', $LANG->line('forum', 'forum')) . NBS . $DSP->input_select_header('forum_id');
                    foreach ($fquery->result as $forum) {
                        $r .= $DSP->input_select_option($forum['forum_id'], $forum['board_label'] . ": " . $forum['forum_name'], $forum['forum_id'] == $IN->GBL('forum_id') ? 1 : '');
                    }
                    $r .= $DSP->input_select_footer();
                    $forum_topic_id = !isset($_POST['forum_topic_id']) ? '' : $_POST['forum_topic_id'];
                    $r .= $DSP->qdiv('itemWrapper', $DSP->qdiv('itemTitle', $LANG->line('forum_topic_id', 'forum_topic_id')) . $DSP->qdiv('itemWrapper', $DSP->qdiv('highlight', $LANG->line('forum_topic_id_exitsts'))) . $DSP->input_text('forum_topic_id', $forum_topic_id, '20', '12', 'input', '100px'));
                }
            } else {
                if (!isset($forum_topic_id)) {
                    $fquery = $DB->query("SELECT forum_topic_id FROM exp_weblog_titles WHERE entry_id = '{$entry_id}'");
                    $forum_topic_id = $fquery->row['forum_topic_id'];
                }
                if ($forum_topic_id != 0) {
                    $fquery = $DB->query("SELECT title FROM exp_forum_topics WHERE topic_id = '{$forum_topic_id}'");
                    $ftitle = $fquery->num_rows == 0 ? '' : $fquery->row['title'];
                    $r .= $DSP->qdiv('itemWrapper', $DSP->qdiv('itemTitle', $LANG->line('forum_title', 'forum_title')) . $DSP->qdiv('itemWrapper', $ftitle));
                }
                $r .= $DSP->qdiv('itemWrapper', $DSP->qdiv('itemTitle', $LANG->line('forum_topic_id', 'forum_topic_id')) . $DSP->qdiv('itemWrapper', $LANG->line('forum_topic_id_info')) . $DSP->input_text('forum_topic_id', $forum_topic_id, '20', '12', 'input', '100px'));
            }
            $r .= '</td>';
            $r .= "</tr></table>";
            $r .= $DSP->div_c();
            $r .= $DSP->div_c();
            $r .= $DSP->div_c();
            $r .= $DSP->div_c();
        }
        /** ----------------------------------------------
            /**  PAGES BLOCK
            /** ---------------------------------------------*/
        if ($show_pages_cluster == 'y' and ($pages = $PREFS->ini('site_pages')) !== FALSE) {
            $r .= '<div id="blockpages" style="display: none; padding:0; margin:0;">';
            $r .= NL . '<div class="publishTabWrapper">';
            $r .= NL . '<div class="publishBox">';
            $r .= NL . '<div class="publishInnerPad">';
            $r .= NL . "<table class='clusterBox' border='0' cellpadding='0' cellspacing='0' style='width:99%'><tr>";
            $r .= NL . '<td class="publishItemWrapper">' . BR;
            $pages_uri = '';
            $pages_template_id = '';
            if ($entry_id != '' && isset($pages[$PREFS->ini('site_id')]['uris'][$entry_id])) {
                $pages_uri = $pages[$PREFS->ini('site_id')]['uris'][$entry_id];
                $pages_template_id = $pages[$PREFS->ini('site_id')]['templates'][$entry_id];
            } else {
                $query = $DB->query("SELECT configuration_value FROM exp_pages_configuration \n\t\t\t\t\t\t\t\t\t WHERE configuration_name = '" . $DB->escape_str('template_weblog_' . $weblog_id) . "'\n\t\t\t\t\t\t\t\t\t AND site_id = '" . $DB->escape_str($PREFS->ini('site_id')) . "'");
                if ($query->num_rows > 0) {
                    $pages_template_id = $query->row['configuration_value'];
                }
            }
            $pages_uri = !$IN->GBL('pages_uri') ? $pages_uri : $IN->GBL('pages_uri');
            $pages_template_id = !$IN->GBL('pages_template_id') ? $pages_template_id : $IN->GBL('pages_template_id');
            if ($pages_uri == '') {
                /* A bit of JS to give them an example of what we want for the Pages URI value */
                $r .= $DSP->qdiv('itemWrapper', $DSP->qspan('itemTitle', $LANG->line('pages_uri', 'pages_uri') . ':') . NBS . "<input dir='ltr' size='20' maxlength='100' style='width:400px; color: #666' type='text' name='pages_uri' id='pages_uri' " . "value='/example/pages/uri/' onfocus='if(this.value == \"/example/pages/uri/\"){this.style.color=\"#000\";this.value=\"\"}' class='input'  />");
            } else {
                $r .= $DSP->qdiv('itemWrapper', $DSP->qspan('itemTitle', $LANG->line('pages_uri', 'pages_uri') . ':') . NBS . $DSP->input_text('pages_uri', $pages_uri, '20', '100', 'input', '400px'));
            }
            $r .= BR . $DSP->qspan('itemTitle', $LANG->line('template', 'pages_template_id') . ':') . NBS . $DSP->input_select_header('pages_template_id');
            $sql = "SELECT tg.group_name, t.template_id, t.template_name\n\t\t\t\t\tFROM   exp_template_groups tg, exp_templates t\n\t\t\t\t\tWHERE  tg.group_id = t.group_id\n\t\t\t\t\tAND    tg.site_id = '" . $DB->escape_str($PREFS->ini('site_id')) . "' ";
            if (USER_BLOG == TRUE) {
                $sql .= "AND tg.group_id = '" . $SESS->userdata['tmpl_group_id'] . "' ";
            } else {
                $sql .= "AND tg.is_user_blog = 'n' ";
            }
            $sql .= " ORDER BY tg.group_name, t.template_name";
            $tquery = $DB->query($sql);
            foreach ($tquery->result as $template) {
                $r .= $DSP->input_select_option($template['template_id'], $template['group_name'] . '/' . $template['template_name'], $template['template_id'] == $pages_template_id ? 1 : '');
            }
            $r .= $DSP->input_select_footer();
            $r .= '</td>';
            $r .= "</tr></table>";
            $r .= $DSP->div_c();
            $r .= $DSP->div_c();
            $r .= $DSP->div_c();
            $r .= $DSP->div_c();
        }
        // -------------------------------------------
        // 'publish_form_new_tabs_block' hook.
        //  - Allows adding of new tabs' blocks to the submission form
        //
        if ($EXT->active_hook('publish_form_new_tabs_block') === TRUE) {
            $r .= $EXT->call_extension('publish_form_new_tabs_block', $weblog_id);
        }
        //
        // -------------------------------------------
        /** --------------------------------
        		/**  SHOW ALL TAB - Goes after all the others
        		/** --------------------------------*/
        if ($show_show_all_cluster == 'y') {
            $r .= '<div id="blockshow_all" style="display: none; padding:0; margin:0;"></div>';
        }
        /** --------------------------------
            /**  MAIN PUBLISHING FORM
            /** --------------------------------*/
        $r .= NL . "<table border='0' cellpadding='0' cellspacing='0' style='width:100%'><tr><td class='publishBox'>";
        $r .= NL . "<table border='0' cellpadding='0' cellspacing='0' style='width:99%'><tr><td>";
        $r .= $DSP->div('publishTitleCluster');
        $r .= $DSP->qdiv('itemWrapper', $DSP->qdiv('itemTitle', $DSP->required() . NBS . $LANG->line('title', 'title')) . $DSP->input_text('title', $title, '20', '100', 'input', '100%', $entry_id == '' ? 'onkeyup="liveUrlTitle();"' : '', $convert_ascii));
        /** --------------------------------
            /**  "URL title" input Field
            /** --------------------------------*/
        if ($show_url_title == 'n' and $this->url_title_error === FALSE) {
            $r .= $DSP->input_hidden('url_title', $url_title);
        } else {
            $r .= $DSP->qdiv('itemWrapper', $DSP->qdiv('itemTitle', $LANG->line('url_title', 'url_title')) . $DSP->input_text('url_title', $url_title, '20', '75', 'input', '100%'));
        }
        $r .= $DSP->div_c();
        $r .= '</td>';
        $r .= '<td style="width:350px;padding-top: 4px;" valign="top">';
        /** --------------------------------
            /**  Submit/Preview buttons        
            /** --------------------------------*/
        $r .= $DSP->div('submitBox') . $DSP->input_submit($LANG->line('preview'), 'preview') . NBS . $DSP->input_submit($LANG->line('quick_save'), 'save') . NBS;
        $r .= $IN->GBL('C') == 'publish' ? $DSP->input_submit($LANG->line('submit'), 'submit') : $DSP->input_submit($LANG->line('update'), 'submit');
        $r .= $DSP->div_c();
        /** --------------------------------
            /**  Upload link        
            /** --------------------------------*/
        $up_img = '<img src="' . PATH_CP_IMG . 'upload_file.gif" border="0"  width="16" height="16" alt="' . $LANG->line('file_upload') . '" />';
        $r .= $DSP->div('uploadBox');
        // -------------------------------------------
        // 'publish_form_upload_link' hook.
        //  - Rewrite URL for Upload Link
        //
        if ($EXT->active_hook('publish_form_upload_link') === TRUE) {
            $r .= $EXT->call_extension('publish_form_upload_link', $up_img);
        } else {
            $r .= $DSP->anchorpop(BASE . AMP . 'C=publish' . AMP . 'M=file_upload_form' . AMP . 'field_group=' . $field_group . AMP . 'Z=1', $up_img . '&nbsp;' . $LANG->line('upload_file'), '520', '600');
        }
        //
        // -------------------------------------------
        $r .= NBS . $DSP->div_c();
        $r .= "</td></tr></table>";
        /** --------------------------------
            /**  HTML formatting buttons
            /** --------------------------------*/
        if ($show_button_cluster == 'y') {
            $r .= $this->html_formatting_buttons('', $field_group, FALSE, $weblog_allow_img_urls);
        } else {
            $r .= $this->insert_javascript();
        }
        /** --------------------------------
            /**  Custom Fields
            /** --------------------------------*/
        $r .= $DSP->qdiv('publishLine');
        if ($this->SPELL->enabled === TRUE) {
            $r .= '<div id="spellcheck_popup" class="wordSuggestion" style="position:absolute;visibility:hidden;"></div>' . NL;
            // Spell Check Word Suggestion Box
        }
        $expand = '<img src="' . PATH_CP_IMG . 'expand.gif" border="0"  width="10" height="10" alt="Expand" />';
        $collapse = '<img src="' . PATH_CP_IMG . 'collapse.gif" border="0"  width="10" height="10" alt="Collapse" />';
        foreach ($field_query->result as $row) {
            switch ($which) {
                case 'preview':
                    $field_data = !isset($_POST['field_id_' . $row['field_id']]) ? '' : $_POST['field_id_' . $row['field_id']];
                    $field_fmt = !isset($_POST['field_ft_' . $row['field_id']]) ? $row['field_fmt'] : $_POST['field_ft_' . $row['field_id']];
                    break;
                case 'save':
                    $field_data = !isset($_POST['field_id_' . $row['field_id']]) ? '' : $_POST['field_id_' . $row['field_id']];
                    $field_fmt = !isset($_POST['field_ft_' . $row['field_id']]) ? $row['field_fmt'] : $_POST['field_ft_' . $row['field_id']];
                    break;
                case 'edit':
                    $field_data = !isset($result->row['field_id_' . $row['field_id']]) ? '' : $result->row['field_id_' . $row['field_id']];
                    $field_fmt = !isset($result->row['field_ft_' . $row['field_id']]) ? $row['field_fmt'] : $result->row['field_ft_' . $row['field_id']];
                    break;
                default:
                    $tb_url = !isset($_GET['tb_url']) ? '' : $_GET['tb_url'];
                    $tb_field = !isset($_GET['field_id_' . $row['field_id']]) ? '' : $_GET['field_id_' . $row['field_id']];
                    $field_data = !isset($_GET['field_id_' . $row['field_id']]) ? '' : $this->bm_qstr_decode($tb_url . "\n\n" . $tb_field);
                    $field_fmt = $row['field_fmt'];
                    break;
            }
            $required = $row['field_required'] == 'n' ? '' : $DSP->required() . NBS;
            $text_direction = $row['field_text_direction'] == 'rtl' ? 'rtl' : 'ltr';
            $flink = $DSP->qdiv('itemWrapper', '<label for="field_id_' . $row['field_id'] . '"><a href="javascript:void(0);" onclick="showhide_field(\'' . $row['field_id'] . '\');return false;">{ICON}<b>' . NBS . NBS . $required . $row['field_label'] . '</b></a></label>');
            // Enclosing DIV for each row
            $r .= $DSP->div('publishRows');
            if ($row['field_is_hidden'] == 'y') {
                $r .= '<div id="field_pane_off_' . $row['field_id'] . '" style="display: block; padding:0; margin:0;">';
                $r .= str_replace('{ICON}', $expand, $flink);
                $r .= $DSP->div_c();
                $r .= '<div id="field_pane_on_' . $row['field_id'] . '" style="display: none; padding:0; margin:0;">';
                $r .= str_replace('{ICON}', $collapse, $flink);
            } else {
                $r .= '<div id="field_pane_off_' . $row['field_id'] . '" style="display: none; padding:0; margin:0;">';
                $r .= str_replace('{ICON}', $expand, $flink);
                $r .= $DSP->div_c();
                $r .= '<div id="field_pane_on_' . $row['field_id'] . '" style="display: block; padding:0; margin:0;">';
                $r .= str_replace('{ICON}', $collapse, $flink);
            }
            /** --------------------------------
                /**  Instructions for Field
                /** --------------------------------*/
            if (trim($row['field_instructions']) != '') {
                $r .= $DSP->qdiv('paddedWrapper', $DSP->qspan('defaultBold', $LANG->line('instructions')) . $row['field_instructions']);
            }
            /** --------------------------------
                /**  Textarea field types
                /** --------------------------------*/
            if ($row['field_type'] == 'textarea') {
                $rows = !isset($row['field_ta_rows']) ? '10' : $row['field_ta_rows'];
                $field_js = $show_button_cluster == 'y' ? "onFocus='setFieldName(this.name)'" : '';
                // This table fixes a Safari bug.  Kill the table once Safari has fixed it.
                $r .= "<table border='0' cellpadding='0' cellspacing='0' style='width:99%;margin-bottom:0;'><tr><td>";
                // -------------------------------------------
                // 'publish_form_field_textarea' hook.
                //  - Allows modification of the field textareas
                //
                if ($EXT->active_hook('publish_form_field_textarea') === TRUE) {
                    $r .= $EXT->call_extension('publish_form_field_textarea', $row['field_id'], $field_data, $rows, $field_js, $convert_ascii, $text_direction);
                } else {
                    $r .= $DSP->input_textarea('field_id_' . $row['field_id'], $field_data, $rows, 'textarea', '100%', $field_js, $convert_ascii, $text_direction);
                }
                //
                // -------------------------------------------
                if ($row['field_show_fmt'] == 'y') {
                    $r .= $this->text_formatting_buttons($row['field_id'], $field_fmt);
                } else {
                    $r .= $DSP->input_hidden('field_ft_' . $row['field_id'], $field_fmt);
                }
                // Safari Fix
                $r .= "</td></tr></table>";
                /** --------------------------------
                    /**  Smileys Pane
                    /** --------------------------------*/
                if ($row['field_show_fmt'] == 'y') {
                    $r .= '<div id="smileys_' . $row['field_id'] . '" style="display: none; padding:0; margin:0;">';
                    $r .= NL . "<table border='0' cellpadding='0' cellspacing='0' style='width:99%'><tr><td>";
                    $r .= NL . '<div class="clusterBox">';
                    $r .= NL . '<div class="publishItemWrapper">';
                    $r .= $this->fetch_emoticons($row['field_id']);
                    $r .= NL . '</div>';
                    $r .= NL . '</div>';
                    $r .= $DSP->td_c();
                    $r .= $DSP->tr_c();
                    $r .= $DSP->table_c();
                    $r .= NL . '</div>';
                    /** --------------------------------
                    				/**  Glossary Pane
                    				/** --------------------------------*/
                    $r .= '<div id="glossary_' . $row['field_id'] . '" style="display: none; padding:0; margin:0;">';
                    $r .= NL . "<table border='0' cellpadding='0' cellspacing='0' style='width:99%'><tr><td>";
                    $r .= $this->fetch_glossary($row['field_id']);
                    $r .= $DSP->td_c();
                    $r .= $DSP->tr_c();
                    $r .= $DSP->table_c();
                    $r .= NL . '</div>';
                    /** --------------------------------
                    				/**  Spell Check Pane
                    				/** --------------------------------*/
                    if ($this->SPELL->enabled === TRUE) {
                        $spacer = NBS . NBS . NBS . NBS . '|' . NBS . NBS . NBS . NBS;
                        $r .= '<div id="spellcheck_field_id_' . $row['field_id'] . '" style="display: none; padding:0; margin:0;">';
                        $r .= NL . "<table border='0' cellpadding='0' cellspacing='0' style='width:99%'><tr><td>";
                        $r .= NL . '<div class="clusterBox">';
                        $r .= NL . '<div class="publishItemWrapper">';
                        $r .= $DSP->div('highlight') . $LANG->line('spell_check');
                        $r .= '<span id="spellcheck_hidden_field_id_' . $row['field_id'] . '" style="visibility:hidden;">' . $spacer;
                        $r .= '<a href="javascript:void(0);" onclick="SP_saveSpellCheck();return false">';
                        $r .= $LANG->line('save_spellcheck') . '</a>' . $spacer;
                        $r .= '<a href="javascript:void(0);" onclick="SP_revertToOriginal();return false">';
                        $r .= $LANG->line('revert_spellcheck') . '</a></span>';
                        $r .= $DSP->div_c();
                        $r .= BR . BR;
                        $r .= '<iframe src="' . BASE . AMP . 'C=publish' . AMP . 'M=spellcheck_iframe" width="100%" style="display:none; border:1px solid #6600CC;" id="spellcheck_frame_field_id_' . $row['field_id'] . '" name="spellcheck_frame_field_id_' . $row['field_id'] . '"></iframe>';
                        $r .= NL . '</div>';
                        $r .= NL . '</div>';
                        $r .= $DSP->td_c();
                        $r .= $DSP->tr_c();
                        $r .= $DSP->table_c();
                        $r .= NL . '</div>';
                    }
                }
            } elseif ($row['field_type'] == 'date') {
                if (!class_exists('js_calendar')) {
                    if (include_once PATH_LIB . 'js_calendar' . EXT) {
                        $CAL = new js_calendar();
                        $DSP->extra_header .= $CAL->calendar();
                    }
                }
                // This table fixes a Safari bug.  Kill the table once Safari has fixed it.
                $r .= "<table border='0' cellpadding='0' cellspacing='0' style='margin-bottom:0;'><tr><td>";
                $date_field = 'field_id_' . $row['field_id'];
                $date_local = 'field_dt_' . $row['field_id'];
                if ($field_data == 0) {
                    $field_data = '';
                }
                $dtwhich = $which;
                if (isset($_POST[$date_field])) {
                    $field_data = $_POST[$date_field];
                    $dtwhich = $which != 'save' ? 'preview' : '';
                }
                $custom_date = '';
                $localize = FALSE;
                if ($dtwhich != 'preview' or $submission_error != '') {
                    $localize = TRUE;
                    if ($field_data != '' and isset($result)) {
                        if (isset($result->row['field_dt_' . $row['field_id']]) and $result->row['field_dt_' . $row['field_id']] != '') {
                            $field_data = $LOC->offset_entry_dst($field_data, $dst_enabled);
                            $field_data = $LOC->simpl_offset($field_data, $result->row['field_dt_' . $row['field_id']]);
                            $localize = FALSE;
                        }
                    }
                    if ($field_data != '') {
                        $custom_date = $LOC->set_human_time($field_data, $localize);
                    }
                    $cal_date = $LOC->set_localized_time($field_data) * 1000;
                } else {
                    $custom_date = $_POST[$date_field];
                    $cal_date = $custom_date != '' ? $LOC->set_localized_time($LOC->convert_human_date_to_gmt($custom_date)) * 1000 : $LOC->set_localized_time() * 1000;
                }
                /** --------------------------------
                				/**  JavaScript Calendar
                				/** --------------------------------*/
                $cal_img = '<a href="javascript:void(0);" onClick="showhide_item(\'calendar' . $date_field . '\');"><img src="' . PATH_CP_IMG . 'calendar.gif" border="0"  width="16" height="16" alt="' . $LANG->line('calendar') . '" /></a>';
                $r .= $DSP->input_text($date_field, $custom_date, '18', '23', 'input', '150px', ' onkeyup="update_calendar(\'' . $date_field . '\', this.value);" ', $text_direction) . $cal_img;
                $r .= '<div id="calendar' . $date_field . '" style="display:none;margin:4px 0 0 0;padding:0;">';
                $xmark = $custom_date == '' ? 'false' : 'true';
                $r .= NL . '<script type="text/javascript">
						
						var ' . $date_field . ' = new calendar(
												"' . $date_field . '", 
												new Date(' . $cal_date . '), 
												' . $xmark . '
												);
						
						document.write(' . $date_field . '.write());
						</script>' . NL;
                $r .= '</div>';
                $r .= $DSP->div_c();
                $r .= $DSP->div_c();
                $localized = !isset($_POST['field_offset_' . $row['field_id']]) ? $localize == FALSE ? 'n' : 'y' : $_POST['field_offset_' . $row['field_id']];
                $r .= $DSP->div('itemWrapper') . $DSP->div('lightLinks');
                $r .= $DSP->input_select_header('field_offset_' . $row['field_id']);
                $r .= $DSP->input_select_option('y', $LANG->line('localized_date'), $localized == 'y' ? 1 : 0);
                $r .= $DSP->input_select_option('n', $LANG->line('fixed_date'), $localized == 'n' ? 1 : 0);
                $r .= $DSP->input_select_footer() . NBS . NBS;
                $r .= '<a href="javascript:void(0);" onClick="set_to_now(\'' . $date_field . '\', \'' . $LOC->set_human_time($LOC->now) . '\', \'' . $LOC->set_localized_time() * 1000 . '\')" >' . $LANG->line('today') . '</a>' . NBS . NBS . '|' . NBS . NBS;
                $r .= '<a href="javascript:void(0);" onClick="clear_field(\'' . $date_field . '\');" >' . $LANG->line('clear') . '</a>';
                $r .= $DSP->div_c();
                $r .= $DSP->div_c();
                // Safari
                $r .= "</td></tr></table>";
            } elseif ($row['field_type'] == 'rel') {
                // This table fixes a Safari bug.  Kill the table once Safari has fixed it.
                $r .= "<table border='0' cellpadding='0' cellspacing='0' style='margin-bottom:0;'><tr><td>";
                if ($row['field_related_to'] == 'blog') {
                    $relto = 'exp_weblog_titles';
                    $relid = 'weblog_id';
                } else {
                    $relto = 'exp_gallery_entries';
                    $relid = 'gallery_id';
                }
                if ($row['field_related_orderby'] == 'date') {
                    $row['field_related_orderby'] = 'entry_date';
                }
                $sql = "SELECT entry_id, title FROM " . $relto . " WHERE " . $relid . " = '" . $DB->escape_str($row['field_related_id']) . "' ";
                $sql .= "ORDER BY " . $row['field_related_orderby'] . " " . $row['field_related_sort'];
                if ($row['field_related_max'] > 0) {
                    $sql .= " LIMIT " . $row['field_related_max'];
                }
                $relquery = $DB->query($sql);
                if ($relquery->num_rows == 0) {
                    $r .= $DSP->qdiv('highlight_alt', $LANG->line('no_related_entries'));
                } else {
                    $relentry_id = '';
                    if (!isset($_POST['field_id_' . $row['field_id']]) or $which == 'save') {
                        $relentry = $DB->query("SELECT rel_child_id FROM exp_relationships WHERE rel_id = '" . $DB->escape_str($field_data) . "'");
                        if ($relentry->num_rows == 1) {
                            $relentry_id = $relentry->row['rel_child_id'];
                        }
                    } else {
                        $relentry_id = $_POST['field_id_' . $row['field_id']];
                    }
                    $r .= $DSP->input_select_header('field_id_' . $row['field_id']);
                    $r .= $DSP->input_select_option('', '--', '', "dir='{$text_direction}'");
                    foreach ($relquery->result as $relrow) {
                        $r .= $DSP->input_select_option($relrow['entry_id'], $relrow['title'], $relentry_id == $relrow['entry_id'] ? 1 : 0, "dir='{$text_direction}'");
                    }
                    $r .= $DSP->input_select_footer();
                }
                // Safari
                $r .= "</td></tr></table>";
            } elseif ($row['field_type'] == 'text') {
                // This table fixes a Safari bug.  Kill the table once Safari has fixed it.
                $r .= "<table border='0' cellpadding='0' cellspacing='0' style='width:99%;margin-bottom:0;'><tr><td>";
                // -------------------------------------------
                // 'publish_form_field_text_input' hook.
                //  - Allows modification of the field text inputs
                //
                $field_js = $show_button_cluster == 'y' ? "onFocus='setFieldName(this.name)'" : '';
                if ($EXT->active_hook('publish_form_field_text_input') === TRUE) {
                    $r .= $EXT->call_extension('publish_form_field_text_input', $row['field_id'], $field_data, $row['field_maxl'], $field_js, $convert_ascii, $text_direction);
                } else {
                    $r .= $DSP->input_text('field_id_' . $row['field_id'], $field_data, '50', $row['field_maxl'], 'input', '100%', $field_js, $convert_ascii, $text_direction);
                }
                //
                // -------------------------------------------
                if ($row['field_show_fmt'] == 'y') {
                    $r .= $this->text_formatting_buttons($row['field_id'], $field_fmt);
                } else {
                    $r .= $DSP->input_hidden('field_ft_' . $row['field_id'], $field_fmt);
                }
                // Safari
                $r .= "</td></tr></table>";
            } elseif ($row['field_type'] == 'select') {
                // -------------------------------------------
                // 'publish_form_field_select_header' hook.
                //  - Allows modification of the field select header
                //
                if ($EXT->active_hook('publish_form_field_select_header') === TRUE) {
                    $r .= $EXT->call_extension('publish_form_field_select_header', $row['field_id'], $field_data, $text_direction);
                } else {
                    $r .= $DSP->input_select_header('field_id_' . $row['field_id'], '', '');
                }
                //
                // -------------------------------------------
                if ($row['field_pre_populate'] == 'n') {
                    foreach (explode("\n", trim($row['field_list_items'])) as $v) {
                        $v = trim($v);
                        $selected = $v == $field_data ? 1 : '';
                        // -------------------------------------------
                        // 'publish_form_field_select_option' hook.
                        //  - Allows modification of the field selection options
                        //  - Version 1.4.2 : Added $field_data variable
                        //
                        if ($EXT->active_hook('publish_form_field_select_option') === TRUE) {
                            $r .= $EXT->call_extension('publish_form_field_select_option', $v, $v, $selected, $field_data);
                        } else {
                            $v = $REGX->form_prep($v);
                            $r .= $DSP->input_select_option($v, $v, $selected, "dir='{$text_direction}'");
                        }
                        //
                        // -------------------------------------------
                    }
                } else {
                    // We need to pre-populate this menu from an another weblog custom field
                    $pop_query = $DB->query("SELECT field_id_" . $row['field_pre_field_id'] . " FROM exp_weblog_data WHERE weblog_id = " . $row['field_pre_blog_id'] . "");
                    $r .= $DSP->input_select_option('', '--', '', $text_direction);
                    if ($pop_query->num_rows > 0) {
                        foreach ($pop_query->result as $prow) {
                            $selected = $prow['field_id_' . $row['field_pre_field_id']] == $field_data ? 1 : '';
                            $pretitle = substr($prow['field_id_' . $row['field_pre_field_id']], 0, 110);
                            $pretitle = preg_replace("/\r\n|\r|\n|\t/", ' ', $pretitle);
                            $pretitle = $REGX->form_prep($pretitle);
                            $r .= $DSP->input_select_option($REGX->form_prep($prow['field_id_' . $row['field_pre_field_id']]), $pretitle, $selected, $text_direction);
                        }
                    }
                }
                $r .= $DSP->input_select_footer();
                if ($row['field_show_fmt'] == 'y') {
                    $r .= $this->text_formatting_buttons($row['field_id'], $field_fmt);
                } else {
                    $r .= $DSP->input_hidden('field_ft_' . $row['field_id'], $field_fmt);
                }
            } else {
                /* -------------------------------------------
                			/* 'publish_form_field_unique' hook.
                			/*  - Allows adding of unique custom fields via extensions
                			/*  - Added 1.4.2
                			*/
                if ($EXT->active_hook('publish_form_field_unique') === TRUE) {
                    $r .= $EXT->call_extension('publish_form_field_unique', $row, $field_data, $text_direction);
                }
                /*
                				/* -------------------------------------------*/
            }
            // Close Div -  SHOW/HIDE FIELD PANES
            $r .= $DSP->div_c();
            // Close outer DIV
            $r .= $DSP->div_c();
        }
        // -------------------------------------------
        // 'publish_form_end' hook.
        //  - Allows adding to end of submission form
        //
        if ($EXT->active_hook('publish_form_end') === TRUE) {
            $r .= $EXT->call_extension('publish_form_end', $weblog_id);
        }
        //
        // -------------------------------------------
        /** ----------------------------------------------
            /**  END PUBLISH FORM BLOCK
            /** ---------------------------------------------*/
        $r .= "</td></tr></table>";
        $r .= $DSP->form_close();
        if ($this->direct_return == TRUE) {
            return $r;
        }
        $DSP->body = $r;
    }
    /** ----------------------------------------
        /**  Stand-alone version of the entry form
        /** ----------------------------------------*/
    function entry_form($return_form = FALSE, $captcha = '')
    {
        global $TMPL, $LANG, $LOC, $OUT, $DB, $IN, $REGX, $FNS, $SESS, $PREFS, $EXT;
        $field_data = '';
        $catlist = '';
        $status = '';
        $title = '';
        $url_title = '';
        $dst_enabled = $SESS->userdata('daylight_savings');
        $LANG->fetch_language_file('weblog');
        // No loggy? No looky...
        if ($SESS->userdata('member_id') == 0) {
            return '';
        }
        if (!($weblog = $TMPL->fetch_param('weblog'))) {
            return $OUT->show_user_error('general', $LANG->line('weblog_not_specified'));
        }
        // Fetch the action ID number.  Even though we don't need it until later
        // we'll grab it here.  If not found it means the action table doesn't
        // contain the ID, which means the user has not updated properly.  Ya know?
        if (!($insert_action = $FNS->fetch_action_id('Weblog', 'insert_new_entry'))) {
            return $OUT->show_user_error('general', $LANG->line('weblog_no_action_found'));
        }
        // We need to first determine which weblog to post the entry into.
        $assigned_weblogs = $FNS->fetch_assigned_weblogs();
        $weblog_id = !$IN->GBL('weblog_id', 'POST') ? '' : $IN->GBL('weblog_id');
        if ($weblog_id == '') {
            $query = $DB->query("SELECT weblog_id from exp_weblogs WHERE site_id IN ('" . implode("','", $TMPL->site_ids) . "') AND blog_name = '" . $DB->escape_str($weblog) . "' AND is_user_blog = 'n'");
            if ($query->num_rows == 1) {
                $weblog_id = $query->row['weblog_id'];
            }
        }
        /** ----------------------------------------------
            /**  Security check
            /** ---------------------------------------------*/
        if (!in_array($weblog_id, $assigned_weblogs)) {
            return $TMPL->no_results();
        }
        /** ----------------------------------------------
            /**  Fetch weblog preferences
            /** ---------------------------------------------*/
        $query = $DB->query("SELECT * FROM  exp_weblogs WHERE weblog_id = '{$weblog_id}'");
        if ($query->num_rows == 0) {
            return "The weblog you have specified does not exist.";
        }
        foreach ($query->row as $key => $val) {
            ${$key} = $val;
        }
        if (!isset($_POST['weblog_id'])) {
            $title = $default_entry_title;
            $url_title = $url_title_prefix;
        }
        // -------------------------------------------
        // 'weblog_standalone_form_start' hook.
        //  - Rewrite the Stand Alone Entry Form completely
        //
        $edata = $EXT->call_extension('weblog_standalone_form_start', $return_form, $captcha, $weblog_id);
        if ($EXT->end_script === TRUE) {
            return;
        }
        //
        // -------------------------------------------
        /** ----------------------------------------
            /**  Return the "no cache" version of the form
            /** ----------------------------------------*/
        if ($return_form == FALSE) {
            $nc = '{{NOCACHE_WEBLOG_FORM ';
            if (count($TMPL->tagparams) > 0) {
                foreach ($TMPL->tagparams as $key => $val) {
                    $nc .= ' ' . $key . '="' . $val . '" ';
                }
            }
            $nc .= '}}' . $TMPL->tagdata . '{{/NOCACHE_FORM}}';
            return $nc;
        }
        /** ----------------------------------------------
            /**  JavaScript For URL Title
            /** ---------------------------------------------*/
        $convert_ascii = $PREFS->ini('auto_convert_high_ascii') == 'y' ? TRUE : FALSE;
        $word_separator = $PREFS->ini('word_separator') != "dash" ? '_' : '-';
        /** -------------------------------------
           	/**  Create Foreign Character Conversion JS
           	/** -------------------------------------*/
        /* -------------------------------------
        		/*  'foreign_character_conversion_array' hook.
        		/*  - Allows you to use your own foreign character conversion array
        		/*  - Added 1.6.0
        		*/
        if (isset($EXT->extensions['foreign_character_conversion_array'])) {
            $foreign_characters = $EXT->call_extension('foreign_character_conversion_array');
        } else {
            $foreign_characters = array('223' => "ss", '224' => "a", '225' => "a", '226' => "a", '229' => "a", '227' => "ae", '230' => "ae", '228' => "ae", '231' => "c", '232' => "e", '233' => "e", '234' => "e", '235' => "e", '236' => "i", '237' => "i", '238' => "i", '239' => "i", '241' => "n", '242' => "o", '243' => "o", '244' => "o", '245' => "o", '246' => "oe", '249' => "u", '250' => "u", '251' => "u", '252' => "ue", '255' => "y", '257' => "aa", '269' => "ch", '275' => "ee", '291' => "gj", '299' => "ii", '311' => "kj", '316' => "lj", '326' => "nj", '353' => "sh", '363' => "uu", '382' => "zh", '256' => "aa", '268' => "ch", '274' => "ee", '290' => "gj", '298' => "ii", '310' => "kj", '315' => "lj", '325' => "nj", '352' => "sh", '362' => "uu", '381' => "zh");
        }
        /*
        		/* -------------------------------------*/
        $foreign_replace = '';
        foreach ($foreign_characters as $old => $new) {
            $foreign_replace .= "if (c == '{$old}') {NewTextTemp += '{$new}'; continue;}\n\t\t\t\t";
        }
        $default_entry_title = $REGX->form_prep($default_entry_title);
        $url_title_js = <<<EOT
        <script type="text/javascript"> 
        <!--
        function liveUrlTitle()
        {
\t\t\tvar defaultTitle = '{$default_entry_title}';
\t\t\tvar NewText = document.getElementById("title").value;
\t\t\t
\t\t\tif (defaultTitle != '')
\t\t\t{
\t\t\t\tif (NewText.substr(0, defaultTitle.length) == defaultTitle)
\t\t\t\t{
\t\t\t\t\tNewText = NewText.substr(defaultTitle.length);
\t\t\t\t}\t
\t\t\t}
\t\t\t
\t\t\tNewText = NewText.toLowerCase();
\t\t\tvar separator = "{$word_separator}";
\t
\t\t\t// Foreign Character Attempt
\t\t\t
\t\t\tvar NewTextTemp = '';
\t\t\tfor(var pos=0; pos<NewText.length; pos++)
\t\t\t{
\t\t\t\tvar c = NewText.charCodeAt(pos);
\t\t\t\t
\t\t\t\tif (c >= 32 && c < 128)
\t\t\t\t{
\t\t\t\t\tNewTextTemp += NewText.charAt(pos);
\t\t\t\t}
\t\t\t\telse
\t\t\t\t{
\t\t\t\t\t{$foreign_replace}
\t\t\t\t}
\t\t\t}
    
\t\t\tvar multiReg = new RegExp(separator + '{2,}', 'g');
\t\t\t
\t\t\tNewText = NewTextTemp;
\t\t\t
\t\t\tNewText = NewText.replace('/<(.*?)>/g', '');
\t\t\tNewText = NewText.replace(/\\s+/g, separator);
\t\t\tNewText = NewText.replace(/\\//g, separator);
\t\t\tNewText = NewText.replace(/[^a-z0-9\\-\\._]/g,'');
\t\t\tNewText = NewText.replace(/\\+/g, separator);
\t\t\tNewText = NewText.replace(multiReg, separator);
\t\t\tNewText = NewText.replace(/-\$/g,'');
\t\t\tNewText = NewText.replace(/_\$/g,'');
\t\t\tNewText = NewText.replace(/^_/g,'');
\t\t\tNewText = NewText.replace(/^-/g,'');
\t\t\t
\t\t\tif (document.getElementById("url_title"))
\t\t\t{
\t\t\t\tdocument.getElementById("url_title").value = "{$url_title_prefix}" + NewText;\t\t\t
\t\t\t}
\t\t\telse
\t\t\t{
\t\t\t\tdocument.forms['entryform'].elements['url_title'].value = "{$url_title_prefix}" + NewText; 
\t\t\t}\t\t
\t\t}


        function showhide_item(id)
        {
\t\t\tif (document.getElementById(id).style.display == "block")
\t\t\t{
\t\t\t\tdocument.getElementById(id).style.display = "none";
        \t}
        \telse
        \t{
\t\t\t\tdocument.getElementById(id).style.display = "block";
        \t}
        }
\t\t
\t\t
\t\t-->
\t\t</script>
EOT;
        // -------------------------------------------
        // 'weblog_standalone_form_urltitle_js' hook.
        //  - Rewrite the Stand Alone Entry Form's URL Title JavaScript
        //
        if ($EXT->active_hook('weblog_standalone_form_urltitle_js') === TRUE) {
            $url_title_js = $EXT->call_extension('weblog_standalone_form_urltitle_js', $url_title_js);
            if ($EXT->end_script === TRUE) {
                return;
            }
        }
        //
        // -------------------------------------------
        $LANG->fetch_language_file('publish');
        /** ----------------------------------------
            /**  Compile form declaration and hidden fields
            /** ----------------------------------------*/
        $RET = isset($_POST['RET']) ? $_POST['RET'] : $FNS->fetch_current_uri();
        $XID = !isset($_POST['XID']) ? '' : $_POST['XID'];
        $PRV = isset($_POST['PRV']) ? $_POST['PRV'] : '{PREVIEW_TEMPLATE}';
        $hidden_fields = array('ACT' => $insert_action, 'RET' => $RET, 'PRV' => $PRV, 'URI' => $IN->URI == '' ? 'index' : $IN->URI, 'XID' => $XID, 'return_url' => isset($_POST['return_url']) ? $_POST['return_url'] : $TMPL->fetch_param('return'), 'author_id' => $SESS->userdata('member_id'), 'weblog_id' => $weblog_id);
        /** ----------------------------------------
            /**  Add status to hidden fields
            /** ----------------------------------------*/
        $status_id = !isset($_POST['status_id']) ? $TMPL->fetch_param('status') : $_POST['status_id'];
        if ($status_id == 'Open' || $status_id == 'Closed') {
            $status_id = strtolower($status_id);
        }
        $status_query = $DB->query("SELECT * FROM exp_statuses WHERE group_id = '{$status_group}' order by status_order");
        if ($status_id != '') {
            $closed_flag = TRUE;
            if ($status_query->num_rows > 0) {
                foreach ($status_query->result as $row) {
                    if ($row['status'] == $status_id) {
                        $closed_flag = FALSE;
                    }
                }
            }
            $hidden_fields['status'] = $closed_flag == TRUE ? 'closed' : $status_id;
        }
        /** ----------------------------------------
            /**  Add "allow" options
            /** ----------------------------------------*/
        $allow_cmts = !isset($_POST['allow_cmts']) ? $TMPL->fetch_param('allow_comments') : $_POST['allow_cmts'];
        if ($allow_cmts != '' and $comment_system_enabled == 'y') {
            $hidden_fields['allow_comments'] = $allow_cmts == 'yes' ? 'y' : 'n';
        }
        $allow_tbks = !isset($_POST['allow_tbks']) ? $TMPL->fetch_param('allow_trackbacks') : $_POST['allow_tbks'];
        if ($allow_tbks != '') {
            $hidden_fields['allow_trackbacks'] = $allow_tbks == 'yes' ? 'y' : 'n';
        }
        $sticky_entry = !isset($_POST['sticky_entry']) ? $TMPL->fetch_param('sticky_entry') : $_POST['sticky_entry'];
        if ($sticky_entry != '') {
            $hidden_fields['sticky'] = $sticky_entry == 'yes' ? 'y' : 'n';
        }
        /** ----------------------------------------
            /**  Add categories to hidden fields
            /** ----------------------------------------*/
        if ($category_id = $TMPL->fetch_param('category')) {
            if (isset($_POST['category'])) {
                foreach ($_POST as $key => $val) {
                    if (strstr($key, 'category') and is_array($val)) {
                        $i = 0;
                        foreach ($val as $v) {
                            $hidden_fields['category[' . $i++ . ']'] = $v;
                        }
                    }
                }
            } else {
                if (strpos($category_id, '|') === FALSE) {
                    $hidden_fields['category[]'] = $category_id;
                } else {
                    $category_id = trim($category_id, '|');
                    $i = 0;
                    foreach (explode("|", $category_id) as $val) {
                        $hidden_fields['category[' . $i++ . ']'] = $val;
                    }
                }
            }
        }
        /** ----------------------------------------
            /**  Add pings to hidden fields
            /** ----------------------------------------*/
        $hidden_pings = !isset($_POST['hidden_pings']) ? $TMPL->fetch_param('hidden_pings') : $_POST['hidden_pings'];
        if ($hidden_pings == 'yes') {
            $hidden_fields['hidden_pings'] = 'yes';
            $ping_servers = $this->fetch_ping_servers('new');
            if (is_array($ping_servers) and count($ping_servers) > 0) {
                $i = 0;
                foreach ($ping_servers as $val) {
                    if ($val['1'] != '') {
                        $hidden_fields['ping[' . $i++ . ']'] = $val['0'];
                    }
                }
            }
        }
        /** -------------------------------------
        		/**  Parse out the tag
        		/** -------------------------------------*/
        $tagdata = $TMPL->tagdata;
        /** ----------------------------------------------
            /**  Upload and Smileys Link
            /** ---------------------------------------------*/
        $s = $PREFS->ini('admin_session_type') != 'c' ? $SESS->userdata['session_id'] : 0;
        $cp_url = $PREFS->ini('cp_url') . '?S=' . $s;
        // -------------------------------------------
        // 'weblog_standalone_form_upload_url' hook.
        //  - Rewrite URL for Upload Link
        //
        if ($EXT->active_hook('weblog_standalone_form_upload_url') === TRUE) {
            $upload_url = $EXT->call_extension('weblog_standalone_form_upload_url', $weblog_id);
        } else {
            $upload_url = $cp_url . '&amp;C=publish&amp;M=file_upload_form&amp;field_group=' . $field_group . '&amp;Z=1';
        }
        //
        // -------------------------------------------
        $tagdata = str_replace('{upload_url}', $upload_url, $tagdata);
        $tagdata = str_replace('{smileys_url}', $cp_url . '&amp;C=publish&amp;M=emoticons&amp;field_group=' . $field_group . '&amp;Z=1', $tagdata);
        // Onward...
        $which = $IN->GBL('preview', 'POST') ? 'preview' : 'new';
        /** --------------------------------
        		/**  Fetch Custom Fields
        		/** --------------------------------*/
        if ($TMPL->fetch_param('show_fields') !== FALSE) {
            if (strncmp('not ', $TMPL->fetch_param('show_fields'), 4) == 0) {
                $these = "AND field_name NOT IN ('" . str_replace('|', "','", trim(substr($TMPL->fetch_param('show_fields'), 3))) . "') ";
            } else {
                $these = "AND field_name IN ('" . str_replace('|', "','", trim($TMPL->fetch_param('show_fields'))) . "') ";
            }
        } else {
            $these = '';
        }
        $query = $DB->query("SELECT * FROM  exp_weblog_fields WHERE group_id = '{$field_group}' {$these} ORDER BY field_order");
        $fields = array();
        $date_fields = array();
        $cond = array();
        if ($which == 'preview') {
            foreach ($query->result as $row) {
                $fields['field_id_' . $row['field_id']] = $row['field_name'];
                $cond[$row['field_name']] = '';
                if ($row['field_type'] == 'date') {
                    $date_fields[$row['field_name']] = $row['field_id'];
                }
            }
        }
        /** ----------------------------------------
        		/**  Preview
        		/** ----------------------------------------*/
        if (preg_match("#" . LD . "preview" . RD . "(.+?)" . LD . '/' . "preview" . RD . "#s", $tagdata, $match)) {
            if ($which != 'preview') {
                $tagdata = str_replace($match['0'], '', $tagdata);
            } else {
                /** ----------------------------------------
                				/**  Instantiate Typography class
                				/** ----------------------------------------*/
                if (!class_exists('Typography')) {
                    require PATH_CORE . 'core.typography' . EXT;
                }
                $TYPE = new Typography();
                $TYPE->convert_curly = FALSE;
                $title = $TYPE->format_characters(stripslashes($IN->GBL('title', 'POST')));
                $match['1'] = str_replace(LD . 'title' . RD, $title, $match['1']);
                // We need to grab each global array index and do a little formatting
                $str = '';
                foreach ($_POST as $key => $val) {
                    if (!is_array($val)) {
                        if (strstr($key, 'field_id')) {
                            $expl = explode('field_id_', $key);
                            if (in_array($expl['1'], $date_fields)) {
                                $temp_date = $LOC->convert_human_date_to_gmt($_POST['field_id_' . $expl['1']]);
                                $temp = $_POST['field_id_' . $expl['1']];
                                $cond[$fields['field_id_' . $expl['1']]] = $temp_date;
                            } else {
                                $cond[$fields['field_id_' . $expl['1']]] = $_POST['field_id_' . $expl['1']];
                                $txt_fmt = !isset($_POST['field_ft_' . $expl['1']]) ? 'xhtml' : $_POST['field_ft_' . $expl['1']];
                                $temp = $TYPE->parse_type(stripslashes($val), array('text_format' => $txt_fmt, 'html_format' => $weblog_html_formatting, 'auto_links' => $weblog_allow_img_urls, 'allow_img_url' => $weblog_auto_link_urls));
                            }
                            if (isset($fields[$key])) {
                                $match['1'] = str_replace(LD . $fields[$key] . RD, $temp, $match['1']);
                            }
                            $str .= $temp;
                        }
                    }
                }
                $match['1'] = str_replace(LD . 'display_custom_fields' . RD, $str, $match['1']);
                $match['1'] = $FNS->prep_conditionals($match['1'], $cond);
                $tagdata = str_replace($match['0'], $match['1'], $tagdata);
            }
        }
        /** -------------------------------------
        		/**  Formatting buttons
        		/** -------------------------------------*/
        if (preg_match("#" . LD . "formatting_buttons" . RD . "#s", $tagdata)) {
            if (!defined('BASE')) {
                $s = $PREFS->ini('admin_session_type') != 'c' ? $SESS->userdata['session_id'] : 0;
                define('BASE', $PREFS->ini('cp_url', FALSE) . '?S=' . $s);
            }
            if (!class_exists('Display')) {
                require PATH_CP . 'cp.display' . EXT;
            }
            global $DSP;
            $DSP = new Display();
            if (!class_exists('Publish')) {
                require PATH_CP . 'cp.publish' . EXT;
            }
            $PUB = new Publish();
            $tagdata = str_replace(LD . 'formatting_buttons' . RD, str_replace('.entryform.', ".getElementById('entryform').", $PUB->html_formatting_buttons($SESS->userdata('member_id'), $field_group)), $tagdata);
        }
        /** -------------------------------------
        		/**  Fetch the {custom_fields} chunk
        		/** -------------------------------------*/
        $custom_fields = '';
        if (preg_match("#" . LD . "custom_fields" . RD . "(.+?)" . LD . '/' . "custom_fields" . RD . "#s", $tagdata, $match)) {
            $custom_fields = trim($match['1']);
            $tagdata = str_replace($match['0'], LD . 'temp_custom_fields' . RD, $tagdata);
        }
        // If we have custom fields to show, generate them
        if ($custom_fields != '') {
            $field_array = array('textarea', 'textinput', 'pulldown', 'date', 'relationship');
            $textarea = '';
            $textinput = '';
            $pulldown = '';
            $date = '';
            $relationship = '';
            $rel_options = '';
            $pd_options = '';
            $required = '';
            foreach ($field_array as $val) {
                if (preg_match("#" . LD . "\\s*if\\s+" . $val . RD . "(.+?)" . LD . '/' . "if" . RD . "#s", $custom_fields, $match)) {
                    ${$val} = $match['1'];
                    if ($val == 'pulldown') {
                        if (preg_match("#" . LD . "options" . RD . "(.+?)" . LD . '/' . "options" . RD . "#s", $pulldown, $pmatch)) {
                            $pd_options = $pmatch['1'];
                            $pulldown = str_replace($pmatch['0'], LD . 'temp_pd_options' . RD, $pulldown);
                        }
                    }
                    if ($val == 'relationship') {
                        if (preg_match("#" . LD . "options" . RD . "(.+?)" . LD . '/' . "options" . RD . "#s", $relationship, $pmatch)) {
                            $rel_options = $pmatch['1'];
                            $relationship = str_replace($pmatch['0'], LD . 'temp_rel_options' . RD, $relationship);
                        }
                    }
                    $custom_fields = str_replace($match['0'], LD . 'temp_' . $val . RD, $custom_fields);
                }
            }
            if (preg_match("#" . LD . "if\\s+required" . RD . "(.+?)" . LD . '/' . "if" . RD . "#s", $custom_fields, $match)) {
                $required = $match['1'];
                $custom_fields = str_replace($match['0'], LD . 'temp_required' . RD, $custom_fields);
            }
            /** --------------------------------
            			/**  Parse Custom Fields
            			/** --------------------------------*/
            $build = '';
            foreach ($query->result as $row) {
                $temp_chunk = $custom_fields;
                $temp_field = '';
                switch ($which) {
                    case 'preview':
                        $field_data = !isset($_POST['field_id_' . $row['field_id']]) ? '' : $_POST['field_id_' . $row['field_id']];
                        $field_fmt = !isset($_POST['field_ft_' . $row['field_id']]) ? $row['field_fmt'] : $_POST['field_ft_' . $row['field_id']];
                        break;
                    case 'edit':
                        $field_data = !isset($result->row['field_id_' . $row['field_id']]) ? '' : $result->row['field_id_' . $row['field_id']];
                        $field_fmt = !isset($result->row['field_ft_' . $row['field_id']]) ? $row['field_fmt'] : $result->row['field_ft_' . $row['field_id']];
                        break;
                    default:
                        $field_data = '';
                        $field_fmt = $row['field_fmt'];
                        break;
                }
                /** --------------------------------
                				/**  Textarea field types
                				/** --------------------------------*/
                if ($row['field_type'] == 'textarea' and $textarea != '') {
                    $temp_chunk = str_replace(LD . 'temp_textarea' . RD, $textarea, $temp_chunk);
                }
                if ($row['field_type'] == 'text' and $textinput != '') {
                    $temp_chunk = str_replace(LD . 'temp_textinput' . RD, $textinput, $temp_chunk);
                }
                if ($row['field_type'] == 'rel') {
                    if ($row['field_related_to'] == 'blog') {
                        $relto = 'exp_weblog_titles';
                        $relid = 'weblog_id';
                    } else {
                        $relto = 'exp_gallery_entries';
                        $relid = 'gallery_id';
                    }
                    if ($row['field_related_orderby'] == 'date') {
                        $row['field_related_orderby'] = 'entry_date';
                    }
                    $sql = "SELECT entry_id, title FROM " . $relto . " WHERE " . $relid . " = '" . $DB->escape_str($row['field_related_id']) . "' ";
                    $sql .= "ORDER BY " . $row['field_related_orderby'] . " " . $row['field_related_sort'];
                    if ($row['field_related_max'] > 0) {
                        $sql .= " LIMIT " . $row['field_related_max'];
                    }
                    $relquery = $DB->query($sql);
                    if ($relquery->num_rows > 0) {
                        $relentry_id = '';
                        if (!isset($_POST['field_id_' . $row['field_id']])) {
                            $relentry = $DB->query("SELECT rel_child_id FROM exp_relationships WHERE rel_id = '" . $DB->escape_str($field_data) . "'");
                            if ($relentry->num_rows == 1) {
                                $relentry_id = $relentry->row['rel_child_id'];
                            }
                        } else {
                            $relentry_id = $_POST['field_id_' . $row['field_id']];
                        }
                        $temp_options = $rel_options;
                        $temp_options = str_replace(LD . 'option_name' . RD, '--', $temp_options);
                        $temp_options = str_replace(LD . 'option_value' . RD, '', $temp_options);
                        $temp_options = str_replace(LD . 'selected' . RD, '', $temp_options);
                        $pdo = $temp_options;
                        foreach ($relquery->result as $relrow) {
                            $temp_options = $rel_options;
                            $temp_options = str_replace(LD . 'option_name' . RD, $relrow['title'], $temp_options);
                            $temp_options = str_replace(LD . 'option_value' . RD, $relrow['entry_id'], $temp_options);
                            $temp_options = str_replace(LD . 'selected' . RD, $relentry_id == $relrow['entry_id'] ? ' selected="selected"' : '', $temp_options);
                            $pdo .= $temp_options;
                        }
                        $temp_relationship = str_replace(LD . 'temp_rel_options' . RD, $pdo, $relationship);
                        $temp_chunk = str_replace(LD . 'temp_relationship' . RD, $temp_relationship, $temp_chunk);
                    }
                }
                if ($row['field_type'] == 'date' and $date != '') {
                    $temp_chunk = $custom_fields;
                    $date_field = 'field_id_' . $row['field_id'];
                    $date_local = 'field_dt_' . $row['field_id'];
                    $dtwhich = $which;
                    if (isset($_POST[$date_field])) {
                        $field_data = $_POST[$date_field];
                        $dtwhich = 'preview';
                    }
                    $custom_date = '';
                    $localize = FALSE;
                    if ($dtwhich != 'preview') {
                        $localize = TRUE;
                        if ($field_data != '' and isset($result->row['field_dt_' . $row['field_id']]) and $result->row['field_dt_' . $row['field_id']] != '') {
                            $field_data = $LOC->offset_entry_dst($field_data, $dst_enabled);
                            $field_data = $LOC->simpl_offset($field_data, $result->row['field_dt_' . $row['field_id']]);
                            $localize = FALSE;
                        }
                        if ($field_data != '') {
                            $custom_date = $LOC->set_human_time($field_data, $localize);
                        }
                        $cal_date = $LOC->set_localized_time($custom_date) * 1000;
                    } else {
                        $custom_date = $_POST[$date_field];
                        $cal_date = $custom_date != '' ? $LOC->set_localized_time($LOC->convert_human_date_to_gmt($custom_date)) * 1000 : $LOC->set_localized_time() * 1000;
                    }
                    $temp_chunk = str_replace(LD . 'temp_date' . RD, $date, $temp_chunk);
                    $temp_chunk = str_replace(LD . 'date' . RD, $custom_date, $temp_chunk);
                } elseif ($row['field_type'] == 'select' and $pulldown != '') {
                    if ($row['field_pre_populate'] == 'n') {
                        $pdo = '';
                        if ($row['field_required'] == 'n') {
                            $temp_options = $pd_options;
                            $temp_options = str_replace(LD . 'option_name' . RD, '--', $temp_options);
                            $temp_options = str_replace(LD . 'option_value' . RD, '', $temp_options);
                            $temp_options = str_replace(LD . 'selected' . RD, '', $temp_options);
                            $pdo = $temp_options;
                        }
                        foreach (explode("\n", trim($row['field_list_items'])) as $v) {
                            $temp_options = $pd_options;
                            $v = trim($v);
                            $temp_options = str_replace(LD . 'option_name' . RD, $v, $temp_options);
                            $temp_options = str_replace(LD . 'option_value' . RD, $v, $temp_options);
                            $temp_options = str_replace(LD . 'selected' . RD, $v == $field_data ? ' selected="selected"' : '', $temp_options);
                            $pdo .= $temp_options;
                        }
                        $temp_pulldown = str_replace(LD . 'temp_pd_options' . RD, $pdo, $pulldown);
                        $temp_chunk = str_replace(LD . 'temp_pulldown' . RD, $temp_pulldown, $temp_chunk);
                    } else {
                        // We need to pre-populate this menu from an another weblog custom field
                        $pop_query = $DB->query("SELECT field_id_" . $row['field_pre_field_id'] . " FROM exp_weblog_data WHERE weblog_id = " . $row['field_pre_blog_id'] . "");
                        if ($pop_query->num_rows > 0) {
                            $temp_options = $rel_options;
                            $temp_options = str_replace(LD . 'option_name' . RD, '--', $temp_options);
                            $temp_options = str_replace(LD . 'option_value' . RD, '', $temp_options);
                            $temp_options = str_replace(LD . 'selected' . RD, '', $temp_options);
                            $pdo = $temp_options;
                            foreach ($pop_query->result as $prow) {
                                $pretitle = substr($prow['field_id_' . $row['field_pre_field_id']], 0, 110);
                                $pretitle = preg_replace("/\r\n|\r|\n|\t/", ' ', $pretitle);
                                $pretitle = $REGX->form_prep($pretitle);
                                $temp_options = $rel_options;
                                $temp_options = str_replace(LD . 'option_name' . RD, $pretitle, $temp_options);
                                $temp_options = str_replace(LD . 'option_value' . RD, $REGX->form_prep($prow['field_id_' . $row['field_pre_field_id']]), $temp_options);
                                $temp_options = str_replace(LD . 'selected' . RD, $prow['field_id_' . $row['field_pre_field_id']] == $field_data ? ' selected="selected"' : '', $temp_options);
                                $pdo .= $temp_options;
                            }
                            $temp_relationship = str_replace(LD . 'temp_rel_options' . RD, $pdo, $relationship);
                            $temp_chunk = str_replace(LD . 'temp_relationship' . RD, $temp_relationship, $temp_chunk);
                        }
                    }
                }
                if ($row['field_required'] == 'y') {
                    $temp_chunk = str_replace(LD . 'temp_required' . RD, $required, $temp_chunk);
                } else {
                    $temp_chunk = str_replace(LD . 'temp_required' . RD, '', $temp_chunk);
                }
                $temp_chunk = str_replace(LD . 'field_data' . RD, $REGX->form_prep($field_data), $temp_chunk);
                $temp_chunk = str_replace(LD . 'temp_date' . RD, '', $temp_chunk);
                $temp_chunk = str_replace(LD . 'temp_textarea' . RD, '', $temp_chunk);
                $temp_chunk = str_replace(LD . 'temp_relationship' . RD, '', $temp_chunk);
                $temp_chunk = str_replace(LD . 'temp_textinput' . RD, '', $temp_chunk);
                $temp_chunk = str_replace(LD . 'temp_pulldown' . RD, '', $temp_chunk);
                $temp_chunk = str_replace(LD . 'temp_pd_options' . RD, '', $temp_chunk);
                $temp_chunk = str_replace(LD . 'calendar_link' . RD, '', $temp_chunk);
                $temp_chunk = str_replace(LD . 'calendar_id' . RD, '', $temp_chunk);
                $temp_chunk = str_replace(LD . 'rows' . RD, !isset($row['field_ta_rows']) ? '10' : $row['field_ta_rows'], $temp_chunk);
                $temp_chunk = str_replace(LD . 'field_label' . RD, $row['field_label'], $temp_chunk);
                $temp_chunk = str_replace(LD . 'field_instructions' . RD, $row['field_instructions'], $temp_chunk);
                $temp_chunk = str_replace(LD . 'text_direction' . RD, $row['field_text_direction'], $temp_chunk);
                $temp_chunk = str_replace(LD . 'maxlength' . RD, $row['field_maxl'], $temp_chunk);
                $temp_chunk = str_replace(LD . 'field_name' . RD, 'field_id_' . $row['field_id'], $temp_chunk);
                $hidden_fields['field_ft_' . $row['field_id']] = $field_fmt;
                // $temp_chunk .= "\n<input type='hidden' name='field_ft_".$row['field_id']."' value='".$field_fmt."' />\n";
                $build .= $temp_chunk;
            }
            $tagdata = str_replace(LD . 'temp_custom_fields' . RD, stripslashes($build), $tagdata);
        }
        /** ----------------------------------------
        		/**  Categories
        		/** ----------------------------------------*/
        if (preg_match("#" . LD . "category_menu" . RD . "(.+?)" . LD . '/' . "category_menu" . RD . "#s", $tagdata, $match)) {
            // -------------------------------------------
            // 'weblog_standalone_form_category_menu' hook.
            //  - Rewrite the displaying of categories, if you dare!
            //
            if ($EXT->active_hook('weblog_standalone_form_category_menu') === TRUE) {
                $edata = $EXT->call_extension('weblog_standalone_form_category_menu', $cat_group, $which, $deft_category, $catlist);
                $match['1'] = str_replace(LD . 'select_options' . RD, $edata, $match['1']);
                $tagdata = str_replace($match['0'], $match['1'], $tagdata);
                if ($EXT->end_script === TRUE) {
                    return;
                }
            } else {
                $this->category_tree_form($cat_group, $which, $deft_category, $catlist);
                if (count($this->categories) == 0) {
                    $tagdata = str_replace($match['0'], '', $tagdata);
                } else {
                    $c = '';
                    foreach ($this->categories as $val) {
                        $c .= $val;
                    }
                    $match['1'] = str_replace(LD . 'select_options' . RD, $c, $match['1']);
                    $tagdata = str_replace($match['0'], $match['1'], $tagdata);
                }
            }
            //
            // -------------------------------------------
        }
        /** ----------------------------------------
        		/**  Ping Servers
        		/** ----------------------------------------*/
        if (preg_match("#" . LD . "ping_servers" . RD . "(.+?)" . LD . '/' . "ping_servers" . RD . "#s", $tagdata, $match)) {
            $field = preg_match("#" . LD . "ping_row" . RD . "(.+?)" . LD . '/' . "ping_row" . RD . "#s", $tagdata, $match1) ? $match1['1'] : '';
            if (!isset($match1['0'])) {
                $tagdata = str_replace($match['0'], '', $tagdata);
            }
            $ping_servers = $this->fetch_ping_servers($which);
            if (!is_array($ping_servers) or count($ping_servers) == 0) {
                $tagdata = str_replace($match['0'], '', $tagdata);
            } else {
                $ping_build = '';
                foreach ($ping_servers as $val) {
                    $temp = $field;
                    $temp = str_replace(LD . 'ping_value' . RD, $val['0'], $temp);
                    $temp = str_replace(LD . 'ping_checked' . RD, $val['1'], $temp);
                    $temp = str_replace(LD . 'ping_server_name' . RD, $val['2'], $temp);
                    $ping_build .= $temp;
                }
                $match['1'] = str_replace($match1['0'], $ping_build, $match['1']);
                $tagdata = str_replace($match['0'], $match['1'], $tagdata);
            }
        }
        /** ----------------------------------------
        		/**  Status
        		/** ----------------------------------------*/
        if (preg_match("#" . LD . "status_menu" . RD . "(.+?)" . LD . '/' . "status_menu" . RD . "#s", $tagdata, $match)) {
            if (isset($_POST['status'])) {
                $deft_status = $_POST['status'];
            }
            if ($deft_status == '') {
                $deft_status = 'open';
            }
            if ($status == '') {
                $status = $deft_status;
            }
            /** --------------------------------
            				/**  Fetch disallowed statuses
            				/** --------------------------------*/
            $no_status_access = array();
            if ($SESS->userdata['group_id'] != 1) {
                $query = $DB->query("SELECT status_id FROM exp_status_no_access WHERE member_group = '" . $SESS->userdata['group_id'] . "'");
                if ($query->num_rows > 0) {
                    foreach ($query->result as $row) {
                        $no_status_access[] = $row['status_id'];
                    }
                }
            }
            /** --------------------------------
            				/**  Create status menu
            				/** --------------------------------*/
            $r = '';
            if ($status_query->num_rows == 0) {
                // if there is no status group assigned, only Super Admins can create 'open' entries
                if ($SESS->userdata['group_id'] == 1) {
                    $selected = $status == 'open' ? " selected='selected'" : '';
                    $r .= "<option value='open'" . $selected . ">" . $LANG->line('open') . "</option>";
                }
                $selected = $status == 'closed' ? " selected='selected'" : '';
                $r .= "<option value='closed'" . $selected . ">" . $LANG->line('closed') . "</option>";
            } else {
                $no_status_flag = TRUE;
                foreach ($status_query->result as $row) {
                    $selected = $status == $row['status'] ? " selected='selected'" : '';
                    if ($selected != 1) {
                        if (in_array($row['status_id'], $no_status_access)) {
                            continue;
                        }
                    }
                    $no_status_flag = FALSE;
                    $status_name = ($row['status'] == 'open' or $row['status'] == 'closed') ? $LANG->line($row['status']) : $row['status'];
                    $r .= "<option value='" . $REGX->form_prep($row['status']) . "'" . $selected . ">" . $REGX->form_prep($status_name) . "</option>\n";
                }
                if ($no_status_flag == TRUE) {
                    $tagdata = str_replace($match['0'], '', $tagdata);
                }
            }
            $match['1'] = str_replace(LD . 'select_options' . RD, $r, $match['1']);
            $tagdata = str_replace($match['0'], $match['1'], $tagdata);
        }
        /** ----------------------------------------
        		/**  Trackback field
        		/** ----------------------------------------*/
        if (preg_match("#" . LD . "if\\s+trackback" . RD . "(.+?)" . LD . '/' . "if" . RD . "#s", $tagdata, $match)) {
            if ($show_trackback_field == 'n') {
                $tagdata = str_replace($match['0'], '', $tagdata);
            } else {
                $tagdata = str_replace($match['0'], $match['1'], $tagdata);
            }
        }
        /** ----------------------------------------
        		/**  Parse single variables
        		/** ----------------------------------------*/
        foreach ($TMPL->var_single as $key => $val) {
            /** ----------------------------------------
                /**  {title}
                /** ----------------------------------------*/
            if ($key == 'title') {
                $title = !isset($_POST['title']) ? $title : stripslashes($_POST['title']);
                $tagdata = $TMPL->swap_var_single($key, $REGX->form_prep($title), $tagdata);
            }
            /** ----------------------------------------
                /**  {allow_comments}
                /** ----------------------------------------*/
            if ($key == 'allow_comments') {
                if ($which == 'preview') {
                    $checked = !isset($_POST['allow_comments']) || $comment_system_enabled != 'y' ? '' : "checked='checked'";
                } else {
                    $checked = $deft_comments == 'n' || $comment_system_enabled != 'y' ? '' : "checked='checked'";
                }
                $tagdata = $TMPL->swap_var_single($key, $checked, $tagdata);
            }
            /** ----------------------------------------
                /**  {allow_trackbacks}
                /** ----------------------------------------*/
            if ($key == 'allow_trackbacks') {
                if ($which == 'preview') {
                    $checked = !isset($_POST['allow_trackbacks']) || $trackback_system_enabled != 'y' ? '' : "checked='checked'";
                } else {
                    $checked = $deft_trackbacks == 'n' || $trackback_system_enabled != 'y' ? '' : "checked='checked'";
                }
                $tagdata = $TMPL->swap_var_single($key, $checked, $tagdata);
            }
            /** ----------------------------------------
                /**  {dst_enabled}
                /** ----------------------------------------*/
            if ($key == 'dst_enabled') {
                if ($which == 'preview') {
                    $checked = isset($_POST['dst_enabled']) && $PREFS->ini('honor_entry_dst') == 'y' ? "checked='checked'" : '';
                } else {
                    $checked = $dst_enabled == 'y' ? "checked='checked'" : '';
                }
                $tagdata = $TMPL->swap_var_single($key, $checked, $tagdata);
            }
            /** ----------------------------------------
                /**  {sticky}
                /** ----------------------------------------*/
            if ($key == 'sticky') {
                $checked = '';
                if ($which == 'preview') {
                    $checked = !isset($_POST['sticky']) ? '' : "checked='checked'";
                }
                $tagdata = $TMPL->swap_var_single($key, $checked, $tagdata);
            }
            /** ----------------------------------------
                /**  {url_title}
                /** ----------------------------------------*/
            if ($key == 'url_title') {
                $url_title = !isset($_POST['url_title']) ? $url_title : $_POST['url_title'];
                $tagdata = $TMPL->swap_var_single($key, $url_title, $tagdata);
            }
            /** ----------------------------------------
                /**  {entry_date}
                /** ----------------------------------------*/
            if ($key == 'entry_date') {
                $entry_date = !isset($_POST['entry_date']) ? $LOC->set_human_time($LOC->now) : $_POST['entry_date'];
                $tagdata = $TMPL->swap_var_single($key, $entry_date, $tagdata);
            }
            /** ----------------------------------------
                /**  {expiration_date}
                /** ----------------------------------------*/
            if ($key == 'expiration_date') {
                $expiration_date = !isset($_POST['expiration_date']) ? '' : $_POST['expiration_date'];
                $tagdata = $TMPL->swap_var_single($key, $expiration_date, $tagdata);
            }
            /** ----------------------------------------
                /**  {comment_expiration_date}
                /** ----------------------------------------*/
            if ($key == 'comment_expiration_date') {
                $comment_expiration_date = '';
                if ($which == 'preview') {
                    $comment_expiration_date = !isset($_POST['comment_expiration_date']) ? '' : $_POST['comment_expiration_date'];
                } else {
                    if ($comment_expiration > 0) {
                        $comment_expiration_date = $comment_expiration * 86400;
                        $comment_expiration_date = $comment_expiration_date + $LOC->now;
                        $comment_expiration_date = $LOC->set_human_time($comment_expiration_date);
                    }
                }
                $tagdata = $TMPL->swap_var_single($key, $comment_expiration_date, $tagdata);
            }
            /** ----------------------------------------
                /**  {trackback_urls}
                /** ----------------------------------------*/
            if ($key == 'trackback_urls') {
                $trackback_urls = !isset($_POST['trackback_urls']) ? '' : stripslashes($_POST['trackback_urls']);
                $tagdata = $TMPL->swap_var_single($key, $trackback_urls, $tagdata);
            }
        }
        // -------------------------------------------
        // 'weblog_standalone_form_end' hook.
        //  - Allows adding to end of submission form
        //
        if ($EXT->active_hook('weblog_standalone_form_end') === TRUE) {
            $tagdata = $EXT->call_extension('weblog_standalone_form_end', $tagdata);
        }
        //
        // -------------------------------------------
        // Build the form
        $data = array('hidden_fields' => $hidden_fields, 'action' => $RET, 'id' => 'entryform');
        $res = $FNS->form_declaration($data);
        if ($TMPL->fetch_param('use_live_url') != 'no') {
            $res .= $url_title_js;
        }
        $res .= $tagdata;
        $res .= "</form>";
        return $res;
    }