/*************
Roboshrub Inc.
Rich Text
*************/

/*
0	Light Color
1	Dark Color
2	Header Color
3	Border Color
4	Light Text Color
5	Dark Text Color
6	Header Text Color
7	Name of scheme
8	Associated background
*/
var tableColors=[
  ['#E9967A','#F08080','#CD5C5C','#DC143C','#000','#000','#FFF','Red 1',3],
  ['#FF0000','#DC143C','#8B0000','#CD5C5C','#FFF','#FFF','#FFF','Red 2',0],
  ['#FFA500','#FF6347','#FF4500','#FF8C00','#000','#000','#FFF','Orange',10],
  ['#FFFF00','#FFD700','#DAA520','#F0E68C','#000','#000','#000','Yellow 1',16],
  ['#FAFAD2','#F0E68C','#FFD700','#DAA520','#000','#000','#000','Yellow 2',18],
  ['#EEE8AA','#DAA520','#B8860B','#FFD700','#000','#000','#FFF','Yellow 3',-1],
  ['#6B8E23','#808000','#556B2F','#D2B48C','#FFF','#FFF','#FFF','Olive 1',-1],
  ['#9ACD32','#6B8E23','#808000','#8FBC8F','#000','#000','#FFF','Olive 2',-1],
  ['#90EE90','#008000','#006400','#000000','#000','#FFF','#FFF','Green 1',26],
  ['#90EE90','#32CD32','#008000','#228B22','#000','#000','#FFF','Green 2',-1],
  ['#00FF00','#32CD32','#228B22','#008000','#000','#000','#FFF','Green 3',-1],
  ['#3CB371','#2E8B57','#006400','#2F4F4F','#FFF','#FFF','#FFF','Green 4',-1],
  ['#228B22','#006400','#2F4F4F','#708090','#000','#FFF','#FFF','Green 5',25],
  ['#66CDAA','#20B2AA','#008080','#008B8B','#000','#000','#FFF','Teal',-1],
  ['#AFEEEE','#00CED1','#5F9EA0','#A9A9A9','#000','#000','#FFF','Turquoise',-1],
  ['#00FFFF','#00CED1','#40E0D0','#C0C0C0','#000','#000','#000','Aqua',-1],
  ['#E0FFFF','#00FFFF','#6495ED','#B0C4DE','#000','#000','#FFF','Cyan',-1],
  ['#B0E0E6','#B0C4DE','#4682B4','#D3D3D3','#000','#000','#FFF','Blue 1',34],
  ['#ADD8E6','#00BFFF','#1E90FF','#6495ED','#000','#000','#FFF','Blue 2',32],
  ['#ADD8E6','#6495ED','#0000FF','#4682B4','#000','#000','#FFF','Blue 3',36],
  ['#4169E1','#0000FF','#191970','#4682B4','#FFF','#FFF','#FFF','Blue 4',37],
  ['#DDA0DD','#DA70D6','#FF00FF','#EE82EE','#000','#000','#FFF','Fuchsia',-1],
  ['#FFB6C1','#FF69B4','#FF1493','#DB7093','#000','#000','#FFF','Pink 1',-1],
  ['#FF69B4','#FF1493','#C71585','#FF69B4','#000','#FFF','#FFF','Pink 2',-1],
  ['#BA55D3','#800080','#4B0082','#9932CC','#FFF','#FFF','#FFF','Purple 1',40],
  ['#9370DB','#8A2BE2','#483D8B','#6A5ACD','#000','#FFF','#FFF','Purple 2',-1],
  ['#E6E6FA','#9370DB','#9400D3','#9932CC','#000','#FFF','#FFF','Purple 3',-1],
  ['#FFEBCD','#D2B48C','#CD853F','#F5DEB3','#000','#000','#000','Brown 1',50],
  ['#FFF8DC','#DEB887','#BC8F8F','#FFEBCD','#000','#000','#FFF','Brown 2',-1],
  ['#CD853F','#A0522D','#8B4513','#F4A460','#000','#FFF','#FFF','Brown 3',-1],
  ['#D2691E','#A52A2A','#800000','#B8860B','#000','#FFF','#FFF','Brown 4',-1],
  ['#F4A460','#A0522D','#8B4513','#D2691E','#000','#FFF','#FFF','Brown 5',-1],
  ['#FFFFFF','#FFE4E1','#FFB6C1','#FFF5EE','#000','#000','#000','White 1',-1],
  ['#FFFFFF','#FAEBD7','#FFDEAD','#FFE4C4','#000','#000','#000','White 2',-1],
  ['#FFFFFF','#FFFACD','#FFE4B5','#FAFAD2','#000','#000','#000','White 3',-1],
  ['#FFFFFF','#E0FFE0','#B8FBB8','#F0FFF0','#000','#000','#000','White 4',-1],
  ['#FFFFFF','#E0E8FF','#D0D8EF','#D0D8EF','#000','#000','#000','White 5',-1],
  ['#FFFFFF','#CCCCCC','#0000AA','#BBBBBB','#000','#000','#FFF','White 6',45],
  ['#FFFFFF','#CCCCCC','#AAAAAA','#BBBBBB','#000','#000','#000','White 7',46],
  ['#D3D3D3','#696969','#2F4F4F','#696969','#000','#FFF','#FFF','Gray 1',-1],
  ['#808080','#2F4F4F','#222222','#696969','#FFF','#FFF','#FFF','Gray 2',57],
  ['#666666','#333333','#000000','#000000','#FFF','#FFF','#FFF','Black',58]
];

