// JavaScript Document
//Preload buttons that need it
fcUp = new Image(23,22)
fcUp.src = "HTA_FormatTextBox/images/forecolorUp.gif"
fcOver = new Image(23,22)
fcOver.src = "HTA_FormatTextBox/images/forecolorOver.gif"
bcUp = new Image(23,22)
bcUp.src = "HTA_FormatTextBox/images/bgcolorUp.gif"
bcOver = new Image(23,22)
bcOver.src = "HTA_FormatTextBox/images/bgcolorOver.gif"

function frameDoc (f) {
	if (f.contentWindow) {return f.contentWindow.document;}
	else if (f.contentDocument) {return f.contentDocument;}
	else if (f.id) {return eval(f.id + '.document');}
	else {return f.document;}
}
	
function HTA_FTB_CopyBoxData (input, theFrame, buttonsHTML, buttons) {
	var theFrameD = frameDoc(theFrame);
	makeTextView(theFrame,buttonsHTML,buttons);
	input.value=theFrameD.body.innerHTML;
}

function HTA_FTB_Init(theFrame,theStyleFile,theText,hideButtons,showButtons,bgcolorBtn,bgcolorPickBtn,forecolorBtn,forecolorPickBtn) {
	var theFrameD = frameDoc(theFrame);
	if (theText != '') {
		theFrameD.open();
		theFrameD.write("<html><link href='" + theStyleFile + "' rel='stylesheet' type='text/css'><style>body { background-color: #FFFFFF; background-image: none;}</style><body bgcolor='#FFFFFF'>" + theText + "</body></html>");
		theFrameD.close()
		}
    //make content editable for IE
	theFrameD.designMode = 'On';
	//make content editable for Mozilla
	if (typeof theFrameD.body == "object" && theFrameD.body != null) theFrameD.body.contentEditable = true;
	theFrame.viewMode = 0;
	makeTextView(theFrame,hideButtons,showButtons);
	UpPick(bgcolorPickBtn);
	UpPick(forecolorPickBtn);
	bgcolorBtn.style.backgroundColor = '#FFFFFF';
	forecolorBtn.style.backgroundColor = '#000000';
}
  
function Over(btn) {
	btn.style.backgroundColor = '#FFFFE2';
	btn.style.cursor = 'hand';	
}
  
function Up(btn) {
	btn.style.backgroundColor = '#EEEEEE';
}
  
function Down(btn) {
	btn.style.backgroundColor = '#FFFFE2';
}
  
function OverPick(btn) {
	btn.style.borderColor = '#000000';
	btn.style.cursor = 'hand'
;	
}
  
function UpPick(btn) {
	btn.style.borderColor = '#EEEEEE';
}
  
function DownPick(btn) {
	btn.style.borderColor = '#000000';
}
  
function openColorPickWindow (ret) {
	var theWindow = window.open('HTA_FormatTextBox/ColorPicker.htm?document.getElementById("' + ret + '").style.backgroundColor','ColorPicker','width=700,height=250');
	theWindow.focus();
}

function openSpCharsWindow () {
	var theWindow = window.open('HTA_FormatTextBox/SpChars.htm','SpecChars','height=350,width=350,resizable,scrollbars');
	theWindow.focus();
}

function openTableWindow () {
	var theWindow = window.open('HTA_FormatTextBox/MakeTable.htm','MakeTable','height=350,width=500,resizable,scrollbars');
	theWindow.focus();
}

function openHelpWindow () {
	var theWindow = window.open('HTA_FormatTextBox/Help.htm','MakeTable','height=350,width=500,resizable,scrollbars');
	theWindow.focus();
}

function openFileWindow () {
	var theWindow = window.open('UploadMisc.cfm','NewFile','height=300,width=530,resizable,scrollbars');
	theWindow.focus();
	}

function openPhotoWindow () {
	var theWindow = window.open('HTA_FormatTextBox/LoadPhoto.htm','Photo','height=550,width=750,resizable,scrollbars');
	theWindow.focus();
}

