<?php
/**
 * The main GUI for the ImageManager.
 * Modifications for Mambo TinyMCE - Ryan Demmer Dec 2004
 * Portions from remository.php
 * @Original author $Author: Wei Zhuo $
 * @version $Id: manager.php 26 2004-03-31 02:35:21Z Wei Zhuo $
 * @package ImageManager
 */
define( '_VALID_MOS', 1 );

$base_path = '../../../../../../../../';

//Start Authorisation
//Some includes
include_once ($base_path."configuration.php");
$tinyMCE_path = $mosConfig_absolute_path."/mambots/editors/mosce/jscripts/tiny_mce/";
$tinyMCE_URL = $mosConfig_live_site."/mambots/editors/mosce/jscripts/tiny_mce";

include_once ($tinyMCE_path."auth_plugin.php");
include ($tinyMCE_path."mosce_config.php");
include ($tinyMCE_path."plugins/imgmanager/langs/$editor_lang.php");

//Reset auth variables
$isAdmin=False;
$isUser=False;
$upload_auth=False;
$new_folder_auth=False;

//Check for access rights

//Check for Admin rights
$isAdmin=checkType('admin');

//Check for User rights
if (!$isAdmin) {
     $isUser=checkType('user');
     $upload_auth=checkType('IM_upload');
     $new_folder_auth=checkType('IM_new_folder');
   }
