
/*
editorDir  - cesta k souborum editoru
cssFile    - cesta a jmeno css souboru, bud absolutni nebo relativni vzhledem k editorDir
imageDialogUrl - esta a jmeno aspcka pro vkladani obrazku, bud absolutni nebo relativni vzhledem k editorDir
*/

function openEditor(editorDir,cssFile,submitName,heditWidth,heditHeight,imageDialogUrl, imageDialogWidth, imageDialogHeight,enhanced) {
	window.submitName	= submitName
	window.heditWidth	= heditWidth
	window.heditHeight	= heditHeight
	window.imageDialogUrl	 = imageDialogUrl
	window.imageDialogWidth	 = imageDialogWidth
	window.imageDialogHeight = imageDialogHeight
	window.editorDir = editorDir
	window.cssFile = cssFile
	showModalDialog(editorDir+"hedit.asp?enhanced="+enhanced,window,"dialogheight="+heditHeight+";dialogwidth="+heditWidth+";status=no;toolbar=no;menubar=no;location=no;resizable=yes;scroll:no")
}

function initFrameset(submitName, cssFile) {
	ifr = document.frames(submitName +"_iframe")
	ifr.document.clear()
	ifr.document.open()
	ifr.document.write('<HTML><head><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1250"/><link href="'+cssFile+'" rel="stylesheet" type="text/css"></head><body marginheight="0" marginwidth="0" topmargin="0" leftmargin="0"></body></html>')
	ifr.document.close()
	ifr.document.body.innerHTML = document.all(submitName).value
	//close()
}

function initEditor(editorDir,cssFile,submitName, imageDialogUrl, imageDialogWidth, imageDialogHeight) {
	window.whe = document.all(submitName)
	wte.document.designMode="On"
	wte.document.open()
	wte.document.write('<HTML><head><title>aaa</title><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1250"/><link href="'+cssFile+'" rel="stylesheet" type="text/css"></head><body></body></html>')
	wte.document.body.contentEditable=true
	wte.document.body.innerHTML = whe.value
	wte.document.close()
	wte.focus()
	window.htmlMode=true; //false = editace zdrojaku
	window.htmlMode=true; //false = editace zdrojaku
	window.gridMode=false; //true = ukaz teckovane ramecky
	// TODO: zapnout grid volanim swapGrid....
	// TODO: pozor na vytvareni nove tabulky pri zapnutem gridu jestli u ni nastavuju taky grid
	window.imageDialogUrl = imageDialogUrl
	window.imageDialogWidth = imageDialogWidth
	window.imageDialogHeight = imageDialogHeight
	window.editorDir = editorDir
	window.cssFile = cssFile
	window.document.body.onload=LoadStylesSelect;
	
	window.dlgstyle = '<style type="text/css">img.he_tb { cursor: hand }HR { HEIGHT: 1px; COLOR: #999999 }P { FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif;	COLOR: #000000; TEXT-DECORATION: none; FONT-SIZE: 10pt; FONT-WEIGHT: bold; MARGIN-LEFT : 5px; MARGIN-RIGHT : 5px }BODY { FONT-SIZE: 10px; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif; BACKGROUND-COLOR: #D6D3CE }.he_inp { BORDER-RIGHT: #999999 1pt solid; BORDER-TOP: #999999 1pt solid; FONT-SIZE: 8pt; BORDER-LEFT: #999999 1pt solid; COLOR: #000000; BORDER-BOTTOM: #999999 1pt solid; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif; BACKGROUND-COLOR: #FFFFFF }.he_btn { BORDER-RIGHT: #999999 1px solid; BORDER-TOP: #999999 1px solid; FONT-SIZE: 11px; BORDER-LEFT: #999999 1px solid; COLOR: #000000; BORDER-BOTTOM: #999999 1px solid; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif; BACKGROUND-COLOR: #FFFFFF }.he_col { FONT-SIZE: 8pt; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif; COLOR: #FFFFFF; TEXT-DECORATION: none; BACKGROUND-COLOR: #999999 }.he_row { FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif }</style>'

}

function copyData() {
	if (!htmlMode) swapModes();
	whe.value = wte.document.body.innerHTML
}

function swapModes() {
	if (htmlMode) {
		copyData()
		document.all.wte.style.display = 'none';   //to je divne, ze misto jenom wte je treba uzit document.all.wte, pritom body od document.all.wte obsahuje telo celehe top level stranky
		whe.style.display = '';
		whe.focus();
		htmlMode = false;
	}
	else {
		wte.document.body.innerHTML = whe.value
		document.all.wte.style.display = '';
		whe.style.display = 'none';
		wte.focus()
		htmlMode = true;
	}
}

