var errorMessageDefault = 'Wystąpił nieoczekiwany błąd. \nEdytor HTMLowy działa prawidłowo w Internel Explorer 5.0 lub nowszym.';
// ----------------------------------------------------------------------------
var rng,bTextMode,colorCommand;
bTextMode=false;
// ----------------------------------------------------------------------------
function check(a,name){
	if(a){
		document.getElementById(name).submit()
	}else{
		alert('Browse to image first!')
	}
}
// ----------------------------------------------------------------------------
window.onerror=errorTrap;
// ----------------------------------------------------------------------------
function errorTrap(sMsg,sUrl,sLine){
   content="An error was thrown and caught.\n\n";
   content+="Error: " + sMsg + "\n";
   content+="Line: " + sLine + "\n";
   content+="URL: " + sUrl + "\n";
   alert(content);
   return false;
}
// ----------------------------------------------------------------------------
function formatC(f,opt) {
	if (bTextMode) { return false; }
	if (opt=="removeFormat") {
		f=opt;
		opt=null;
	}
	if (opt==null) document.getElementById('editor').contentWindow.document.execCommand(f);
	else document.getElementById('editor').contentWindow.document.execCommand(f,"",opt);
	editor.focus();
}
// ----------------------------------------------------------------------------
function command(c) {
	HideAll();
	if (c=="New") {
		sonw=showModelessDialog(null,window,"edge:Raised;center:Yes;help:No;resizable:No; status:No;dialogWidth:280px;dialogHeight:100px");
		sonw.document.open();
		sonw.document.write('<body style="background-color: #d4d0c8; margin-left:20px"><table><tr><td><img src="gfx/wykrzyknik.gif"></td><td style="font-family:Arial;font-size:12px">Czy zapisa� zmiany w pliku?</td></tr><tr><td nowrap align="center" colspan="2">&nbsp;&nbsp;<input type="button" value="Tak" style="background-color: #d4d0c8; width: 30%;font-family:Arial;font-size:12px" onclick="dialogArguments.commandEx(\'SaveClose\')">&nbsp;&nbsp;<input type="button" value="Nie" onclick="dialogArguments.commandEx(\'New\');window.close()" style="background-color: #d4d0c8; width: 30%;font-family:Arial;font-size:12px">&nbsp;&nbsp;<input type="button" value="Anuluj" onclick="window.close()" style="background-color: #d4d0c8; width: 30%;font-family:Arial;font-size:12px"></td></tr></table>');
		sonw.document.close();
	}
	if (c=="Open") {
		document.fileform.filetoload.click();
	}
	if (c=="countBytes") {
		alert("Text only: "+editor.document.body.createTextRange().text.length+" bytes.\n"+"Text and source code: "+editor.document.body.createTextRange().htmlText.length+" bytes.\n");
	}

	if (bTextMode) { return false; }

	if (c=="InsertTable") {
		//sonw=showModelessDialog(null,window,"edge:Raised;center:Yes;help:No;resizable:No; status:No;dialogWidth:280px;dialogHeight:130px");
		//sonw.document.open();
		//sonw.document.write('<body style="background-color: menu; margin-left:20px"><table><tr><td>Rows:</td><td><input name="rows" type="text" value="2"></td></tr><tr><td>Columns:</td><td><input name="cols" type="text" value="2"></td></tr><tr><td colspan="2" align="center"><input type="button" value=" OK " onclick="dialogArguments.wstawTabeleEx(document.getElementById.rows.value,document.getElementById.cols.value);window.close()">&nbsp;&nbsp;<input type="button" value="Cancel" onclick="window.close()"></td></tr></table>');
		//sonw.document.close();
		document.getElementById('insertTableDialog').style.display='none';
		var inpRows=document.getElementById('inpRows');
		var inpCols=document.getElementById('inpCols');
		wstawTabeleEx(inpRows.value, inpCols.value);
	}
	if(c=="ShowTableDialog"){
		var td=document.getElementById('insertTableDialog');
		td.style.display="block";
		setTimeout('hideTableDialog()', 10000);
	}


	if (c=="prepareForecolor") {
		document.getElementById('colorSet').style.display='block'; colorCommand='Forecolor';
		setTimeout('hideColorSet()',9000);
	}
	if (c=="prepareBackcolor") {
		document.getElementById('colorSet').style.display='block'; colorCommand='Backcolor';
		setTimeout('hideColorSet()',9000);
	}
	if (c=="prepareBackground") {
		document.getElementById('colorSet').style.display='block'; colorCommand='Background';
		setTimeout('hideColorSet()',9000);
	}
	editor.focus();
}
// ----------------------------------------------------------------------------
function hideColorSet() {
	document.getElementById('colorSet').style.display='none';
}
// ----------------------------------------------------------------------------
function hideTableDialog(){
	document.getElementById('insertTableDialog').style.display='none';
}
// ----------------------------------------------------------------------------
function HideAll() {
	HideInsertLink();
	HideInsertLinkLocal();
	hideColorSet();
	hideTableDialog();
}
function HideInsertLink() {
	document.getElementById('insertLink').style.display = 'none';
}
function HideInsertLinkLocal() {
	document.getElementById('insertLinkLocal').style.display = 'none';
}
// ----------------------------------------------------------------------------
function setColor(c) {
	if (bTextMode) {
		return false;
	}
	document.getElementById('colorSet').style.display="none";
	var ed = document.getElementById('editor');
	if (colorCommand=="Background") {
		ed.contentWindow.document.body.style.background=c;
	} else {
		ed.contentWindow.focus();
		ed.contentWindow.document.execCommand(colorCommand,"",c);
	}

	editor.focus();
}
// ----------------------------------------------------------------------------
function fileload() {
	alert();
	//document.fileform.submit();
}