function deWord(theFrame) {
	var theFrameD = frameDoc(theFrame);
	var sText = theFrameD.body.innerHTML;
	var startLength = sText.length;
	var changeLength = 0;
	var len = -1;
	sText = sText.replace(/\u201C/gi,"&#147;");
	sText = sText.replace(/\u201D/gi,"&#148;");
	sText = sText.replace(/\u2014/gi,"--");
	sText = sText.replace(/\u2013/gi,"-");
	sText = sText.replace(/\u2018/gi,"&#145;");
	sText = sText.replace(/\u2019/gi,"&#146;");
	sText = sText.replace(/\u2026/gi,"...");
	sText = sText.replace(/\xa9/gi,"&copy;");
	sText = sText.replace(/\xae/gi,"&reg;");
	sText = sText.replace(/class=MsoNormal/gi,"");
	sText = sText.replace(/class=MsoTableGrid/gi,"");
	sText = sText.replace(/MARGIN:[^;"]*(;|")/gi,"$1")
	sText = sText.replace(/<o:p>/gi,"");
	sText = sText.replace(/<\/o:p>/gi,"");
	sText = sText.replace(/<font[^>]*>/gi,"");
	sText = sText.replace(/<\/font>/gi,"");
	sText = sText.replace(/<st1:[^>]*>/gi,"");
	sText = sText.replace(/<\/st1:[^>]*>/gi,"");
	sText = sText.replace(/<\?xml:namespace[^>]*>/gi,"");
	sText = sText.replace(/mso-[^:]*:[^;"]*(;|")/gi,"$1");
	sText = sText.replace(/font-family: 'times new roman'[^;"]*(;|")/gi,"$1");
	sText = sText.replace(/tab-stops:[^;"]*(;|")/gi,"$1");
	sText = sText.replace(/font-size:[^;"]*(;|")/gi,"$1");
	sText = sText.replace(/border-right:[^;"]*(;|")/gi,"$1");
	sText = sText.replace(/border-top:[^;"]*(;|")/gi,"$1");
	sText = sText.replace(/border-left:[^;"]*(;|")/gi,"$1");
	sText = sText.replace(/border-bottom:[^;"]*(;|")/gi,"$1");
	sText = sText.replace(/border-collapse:[^;"]*(;|")/gi,"$1");
	sText = sText.replace(/padding-right:[^;"]*(;|")/gi,"$1");
	sText = sText.replace(/padding-left:[^;"]*(;|")/gi,"$1");
	sText = sText.replace(/padding-bottom:[^;"]*(;|")/gi,"$1");
	sText = sText.replace(/padding-top:[^;"]*(;|")/gi,"$1");
	sText = sText.replace(/background-color: transparent[^;"]*(;|")/gi,"$1");
	sText = sText.replace(/line-height:[^;"]*(;|")/gi,"$1");
	sText = sText.replace(/style="[; ]*"/gi,"");
	//sText = sText.replace(/style=".*"/gi,"");
	sText = sText.replace(/ *>/gi,">");
	while (len != sText.length) {
		len = sText.length;
		sText = sText.replace(/<span>([^<]*)<\/span>/gi,"$1");
	}
	sText = sText.replace(/<P>&nbsp;<\/P>/gi,"");
	changeLength = startLength - sText.length;
	alert("Removed " + changeLength + " characters of hidden formatting info and replaced incompatible characters." );
	theFrameD.body.innerHTML = sText;
}

function Sub(theFrame) {
	var theFrameD = frameDoc(theFrame);
	var sText = theFrameD.selection.createRange();
	if (!sText=="") {
		if (sText.parentElement().tagName == "SUB") {
			sText.parentElement().outerHTML = sText.parentElement().innerHTML;
		}
		else {
			sText.pasteHTML("<sub>" + sText.text + "</sub>");
		}
	}
}

function Sup(theFrame) {
	var theFrameD = frameDoc(theFrame);
	var sText = theFrameD.selection.createRange();
	if (!sText=="") {
		if (sText.parentElement().tagName == "SUP") {
			sText.parentElement().outerHTML = sText.parentElement().innerHTML;
		}
		else {
			sText.pasteHTML("<sup>" + sText.text + "</sup>");
		}
	}
}

