$(document).ready(function() {

$('#selaa-tiedostoja').click(function(){
$('#selaa_tiedostoja').load('./php/selaatiedostoja.php');
});

$('#chngtopreview').click(function(){

document.getElementById("editingsite").style.display="none";
document.getElementById("previewsite").style.display="block";

	document.getElementById("previewsitecode").innerHTML="Ladataan esikatselua...";

	var previewsiteing = document.getElementById("editoitavasivuzx").value;
	$.post("./php/ajax.php", { action: "preview", sisalto: previewsiteing, ip: "0" },
   	function(data){

			document.getElementById("previewsitecode").innerHTML=data;

   	});

});

$('#chngtopreviewno').click(function(){

document.getElementById("editingsite").style.display="block";
document.getElementById("previewsite").style.display="none";
document.getElementById("previewsitecode").innerHTML="";

});

				$('#poisto-sivu-popup').dialog({
					autoOpen: false,
					modal: true,
					buttons: {
						"Peruuta": function() { 
							$(this).dialog("close"); 
						}, 
						"Poista": function() { 
							var kategorzia = document.getElementById("tamansivunkategoria").value;
							var tietoox = document.getElementById("deletoitavasivu").value;

							$.post("./php/ajax.php", { action: "dothedelete", id: tietoox },
   							function(data){
							if (data != "ok"){
    							 alert(data);
							} else {
    							 window.open (kategorzia+".html","_top");
							}
   							}); 
						}
					}
				});

				$('#poisto-artikkeli-popup').dialog({
					autoOpen: false,
					modal: true,
					buttons: {
						"Peruuta": function() { 
							$(this).dialog("close"); 
						}, 
						"Poista": function() { 
							var kategorzia = document.getElementById("tamansivunkategoria").value;
							var sivuzia = document.getElementById("tamansivunnimi").value;
							var tietoox = document.getElementById("toidz").value;

							$.post("./php/ajax.php", { action: "dothedeletetwo", id: tietoox },
   							function(data){
							if (data != "ok"){
    							 alert(data);
							} else {
    							 window.open (kategorzia+"-"+sivuzia+".html","_top");
							}
   							}); 
						}
					}
				});

				$('#poisto-viesti-popup').dialog({
					autoOpen: false,
					modal: true,
					buttons: {
						"Peruuta": function() { 
							$(this).dialog("close"); 
						}, 
						"Poista": function() { 
							var kategorzia = document.getElementById("tamansivunkategoria").value;
							var sivuzia = document.getElementById("tamansivunnimi").value;
							var tietoox = document.getElementById("toidz").value;

							$.post("./php/ajax.php", { action: "dothedeletethree", id: tietoox },
   							function(data){
							if (data != "ok"){
    							 alert(data);
							} else {
    							 window.open (kategorzia+"-"+sivuzia+".html","_top");
							}
   							}); 
						}
					}
				});

				$('#poisto-kisasivu-popup').dialog({
					autoOpen: false,
					modal: true,
					buttons: {
						"Peruuta": function() { 
							$(this).dialog("close"); 
						}, 
						"Poista": function() { 
							var kategorzia = document.getElementById("tamansivunkategoria").value;
							var sivuzia = document.getElementById("tamansivunnimi").value;
							var tietoxox = document.getElementById("kzidz").value;

							$.post("./php/ajax.php", { action: "dothedeletefour", id: tietoxox },
   							function(data){
							if (data != "ok"){
    							 alert(data);
							} else {
    							 window.open (kategorzia+"-"+sivuzia+".html","_top");
							}
   							}); 
						}
					}
				});

		$('#box-kysymys-kuva').click(function(){
			$('#box-kysymys-kuva-popup').dialog('open');
			return false;
		});

				$('#box-kysymys-kuva-popup').dialog({
					autoOpen: false,
					modal: true,
					width: 430,
					height: 310,
					maxHeight: 1000,
					maxWidth: 1200,
					minHeight: 310,
					minWidth: 430,
					buttons: {
						"Ok": function() { 
							$(this).dialog("close"); 
						}
					}
				});

				$('#dialog').dialog({
					autoOpen: false,
					width: 800,
					height: 340,
					maxHeight: 400,
					maxWidth: 1000,
					minHeight: 340,
					minWidth: 800,
					buttons: {
						"Ei": function() { 
							$(this).dialog("close"); 
						}, 
						"Kyllä": function() { 
							$(this).dialog("close"); 
						}
					}
				});
				

				$('#dialog_link').click(function(){
					$('#dialog').dialog('open');
					return false;
				});

		$("#dialogi-message").dialog({
			modal: true,
			buttons: {
				Ok: function() {
					$(this).dialog('close');
				}
			}
		});

		$('#password').hover(
			function() { $(this).addClass('ui-state-hover'); }, 
			function() { $(this).removeClass('ui-state-hover'); }
		);

		$('#password').click(function(){
			$('#password-popup').dialog('open');
			return false;
		});

				$('#password-popup').dialog({
					autoOpen: false,
					modal: true,
					width: 330,
					height: 210,
					maxHeight: 1000,
					maxWidth: 1200,
					minHeight: 300,
					minWidth: 300,
					buttons: {
						"Peruuta": function() { 
							$(this).dialog("close"); 
						}, 
						"Tallenna": function() { 
							var tietoo1 = document.getElementById("salasana1").value;
							var tietoo2 = document.getElementById("salasana2").value;
							var tietoo3 = document.getElementById("salasana3").value;
							$.post("./php/ajax.php", { action: "salasana", tieto1: tietoo1, tieto2: tietoo2, tieto3: tietoo3 },
   							function(data){
							if (data != "ok"){
    							 alert("Salasanaa ei ole vaihdettu. Syy: " + data);
							} else {
							 $('#password-popup').dialog("close"); 
    							 alert("Salasana on vaihdettu! Kirjaudu sivustolta ulos ja takaisin sisään.");
							}
   							});
						}
					}
				});

		$('#wrench').hover(
			function() { $(this).addClass('ui-state-hover'); }, 
			function() { $(this).removeClass('ui-state-hover'); }
		);

		$('#wrench').click(function(){
			$('#wrench-popup').dialog('open');
			return false;
		});

				$('#wrench-popup').dialog({
					autoOpen: false,
					modal: true,
					width: 330,
					height: 210,
					maxHeight: 1000,
					maxWidth: 1200,
					minHeight: 300,
					minWidth: 300,
					buttons: {
						"Peruuta": function() { 
							$(this).dialog("close"); 
						}, 
						"Julkaise": function() { 
							$(this).dialog("close"); 
							$.post("./php/ajax.php", { action: "julkaise", ip: "0" },
   							function(data){
    							 alert("Sivusto on nyt julkaistu!");
   							});
						}
					}
				});

		$('#wrench-2').hover(
			function() { $(this).addClass('ui-state-hover'); }, 
			function() { $(this).removeClass('ui-state-hover'); }
		);

		$('#wrench-2').click(function(){
			$('#wrench-2-popup').dialog('open');
			return false;
		});

				$('#wrench-2-popup').dialog({
					autoOpen: false,
					modal: true,
					width: 330,
					height: 210,
					maxHeight: 1000,
					maxWidth: 1200,
					minHeight: 300,
					minWidth: 300,
					buttons: {
						"Peruuta": function() { 
							$(this).dialog("close"); 
						}, 
						"Sulje sivusto": function() { 
							$(this).dialog("close"); 
							$.post("./php/ajax.php", { action: "sulje", ip: "0" },
   							function(data){
    							 alert("Sivusto on nyt suljettu!");
   							});
						}
					}
				});

		$('#arrowout').hover(
			function() { $(this).addClass('ui-state-hover'); }, 
			function() { $(this).removeClass('ui-state-hover'); }
		);

		$('#arrowout').click(function(){
			$('#arrowout-popup').dialog('open');
			return false;
		});

				$('#arrowout-popup').dialog({
					autoOpen: false,
					modal: true,
					width: 330,
					height: 160,
					maxHeight: 1000,
					maxWidth: 1200,
					minHeight: 160,
					minWidth: 320,
					buttons: {
						"Peruuta": function() { 
							$(this).dialog("close"); 
						}, 
						"Kirjaudu ulos": function() { 
							window.open ("logout.html","_top");
						}
					}
				});

		$('#script').hover(
			function() { $(this).addClass('ui-state-hover'); }, 
			function() { $(this).removeClass('ui-state-hover'); }
		);

		$('#script').click(function(){
			$('#script-popup').dialog('open');
			return false;
		});

				$('#script-popup').dialog({
					autoOpen: false,
					modal: true,
					width: 700,
					height: 540,
					maxHeight: 1200,
					maxWidth: 1600,
					minHeight: 160,
					minWidth: 320,
					buttons: {
						"Peruuta": function() { 
							$(this).dialog("close"); 
						}, 
						"Tallenna": function() { 
							var csstiedostotahan = document.getElementById("nykyinen_css").value;
							$.post("./php/ajax.php", { action: "css", csstiedosto: csstiedostotahan, ip: "0" },
   							function(data){
							if (data == "ok"){
							window.open ("etusivu.html","_top");
							} else {
    							 alert("Tyylitiedoston tallennus epäonnistui.");
							}
   							});
						}
					}
				});

		$('#upload').hover(
			function() { $(this).addClass('ui-state-hover'); }, 
			function() { $(this).removeClass('ui-state-hover'); }
		);

		$('#upload').click(function(){
			$('#selaa_tiedostoja').load('./php/selaatiedostoja.php');
			$('#upload-popup').dialog('open');
			return false;
		});

				$('#upload-popup').dialog({
					autoOpen: false,
					modal: true,
					width: 610,
					height: 450,
					maxHeight: 1200,
					maxWidth: 1900,
					minHeight: 350,
					minWidth: 610,
					buttons: {
						"Sulje ikkuna": function() { 
							$(this).dialog("close"); 
						}
					}
				});

});