function DoOnClick(what, arg, bUserInterface) {
	var blInterface = true;
	if (!bUserInterface) { blInterface = false; }
	wte.focus();
	if(what == '') {
		alert('This function not available at the moment.');
		return;
	}
	var tr = wte.document.selection.createRange();
	var x = tr.queryCommandValue(what)
	if (arg=='removeFormat')  // XXXXXX removeFormating odstranuje pouze <b> apod., nikoliv <H1> atd, ve kterych se vyskytuje, to je nelogicke, takze to pude ven
		what=arg;
	if (arg == null){
		if(what=='Redo' || what=='Undo') 
			wte.document.execCommand(what, true);
		else
			tr.execCommand(what, true);
	}else
		tr.execCommand(what, true, arg);
	
	tr.select();
	wte.focus();
}


function ChangeColor(forWhat)
{
	wte.focus();
	var cColor, Color;
	var Colors = new Array('','WHITE','IVORY','LIGHTYELLOW','YELLOW','SNOW','FLORALWHITE','LEMONCHIFFON','CORNSILK','SEASHELL','LAVENDERBLUSH','PAPAYAWHIP','BLANCHEDALMOND','MISTYROSE','BISQUE','MOCCASIN','NAVAJOWHITE','PEACHPUFF','GOLD','PINK','LIGHTPINK','ORANGE','LIGHTSALMON','DARKORANGE','CORAL','HOTPINK','TOMATO','ORANGERED','DEEPPINK','FUCHSIA','MAGENTA','RED','OLDLACE','LIGHTGOLDENRODYELLOW','LINEN','ANTIQUEWHITE','SALMON','GHOSTWHITE','MINTCREAM','WHITESMOKE','BEIGE','WHEAT','SANDYBROWN','AZURE','HONEYDEW','ALICEBLUE','KHAKI','LIGHTCORAL','PALEGOLDENROD','VIOLET','DARKSALMON','LAVENDER','LIGHTCYAN','BURLYWOOD','PLUM','GAINSBORO','CRIMSON','PALEVIOLETRED','GOLDENROD','ORCHID','THISTLE','LIGHTGREY','TAN','CHOCOLATE','PERU','INDIANRED','MEDIUMVIOLETRED','SILVER','DARKKHAKI','ROSYBROWN','MEDIUMORCHID','DARKGOLDENROD','FIREBRICK','POWDERBLUE','LIGHTSTEELBLUE','PALETURQUOISE','GREENYELLOW','LIGHTBLUE','DARKGRAY','BROWN','SIENNA','YELLOWGREEN','DARKORCHID','PALEGREEN','DARKVIOLET','MEDIUMPURPLE','LIGHTGREEN','DARKSEAGREEN','SADDLEBROWN','DARKMAGENTA','DARKRED','BLUEVIOLET','LIGHTSKYBLUE','SKYBLUE','GRAY','OLIVE','PURPLE','MAROON','AQUAMARINE','CHARTREUSE','LAWNGREEN','MEDIUMSLATEBLUE','LIGHTSLATEGRAY','SLATEGRAY','OLIVEDRAB','SLATEBLUE','DIMGRAY','MEDIUMAQUAMARINE','CORNFLOWERBLUE','CADETBLUE','DARKOLIVEGREEN','INDIGO','MEDIUMTURQUOISE','DARKSLATEBLUE','STEELBLUE','ROYALBLUE','TURQUOISE','MEDIUMSEAGREEN','LIMEGREEN','DARKSLATEGRAY','SEAGREEN','FORESTGREEN','LIGHTSEAGREEN','DODGERBLUE','MIDNIGHTBLUE','AQUA','CYAN','SPRINGGREEN','LIME','MEDIUMSPRINGGREEN','DARKTURQUOISE','DEEPSKYBLUE','DARKCYAN','TEAL','GREEN','DARKGREEN','BLUE','MEDIUMBLUE','DARKBLUE','NAVY','BLACK')
	var HTML;
	HTML = "<head><Title>Barvy&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;v</Title>"+dlgstyle+"</head>"
	HTML = HTML + '<table id=tabcolor cellpadding=0 cellspacing=1 border=0 bgcolor=\"#999999\">';
	//	HTML = HTML + '<tr><td colspan=20 class=\"he_col\">&nbsp;Select color</td><td colspan=\"4\" align=\"right\" class=\"he_col\"><input type=button value=\"Close\" class=\"he_inp\" onclick=\"divFrm.style.visibility = \'hidden\';\"></td></tr>';
	for(cColor in Colors) {
		Color = Colors[cColor];
		if(((cColor) % 24) == 0 && cColor != 0)	{
			HTML = HTML + '</tr><tr>';
		};
		HTML = HTML + '<td width=\"10\" bgcolor=\"'+Color+'\"><img src=\"img/emp_dot.gif\" alt=\"'+Color+'\" onclick=\"dialogArguments.SetColor(\'' + forWhat + '\', \'' + Color + '\')\" width=\"10\" height=\"10\" border=\"0\" class=\"he_tb\"></td>';
	}
	HTML = HTML + '<td colspan=4><img src=\"img/emp_dot.gif\" width=1 height=1 alt=\"\" border=\"0\"></td></tr></table>';
	if (typeof colorDlg != "undefined") window.colorDlg.close();
	var dlg = window.showModelessDialog("empty.htm",window,"dialogheight=50px;dialogwidth=280px;status=no;toolbar=no;menubar=no;location=no");
	dlg.document.open();
	dlg.document.write(HTML);
	dlg.document.close();
	//dlg.dialogHeight = dlg.tabcolor.offsetHeight+'px';
	//dlg.dialogWidth = dlg.tabcolor.offsetWidth+'px';
	window.colorDlg = dlg;
}