//  var sData = ;
 // sData.sUserName = oEnterName.value;
  //sData.fnUpdate();
// ----------------------------------------------------------------------------
function CreateLink() {
	HideAll();
	document.getElementById('insertLink').style.display = '';
	editor.focus();
	setTimeout("HideInsertLink();", 20000);
}
// ----------------------------------------------------------------------------
function CreateLinkLocal() {
	HideAll();
	document.getElementById('insertLinkLocal').style.display = '';
	editor.focus();
	setTimeout("HideInsertLinkLocal();", 30000);
}

// ----------------------------------------------------------------------------
function InsertLinkOld() {
	var link;
	link = "<a href='http://"+document.getElementById('insertLinkValue').value+"'>"+document.getElementById('insertLinkName').value+"</a>";
	//alert(link);
	editor.document.body.innerHTML = editor.document.body.innerHTML + link;
	document.getElementById('insertLink').style.display = 'none';
	editor.focus();
}
// ----------------------------------------------------------------------------
function InsertLink() {
	var link;
	var linkValue;
	linkValue = document.getElementById('insertLinkValue').value;
	linkValue = linkValue.replace('http://', '');
	link = "<a href='http://"+linkValue+"'>"+document.getElementById('insertLinkName').value+"</a>";
	editor.focus();
	InsertInEditor(link);
	editor.focus();
}
function InsertLinkLocal() {
	var link;
	var linkValue;
	linkValue = document.getElementById('insertLinkValueLocal').value;
	//linkValue = linkValue.replace('http://', '');
	link = "<a href='"+linkValue+"'>"+document.getElementById('insertLinkNameLocal').value+"</a>";
	editor.focus();
	InsertInEditor(link);
	editor.focus();
}
// ----------------------------------------------------------------------------
function InsertAskMe(p_idLangauge) {
	var text = "";
	if (p_idLangauge==1) {
		text = "<P><A href=\"./site.php?action=site&id_site=61&id_language=1&display_menu=1&\"><IMG src=\"./img/zapytaj.gif\" border=0></A></P>";
	}
	else if (p_idLangauge==2) {
		text = "<P><A href=\"./site.php?action=site&id_site=91&id_language=1&display_menu=1&\"><IMG src=\"./img/zapytaj_en.gif\" border=0></A></P>";
	}
	SetHTML();
	editor.focus();
	editor.document.body.innerHTML = editor.document.body.innerHTML + text;
	editor.focus();
}
// ----------------------------------------------------------------------------
function InsertInEditor(p_text) {
	try {
		var ed = document.getElementById('editor');
		ed.contentWindow.focus();

		if (window.getSelection)
		{ // firefox
			trng = ed.contentWindow.getSelection();
//			alert('window.getSelection: ' + trng);
			// TODO: Zastanowić sie jak wstawiać to we własciwe miejsce
			ed.contentWindow.document.body.innerHTML = ed.contentWindow.document.body.innerHTML + p_text;
		}
		else if (document.getSelection)
		{
			trng = ed.contentWindow.document.getSelection();
//			alert('document.getSelection: ' + trng);
			ed.contentWindow.document.body.innerHTML = ed.contentWindow.document.body.innerHTML + p_text;
		}
		else if (document.selection)
		{ // IE
			trng = ed.contentWindow.document.selection.createRange();
			trng.pasteHTML(p_text);
	//		alert('document.selection: ' + trng);
		}


		trng=null;
	}
	catch (ex) {
		trng=null;
		alert(ex);
	}
}
// ----------------------------------------------------------------------------
function InsertProduct(p_path, p_link, p_description, p_description_size, p_idLanguage) {
	var p_html = GetProductDescription(p_path, p_link, p_description, p_description_size, p_idLanguage);
	//alert(p_html);
	var ed = window.opener.document.getElementById('editor');
	ed.contentWindow.focus();
	trng = ed.contentWindow.document.selection.createRange();
//
	trng.pasteHTML(p_html);
	trng=null;

//	InsertInEditor(p_html);
//	window.opener.document.getElementById('editor').document.body.innerHTML += p_html;
	window.opener.focus();
	window.close();
}
function GetProductDescription(p_path, p_link, p_description, p_description_size, p_idLanguage) {
	var html;
	html = '<table width="485" cellpadding="0" cellspacing="0">';
	html += '		<tr>';
	html += '			<td valign="top" align="center" width="265">';
	html += '				<img src="'+p_path+'" style="border:1px solid #0E618B;">';
	html += '			</td>';
	html += '			<td valign="top" align="left" width="220">';
	if (p_link) {
		html += '				<img src="./img/product/dokumentacja_'+p_idLanguage+'.gif" width="118" height="15" alt="" border="0"><p>';
		html += '				<table width="100%" cellpadding="0" cellspacing="0">';
		html += '					<tr>';
		html += '						<td width="40" valign="top" align="left"><img src="./img/product/pdf.gif" width="32" height="32" alt="" border="0"></td>';
		html += '						<td valign="top" align="left">';
		html += '							<a href="'+p_link+'" class="product" target="_blank">';
		html += '								<b>'+p_description+'</b><br />';
		html += '								'+p_description_size+'';
		html += '							</a>';
		html += '						</td>';
		html += '					</tr>';
		html += '				</table>';
	}
	html += '			</td>';
	html += '		</tr>';
	html += '	</table>';
//	alert(html);
	return html;
}


