window.onload=doInit
//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
function doInit(){
 	for (i=0; i<document.all.length; i++){ 
		if( document.all(i).className.indexOf('buttons') >= 0 )document.all(i).unselectable = "on"; 
	}
		//ensure that all document elements except the content editable DIV are unselectable
    	if(document.getElementById('oDiv'))document.getElementById('oDiv').unselectable = "off";
    	if(document.getElementById('oDiv'))document.getElementById('oDiv').innerHTML.unselectable = "off";
    	if(document.all.imageFrame)document.all.imageFrame.unselectable = "off";
    	if(document.all.archives)document.all.archives.unselectable = "off";
    	if(document.all.pageInfoFr)document.all.pageInfoFr.unselectable = "on";
    	//document.all.subTitle.unselectable = "off";
		//if(document.getElementById('oDiv'))oDiv.focus();
		if(document.getElementById('oDiv'))oDiv.designMode = "On";
		document.execCommand("LiveResize");
		document.execCommand("2D-Position");
}

//This function works for all of the command identifiers used in this page
function callFormatting(sFormatString,interf,what){
	//alert(sFormatString+' '+interf+' '+what);
	if(sFormatString=='save'){
		saveDocument();
	} else {
		whichText = document.selection.createRange().text;
		//if (whichText=="") return;
		//document.selection.createRange().text = "<b>" + whichText + "</b>";
		document.execCommand(sFormatString,interf,what);
	}
}

//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(){

	//alert("whoot!:");
	var a=dlgHelper.blockFormats.count;
	alert( a);
	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);
}

//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(){
	var sSelected=oToolBar.getItem(5).getOptions().item(oToolBar.getItem(5).getAttribute("selectedIndex"));
	document.execCommand("FormatBlock", false, sSelected.text);
} 


//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;
	oDiv.focus();
}

//VerticalMode changes the orientation of the text from left to right to top to bottom
 function VerticalMode(){
 	if (oDiv.style.writingMode == 'tb-rl')
    	oDiv.style.writingMode = 'lr-tb';
  	else
    	oDiv.style.writingMode = 'tb-rl';
}

//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
function saveDocument(which){
	if(which===0 || which===1){
		document.forms[0].showEdit.value = which;
	}
	document.forms[0].doSave.value = "true";
	document.forms[0].editContent.value = document.getElementById('oDiv').innerHTML;
	//alert(document.forms[0].editContent.value);
	document.forms[0].submit();

  }

function testContent(){
	alert(document.getElementById('oDiv').innerHTML);
}

function deleteDocument(namn){
input_box=confirm("Delete \""+namn+"\"?\nClick \"OK\" to delete this entry\nor \"Cancel\" to do nothing.");
if (input_box==true){ 
	document.forms[0].updatePage.value = "delete";
	document.forms[0].submit();
  }
}

//LoadDocument uses the common dialog box object to display the open dialog box, then reads the file and displays its contents in the div
function LoadDocument(){
//Setting CancelError to true and using try/catch allows the user to click cancel on the save as dialog without causing a script error
   	cDialog.CancelError=true;
   	try{
   		var answer = checkForSave();
   		//The user has clicked yes in the modal dialog box called in the checkForSave function
   		if (answer) {var sCancel = saveDocument();
   			//The user has clicked cancel in the save as dialog box; exit function
   			if (sCancel) return; 
   			cDialog.Filter="HTM Files (*.htm)|*.htm|Text Files (*.txt)|*.txt"
	    	cDialog.ShowOpen();
   			var ForReading = 1;
   			var fso = new ActiveXObject("Scripting.FileSystemObject");
   			var f = fso.OpenTextFile(cDialog.filename, ForReading);
   			var r = f.ReadAll();
   			f.close();
   			document.getElementById('oDiv').innerHTML=r;
   			//This variable is used in the checkForSave function to see if there is new content in the div 
   			sPersistValue=document.getElementById('oDiv').innerHTML;
			
   		}
   		//The user has clicked no in the modal dialog box called in the checkForSave function
  		if (answer==false)
   			{cDialog.Filter="HTM Files (*.htm)|*.htm|Text Files (*.txt)|*.txt"
  			cDialog.ShowOpen();
   			var ForReading = 1;
  			var fso = new ActiveXObject("Scripting.FileSystemObject");
   			var f = fso.OpenTextFile(cDialog.filename, ForReading);
  			var r = f.ReadAll();
  			f.close();
  			document.getElementById('oDiv').innerHTML=r;
  			sPersistValue=document.getElementById('oDiv').innerHTML;
			}
  		document.getElementById('oDiv').focus();
		}
   	catch(e){
   		var sCancel="true";
   		return sCancel;}
} 

//NewDocument creates a new "document" by clearing the content of the div. If there is any new content in the div, the user is asked whether or not to save
function NewDocument(){
  	var answer = checkForSave();
    if (answer==false){
		document.forms[0].title.value = "";
        document.getElementById('oDiv').innerHTML="";}
}

//This function checks to see if the div has new text, then displays a modal dialog box if appropriate
function checkForSave()
{ 
	if ((document.getElementById('oDiv').innerHTML != "")||(document.forms[0].title.value != ""))
	 	//var checkSave=false;//showModalDialog('dcheckForSave.htm','','dialogHeight:125px;dialogWidth:250px;scroll:off');
		var checkSave = !window.confirm("Are you sure you want to create a new page?\nThe existing page contents will not be saved.")
	else
	  	var checkSave=false;
  	return checkSave;
}

//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;
			}
}