function showandhide(number){
if (number == "1"){
document.getElementById("alkuperainen_painike").style.backgroundColor="#ecf8e3";
document.getElementById("muokattava_painike").style.backgroundColor="#d2e7c2";
document.getElementById("alkuperainen_painike").style.borderColor="#2f6506";
document.getElementById("muokattava_painike").style.borderColor="#72b42d";
document.getElementById("alkuperainen_css_div").style.display="inline";
document.getElementById("alkuperainen_css").style.display="inline";
document.getElementById("nykyinen_css_div").style.display="none";
document.getElementById("nykyinen_css").style.display="none";
} else {
document.getElementById("alkuperainen_painike").style.backgroundColor="#d2e7c2";
document.getElementById("muokattava_painike").style.backgroundColor="#ecf8e3";
document.getElementById("alkuperainen_painike").style.borderColor="#72b42d";
document.getElementById("muokattava_painike").style.borderColor="#2f6506";
document.getElementById("alkuperainen_css_div").style.display="none";
document.getElementById("alkuperainen_css").style.display="none";
document.getElementById("nykyinen_css_div").style.display="inline";
document.getElementById("nykyinen_css").style.display="inline";
}
}

function changeview(){
var mikadisplay = document.getElementById("normalsite").style.display;
if (mikadisplay == "block"){
document.getElementById("normalsite").style.display="none";
document.getElementById("editingsite").style.display="block";
ConvertContent();
} else {
document.getElementById("normalsite").style.display="block";
document.getElementById("editingsite").style.display="none";
document.getElementById("previewsite").style.display="none";
}
}