if ($isAdmin || $isUser) {

//End Authorisation

include_once ($tinyMCE_path."plugins/imgmanager/ImageManager/config.inc.php");
include_once ($tinyMCE_path."plugins/imgmanager/ImageManager/Classes/ImageManager.php");
include_once ($tinyMCE_path."plugins/imgmanager/ImageManager/Classes/Functions.php");

//Help file check
if (file_exists($tinyMCE_path."plugins/imgmanager/ImageManager/docs/".$editor_lang)){
  $help_lang = $editor_lang;
}else{
  $help_lang = 'en';
}

$manager = new ImageManager($IMConfig);
$dirs = $manager->getDirs();
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
     <title><?php echo _imgmanager_desc ?></title>
     <meta http-equiv="Content-Type" content="text/html; charset=<?php echo _imgmanager_iso;?>" />
     <meta http-equiv="Pragma" content="no-cache" />
     <meta http-equiv="Cache-Control" content="no-cache" />
     <meta http-equiv="Expires" content="Fri, Oct 24 1976 00:00:00 GMT" />
     <link href="<?php echo $tinyMCE_URL;?>/plugins/imgmanager/ImageManager/assets/manager.css" rel="stylesheet" type="text/css" />
     <script type="text/javascript" src="<?php echo $tinyMCE_URL;?>/plugins/imgmanager/ImageManager/assets/popup.js">
     </script>
     <script type="text/javascript" src="<?php echo $tinyMCE_URL;?>/plugins/imgmanager/ImageManager/assets/dialog.js">
     </script>
     <script language="javascript" src="<?php echo $tinyMCE_URL;?>/tiny_mce_popup.js" type="text/javascript">
     </script>
     <script type="text/javascript">
     /*<![CDATA[*/
        var thumbdir = "<?php echo $IMConfig['thumbnail_dir']; ?>";
        var base_url = "<?php echo $manager->getBaseURL(); ?>";
        var hostUrl = "<?php echo $mosConfig_live_site; ?>";
        var defVSpace = "<?php echo $editor_im_def_vspace; ?>";
        var defHSpace = "<?php echo $editor_im_def_hspace; ?>";
        var defBorder = "<?php echo $editor_im_def_border; ?>";
        var defAlign = "<?php echo $editor_im_def_align; ?>";
        var editor_im_thumbs = "<?php echo $editor_im_thumbs; ?>";
        var editor_popup_mode = "<?php echo $editor_im_popup_mode; ?>";
     /*]]>*/
     </script>
     <script type="text/javascript">
     //tinyMCE.setWindowArg('mce_windowresize', false);
     /**
     * Javascript Functions for the ImageManager
     */
        function myRegexpReplace(in_str, reg_exp, replace_str, opts) {
        if (typeof opts == "undefined")
            opts = 'g';
        var re = new RegExp(reg_exp, opts);
        return in_str.replace(re, replace_str);
        }
        //set the alignment options
        function setAlign(align)
        {
                var selection = document.getElementById('f_align');
                for(var i = 0; i < selection.length; i++)
                {
                        if(selection.options[i].value == align)
                        {
                                selection.selectedIndex = i;
                                break;
                        }
                }
        }
        //initialise the form
        //Added modifications for TinyMCE - Ryan Demmer
        function init()
        {
                    var uploadForm = document.getElementById('uploadForm');
                    if(uploadForm) uploadForm.target = 'imgManager';

                    var formObj = document.forms[0];
                    var arrOnOver = new Array(), arrOnOut  = new Array();
                    var strOnOver = "", strOnOut  = "";
                    var leftstr = "", rightstr = "", namestr = "", modestr = "", widthstr = "", heightstr = "";
                    
                    formObj.main.checked = true;
                    document.getElementById('im_main').style.border = "2px dotted #B6BDD2";

                    var arrOnClick = new Array(), arrFeatures = new Array(), arrPopJS = new Array();
                    var strOnClick = '', strDocReturn = '', strPopJS = '';
                    //New popThumb.php Image Popup
                    popup_href = unescape(tinyMCE.getWindowArg('href'));
                    arrOnClick = popup_href.split(';');
                    for (var i=0; i<arrOnClick.length; i++) {
                        if (arrOnClick[i].indexOf('javascript:void window.open')!=-1) {
                            strOnClick = arrOnClick[i];
                            strOnClick = strOnClick.substring(12,strOnClick.length-1);
                            formObj.popupenable.checked=true;
                            break;
                        }
                    }
                    //Old thumbPopup.js Image Popup
                    popup_href = unescape(tinyMCE.getWindowArg('href'));
                    arrPopJS = tinyMCE.getWindowArg('href').split(';');
                    for (var i=0; i<arrPopJS.length; i++) {
                        if (arrPopJS[i].indexOf('javascript:thumbPopup')!=-1) {
                            strPopJS = arrPopJS[i];
                            strPopJS = strPopJS.substring(12,strPopJS.length-1);
                            formObj.popupenable.checked=true;
                            break;
                        }
                    }
                    //Get default Popup mode from config
                    var mode_selec = document.getElementById('popup_mode');
                        for(var i = 0; i < mode_selec.length; i++)
                            {
                                if(mode_selec.options[i].value == editor_popup_mode)
                                {
                                    mode_selec.selectedIndex = i;
                                    break;
                                }
                            }
                    if (tinyMCE.getWindowArg('caption') == "1")
                            formObj.thumbcaption.checked == true;
                    if (formObj.popupenable.checked == true) {
                        //thumbPopup.js
                        if (strPopJS!='') {
                            // get the win features - thumbPopup.js
                            strPopJS = myRegexpReplace(strPopJS, "'", "", "gi");
                            arrFeatures = strPopJS.split(',');
                            if (arrFeatures[0]!='') // link , url
                                formObj.popupthumb.value = unescape(arrFeatures[0].substring( arrFeatures[0].indexOf( "(" ) +1));
                                formObj.thumbWidth = arrFeatures[2];
                                formObj.thumbHeight.value = arrFeatures[3];
                                formObj.original_width_thumb.value = arrFeatures[2];
                                formObj.original_height_thumb.value = arrFeatures[3];
                                formObj.thumbname.value = tinyMCE.getWindowArg('alt');
                                //Alt and Caption values
                                formObj.f_alt.value = tinyMCE.getWindowArg('alt');
                                //Image Src and Thumb src values
                                formObj.f_url.value = tinyMCE.getWindowArg('src');
                                //Width & Height
                                formObj.f_width.value = tinyMCE.getWindowArg('width');
                                formObj.f_height.value = tinyMCE.getWindowArg('height');
                                formObj.original_width.value = tinyMCE.getWindowArg('width');
                                formObj.original_height.value = tinyMCE.getWindowArg('height');
                        }
                        //popupThumb.php
                        if (strOnClick!='') {
                            // get the win features - popupThumb.php
                            arrFeatures = strOnClick.split(',');
                            if (arrFeatures[0]!='') // link , url
                                
                                leftstr = myRegexpReplace(arrFeatures[0],"'","","gi");
                                leftstr = leftstr.substring( leftstr.indexOf( '?' ) -1);
                                leftstr = leftstr.substring( leftstr.indexOf( '=' ) +1, leftstr.length );
                                rightstr = leftstr.substring( leftstr.indexOf( '&' ) +1, leftstr.length );
                                imagestr = myRegexpReplace(leftstr, "&"+rightstr+"", "", "gi");
                                imagestr = unescape(imagestr);
                                //Determine return values for new Popup style
                                if (rightstr.indexOf('title=') == 0){
                                    namestr = rightstr.substring( rightstr.indexOf( 'title=' ) +6, rightstr.length );
                                    modestr = namestr.substring( namestr.indexOf( '&' ) +0, namestr.length );
                                    namestr = myRegexpReplace(namestr, modestr, '', 'gi');
                                    namestr = unescape(namestr);

                                    modestr = myRegexpReplace(modestr, '&mode=', '', 'gi');
                                    dimstr = modestr.substring( modestr.indexOf( '&' ) +0, modestr.length );
                                    modestr = myRegexpReplace(modestr, dimstr, '', 'gi');

                                    if (modestr == '' || modestr == 'null' || modestr == 'undefined')
                                        modestr = editor_popup_mode;
                                      
                                    var mode_selec = document.getElementById('popup_mode');
                                    for(var i = 0; i < mode_selec.length; i++)
                                    {
                                            if(mode_selec.options[i].value == modestr)
                                            {
                                                    mode_selec.selectedIndex = i;
                                                    break;
                                            }
                                    }
                                    //Width and Height
                                    dimstr = myRegexpReplace(dimstr, '&imgwidth=', '', 'gi');
                                    heightstr = dimstr.substring( dimstr.indexOf( '&' ) +0, dimstr.length );
                                    widthstr = myRegexpReplace(dimstr, heightstr, '', 'gi');
                                    heightstr = myRegexpReplace(heightstr, '&imgheight=', '', 'gi');
                                    
                                    //Width & Height
                                    formObj.thumbWidth.value = widthstr;
                                    formObj.thumbHeight.value = heightstr;
                                    formObj.original_width_thumb.value = widthstr;
                                    formObj.original_height_thumb.value = heightstr;
                                }
                                //Get return values for old Popup style
                                if (rightstr.indexOf('imgwidth=') == 0){
                                  widthstr = rightstr.substring( rightstr.indexOf( 'imgwidth' ) +8, rightstr.length );
                                  widthstr = widthstr.substring ( widthstr.indexOf('=') + 1, rightstr.length );
                                  heightstr = widthstr.substring ( widthstr.indexOf('&') + 0, widthstr.length );
                                  widthstr = myRegexpReplace(widthstr, heightstr, '', 'gi');

                                  heightstr = heightstr.substring ( heightstr.indexOf('=') + 1, heightstr.length );
                                  namestr = heightstr.substring ( heightstr.indexOf('&') +0, heightstr.length );
                                  heightstr = myRegexpReplace(heightstr, namestr, '', 'gi');
                                  namestr = namestr.substring ( namestr.indexOf('=') +1, namestr.length );

                                  formObj.thumbWidth.value = widthstr;
                                  formObj.thumbHeight.value = heightstr;
                                  formObj.original_width_thumb.value = widthstr;
                                  formObj.original_height_thumb.value = heightstr;
                                  formObj.thumbname.value = namestr;
                                }
                                
                                //Alt and Caption values
                                formObj.f_alt.value = tinyMCE.getWindowArg('alt');
                                formObj.thumbname.value = namestr;
                                //Image Src and Thumb src values
                                //Hotlinking support
                                if (imagestr.indexOf('http://') == 0 || imagestr.indexOf('https://') == 0){
                                    formObj.popupthumb.value = imagestr;
                                    formObj.offsite.checked = true;
                                }else{
                                    formObj.popupthumb.value = hostUrl+'/'+imagestr;
                                }
                                formObj.f_url.value = tinyMCE.getWindowArg('src');
                                //Width & Height
                                formObj.f_width.value = tinyMCE.getWindowArg('width');
                                formObj.f_height.value = tinyMCE.getWindowArg('height');
                                formObj.original_width.value = tinyMCE.getWindowArg('width');
                                formObj.original_height.value = tinyMCE.getWindowArg('height');
                        }
                    }else{
                        formObj.f_url.value = tinyMCE.getWindowArg('src');
                        formObj.f_alt.value = tinyMCE.getWindowArg('alt');
                        formObj.f_width.value = tinyMCE.getWindowArg('width');
                        formObj.f_height.value = tinyMCE.getWindowArg('height');
                        formObj.original_width.value = tinyMCE.getWindowArg('width');
                        formObj.original_height.value = tinyMCE.getWindowArg('height');
                    }
                    //Align, Vspace, Hspace, Border, Popup Mode
                        var selection = document.getElementById('f_align');
                        var alignValue = (tinyMCE.getWindowArg('align') == '')?defAlign:tinyMCE.getWindowArg('align');
                        for(var i = 0; i < selection.length; i++)
                        {
                                if(selection.options[i].value == alignValue)
                                {
                                        selection.selectedIndex = i;
                                        break;
                                }
                        }
                        formObj.f_border.value = (tinyMCE.getWindowArg('border') == '')?defBorder:tinyMCE.getWindowArg('border');
                        formObj.f_vert.value = (tinyMCE.getWindowArg('vspace') == '')?defVSpace:tinyMCE.getWindowArg('vspace');
                        formObj.f_horiz.value = (tinyMCE.getWindowArg('hspace') == '')?defHSpace:tinyMCE.getWindowArg('hspace');
                        
                    // supporting onmouse over / out for image swap ...Michael Keck (me@michaelkeck.de)
                    arrOnOver = tinyMCE.getWindowArg('onmouseover').split(';');
                    arrOnOut = tinyMCE.getWindowArg('onmouseout').split(';');
                    for (var i=0; i<arrOnOver.length; i++) {
                    if (arrOnOver[i].indexOf('this.src=\'')!=-1) {
                        strOnOver = arrOnOver[i];
                        break;
                        }
                    }
                    for (var i=0; i<arrOnOut.length; i++) {
                         if (arrOnOut[i].indexOf('this.src=\'')!=-1) {
                         strOnOut = arrOnOut[i];
                         break;
                         }
                    }
                    if (strOnOver!='') {
                        strOnOver = myRegexpReplace(strOnOver,"this.src='","","gi");
                        strOnOver = myRegexpReplace(strOnOver,"'","","gi");
                        strOnOver = myRegexpReplace(strOnOver,";","","gi");
                    }
                    if (strOnOut!='') {
                        strOnOut = myRegexpReplace(strOnOut,"this.src='","","gi");
                        strOnOut = myRegexpReplace(strOnOut,"'","","gi");
                        strOnOut = myRegexpReplace(strOnOut,";","","gi");
                    }
                    if (strOnOver!='' && strOnOut!='') {
                        setOnMouseInput('enabled');
                        formObj.onmousemove.checked = true;
                        formObj.onmouseover.value   = strOnOver;
                        formObj.onmouseout.value    = strOnOut;
                        if (formObj.f_width.value !='' && formObj.f_height.value !=''){
                          formObj.onmousewidth.checked = true;
                          formObj.onmouseheight.checked = true;
                        }
                    } else {
                    setOnMouseInput('disabled');
                    formObj.onmousemove.checked = false;
                    formObj.onmouseover.value   = '';
                    formObj.onmouseout.value    = '';
                    }
                    if (formObj.popupenable.checked == false)
                        setPopupImage('disabled');

                    document.forms[0].insert.value = tinyMCE.getLang('lang_' + tinyMCE.getWindowArg('action'));
                    window.focus();
        }
        //Added modifications for TinyMCE - Ryan Demmer
        function onCancel()
        {
                top.close();
                return false;
        };
        // supporting onmouse over / out for image swap ...by Michael Keck (me@michaelkeck.de)
        // this function is needed for visual show, if onmouse over/out available
            function setOnMouseInput(stat){
                var formObj = document.forms[0];
                if (stat=='enabled') {
                    formObj.onmouseover.disabled = false;
                    formObj.onmouseout.disabled  = false;
                    formObj.popupenable.disabled = true;
                    formObj.thumbcaption.disabled = true;
                    formObj.thumbWidth.disabled = true;
                    formObj.thumbHeight.disabled = true;
                    formObj.thumbname.disabled = true;

                    if (document.getElementById) {
                        document.getElementById('showInput1').style.color="#000000";
                        document.getElementById('showInput2').style.color="#000000";
                        document.getElementById('showInput6').style.color="#000000";
                        
                    }
                    formObj.onmouseout.value     = document.getElementById('f_url').value;
                } else {
                    formObj.onmouseover.disabled = true;
                    formObj.onmouseout.disabled  =true;
                    if (editor_im_thumbs == 'true'){
                        formObj.popupenable.disabled = false;
                        formObj.thumbcaption.disabled = false;
                        formObj.thumbWidth.disabled = false;
                        formObj.thumbHeight.disabled = false;
                        formObj.thumbname.disabled = false;
                    }
                    if (document.getElementById) {
                        document.getElementById('showInput1').style.color="#666666";
                        document.getElementById('showInput2').style.color="#666666";
                        document.getElementById('showInput6').style.color="#666666";
                        
                    }
                }
            }
        // supporting onmouse over / out for image swap ...by Michael Keck (me@michaelkeck.de)
        // this function is needed for visual show, if onmouse over/out available
            function setPopupImage(stat){
                var formObj = document.forms[0];
                if (stat=='enabled') {
                    formObj.popupthumb.disabled = false;
                    formObj.thumbcaption.disabled = false;
                    formObj.thumbWidth.disabled = false;
                    formObj.thumbHeight.disabled = false;
                    formObj.thumbname.disabled = false;
                    formObj.onmousemove.disabled = true;
                    formObj.onmouseheight.disabled = true;
                    formObj.onmousewidth.disabled = true;
                    if (document.getElementById) {
                        document.getElementById('showInput3').style.color="#000000";
                        document.getElementById('showInput4').style.color="#000000";
                        document.getElementById('showInput5').style.color="#000000";
                        document.getElementById('showInput7').style.color="#000000";
                    }
                } else {
                    formObj.popupthumb.disabled = true;
                    formObj.thumbWidth.disabled = true;
                    formObj.thumbHeight.disabled = true;
                    formObj.thumbname.disabled = true;
                    formObj.onmousemove.disabled = false;
                    formObj.onmouseheight.disabled = false;
                    formObj.onmousewidth.disabled = false;
                    if (document.getElementById) {
                        document.getElementById('showInput3').style.color="#666666";
                        document.getElementById('showInput4').style.color="#666666";
                        document.getElementById('showInput5').style.color="#666666";
                        document.getElementById('showInput7').style.color="#666666";
                    }
                }
            }
                //Added modifications for TinyMCE - Ryan Demmer
                //Added Mouseover and Popup functionality - Ryan Demmer Feb 2005
                function onOK()
                {
                var formObj = document.forms[0];
                if (formObj.f_url.value == ''){
                    alert('Please choose an image or cancel.');
                    return false;
                }
                          if (window.opener) {
                                var iSrc = formObj.f_url.value;
                                var iWidth = formObj.f_width.value;
                                var iHeight = formObj.f_height.value;
                                var iName = formObj.f_alt.value;
                                var iAlign = formObj.f_align.options[formObj.f_align.selectedIndex].value;
                                var iVspace = formObj.f_vert.value;
                                var iHspace = formObj.f_horiz.value;
                                var iBorder = formObj.f_border.value;
                                //Hotlinking support
                                var pSrc = formObj.popupthumb.value;
                                var pSrcShrt = (formObj.offsite.checked)? pSrc : pSrc.replace(hostUrl+'/', '', 'gi');

                                var pWidth = formObj.thumbWidth.value;
                                var pHeight = formObj.thumbHeight.value;
                                var pName = formObj.thumbname.value;
                                var pMode = formObj.popup_mode.value;
                                
                                var winl = (screen.width - pWidth) / 2;
                               	var wint = (screen.height - pHeight) / 2;

                                var features =
                                    'location='    + 'no'  +
                    				',menubar='     + 'no'  +
                    				',scrollbars='  + 'no'  +
                    				',status='      + 'no'  +
                    				',toolbar='     + 'no'  +
                    				',resizable='   + 'no'  +
                                    ',screenX='     + winl +
                                    ',screenY='     + wint;
                                    //Changed '?src' to ?img to support hotlinking 29/05/2005 Ryan Demmer
                                        var phref = 'javascript:void window.open(';
                                            phref += "'" + hostUrl + "/mambots/editors/mosce/jscripts/tiny_mce/popupImage.php?img=";
                                            phref += "" + pSrcShrt + "";
                                            phref += '&title=';
                                            phref += "" + pName + "";
                                            phref += '&mode=';
                                            phref += "" + pMode + "";
                                            phref += '&imgwidth=';
                                            phref += "" + pWidth + "";
                                            phref += '&imgheight=';
                                            phref += "" + pHeight + "',";
                                            phref += "'Popup','";
                                            phref += 'width=';
                                            phref += "" + pWidth + ",";
                                            phref += 'height=';
                                            phref += "" + pHeight + ",";
                                            phref += "" + features + "');";

                                        var tcaption = (formObj.thumbcaption.checked == true)?true:false;
                                        var tpopup = (formObj.popupenable.checked == true)?true:false;
                                        var imouseover = (formObj.onmousemove.checked == true)?true:false;

                                        phref = unescape(phref);
                                        var args = new Array();
                                            args["phref"] = phref;
                                            args["pSrc"] = pSrc;
                                            args["pName"] = pName;
                                            args["pHeight"] = pHeight;
                                            args["pWidth"] = pWidth;
                                            args["tcaption"] = tcaption;
                                            args["tpopup"] = tpopup;

                                            args["iSrc"] = iSrc;
                                            args["iBorder"] = iBorder;
                                            args["iAlign"] = iAlign;
                                            args["iVspace"] = iVspace;
                                            args["iHspace"] = iHspace;
                                            args["iName"] = iName;
                                            args["iHeight"] = iHeight;
                                            args["iWidth"] = iWidth;
                                            args["ionmouseover"] = formObj.onmouseover.value;
                                            args["ionmouseout"] = formObj.onmouseout.value;
                                            
                                            // supporting onmouse over / out for image swap ...by Michael Keck (me@michaelkeck.de)
                                              // only support the onmouse over/out if both values are given
                                              if (args["ionmouseover"]!='' && args["ionmouseout"]!='' && document.forms[0].onmousemove.checked==true) {
                                              args["ionmouseover"]="this.src='" + args["ionmouseover"] + "';";
                                              args["ionmouseout"] ="this.src='" + args["ionmouseout"] + "';";
                                                  if (formObj.onmousewidth.checked==false)
                                                      args["iWidth"]="";
                                                  
                                                  if (formObj.onmouseheight.checked==false)
                                                      args["iHeight"]="";
                                              } else {
                                                args["ionmouseover"]="";
                                                args["ionmouseout"] ="";
                                              }
                                            window.opener.tinyMCE.execCommand("mceImageManager",false,args);
                                            top.close();
                    }
                };
                //similar to the Files::makeFile() in Files.php
                function makeURL(pathA, pathB)
                {
                        if(pathA.substring(pathA.length-1) != '/')
                                pathA += '/';

                        if(pathB.charAt(0) == '/');
                                pathB = pathB.substring(1);

                        return pathA+pathB;
                }
                function updateDir(selection)
                {
                        var newDir = selection.options[selection.selectedIndex].value;
                        changeDir(newDir);
                }
                function goUpDir()
                {
                        var selection = document.getElementById('dirPath');
                        var currentDir = selection.options[selection.selectedIndex].text;
                        if(currentDir.length < 2)
                                return false;
                        var dirs = currentDir.split('/');
                        var search = '';
                        for(var i = 0; i < dirs.length - 2; i++)
                        {
                                search += dirs[i]+'/';
                        }
                        for(var i = 0; i < selection.length; i++)
                        {
                                var thisDir = selection.options[i].text;
                                if(thisDir == search)
                                {
                                        selection.selectedIndex = i;
                                        var newDir = selection.options[i].value;
                                        changeDir(newDir);
                                        break;
                                }
                        }
                }
                function changeDir(newDir)
                {
                        if(typeof imgManager != 'undefined')
                                imgManager.changeDir(newDir);
                }
                
                function toggleConstrains(constrains)
                {
                        var lockImage = document.getElementById('imgLock');
                        var constrains = document.getElementById('constrain_prop');

                        if(constrains.checked)
                        {
                                lockImage.src = "img/islocked2.gif";
                                checkConstrains('width')
                        }
                        else
                        {
                                lockImage.src = "img/unlocked2.gif";
                        }
                }
                function checkConstrains(changed)
                {
                        //alert(document.form1.constrain_prop);
                        var constrains = document.getElementById('constrain_prop');

                        if(constrains.checked)
                        {
                                var obj = document.getElementById('original_width');
                                var original_width = parseInt(obj.value);
                                var obj = document.getElementById('original_height');
                                var original_height = parseInt(obj.value);

                                var widthObj = document.getElementById('f_width');
                                var heightObj = document.getElementById('f_height');

                                var width = parseInt(widthObj.value);
                                var height = parseInt(heightObj.value);

                                if(original_width > 0 && original_height > 0)
                                {
                                        if(changed == 'width' && width > 0) {
                                                heightObj.value = parseInt((width/original_width)*original_height);
                                        }

                                        if(changed == 'height' && height > 0) {
                                                widthObj.value = parseInt((height/original_height)*original_width);
                                        }
                                }
                        }
                }
                function toggleConstrains_pop(constrains)
                {
                        var lockImage = document.getElementById('imgLock_pop');
                        var constrains = document.getElementById('constrain_prop_pop');

                        if(constrains.checked)
                        {
                                lockImage.src = "img/islocked2.gif";
                                checkConstrains('width')
                        }
                        else
                        {
                                lockImage.src = "img/unlocked2.gif";
                        }
                }
                function checkConstrains_pop(changed)
                {
                        var constrains = document.getElementById('constrain_prop_pop');
                        if(constrains.checked)
                        {
                                var obj = document.getElementById('original_width_thumb');
                                var original_width = parseInt(obj.value);
                                var obj = document.getElementById('original_height_thumb');
                                var original_height = parseInt(obj.value);

                                var widthObj = document.getElementById('thumbWidth');
                                var heightObj = document.getElementById('thumbHeight');

                                var width = parseInt(widthObj.value);
                                var height = parseInt(heightObj.value);

                                if(original_width > 0 && original_height > 0)
                                {
                                        if(changed == 'width' && width > 0) {
                                                heightObj.value = parseInt((width/original_width)*original_height);
                                        }

                                        if(changed == 'height' && height > 0) {
                                                widthObj.value = parseInt((height/original_height)*original_width);
                                        }
                                }
                        }
                }
                function checkTableStyle(table)
                {
                  switch (table) {
                    case 'main' :
                        if (document.getElementById('main').checked == true) {
                          document.getElementById('im_main').style.border = "2px dotted #B6BDD2";
                          document.getElementById('im_option').style.border = "";
                        }
                    break;
                    case 'option' :
                        if (document.getElementById('option').checked == true) {
                          document.getElementById('im_option').style.border = "2px dotted #B6BDD2";
                          document.getElementById('im_main').style.border = "";
                    }
                    break;
                  }
                }

                function showMessage(newMessage)
                {
                        var message = document.getElementById('message');
                        var messages = document.getElementById('messages');
                        if(message.firstChild)
                                message.removeChild(message.firstChild);

                        message.appendChild(document.createTextNode(newMessage));
                        messages.style.display = "block";
                }
                function doUpload()
                {
                        var uploadForm = document.getElementById('uploadForm');
                        var uploaddlg = document.getElementById('uploaddlg');
                        if(uploadForm){
                                showMessage('Uploading');
                                uploaddlg.style.display = "none";
                        }
                }
                function refresh()
                {
                        var selection = document.getElementById('dirPath');
                        updateDir(selection);
                }
                function newFolder()
                {
                        var selection = document.getElementById('dirPath');
                        var dir = selection.options[selection.selectedIndex].value;
                        var newfolder = document.getElementById('folderdlg');

                        var folder = document.getElementById('folder').value;
                        if(folder == thumbdir)
                                        {
                                                alert('Invalid folder name, please choose another folder name.');
                                                return false;
                                        }
                        if (folder && folder != '' && typeof imgManager != 'undefined')
                                                imgManager.newFolder(dir, encodeURI(folder));
                        newfolder.style.display = "none";
                        document.getElementById('folder').value = '';
                }
                function openHelp() {
                  window.open('docs/<?php echo $help_lang; ?>/index.htm', 'Help', 'menubar=no,toolbar=no,scrollbars=yes,left=20,top=20,width=700,height=600');
                }
                function switchMode(mode) {
                    var uploadForm = document.getElementById('uploadForm');
                    document.getElementById('imgManager').src = mode+'.php';
                    uploadForm.action = mode+'.php';
                    
                }
                function showUpload(option) {
                    var uploaddlg = document.getElementById('uploaddlg');
                    switch (option) {
                        case 'show' :
                            if(uploaddlg.style.display == "none"){
                                uploaddlg.style.display = "block";
                            }else{
                                uploaddlg.style.display = "none";
                            }
                        break;
                        case 'hide' :
                                uploaddlg.style.display = "none";
                                document.getElementById('resize').checked = false;
                                document.getElementById('max_value').disabled = true;
                                document.getElementById('make_thumb').checked = false;
                        break;
                    }
                }
                function showNewFolder(option) {
                    var newfolder = document.getElementById('folderdlg');
                    switch (option) {
                      case 'show' :
                        if(newfolder.style.display == "none"){
                            newfolder.style.display = "block";
                        }else{
                            newfolder.style.display = "none";
                        }
                      break;
                      case 'hide' :
                            newfolder.style.display = "none";
                            document.getElementById('folder').value = '';
                      break;
                    }
                }
     //End Javascript Functions
     </script>
</head>
<body onload="init();">
     <form action="<?php echo $editor_im_mode;?>.php" name="uploadForm" id="uploadForm" method="POST" enctype="multipart/form-data">
          <fieldset>
               <legend><strong><?php echo _imgmanager_desc ?></strong></legend>
               <div class="dirs">
                    <label for="dirPath"><?php echo _imgmanager_dir ?></label>
                    <select name="dir" class="dirWidth" id="dirPath" onchange="updateDir(this)">
                         <option value="/">/</option>
                         <?php foreach($dirs as $relative=>$fullpath) { ?>
                            <option value="<?php echo rawurlencode($relative); ?>">
                            <?php echo $relative; ?></option>
                         <?php } ?>
                    </select>
                    <a href="#" onclick="javascript: goUpDir();" title="<?php echo _imgmanager_dirUp ?>"><img src="img/btnFolderUp.gif" height="15" width="15" alt="<?php echo _imgmanager_dirUp ?>" /></a> <!-- Added by Ryan Demmer - Dec 2004. Auth and option check -->
                    <?php if(($new_folder_auth || $isAdmin) && $editor_im_folder && $IMConfig['safe_mode'] == 'off'){?>
                        <a href="#" onclick="showNewFolder('show');" title="<?php echo _imgmanager_newFolder ?>"><img src="img/btnFolderNew.gif" height="15" width="15" alt="<?php echo _imgmanager_newFolder ?>" /></a>
                    <?php }?>
                    <?php if(($upload_auth || $isAdmin) && $editor_im_upload){?>
                        <a href="#" onclick="javascript: showUpload('show');"><img src="img/upload.gif" border="0" alt="<?php echo _imgmanager_upload ?>" title="<?php echo _imgmanager_upload ?>" /></a>
                    <?php }?>
                    <a href="#" onclick="javascript: switchMode('list');"><img src="img/detailview.gif" border="0" alt="<?php echo _imgmanager_list ?>" title="<?php echo _imgmanager_list ?>" /></a>
                    <a href="#" onclick="javascript: switchMode('images');"><img src="img/thumbview.gif" border="0" alt="<?php echo _imgmanager_image ?>" title="<?php echo _imgmanager_image ?>" /></a>
                    <a href="#" onclick="javascript: openHelp();"><img src="img/help.gif" border="0" alt="<?php echo _imgmanager_help ?>" title="<?php echo _imgmanager_help ?>" /></a>

                    <div id="messages" style="display: none;">
                         <span id="message"></span><img src="img/dots.gif" width="22" height="12" alt="..." />
                    </div>
                    <iframe src="<?php echo $editor_im_mode;?>.php" name="imgManager" id="imgManager" class="imageFrame" scrolling="auto" title="<?php echo _imgmanager_imageSel ?>" frameborder="0"></iframe>
               </div>
               <!--Upload Dialog-->
               <div id="uploaddlg" style="display: none;">
                    <div align="center">
                    <fieldset><legend><?php echo _imgmanager_upload;?></legend>
                         <table>
                         <tr>
                              <td colspan="4"><input type="file" name="upload" id="upload" size="50" value="" /></td>
                         </tr>
                         <tr>
                              <?php if($editor_im_thumbs == 'true'){?>
                                    <td><label><?php echo _imgmanager_resize_upload ?></label></td>
                                    <td><input type="checkbox" name="resizeImage" id="resize" onclick="if(this.checked==true){document.getElementById('max_value').disabled=false }else {if(this.checked==false){document.getElementById('max_value').disabled=true;}};" /></td>
                                    <td align="right"><label><?php echo _imgmanager_maxvalue ?>:</label></td>
                                    <td><input type="text" name="maxValue" id="max_value" value="<?php echo $editor_im_max_value ?>" size="5" disabled="disabled" /></td>
                         </tr>
                         <tr>
                                <td><label><?php echo _imgmanager_make_thumb ?></label></td>
                                <td><input type="checkbox" name="makeThumb" id="make_thumb" /></td>
                                <td colspan="3">&nbsp;</td>
                         </tr>
                              <?php }else{?>
                                    <tr>
                                        <td colspan="4">&nbsp;</td>
                                    </tr>
                              <?php }?>
                         </tr>
                         <tr>
                            <td><input type="submit" name="submit" style="cursor:pointer;" value="<?php echo _imgmanager_upload ?>" onclick="doUpload();" /></td>
                            <td colspan="2">&nbsp;</td>
                            <td align="right"><input type="button" style="cursor:pointer;" name="cancel" value="<?php echo _imgmanager_cancel ?>" onclick="showUpload('hide');" /></td>
                         </tr>
                         </table>
                         </fieldset>
                    </div>
               </div>
               <!--End Upload Dialog-->
               <!--New Folder Dialog-->
               <div id="folderdlg" style="display: none;">
                        <fieldset><legend><?php echo _imgmanager_newFolder;?></legend>
                         <table>
                         <tr>
                              <td colspan="2"><input type="text" name="folder" id="folder" size="40" /></td>
                         </tr>
                         <tr>
                              <td><input type="button" name="createFolder" value="<?php echo _imgmanager_ok ?>" onclick="newFolder();" /></td>
                              <td align="right"><input type="button" name="cancelFolder" value="<?php echo _imgmanager_cancel ?>" onclick="showNewFolder('hide');" /></td>
                         </tr>
                         </table>
                         </fieldset>
               </div>
               <!--End New Folder Dialog-->
               <div align="center" id="im_main" name="im_main">
                    <table summary="Image Properties"  style="width:630px;">
                         <tr>
                              <td><input type="radio" id="main" name="im_function" value="main" onclick="javascript:checkTableStyle('main');" /></td>
                              <td align="right" nowrap="nowrap"><label for="f_url"><?php echo _imgmanager_image ?></label></td>
                              <td colspan="6"><input type="text" id="f_url" size="50" value="" /></td>
                              <td align="right"><label for="f_alt"><?php echo _imgmanager_alt2 ?></label></td>
                              <td colspan="3"><input type="text" id="f_alt" size="40" value="" /></td>
                         </tr>
                         <tr>
                              <td>&nbsp;</td>
                              <td align="right"><label for="f_width"><?php echo _imgmanager_width ?></label></td>
                              <td width="20px"><input type="text" id="f_width" class="smallWidth" value="" onchange="javascript:checkConstrains('width');" /></td>
                              <td width="5px"><img src="img/islocked2.gif" id="imgLock" alt="<?php echo _imgmanager_constrain ?>" /></td>
                              <td align="right"><label for="f_height"><?php echo _imgmanager_height ?></label></td>
                              <td width="20px"><input type="text" id="f_height" class="smallWidth" value="" onchange="javascript:checkConstrains('height');" /></td>
                              <td align="right"><input type="checkbox" id="constrain_prop" checked="checked" onclick="javascript:toggleConstrains(this);" /></td>
                              <td><label for="constrain_prop" style="cursor:pointer;"><?php echo _imgmanager_constrain ?></label></td>
                              <input type="hidden" id="original_width" /> <input type="hidden" id="original_height" />
                              
                              <td align="right"><label for="f_border"><?php echo _imgmanager_border ?></label></td>
                              <td><input type="text" id="f_border" class="smallWidth" value="" /></td>
                              <td align="right"><label for="f_align"><?php echo _imgmanager_align ?></label></td>
                              <td><select size="1" id="f_align" title="<?php echo _imgmanager_align_desc ?>">
                                   <option value="">
                                        <?php echo _imgmanager_align_default ?>
                                   </option>

                                   <option value="baseline">
                                        <?php echo _imgmanager_align_baseline ?>
                                   </option>

                                   <option value="top">
                                        <?php echo _imgmanager_align_top ?>
                                   </option>

                                   <option value="middle">
                                        <?php echo _imgmanager_align_middle ?>
                                   </option>

                                   <option value="bottom">
                                        <?php echo _imgmanager_align_bottom ?>
                                   </option>

                                   <option value="texttop">
                                        <?php echo _imgmanager_align_texttop ?>
                                   </option>

                                   <option value="absmiddle">
                                        <?php echo _imgmanager_align_absmiddle ?>
                                   </option>

                                   <option value="absbottom">
                                        <?php echo _imgmanager_align_absbottom ?>
                                   </option>

                                   <option value="left">
                                        <?php echo _imgmanager_align_left ?>
                                   </option>

                                   <option value="right">
                                        <?php echo _imgmanager_align_right ?>
                                   </option>
                              </select></td>
                         </tr>
                         <tr>
                              <td colspan="8">&nbsp;</td>
                              <td align="right"><label for="f_vert"><?php echo _imgmanager_vspace ?></label></td>
                              <td><input type="text" id="f_vert" class="smallWidth" value="" /></td>
                              <td align="right"><label for="f_horiz"><?php echo _imgmanager_hspace ?></label></td>
                              <td><input type="text" id="f_horiz" class="smallWidth" value="" /></td>
                         </tr>
                    </table>
               </div>
               <div align="center" id="im_option">
                    <table summary="Image Options"  style="width:630px;" border="0">
                         <tr>
                              <td><input type="radio" id="option" name="im_function" value="option" onclick="javascript:checkTableStyle('option');"/></td>
                              <td class="option" colspan="4"><input type="checkbox" name="popupenable" id="popupenable" style="border: 1px none #000000; background-color: transparent; vertical-align: middle;" onclick="if(this.checked==true){ setPopupImage('enabled'); }else{ setPopupImage('enable'); }" /><label for="popenable"><b><?php echo _imgmanager_thumbPopup ?></b>:</label>
                              <select name="popup_mode" id="popup_mode">
                                  <option value="basic">Basic</option>
                                  <option value="advanced">Advanced</option>
                              </select>
                              <label><?php echo _imgmanager_offsite;?></label></td>
                              <td class="option"><input type="checkbox" id="offsite" name="offsite" /></td>
                              <td colspan="2" class="option"><input type="checkbox" name="onmousemove" id="onmousemove" style="border: 1px none #000000; background-color: transparent; vertical-align: middle;" onclick="if(this.checked==true){ setOnMouseInput('enabled'); }else{ setOnMouseInput('enable'); }" /><label for="onmousemove"><b><?php echo _imgmanager_onmousemove ?></b>:</label></td>
                         </tr>
                         <tr>
                              <td>&nbsp;</td>
                              <td nowrap="nowrap" id="showInput3" class="option" colspan="5"><?php echo _imgmanager_popupThumb ?>: <input name="popupthumb" type="text" id="popupthumb" value="" style="width: 160px" /></td>
                              <td colspan="2" id="showInput2" class="option"><input type="checkbox" name="onmousewidth" id="onmousewidth" style="border: 1px none #000000; background-color: transparent; vertical-align: middle;" /><label for="onmousemove"><?php echo _imgmanager_includeWidth ?></label> <input type="checkbox" name="onmouseheight" id="onmouseheight" style="border: 1px none #000000; background-color: transparent; vertical-align: middle;" /><label for="onmousemove"><?php echo _imgmanager_includeHeight ?></label></td>
                         </tr>
                         <tr>
                              <td>&nbsp;</td>
                              <td nowrap="nowrap" id="showInput4" class="option"><?php echo _imgmanager_width ?>:<input type="text" name="thumbWidth" id="thumbWidth" value="" size="5" onchange="javascript:checkConstrains_pop('width');"/></td>
                              <td class="option"><img src="img/islocked2.gif" id="imgLock_pop" valign="absmiddle" alt="<?php echo _imgmanager_constrain ?>" /></td>
                              <td nowrap="nowrap" id="showInput5" class="option"><?php echo _imgmanager_height ?>:<input type="text" name="thumbHeight" id="thumbHeight" value="" size="5" onchange="javascript:checkConstrains_pop('height');"/></td>
                              <td class="option" nowrap="nowrap"><label for="constrain_prop_pop" style="cursor:pointer;"><?php echo _imgmanager_constrain ?></label>
                              <input type="hidden" id="original_width_thumb" /><input type="hidden" id="original_height_thumb" /></td>
                              <td class="option"><input type="checkbox" id="constrain_prop_pop" checked="checked" onclick="javascript:toggleConstrains_pop(this);" /></td>

                              <td nowrap="nowrap" id="showInput1" class="option"><?php echo _imgmanager_mouseover ?>:</td>
                              <td class="option"><input name="onmouseover" type="text" id="onmouseover" value="" style="width: 180px" /></td>
                         </tr>
                         <tr>
                              <td>&nbsp;</td>
                              <td nowrap="nowrap" id="showInput7" class="option" colspan="5"><input name="thumbcaption" type="checkbox" id="thumbcaption" value="" style="border: 1px none #000000; background-color: transparent; vertical-align: middle;" /><label for="thumbcaption"><?php echo _imgmanager_thumbCaption ?></label>
                              <input name="thumbname" type="text" size="30" id="thumbname" value="" /></td>

                              <td nowrap="nowrap" id="showInput6" class="option"><?php echo _imgmanager_mouseout ?>:</td>
                              <td class="option"><input name="onmouseout" type="text" id="onmouseout" value="" style="width: 180px" /></td>
                         </tr>
                    </table>
               </div>
          </fieldset><!--// image properties -->
          <div style="text-align: right;">
               <hr />
               <input type="button" class="buttons" value="<?php echo _imgmanager_refresh ?>" onclick="return refresh();" /> <input type="reset" class="buttons" value="<?php echo _imgmanager_reset ?>" /> <input type="button" class="buttons" value="{$lang_insert}" name="insert" id="insert" onclick="return onOK();" /> <input type="button" class="buttons" value="<?php echo _imgmanager_cancel ?>" onclick="return onCancel();" />
          </div>
     </form><?php
          }else{
          die( 'You are not authorised to view this page.' );
          }
          ?>
</body>
</html>