// Toggles the inset/opaque state of the format buttons.
function buttonState() {
  var c_frame = g('contentFrame').contentDocument;
  var buttons = [['boldB', 'bold'], ['italicB', 'italic'], ['underB', 'underline'], ['strikeB', 'strikethrough'], ['superB', 'superscript'], ['subB', 'subscript'], ['aLB', 'justifyleft'], ['aCB', 'justifycenter'], ['aRB', 'justifyright'], ['aJB', 'justifyfull']];
  
  // Active buttons should be opaque and inset.
  for (i=0; i<buttons.length; i++) {
    g(buttons[i][0]).style.borderStyle = (c_frame.queryCommandState(buttons[i][1]) && HTMLToggle) ? 'inset' : 'outset';
    g(buttons[i][0]).style.opacity = (c_frame.queryCommandState(buttons[i][1]) && HTMLToggle) ? 1 : 0.75;
  }
  
  // Inactive undo/redo buttons should be more faded than the others.
  g('undoB').style.opacity = c_frame.queryCommandEnabled('undo') ? 0.75 : 0.3;
  g('redoB').style.opacity = c_frame.queryCommandEnabled('redo') ? 0.75 : 0.3;
  
  var isFound = false;
  
  // Font name
  var curFont = c_frame.queryCommandValue('fontname');
  for (i=0; i<g('fontNames').options.length; i++) {
    if (g('fontNames').options[i].text == curFont) {
      g('fontNames').selectedIndex = i;
      isFound = true;
    }
  }
  if (!isFound || !HTMLToggle) {g('fontNames').selectedIndex = 0;}
  
  // Font size
  isFound = false;
  var curSize = c_frame.queryCommandValue('fontsize');
  for (i=0; i<g('fontSize').options.length; i++) {
    if (g('fontSize').options[i].value == curSize) {
      g('fontSize').selectedIndex = i;
      isFound = true;
    }
  }
  if (!isFound || !HTMLToggle) {g('fontSize').selectedIndex = 0;}
}

function toggleFormat(a, b) {
  g('contentFrame').contentWindow.document.execCommand(a, false, b);
  buttonState();
}

function insertTagAtSelection(textID, openTag, closeTag, deleteSelection) {
  var posStart=g(textID).selectionStart;
  var posEnd=g(textID).selectionEnd;
  g(textID).value = g(textID).value.substring(0, posStart) + openTag + ((deleteSelection) ? '' : g(textID).value.substring(posStart, posEnd)) + closeTag + g(textID).value.substring(posEnd);
}