function SetColor(forWhat, Color)
{
  switch(forWhat)
	{
    case 'F':
			DoOnClick('ForeColor',Color);
			break;
    case 'B':
		 	wte.document.body.bgColor=Color;
			wte.document.body.background='';
			break;
    case 'W' :		
    		window.WColor.bgColor = Color;
			window.colorDlg.close();
			break;
  }
}


/////////////////////////////////////////////////////////////////////
// Insert Table
/////////////////////////////////////////////////////////////////////
function FormDoInsertTbl()
{
	var HTML
	
	var nRowCount = tableDlg.document.all.row_count.value
	var nColCount = tableDlg.document.all.col_count.value
	//var nBorder = tableDlg.document.all.tbl_border.value
	
	//var nCellSp = tableDlg.document.all.tbl_csp.value 
	//var nCellPd = tableDlg.document.all.tbl_cpd.value
	
	var nRow, nCol;
	HTML = '<table  class="datatable">';

	for(nRow = 1; nRow <= nRowCount; nRow++)
	{
		HTML = HTML + '<tr' + (nRow==1 ? '' : '') + '>';
		for (nCol = 1; nCol <= nColCount; nCol++)
		{
			if(nRow == 1)
				HTML = HTML + '<th>Col'+nCol+'</th>';
			else
				HTML = HTML + '<td>&nbsp;</td>';
		};
		HTML = HTML + '</tr>';
	}
	HTML = HTML + '</table>';
	InsertHTML(HTML);
	window.tableDlg.close();
};

function FormInsertTbl()
{
	wte.focus();
	var HTML 
	HTML = "<head><Title>Vložit tabulku &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;v</Title>"+dlgstyle+"</head>"
	HTML = HTML + '<table cellpadding=2 cellspacing=0 border=0 width=\"100%\"><tr><td colspan=2 class=\"he_col\">Vložit tabulku</td></tr>';
	//HTML = HTML + '<tr><td align=\"left\" class=\"he_row\"> Šířka </td><td><input id=\"tbl_width\" class=\"he_inp\" value=0 size=5></td></tr>'
	//HTML = HTML + '<tr><td align=\"left\" class=\"he_row\"> Výška </td><td><input id=\"tbl_height\" class=\"he_inp\" value=0 size=5></td></tr>'
	HTML = HTML + '<tr><td align=\"left\" class=\"he_row\"> Řádky </td><td><input id=\"row_count\" class=\"he_inp\" value=3 size=5></td></tr>'
	HTML = HTML + '<tr><td align=\"left\" class=\"he_row\"> Sloupce </td><td><input id=\"col_count\" class=\"he_inp\" value=2 size=5></td></tr>'
	//HTML = HTML + '<tr><td align=\"left\" class=\"he_row\"> Ohraničení </td><td><input id=\"tbl_border\" class=\"he_inp\" value=1 size=5></td></tr>'
	///HTML = HTML + '<tr><td align=\"left\" class=\"he_row\"> Odsazení </td><td><input id=\"tbl_csp\" class=\"he_inp\" value=0 size=5></td></tr>'
	//HTML = HTML + '<tr><td align=\"left\" class=\"he_row\"> &nbsp;Okraje </td><td><input id=\"tbl_cpd\" class=\"he_inp\" value=0 size=5></td></tr>'
	HTML = HTML + '<tr><td colspan=2 align=\"center\" class=\"he_col\"><input type=Button value=\"Vložit\" class=\"he_btn\" onclick="dialogArguments.FormDoInsertTbl()">&nbsp;<input type=Button class=\"he_btn\" value=\"Zrušit\" onclick=\"window.close()\">';
	HTML = HTML + '</td></tr></table>'

	if (typeof tableDlg != "undefined") window.tableDlg.close();
	var dlg = window.showModelessDialog("empty.htm",window,"dialogheight=120px;dialogwidth=270px;status=no;toolbar=no;menubar=no;location=no");
	dlg.document.open();
	dlg.document.write(HTML);
	dlg.document.close();
	//dlg.dialogHeight = dlg.tabcolor.offsetHeight+'px';
	//dlg.dialogWidth = dlg.tabcolor.offsetWidth+'px';
	window.tableDlg = dlg;
}



