<?php
##############################################################
##In-portal                                     ##
##############################################################
##                   In-portal                     ##
##             Intechnic Corporation               ##
##          All Rights Reserved, 1998-2002            ##
##                                           ## 
## No portion of this code may be copied, reproduced or  ## 
##    otherwise redistributed without proper written     ##
##   consent of Intechnic Corporation.  Violation will      ##
##    result in revocation of the license and support    ##
##  privileges along maximum prosecution allowed by law. ##
##############################################################
if( !(isset($pathtoroot) && $pathtoroot) )
{
  $path=dirname(realpath(__FILE__));
  if(strlen($path))
  {
    /* determine the OS type for path parsing */
    $pos = strpos($path,":");
    if ($pos === false)
    {
      $gOS_TYPE="unix";
      $pathchar = "/";
    }
    else
    {
      $gOS_TYPE="win";
      $pathchar="\\";
    }
    $p = $path.$pathchar;
    /*Start looking for the root flag file */
    while(!strlen($pathtoroot) && strlen($p))
    {
      $sub = substr($p,strlen($pathchar)*-1);
      if($sub==$pathchar)
      {
        $filename = $p."root.flg";
      }
      else
        $filename = $p.$pathchar."root.flg";
      if(file_exists($filename))
      {
        $pathtoroot = $p;
      }
      else
      {
        $parent = realpath($p.$pathchar."..".$pathchar);
   if($parent!=$p)
   {
     $p = $parent;
   }
   else
     $p = "";
      }
    }
    if( !(isset($pathtoroot) && $pathtoroot) )
      $pathtoroot = ".".$pathchar;
  }
  else
  {
    $pathtoroot = ".".$pathchar;
  }
}

$sub = substr($pathtoroot,strlen($pathchar)*-1);
if($sub!=$pathchar)
{
  $pathtoroot = $pathtoroot.$pathchar;
}
//echo $pathtoroot;

require_once($pathtoroot."kernel/startup.php");
//admin only util
$rootURL="http://".ThisDomain().$objConfig->Get("Site_Path");
$admin = $objConfig->Get("AdminDirectory");
if(!strlen($admin))
    $admin = "admin";
$adminURL = $rootURL.$admin;
$imagesURL = $adminURL."/images"; 
require_once ($pathtoroot.$admin."/include/elements.php"); 
require_once ($pathtoroot."kernel/admin/include/navmenu.php"); 
require_once($pathtoroot.$admin."/toolbar.php");
require_once($pathtoroot.$admin."/editor/cmseditor/fckeditor.php");

$style_sheet_global = $adminURL."/include/style.css";
?>
<html>
   <head>
      <title>Online Editor</title>
      <?php print "<link rel=\"stylesheet\" type=\"text/css\" href=\"$style_sheet_global\">\n"; ?>
      <?php require_once($pathtoroot.$admin."/include/mainscript.php"); ?>
      <script>
      function HasParam(param)
	{
		// checks of parameter is passed to function (cross-browser)
		return typeof(param) == 'undefined' ? false : true;
	}
      </script>
   </head>
   <body marginwidth="0" leftmargin="0" topmargin="0" marginheight="0" style="overflow:auto" onload="update_content()">
<table width="100%" height="100%" cellspacing="0" cellpadding="0" border="0" align="center"><tr><td align="left" valign="top">
<?php
   $section=$_GET["section"];
   $objListToolBar = new clsToolBar();

   $objListToolBar->Set("section",$section);
   $objListToolBar->Set("load_menu_func","");
   $objListToolBar->Set("CheckClass","");
  
   $listImages = array();
                    //$img, $alt, $link, $onMouseOver, $onMouseOut, $onClick

   $objListToolBar->Add("select", "la_ToolTip_Select","#","swap('select','toolbar/tool_select_f2.gif');",
                        "swap('select', 'toolbar/tool_select.gif');",
                        "document.frm.submit();","tool_select.gif");

   $objListToolBar->Add("cancel", "la_ToolTip_Stop","#","swap('cancel','toolbar/tool_stop_f2.gif');",
                    "swap('cancel', 'toolbar/tool_stop.gif');","window.close();","tool_stop.gif");

   $title = "Online HTML Editor";

   $objSections->SetCurrentSection($section);
   print $objSections->section_header($envar,NULL,$title);
   $TargetForm = $_GET["TargetForm"];
   $TargetField = $_GET["TargetField"];
   echo $objListToolBar->Build();
?>
  <form name="frm" action="javascript:update_opener();">
<?php  
	$oFCKeditor = new FCKeditor('Content');
	
	$oFCKeditor->BasePath	= 'cmseditor/' ;
	$oFCKeditor->Width		= '100%' ;
	$oFCKeditor->Height		= '500' ;
	$oFCKeditor->ToolbarSet	= 'Advanced' ;
	$oFCKeditor->Value		= '' ;
	$oFCKeditor->Config = Array(
		//'UserFilesPath' => $pathtoroot.'kernel/user_files',
		'ProjectPath' => $objConfig->Get("Site_Path"),
		'CustomConfigurationsPath' => $rootURL.'admin/editor/inp_fckconfig.js',
		'EditorAreaCSS' => GetThemeCSS(),
		//'StylesXmlPath' => '../../inp_styles.xml',
		//'Debug' => 1,
	);
	
	function GetThemeCSS()
	{
		$query = 'SELECT LastCompiled, '.GetTablePrefix().'Stylesheets.Name as StyleName FROM '.GetTablePrefix().'Theme
							LEFT JOIN '.GetTablePrefix().'Stylesheets
							ON '.GetTablePrefix().'Theme.StylesheetId = '.GetTablePrefix().'Stylesheets.StylesheetId
							WHERE	ThemeId = 4';

		$conn =& GetADODBConnection();
		
		$res = $conn->GetRow($query);
					
		$last_compiled = $res['LastCompiled'];
			
		$style_name = strtolower( $res['StyleName'] );
		
		global $pathtoroot,$rootURL;
		
		$css_path = $pathtoroot.'kernel/stylesheets';
		
		$css_url = $rootURL.'kernel/stylesheets';
		
		if( file_exists($css_path.'/'.$style_name.'-'.$last_compiled.'.css') )
		{
			$ret = $css_url.'/'.$style_name.'-'.$last_compiled.'.css';
		}
		return $ret;
	}
	
  echo $oFCKeditor->CreateHtml() ;            
?>	
	</FORM>  
      </td></tr></table>
      
    </form>

   </body>
   <script>
<?php
print <<<END
   function update_content()
   {
     if (window.opener)
     {
        if (!window.opener.closed)
        {
           bf = window.opener.document.$TargetForm;
//           if (typeof (bf.$TargetField.value) != 'undefined') {
//           	current = bf.$TargetField.value;
//           }
//           else {
						current = window.opener.document.getElementById('$TargetField').value;
//           }
           //f = document.getElementById('editform');
           d = document.getElementById('Content');
           d.value = current;
        } 
        else 
        	window.close()
     }
     else 
     	window.close()
    }
         
    function update_opener()
    {       
       d = document.getElementById('Content');      
       if (d.form)
         if (d.form.onsubmit)
            d.form.onsubmit()

       if (!window.opener) return;
       if (!window.opener.closed)
       
//       if (typeof (bf.$TargetField.value) != 'undefined') {
//         current = bf.$TargetField;
//       }
//       else {
				current = window.opener.document.getElementById('$TargetField');
//       }
       
       current.value = d.value;
       //alert('Setting bf.$TargetField.value to'+d.value); 	
       window.close();
    }
         
END;
?>
//   setTimeout('update_content();',100);
   </script>
</html>