function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}


function turnMenu() {
	if (document.getElementById("tdMainMenu").style.display == "") {
		tStyle	=	"none";
		tText	=	"<<";
		setCookie("turnMenu", "1", "Mon, 01-Jan-2020 00:00:00 GMT", "/");
	}
	else {
		tStyle	=	"";
		tText	=	">>";
		setCookie("turnMenu", "", "Mon, 01-Jan-2020 00:00:00 GMT", "/");
	}

	document.getElementById("tdMainMenu").style.display = tStyle;
	document.getElementById("tdMainWrapper").style.display = tStyle;
	document.getElementById("turnText").innerHTML = tText;
}

function getTurnMenu() {
	turnMenuSet = getCookie("turnMenu");
	if (turnMenuSet == "1") {
		tStyle	=	"none";
		tText	=	"<<";
	}
	else {
		tStyle	=	"";
		tText	=	">>";
	}

	document.getElementById("tdMainMenu").style.display = tStyle;
	document.getElementById("tdMainWrapper").style.display = tStyle;
	document.getElementById("turnText").innerHTML = tText;
}

function setBold() {
	mainContentWin.focus();
	mainContentWin.document.execCommand("bold", null, "");
}
function setItal() {
	mainContentWin.focus();
	mainContentWin.document.execCommand("italic", null, "");
}
function setUnder() {
	mainContentWin.focus();
	mainContentWin.document.execCommand("underline", null, "");
}
function setStrike() {
	mainContentWin.focus();
	mainContentWin.document.execCommand("strikethrough", null, "");
}

function setCenter() {
	mainContentWin.focus();
	mainContentWin.document.execCommand("justifycenter", null, "");
}
function setLeft() {
	mainContentWin.focus();
	mainContentWin.document.execCommand("justifyleft", null, "");
}
function setRight(frameWin) {
	mainContentWin.focus();
	mainContentWin.document.execCommand("justifyright", null, "");
}

function setUndo() {
	mainContentWin.focus();
	mainContentWin.document.execCommand("undo", null, "");
}
function setRedo() {
	mainContentWin.focus();
	mainContentWin.document.execCommand("redo", null, "");
}

function setLink() {
	var link = prompt("Укажите URL-адрес ссылки:", "http://");
	if (link != "http://" && link != "" && link) {
		mainContentWin.focus();
		mainContentWin.document.execCommand("createlink", null, link);
	}
}

function getTable() {
	if (document.getElementById("divTable").style.display == "block") {
		document.getElementById("divTable").style.display = "none";
	}
	else {
		document.getElementById("divTable").style.display = "block";
	}
}
function getImage() {
	if (document.getElementById("divImage").style.display == "block") {
		document.getElementById("divImage").style.display = "none";
	}
	else {
		document.getElementById("divImage").style.display = "block";
	}
}

function setFormat() {
	mainContentWin.focus();
	mainContentWin.document.execCommand("removeformat", null, "");
}

function setH1() {
	mainContentWin.focus();
	mainContentWin.document.execCommand("formatblock", null, "<h1>");
}
function setH2() {
	mainContentWin.focus();
	mainContentWin.document.execCommand("formatblock", null, "<h2>");
}


function setSup() {
	mainContentWin.focus();
	mainContentWin.document.execCommand("superscript", null, "");
}
function setSub() {
	mainContentWin.focus();
	mainContentWin.document.execCommand("subscript", null, "");
}
function setHorizontLine() {
	mainContentWin.focus();
	mainContentWin.document.execCommand("inserthorizontalrule", null, "");
}
function setList() {
	mainContentWin.focus();
	mainContentWin.document.execCommand("insertorderedlist", null, "");
}



