<?php
if (!defined('IS_DEDI')) die();
include_once($cfg_dedi['dedi_path'].'inc/inc.init_external.php');?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD W3 HTML 3.2//EN">
<HTML id=dlgImage STYLE="width: 560px; height: 440px">
<HEAD>
 <?php echo '<meta http-equiv="content-type" content="text/html; charset='.$lang_charset.'">'."\n"; ?>
<meta http-equiv="MSThemeCompatible" content="Yes">
<TITLE>Insert Image</TITLE>
<style>
  html, body, button, div, input, select, fieldset, span  { font-family: MS Shell Dlg; font-size: 8pt; };
</style>
<!--
// htmlArea Released under the (Modified) BSD license
// Copyright (c) 2002, interactivetools.com, inc
// All rights reserved.

// Browse image dialog ASP code released under the (Modified) BSD license
// Copyright (c) 2002-2003, S Babu, vsbabu@vsbabu.org, http://vsbabu.org/
// All rights reserved.

// Adaptation to htmlArea and security addon released under the (Modified) BSD license
// Copyright (c) 2002-2003, P Engvall, pengvall@engvall.nu, // http://www.engvall.nu/
// All rights reserved.
-->
<SCRIPT LANGUAGE=JavaScript>
<!--
//alert('<?php echo $QUERY_STRING ?>');
function showPreview(width,height,thumb)
{
if (document.frmImagePick.ImgUrl.value != "") {

   document.PREVIEWPIC.style.visibility = "hidden";
   if(thumb != "") {
   	document.PREVIEWPIC.src=thumb;
   } else {
   	document.PREVIEWPIC.src=document.frmImagePick.ImgUrl.value;
   }
   if (width > 220 || height > 220){
	if (height/width <= 1){
		f = width / 220;
		bn = 220;
		hn = height / f ;
	} else {
		f = height / 220;
		hn = 220;
		bn = width / f;
	};
	width = bn;
	height = hn;
   };
   document.PREVIEWPIC.style.height = height;
   document.PREVIEWPIC.style.width = width;
   document.PREVIEWPIC.style.visibility = "visible";
}
else
 document.PREVIEWPIC.style.visibility = "hidden";
}

function UploadImages() {
	var newWindow;
	var props = 'scrollBars=yes,resizable=no,toolbar=no,menubar=no,location=no,directories=no,width=360,height=190,top=180,left=200';
	newWindow = window.open('../imgupload.php', 'Upload_Images_to_server', props);
}


// -->
</SCRIPT>
<SCRIPT defer>

function _CloseOnEsc() {
	if (event.keyCode == 27) { window.close(); return; }
}

function _getTextRange(elm) {
	var r = elm.parentTextEdit.createTextRange();
	r.moveToElementText(elm);
	return r;
}
window.onerror = HandleError

function HandleError(message, url, line) {
	var str = "An error has occurred in this dialog." + "\n\n"
	+ "Error: " + line + "\n" + message;
	alert(str);
	window.close();
	return true;
}

function Init() {
	var elmSelectedImage;
	var htmlSelectionControl = 'Control';
	var globalDoc = window.dialogArguments;
	var grngMaster = globalDoc.selection.createRange();

	// event handlers
	document.body.onkeypress = _CloseOnEsc;
	btnOK.onclick = new Function("btnOKClick()");
	txtFileName.fImageLoaded = false;
	txtFileName.intImageWidth = 0;
	txtFileName.intImageHeight = 0;
	if (globalDoc.selection.type == htmlSelectionControl) {
		if (grngMaster.length == 1) {
			elmSelectedImage = grngMaster.item(0);
			if (elmSelectedImage.tagName == 'IMG') {
				txtFileName.fImageLoaded = true;
				if (elmSelectedImage.src) {
					document.frmImagePick.ImgUrl.value = elmSelectedImage.src;
					showPreview(elmSelectedImage.width,elmSelectedImage.height,'');
					txtFileName.value = elmSelectedImage.src.replace(/^[^*]*(\*\*\*)/, "$1");  // fix placeholder src values that editor converted to abs paths
					txtFileName.intImageHeight = elmSelectedImage.height;
					txtFileName.intImageWidth = elmSelectedImage.width;
					txtVertical.value = elmSelectedImage.vspace;
					txtHorizontal.value = elmSelectedImage.hspace;
					txtBorder.value = elmSelectedImage.border;
					frmImagePick.txtAltText.value = elmSelectedImage.alt;
					selAlignment.value = elmSelectedImage.align;
				}
			}
		}
	}
	txtFileName.value = txtFileName.value || 'http://';

	// FOCUS REMOVED, Focus on ImgURL instead
	// txtFileName.focus();
	document.frmImagePick.ImgUrl.focus();
}