function getRangeObject (sel) {
		if (sel.getRangeAt)
			return sel.getRangeAt(0);
		else { //Safari
			var range = document.createRange();
			range.setStart(sel.anchorNode,sel.anchorOffset);
			range.setEnd(sel.focusNode,sel.focusOffset);
			return range;
		}
}


function ImageAlign(theFrame,a) {
	var theFrameD = frameDoc(theFrame);
	var theFrameW = "";
	if (theFrame.contentWindow) theFrameW = theFrame.contentWindow;
	else theFrameW = theFrame;
	if (theFrameW.getSelection) {
		var sText = getRangeObject(theFrameW.getSelection());
		if (!sText.collapsed) {
			if (sText.startContainer.hasChildNodes()) {
				var elm = sText.startContainer.childNodes[sText.startOffset];
				if (elm.nodeName == "IMG") {
					elm.align = a;
					elm.vspace = 5;
					elm.hspace = 5;
					}
				}
		}
	}
	else if (document.getSelection) {
		alert("Image alignment may not work in this browser");
		var sel = theFrameD.getSelection();
		var sText = sel.focusNode;
		if (sel.type == "Text") {
			sText.parentNode.align = a;
			sText.parentNode.vspace = 5;
			sText.parentNode.hspace = 5;
		}
		else if (sel.type == "Control") {
			alert("Sorry, it appears that you clicked an image rather than dragging across it.\nTo select an image for alignment you must drag your mouse across it.");
		}
	}
	else if (document.selection)
	{
		var sel = theFrameD.selection;
		var sText = sel.createRange();
		if (sel.type == "Text") {
			sText.parentElement().align = a;
			sText.parentElement().vspace = 5;
			sText.parentElement().hspace = 5;
		}
		else if (sel.type == "Control") {
			alert("Sorry, it appears that you clicked an image rather than dragging across it.\nTo select an image for alignment you must drag your mouse across it.");
		}
	} }

function Bold(theFrame) {
	var theFrameD = frameDoc(theFrame);
	theFrameD.execCommand('bold', false, null);
}

function Italic(theFrame) {
	var theFrameD = frameDoc(theFrame);
	theFrameD.execCommand('italic', false, null);
}

function Underline(theFrame) {
	var theFrameD = frameDoc(theFrame);
	theFrameD.execCommand('underline', false, null);
}
  
function Left(theFrame) {
	var theFrameD = frameDoc(theFrame);
    theFrameD.execCommand('justifyleft', false, null);
}

function Center(theFrame) {
	var theFrameD = frameDoc(theFrame);
    theFrameD.execCommand('justifycenter', false, null);
}

function Right(theFrame) {
	var theFrameD = frameDoc(theFrame);
    theFrameD.execCommand('justifyright', false, null);
}

function OrdList(theFrame) {
	var theFrameD = frameDoc(theFrame);
    theFrameD.execCommand('insertorderedlist', false, null);
}

function Indent(theFrame) {
	var theFrameD = frameDoc(theFrame);
    theFrameD.execCommand('indent', false, null);
}

function Outdent(theFrame) {
	var theFrameD = frameDoc(theFrame);
    theFrameD.execCommand('outdent', false, null);
}

function BulList(theFrame) {
	var theFrameD = frameDoc(theFrame);
    theFrameD.execCommand('insertunorderedlist', false, null);
}
  
function ForeColor(theFrame,thecolor) {
	var theFrameD = frameDoc(theFrame);
    if(thecolor != null)
      	theFrameD.execCommand('forecolor', false, thecolor)
}

function BackColor(theFrame,thecolor) {
	var theFrameD = frameDoc(theFrame);
    if(thecolor != null)
	    if (navigator.product == "Gecko") {
      		theFrameD.execCommand('hilitecolor', false, thecolor);
		}
		else {
      		theFrameD.execCommand('backcolor', false, thecolor);
		}
}


