onload = function(){
   init_editor();
}

function init_editor() {
	if (document.getElementById('editor')) {
		edi = document.getElementById('editor');
		edi.contentWindow.document.designMode = 'On';
    try{
       edi.contentWindow.document.execCommand("useCSS", false, true)
    }catch(e){}
	}
}

function simpletag(cmd, opt){
	edi = document.getElementById('editor');
	edi.contentWindow.document.execCommand(cmd, false, opt || "");
}
function tag_url(){
	edi = document.getElementById('editor');

	var enterURL = prompt("Введите адрес сайта", "http://");

	if(edi.contentWindow.document.getSelection) {
		len=edi.contentWindow.document.getSelection();
	}else if(edi.contentWindow.document.selection){
		len=edi.contentWindow.document.selection.createRange().text;
	}

	if(len!=""){
		edi.contentWindow.document.execCommand("CreateLink",false,enterURL);
	} else {
		var enterText = prompt("Введите название сайта", "Сайт");

		link=document.createElement('a');
		link.href=enterURL;
		link.innerHTML=enterText;

		insertNode(link);
	}
}
function tag_quote(){
	edi = document.getElementById('editor');
	var iWin = edi.contentWindow;
	var iDoc = edi.contentWindow.document;

	var sel = (iDoc.selection) ? iDoc.selection : iWin.getSelection();
	var rng = (sel.createRange) ? sel.createRange() : sel.getRangeAt(sel.rangeCount - 1);

	if (rng.pasteHTML) { // IE
		html = '<div class="quotetop">Цитата</div><div class="quotemain">' + rng.text + "</div>";
		rng.pasteHTML(html);
	} else if (rng.insertNode) { // Gecko
		var div = document.createElement("div");
		div.innerHTML = sel;
		div.className = "quotemain";
		rng.deleteContents();
		rng.insertNode(div);
	}

}

function addQuote(text){
	edi = document.getElementById('editor');
	edi.contentWindow.document.body.innerHTML += '<div class="quotemain">' + text + "</div><br><br>";
	return true;
}


function insertNode(anode) {
	edi = document.getElementById('editor');
	var iWin = edi.contentWindow;
	var iDoc = edi.contentWindow.document;

	var ua = navigator.userAgent.toLowerCase();
	var isOpera = ua.indexOf("opera") != -1;
	var sel = (iDoc.selection) ? iDoc.selection : iWin.getSelection();
	var rng = (sel.createRange) ? sel.createRange() : sel.getRangeAt(sel.rangeCount - 1);
	if (rng.pasteHTML) { // IE
		var sp = document.createElement("span");
		sp.appendChild(anode);
		rng.pasteHTML(sp.innerHTML);
	} else if (rng.insertNode && !isOpera) { // Gecko
		rng.deleteContents();
		rng.insertNode(anode);
	} else if (isOpera) { // Opera
		var sp = document.createElement("span");
		sp.appendChild(anode);
		iDoc.body.innerHTML += sp.innerHTML;
	}
}

function getText(){
	var edi = document.getElementById('editor');
  var doc = edi.contentDocument;
  if (doc == undefined || doc == null)
    doc = edi.contentWindow.document;
  var inp = document.getElementById('strText');
  inp.value = doc.body.innerHTML;
	return true;
}

function doInsert() {
  var iframe = document.getElementById('editor');
  var wysiwyg = iframe.contentWindow.document;

  var sHTML = fixWord(wysiwyg.body);
  
  wysiwyg.body.innerHTML = sHTML;
  console.info(wysiwyg.body.innerHTML);
  
  iframe.focus();
}

function fixWord(wysiwyg) {
  for (var i=0;i<wysiwyg.getElementsByTagName("*").length;i++) {
		wysiwyg.getElementsByTagName("*")[i].removeAttribute("class");
		wysiwyg.getElementsByTagName("*")[i].removeAttribute("style");
  }
  
  var str=wysiwyg.innerHTML;

	str=String(str).replace(/<\\?\?xml[^>]*>/gi,"");
	str=String(str).replace(/<\/?xml[^>]*>/gi,"");
	str=String(str).replace(/<\/?o:p[^>]*>/gi,"");
	str=String(str).replace(/<\/?v:[^>]*>/gi,"");
	str=String(str).replace(/<\/?o:[^>]*>/gi,"");
	str=String(str).replace(/<\/?w:[^>]*>/gi,"");
	str=String(str).replace(/<\/?st1:[^>]*>/gi,"");
	
	//str=String(str).replace(/<!--[^>]*-->/gi,"");
	//str=String(str).replace(/<style>[.]+<\/style>/gi,"");
	
	str=String(str).replace(/<p>&nbsp;<\/p>/gi,"");//<p>&nbsp;</p>
	str=String(str).replace(/<p><\/p>/gi,"<br>");//<p>&nbsp;</p>
	
	str=String(str).replace(/<\/?meta[^>]*>/gi,"");
	str=String(str).replace(/<\/?link[^>]*>/gi,"");
	
	str=String(str).replace(/<\/?span[^>]*>/gi,"");
	str=String(str).replace(/<\/?font[^>]*>/gi,"");
	//str=String(str).replace(/<\/?strong[^>]*>/gi,"");
	
	str=String(str).replace(/<\/?H1[^>]*>/gi,"");
	str=String(str).replace(/<\/?H2[^>]*>/gi,"");
	str=String(str).replace(/<\/?H3[^>]*>/gi,"");
	str=String(str).replace(/<\/?H4[^>]*>/gi,"");
	str=String(str).replace(/<\/?H5[^>]*>/gi,"");
	str=String(str).replace(/<\/?H6[^>]*>/gi,"");
	
	str=String(str).replace(/<\/?P[^>]*><\/P>/g,"");
	return str; 
}