function _isValidNumber(txtBox) {
	var val = parseInt(txtBox);
	if (isNaN(val) || val < 0 || val > 999) return false;
	return true;
}

function btnOKClick() {
	var elmImage;
	var intAlignment;
	var htmlSelectionControl = 'Control';
	var globalDoc = window.dialogArguments;
	var grngMaster = globalDoc.selection.createRange();
	if (txtHorizontal.value && !_isValidNumber(txtHorizontal.value)) {
		alert("<?php echo $dedi_lang['wys_pop_NumberRange'].$dedi_lang['wys_pop_HSpace'] ?>");
		txtHorizontal.focus();
		return;
	}
	if (txtBorder.value && !_isValidNumber(txtBorder.value)) {
		alert("<?php echo $dedi_lang['wys_pop_NumberRange'].$dedi_lang['wys_pop_Border'].' '.$dedi_lang['wys_pop_width'] ?>");
		txtBorder.focus();
		return;
	}
	if (txtVertical.value && !_isValidNumber(txtVertical.value)) {
		alert("<?php echo $dedi_lang['wys_pop_NumberRange'].$dedi_lang['wys_pop_VSpace'] ?>");
		txtVertical.focus();
		return;
	}

	// delete selected content and replace with image
	if (globalDoc.selection.type == htmlSelectionControl && !txtFileName.fImageLoaded) {
		grngMaster.execCommand('Delete');
		grngMaster = globalDoc.selection.createRange();
	}
	idstr = "\" id=\"556e697175657e537472696e67";     // new image creation ID
	if (!txtFileName.fImageLoaded) {
		grngMaster.execCommand('InsertImage', false, idstr);
		elmImage = globalDoc.all['556e697175657e537472696e67'];
		elmImage.removeAttribute('id');
		elmImage.removeAttribute('src');
		grngMaster.moveStart("character", -1);
	} else {
		elmImage = grngMaster.item(0);
		if (elmImage.src != txtFileName.value) {
			grngMaster.execCommand('Delete');
			grngMaster = globalDoc.selection.createRange();
			grngMaster.execCommand("InsertImage", false, idstr);
			elmImage = globalDoc.all['556e697175657e537472696e67'];
			elmImage.removeAttribute('id');
			elmImage.removeAttribute('src');
			grngMaster.moveStart('character', -1);
			txtFileName.fImageLoaded = false;
		}
		grngMaster = _getTextRange(elmImage);
	}
	if (txtFileName.fImageLoaded) {
		elmImage.style.width = txtFileName.intImageWidth;
		elmImage.style.height = txtFileName.intImageHeight;
	}
	if (txtFileName.value.length > 2040) txtFileName.value = txtFileName.value.substring(0,2040);
	elmImage.src = document.frmImagePick.ImgUrl.value
	if (txtHorizontal.value != '') elmImage.hspace = parseInt(txtHorizontal.value);
	else elmImage.hspace = 0;
	if (txtVertical.value != '') elmImage.vspace = parseInt(txtVertical.value);
	else elmImage.vspace = 0;
	elmImage.alt = frmImagePick.txtAltText.value;
	if (txtBorder.value != '') elmImage.border = parseInt(txtBorder.value);
	else elmImage.border = 0;
	elmImage.align = selAlignment.value;
	grngMaster.collapse(false);
	grngMaster.select();
	window.close();
}

function showPreview()
{
if (document.frmImagePick.ImgUrl.value != "") {
   document.PREVIEWPIC.style.height = "";
   document.PREVIEWPIC.style.height = "";
   document.PREVIEWPIC.src=document.frmImagePick.ImgUrl.value;

   ho = document.PREVIEWPIC.height;
   bo = document.PREVIEWPIC.width;
   if (bo > 220 || ho > 220){
	if (ho/bo <= 1){
		f = bo / 220;
		bn = 220;
		hn = ho / f ;
	} else {
		f = ho / 220;
		hn = 220;
		bn = bo / f;
	};
	document.PREVIEWPIC.style.height = hn;
	document.PREVIEWPIC.style.width = bn;
   };
   document.PREVIEWPIC.style.visibility = "visible";
}
else document.PREVIEWPIC.style.visibility = "hidden";
}