function changeviewtwo(){
var mikadisplayx = document.getElementById("normalteksti").style.display;
if (mikadisplayx == "none"){
document.getElementById("normalteksti").style.display="block";
document.getElementById("editingteksti").style.display="none";
} else {
document.getElementById("normalteksti").style.display="none";
document.getElementById("editingteksti").style.display="block";
}
}

function changeviewthree(){
var mikadisplayxx = document.getElementById("newsite").style.display;
if (mikadisplayxx == "none"){
document.getElementById("newsite").style.display="block";
} else {
document.getElementById("newsite").style.display="none";
}
}

function changeviewfour(){
var mikadisplayxx = document.getElementById("newarticle").style.display;
if (mikadisplayxx == "none"){
document.getElementById("newarticle").style.display="block";
} else {
document.getElementById("newarticle").style.display="none";
}
}

function deletepage(thexid){

document.getElementById("deletoitavasivu").value=thexid;

$.ajax({
         type: "POST",
         url: "./php/ajax.php",
         data: "action=getdeletepagetext&id=" + thexid,
         success: function(msg){
		document.getElementById("psttexttaha").innerHTML=msg;
		$('#poisto-sivu-popup').dialog('open');
         }
});

}

function deletearticle(theid){

document.getElementById("toidz").value=theid;

$.ajax({
         type: "POST",
         url: "./php/ajax.php",
         data: "action=getdeletepagetexttwo&id=" + theid,
         success: function(msg){
		document.getElementById("psttexttahakaks").innerHTML=msg;
		$('#poisto-artikkeli-popup').dialog('open');
         }
});

}