function SetHTML() {
	var viewSource = document.getElementById('switchMode');
	if (viewSource && viewSource.checked == true) {
		viewSource.click();
	}
//		cleanWordHTML();
}

function tmpABCA(p_formName, p_formFieldName) {
	var isEditorTxt = false;
	try {
		isEditorTxt = editorTxt
	}
	catch (Exception) {
		isEditorTxt = false;
	}
	if (!isEditorTxt) {
		//alert('test');
		SetHTML();
		f=document.getElementById(p_formName);
		document.getElementById(p_formFieldName).value = editor.document.body.innerHTML;
	}
}
function dataToEditor(p_formFieldName) {
	var value = document.getElementById(p_formFieldName).value;
//	alert('dataToEditor: ' + value + document.getElementById(p_formFieldName) + p_formFieldName);
//	editor.contentWindow.document.open();
//	editor.document.write(value);
//	editor.contentWindow.document.close();
//	editor.contentWindow.focus();
	document.getElementById('editor').contentWindow.document.open();
	document.getElementById('editor').contentWindow.document.write('<html><head><META http-equiv="Content-Type" content="text/html; charset=utf-8;">');
	document.getElementById('editor').contentWindow.document.write('<link href="./css/mian.css" rel="stylesheet" type="text/css">');
	document.getElementById('editor').contentWindow.document.write('<link href="./css/body.css" rel="stylesheet" type="text/css">');
	document.getElementById('editor').contentWindow.document.write('<link href="./css/menu.css" rel="stylesheet" type="text/css">');
	document.getElementById('editor').contentWindow.document.write('</head><body style="background-image: none; background-color: white;"> ');
	document.getElementById('editor').contentWindow.document.write('' + value);
	document.getElementById('editor').contentWindow.document.close();
	document.getElementById('editor').contentWindow.focus();
}

