//var mode = 'editor'
//var myform = document.msg_form
var ie = document.all;
var ie5 = document.all && !document.fireEvent && !window.opera;
var nouze = 0
var max_len = 600;

if (ie5){ var objEdit = frames.iedit }
else	{ var objEdit = document.getElementById('iedit').contentWindow; }

function init()	
{
	//if (!objEdit || ((SZN.browser.klient != 'ie') && (SZN.browser.klient != 'geck'))) { nouzovka(); return; }
	//if (!objEdit || window.opera) { nouzovka(); return; }

	//document.getElementById("editor").style.display = 'block'
	document.getElementById("textarea").style.display = 'none'
	document.getElementById("iedit").style.display = 'block'

	objEdit.document.designMode="On";
	
	value = document.getElementById('textarea').value
	
	if (!value)
		value="<span></span>";
		//value="";
	//txt  = "<html><head><style>p{ margin-bottom:0.5em !important; border:1px dotted #D0F3E0;}</style></head>"
	txt  = "<html><head><style>";
	txt += "body { font-size: 12px; font-family: Arial, Tahoma, Verdana, Helvetica, sans-serif; color : #000; }";
	//txt += "ul { list-style-image: url('images/default/e.gif') }";
	//txt += "p{ margin: 0px; padding:0px; border:1px dotted #D0F3E0;}";
	txt += "</style></head>";
	txt += "<body style='margin:5px;'>"+value+"</body></html>";

	objEdit.document.open()
	objEdit.document.write(txt)
	objEdit.document.close()

	if (!ie) {  objEdit.document.execCommand('useCSS', false, true); }

	if(!ie) {
		//objEdit.document.addEventListener("keydown", cntLen, true);
		//objEdit.document.addEventListener("keypress", cntLen, true);
		//objEdit.document.addEventListener("keyup", cntLen, true);
		//objEdit.document.addEventListener("change", cntLen, true);
	}
	else {
		//objEdit.document.onkeydown = cntLen;
		//objEdit.document.onkeypress= cntLen;
		//objEdit.document.onkeyup   = cntLen;
		//objEdit.document.onchange  = cntLen;
		objEdit.document.contentType == 'application/xhtml+xml';
	}
	//objEdit.focus()
	document.getElementById('textarea').form.onsubmit = for_submit;
	//cntLen();
} //end init()

function keyPress(e)
{
	e = e || objEdit.event;

	if (e.keyCode == 13) {
		objEdit.document.write('<br />');
	}
		
}

function cntLen() {
	cnt = max_len - objEdit.document.body.innerHTML.length;
	//document.getElementById('charCnt').innerHTML = cnt + " znaků";
} // cntLen()

//if (document.all)
//window.onload = init

function nouzovka()
{
	nouze = 1
	document.getElementById("editor").style.display = 'none'
	tx = document.getElementById("textarea")
	tx.style.display = 'block'
	//tx.focus()
}

function cmdExec(cmd,opt) 
{
	if (cmd=='createLink' && !ie) { 
		opt=prompt("Zadejte URL: ", "http://");
		if (opt.substring(0,7) != "http://" &&  opt.substring(0,8) != "https://") { 
			opt = "http://" + opt;
		}
	}
	
	if (cmd=='createMailLink') {
		opt=prompt("Zadejte emailovou adresu","@");
		
		if (opt.substring(0,7) != "mailto:") { 
			opt = "http://" + opt;
		}
	}
	//if (ie5){ var edit2 = frames.iedit }
	//else	{ var edit2 = document.getElementById('iedit').contentWindow; }

	objEdit.focus();
	objEdit.document.execCommand(cmd,false,opt);
	objEdit.focus();
	cntLen();
}

function insertImage()
{
	txt  = "Zadejte adresu obrázku, který chcete vložit. "
	var sImgSrc=prompt(txt, "http://");
	if(sImgSrc!=null) cmdExec("InsertImage",sImgSrc);
}

function insertSmile(sImgSmile)
{
	if(sImgSmile!=null) cmdExec("InsertImage",sImgSmile);
}

function emoticon(smile, src) {
	if (nouze==1) {
		var txtarea = document.getElementById('textarea');
		/*txtarea.value  += " :"+smile+":";*/
		txtarea.value  += '<img src="'+src+'" alt="" />';
		txtarea.focus();
	} else {
	/*	if ( smile.lenght >= 1 ) { */
			objEdit.focus();
			
			if(document.selection) {
				sel = objEdit.document.selection.createRange();
				// zkusit getSelection()
				sel.pasteHTML('<img src="'+src+'" alt="" />');
				
				sel.select()
			} else {
					try	{
						theSelection = objEdit.getSelection();
					} catch (e)	{
						return false;
					}
					
			
					theRange = theSelection.getRangeAt(0);
					
						
					theRange.collapse(false);
						
					var theImageNode = document.createElement("img");
						
					theImageNode.src = src;
					theImageNode.width = "15";
					theImageNode.height= "15";
					theImageNode.alt = "";
					
					theRange.insertNode(theImageNode);	
												
			}
	/*	} */
	}
	return false;
}
function for_submit()
{
	if (nouze==0)
	{
		document.getElementById('textarea').value = objEdit.document.body.innerHTML
	}
	cont_len = document.getElementById('textarea').value.length
	if ( cont_len > max_len)
	{
		alert("Bylo napsáno "+cont_len+" znaku. Povoleno je "+max_len+" znaku.")
		return false;
	}
	return true
}

function showColor()
{
	if (document.getElementById('colorTab').style.display == 'none')
		document.getElementById('colorTab').style.display = 'block';
	else
		document.getElementById('colorTab').style.display = 'none';
	
	document.getElementById('colorTab').innerHTML = color();
}
 	

function setColor(fcolor)
{
	objEdit.document.execCommand("foreColor",false,fcolor);
}
function color()
{
	txt = ""
	txt += "<table border=0 cellpadding=0 cellspacing=1 bgcolor='black'>"
	/*begin:added for ecredit*/
	//txt += "<tr>\n";
	//txt += "<td bgcolor=\"#4D595E\" ><a href=\"javascript:setColor('4D595E')\"><img src='images/layout/blank.gif' width='10' height='10' ></a></a></td>";
	//txt += "<td bgcolor=\"#f47423\" ><a href=\"javascript:setColor('f47423')\"><img src='images/layout/blank.gif' width='10' height='10' ></a></a></td>";
	txt += "\n</tr>";
	/*end:added for ecredit*/
	
	c = new Array("","FF","CC","99","66","33","00")
	for (i=1;i <=6;i++)
	{
		if (i >1)
			txt = txt + "</tr>\n<tr>\n"
		for (m=1;m <=6;m++)
		{
			for (n=1;n <=6;n++)
			{
				colour = c[n] + c[i] + c[m]; // imn
				txt += "<td bgcolor=\"#"+colour+"\" ><a href=\"javascript:setColor('"+colour+"')\"><img src='images/layout/blank.gif' width='10' height='10' ></a></a></td>\n"
			}
		}
	}
	txt += "</table><div style='font-size:92%;'>Zvolte barvu a pište text nebo označte část textu a klikněte na barvu</div>"
	return txt
}
init();