function deleteviesti(theid){

document.getElementById("toidz").value=theid;

$.ajax({
         type: "POST",
         url: "./php/ajax.php",
         data: "action=getdeletepagetextthree&id=" + theid,
         success: function(msg){
		document.getElementById("psttexttahakolme").innerHTML=msg;
		$('#poisto-viesti-popup').dialog('open');
         }
});

}

function nullthebuttons(tehyid,firstid,secondid,thirdid,fourthid,fifthid){

document.getElementById(firstid+"-active").value="1";
document.getElementById(secondid+"-active").value="1";
document.getElementById(thirdid+"-active").value="1";
if (fourthid != ""){
document.getElementById(fourthid+"-active").value="1";
document.getElementById(fifthid+"-active").value="1";
}
document.getElementById("b-"+firstid).style.backgroundColor="#ececdf";
document.getElementById("b-"+secondid).style.backgroundColor="#ececdf";
document.getElementById("b-"+thirdid).style.backgroundColor="#ececdf";
if (fourthid != ""){
document.getElementById("b-"+fourthid).style.backgroundColor="#ececdf";
document.getElementById("b-"+fifthid).style.backgroundColor="#ececdf";
}

}

function showhide(tehid){

var checker = document.getElementById(tehid).style.display;

if (checker == "block"){
document.getElementById(tehid).style.display="none";
} else {
document.getElementById(tehid).style.display="block";
}

}

function showhidetwo(tehid,tehidtwo){

var checker = document.getElementById(tehid).style.display;

if (checker == "block"){
document.getElementById(tehid).style.display="none";
document.getElementById(tehidtwo).style.display="block";
} else {
document.getElementById(tehid).style.display="block";
document.getElementById(tehidtwo).style.display="none";
}

}

function showhidefolder(thsid){

var sisaltoid = thsid+" sisalto";
var kuvaid = thsid+" kuva";

if (document.getElementById(sisaltoid).style.display == "none"){
document.getElementById(sisaltoid).style.display="block";
document.getElementById(kuvaid).src="./image/minus.gif";
} else {
document.getElementById(sisaltoid).style.display="none";
document.getElementById(kuvaid).src="./image/plus.gif";
}

}