function insertTheHTML(a, deleteSelection) {
  if (!HTMLToggle) {
    insertTagAtSelection('textWindow', a, '', deleteSelection);
    closeAllWindows();
    return;
  }
  try {
    toggleFormat("inserthtml", a);
  }
  catch (e) {
    g('contentFrame').contentWindow.document.body.innerHTML+=a;
  }
  closeAllWindows();
}

/** Rich Text Formatting **/
function RTbold() {
  (HTMLToggle) ? toggleFormat('bold') : insertTagAtSelection('textWindow', '<strong>', '</strong>');
}
function RTitalic() {
  (HTMLToggle) ? toggleFormat('italic') : insertTagAtSelection('textWindow', '<em>', '</em>');
}
function RTunderline() {
  (HTMLToggle) ? toggleFormat('underline') : insertTagAtSelection('textWindow', '<span style="text-decoration:underline;">', '</span>');
}
function RTstrike() {
  (HTMLToggle) ? toggleFormat('strikethrough') : insertTagAtSelection('textWindow', '<span style="text-decoration:line-through;">', '</span>');
}
function RTsub() {
  (HTMLToggle) ? toggleFormat('subscript') : insertTagAtSelection('textWindow', '<sub>', '</sub>');
}
function RTsup() {
  (HTMLToggle) ? toggleFormat('superscript') : insertTagAtSelection('textWindow', '<sup>', '</sup>');
}
function RTal() {
  (HTMLToggle) ? toggleFormat('justifyleft') : insertTagAtSelection('textWindow', '<p style="text-align:left;">', '</p>');
}
function RTac() {
  (HTMLToggle) ? toggleFormat('justifycenter') : insertTagAtSelection('textWindow', '<p style="text-align:center;">', '</p>');
}
function RTar() {
  (HTMLToggle) ? toggleFormat('justifyright') : insertTagAtSelection('textWindow', '<p style="text-align:right;">', '</p>');
}
function RTaj() {
  (HTMLToggle) ? toggleFormat('justifyfull') : insertTagAtSelection('textWindow', '<p style="text-align:justify;">', '</p>');
}
function RTul() {
  (HTMLToggle) ? toggleFormat('insertunorderedlist') : insertTagAtSelection('textWindow', '<ul>\n  <li>', '</li>\n  <li></li>\n  <li></li>\n</ul>\n');
}
function RTol() {
  (HTMLToggle) ? toggleFormat('insertorderedlist') : insertTagAtSelection('textWindow', '<ol>\n  <li>', '</li>\n  <li></li>\n  <li></li>\n</ol>\n');
}
function RTformat() {
  var forms=['p','h1','h2','h3','h4','h5','h6','address','pre'];
  var tempIndex=g('fontFormat').selectedIndex-1;
  insertTagAtSelection('textWindow', "<"+forms[tempIndex]+">", "</"+forms[tempIndex]+">");
}
function RTfont() {
  insertTagAtSelection('textWindow', "<span style=\"font-family:'"+g('fontNames').options[g('fontNames').selectedIndex].innerHTML+"';\">", "</span>");
  g('fontNames').selectedIndex=0;
}
function RTsize() {
  var fSizes=['8pt','10pt','12pt','14pt','18pt','24pt','36pt'];
  var tempIndex=g('fontSize').selectedIndex-1;
  insertTagAtSelection('textWindow', "<span style=\"font-size:"+fSizes[tempIndex]+";\">", "</span>");
  g('fontSize').selectedIndex=0;
}
function RTchangeFormat() {
  if (g('fontFormat').selectedIndex>0 && HTMLToggle) {
    toggleFormat('formatblock',g('fontFormat').options[g('fontFormat').selectedIndex].value);
  }
  else if (g('fontFormat').selectedIndex>0) {
    RTformat();
  }
  g('fontFormat').selectedIndex=0;
}
function RTchangeName() {
  if (g('fontNames').selectedIndex>0 && HTMLToggle) {
    toggleFormat('fontname',g('fontNames').options[g('fontNames').selectedIndex].text);
  }
  else if (g('fontNames').selectedIndex>0) {
    RTfont();
  }
}
function RTchangeSize() {
  if (g('fontSize').selectedIndex>0 && HTMLToggle) {
    toggleFormat('fontsize',g('fontSize').options[g('fontSize').selectedIndex].value);
  }
  else if (g('fontSize').selectedIndex>0) {
    RTsize();
  }
}