function setTable() {
	var columns	= document.getElementById("tableColumns").value;
	var lines	= document.getElementById("tableLines").value;
	var border	= document.getElementById("tableBorder").value;
	var demo	= document.getElementById("tableDemo").checked;

	var tableText	=	"<table>";

	for (i = 1; i <= columns; i++) {
		tableText	+=	"<tr>";
		for (k = 1; k <= lines; k++) {
			tableText	+=	"<td";
			if (border == "1") {
				tableText	+=	" style='border: #000000 1px solid;'";
			}
			tableText	+=	">";

			if (demo == true) {
				tableText	+=	i+""+k;
			}

			tableText	+=	"</td>";
		}
		tableText	+=	"</tr>";
	}

	tableText	+=	"</table>";

	mainContentWin.focus();
	if (window.navigator.userAgent.indexOf("MSIE")!=-1) {
		var rng = mainContentWin.document.selection.createRange();
		rng.pasteHTML("" + tableText + "");
	}
	else {
 		mainContentWin.document.execCommand("inserthtml", false, tableText);
	}
	document.getElementById("divTable").style.display = "none";
}




function setImage(imgLink, align) {
	var talign = "";

	if (align == "1") {
		talign = ' style="float: left; margin-right: 20px; margin-bottom: 20px;"';
	}
	else if (align == "2") {
		talign = ' style="float: right; margin-left: 20px; margin-bottom: 20px;"';
	}
	else if (align == "3") {
		talign = ' style="display: block; margin: 0 auto;"';
	}

	imgText	=	"<img src='"+imgLink+"' border='0' "+talign+" />";

	mainContentWin.focus();
	if (window.navigator.userAgent.indexOf("MSIE")!=-1) {
		var rng = mainContentWin.document.selection.createRange();
		rng.pasteHTML("" + imgText + "");
	}
	else {
 		mainContentWin.document.execCommand("inserthtml", false, imgText);
	}
}



function resizeForm(element, type, size) {
	if (type == "1") {
		document.getElementById(element).style.height	=	(document.getElementById(element).offsetHeight+size)+"px";
	}
	else {
		document.getElementById(element).style.height	=	(document.getElementById(element).offsetHeight-size)+"px";
	}
}




function ArticlEgetSource(idType) {
	if (idType == 1) {
		document.getElementById("sourceTextLabel").style.display = "none";
	}
	else if (idType != 0) {
		document.getElementById("sourceTextLabel").style.display = "block";
	}
}

function ArticlEdropArea(name, height) {
	document.getElementById(name).style.height = height;
}


function ArticlEgetNumCharsAnons() {
	var anons	=	document.getElementById("anonsText").value;
	anons	=	anons.split(" ").join("");
	anons	=	anons.split("\r").join("");
	anons	=	anons.split("\n").join("");
    var alength	=	anons.length;

	document.getElementById("numCharsAnons").innerHTML	=	alength + " символов без учёта пробелов";
}


function OrdergetNumCharsAnons(length) {
	var anons	=	document.getElementById("anonsText").value;
	anons	=	anons.split(" ").join("");
	anons	=	anons.split("\r").join("");
	anons	=	anons.split("\n").join("");
    var alength	=	anons.length;
    var difference =  length-alength;

    if (difference > 0) {
		document.getElementById("numCharsAnons").innerHTML	=	"Набрано " + alength + " символов без учёта пробелов. Осталось набрать " + difference + " символов.";
    }
    else {
		document.getElementById("numCharsAnons").innerHTML	=	"Набрано " + alength + " символов без учёта пробелов";
    }
}


function PostingGetNumChars(length) {
	var content	=	document.getElementById("content").value;
	content	=	content.split(" ").join("");
	content	=	content.split("\r").join("");
	content	=	content.split("\n").join("");
    var alength	=	content.length;
    var difference =  length-alength;

    if (difference > 0) {
		document.getElementById("numChars").innerHTML	=	"Набрано " + alength + " символов без учёта пробелов. Осталось набрать " + difference + " символов.";
    }
    else {
		document.getElementById("numChars").innerHTML	=	"Набрано " + alength + " символов без учёта пробелов";
    }
}

function addCommentPhoto() {
	for (i = 1; i <= 5; i++) {
		if (document.getElementById("trPhoto"+i).style.display == "none") {
			document.getElementById("trPhoto"+i).style.display = "";
			break;
		}
	}
}

function deleteCommentPhoto(id) {
	document.getElementById("trPhoto"+id).style.display = "none";
	document.getElementById("photo"+id).value = "";
}
