
function Start(){
  document.getElementById('rteIFrame').contentWindow.document.designMode = "on";
  if(is_gecko){ document.getElementById('rteIFrame').contentWindow.document.execCommand("useCSS", false, null); }
  try {
    document.getElementById('rteIFrame').contentWindow.document.execCommand("undo", false, null);
  }  catch (e) {
    alert("Your level of Mozilla does not support the this page");
  }

  InitToolbarButtons();
  // Get value existing detail to populate the iframe
  var detail = document.getElementById("hidIFrameCode").value;
  // Check for mozilla or ie and assign document object to object variable
  var iframeDoc;
  var stylePath;
  if(document.getElementById("rteIFrame").contentDocument){
	iframeDoc = document.getElementById("rteIFrame").contentDocument;
	stylePath = "<style type='text/css'>@import 'http://glasnevin/DMBB/css/rte.css';</style>"
	iframeArray = iframeDoc.documentElement.childNodes;
	iframeArray[0].innerHTML = stylePath;
	
	iframeArray[1].innerHTML = detail;
  }
  else{
	iframeDoc = document.frames["rteIFrame"].document;
	stylePath = "<style type='text/css'>@import '../css/rte.css';</style>";
	iframeDoc.write("<html><head>" + stylePath + "</head><body>" + detail  + "</body></html>");
  }

  /*
  /* ***Call functions to hide color palette*** 
  document.addEventListener("mousedown", dismisscolorpalette, true);
  document.getElementById("rteIFrame").contentWindow.document.addEventListener("mousedown", dismisscolorpalette, true);
  document.addEventListener("keypress", dismisscolorpalette, true);
  document.getElementById("rteIFrame").contentWindow.document.addEventListener("keypress", dismisscolorpalette, true);
  */
}

function checkSize(){
 alert("keypressed");
}

function dismisscolorpalette()
{
  document.getElementById("colorpalette").style.visibility="hidden";
}

function InitToolbarButtons() {
  var kids = document.getElementsByTagName('DIV');

  for (var i=0; i < kids.length; i++) {
    if (kids[i].className == "imagebutton") {
      kids[i].onmouseover = tbmouseover;
      kids[i].onmouseout = tbmouseout;
      kids[i].onmousedown = tbmousedown;
      kids[i].onmouseup = tbmouseup;
      kids[i].onclick = tbclick;
    }
  }
}

/* ***Start Button States *** */
function tbmousedown()
{
  this.firstChild.style.left = 2;
  this.firstChild.style.top = 2;
  this.style.border="inset 2px";
}

function tbmouseup()
{
  this.firstChild.style.left = 1;
  this.firstChild.style.top = 1;
  this.style.border="outset 2px";
}

function tbmouseout()
{
  this.style.border="solid 2px #c0c0c0";
}

function tbmouseover()
{
  this.style.border="outset 2px";
}

/* ***End Button States *** */

function tbclick(){
  if ((this.id == "forecolor") || (this.id == "hilitecolor")) {
    parent.command = this.id;
    buttonElement = document.getElementById(this.id);
    document.getElementById("colorpalette").style.left = getOffsetLeft(buttonElement);
    document.getElementById("colorpalette").style.top = getOffsetTop(buttonElement) + buttonElement.offsetHeight;
    document.getElementById("colorpalette").style.visibility="visible";
  } else if (this.id == "createlink") {
    var szURL = prompt("Enter a URL:", "http://");
    if ((szURL != null) && (szURL != "")) {
      document.getElementById('rteIFrame').contentWindow.document.execCommand("CreateLink",false,szURL);
    }
  } else if (this.id == "createimage") {
    imagePath = prompt('Enter Image URL:', 'http://');
    if ((imagePath != null) && (imagePath != "")) {
      document.getElementById('rteIFrame').contentWindow.document.execCommand('InsertImage', false, imagePath);
    }
  } else if (this.id == "createtable") {
    e = document.getElementById("rteIFrame");
    rowstext = prompt("enter rows");
    colstext = prompt("enter cols");
    rows = parseInt(rowstext);
    cols = parseInt(colstext);
    if ((rows > 0) && (cols > 0)) {
      table = e.contentWindow.document.createElement("table");
      table.setAttribute("border", "1");
      table.setAttribute("cellpadding", "2");
      table.setAttribute("cellspacing", "2");
      tbody = e.contentWindow.document.createElement("tbody");
      for (var i=0; i < rows; i++) {
        tr =e.contentWindow.document.createElement("tr");
        for (var j=0; j < cols; j++) {
          td =e.contentWindow.document.createElement("td");
          br =e.contentWindow.document.createElement("br");
          td.appendChild(br);
          tr.appendChild(td);
        }
        tbody.appendChild(tr);
      }
      table.appendChild(tbody);      
      insertNodeAtSelection(e.contentWindow, table);
    }
  } else {
    document.getElementById('rteIFrame').contentWindow.document.execCommand(this.id, false, null);
  }
}