function insertTextAtSelection(textID, text) {
  var posStart=g(textID).selectionStart;
  var posEnd=g(textID).selectionEnd;
  g(textID).value = g(textID).value.substring(0, posStart) + text + g(textID).value.substring(posEnd);
}

// Toggle Search Bar
function toggleSearch() {
  g('searchBar').style.display=(g('searchBar').style.display=='block') ? 'none' : 'block';
  if(g('searchBar').style.display=='block') {
    g('sText').style.borderStyle="inset";
    g('sText').style.opacity=1;
    g('sSearchText').focus();
  }
  else {
    g('sText').style.borderStyle="outset";
    g('sText').style.opacity=.75;
  }
}

// Text Replace
function replaceText(textID, oldText, newText) {
  var posStart=g(textID).selectionStart;
  var posEnd=g(textID).selectionEnd;
  var oldText=oldText||"";
  var newText=newText||"";
  if (oldText=='') {return;} // Inserts newText between each letter! Horrible!
  
  // Replace within selection?
  var finalString=(g('rSelOnly').checked) ? g(textID).value.substring(posStart, posEnd) : g(textID).value;
  
  // Replace first only + case sensitive
  if (g('rCaseSensitive').checked) {
    finalString=(g('rFirstOnly').checked) ? finalString.replace(new RegExp(oldText), newText) : finalString.replace(new RegExp(oldText, "g"), newText);
  }
  else {
    finalString=(g('rFirstOnly').checked) ? finalString.replace(new RegExp(oldText, "i"), newText) : finalString.replace(new RegExp(oldText, "gi"), newText);
  }
  
  // Set replaced text
  g(textID).value=(g('rSelOnly').checked) ? g(textID).value.substring(0,posStart)+finalString+g(textID).value.substring(posEnd) : finalString;
}

// Find Next
function sFindNext(textID, findText) {
  // Makes search case insensitive.
  var tempValue=g(textID).value.toLowerCase();
  findText=findText.toLowerCase();
  
  var findIndex=tempValue.indexOf(findText, g(textID).selectionEnd); // Find Next
  // Resets to 0
  if (findIndex<0) {
    g(textID).selectionEnd=0;
    g(textID).selectionStart=0;
    findIndex=tempValue.indexOf(findText);
  }
  if (findIndex<0) {return;} // Not found at all
  
  // Repositions selection
  g(textID).selectionEnd=findIndex+findText.length;
  g(textID).selectionStart=findIndex;
  g(textID).focus();
}

// Find as you type search
function updateSearchBox(e) {
  if (e.keyCode==13 || e.keyCode==10) {
    sFindNext('textWindow', g('sSearchText').value);
  }
  // Escape key
  if (e.keyCode==27) {
    toggleSearch();
  }
  var temp=g('textWindow').value.toLowerCase().indexOf(g('sSearchText').value.toLowerCase());
  
  // Not found
  if (temp<0) {
    g('sSearchText').style.backgroundColor="#f66";
  }
  else {
    g('sSearchText').style.backgroundColor='';
	g('textWindow').selectionEnd=temp+g('sSearchText').value.length;
	g('textWindow').selectionStart=temp;
  }
}

