var oEditing = null;
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

//window.onload = getBlockFormats

function dockToolbar() {

      oToolBar.style.removeExpression("height");

      oToolBar.style.height = "";

      oToolBar.style.setExpression("left", "scrollLeft-scrollLeft+document.body.scrollLeft", "JavaScript");

      oToolBar.style.setExpression("top", "scrollTop-scrollTop+document.body.scrollTop", "JavaScript");

      oToolBar.style.setExpression("width", "document.body.clientWidth");

}
var editdoc = 1;

function showModal(){
showModalDialog("/interact/includes/insert_image.html", editdoc, "resizable: no; help: no; status: no; scroll: no; status: No;")
}

function loadHistory(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
function loadProducts(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
function submitForm(formID){
//alert(formID);
thisFormHTML = document.getElementById('htmlCopy'+formID);
//alert(thisFormHTML);
thisFormHTML.value=document.getElementById('copy'+formID).innerHTML;
document.getElementById('f'+formID).submit();
}
function submitProductForm(formID){
document.getElementById('f'+formID).submit();
}
function pageProperties(sectionID)
{
location.replace("/interact/edit/editSection.php?sectionID="+sectionID);
}
function cancel(cancelUrl) {
alert(cancelUrl);
}
var editing = "0";
function setEdit(ctrlnum){
if ( editing=="0" ){
editmenu=document.getElementById('editmenu_'+ctrlnum);
editoptions=document.getElementById('editoptions_'+ctrlnum);
editmenu.style.display = "none";
editoptions.style.display = "inline";
editing="1";
ctrl = document.getElementById('copy'+ctrlnum);
oEditing = ctrl;
//alert(ctrl);
ctrl.style.padding="2px";
ctrl.contentEditable="true";
ctrl.style.border="1px";
ctrl.style.borderStyle="dotted";
ctrl.style.borderColor="red";
}else{
alert("you are currently editing another area");
}
}
function changeProduct(ctrlnum){
if ( editing=="0" ){
editmenu=document.getElementById('editmenu_'+ctrlnum);
editoptions=document.getElementById('editoptions_'+ctrlnum);
editmenu.style.display = "none";
editoptions.style.display = "inline";
editing="1";
}else{
alert("you are currently editing another area");
}
}

//sInitColor is a global variable. It holds the value of the selected color in the color dialog box when it displays
var sInitColor = null;
//sPersistValue holds the value of the saved innerHTML 
var sPersistValue


//This function works for all of the command identifiers used in this page
function callFormatting(sFormatString){
//if (editing=="1"){
      var curRange = document.selection.createRange();

      var pElement;

      if (curRange.length) {

            pElement = curRange[0]; 

      }

      else {

            pElement = curRange.parentElement();

      }

      while (pElement && (pElement.tagName != "DIV")) {

            pElement = pElement.parentElement;

      }

      if (pElement.isContentEditable) {

            curRange.execCommand(sFormatString, true);

      }

	//document.execCommand(sFormatString);
	//}
}

//Fonts routines
//getSystemFonts uses the dialog helper object to return an array of all of the fonts on the user's system, then populates a drop-down listbox in the toolbar with the array elements
function getSystemFonts(){
	var a=dlgHelper.fonts.count;
	var fArray = new Array();
	var oDropDown = oToolBar.createDropDownListAt("4");
	oDropDown.setAttribute("id","FontNameList");
	for (i = 1;i < dlgHelper.fonts.count;i++){ 
		fArray[i] = dlgHelper.fonts(i);
		var aOptions = oDropDown.getOptions();	
		var oOption = document.createElement("OPTION");
		aOptions.add(oOption);	
		oOption.text = fArray[i];
		oOption.Value = i;
	} 
	//attaching the onchange event is necessary in order to detect when a user changes the value in the drop-down listbox
		oDropDown.setAttribute("onchange",ChangeFont);
}

//changeFontSize detects the value of the item in the drop-down listbox and applies the value to the font of the selected text
function changeFontSize(){
	var sSelected=oToolBar.getItem(6).getOptions().item(oToolBar.getItem(6).getAttribute("selectedIndex"));
   	document.execCommand("FontSize", false, sSelected.value);
}

//changeFont detects the value of the item in the drop-down listbox and applies the value to the font of the selected text
function ChangeFont(){	
	var sSelected=oToolBar.getItem(4).getOptions().item(oToolBar.getItem(4).getAttribute("selectedIndex"));
	document.execCommand("FontName", false, sSelected.text);
}

//BlockFormats routines
//getBlockFormats uses the dialog helper object to return an array of all of the block formats on the user's system, then populates a drop-down listbox in the toolbar with the array elements
function getBlockFormats(){
	var a=dlgHelper.blockFormats.count;
	var fArray = new Array();
	var oDropDown = oToolBar.createDropDownListAt("5");
	oDropDown.setAttribute("id","FormatList");
	for (i = 1;i < dlgHelper.blockFormats.count;i++)
	{ 
		fArray[i] = dlgHelper.blockFormats(i);
		var aOptions = oDropDown.getOptions();	
		var oOption = document.createElement("OPTION");
		aOptions.add(oOption);	
		oOption.text = fArray[i];
		oOption.Value = i;
	} 
	//attach the onchange event
	oDropDown.setAttribute("onchange",ChangeFormat);
}
function SaveDocument(){
document.htmlControls.submit();
}

//ChangeFormat detects the value of the item in the drop-down listbox and applies the value to the font of the selected text
function ChangeFormat(sSelected){
	//alert(sSelected);
	var curRange = document.selection.createRange();
	//var sSelected=oToolBar.getItem(5).getOptions().item(oToolBar.getItem(5).getAttribute("selectedIndex"));
	document.execCommand("FormatBlock", false, sSelected);
} 


function ChangeStyle(sSelected){
	//alert(sSelected);
	var oSelection = document.selection.createRange();
	//var sSelected=oToolBar.getItem(5).getOptions().item(oToolBar.getItem(5).getAttribute("selectedIndex"));
	//document.execCommand("Div", false, sSelected);
	if (sSelected == ""){
	oSelection.execCommand('RemoveFormat');
	} else {
	oSelection.execCommand('RemoveFormat');
	        oSelection.execCommand('FontName',false,'TemporaryPlaceholder1234509876');
	        var oChildren = document.all.tags("FONT");
	        for (i=0; i<oChildren.length; i++) {
		        if (oChildren[i].face == 'TemporaryPlaceholder1234509876') {
			        oChildren[i].face = "";
			        oChildren[i].className = sSelected;
			        oChildren[i].outerHTML = oChildren[i].outerHTML.replace("face=", "");
			        oChildren[i].outerHTML = oChildren[i].outerHTML.replace("??", "");
		        }
				}
				}
} 


//callColorDlg uses the dialog helper object's ChooseColorDlg method to open the color dialog box, then changes the font or back color of the selected text
function callColorDlg(sColorType){

if (sInitColor == null) 
	//display color dialog box
	var sColor = dlgHelper.ChooseColorDlg();
else
	var sColor = dlgHelper.ChooseColorDlg(sInitColor);
	//change decimal to hex
	sColor = sColor.toString(16);
	//add extra zeroes if hex number is less than 6 digits
if (sColor.length < 6) {
  	var sTempString = "000000".substring(0,6-sColor.length);
  	sColor = sTempString.concat(sColor);
}
	//change color of the selected text
	document.execCommand(sColorType, false, sColor);
	sInitColor = sColor;
}

//VerticalMode changes the orientation of the text from left to right to top to bottom


//SaveDocument uses the common dialog box object to display the save as dialog, then writes a textstream object from the value of the div's innerHTML property


//LoadDocument uses the common dialog box object to display the open dialog box, then reads the file and displays its contents in the div



//this function is used to call other functions when the user clicks on a menu item. These are the same functions that are called by the toolbar buttons.
function CallMenuFunction(){
var menuChoice = event.result;
switch(menuChoice){
     	case "open":	
			LoadDocument();
			break;
		case "new":
            NewDocument();
		    break;
		case "save":
			SaveDocument();
			break;
		case "exit":
			 window.close();
			break;
		case "cut":
			callFormatting('Cut');
			break;
        case "copy":
			callFormatting('Copy');
		    break;

        case "paste":
			callFormatting('Paste');
            break;
		case "bold":
			callFormatting('Bold');
            break;
		case "underline":
			callFormatting('Underline');
            break;
		case "italic":
			callFormatting('Italic');
            break;
		case "fontColor":
			callColorDlg('ForeColor');
            break;
		case "highlight":
			callColorDlg('BackColor');
            break;
		 case "about":
            goContext(); 
            break;
        default:
			break;
			}
}

//These functions create and display the splash screen and are used when the application is launched (called by onInit function) as well as when the user clicks help/about on the menu
var oPopup = window.createPopup()
function goContext()
{
  var oPopupBody = oPopup.document.body;

  oPopupBody.innerHTML = oContext.innerHTML;
  oPopup.show(175, 125, 400, 300, document.body);
  document.body.onmousedown = oPopup.hide;
}
window.onload=
function (){
//ensure that all document elements except the content editable DIV are unselectable

//getSystemFonts();
getBlockFormats();
//getStyles();
dockToolbar();
}
function callFormatting(sFormatString){
document.execCommand(sFormatString);
}
function changeFontSize(){
var sSelected=oToolBar.getItem(6).getOptions().item(oToolBar.getItem(6).getAttribute("selectedIndex"));
document.execCommand("FontSize", false, sSelected.value);
}
 
function getSystemFonts(){
	var a=dlgHelper.fonts.count;
	var fArray = new Array();
	var oDropDown = oToolBar.createDropDownListAt("4");
	oDropDown.setAttribute("id","FontNameList");
	for (i = 1;i < dlgHelper.fonts.count;i++){ 
		fArray[i] = dlgHelper.fonts(i);
		var aOptions = oDropDown.getOptions();	
		var oOption = document.createElement("OPTION");
		aOptions.add(oOption);	
		oOption.text = fArray[i];
		oOption.Value = i;
	}
	//attaching the onchange event is necessary in order to detect when a user changes the value in the drop-down listbox
		oDropDown.setAttribute("onchange",ChangeFont);
}
function ChangeFont(){	
var sSelected=oToolBar.getItem(4).getOptions().item(oToolBar.getItem(4).getAttribute("selectedIndex"));
document.execCommand("FontName", false, sSelected.text);
}
function getBlockFormats(){
	var a=dlgHelper.blockFormats.count;
	var fArray = new Array();
	var oDropDown = oToolBar.createDropDownListAt("5");
	oDropDown.setAttribute("id","FormatList");
	for (i = 1;i < dlgHelper.blockFormats.count;i++)
	{ 
		fArray[i] = dlgHelper.blockFormats(i);
		var aOptions = oDropDown.getOptions();	
		var oOption = document.createElement("OPTION");
		aOptions.add(oOption);	
		oOption.text = fArray[i];
		oOption.Value = i;
	} 
	//attach the onchange event
	oDropDown.setAttribute("onchange",ChangeFormat);
}
	
var sInitColor=null;
function callColorDlg(sColorType){
if (sInitColor == null) 
	//display color dialog box
	var sColor = dlgHelper.ChooseColorDlg();
else
	var sColor = dlgHelper.ChooseColorDlg(sInitColor);
	//change decimal to hex
	sColor = sColor.toString(16);
	//add extra zeroes if hex number is less than 6 digits
if (sColor.length < 6) {
  	var sTempString = "000000".substring(0,6-sColor.length);
  	sColor = sTempString.concat(sColor);
}
	//change color of the selected text
	document.execCommand(sColorType, false, sColor);
	sInitColor = sColor;

}
function getStyles(){
	var oDropDown = oToolBar.createDropDownListAt("6");
	oDropDown.setAttribute("id","StylesList");
		var aOptions = oDropDown.getOptions();	
		var oOption = document.createElement("OPTION");
		aOptions.add(oOption);	
		oOption.text = "Header";
		oOption.Value = "Header";
		var aOptions = oDropDown.getOptions();	
		var oOption = document.createElement("OPTION");
		aOptions.add(oOption);	
		oOption.text = "subHeader";
		oOption.Value = "subHeader";
	//attach the onchange event
	oDropDown.setAttribute("onchange",CssSelect);
}

function CssSelect(strValue) {
	var oSelection = document.selection.createRange();
	var oTemp = oSelection.parentElement();
	while (oTemp != null && oTemp != oEditing)
		oTemp = oTemp.parentElement;
	if (oTemp != null) {
	    addUndoAction("CSS FORMAT", oEditing.innerHTML);
	    if (oSelection.text == "" && oSelection.parentElement() != null) {
	        oSelection.parentElement().className = strValue;
	    }
        else if (oPos != null && (oSelection.text == "" || oPos.tagName == "A"))
            oPos.className = strValue;
        else {
	        oSelection.execCommand('RemoveFormat');
	        oSelection.execCommand('FontName',0,'TemporaryPlaceholder1234509876');
	        var oChildren = document.all.tags("FONT");
	        for (i=0; i<oChildren.length; i++) {
		        if (oChildren[i].face == 'TemporaryPlaceholder1234509876') {
			        oChildren[i].face = "";
			        oChildren[i].className = strValue;
			        oChildren[i].outerHTML = oChildren[i].outerHTML.replace("face=", "");
			        oChildren[i].outerHTML = oChildren[i].outerHTML.replace("??", "");
		        }
	        }
	    }
        oSelection.select();
	    //HideHtmlMenus();
	}
}