/////////////////////////////////////////////////////////////////////
// Insert Hyperlink
/////////////////////////////////////////////////////////////////////
function FormDoInsertLnk()
{
	var HTML;
	var strURL = linkDlg.document.all.url_addr.value
	var strTitle = linkDlg.document.all.title_str.value
	var strHint = linkDlg.document.all.hint_str.value
	
	HTML = '<a href="' + strURL +'" title="' + strHint + '"';
	HTML = HTML + ' imgroot="' + strURL.substr(0, strURL.lastIndexOf('/')+1) +'">' + strTitle + '</a>';
	InsertHTML(HTML);
	window.linkDlg.close();
};

function FormInsertLnk()
{
	wte.focus();
	var HTML
	HTML = "<head><Title>Vložit odkaz &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Title>"+dlgstyle+"</head>"
	HTML = HTML + '<table cellpadding=2 cellspacing=0 border=0 width=\"100%\"><tr><td colspan=2 class=\"he_col\">Vložit odkaz</td></tr>';
	HTML = HTML + '<tr><td align=\"left\" class=\"he_row\"> URL </td><td><input id=\"url_addr\" class=\"he_inp\" value=\"http://\" size=60></td></tr>';
	HTML = HTML + '<tr><td align=\"left\" class=\"he_row\"> Titulek </td><td><input id=\"title_str\" class=\"he_inp\" value=\"' + '\" size=60></td></tr>';
	HTML = HTML + '<tr><td align=\"left\" class=\"he_row\"> Nápověda </td><td><input id=\"hint_str\" class=\"he_inp\" value=\"\" size=60></td></tr>';
	HTML = HTML + '<tr><td colspan=2 align=\"center\" class=\"he_col\"><input type=Button class=\"he_btn\" value=\"Cancel\" onclick=\"window.close()\">';
	HTML = HTML + '&nbsp;<input type=Button value=\"Insert\" class=\"he_btn\" onclick="dialogArguments.FormDoInsertLnk();"></td></tr></table>'
	if (typeof linkDlg != "undefined") window.linkDlg.close();
	var dlg = window.showModelessDialog("empty.htm",window,"dialogheight=150px;dialogwidth=450px;status=no;toolbar=no;menubar=no;location=no");
	dlg.document.open();
	dlg.document.write(HTML);
	dlg.document.close();
	dlg.document.all.title_str.value = wte.document.selection.createRange().htmlText
	window.linkDlg = dlg
}


/////////////////////////////////////////////////////////////////////
// Insert Image
/////////////////////////////////////////////////////////////////////
function FormDoInsertImage()
{
	var strImgHtml = imageDlg.imgifr.document.all.myDIV.innerHTML;
	InsertHTML(strImgHtml);
	window.imageDlg.close();
};

function InsertImage() ///testing...
{
	var strImgHtml = imageDlg.imgifr.imgHtml.value
	DoOnClick('InsertImage', strImgHtml, true)
	window.imageDlg.close();
};

function FormInsertImage()
{
	wte.focus();
	var HTML;
	HTML = '<head><Title>Správa obrázků</Title></head><body marginheight="0" marginwidth="0" topmargin="0" leftmargin="0"><div align="center">'
	HTML = HTML + '<iframe width=100% height=100% id=imgifr src="'+imageDialogUrl+'" border="0" marginwidth="0" marginheight="0" scrolling="yes"></iframe></body></html>';
	if (typeof imageDlg != "undefined") window.imageDlg.close();
	var dlg = window.showModelessDialog("empty.htm",window,"dialogheight="+imageDialogHeight+";dialogwidth="+imageDialogWidth+";status=no;toolbar=no;menubar=no;location=no;resizable=no;scroll=no");
	dlg.document.open();
	dlg.document.write(HTML);
	dlg.document.close();
	window.imageDlg = dlg
}



// write HTML code over selected text
function InsertHTML(HTML)
{
  wte.document.selection.createRange().pasteHTML(HTML);
}

