
var inhtmlview=false;

// Function to allow new page editing function compatible with this js file.
function setInhtmlview(bol)
{
    inhtmlview = bol;
}

// Function to write HTML from a regular textarea to the IFRAME editfield.
function writeHtml(iframefield, textfield)
{	
    iframefield.document.open();
    if(textfield.value == "")
        iframefield.document.write("<font></font>");
    else
        iframefield.document.write(textfield.value);
    iframefield.document.close();
    iframefield.document.designMode="On";
}

// Function to save HTML from an IFRAME editfield to a textarea.
function saveHtml(textfield, iframefield)
{
	// Is user in HTML-viewing mode?  If so, first switch back to rich-text view.
	if('no' == iframefield.document.body.getAttribute('viewastext'))
    	{
		textfield.value = iframefield.document.body.innerText;
    	}
	else
	{
		textfield.value = iframefield.document.body.innerHTML;
	}
}

function switchView(iframefield, imgptr)
// Toggle between HTML and rich text view.
{
   // Re-set the image button that called this.	
   if('html' == imgptr.getAttribute('value'))
   {
       var innerhtml = iframefield.document.body.innerHTML;
       iframefield.document.body.innerText = innerhtml;
       iframefield.document.body.setAttribute('viewastext', 'no');	

       imgptr.setAttribute('src', '../images/checkon.gif');
       imgptr.setAttribute('value', 'text');

	   inhtmlview=true;
   }
   else
   {
      var innertext = iframefield.document.body.innerText;
      iframefield.document.body.innerHTML = innertext;
      iframefield.document.body.setAttribute('viewastext', 'yes');

      imgptr.setAttribute('src', '../images/checkoff.gif');
      imgptr.setAttribute('value', 'html');

      inhtmlview=false;
   }

   iframefield.focus();
}

function check_html_mode()
{
	if(inhtmlview)
    {
       alert("Uncheck \"View HTML\" to use this command.");
       return true;
    }

    return false;
}

// Function to perform a command in one of the IFRAME editfields.
function doCommand(iframefield, cmd)
{
	if(check_html_mode())
        {
		return;
	}
	else
    {
        iframefield.document.execCommand(cmd);
        // Sometimes doc loses focus after command.
        iframefield.focus();
    }
}

function doBold(iframefield)
{
    doCommand(iframefield, 'bold');
}

function doUnderline(iframefield)
{
    doCommand(iframefield, 'underline');
}

function doItalic(iframefield)
{
    doCommand(iframefield, 'italic');
}

function doUnorderedList(iframefield)
{
    doCommand(iframefield, 'insertunorderedlist');
}

function doOrderedList(iframefield)
{
    doCommand(iframefield, 'insertorderedlist');
}

function doCut(iframefield)
{
    doCommand(iframefield, 'cut');
}
function doCopy(iframefield)
{
    doCommand(iframefield, 'copy');
}
function doPaste(iframefield)
{
    doCommand(iframefield, 'paste');
}
function doUndo(iframefield)
{
    doCommand(iframefield, 'undo');
}
function doRedo(iframefield)
{
    doCommand(iframefield, 'redo');
}
// Pop up a link dialog, then make the selected text into a link.
// The IFRAME handles this nicely.
function doBrowserLink(iframefield)
{
	iframefield.focus();
	doCommand(iframefield, 'createlink');
}


function doLink(iframefield) 
{
  if(check_html_mode())
      return;
  
  if(iframefield.document.selection.type == "None") 
  {
      var isA = getEl("A",iframefield.document.selection.createRange().parentElement());
      var str=prompt("Enter link location (e.g. http://www.norvax.com):", isA ? isA.href : "http:\/\/"); 

	  iframefield.focus();

      if ((str!=null) && (str!="http://")) {
        var sel=iframefield.document.selection.createRange();
        sel.pasteHTML("<A HREF=\""+str+"\">"+str+"</A> ");
        sel.select();
      }
  }  
  else doBrowserLink(iframefield);

}


//Finds and returns an element.
function getEl(sTag,start) {
  while ((start!=null) && (start.tagName!=sTag)) start = start.parentElement;
  return start;
}


function removeFormatting(iframefield)
// Remove the formatting from the entire document.
{
   if(check_html_mode())
      return;

   iframefield.focus();   

   var sel_range = iframefield.document.selection.createRange();
   var body_elem = getEl("BODY", sel_range.parentElement());
   
   if(body_elem)
      body_elem.innerHTML = body_elem.innerText;

   iframefield.focus();
}

function doHeader(iframefield, header)
{
   if(check_html_mode())
      return;

   // 7 means "Remove Formatting".
   if(header == "7")
   {
	   removeFormatting(iframefield);
       return;
   }

   // Handle <h1> through <h6> tags.
   // This will remove the tag if it's already on the selection.
   // Incoming header should be "1" through "6", or "none".
   // Make a range for the selection.
   var sel_range=iframefield.document.selection.createRange();
   var parent_elem=sel_range.parentElement();
	
   if(sel_range.text == "")
	  return;

   // Check if there is already a header on this selection.
   var isH1 = getEl("H1", parent_elem);
   var isH2 = getEl("H2", parent_elem);
   var isH3 = getEl("H3", parent_elem);
   var isH4 = getEl("H4", parent_elem);
   var isH5 = getEl("H5", parent_elem);
   var isH6 = getEl("H6", parent_elem);

   // If there is already a header, remove the header.
   if(isH1 || isH2 || isH3 || isH4 || isH5 || isH6)
   {      
      // Replace the HTML containing the header with the HTML inside the header.
      var str=parent_elem.innerHTML;
      parent_elem.outerHTML = str;
   }

   var str = sel_range.htmlText;

   // If there was no H1 on this selection, and that's what's requested, then add the h1.
   if (!isH1 && header == "1") 
   {
      sel_range.pasteHTML("<H1>" + str + "</H1>");
   }

   // Likewise with H2, H3, H4...
   if (!isH2 && header == "2") 
   {
      sel_range.pasteHTML("<H2>" + str + "</H2>");
   }   
   if (!isH3 && header == "3") 
   {
      sel_range.pasteHTML("<H3>" + str + "</H3>");
   }
   if (!isH4 && header == "4") 
   {
      sel_range.pasteHTML("<H4>" + str + "</H4>");
   }
   if (!isH5 && header == "5") 
   {
      sel_range.pasteHTML("<H5>" + str + "</H5>");
   }
   if (!isH6 && header == "6") 
   {
      sel_range.pasteHTML("<H6>" + str + "</H6>");
   }

   iframefield.focus();
}
