<?php
if (!defined('IS_DEDI')) die();
include_once($cfg_dedi['dedi_path'].'inc/inc.init_external.php');
include $cfg_dedi['dedi_path'] .'inc/fnc.type_common.php';
?>
<html style="Width: 310px; Height: 200px;">
<head>
 <?php echo '<meta http-equiv="content-type" content="text/html; charset='.$lang_charset.'">'."\n"; ?>
<title><?php echo $dedi_lang['wys_insertfile'] ?></title>
<style type="text/css">
<!--
td {font-family: arial; font-size: 8pt;}
select {font-family: arial; font-size: 8pt;}
input {font-family: arial; font-size: 8pt;}
-->
</style>
<script language="javascript">
document.title = "<?php echo $dedi_lang['wys_insertfile'] ?>";
var myValues = window.dialogArguments;
var URLtext = myValues.href_attribute;
var highlightedText = myValues.highlightedText;
var target = myValues.tar_attribute;
var linkText = myValues.linkText;
var filelink = new Object();
<?php
$qstring = "dedi=$dedi&filetypes=$filetypes&folders=$folders&subfolders=$subfolders";
$allfiles = _type_get_files($filetypes , $folders, $subfolders);
if (is_array($allfiles)) {
	foreach($allfiles as $a) {
		$b = substr($a['dirname'],0,-1);
		$filetree[$b][$a['id']] = $a;
		echo "filelink.S".$a['id']." = '".$cfg_client['upl_htmlpath'].$a['dirname'].$a['filename']."';\n";
	}
}
?>


function returnSelected() {

  // if being edited, REMOVE old link.

	var target = document.set.target.value;
	var text = escape( "<a href='" );
	text = text + escape( document.set.link_value.value );

	if (target != '') {
	  	text = text + escape( "' target='" );
	  	text = text + escape( target );
  	}

	text = text + escape("'>");

  	if(document.set.link_title.value != '') {
		text = text + escape( document.set.link_title.value );
  	} else {
		text = text + escape( highlightedText );
  	}

  	text = text + escape('</a>');

	if(document.set.link_value.value == '') {
		text = -1;
	}

	window.returnValue = text;          // set return value
	window.close();                     // close dialog

}


// Now, if a person changes the protocol AFTER having entered the
// link, the inputted link info isn't erased.
function myChange() {

  var selectedItem      	= document.set.type.selectedIndex;
  var selectedItemValue 	= document.set.type.options[selectedItem].value;
  var selectedItemText  	= document.set.type.options[selectedItem].text;
  var inputtedText		= document.set.link_value.value;

  var protocol = document.set.link_value.value.split(':');
  if (protocol[1]) {
     var datum = protocol[1].replace(/^\/\//, '');
	}
	else {
	   var datum = protocol[0];
	}
	document.set.link_value.value = selectedItemValue + datum;

}

function updateForm() {

	if (URLtext != '') {

  	// 2. set link URL
		URLtext = URLtext.replace(/\/\/\//, "//");
		document.set.link_value.value = URLtext;
	}

	// 3. set link text
        if(linkText != '') {
            	document.set.link_title.value = linkText;
        } else {
        	document.set.link_title.value = highlightedText;
        }

	// 4. set target
	if (target != '') {
	  	document.set.target.value = target;
	} else {
		document.set.target.value = '_blank';
	}

	// 5. set internal
	Ausdruck = /http:\/\/dedilink\/fileid=(\d+)/;
  	Ausdruck.exec(document.set.link_value.value);
  	if(RegExp.$1 > 0){
  		var iCompare = RegExp.$1+":";
		for (i=0; i<document.set.internlinks.length; i++) {
	  	  if (document.set.internlinks.options[i].value.search(iCompare) == 0) {
	  	    document.set.internlinks.options[i].selected = true;
	  	  }
		}
	}
}
function change() {
  if (document.set.internlinks.value != ""){
  	Values = document.set.internlinks.value.split(":");
  	document.set.link_value.value='http://dedilink/fileid=' + Values[0];
  	if(document.set.link_title.value == ""){
	  	document.set.link_title.value=Values[1];
  	}
  }
}
function preview() {
	if(document.set.link_value.value != ''){
		Ausdruck = /http:\/\/dedilink\/fileid=(\d+)/;
		Ausdruck.exec(document.set.link_value.value);
		if(RegExp.$1 > 0){
			eval("prevurl = filelink.S"+RegExp.$1+";");
			F1 = window.open(prevurl,"Preview");
		}
	}
}
</script>

</head>
<body style="background: threedface; color: windowtext;" topmargin=5 leftmargin=0 onload="updateForm();">
<center>
<form method="post" name="set">

<table cellpadding="0" cellspacing="0" border="0" align="center">
<tr>
<td align="center">
<fieldset align="center" width="290">
<legend align="left"><?php echo $dedi_lang['wys_insertfile'] ?></legend>

<table border="0" cellspacing="4" align="center">
    <tr>
      <td align="right" nowrap="nowrap" width="40"><?php echo $dedi_lang['wys_pop_File'] ?>: </td>
      <td nowrap="nowrap" colspan="3">
	<select name="internlinks" size="1" style="width:205" onchange="change();">
<?php
if(is_array($filetree)){
	foreach (array_keys($filetree) as $folder){
		echo "<option value=''>$folder</option>\n";
		if(is_array($filetree[$folder])){
			foreach($filetree[$folder] as $file){
				$path = $file['id'].":".$file['filename'];
				echo "<option value='$path'>&nbsp; ".$file['filename']."</option>\n";
			}
		}
	}
}
?>
	</select>
     </td>
    </tr>
    <tr>
      <td align="right" nowrap="nowrap" width="40"><?php echo $dedi_lang['wys_pop_Target'] ?>: </td>
      <td><input type="text" name="target" size="35"></td>
    </tr>
    <tr>
      <td align="right" nowrap="nowrap"><?php echo $dedi_lang['wys_pop_Title'] ?>: </td>
      <td nowrap="nowrap" colspan="3">
        <input type="text" name="link_title" value="" size="35">&nbsp;&nbsp;&nbsp;&nbsp;
      </td>
    </tr>
</table>
</fieldset>
</td>
</tr>
<tr>
  <td colspan="2" align="center">
    <table cellpadding="3">
      <tr>
        <td align="center">
          <input type="hidden" name="link_value" value="">
  	  <input type="hidden" value="" name="empty">
	  <input type="button" value=" OK " onClick="returnSelected()">
	  <input type="button" value=" <?php echo $dedi_lang['gen_abort']; ?> " type="reset" onClick="window.close();">
	  <input type="button" value=" <?php echo $dedi_lang['wys_pop_Preview'] ?> " onClick="preview();">
	</td>
      </tr>
    </table>
  </td>
</tr>
</table>
</center>
</form>
</body>
</html>