/**
 * Put top code for the page
 **/
function PageCode($admintmpl = 0)
{
    global $dir;
    global $site;
    global $_page;
    global $_page_comp;
    global $logged;
    global $langHTMLCharset;
    global $tmpl;
    global $ADMIN;
    global $tmi_letters;
    global $dbh_letters;
    global $max_thumb_height;
    global $max_thumb_width;
    global $_page_cont;
    $ni = $_page['name_index'];
    global $oTemplConfig;
    $oTemplMenu = new BxTemplMenu($oTemplConfig);
    $free_mode = getParam("free_mode") == "on" ? 1 : 0;
    // reading templates
    if (!$admintmpl) {
        $fn = "{$dir['root']}templates/tmpl_{$tmpl}/page_{$ni}.html";
        if (!file_exists($fn)) {
            $fn = "{$dir['root']}templates/tmpl_{$tmpl}/default.html";
        }
    } else {
        $fn = "{$dir['root']}admin/tmpl_admin.html";
    }
    $templ = file_get_contents($fn);
    // process includes (multi-level)
    do {
        $templ1 = $templ;
        $templ = preg_replace_callback("/__include (.*)__/", "TmplInclude", $templ1);
        $templ = preg_replace_callback("/__includebase (.*)__/", "TmplIncludeBase", $templ);
    } while ($templ1 != $templ);
    //insert to your template page key:    __t: lang_key__     and you will get translated string
    $templ = preg_replace_callback("/__t: (_.+)__/", create_function('$matches', 'return _t($matches[1]);'), $templ);
    PageStaticComponents();
    // lang block
    if ((int) getParam('lang_enable')) {
        ob_start();
        lang_select_txt();
        $_page_cont[0]['switch_lang_block'] = ob_get_clean();
    } else {
        $_page_cont[0]['switch_lang_block'] = '';
    }
    // charset
    $_page_cont[0]['page_charset'] = $langHTMLCharset;
    //change templates
    if ('on' == getParam("enable_template")) {
        $_page_cont[0]['switch_skin_block'] = templates_select_txt();
    } else {
        $_page_cont[0]['switch_skin_block'] = '';
    }
    //Path to css
    if (strlen($_page['css_name']) > 0) {
        $filename = $dir['root'] . $site['css_dir'] . $_page['css_name'];
        if (file_exists($filename) && is_file($filename)) {
            $_page_cont[0]['styles'] = '<link href="' . $site['css_dir'] . $_page['css_name'] . '" rel="stylesheet" type="text/css" />';
        } else {
            $_page_cont[0]['styles'] = '';
        }
    } else {
        $_page_cont[0]['styles'] = '';
    }
    //Path to js
    if (strlen($_page['js_name']) > 0) {
        $filename = $dir['root'] . 'inc/js/' . $_page['js_name'];
        if (file_exists($filename) && is_file($filename)) {
            $langDelete = _t('_delete');
            $langLoading = _t('_loading ...');
            $langDeleteMessage = _t('_poll successfully deleted');
            $langMakeIt = _t('_make it');
            $lang_you_should_specify_member = _t('_You should specify at least one member');
            if ($site['js_init']) {
                $_page_cont[0]['java_script'] = $site['js_init'];
            }
            $_page_cont[0]['java_script'] .= <<<EOJ
<script type="text/javascript" language="javascript">
\tvar site_url = '{$site['url']}';
\tvar lang_delete = '{$langDelete}';
\tvar lang_loading = '{$langLoading}';
\tvar lang_delete_message = '{$langDeleteMessage}';
\tvar lang_make_it = '{$langMakeIt}';
\tvar lang_you_should_specify_member = '{$lang_you_should_specify_member}';
\t
\tvar iQSearchWindowWidth  = {$oTemplConfig->iQSearchWindowWidth};
\tvar iQSearchWindowHeight = {$oTemplConfig->iQSearchWindowHeight};
</script>
<script src="{$site['url']}inc/js/{$_page['js_name']}" type="text/javascript" language="javascript"></script>
EOJ;
        } else {
            $_page_cont[0]['java_script'] = '';
        }
    } else {
        $_page_cont[0]['java_script'] = '';
    }
    $_page_cont[0]['css_dir'] = $site['css_dir'];
    $_page_cont[0]['plugins'] = $site['plugins'];
    $_page_cont[0]['thumb_width'] = $max_thumb_width;
    $_page_cont[0]['thumb_height'] = $max_thumb_height;
    $_page_cont[0]['site_url'] = $site['url'];
    $_page_cont[0]['images'] = $site['images'];
    $_page_cont[0]['icons'] = $site['icons'];
    $_page_cont[0]['zodiac'] = $site['zodiac'];
    $_page_cont[0]['bottom_text'] = _t("_bottom_text", date("Y"));
    $_page_cont[0]['copyright'] = _t("_copyright", date("Y")) . getVersionComment();
    // please do not delete version for debug possibilities
    $_page_cont[0]['powered'] = getParam('enable_boonex_footers') ? _t("_powered_by_Dolphin") : '';
    $_page_cont[0]['main_logo'] = getMainLogo();
    //place meta data on site pages
    $_page_cont[0]['meta_keywords'] = process_line_output(getParam("MetaKeyWords"));
    $_page_cont[0]['meta_description'] = process_line_output(getParam("MetaDescription"));
    if (strlen($_page['extra_js'])) {
        $_page_cont[0]['extra_js'] = $_page['extra_js'];
    } else {
        $_page_cont[0]['extra_js'] = '';
    }
    if (strlen($_page['extra_css'])) {
        $_page_cont[0]['extra_css'] = $_page['extra_css'];
    } else {
        $_page_cont[0]['extra_css'] = '';
    }
    // top menu items
    $_page_cont[0]['top_menu'] = $oTemplMenu->getTopMenu();
    $_page_cont[0]['hidden_menu'] = getAllMenus();
    $_page_cont[0]['custom_menu'] = $oTemplMenu->getCustomMenu();
    $_page_cont[0]['TOP_Home'] = '<a class="menu_item_link" href="' . $site['url'] . 'index.php">' . _t("_Home") . '</a>';
    $_page_cont[0]['BMI_Home'] = '<a class="bottommenu"     href="' . $site['url'] . 'index.php">' . _t("_Home") . '</a>';
    $_page_cont[0]['TOP_About'] = '<a class="menu_item_link" href="' . $site['url'] . 'about_us.php">' . _t("_About Us") . '</a>';
    $_page_cont[0]['BMI_About'] = '<a class="bottommenu"     href="' . $site['url'] . 'about_us.php">' . _t("_About Us") . '</a>';
    $_page_cont[0]['TOP_Privacy'] = '<a class="menu_item_link" href="' . $site['url'] . 'privacy.php">' . _t("_Privacy") . '</a>';
    $_page_cont[0]['BMI_Privacy'] = '<a class="bottommenu"     href="' . $site['url'] . 'privacy.php">' . _t("_Privacy") . '</a>';
    $_page_cont[0]['TOP_Termsofuse'] = '<a class="menu_item_link" href="' . $site['url'] . 'terms_of_use.php">' . _t("_Terms_of_use") . '</a>';
    $_page_cont[0]['BMI_Termsofuse'] = '<a class="bottommenu"     href="' . $site['url'] . 'terms_of_use.php">' . _t("_Terms_of_use") . '</a>';
    $_page_cont[0]['TOP_Services'] = '<a class="menu_item_link" href="' . $site['url'] . 'services.php">' . _t("_Services") . '</a>';
    $_page_cont[0]['BMI_Services'] = '<a class="bottommenu"     href="' . $site['url'] . 'services.php">' . _t("_Services") . '</a>';
    $_page_cont[0]['TOP_FAQ'] = '<a class="menu_item_link" href="' . $site['url'] . 'faq.php">' . _t("_FAQ") . '</a>';
    $_page_cont[0]['BMI_FAQ'] = '<a class="bottommenu"     href="' . $site['url'] . 'faq.php">' . _t("_FAQ") . '</a>';
    $_page_cont[0]['TOP_Articles'] = '<a class="menu_item_link" href="' . $site['url'] . 'articles.php">' . _t("_Articles") . '</a>';
    $_page_cont[0]['BMI_Articles'] = '<a class="bottommenu"     href="' . $site['url'] . 'articles.php">' . _t("_Articles") . '</a>';
    $_page_cont[0]['TOP_Stories'] = '<a class="menu_item_link" href="' . $site['url'] . 'stories.php">' . _t("_Stories2") . '</a>';
    $_page_cont[0]['BMI_Stories'] = '<a class="bottommenu"     href="' . $site['url'] . 'stories.php">' . _t("_Stories2") . '</a>';
    $_page_cont[0]['TOP_Links'] = '<a class="menu_item_link" href="' . $site['url'] . 'links.php">' . _t("_Links") . '</a>';
    $_page_cont[0]['BMI_Links'] = '<a class="bottommenu"     href="' . $site['url'] . 'links.php">' . _t("_Links") . '</a>';
    $_page_cont[0]['TOP_News'] = '<a class="menu_item_link" href="' . $site['url'] . 'news.php">' . _t("_News") . '</a>';
    $_page_cont[0]['BMI_News'] = '<a class="bottommenu"     href="' . $site['url'] . 'news.php">' . _t("_News") . '</a>';
    $_page_cont[0]['TOP_Aff'] = getParam("enable_aff") == 'on' ? '<a class="menu_item_link" href="' . $site['url'] . 'affiliates.php">' . _t("_Affiliates") . '</a>' : '';
    $_page_cont[0]['BMI_Aff'] = getParam("enable_aff") == 'on' ? '<a class="bottommenu"     href="' . $site['url'] . 'affiliates.php">' . _t("_Affiliates") . '</a>' : '';
    $_page_cont[0]['TOP_Invitefriend'] = '<a class="menu_item_link" href="javascript:void(0);" onclick="return launchTellFriend();">' . _t("_Invite a friend") . '</a>';
    $_page_cont[0]['BMI_Invitefriend'] = '<a class="bottommenu"     href="javascript:void(0);" onclick="return launchTellFriend();">' . _t("_Invite a friend") . '</a>';
    $_page_cont[0]['TOP_Contacts'] = '<a class="menu_item_link" href="' . $site['url'] . 'contact.php">' . _t("_Contacts") . '</a>';
    $_page_cont[0]['BMI_Contacts'] = '<a class="bottommenu"     href="' . $site['url'] . 'contact.php">' . _t("_Contacts") . '</a>';
    $_page_cont[0]['TOP_Browse'] = '<a class="menu_item_link" href="' . $site['url'] . 'browse.php">' . _t("_Browse Profiles") . '</a>';
    $_page_cont[0]['BMI_Browse'] = '<a class="bottommenu"     href="' . $site['url'] . 'browse.php">' . _t("_Browse Profiles") . '</a>';
    $_page_cont[0]['TOP_Feedback'] = '<a class="menu_item_link" href="' . $site['url'] . 'story.php">' . _t("_Add story") . '</a>';
    $_page_cont[0]['BMI_Feedback'] = '<a class="bottommenu"     href="' . $site['url'] . 'story.php">' . _t("_Add story") . '</a>';
    $_page_cont[0]['TOP_ContactUs'] = '<a class="menu_item_link" href="' . $site['url'] . 'contact.php">' . _t("_contact_us") . '</a>';
    $_page_cont[0]['BMI_ContactUs'] = '<a class="bottommenu"     href="' . $site['url'] . 'contact.php">' . _t("_contact_us") . '</a>';
    $_page_cont[0]['TOP_Bookmark'] = '<a class="menu_item_link" href="javascript:void(0);" onclick="addBookmark();">' . _t("_Bookmark") . '</a>';
    $_page_cont[0]['BMI_Bookmark'] = '<a class="bottommenu"     href="javascript:void(0);" onclick="addBookmark();">' . _t("_Bookmark") . '</a>';
    $_page_cont[0]['hello_member'] = HelloMemberSection();
    // member/visitor menu
    if ($logged['admin']) {
        $_page_cont[0]['menu_right'] = $oTemplMenu->loggedAdminMenu();
    } elseif ($logged['aff']) {
        $_page_cont[0]['menu_right'] = $oTemplMenu->loggedAffMenu();
    } elseif ($logged['moderator']) {
        $_page_cont[0]['menu_right'] = $oTemplMenu->loggedModeratorMenu();
    } elseif ($logged['member']) {
        $_page_cont[0]['menu_right'] = $oTemplMenu->loggedMemberMenu();
    } else {
        $_page_cont[0]['menu_right'] = $oTemplMenu->visitorMenu();
    }
    /*if ( !strlen($_page_cont[$ni]['actions_menu']) )
    		$_page_cont[0]['actions_menu'] = $oTemplMenu -> actionsMenu();*/
    if (!strlen($_page_cont[$ni]['add_to_header'])) {
        $_page_cont[0]['add_to_header'] = '';
    }
    $check_res = checkAction((int) $_COOKIE['memberID'], ACTION_ID_USE_IM);
    $enable_im = getParam("enable_im");
    if ($enable_im && $check_res[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED && !$ADMIN) {
        $_page_cont[0]['IM_title'] = _t("_IM title");
        $_page_cont[0]['IM'] = RetIM();
    } else {
        $_page_cont[0]['IM_title'] = "";
        $_page_cont[0]['IM'] = "";
    }
    $enable_shoutBox = 'on' == getParam("enable_shoutBox") ? 1 : 0;
    if ($enable_shoutBox) {
        $_page_cont[0]['shout_box'] = loadShoutbox();
    } else {
        $_page_cont[0]['shout_box'] = '';
    }
    $_page_cont[0]['top_page_head'] = getTopPageHead();
    // page header
    $_page_cont[0]['page_header'] = $_page['header'];
    $_page_cont[0]['page_header_text'] = $_page['header_text'];
    // banner rotation/shifting system
    if (strstr($templ, "__banner_top__")) {
        $_page_cont[0]['banner_top'] = banner_put_nv(1);
    }
    if (strstr($templ, "__banner_left__")) {
        $_page_cont[0]['banner_left'] = banner_put_nv(2);
    }
    if (strstr($templ, "__banner_right__")) {
        $_page_cont[0]['banner_right'] = banner_put_nv(3);
    }
    if (strstr($templ, "__banner_bottom__")) {
        $_page_cont[0]['banner_bottom'] = banner_put_nv(4);
    }
    //end of banner rotation/shifting system
    //--- Ray IM Integration ---//
    global $sRayHomeDir;
    $_page_cont[0]['ray_invite_js'] = "\r\n\t\t<script type=\"text/javascript\" language=\"javascript\">\r\n\t\t\tvar sRayUrl = '" . $site['url'] . $sRayHomeDir . "';\r\n\t\t</script>\r\n\t\t<script src=\"" . $site['url'] . "ray/modules/global/js/integration.js\" type=\"text/javascript\" language=\"javascript\"></script>";
    $_page_cont[0]['ray_invite_swf'] = "";
    $iId = (int) $_COOKIE['memberID'];
    $sPassword = getPassword($iId);
    $bEnableRay = getParam('enable_ray') == 'on';
    $check_res = checkAction($iId, ACTION_ID_USE_RAY_IM);
    if ($bEnableRay && $check_res[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED) {
        $_page_cont[0]['ray_invite_swf'] = getApplicationContent("im", "invite", array('id' => $iId, 'password' => $sPassword), true);
    }
    //--- Ray IM Integration ---//
    foreach ($_page_cont[0] as $key => $value) {
        $templ = str_replace("__{$key}__", $value, $templ);
    }
    if (is_array($_page_cont[$ni])) {
        foreach ($_page_cont[$ni] as $key => $value) {
            $templ = str_replace("__{$key}__", $value, $templ);
        }
    }
    header('Content-type: text/html; charset=utf-8');
    echo $templ;
}
    function TmplKeysReplace($m)
    {
        global $site;
        global $dir;
        global $logged;
        global $aPageContCache;
        global $_page_cont;
        global $oTemplConfig;
        global $_page;
        global $oTemplConfig;
        if (!isset($aPageContCache)) {
            $aPageContCache = array();
        }
        //if already generated it, return it.
        if (isset($aPageContCache[$m[1]])) {
            return $aPageContCache[$m[1]];
        }
        //if it already exists, return it
        if (isset($_page_cont[0]) and array_key_exists($m[1], $_page_cont[0])) {
            return $_page_cont[0][$m[1]];
        }
        $ni = $_page['name_index'];
        //if page generated it, return it
        if ($ni and isset($_page_cont[$ni]) and array_key_exists($m[1], $_page_cont[$ni])) {
            return $_page_cont[$ni][$m[1]];
        }
        //echoDbg( $m );
        $sRet = '';
        // now switch what we have
        switch ($m[1]) {
            case 'page_charset':
                $sRet = 'UTF-8';
                break;
                // it will be removed soon
            // it will be removed soon
            case 'site_url':
                $sRet = $site['url'];
                break;
            case 'plugins':
                $sRet = $site['plugins'];
                break;
            case 'images':
                $sRet = $site['images'];
                break;
            case 'css_dir':
                $sRet = $site['css_dir'];
                break;
            case 'icons':
                $sRet = $site['icons'];
                break;
            case 'zodiac':
                $sRet = $site['zodiac'];
                break;
            case 'switch_lang_block':
                $sRet = getLangSwitcher();
                break;
            case 'main_logo':
                $sRet = getMainLogo();
                break;
            case 'hello_member':
                $sRet = HelloMemberSection();
                break;
            case 'thumb_width':
                $sRet = getParam('max_thumb_width');
                break;
            case 'thumb_height':
                $sRet = getParam('max_thumb_height');
                break;
            case 'main_div_width':
                $sRet = getParam('main_div_width');
                break;
            case 'switch_skin_block':
                $sRet = getParam("enable_template") ? templates_select_txt() : '';
                break;
            case 'meta_keywords':
                $sRet = process_line_output(getParam("MetaKeyWords"));
                break;
            case 'meta_description':
                $sRet = process_line_output(getParam("MetaDescription"));
                break;
            case 'top_menu':
                $oMenu = new BxTemplMenu();
                $sRet = $oMenu->getCode();
                break;
            case 'extra_js':
                $sRet = $_page['extra_js'];
                break;
            case 'extra_css':
                $sRet = $_page['extra_css'];
                break;
            case 'page_header':
                $sRet = $_page['header'];
                break;
            case 'page_header_text':
                $sRet = $_page['header_text'];
                break;
            case 'banner_top':
                $sRet = banner_put_nv(1);
                break;
            case 'banner_left':
                $sRet = banner_put_nv(2);
                break;
            case 'banner_right':
                $sRet = banner_put_nv(3);
                break;
            case 'banner_bottom':
                $sRet = banner_put_nv(4);
                break;
            case 'bottom_text':
                $sRet = _t('_bottom_text', date('Y'));
                break;
            case 'copyright':
                $sRet = _t('_copyright', date('Y')) . getVersionComment();
                break;
                // please do not delete version for debug possibilities
                //Path to css
            // please do not delete version for debug possibilities
            //Path to css
            case 'styles':
                if (strlen($_page['css_name'])) {
                    $sFile = $dir['root'] . $site['css_dir'] . $_page['css_name'];
                    if (file_exists($sFile) && is_file($sFile)) {
                        $sRet = '
							<link href="' . $site['url'] . $site['css_dir'] . $_page['css_name'] . '" rel="stylesheet" type="text/css" />';
                    }
                }
                break;
                //Path to js
            //Path to js
            case 'java_script':
                if (strlen($_page['js_name'])) {
                    $sFile = $dir['root'] . 'inc/js/' . $_page['js_name'];
                    if (file_exists($sFile) && is_file($sFile)) {
                        $langDelete = _t('_delete');
                        $langLoading = _t('_loading ...');
                        $langDeleteMessage = _t('_poll successfully deleted');
                        $langMakeIt = _t('_make it');
                        $lang_you_should_specify_member = _t('_You should specify at least one member');
                        if ($site['js_init']) {
                            $sRet = $site['js_init'];
                        }
                        $sRet .= <<<EOJ
\t<script type="text/javascript" language="javascript">
\t\t\t\t\tvar site_url = '{$site['url']}';
\t\t\t\t\tvar lang_delete = '{$langDelete}';
\t\t\t\t\tvar lang_loading = '{$langLoading}';
\t\t\t\t\tvar lang_delete_message = '{$langDeleteMessage}';
\t\t\t\t\tvar lang_make_it = '{$langMakeIt}';
\t\t\t\t\tvar lang_you_should_specify_member = '{$lang_you_should_specify_member}';
\t\t\t\t\t
\t\t\t\t\tvar iQSearchWindowWidth  = {$oTemplConfig->iQSearchWindowWidth};
\t\t\t\t\tvar iQSearchWindowHeight = {$oTemplConfig->iQSearchWindowHeight};
\t</script>
\t<script src="{$site['url']}inc/js/{$_page['js_name']}" type="text/javascript" language="javascript"></script>
EOJ;
                    }
                }
                break;
                //--- Ray IM Integration ---//
            //--- Ray IM Integration ---//
            case 'ray_invite_js':
                $sRet = getRayIntegrationJS();
                break;
            case 'ray_invite_swf':
                if ($logged['member']) {
                    $iId = (int) $_COOKIE['memberID'];
                    $sPassword = getPassword($iId);
                    $bEnableRay = getParam('enable_ray');
                    $aCheckRes = checkAction($iId, ACTION_ID_USE_RAY_IM);
                    if ($bEnableRay && $aCheckRes[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED) {
                        $sRet = getApplicationContent('im', 'invite', array('id' => $iId, 'password' => $sPassword), true);
                    }
                }
                break;
                //--- Ray IM Integration ---//
            //--- Ray IM Integration ---//
            default:
                if (substr($m[1], 0, 4) == 'TOP_' or substr($m[1], 0, 4) == 'BMI_') {
                    // insert top and bottom links
                    global $aMainLinks;
                    $sPre = substr($m[1], 0, 4);
                    $sLinkName = substr($m[1], 4);
                    if (isset($aMainLinks[$sLinkName])) {
                        $aLink = $aMainLinks[$sLinkName];
                        $bShow = true;
                        if (isset($aLink['Check'])) {
                            $sFunc = create_function('', $aLink['Check']);
                            $bShow = $sFunc();
                        }
                        if ($bShow) {
                            $sClass = $sPre == 'TOP_' ? 'menu_item_link' : 'bottommenu';
                            $sOnclick = isset($aLink['onclick']) ? 'onclick="' . $aLink['onclick'] . '"' : '';
                            $sTitle = _t($aLink['Title']);
                            $sRet = <<<BLAH
\t\t\t\t\t\t\t\t<a href="{$site['url']}{$aLink['href']}" class="{$sClass}" {$sOnclick}>{$sTitle}</a>
BLAH;
                        }
                    } else {
                        $sRet = $m[0];
                    }
                    //return found string back
                } elseif (($sTemplAdd = TemplPageAddComponent($m[1])) !== false) {
                    $sRet = $sTemplAdd;
                } else {
                    $sRet = $m[0];
                }
                //return found string back
        }
        $aPageContCache[$m[1]] = $sRet;
        return $sRet;
    }