function hoverimage(thsid,action,where){

var sisaltoid = thsid+" sisalto";
var kuvaid = thsid+" kuva";
var kuvaid2 = thsid+" sisalto kuva";
var lisatty = thsid+" sisalto lisatty";
var tyyppi = thsid+" sisalto tyyppi";

if (where == "1"){

if (action == "1"){

if (document.getElementById(sisaltoid).style.display == "none"){
document.getElementById(kuvaid).src="./image/plus-2.gif";
} else {
document.getElementById(kuvaid).src="./image/minus-2.gif";
}

} else {

if (document.getElementById(sisaltoid).style.display == "none"){
document.getElementById(kuvaid).src="./image/plus.gif";
} else {
document.getElementById(kuvaid).src="./image/minus.gif";
}

}

} else {

if (action == "1"){
document.getElementById(lisatty).style.color="#666666";
document.getElementById(tyyppi).style.color="#666666";
document.getElementById(kuvaid2).src="./image/arrow-link-2.gif";
} else {
document.getElementById(lisatty).style.color="#000000";
document.getElementById(tyyppi).style.color="#000000";
document.getElementById(kuvaid2).src="./image/arrow-link.gif";
}

}

}

function showandhidefolders(number){
if (number == "2"){
document.getElementById("uusi-tiedosto").style.backgroundColor="#ecf8e3";
document.getElementById("selaa-tiedostoja").style.backgroundColor="#d2e7c2";
document.getElementById("uusi-tiedosto").style.borderColor="#2f6506";
document.getElementById("selaa-tiedostoja").style.borderColor="#72b42d";
document.getElementById("uusi_tiedosto").style.display="inline";
document.getElementById("selaa_tiedostoja").style.display="none";
} else {
document.getElementById("uusi-tiedosto").style.backgroundColor="#d2e7c2";
document.getElementById("selaa-tiedostoja").style.backgroundColor="#ecf8e3";
document.getElementById("uusi-tiedosto").style.borderColor="#72b42d";
document.getElementById("selaa-tiedostoja").style.borderColor="#2f6506";
document.getElementById("uusi_tiedosto").style.display="none";
document.getElementById("selaa_tiedostoja").style.display="inline";
}
return false;
}

function vaihdalista(){
if (document.getElementById("lista-vaihto").value == "1"){
$('#selaa_tiedostoja').load('./php/selaatiedostoja.php');
} else {
$('#selaa_tiedostoja').load('./php/selaatiedostoja2.php');
}
}

function deletetiedosto(theid,nimi){

input_box=confirm("Haluatko poistaa tiedoston '" + nimi + "' tietokannasta?");

if (input_box==true) { 
  $.ajax({
         type: "POST",
         url: "./php/ajax.php",
         data: "action=dileittiedosto&id=" + theid,
         success: function(msg){
	    $('#selaa_tiedostoja').load('./php/selaatiedostoja.php');
         }
  });
}

return false;

}

function deletekanzio(theid,nimi){

input_boxi=confirm("Haluatko poistaa kansion '" + nimi + "' ja sen sisältämät tiedostot tietokannasta?");

if (input_boxi==true) { 
  $.ajax({
         type: "POST",
         url: "./php/ajax.php",
         data: "action=dileitkansio&id=" + theid,
         success: function(msg){
	    $('#selaa_tiedostoja').load('./php/selaatiedostoja.php');
         }
  });
}

return false;

}

function falseta(){
return false;
}

function tarttetaankourl(){
if (document.getElementById("uussivutyypxpi").value == "5"){
document.getElementById("tanneseurl").style.display="block";
} else {
document.getElementById("tanneseurl").style.display="none";
}
}

function deletekisasivu(theid){

document.getElementById("kzidz").value=theid;

$.ajax({
         type: "POST",
         url: "./php/ajax.php",
         data: "action=getdeletepagetextfour&id=" + theid,
         success: function(msg){
		document.getElementById("psttexttahanelja").innerHTML=msg;
		$('#poisto-kisasivu-popup').dialog('open');
         }
});

}