function setColor() {
  var temp=(currentColorType=="RGB") ? [document.getElementById('col1').value, document.getElementById('col2').value, document.getElementById('col3').value] : hsl2rgb(document.getElementById('col1').value, document.getElementById('col2').value, document.getElementById('col3').value);
  var r=temp[0];
  var g=temp[1];
  var b=temp[2];
  var forecolor="#000";
  if (validColor(r) && validColor(g) && validColor(b)) {
    forecolor="#"+RGB2HEX(r)+RGB2HEX(g)+RGB2HEX(b);
  }
  (HTMLToggle) ? document.getElementById('contentFrame').contentWindow.document.execCommand("forecolor", false, forecolor) : insertTagAtSelection('textWindow', '<span style="color:'+forecolor+';">', '</span>');
  closeAllWindows();
}

function setHighlight() {
  var temp=(currentColorType=="RGB") ? [document.getElementById('high1').value, document.getElementById('high2').value, document.getElementById('high3').value] : hsl2rgb(document.getElementById('high1').value, document.getElementById('high2').value, document.getElementById('high3').value);
  var r=temp[0];
  var g=temp[1];
  var b=temp[2];
  var highcolor="#f80";
  if (validColor(r) && validColor(g) && validColor(b)) {
    highcolor="#"+RGB2HEX(r)+RGB2HEX(g)+RGB2HEX(b);
  }
  (HTMLToggle) ? document.getElementById('contentFrame').contentWindow.document.execCommand("hilitecolor", false, highcolor) : insertTagAtSelection('textWindow', '<span style="background-color:'+highcolor+';">', '</span>');
  closeAllWindows();
}

// Get Color buttons
function getColor(a, b) {
  var temp=g('contentFrame').contentWindow.document.queryCommandValue(a);
  temp=temp.substring(4,temp.length-1);
  temp=temp.split(",");
  if (temp[2]==undefined) {return;} // Weeds out values like "transparent"
  g(b+'1').value=(currentColorType=="RGB") ? temp[0] : rgb2hsl(temp[0], temp[1], temp[2])[0];
  g(b+'2').value=(currentColorType=="RGB") ? temp[1] : rgb2hsl(temp[0], temp[1], temp[2])[1];
  g(b+'3').value=(currentColorType=="RGB") ? temp[2] : rgb2hsl(temp[0], temp[1], temp[2])[2];
  if (currentColorType.match("HSL")!=null) {
    updateHue(b);
  }
  if (currentColorType.match("RGB")!=null) {
    updateRed(b);
    updateGreen(b);
    updateBlue(b);
  }
  updateColorBox(b);
}

// Makes sure URLs start with http://
function getValidLink(linkURL) {
  return (linkURL.indexOf(':')==-1) ? 'http://'+linkURL : linkURL;
}

function createLink() {
  var temp;
  switch (getCheckedValue('linkType')) {
    // URL
    case 0: temp=getValidLink(g('linkURL').value); break;
	
	// E-Mail
	case 1: temp="mailto:"+g('linkEMAIL').value+getEmailExt(); break;
	
	// Go to slide #
	case 2: if (activeFileType==0 || activeFileType==1) {temp="javascript:slideJump("+g("linkJump").value+");";} else if (activeFileType==2 || activeFileType==3) {temp="#slide"+g("linkJump").value;} break;
	
	// Previous slide
	case 3: if (activeFileType==0 || activeFileType==1) {temp="javascript:slideJump(curSlide);";} else if (activeFileType==2 || activeFileType==3) {temp="javascript:go(-1)";} break;
	
	// Next slide
	case 4: if (activeFileType==0 || activeFileType==1) {temp="javascript:slideJump(curSlide+2);";} else if (activeFileType==2 || activeFileType==3) {temp="javascript:go(1)";} break;
	default: temp="#";
  }
  (HTMLToggle) ? g('contentFrame').contentWindow.document.execCommand("createlink", false, temp) : insertTagAtSelection('textWindow', "<a href=\""+temp+"\">","</a>");
  closeAllWindows();
}