//These global variables are used asynchronously with the Link popup window
var FTBLink = "";
var FTBTarget = "";
var FTBATag = "";

function Link(theFrame) {
	var theURL = "";
	var theFrameD = frameDoc(theFrame);
	var sText = "";
	if (window.getSelection) {
		//Firefox, NN
		var a = theFrame.contentWindow.window.getSelection().focusNode;
		if (a.nodeName == "A") theURL = a.href;
		else if (a.parentNode.nodeName == "A") {theURL = a.parentNode.href;}
		else {
			if (a.nodeType == a.ELEMENT_NODE) var anchors = a.getElementsByTagName("A");
			else var anchors = a.parentNode.getElementsByTagName("A");
			if (anchors.length > 0) theURL = anchors[0].href;
			else theURL = "http://";
			}
		theURL = prompt("URL",theURL);
		if (theURL == "" || theURL == "http://") {
			alert("In order to remove a link with this browser, delete the link and retype the text.");
		}
		else {
			theFrameD.execCommand('createlink',false,theURL);
		}
	}
	else if (document.selection)
	{
		//IE
		document.execCommand('createlink');
	}
}
 
function completeLink (theURL,theTarget) {
	if ((theURL == "" || theURL == "http://") && FTBATag.tagName=="A") {
		var pn = FTBATag.parentNode;
		pn.replaceChild(FTBATag.firstChild,FTBATag);
		pn.normalize();
	}
	else if (FTBATag.tagName=="A") {
		FTBATag.href=theURL;
		FTBATag.target=theTarget;
		FTBATag.parentNode.normalize();
	}
}

function Rule(theFrame) {
	var theFrameD = frameDoc(theFrame);
    theFrameD.execCommand('inserthorizontalrule', false, null);
}
  
function pickFont(theFrame,fName) {
	var theFrameD = frameDoc(theFrame);
    if(fName != '')
      theFrameD.execCommand('fontname', false, fName);
}
  
function pickSize(theFrame,fSize) {
	var theFrameD = frameDoc(theFrame);
    if(fSize != '')
      theFrameD.execCommand('fontsize', false, fSize);
}
  
function Head(theFrame,hType) {
	var theFrameD = frameDoc(theFrame);
    if(hType != '') {
      theFrameD.execCommand('formatblock', false, hType); 
	  }
}
  
function makeHTMLView(theFrame,hideButtons,showButtons) {  
	var theFrameD = frameDoc(theFrame);
	if(theFrame.viewMode == 1) {
	    //if currently in Text mode, change to HTML
		if (navigator.product == "Gecko") {
			var html = document.createTextNode(theFrameD.body.innerHTML);
			theFrameD.body.innerHTML = "";
			theFrameD.body.appendChild(html);
			}
		else {
		  //in text mode but not Gecko (that is, is IE)
		  iHTML = theFrameD.body.innerHTML;
		  theFrameD.body.innerText = iHTML.toString();
		  }
		}
	  
	  showButtons.style.display = 'inline';
	  hideButtons.style.display = 'none';
	  if (theFrameD.defaultView) theFrameD.defaultView.focus();
	  else theFrameD.focus();
	 
	  theFrame.viewMode = 2; // HTML
}

function makeTextView(theFrame,hideButtons,showButtons) {  
	var theFrameD = frameDoc(theFrame);
	if(theFrame.viewMode == 2) {
		if (navigator.product == "Gecko") {
			var html = theFrameD.createRange();
			html.selectNodeContents(theFrameD.body);
			theFrameD.body.innerHTML = html.toString();
		}
		else {
		  iText = theFrameD.body.innerText;
		  theFrameD.body.innerHTML = iText.toString();
		  }
		}
	if(theFrame.viewMode != 1) {
	  showButtons.style.display = 'inline';
	  hideButtons.style.display = 'none';
	  if (theFrameD.defaultView) theFrameD.defaultView.focus();
	  else theFrameD.focus();
	 
	  theFrame.viewMode = 1; // Text
	  }
}


