﻿function makeBold(){
   document.execCommand("BOLD");
}


function ApplyStyle(){
  mystyle = document.myform.currentStyle.value;
  if(mystyle!="") {

  theSelection = document.selection.createRange().text;
  if (theSelection) {
  
    
  document.selection.createRange().text = "<span class='"+mystyle+"'>" + theSelection + "</span>";

  var contents = document.getElementById('mycontent1').innerHTML;
  contents =  replacewithTags(contents,"&lt;span class='"+mystyle+"'&gt;","<span class='"+mystyle+"'>");
  contents =  replacewithTags(contents,"&lt;/span&gt;","</span>");
  document.getElementById('mycontent1').innerHTML = "";
  document.getElementById('mycontent1').innerHTML = contents;
  document.myform.currentStyle.value = "";
	 } // if (theSelection) {

}  // if(mystyle!="") {

}


function ApplyLink(){
  mytarget = document.myform.currentTarget.value;
  mylink = document.myform.currentLink.value;

  if(mylink!="") {
 		
  
  theSelection = document.selection.createRange().text;
  if (theSelection) {
     	
     
  document.selection.createRange().text = "<A href='"+mylink+"' target='"+mytarget+"'>" + theSelection + "</A>";

  var contents = document.getElementById('mycontent1').innerHTML;
  contents =  replacewithTags(contents,"&lt;A href='"+mylink+"' target='"+mytarget+"'&gt;","<A href='"+mylink+"' target='"+mytarget+"'>");
  contents =  replacewithTags(contents,"&lt;/A&gt;","</A>");
  document.getElementById('mycontent1').innerHTML = "";
  document.getElementById('mycontent1').innerHTML = contents;
  document.myform.currentLink.value="";
  document.myform.currentTarget.value="";
	} // if (theSelection) {
} // if(mylink!="") {
}


function setImage(headimage)
  {
  	if(opener) {
	   opener.document.myform.headimage.value = headimage ;
	} else  {
	  document.myform.headimage.value = headimage ;
   	}
 }



function CHOOSESTYLE()
	{
		document.getElementById('styleselector').style.visibility='visible';
	}


function CHOOSELINK()
	{
		document.getElementById('linkselector').style.visibility='visible';
	}


function replacewithTags(originalSTR,tobereplaced,replace)
	{
		if(!originalSTR)
			return "";
		str = originalSTR.replace(tobereplaced,replace);
		return str;

	}

function CUT(){
 document.execCommand("cut");
}
function COPY(){
 document.execCommand("copy");
}
function PASTE(){
 document.execCommand("paste");
}

function LIST(){
 document.execCommand("InsertUnorderedList");
}

function UNDO(){
 document.execCommand("UNDO");
}

function REDO(){
 document.execCommand("REDO");
}

function LINK(){
 document.execCommand("CreateLink");
}

function INDENT(){
 document.execCommand("Indent");
}


function ADDNEXTTEXTBLOCK_BEGINNING(){
document.getElementById('mycontent1').innerHTML = textTemplate+ document.getElementById('mycontent1').innerHTML;
}


function ADDNEXTTEASERBLOCK_BEGINNING(){
document.getElementById('mycontent1').innerHTML = teaserTemplate + document.getElementById('mycontent1').innerHTML;
}

function ADDNEXTTEASERBLOCK_END(){
document.getElementById('mycontent1').innerHTML =  document.getElementById('mycontent1').innerHTML + teaserTemplate;
}


function ADDNEXTIMAGEBLOCK_BEGINNING(){
document.getElementById('mycontent1').innerHTML = imageTemplate+ document.getElementById('mycontent1').innerHTML;
}


function ADDNEXTIMAGEBLOCK_END(){
document.getElementById('mycontent1').innerHTML = document.getElementById('mycontent1').innerHTML + imageTemplate;
}


function ADDNEXTTEXTBLOCK_END(){
document.getElementById('mycontent1').innerHTML = document.getElementById('mycontent1').innerHTML + textTemplate;
}

function RemoveLink() {
   document.execCommand("Unlink");
}

function postValues()
	{
		document.myform.divcontents.value = document.getElementById('mycontent1').innerHTML;
		document.myform.submit();
	}


function replaceImage(imgname)
   {
   	if(opener) {
   	   var mytext = opener.document.getElementById('mycontent1').innerHTML;
   	} else  {
   	     var mytext = document.getElementById('mycontent1').innerHTML;
   	}
   	
   	mytext = stringReplace(mytext, "replacethisimage.jpg", imgname);
   	if(opener) {
   		opener.document.getElementById('mycontent1').innerHTML = mytext;
   	} else  {
   		document.getElementById('mycontent1').innerHTML = mytext;
   	}
   	


   }
    
 
     function stringReplace(originalString, findText, replaceText) {
       var pos = 0;
       var len = findText.length;
       pos = originalString.indexOf(findText);
       while (pos != -1) {
         preString = originalString.substring(0, pos);
         postString = originalString.substring(pos + len, originalString.length);
         originalString = preString + replaceText + postString;
         pos = originalString.indexOf(findText);
       }
       return originalString;
    }