function commandEx(c) {
	if (c=="New") {
		document.location="pliki_baza/plik_baza.txt";
	}
	if (c=="Save" || c=="SaveClose") {
		f=document.forms("DataSet");
		f.Body.value=editor.document.body.innerHTML;
		f.Action.value=c;
		if (f.FileName.value.length<2) {
			document.getElementById('PageOne').style.cssText="display:none";

			//hide all <select> elements cause it doesn't disapear with div
			var aReturn=document.getElementsByTagName("SELECT");
			for (i=0; i<aReturn.length; i++)
			{
				aReturn[i].style.visibility="hidden";
			}

			document.getElementById('PageTwo').style.cssText="display:block";
		} else {
			f.submit();
		}
	}

	editor.focus();
}

function getObjInnerText (obj) {
	return (obj.innerText) ? obj.innerText : ((obj.textContent) ? obj.textContent : "");
}
function setObjInnerText(obj, text) {
	if (obj.textContent) {
		obj.textContent = text;
	}
	else if (obj.innerText) {
		obj.innerText = text;
	}
}
var cont;
function setMode(newMode) {
	bTextMode = newMode;
	if (bTextMode) {
		cont = editor.document.body.innerHTML;
		setObjInnerText(editor.document.body, cont);
	} else {
		cont = getObjInnerText(editor.document.body);
		editor.document.body.innerHTML = cont;
	}
	editor.focus();
}

function saveUserData(){
	var oPersist=document.getElementById.tempsave;
	oPersist.innerHTML=editor.document.body.innerHTML;
	oPersist.setAttribute("sPersistText",oPersist.innerHTML);
	oPersist.save("oXMLBranch");
}

function loadUserData(){
	var oPersist=document.getElementById.tempsave;
	oPersist.load("oXMLBranch");
	oPersist.innerHTML=oPersist.getAttribute("sPersistText");
	editor.document.body.innerHTML=oPersist.innerHTML;
}

function setStatus(txt) {
	document.getElementById.status.style.background='#000077';
	document.getElementById.status.style.color='#ffffff';
	document.getElementById.status.value=txt;
}

function clearStatus() {
	document.getElementById.status.style.background='#d4d0c8';
	document.getElementById.status.value="";
}