function fullPreview() {
	window.open(document.frmImagePick.ImgUrl.value, 'Preview');
}
</script>
</head>
<body id="bdy" onload="Init()" style="background: threedface; color: windowtext;" scrolling="no">
<table widht="550" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="550" align="center"><table width="520" cellpadding="0" cellspacing="0" style="table-layout:fixed">
      <tr>
        <td width="50%"><span><?php echo $dedi_lang['wys_pop_ImgBrowser'] ?>:</span></td>
        <td width="50%"><span><?php echo $dedi_lang['wys_pop_Image'].'-'.$dedi_lang['wys_pop_Preview'] ?>:</span></td>
      </tr>
      <tr height="245">
        <td width="50%" valign="middle"><iframe name="IMGPICK" src="<?PHP echo $sess->url($cfg_client['htmlpath'].$cfg_client['contentfile'] .'?popup=browseimage&view=edit&dummy=true'); ?>&filetypes=<?PHP echo $filetypes; ?>&folders=<?PHP echo $folders; ?>&subfolders=<?PHP echo $subfolders; ?>" style="border: solid black 1px; width: 240px; height:240px; z-index:1"></iframe></td>
        <td width="50%" align="center" valign="middle" style="border:1pt solid black;"><a href="#"><img id="PREVIEWPIC" name="PREVIEWPIC" src="" alt="Preview" onclick="fullPreview()" border="0" style="visibility:hidden"></a></td>
      </tr>
      <tr>
        <form name="frmImagePick" method="post" action="">
        <td width="50%" align="left"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td height="20" valign="bottom"><span><?php echo $dedi_lang['wys_pop_Select_Image'] ?></span></td>
          </tr>
          <tr>
            <td><input type="TEXT" size="30" name="ImgUrl" style="height: 2.2em; width : 243px;" value="" onChange="showPreview();showPreview();"></td>
          </tr>
          <tr>
            <td height="20" valign="bottom"><span><?php echo $dedi_lang['wys_pop_Tooltip'] ?>:</span></td>
          </tr>
          <tr>
            <td><input type="text" id="txtAltText" tabIndex="15" style="width: 22.65em; height: 2.1294em; " onfocus="select()"></td>
          </tr>
        </table></td>
        </form>
        <td width="50%"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="160"><fieldset id=fldLayout style="width: 14.15em; height: 7.6em; padding: 5px;"><legend id=lgdLayout>Layout</legend><span style="width:45pt;"><?php echo $dedi_lang['wys_pop_Align'] ?>:</span>
	   <select size="1" id="selAlignment" tabIndex="20" style="width: 6.72em; height: 1.2168em;">
              <option id="optNotSet" value=""> --- </option>
              <option id="optLeft" value="left" selected> Links </option>
              <option id="optRight" value="right"> Rechts </option>
              <option id="optTexttop" value="textTop"> Texttop </option>
              <option id="optAbsMiddle" value="absMiddle"> Absmiddle </option>
              <option id="optBaseline" value="baseline"> Baseline </option>
              <option id="optAbsBottom" value="absBottom"> Absbottom </option>
              <option id="optTop" value="top"> Oben </option>
              <option id="optMiddle" value="middle"> Mitte </option>
              <option id="optBottom" value="bottom"> Unten </option>
            </select><br><span style="width:70pt;"><?php echo $dedi_lang['wys_pop_Border'].' '.$dedi_lang['wys_pop_width'] ?>:</span>
            <input id="txtBorder" style="width: 3.6em; height: 2.1294em; ime-mode: disabled;" type=text size=3 maxlength=3 value="" tabIndex=21 onfocus="select()"></fieldset></td>
            <td><fieldset id="fldSpacing" style="width: 9.5em; height: 7.6em; padding: 5px"><legend id="lgdSpacing"><?php echo $dedi_lang['wys_pop_Spacing'] ?></legend>
            <span style="width:40pt">Horizontal:</span><input id="txtHorizontal" style="width: 2.7em; height: 2.1294em; ime-mode: disabled;" type=text size=3 maxlength=3 value="5" tabIndex=25 onfocus="select()"><br>
            <span style="width:40pt">Vertikal:</span><input ID=txtVertical style="width: 2.7em; height: 2.1294em; ime-mode: disabled;" type=text size=3 maxlength=3 value="5" tabIndex=30 onfocus="select()"></fieldset></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td align="right"><button id="btnOK" style="width: 7em; height: 2.2em;" type="submit" tabIndex="40">OK</button><span style="width:80pt"></span><button id="btnCancel" style="width: 7em; height: 2.2em;" type=reset tabIndex=45 onClick="window.close();"><?php echo $dedi_lang['gen_abort']; ?></button>
        <input id="txtFileName" type="text" style="width: 21.5em;height: 2.1294em; visibility:hidden" tabIndex=10></td>
      </tr>
    </table></form></td>
  </tr>
</table>
</body>
</html>