function SubmitContent() {

var str = textEditor.document.body.innerHTML;
str = str.replace(/<span style=\"font-weight: bold;\">(.*?)<\/span>/gi, "[B]$1[/B]");
str = str.replace(/<span style=\"text-decoration: underline;\">(.*?)<\/span>/gi, "[U]$1[/U]");
str = str.replace(/<span style=\"font-style: italic;\">(.*?)<\/span>/gi, "[I]$1[/I]");
str = str.replace(/<img src=\"(.*?)\" alt=\"(.*?)\" style=\"margin-right: 5px; border: 0px solid black; float: left; clear: both;(.*?)\">/gi, "[IMG TASAUS=1 KOKO= OTSIKKO=$2]$1[/IMG]");
str = str.replace(/<img src=\"(.*?)\" alt=\"(.*?)\" style=\"border: 0px solid black; display: block; margin-left: auto; margin-right: auto;(.*?)\">/gi, "[IMG TASAUS=2 KOKO= OTSIKKO=$2]$1[/IMG]");
str = str.replace(/<img src=\"(.*?)\" alt=\"(.*?)\" style=\"margin-left: 5px; border: 0px solid black; float: right; clear: both;(.*?)\">/gi, "[IMG TASAUS=3 KOKO= OTSIKKO=$2]$1[/IMG]");
str = str.replace(/<a href=\"(.*?)\">(.*?)<\/a>/gi, "[URL=$1]$2[/URL]");
str = str.replace(/<br>/gi, "[BR]");
str = str.replace(/<table style=\"font-family: Arial; font-size: 12px; border-width: 0px;(.*?)\"><tbody>(.*?)<\/tbody><\/table>/gi, "[TABLE]$2[/TABLE]");
str = str.replace(/<table style=\"font-family: Arial; font-size: 12px; width: 100%; border-width: 1px;(.*?)\"><tbody>(.*?)<\/tbody><\/table>/gi, "[TABLE 100%]$2[/TABLE]");
str = str.replace(/<table class=\"tabe\" style=\"border-width: 1px;(.*?)\"><tbody>(.*?)<\/tbody><\/table>/gi, "[TABLE REUNAT]$2[/TABLE]");
str = str.replace(/<table class=\"tabe\" style=\"width: 100%; border-width: 1px;(.*?)\"><tbody>(.*?)<\/tbody><\/table>/gi, "[TABLE 100% REUNAT]$2[/TABLE]");
str = str.replace(/<tr>(.*?)<\/tr>/gi, "[TR]$1[/TR]");
str = str.replace(/<td>(.*?)<\/td>/gi, "[TD]$1[/TD]");
str = str.replace(/<td style=\"vertical-align: top;\">(.*?)<\/td>/gi, "[TD]$1[/TD]");
str = str.replace(/<td valign=\"top\">(.*?)<\/td>/gi, "[TD]$1[/TD]");
str = str.replace(/<div class=\"content-text\">(.*?)<\/div>/gi, "[TEKSTI]$1[/TEKSTI]");
str = str.replace(/<h1>(.*?)<\/h1>/gi, "[OTSIKKO]$1[/OTSIKKO]");
str = str.replace(/<hr class=(.*?)content-viiva(.*?)>/gi, "[HR]");

if (document.editor.whateditor.value == "uusarticle"){
document.editor.uusarticle.value = str;
} else if (document.editor.whateditor.value == "editoitavasivu"){
document.editor.editoitavasivuzx.value = str;
} else if (document.editor.whateditor.value == "uuskeskustelu"){
document.editor.uuskeskustelu.value = str;
}

document.editor.submit();

return true;
}

function ConvertContent() {
var str = document.editor.editoitavasivuzx.value;

str = str.replace(/\[TEKSTI\](.*?)\[\/TEKSTI\]/gi, "<div class=\"content-text\">$1</div>");
str = str.replace(/\[OTSIKKO\](.*?)\[\/OTSIKKO\]/gi, "<h1>$1</h1>");
str = str.replace(/\[HR\]/gi, "<hr class=\"content-viiva\" />");
str = str.replace(/\[B\](.*?)\[\/B\]/gi, "<span style=\"font-weight: bold;\">$1</span>");
str = str.replace(/\[U\](.*?)\[\/U\]/gi, "<span style=\"text-decoration: underline;\">$1<\/span>");
str = str.replace(/\[I\](.*?)\[\/I\]/gi, "<span style=\"font-style: italic;\">$1<\/span>");
str = str.replace(/\[IMG TASAUS=1 KOKO=(.*?) OTSIKKO=(.*?)\](.*?)\[\/IMG\]/gi, "<img src=\"$3\" alt=\"$2\" style=\"margin-right: 5px; border: 0px solid black; float: left; clear: both;\">");
str = str.replace(/\[IMG TASAUS=2 KOKO=(.*?) OTSIKKO=(.*?)\](.*?)\[\/IMG\]/gi, "<img src=\"$3\" alt=\"$2\" style=\"border: 0px solid black; display: block; margin-left: auto; margin-right: auto;\">");
str = str.replace(/\[IMG TASAUS=3 KOKO=(.*?) OTSIKKO=(.*?)\](.*?)\[\/IMG\]/gi, "<img src=\"$3\" alt=\"$2\" style=\"margin-left: 5px; border: 0px solid black; float: right; clear: both;\">");
str = str.replace(/\[URL=(.*?)\](.*?)\[\/URL\]/gi, "<a href=\"$1\">$2<\/a>");
str = str.replace(/\[BR\]/gi, "<br>");
str = str.replace(/\[TABLE\](.*?)\[\/TABLE\]/gi, "<table style=\"font-family: Arial; font-size: 12px; border-width: 0px;\"><tbody>$1</tbody></table>");
str = str.replace(/\[TABLE 100%\](.*?)\[\/TABLE\]/gi, "<table style=\"font-family: Arial; font-size: 12px; width: 100%; border-width: 1px;\"><tbody>$1</tbody></table>");
str = str.replace(/\[TABLE REUNAT\](.*?)\[\/TABLE\]/gi, "<table class=\"tabe\" style=\"border-width: 1px;\"><tbody>$1</tbody></table>");
str = str.replace(/\[TABLE 100% REUNAT\](.*?)\[\/TABLE\]/gi, "<table class=\"tabe\" style=\"width: 100%; border-width: 1px;\"><tbody>$1</tbody></table>");
str = str.replace(/\[TR\](.*?)\[\/TR\]/gi, "<tr>$1</tr>");
str = str.replace(/\[TD\](.*?)\[\/TD\]/gi, "<td valign=\"top\">$1</td>");

textEditor.document.body.innerHTML = str;
}

function putcode(codetype, towhere) {

if (codetype == "1"){
var urlx = document.getElementById("box-kuva-url").value;
var topicx = document.getElementById("box-kuva-otsikko").value;
var tasausx = document.getElementById("box-kuva-tasaus").value;

	if (tasausx == "1"){
	var tasausxx = "style\='margin-right: 5px\; border: 0px solid black\; float: left\; clear: both\;'";
	} else if (tasausx == "2"){
	var tasausxx = "style\='border: 0px solid black\; display: block\; margin-left: auto\; margin-right: auto\;'";
	} else if (tasausx == "3"){
	var tasausxx = "style\='margin-left: 5px\; border: 0px solid black\; float: right\; clear: both\;'";
	} else {
	var tasausxx;
	}

	var thycode="<img src='"+urlx+"' alt='"+topicx+"' "+tasausxx+" />";

} else if (codetype == "2"){

	var urlx = document.getElementById("box-linkki-url").value;
	var urlxtwo = document.getElementById("box-linkki-teksti").value;

	var thycode="<a href='"+urlx+"' />"+urlxtwo+"</a>";

} else if (codetype == "3"){

var riveja = document.getElementById("box-taulu-riveja").value;
var soluja = document.getElementById("box-taulu-soluja").value;

if (riveja == parseInt(riveja)){
if (soluja == parseInt(soluja)){
if (riveja < 100 && riveja > 0){
if (soluja < 18 && soluja > 0){

if (document.getElementById("box-taulu-koko").checked == true && document.getElementById("box-taulu-viiva").checked == true){
var end = "<table class='tabe' style='width: 100%; border-width: 1px;'>";
} else if (document.getElementById("box-taulu-koko").checked == true && document.getElementById("box-taulu-viiva").checked == false){
var end = "<table style='font-family: Arial; font-size: 12px; width: 100%; border-width: 1px;'>";
} else if (document.getElementById("box-taulu-koko").checked == false && document.getElementById("box-taulu-viiva").checked == true){
var end = "<table class='tabe' style='border-width: 1px;'>";
} else {
var end = "<table style='font-family: Arial; font-size: 12px; border-width: 0px;'>";
}

var rivit;

for (rivit=1;rivit<=riveja;rivit++){
var end = end+"<tr>";
	for (solut=1;solut<=soluja;solut++){
	var end = end+"<td valign='top'>Rivi "+rivit+", solu "+solut+"</td>";
	}
var end = end+"</tr>";
}

var thycode = end+"</table><br/>";
}
}
}
}

} else if (codetype == "4"){

	var thycode="<h1>Otsikko</h1>";

} else if (codetype == "5"){

	var thycode="<hr class=\"content-viiva\">";

} else if (codetype == "6"){

	var thycode="<div class=\"content-text\">Teksti</div>";

}

textEditor.document.body.innerHTML += thycode;
}

function def()
{
var testframe = document.createElement("iframe");
testframe.name = testframe.id = "textEditor";
testframe.width = "531";
testframe.height = "300";
testframe.style.borderWidth = "1px";
testframe.style.borderColor = "#3a4140";
testframe.style.borderStyle = "solid";
testframe.style.borderSpacing = "0px";
testframe.style.backgroundColor = "#f3f3e9";
testframe.style.marginTop = "2px";
testframe.style.padding = "0px";

if (testframe.addEventListener){
testframe.addEventListener("load",function(e){this.contentWindow.document.designMode = "on";}, false);
} else if (testframe.attachEvent){
testframe.attachEvent("load", function(e){this.contentWindow.document.designMode = "on";});
}

document.getElementById("textEditorDiv").appendChild(testframe);
textEditor.document.designMode="on";
textEditor.document.open();
textEditor.document.write('<head><style type="text/css">html { border-width: 0px; } body{ font-family: Arial; font-size: 12px; padding: 0px; margin: 0px; background-color: #f3f3e9; } .tabe {font-family: Arial; font-size: 12px; border-width: 1px; border-style: solid; border-spacing: 2px; border-collapse: collapse; border-color: #505050; padding: 1px; } .tabe td { border-width: 1px; border-style: solid; border-color: #505050; padding: 1px; } h1 { font-family: Times New Roman, Times, serif; font-size: 24px; font-weight: bold; color: #3a4140; margin-bottom: 5px; display: inline; } .content-text { font-size: 12px; color: #000000; line-height: 17px; } .content-viiva { display: block; border-width: 1px 0px 0px 0px; border-style: dotted; border-color: #000000; margin-top: 10px; margin-bottom: 9px; background-color: transparent; } h2 { font-family: Times New Roman, Times, serif; font-size: 20px; font-weight: bold; color: #3a4140; margin-bottom: 1px; display: inline; } </style> </head>');
textEditor.document.close();
}

function fontEdit(x,y)
{
textEditor.document.execCommand(x,"",y);
textEditor.focus();
}