function cleanWordHTML() {
	try {
		//alert('cleanWordHTML');
		bt=new Array(); nt=new Array();
		bt[0]=/\<br[^\>]+\>/gi;					nt[0]='<br>';
		bt[1]=/\<o\:p\>/gi;						nt[1]='';
		bt[2]=/\<\/o\:p\>/gi;					nt[2]='';
		bt[3]=/ class=MsoNormal/gi;				nt[3]='';
		bt[4]=/ class=MsoBodyText3/gi;			nt[4]='';
		bt[5]=/tab\-stops\: [0-9][^\;\"]*/gi;   nt[5]='';
		bt[6]=/ style=\"MARGIN: 0cm 0cm 0pt[\;\ ]*\"/gi;	nt[6]='';
		bt[7]=/MARGIN: 0cm 0cm 0pt\;/gi;					nt[7]='';
		bt[8]=/\<span[^\>]*\>\<\/span\>/gi;					nt[8]='';
		bt[9]=/\<span[^\>]*\>\<br\>\<\/span\>/gi;			nt[9]='<br>';
		bt[10]=/\<span[^\>]*\>\<br\>\<br\>\<\/span\>/gi;			nt[10]='<br><br>';
		bt[11]=/\<span[^\>]*\>\<br\>\<br\>\<br\>\<\/span\>/gi;			nt[11]='<br><br><br>';
		bt[12]=/\<p[^\>]*\>&nbsp;\<\/p\>/gi;				nt[12]='<br>';
		bt[13]=/\<p\>\<\/p\>/gi;							nt[13]='<br>';



	//	editor.document.designMode="Off";
		var setbackmode=false;

		if (bTextMode!=true) { setMode(true); setbackmode=true;}
		//editor.document.getElementById["cialo"].style.cursor="wait";
		for (j=0; j<=13; j++) {
	//		re = new RegExp(bt[j]);
	//		alert(bt[j]);
			trng=editor.document.body.createTextRange();
			trng.text=trng.text.replace(bt[j],nt[j]);
	//		trng.move('word',-99999);
		}
		if (bTextMode && setbackmode) { setMode(false); }
		//editor.document.getElementById["cialo"].style.cursor="default";
		editor.focus();
	}
	catch (ex) {
		alert(errorMessageDefault);
	}
}

function szukaj(what,iFlags) {
	if (!what) { setStatus('Nothing to search');setTimeout('clearStatus()',3000); return false;}
	if (!iFlags) { iFlags=0; }
	if (rng!=null) {
		if (rng.findText(what,16000000,iFlags)==true) { rng.scrollIntoView(); rng.select(); rng.move('word'); }
		else {
			rng.move('word',-99999);
			setStatus('Passed to the end of file');setTimeout('clearStatus()',3000);
		}
	} else {
		rng = editor.document.body.createTextRange();
		szukaj(what,iFlags);
	}
}

function zastap() {
		sonw=showModelessDialog("findreplace.html",window,"edge:Raised;center:Yes;help:No;resizable:No; status:No;dialogWidth:390px;dialogHeight:190px");
//		sonw.document.open();
//		sonw.document.write('');
//		sonw.document.close();
}

function zastapEx(what,withword,where,how,iFlags) {
	if (!what) { setStatus('Nothing to search');setTimeout('clearStatus()',3000); return false;}
	//replace all
	if (how==1) {
		if (where==0) {
			if (rng==null) { rng = editor.document.body.createTextRange(); }
			var i=0;
			while (rng.findText(what,16000000,iFlags)==true) {
				rng.pasteHTML(withword); rng.move('word'); i++;
			}
			rng.move('word',-99999);
			setStatus(i+' occurences have been replaced.');setTimeout('clearStatus()',3000);
		} else if (where==1) {
			var trng=null,tmp=null;
			trng = editor.document.selection.createRange();
			tmp = editor.document.selection.createRange();
			var i=0;
			while (trng.findText(what,16000000,iFlags)==true && trng.compareEndPoints('EndToEnd',tmp)<1) {
				trng.pasteHTML(withword); trng.move('word'); i++;
			}
			trng.move('word',-99999);
			setStatus(i+' occurences have been replaced.');setTimeout('clearStatus()',3000);
		}
	}
	//replace one
	if (how==0) {
		if (rng==null) { szukaj(what,iFlags); return false;}
		trng = editor.document.selection.createRange();
		if (trng.text==what) { trng.pasteHTML(withword); szukaj(what); }
	}
}

//function wstawTabeleEx(rows,cols) {
//	var i,j,trng=null;
//	var content;
//	content="";
//	var ed = document.getElementById('editor');
//	ed.contentWindow.focus();
//	trng = ed.contentWindow.document.selection.createRange();
//
//	for (i=1; i<=rows; i++) {
//		content+='<tr>';
//		for (j=1; j<=cols; j++) {
//			content+='<td>&nbsp;</td>';
//		}
//		content+='</tr>';
//	}
//	trng.pasteHTML('<table border="1" cellspacing="0" cellpadding="2">'+content+'</table>');
//	trng=null;
//}
function wstawTabeleEx(rows,cols) {
	var i,j,trng=null;
	var content;
	content="";
	var ed = document.getElementById('editor');
	ed.contentWindow.focus();
	trng = ed.contentWindow.document.selection.createRange();

	for (i=1; i<=rows; i++) {
		if (i%2==0) {
			content+='<tr bgcolor="#fefbf2">';
		}
		else {
			content+='<tr bgcolor="#f4ebc2">';
		}
		for (j=1; j<=cols; j++) {
			content+='<td>&nbsp;</td>';
		}
		content+='</tr>';
	}
	trng.pasteHTML('<TABLE cellpadding="3" width="80%" border="0" cellspacing="2">'+content+'</table>');
	trng=null;
}

function copyBetweenAux() {
	editor.document.body.innerHTML=auxframe.document.body.innerHTML;
}

function loadFromURL(url) {
	auxframe.document.location="readURL.php?URL="+url;
//	auxframe.onload=copyBetweenAux();
}