function doAction(action){
  if ((action == "forecolor") || (action == "hilitecolor")) {
    parent.command = action;
    buttonElement = document.getElementById(action);
    document.getElementById("colorpalette").style.left = getOffsetLeft(buttonElement);
    document.getElementById("colorpalette").style.top = getOffsetTop(buttonElement) + buttonElement.offsetHeight;
    document.getElementById("colorpalette").style.visibility="visible";
  } else if (action == "createlink") {
    var szURL = prompt("Enter a URL:", "http://");
    if ((szURL != null) && (szURL != "")) {
      document.getElementById('rteIFrame').contentWindow.document.execCommand("CreateLink",false,szURL);
    }
  } else if (action == "createimage") {
    imagePath = prompt('Enter Image URL:', 'http://');
    if ((imagePath != null) && (imagePath != "")) {
      document.getElementById('rteIFrame').contentWindow.document.execCommand('InsertImage', false, imagePath);
    }
  } else if (action == "createtable") {
    e = document.getElementById("rteIFrame");
    rowstext = prompt("enter rows");
    colstext = prompt("enter cols");
    rows = parseInt(rowstext);
    cols = parseInt(colstext);
    if ((rows > 0) && (cols > 0)) {
      table = e.contentWindow.document.createElement("table");
      table.setAttribute("border", "1");
      table.setAttribute("cellpadding", "2");
      table.setAttribute("cellspacing", "2");
      tbody = e.contentWindow.document.createElement("tbody");
      for (var i=0; i < rows; i++) {
        tr =e.contentWindow.document.createElement("tr");
        for (var j=0; j < cols; j++) {
          td =e.contentWindow.document.createElement("td");
          br =e.contentWindow.document.createElement("br");
          td.appendChild(br);
          tr.appendChild(td);
        }
        tbody.appendChild(tr);
      }
      table.appendChild(tbody);      
      insertNodeAtSelection(e.contentWindow, table);
    }
  } else {
    document.getElementById('rteIFrame').contentWindow.document.execCommand(action, false, null);
  }
}

function Select(selectname){
  var cursel = document.getElementById(selectname).selectedIndex;
  /* First one is always a label */
  if (cursel != 0) {
    var selected = document.getElementById(selectname).options[cursel].value;
    document.getElementById('rteIFrame').contentWindow.document.execCommand(selectname, false, selected);
    document.getElementById(selectname).selectedIndex = 0;
  }
  document.getElementById("rteIFrame").contentWindow.focus();
}

function viewSource(){
 var html = document.getElementById('rteIFrame').contentWindow.document.body.innerHTML;
 alert(html);
}

// **************** CODE CLEANING SECTION ********************** //
function handleCode(){
	// Get code from iframe, clean it and place it in the hidden field to be picked up by aspx
	var html = document.getElementById('rteIFrame').contentWindow.document.body.innerHTML;
	/*alert(html);
	cleanDirtyA = cleanDetail(html);
	html = cleanDirtyA[0];
	// while there's leftover dirty code - clean it and add clean code to html
	while(cleanDirtyA[1] != ""){
		cleanDirtyA = cleanDetail(cleanDirtyA[1]);
		html += cleanDirtyA[0];
	}
	alert(html);
	*/
	document.getElementById('hidIFrameCode').value = html;
}

function cleanDetail(detail){
	var startTagPos, endTagPos, cleanCode, tag, tail;
	var cleanDirtyA = new Array("", detail);
	
	startTagPos = detail.indexOf("<");
	cleanCode = detail.substring(0, startTagPos);
	endTagPos = detail.indexOf(">") + 1;
	tag = detail.substring(startTagPos, endTagPos);
	
	tag = tag.toLowerCase();
	tag = makeReplacements(tag);
	if(tagIsValid(tag))
		cleanCode += tag;
		
	tail = detail.substring(endTagPos, detail.length);
	
	cleanDirtyA[0] = cleanCode;
	cleanDirtyA[1] = tail;

	return cleanDirtyA;
}

function tagIsValid(tag){
	var validTagA = new Array("<p", "/p>", "<h2", "/h2>", "<strong", "/strong>", "<b", "/b>", "<em", "/em>", "<ol", "/ol>", "<li", "/li>", "<ul", "/ul>", "<blockquote", "/blockquote>", "<br>");
	var isValid = false;
	for(var i=0; i<validTagA.length; i++){
		if(tag.indexOf(validTagA[i]) >= 0)
			isValid = true
	}
	return isValid;
}

function makeReplacements(tag){
 tag.replace("<br>", "<br />")
 return tag;
}