function getEmailExt() {
  var temp=[];
  if (g("linkSu").value!='') {
    temp.push("subject="+g("linkSu").value);
  }
  if (g("linkCC").value!='') {
    temp.push("cc="+g("linkCC").value);
  }
  if (g("linkBC").value!='') {
    temp.push("bcc="+g("linkBC").value);
  }
  return (temp.length>0) ? "?"+temp.join("&") : "";
}

function removeLink() {
  g('contentFrame').contentWindow.document.execCommand("unlink", false, null);
  closeAllWindows();
}

function createRule() {
  var temp=(currentColorType=="RGB") ? [document.getElementById('rule1').value, document.getElementById('rule2').value, document.getElementById('rule3').value] : hsl2rgb(document.getElementById('rule1').value, document.getElementById('rule2').value, document.getElementById('rule3').value);
  var r=temp[0];
  var g=temp[1];
  var b=temp[2];

  var finalString="<hr style=\"border:0;border-top:";
  
  // Border Color
  if (validColor(r) && validColor(g) && validColor(b)) {
    finalString+="#"+RGB2HEX(r)+RGB2HEX(g)+RGB2HEX(b);
  }
  
  // Border Size
  if (document.getElementById('ruleSize').value>0 && !isNaN(document.getElementById('ruleSize').value)) {
    finalString+=" "+document.getElementById('ruleSize').value+document.getElementById('ruleThicknessUnits').options[document.getElementById('ruleThicknessUnits').selectedIndex].text;
  }
  
  // Border Style
  finalString+=" "+document.getElementById('ruleBorder').options[document.getElementById('ruleBorder').selectedIndex].value+";";
  
  // Width
  if (document.getElementById('ruleWidth').value>0 && !isNaN(document.getElementById('ruleWidth').value)) {
    finalString+="width:"+document.getElementById('ruleWidth').value+document.getElementById('ruleWidthUnits').options[document.getElementById('ruleWidthUnits').selectedIndex].text+";";
  }
  
  finalString+="\" />";
  insertTheHTML(finalString, true);
}

function insertDateTime() {
  var date=getDateTime(parseInt(data.get("Velt_2_skinGlobal").split("|N|")[3]));
  insertTheHTML(date, true);
}

function createImage() {
  var CI_URL=g('imageURL').value;	// URL
  var CI_ALT=g('imageALT').value;	// Alt text
  var CI_W=g('imageWidth').value;	// Width
  var CI_H=g('imageHeight').value;	// Height
  var CI_WU=g('imageWidthUnits').options[g('imageWidthUnits').selectedIndex].text;	// Width Units
  var CI_HU=g('imageHeightUnits').options[g('imageHeightUnits').selectedIndex].text;	// Height Units
  
  var finalString="";
  switch (g('mediaType').selectedIndex) {
    case 0: finalString+="<iframe"; break;
    case 1: finalString+="<img"; break;
    case 2: finalString+="<audio"; break;
    case 3: finalString+="<video"; break;
  }
  finalString+=" src=\"";
  finalString+=((CI_URL.indexOf(':')==-1)?'http://'+CI_URL:CI_URL)+"\"";
  
  // Custom dimensions
  if (g('imageCustomDimensions').checked) {
    finalString+=" style=\"width:"+CI_W+CI_WU+";height:"+CI_H+CI_HU+";";
    // Custom dimensions + float
    if (g('imageFloat').selectedIndex>0) {
      finalString+="float:"+g('imageFloat').options[g('imageFloat').selectedIndex].text.toLowerCase()+";margin:auto .5em;";
    }
    finalString+="\"";
  }
  
  // No custom dimensions, yes float.
  if (!g('imageCustomDimensions').checked && g('imageFloat').selectedIndex>0) {
    finalString+=" style=\"float:"+g('imageFloat').options[g('imageFloat').selectedIndex].text.toLowerCase()+";margin:auto .5em;\"";
  }
  
  // Audio Options
  if (g('mediaType').selectedIndex==2) {
    finalString+=(g('aud1').checked)?' controls=\"controls\"':'';
    finalString+=(g('aud2').checked)?' autoplay=\"autoplay\"':'';
    finalString+=" loop=\""+g('aud3').value+"\"";
  }
  
  // Video Options
  if (g('mediaType').selectedIndex==3) {
    finalString+=(g('vid1').checked)?' controls=\"controls\"':'';
    finalString+=(g('vid2').checked)?' autoplay=\"autoplay\"':'';
    finalString+=" loop=\""+g('vid3').value+"\"";
	finalString+=" poster=\""+g('vid4').value+"\"";
  }
  
  // Alt text
  switch (g('mediaType').selectedIndex) {
    case 0: finalString+=">"+CI_ALT+ "</iframe>"; break;
    case 1: finalString+=" alt=\""+CI_ALT+"\" />"; break;
    case 2: finalString+=">"+CI_ALT+"</audio>"; break;
    case 3: finalString+=">"+CI_ALT+"</video>"; break;
  }
  
  insertTheHTML(finalString, true);
}

