<?php
if (!defined('IS_DEDI')) die();
include_once($cfg_dedi['dedi_path'].'inc/inc.init_external.php');
ob_start();
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Datum aus Vergangenheit
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // immer geändert
header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$sql = "SELECT A.name, B.idcat, B.parent, B.sortindex FROM $dedi_db[cat_lang] AS A, $dedi_db[cat] AS B WHERE A.idcat=B.idcat AND B.idclient='$client' ORDER BY B.parent, B.sortindex";
$db->query($sql);
while ($db->next_record()){
	$ordner[$db->f('idcat')] = $db->f('name');
	$tlo_tree[$db->f('parent')][$db->f('sortindex')] = $db->f('idcat');
}
tree_level_order_light();
$sql = "SELECT D.idcatside, D.idcat, D.sortindex, D.is_start, F.title FROM $dedi_db[cat_side] AS D, $dedi_db[side] AS E, $dedi_db[side_lang] AS F WHERE D.idside=E.idside AND E.idside=F.idside AND E.idclient='$client' AND F.idlang='$lang' ORDER BY D.idcatside";
$db->query($sql);
while ($db->next_record()){
	$seiten[$db->f('idcat')][$db->f('idcatside')]['name'] = $db->f('title');
	$seiten[$db->f('idcat')][$db->f('idcatside')]['idcatside'] = $db->f('idcatside');
	$seiten[$db->f('idcat')][$db->f('idcatside')]['is_start'] = $db->f('is_start');
	if($db->f('is_start') == 1){ $seiten['start'][$db->f('idcat')] = $db->f('idcatside'); }
	if(!is_array($seiten['sortarray'][$db->f('idcat')])){$seiten['sortarray'][$db->f('idcat')] = array(); }
	array_push($seiten['sortarray'][$db->f('idcat')],$db->f('sortindex'));
}
$baum = array();
foreach($catlist as $a){
	$temp['id'] = $a;
	$temp['name'] = $ordner[$a];
	$temp['level'] = $catlist_level[$a];
	if(isset($seiten['start'][$a])){$temp['typ'] = "o";} else {$temp['typ'] = "i";}
	array_push($baum, $temp);
	if(is_array($seiten[$a])){
		array_multisort($seiten['sortarray'][$a] , SORT_ASC , $seiten[$a] , SORT_ASC);
		foreach(array_keys($seiten[$a]) as $b){
			$temp['id'] = $seiten[$a][$b]['idcatside'];
			$temp['name'] = $seiten[$a][$b]['name'];
			$temp['level'] = $catlist_level[$a] + 1;
			$temp['typ'] = "s";
			array_push($baum, $temp);
		}
	}
}

$style['i'] = "color:#cccccc;";
$style['o'] = "color:#000000;";
$style['s'] = "color:#0000ff;";


?>
<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_insertlink'] ?></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_insertlink'] ?>";
var myValues = window.dialogArguments;

var URLtext = myValues.href_attribute;
var highlightedText = myValues.highlightedText;
var target = myValues.tar_attribute;
var linkText = myValues.linkText;
window.returnValue = -1;

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 != '') {
	// 1. set protocol
    		var compare = URLtext.substring(0,5);
    		for (i=0; i<document.set.type.length; i++) {
		  if (document.set.type.options[i].value.search(compare) != -1) {
				document.set.type.options[i].selected = true;
			}
		}

  	// 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 {
        	if(highlightedText) {
        		document.set.link_title.value = highlightedText;
        	} else {
        		document.set.link_title.value = "";
        	};
        }

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

	// 5. set internal
	Ausdruck = /http:\/\/dedilink\/(.+)$/;
  	Ausdruck.exec(document.set.link_value.value);
  	if(RegExp.$1 != ""){
  		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/' + Values[0];
  	if(document.set.link_title.value == ""){
	  	document.set.link_title.value=Values[1];
  	}
  }
}
</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_insertlink'] ?></legend>

<table border="0" cellspacing="4" align="center">
    <tr>
      <td align="right" nowrap="nowrap" width="40"><?php echo $dedi_lang['wys_pop_Internal']?> </td>
      <td nowrap="nowrap" colspan="3">

<select name="internlinks" size="1" style="width:205" onchange="change();">
<?php
	echo "<option value=\"0\">&nbsp;".$dedi_lang['form_nothing']."</option>\n";
	foreach($baum as $a)
	{
		if($a['typ'] == "s"){$link = "idcatside=".$a['id']; } else { $link = "idcat=".$a['id']; };
		echo "<option value=\"".$link.":".$a['name']."\" style=\"".$style[$a['typ']]."\">".(str_repeat('&nbsp;&nbsp;',$a['level'])).$a['name']."</option>\n";
	}
?>
</select>


<!--      <input type="button" value="<?php echo $dedi_lang['wys_pop_InternalLink'] ?>" onclick="javascript: internal();"> -->
      </td>
    </tr>
    <tr>
      <td align="right" nowrap="nowrap" width="40"><?php echo $dedi_lang['wys_pop_Protocol'] ?>: </td>
      <td nowrap="nowrap">
        <select name="type" onchange="javascript: myChange();">
		<option value="">other</option>
		<option value="file://">File</option>
		<option value="ftp://">FTP</option>
		<option value="gopher:">Gopher</option>
		<option value="http://" selected="selected">HTTP</option>
		<option value="https://">HTTPS</option>
		<option value="mailto:">mailTo</option>
		<option value="news:">News</option>
		<option value="telnet:">Telnet</option>
		<option value="wias:">WIAS</option>
	</select>
      </td>
	    <td align="right"><?php echo $dedi_lang['wys_pop_Target'] ?>: </td>
      <td><input type="text" name="target" size="10">
			</td>
    </tr>
    <tr>
      <td align="right" nowrap="nowrap"><?php echo $dedi_lang['wys_pop_URL'] ?>: </td>
      <td nowrap="nowrap" colspan="3">
        <input type="text" name="link_value" value="http://" size="35">&nbsp;&nbsp;&nbsp;&nbsp;
      </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" value="" name="empty">
		  <input type="button" value=" OK " onClick="returnSelected();">
	    <input type="button" value="<?php echo $dedi_lang['gen_abort']; ?>" onClick="window.close();">
		</td></tr></table>
		</form>
  </td>
</tr>
</table>
</center>
</body>
</html>