/////////////////////////////////////////////////////////////////////
//New functions 21.10.2003
//paste by wEnsion, code by Mira Novakojc
/////////////////////////////////////////////////////////////////////
function setStyle(style) {
	var range = wte.document.selection.createRange();
	var elm = range.parentElement();
	if(style==""){
		if (elm.tagName.toUpperCase()=="SPAN") {
			var zal=elm.innerHTML;
			wte.document.selection.clear();
			range = wte.document.selection.createRange();
			range.pasteHTML(zal);
			//wte.document.selection.pasteHTML(zal);
			//alert(zal+'####'+range.htmlText);
		}
	}else{
		if (elm.tagName.toUpperCase()=="SPAN") {
			elm.className=style;
		}else{
			range.pasteHTML("<SPAN CLASS='"+style+"'>" + range.htmlText + "</span>");
		}
	}
}
/////////////////////////////////////////////////////////////////////
// Styles
/////////////////////////////////////////////////////////////////////
function LoadStylesSelect() {
	var arStyles=[];
	var styleSheets = wte.document.styleSheets;
	for (var i=0; i<styleSheets.length; i++) {
		for (var j=0; j<styleSheets(i).rules.length; j++) {
			var styleText = styleSheets(i).rules(j).selectorText
			if (styleText.indexOf(".HE") == 0)
				arStyles[arStyles.length] = styleText.substring(3,styleText.length)
		}
	}
	
	for (i=0; i<arStyles.length; i++) {
		var opt	= document.createElement("OPTION")
		opt.value = 'HE'+arStyles[i]
		opt.text = arStyles[i]
		styleSelect.add(opt)		
	}
}

function swapGrids() {
	window.gridMode = !window.gridMode;
	setGrids();
}

function setGrids() {
	var tables = wte.document.body.getElementsByTagName("TABLE");
	for (var i=0; i<tables.length; i++) {
		if (gridMode) {
			tables[i].runtimeStyle.border = "1px dotted #bbbbbb"
		} else {
			tables[i].runtimeStyle.cssText = ""
		}
		var rows = tables[i].rows;
		for (var j=0; j<rows.length; j++) {
			cells = rows[j].cells;
			for (var k=0; k<cells.length; k++) {
				if (gridMode) {
					cells[k].runtimeStyle.border = "1px dotted #bbbbbb"
				} else  {
					cells[k].runtimeStyle.cssText = ""
				}
			}
		}
	}
}

//-------New recursive version on 8.12.2003 Miroslav Novak
function TableGetInfoOld() {
	var elm = wte.document.selection.createRange().parentElement()
	if (elm.tagName.toUpperCase() != "TD" && elm.tagName.toUpperCase() != "TH") {
		//alert(elm.tagName.toUpperCase());
		//if (elm.tagName.toUpperCase()=="BODY") return false;
		//elm = elm.parentElement
		//if (!elm) return false;
		return false;
	}
	var tabinfo = [];
	tabinfo.td = elm;
	tabinfo.tr = elm.parentElement	
	tabinfo.tbody = elm.parentElement.parentElement		
	tabinfo.table = elm.parentElement.parentElement.parentElement
	return tabinfo;
}
function TableGetInfo() {
	if (wte.document.selection.type!="Control") {
		var elm = wte.document.selection.createRange().parentElement()
		while (elm.tagName.toUpperCase() != "TD" && elm.tagName.toUpperCase() != "TH") {
			if (elm.tagName.toUpperCase()=="BODY") return false;
			elm = elm.parentElement
			if (!elm) return false;
		}
		var tabinfo = [];
		tabinfo.td = elm;
		tabinfo.tr = elm.parentElement	
		tabinfo.tbody = elm.parentElement.parentElement		
		tabinfo.table = elm.parentElement.parentElement.parentElement
		return tabinfo;
	}else{
		return false;
	}
}


function TableInsertRowBelow() {
	var ti,tCell;
	if (!(ti = TableGetInfo())) return;
	var row = ti.table.insertRow(ti.tr.rowIndex+1);
	for (var i=0; i<ti.tr.cells.length; i++) {
		tCell=row.insertCell();
		tCell.innerText=' ';
	}
	setGrids();
}

function TableInsertRowAbove() {
	var ti,tCell;
	if (!(ti = TableGetInfo())) return;
	var n = ti.tr.cells.length;
	var row = ti.table.insertRow(ti.tr.rowIndex);
	for (var i=0; i<n; i++) {
		tCell=row.insertCell();
		tCell.innerText=' ';
	}
	setGrids();
}

function TableDeleteRow() {
	var ti;
	if (!(ti = TableGetInfo())) return;
	ti.table.deleteRow(ti.tr.rowIndex);
	setGrids();
}