function setParent(num,num2){
  if(num2){
    document.forms[0].doSort.value = '';
    makeMenuGrey();
  }else{
    document.forms[0].doSort.value = num;
    makeMenuYellow();
	parentTimer(10);
	setTimeout('parentTimer(8)',2000);
	setTimeout('parentTimer(6)',4000);
	setTimeout('parentTimer(4)',6000);
	setTimeout('parentTimer(2)',8000);
	setTimeout('setParent("",1);timedOut()',10000);
  }
}

function makeMenuYellow(){
 	for (i=0; i<document.all.length; i++){ 
		if((document.all(i).className.indexOf('sideMenu') >= 0)&&(document.all(i).className.indexOf('selectedMenu') < 0)){
			document.all(i).style.backgroundColor = "#E5DD99"; 
		}
	}
}

function makeMenuGrey(){
 	for (i=0; i<document.all.length; i++){ 
		if((document.all(i).className.indexOf('sideMenu') >= 0)&&(document.all(i).className.indexOf('selectedMenu') < 0)){
			document.all(i).style.backgroundColor = "#BFC9CC"; 
		}
	}
}

function parentTimer(num){
if(tt=document.getElementById('parentCounter'))tt.innerHTML = num + " sec. till menu timeout...";
}
function timedOut(){
document.forms[0].doSort.value = '';
if(tt=document.getElementById('parentCounter'))tt.innerHTML = "Menu timed out...";
setTimeout('document.getElementById("parentCounter").innerHTML=""',1000);
nope();
}

function orderModule(down, col) 
{
  sl = document.forms[0][col].selectedIndex;
  if (sl != -1 && document.forms[0][col].options[sl].value > "") {
    oText = document.forms[0][col].options[sl].text;
    oValue = document.forms[0][col].options[sl].value;
    if (document.forms[0][col].options[sl].value > "" && sl > 0 && down == 0) {
      document.forms[0][col].options[sl].text = document.forms[0][col].options[sl-1].text;
      document.forms[0][col].options[sl].value = document.forms[0][col].options[sl-1].value;
      document.forms[0][col].options[sl-1].text = oText;
      document.forms[0][col].options[sl-1].value = oValue;
      document.forms[0][col].selectedIndex--;
    } else if (sl < document.forms[0][col].length-1 && document.forms[0][col].options[sl+1].value > "" && down == 1) {
      document.forms[0][col].options[sl].text = document.forms[0][col].options[sl+1].text;
      document.forms[0][col].options[sl].value = document.forms[0][col].options[sl+1].value;
      document.forms[0][col].options[sl+1].text = oText;
      document.forms[0][col].options[sl+1].value = oValue;
      document.forms[0][col].selectedIndex++;
    }
	updateOrder();
  } else {
    alert("Please select a module first");
  }
}

function updateOrder(){
	tempValue = "";
	for (m=0;m<numOpts;m++) {
		tempValue += document.forms[0].tempKids.options[m].value;
		if(m<numOpts-1)tempValue += "|";
		tempKidsArr[m] = document.forms[0].tempKids.options[m].value;
	}
	document.forms[0].theseChildren.value = tempValue;
}

//this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.offsetTop
//this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.offsetTop

//this.offsetTop-this.parentNode.parentNode.scrollTop
//this.offsetLeft+this.offsetWidth

	thatMenu = null;
	thatNum = null;
	thatPar = null;
	thatLock = null;
function doMenuPopper(that,num,par,lock) {
	if(noTimer)clearTimeout(noTimer); 
	noTimer = setTimeout('nope()',6000);
	makeMenuGrey();
	thatMenu = that;
	thatNum = num;
	thatPar = par;
	thatLock = lock;
	that.style.backgroundColor = '#f3f1df';
	popTop = that.offsetTop - that.parentNode.parentNode.scrollTop + 67;
	popLeft = that.offsetLeft + that.offsetWidth;
	document.getElementById('menuFloater').style.top = popTop;
	document.getElementById('menuFloater').style.left = popLeft;
}

function nope(){
	makeMenuGrey();
	document.getElementById('menuFloater').style.left=-600;
	thatMenu = null;
	thatNum = null;
	thatPar = null;
	thatLock = null;
	document.forms[0].mirror.value='';
}

function doMenuParent(){
	if(noTimer)clearTimeout(noTimer);
	document.forms[0].oldParent.value = thatPar;
	setParent(thatNum,0);
	thatMenu.style.backgroundColor = '#BFC9CC';
}

function makeNewChild(){
	document.forms[0].parent.value = thatNum;
	document.forms[0].newPage.value = "1";
	document.forms[0].submit();
}

function showAllText(){
	textWin = document.getElementById('oDiv');
	//document.console.document.getElementById('testText').innerHTML = textWin.innerHTML;
	document.console.document.forms[0].consoleText.value = textWin.innerHTML;
	document.console.document.forms[0].submit();
	//document.forms[0].console2.value = textWin.innerHTML;
	//alert(textWin.innerHTML);
}

function outlines() {
  if (document.forms[0].OutlineDisable.value == "Disable Outline") {
    document.getElementById('oDiv').DisableOutline = true;
    document.forms[0].OutlineDisable.value = "Enable Outline";
  } else {
    document.getElementById('oDiv').DisableOutline = false;
    document.forms[0].OutlineDisable.value = "Disable Outline";
  }
}


function toggleEdit(){
	document.console.document.forms[0].toggleEdit.value = textWin.innerHTML;
	document.console.document.forms[0].submit();
}