function createTable() {
  var currentColor=g('tableColorThemes').selectedIndex;
  
  // CSS border color, width, style.
  var borderCSS="border:"+tableColors[currentColor][3];
      borderCSS+=" "+parseInt(g('tableBorderWidth').value)+g('tableBorderUnits').options[g('tableBorderUnits').selectedIndex].text;
      borderCSS+=" "+g('tableBorderStyles').options[g('tableBorderStyles').selectedIndex].text.toLowerCase()+";";

  // Main variables.
  var rows=parseInt(g('tableRows').value);
  var cols=parseInt(g('tableCols').value);
  var finalString="\n<table style=\"border-collapse:collapse;text-align:center;background-color:"+tableColors[currentColor][0]+";color:"+tableColors[currentColor][4]+";"+borderCSS;
  // Float table
  if (g('tableFloat').selectedIndex>0) {
    finalString+="float:"+g('tableFloat').options[g('tableFloat').selectedIndex].text+";margin:auto .5em";
  }
  finalString+="\">";
  
  
  for (i=0; i<rows; i++) {
    finalString+="\n  <tr>";
    for (k=0; k<cols; k++) {
      // Is this cell a header?
      var headerChecks=(i==0 && g('headT').checked) || (k==0 && g('headL').checked) || (k==cols-1 && g('headR').checked) || (i==rows-1 && g('headB').checked);
      
      // Intersection check
      var tripwire=false;
      
      // Top right intersection
      if (g('headR').checked && g('headT').checked && i==0 && k==cols-1) {
        tripwire=true;
      }
      // Top left intersection
      if (g('headL').checked && g('headT').checked && i==0 && k==0) {
        tripwire=true;
      }
      // Bottom left intersection
      if (g('headL').checked && g('headB').checked && i==rows-1 && k==0) {
        tripwire=true;
      }
      // Bottom right intersection
      if (g('headR').checked && g('headB').checked && i==rows-1 && k==cols-1) {
        tripwire=true;
      }
      
      // Start cell
      finalString+="\n    <td";
      
      // New header cell
      if (headerChecks) {
        finalString+=" style=\"font-weight:bold;background-color:"+tableColors[currentColor][2]+";color:"+tableColors[currentColor][6]+";";
        
        // Only headers get borders
        if (!document.getElementsByName('tableBorderType')[0].checked && !tripwire) {
          finalString+=borderCSS;
        }
		
		finalString+='"';
      }
      // Regular table cell
      else {
	    // Style attribute needed
	    if (document.getElementsByName('tableBorderType')[1].checked || ((i%2==0 && g('bandR').checked) || (k%2==0 && g('bandC').checked)) && !((i%2==0 && g('bandR').checked) && (k%2==0 && g('bandC').checked))) {
		  finalString+=' style="';
		}
		
        // All cells get borders
        if (document.getElementsByName('tableBorderType')[1].checked) {
          finalString+=borderCSS;
        }
        
        // Banded rows/columns. If both bands are selected, give a chessboard-like pattern.
        if (((i%2==0 && g('bandR').checked) || (k%2==0 && g('bandC').checked)) && !((i%2==0 && g('bandR').checked) && (k%2==0 && g('bandC').checked))) {
          finalString+="background-color:"+tableColors[currentColor][1]+";color:"+tableColors[currentColor][5]+";"; // Dark Band
        }
		
		// Close style attribute
	    if (document.getElementsByName('tableBorderType')[1].checked || ((i%2==0 && g('bandR').checked) || (k%2==0 && g('bandC').checked)) && !((i%2==0 && g('bandR').checked) && (k%2==0 && g('bandC').checked))) {
		  finalString+='"';
		}
      }
      finalString+=">";
      
      // Cell text
      finalString+=(tripwire)?"<br />":((headerChecks)?"Header":"Text");
      
      
      // End of cell
      finalString+="</td>";
    }
    finalString+="\n  </tr>";
  }
  finalString+="\n</table>\n";
  insertTheHTML(finalString, true);
}

