<?php	

echo "<script type=\"text/javascript\">
var wct_toolbar = document.getElementById(\"ed_toolbar\");";

if ($_GET['wcttab2'] == 'tview') {
	echo "
	var theButtontd = document.createElement('input');
	theButtontd.type = 'button';
	theButtontd.value = 'TABLE';
	theButtontd.onclick = cp_myentryB;
	theButtontd.className = 'ed_button3';
	theButtontd.title = 'TABLE';
	theButtontd.id = 'cp_td';
	wct_toolbar.appendChild(theButtontd);

	var theButtonlink = document.createElement('input');
	theButtonlink.type = 'button';
	theButtonlink.value = 'LINK';
	theButtonlink.onclick = cp_myentryC;
	theButtonlink.className = 'ed_button3';
	theButtonlink.title = 'LINK';
	theButtonlink.id = 'cp_link';
	wct_toolbar.appendChild(theButtonlink);

	var theButtontd = document.createElement('input');
	theButtontd.type = 'button';
	theButtontd.value = 'wctoverlay';
	theButtontd.onclick = cp_myentryD;
	theButtontd.className = 'ed_button3';
	theButtontd.title = 'wctoverlay';
	theButtontd.id = 'cp_wct_ol';
	wct_toolbar.appendChild(theButtontd);";
}

if ($_GET['wcttab2'] == 'eview') {
	echo "
	var theButtonlink = document.createElement('input');
	theButtonlink.type = 'button';
	theButtonlink.value = 'BACK';
	theButtonlink.onclick = cp_myentryC;
	theButtonlink.className = 'ed_button3';
	theButtonlink.title = 'BACK';
	theButtonlink.id = 'cp_link';
	wct_toolbar.appendChild(theButtonlink);";
}

echo "
	var theButtontd = document.createElement('input');
	theButtontd.type = 'button';
	theButtontd.value = 'if';
	theButtontd.onclick = cp_myentryD;
	theButtontd.className = 'ed_button3';
	theButtontd.title = 'if';
	theButtontd.id = 'cp_if';
	wct_toolbar.appendChild(theButtontd);

	var theButtontd = document.createElement('input');
	theButtontd.type = 'button';
	theButtontd.value = 'wctphp';
	theButtontd.onclick = cp_myentryD;
	theButtontd.className = 'ed_button3';
	theButtontd.title = 'wctphp';
	theButtontd.id = 'cp_wctphp';
	wct_toolbar.appendChild(theButtontd);

	var theButtontd = document.createElement('input');
	theButtontd.type = 'button';
	theButtontd.value = 'wctdate';
	theButtontd.onclick = cp_myentryE;
	theButtontd.className = 'ed_button3';
	theButtontd.title = 'wctdate';
	theButtontd.id = 'cp_wctdate';
	wct_toolbar.appendChild(theButtontd);
";

if ($_GET['wcttab'] =="eviw") {
	$myrow = $wpdb->get_row("SELECT `r_fields`,`r_table` FROM `".$wpdb->prefix."wct_form` WHERE `id`='".mres(str_replace("wct_form_","",$_GET['page']))."' LIMIT 1;");	

	$table = $wpdb->get_row("SHOW CREATE TABLE `".$wpdb->prefix."wct".$myrow->r_table."`;");
	$array=array(); foreach($table as $member=>$data) { $array[$member]=$data; }
	$felder = explode("PRIMARY KEY",str_replace(array(",","8 ,2","' ,'","\r","\n"),array(" ,","8.2","'.'","",""),$array['Create Table']));

	preg_match_all("/`.*?`/",$felder[0],$treffer);
	$treffer = $treffer[0]; // Shift array one instance lower
	array_shift($treffer); // remove table name
	array_shift($treffer); // remove first field id
	array_shift($treffer); // remove first field status

	$felder2 =  explode(",",$myrow->r_fields);

	foreach ($treffer as $f => $feld) {
		$feld = str_replace("`","",$feld);
		echo "var theButton".$feld." = document.createElement('input');
			theButton".$feld.".type = 'button';
			theButton".$feld.".value = '".$feld."';
			theButton".$feld.".onclick = cp_myentry;";
		if (in_array($feld,$felder2)) { echo "theButton".$feld.".className = 'ed_button4';"; } else { echo "theButton".$feld.".className = 'ed_button3';"; }
		echo "theButton".$feld.".title = '".$feld."';
			theButton".$feld.".id = 'cp_".$feld."';
			wct_toolbar.appendChild(theButton".$feld.");";
	}
}
else {
	if ($tableid != '0')	{

		echo "
		var theButton = document.createElement('input');
		theButton.type = 'button';
		theButton.value = 'id';
		theButton.onclick = cp_myentry;
		theButton.className = 'ed_button2';
		theButton.title = 'id';
		theButton.id = 'cp_id';
		wct_toolbar.appendChild(theButton);\n";

		$table = $wpdb->get_row("SHOW CREATE TABLE `".$wpdb->prefix."wct".$tableid."`;");
		$array=array(); foreach($table as $member=>$data) { $array[$member]=$data; }
		$tmp = explode("PRIMARY KEY",$array['Create Table']);
		$felder = explode(",",str_replace(array(",","8 ,2","' ,'"),array(" ,","8.2","'.'"),$tmp[0]));
	
		for ($i=3;$felder[$i] != '';$i++) {
			$feldoutput .= preg_replace("/`(.*)`\s.*/","var theButton$1= document.createElement('input');\ntheButton$1.type = 'button';\ntheButton$1.value = '$1';\ntheButton$1.onclick = cp_myentry;\ntheButton$1.className = 'ed_button2';\ntheButton$1.title = '$1';\ntheButton$1.id = 'cp_$1';\nwct_toolbar.appendChild(theButton$1);\n",$felder[$i-1]);
		}
	}
	else {
		$felder = array('author','date','title','content','comment_count');
		foreach ($felder as $feld) {
			$feldoutput .= "var theButton".$feld." = document.createElement('input');\ntheButton".$feld.".type = 'button';\ntheButton".$feld.".value = '".$feld."';\ntheButton".$feld.".onclick = cp_myentry;\ntheButton".$feld.".className = 'ed_button2';\ntheButton".$feld.".title = '".$feld."';\ntheButton".$feld.".id = 'cp_".$feld."';\nwct_toolbar.appendChild(theButton".$feld.");\n";
		}
	}
	echo $feldoutput;
}


echo "
function cp_myentry(querystr) {
	var myField = document.getElementById(\"content\");
	var myInhalt = this.value;

	if (document.selection) {
		myField.focus();
		var sel = document.selection.createRange();
		sel.text +=  \"{\" + myInhalt + \"}\";
		myField.focus();
	}
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart, endPos = myField.selectionEnd, cursorPos = endPos, scrollTop = myField.scrollTop;

		if (startPos != endPos) {
			myField.value = myField.value.substring(0, startPos)
		              + myField.value.substring(startPos, endPos)
		              + \"{\" + myInhalt + \"}\"
		              + myField.value.substring(endPos, myField.value.length);
			cursorPos += edButtons[i].tagStart.length + edButtons[i].tagEnd.length;
		}
		else {

			myField.value = myField.value.substring(0, startPos)
		              + \"{\" + myInhalt + \"}\"
			       + myField.value.substring(endPos, myField.value.length);
			cursorPos = startPos + edButtons[i].tagStart.length;
		}
		myField.focus();
		myField.selectionStart = cursorPos;
		myField.selectionEnd = cursorPos;
		myField.scrollTop = scrollTop;
	}
	else {
		myField.value += \"{\" + myInhalt + \"}\";
		myField.focus();
	}
}
function cp_myentryB(querystr) {
	var myField = document.getElementById(\"content\");
	var myInhalt = this.value;

	if (document.selection) {
		myField.focus();
		var sel = document.selection.createRange();
		sel.text +=  \"<td></td>\";
		myField.focus();
	}
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart, endPos = myField.selectionEnd, cursorPos = endPos, scrollTop = myField.scrollTop;

		if (startPos != endPos) {
			myField.value = myField.value.substring(0, startPos)
		              + myField.value.substring(startPos, endPos)
		              + \"<td></td>\"
		              + myField.value.substring(endPos, myField.value.length);
			cursorPos += edButtons[i].tagStart.length + edButtons[i].tagEnd.length;
		}
		else {

			myField.value = myField.value.substring(0, startPos)
		              + \"<td></td>\"
			       + myField.value.substring(endPos, myField.value.length);
			cursorPos = startPos + edButtons[i].tagStart.length;
		}
		myField.focus();
		myField.selectionStart = cursorPos;
		myField.selectionEnd = cursorPos;
		myField.scrollTop = scrollTop;
	}
	else {
		myField.value += \"<td></td>\";
		myField.focus();
	}
}
function cp_myentryC(querystr) {
	var myField = document.getElementById(\"content\");
	var myInhalt = this.value;

	if (document.selection) {
		myField.focus();
		var sel = document.selection.createRange();
		sel.text +=  \"[\" + myInhalt + \"]\";
		myField.focus();
	}
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart, endPos = myField.selectionEnd, cursorPos = endPos, scrollTop = myField.scrollTop;

		if (startPos != endPos) {
			myField.value = myField.value.substring(0, startPos)
		              + myField.value.substring(startPos, endPos)
		              + \"[\" + myInhalt + \"]\"
		              + myField.value.substring(endPos, myField.value.length);
			cursorPos += edButtons[i].tagStart.length + edButtons[i].tagEnd.length;
		}
		else {

			myField.value = myField.value.substring(0, startPos)
		              + \"[\" + myInhalt + \"]\"
			       + myField.value.substring(endPos, myField.value.length);
			cursorPos = startPos + edButtons[i].tagStart.length;
		}
		myField.focus();
		myField.selectionStart = cursorPos;
		myField.selectionEnd = cursorPos;
		myField.scrollTop = scrollTop;
	}
	else {
		myField.value += \"[\" + myInhalt + \"]\";
		myField.focus();
	}
}
function cp_myentryD(querystr) {
	var myField = document.getElementById(\"content\");
	var myInhalt = this.value;
	if (myInhalt == \"if\") { var myField2 = 'if field=\"\" check=\"==\" var=\"\" else=\"\"'; }
	else { var myField2 = myInhalt; }

	if (document.selection) {
		myField.focus();
		var sel = document.selection.createRange();
		sel.text +=  \"[\" + myInhalt + \"][/\" + myInhalt + \"]\";
		myField.focus();
	}
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart, endPos = myField.selectionEnd, cursorPos = endPos, scrollTop = myField.scrollTop;

		if (startPos != endPos) {
			myField.value = myField.value.substring(0, startPos) + \"[\" + myField2 + \"]\"
		              + myField.value.substring(startPos, endPos)
		              + \"[/\" + myInhalt + \"]\"
		              + myField.value.substring(endPos, myField.value.length);
			cursorPos += edButtons[i].tagStart.length + edButtons[i].tagEnd.length;
		}
		else {
			myField.value = myField.value.substring(0, startPos)
		              + \"[\" + myField2 + \"][/\" + myInhalt + \"]\"
			       + myField.value.substring(endPos, myField.value.length);
			cursorPos = startPos + edButtons[i].tagStart.length;
		}
		myField.focus();
		myField.selectionStart = cursorPos;
		myField.selectionEnd = cursorPos;
		myField.scrollTop = scrollTop;
	}
	else {
		myField.value += \"[\" + myField2 + \"][/\" + myInhalt + \"]\";
		myField.focus();
	}
}
function cp_myentryE(querystr) {
	var myField = document.getElementById(\"content\");
	var myInhalt = this.value;
	if (myInhalt == \"if\") { var myField2 = 'if field=\"\" check=\"==\" var=\"\" else=\"\"'; }
	else { var myField2 = myInhalt; }

	if (document.selection) {
		myField.focus();
		var sel = document.selection.createRange();
		sel.text +=  \"[wctphp]strftime(\\\"%A, %d.%m.%Y\\\",'')[/wctphp]\";
		myField.focus();
	}
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart, endPos = myField.selectionEnd, cursorPos = endPos, scrollTop = myField.scrollTop;

		if (startPos != endPos) {
			myField.value = myField.value.substring(0, startPos) + \"[wctphp]strftime(\\\"%A, %d.%m.%Y\\\",'\"
		              + myField.value.substring(startPos, endPos)
		              + \"')[/wctphp]\"
		              + myField.value.substring(endPos, myField.value.length);
			cursorPos += edButtons[i].tagStart.length + edButtons[i].tagEnd.length;
		}
		else {
			myField.value = myField.value.substring(0, startPos)
		              + \"[wctphp]strftime(\\\"%A, %d.%m.%Y\\\",'')[/wctphp]\"
			       + myField.value.substring(endPos, myField.value.length);
			cursorPos = startPos + edButtons[i].tagStart.length;
		}
		myField.focus();
		myField.selectionStart = cursorPos;
		myField.selectionEnd = cursorPos;
		myField.scrollTop = scrollTop;
	}
	else {
		myField.value += \"[wctphp]strftime(\\\"%A, %d.%m.%Y\\\",'')[/wctphp]\";
		myField.focus();
	}
}


</script>";
?>