<?php	

if (get_bloginfo('version','raw') >= '3.3') {
	echo "<script type=\"text/javascript\">
	function startnow () {
	var wct_toolbar = document.getElementById(\"qt_content_toolbar\");";
	$vorid = "qt_content_";
}
else {
	echo "<script type=\"text/javascript\">
	var wct_toolbar = document.getElementById(\"ed_toolbar\");";
	$vorid = "cp_";
}

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.style.backgroundImage = \"url(".plugins_url('custom-tables/img/fade-butt2.png').")\";
	theButtontd.id = '".$vorid."td';
	wct_toolbar.appendChild(theButtontd);

	var theButtontd = document.createElement('input');
	var theButtonlink = document.createElement('input');
	theButtonlink.type = 'button';
	theButtonlink.value = 'LINK';
	theButtonlink.onclick = cp_myentryC;
	theButtonlink.className = 'ed_button3';
	theButtonlink.style.backgroundImage = \"url(".plugins_url('custom-tables/img/fade-butt2.png').")\";
	theButtonlink.title = 'LINK';
	theButtonlink.id = '".$vorid."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.style.backgroundImage = \"url(".plugins_url('custom-tables/img/fade-butt2.png').")\";
	theButtontd.title = 'wctoverlay';
	theButtontd.id = '".$vorid."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.style.backgroundImage = \"url(".plugins_url('custom-tables/img/fade-butt2.png').")\";
	theButtonlink.title = 'BACK';
	theButtonlink.id = '".$vorid."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.style.backgroundImage = \"url(".plugins_url('custom-tables/img/fade-butt2.png').")\";
	theButtontd.title = 'if';
	theButtontd.id = '".$vorid."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.style.backgroundImage = \"url(".plugins_url('custom-tables/img/fade-butt2.png').")\";
	theButtontd.title = 'wctphp';
	theButtontd.id = '".$vorid."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.style.backgroundImage = \"url(".plugins_url('custom-tables/img/fade-butt2.png').")\";
	theButtontd.title = 'wctdate';
	theButtontd.id = '".$vorid."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';
			theButton".$feld.".style.backgroundImage = \"url(".plugins_url('custom-tables/img/fade-butt3.png').")\";";
		}
		else {
			echo "theButton".$feld.".className = 'ed_button3';
			theButton".$feld.".style.backgroundImage = \"url(".plugins_url('custom-tables/img/fade-butt2.png').")\";";
		}
		echo "theButton".$feld.".title = '".$feld."';
			theButton".$feld.".id = '".$vorid.$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.style.backgroundImage = \"url(".plugins_url('custom-tables/img/fade-butt.png').")\";
		theButton.title = 'id';
		theButton.id = '".$vorid."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.style.backgroundImage = \"url(".plugins_url('custom-tables/img/fade-butt.png').")\";\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$1.style.backgroundImage = \"url(".plugins_url('custom-tables/img/fade-butt.png').")\";\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();
	}
}";

if (get_bloginfo('version','raw') >= '3.3') {
	echo "}
	setTimeout('startnow()',300);";

}

echo "
</script>";
?>