function charMap(a) {
  var range=a.split("-"); // Dash delimits digits.
  
  // No character range selected.
  if (range.length<2) {
    g('specialCharTest').innerHTML="";
    return;
  }
  
  var c1=parseInt(range[0]);
  var c2=parseInt(range[1]);
  var temp="<table id='charTable'><tr>";
  var row=-1;
  for (i=c1; i<=c2; i++) {
    row++;
    if (row>4) {
      temp+="</tr><tr>";
      row=0;
    }
    temp+="<td onclick='g(\"charsToCopy\").value+=this.innerHTML;'>&#"+i+";</td>";
  }
  temp+="</tr></table>";
  g('specialCharTest').innerHTML=temp;
}

function insertCharacters() {
  insertTheHTML(g('charsToCopy').value, true);
}

// Quote Curling
function replaceQuotes() {
  var temp=(HTMLToggle) ? g('contentFrame').contentWindow.document.body.innerHTML : g('textWindow').value;
  
  // Reset quotes
  temp=temp.replace(/(\&(r|l)dquo;)|“|”/g, "\"");
  temp=temp.replace(/(\&(r|l)squo;)|’|‘/g, "'");
  temp=temp.replace(/--/g, "&mdash;");
  
  // Undoes comment conversion
  temp=temp.replace(/<!&mdash;/g, "<!--");
  temp=temp.replace(/&mdash;>/g, "-->");
  
  temp=temp.replace(/\.\.\./g, "&hellip;");
  temp=temp.replace(/\(C\)/g, "&copy;");
  temp=temp.replace(/\(R\)/g, "&reg;");

  var REP=temp.split('');
  var finalString="";
  var openQuote=false, tagline=false, singleHTML=false;
  for (i=0; i<REP.length; i++) {
    // Checks to see if quote is HTML attribute.
    if (REP[i]=="\"" && REP[i-1]!="=") {
      // Closes already-opened quote.
      if (openQuote) {
        finalString+="&rdquo;";
        openQuote=false; continue;
      }
      // Opens a new quote.
      else if (!tagline) {
        finalString+="&ldquo;";
        openQuote=true; continue;
      }
      // Adds straight quote that closes HTML attribute.
      else {
        finalString+=REP[i];
        tagline=false; continue;
      }
    }
    // Does not curl HTML attribute opening quote.
    else if (REP[i]=="\"" && !tagline) {
      finalString+=REP[i];
      tagline=true; continue;
    }
    // Ignores single quotes inside HTML attributes.
    else if (REP[i]=="'" && !tagline) {
      if (singleHTML) {
        singleHTML=false;
        finalString+=REP[i]; continue;
      }
      else if (REP[i-1]=="=") {
        singleHTML=true;
        finalString+=REP[i]; continue;
      }
      else if (REP[i-1]==" ") {
        finalString+="&lsquo;";
        continue;
      }
      else {
        finalString+="&rsquo;";
        continue;
      }
    }
    finalString+=REP[i];
  }
  g('contentFrame').contentWindow.document.body.innerHTML=finalString;
  g('textWindow').value=finalString;
}