function TableDeleteCol() {
	var ti;
	if (!(ti = TableGetInfo())) return;
	var cin = ti.td.cellIndex;
	for (var i=0; i<ti.table.rows.length; ++i) {
		ti.table.rows[i].deleteCell(cin);
	}
	setGrids();
}

function TableInsertColAbove() {
	var ti,tCell;
	if (!(ti = TableGetInfo())) return;
	var cin = ti.td.cellIndex;
	for (var i=0; i<ti.table.rows.length; ++i) {
		tCell=ti.table.rows[i].insertCell(cin);
		tCell.innerText=' ';
		
	}
	setGrids();
}

function TableInsertColBelow() {
	var ti,tCell;
	if (!(ti = TableGetInfo())) return;
	var cin = ti.td.cellIndex;
	for (var i=0; i<ti.table.rows.length; ++i) {
		tCell=ti.table.rows[i].insertCell(cin+1);
		tCell.innerText=' ';
	}
	setGrids();
}
function FormModifyTbl()
{
	var ti = TableGetInfo();
	if (!ti) return;
	wte.focus();
	var HTML 
	HTML = "<head><Title>Upravit tabulku &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;v</Title>"+dlgstyle+"</head>"
	HTML = HTML + '<table cellpadding=2 cellspacing=0 border=0 width=\"100%\"><tr><td colspan=2 class=\"he_col\">Upravit tabulku</td></tr>';
	//HTML = HTML + '<tr><td align=\"left\" class=\"he_row\">&nbsp; Šířka </td><td><input id=\"tbl_width\" class=\"he_inp\" value=0 size=5></td></tr>'
	//HTML = HTML + '<tr><td align=\"left\" class=\"he_row\">&nbsp; Výška </td><td><input id=\"tbl_height\" class=\"he_inp\" value=0 size=5></td></tr>'
	HTML = HTML + '<tr><td align=\"left\" class=\"he_row\">&nbsp; Ohraničení</td><td><input id=\"tbl_border\" class=\"he_inp\" value=1 size=5></td></tr>'
	//HTML = HTML + '<tr><td align=\"left\" class=\"he_row\">&nbsp; Odsazení </td><td><input id=\"tbl_csp\" class=\"he_inp\" value=0 size=5></td></tr>'
	//HTML = HTML + '<tr><td align=\"left\" class=\"he_row\">&nbsp; Okraje</td><td><input id=\"tbl_cpd\" class=\"he_inp\" value=0 size=5></td></tr>'
	//HTML = HTML + '<tr><td align=\"left\" class=\"he_row\"> Cell color </td><td><table border=1 cellpadding=0 cellspacing=0 style="border-color:#000000;"><tr><td id=tbl_color onclick="dialogArguments.ChangeColor(\'W\')">&nbsp;&nbsp;</td></tr></table></td></tr>'
	HTML = HTML + '<input type="hidden" id=tbl_color onclick="dialogArguments.ChangeColor(\'W\')">'
	HTML = HTML + '<tr><td colspan=2 align=\"center\" class=\"he_col\"><input type=Button value=\"Změnit\" class=\"he_btn\" onclick="dialogArguments.FormDoModifyTbl()">&nbsp;<input type=Button class=\"he_btn\" value=\"Zrušit\" onclick=\"window.close()\">';
	HTML = HTML + '</td></tr></table>'

	if (typeof tableDlg != "undefined") window.tableDlg.close();
	var dlg = window.showModelessDialog("empty.htm",window,"dialogheight=100px;dialogwidth=270px;status=no;toolbar=no;menubar=no;location=no");
	dlg.document.open();
	dlg.document.write(HTML);
	dlg.document.close();
	//dlg.dialogHeight = dlg.tabcolor.offsetHeight+'px';
	//dlg.dialogWidth = dlg.tabcolor.offsetWidth+'px';
	//dlg.document.all.tbl_width.value = ti.table.width;
	//dlg.document.all.tbl_height.value = ti.table.height;
	if(ti.table.border!="") dlg.document.all.tbl_border.value = ti.table.border;
	//dlg.document.all.tbl_csp.value = ti.table.cellSpacing;
	//dlg.document.all.tbl_cpd.value = ti.table.cellPadding;
	//dlg.document.all.tbl_color.bgColor = ti.table.bgColor;
	window.WColor = dlg.document.all.tbl_color;
	window.tableDlg = dlg;
}

function FormDoModifyTbl()
{
	var ti = TableGetInfo();
	if (!ti) return;
	var dlg = tableDlg;
	//ti.table.width = dlg.document.all.tbl_width.value;
	//ti.table.height = dlg.document.all.tbl_height.value;
	if(dlg.document.all.tbl_border.value!="") ti.table.border = dlg.document.all.tbl_border.value;
	//ti.table.cellSpacing = dlg.document.all.tbl_csp.value;
	//ti.table.cellPadding = dlg.document.all.tbl_cpd.value;
	//ti.table.bgColor = dlg.document.all.tbl_color.bgColor;
	setGrids();
	window.tableDlg.close();
};

function FormModifyCell()
{
	var ti = TableGetInfo();
	if (!ti) return;
	wte.focus();
	var HTML 
	HTML = "<head><Title>Upravit tabulku &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;v</Title>"+dlgstyle+"</head>"
	HTML = HTML + '<table cellpadding=2 cellspacing=0 border=0 width=\"100%\"><tr><td colspan=2 class=\"he_col\">Upravit tabulku</td></tr>';
	//HTML = HTML + '<tr><td align=\"left\" class=\"he_row\"> Šířka </td><td><input id=\"tbl_width\" class=\"he_inp\" value=0 size=5></td></tr>'
	//HTML = HTML + '<tr><td align=\"left\" class=\"he_row\"> Výška </td><td><input id=\"tbl_height\" class=\"he_inp\" value=0 size=5></td></tr>'

	HTML = HTML + '<tr><td align=\"left\" class=\"he_row\"> &nbsp;Vodorovné zarovnání </td><td> <select id=\"tbl_halign\" class=\"he_inp\"> <option value=left>Doleva<option value=center>Na střed<option value=right>Doprava</select></td></tr>'
	HTML = HTML + '<tr><td align=\"left\" class=\"he_row\"> &nbsp;Svislé zarovnání </td><td> <select id=\"tbl_valign\" class=\"he_inp\"> <option value=top>Nahoru<option value=middle>Na střed<option value=bottom>Dolů</select></td></tr>'

	//HTML = HTML + '<tr><td align=\"left\" class=\"he_row\"> &nbsp;Barva buněk </td><td><table border=1 cellpadding=0 cellspacing=0 style="border-color:#000000;"><tr><td id=tbl_color onclick="dialogArguments.ChangeColor(\'W\')">&nbsp;&nbsp;</td></tr></table></td></tr>'
	HTML = HTML + '<input type="hidden" id=tbl_color onclick="dialogArguments.ChangeColor(\'W\')">'
	HTML = HTML + '<tr><td colspan=2 align=\"center\" class=\"he_col\"><input type=Button value=\"Změnit\" class=\"he_btn\" onclick="dialogArguments.FormDoModifyCell()">&nbsp;<input type=Button class=\"he_btn\" value=\"Zrušit\" onclick=\"window.close()\">';
	HTML = HTML + '</td></tr></table>'

	if (typeof tableDlg != "undefined") window.tableDlg.close();
	var dlg = window.showModelessDialog("empty.htm",window,"dialogheight=120px;dialogwidth=270px;status=no;toolbar=no;menubar=no;location=no");
	dlg.document.open();
	dlg.document.write(HTML);
	dlg.document.close();
	//dlg.dialogHeight = dlg.tabcolor.offsetHeight+'px';
	//dlg.dialogWidth = dlg.tabcolor.offsetWidth+'px';
	//dlg.document.all.tbl_width.value = ti.td.width; 
	//dlg.document.all.tbl_height.value = ti.td.height;
	
	var list;
	list = dlg.document.all.tbl_halign;
	for (var i=0;i<list.options.length; ++i)  if (list.options[i].value == ti.td.align) list.options[i].selected=true;
	list = dlg.document.all.tbl_valign;
	for (var i=0;i<list.options.length; ++i)  if (list.options[i].value == ti.td.vAlign) list.options[i].selected=true;
	
	dlg.document.all.tbl_color.bgColor = ti.td.bgColor;
	window.WColor = dlg.document.all.tbl_color;
	window.tableDlg = dlg;
}

function FormDoModifyCell()
{
	var ti = TableGetInfo();
	if (!ti) return;
	var dlg = tableDlg;
	//ti.td.width = dlg.document.all.tbl_width.value;
	//ti.td.height = dlg.document.all.tbl_height.value;

	var list;
	list = dlg.document.all.tbl_halign;
	ti.td.align = list.options[list.selectedIndex].value;
	list = dlg.document.all.tbl_valign;
	ti.td.vAlign = list.options[list.selectedIndex].value;

	ti.td.bgColor = dlg.document.all.tbl_color.bgColor;
	setGrids();
	window.tableDlg.close();
}

function RemoveFormat(){
	var inHTML;
	inHTML=wte.document.body.innerHTML;

	re = new RegExp("<p[^>]*>","gi");inHTML=inHTML.replace(re, '#p>');
	re = new RegExp("</p[^>]*>","gi");inHTML=inHTML.replace(re, '#/p>');
	re = new RegExp("<br[^>]*>","gi");inHTML=inHTML.replace(re, '#br>');
	re = new RegExp("</br[^>]*>","gi");inHTML=inHTML.replace(re, '#/br>');
	re = new RegExp("<b[^>]*>","gi");inHTML=inHTML.replace(re, '#b>');
	re = new RegExp("</b[^>]*>","gi");inHTML=inHTML.replace(re, '#/b>');
	re = new RegExp("<li[^>]*>","gi");inHTML=inHTML.replace(re, '#li>');
	re = new RegExp("</li[^>]*>","gi");inHTML=inHTML.replace(re, '#/li>');
	re = new RegExp("<ul[^>]*>","gi");inHTML=inHTML.replace(re, '#ul>');
	re = new RegExp("</ul[^>]*>","gi");inHTML=inHTML.replace(re, '#/ul>');
	re = new RegExp("<hr[^>]*>","gi");inHTML=inHTML.replace(re, '#hr>');
	re = new RegExp("</hr[^>]*>","gi");inHTML=inHTML.replace(re, '#/hr>');
	re = new RegExp("<strong[^>]*>","gi");inHTML=inHTML.replace(re, '#strong>');
	re = new RegExp("</strong[^>]*>","gi");inHTML=inHTML.replace(re, '#/strong>');
	
	re = new RegExp("(&nbsp;&nbsp;)+","gi"); inHTML=inHTML.replace(re, '');
	re = new RegExp("(&nbsp; )+","gi"); inHTML=inHTML.replace(re, ' ');

	re = new RegExp('<a href[^=]*=[^\"]*"([^\">]*)".[^>]*\s*.[^>]*title[^=]*=[^\"]*"([^\">]*)"[^>]*>',"gi");inHTML=inHTML.replace(re, '#a!!! href="$1" title="$2">');
	re = new RegExp('<a href[^=]*=[^\"]*"([^\">]*)"[^>]*>',"gi");inHTML=inHTML.replace(re, '#a!!! href="$1" title="">');

	re = new RegExp('<a.[^>]*\s*.[^>]*href[^=]*=[^\"]*"([^\">]*)".[^>]*\s*.[^>]*title[^=]*=[^\"]*"([^\">]*)"[^>]*>',"gi");inHTML=inHTML.replace(re, '#a!!! href="$1" title="$2">');
	re = new RegExp('<a.[^>]*\s*.[^>]*href[^=]*=[^\"]*"([^\">]*)"[^>]*>',"gi");inHTML=inHTML.replace(re, '#a!!! href="$1" title="">');

	re = new RegExp("</a[^>]*>","gi");inHTML=inHTML.replace(re, '#/a>');

	
	re = new RegExp("<[^>]*>","gi");inHTML=inHTML.replace(re, '');

	re = new RegExp("#p>","gi");inHTML=inHTML.replace(re, '<p>');
	re = new RegExp("#/p>","gi");inHTML=inHTML.replace(re, '</p>');
	re = new RegExp("#b>","gi");inHTML=inHTML.replace(re, '<b>');
	re = new RegExp("#/b>","gi");inHTML=inHTML.replace(re, '</b>');
	re = new RegExp("#li>","gi");inHTML=inHTML.replace(re, '<li>');
	re = new RegExp("#/li>","gi");inHTML=inHTML.replace(re, '</li>');
	re = new RegExp("#ul>","gi");inHTML=inHTML.replace(re, '</ul>');
	re = new RegExp("#/ul>","gi");inHTML=inHTML.replace(re, '</ul>');
	re = new RegExp("#br>","gi");inHTML=inHTML.replace(re, '<br>');
	re = new RegExp("#/br>","gi");inHTML=inHTML.replace(re, '</br>');
	re = new RegExp("#hr>","gi");inHTML=inHTML.replace(re, '<hr>');
	re = new RegExp("#/hr>","gi");inHTML=inHTML.replace(re, '</hr>');
	re = new RegExp("#strong>","gi");inHTML=inHTML.replace(re, '<strong>');
	re = new RegExp("#/strong>","gi");inHTML=inHTML.replace(re, '</strong>');
	re = new RegExp("#a!!!","gi");inHTML=inHTML.replace(re, '<a');
	re = new RegExp("#/a>","gi");inHTML=inHTML.replace(re, '</a>');

	re = new RegExp("(<p>&nbsp;</p>)+","gi"); inHTML=inHTML.replace(re, '');

	wte.document.body.innerHTML=inHTML;

}

