if( document.all ) {
 var sPersistValue;
 var sInitColor=null;
 var oPopup = window.createPopup();
}

function doInit(){
// Ensure that all document elements except the content editable DIV are unselectable.
for (i=0; i<document.all.length; i++)
 document.all(i).unselectable = "off";
oDiv.unselectable = "off";
oDiv.style.lineBreak = 'strict';
// Clear any text in the Document window and set the focus.

//getSystemFonts();
//getBlockFormats();
}

function goContext()
{var oPopupBody = oPopup.document.body;

oPopupBody.innerHTML = oContext.innerHTML;
oPopup.show(175,125, 400, 300, document.body);
document.body.onmousedown = oPopup.hide;
}

function callFormatting(sFormatString)
{
document.execCommand(sFormatString);
}

function changeFontSize()
{
var sSelected=oToolBar.getItem(0).getOptions().item(oToolBar.getItem(0).getAttribute("selectedIndex"));
document.execCommand("FontSize", false, sSelected.value);
}

 function VerticalMode(){
if (oDiv.style.writingMode == 'tb-rl')
   	oDiv.style.writingMode = 'lr-tb';
else
   	oDiv.style.writingMode = 'tb-rl';
}

function getSystemFonts(){
    var a=dlgHelper.fonts.count;
    var fArray = new Array();
    var oDropDown = oToolBar.createDropDownListAt("1");
    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 list box.
        oDropDown.setAttribute("onchange",ChangeFont);
}

function ChangeFont(){	
var sSelected=oToolBar.getItem(1).getOptions().item(oToolBar.getItem(1).getAttribute("selectedIndex"));
document.execCommand("FontName", false, sSelected.text);
}


function getBlockFormats(){
	var a=dlgHelper.blockFormats.count;
	var fArray = new Array();
	var oDropDown = oToolBar.createDropDownListAt("2");
	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 ChangeFormat(){
var sSelected=oToolBar.getItem(2).getOptions().item(oToolBar.getItem(2).getAttribute("selectedIndex"));
document.execCommand("FormatBlock", false, sSelected.text);
}

 function VerticalMode(){
if (oDiv.style.writingMode == 'tb-rl')
   	oDiv.style.writingMode = 'lr-tb';
else
   	oDiv.style.writingMode = 'tb-rl';
}

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();
}

function checkForSave()
{if ((oDiv.innerHTML!=sPersistValue)&&(oDiv.innerHTML !=""))
  var checkSave=showModalDialog('dcheckForSave.htm','','dialogHeight:125px;dialogWidth:250px;scroll:off');
else
  var checkSave=false;
return checkSave
}

function NewDocument(){
  	var answer = checkForSave();
   	if (answer) {var sCancel = SaveDocument();
	 	if (sCancel) return;
		oDiv.innerHTML="";}
    if (answer==false)
        oDiv.innerHTML="";
	oDiv.focus();
}


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();
			oDiv.innerHTML=r;
			// This variable is used in the checkForSave function to see
			// if there is new content in the div. 
			sPersistValue=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();
			oDiv.innerHTML=r;
			sPersistValue=oDiv.innerHTML;
}
			oDiv.focus();
		}
		catch(e){
		var sCancel="true";
		return sCancel;}
}
function SaveDocument(){
// 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{
  		cDialog.Filter="HTM Files (*.htm)|*.htm|Text Files (*.txt)|*.txt"
  		cDialog.ShowSave();
  		var fso = new ActiveXObject("Scripting.FileSystemObject");
  		var f = fso.CreateTextFile(cDialog.filename,  true);
  		f.write(oDiv.innerHTML);
  		f.Close();
  		sPersistValue=oDiv.innerHTML;}
  	catch(e){
  		var sCancel="true";
  		return sCancel;}
	oDiv.focus();	
  }
  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;
 case "Image":
	insertpic();
 break;
 case "Link":
	insertlink();
 break;
 case "Line":
	insertline();
 break;
 case "Table":
	 inserttable();
break;
 default:
  break;
			}
}
  
 
function inserttable()
{
	//oDiv.innerHTML = oDiv.innerHTML+"<img src='fgfgfg' alt=&quot;test&quot;>";
	//var info
	var info=new Array(5);
	var c,i,j,w,h;
	info[0]=prompt("Enter No. of Rows","0");
	info[1]=prompt("Enter No. of Columns","0");
	info[2]=prompt("Enter Border Size", "1");
	info[3]=prompt("Enter Width either in percent or pixels (if by percent then have a % symbol appended to the number entered)", "100%");
	info[4]=prompt("Enter Height either in percent or pixels (if by percent then have a % symbol appended to the number entered)","100%");	
	if (info[0]!="0" && info[1]!="0") 
	{
	c = "<table width=" + info[3] + " height=" + info[4] + " border=" + info[2] + ">";
	for (i=1;i<=Number(info[0]) ; i++)
	{
		c = c + "<tr>";
		for(j=1;j<=Number(info[1]);j++)
		{
			c = c + "<td></td>";
		}
		c = c + "</tr>";
	}
	c = c + "</table>";	
	}
	oDiv.innerHTML = oDiv.innerHTML + c;
}
function insertline()
{
	var info = new Array(1);
	info[0]=prompt("Enter the Color of the line","");
	oDiv.innerHTML = oDiv.innerHTML + "<hr color=" + info[0] + ">";
}
function insertlink()
{
	var info = new Array(3);
	info[0]=prompt("Enter the URL of the Link","");
	info[1]=prompt("Enter the name of the link","");
	info[2]=prompt("Enter the target of the link. Like _blank to open in a new page","");
	oDiv.innerHTML = oDiv.innerHTML + "<a href='" + info[0] + "' style='color:blue;' target='" + info[2] + "'>" + info[1] + "</a>";
}
function insertpic()
{
	var info=new Array(8);
	info[0]=prompt("Enter the image name with extension","");
	info[1]=prompt("Enter the images alternate text","");
	info[2]=prompt("Enter hspace value","0");
	info[3]=prompt("Enter vspace value","0");
	info[4]=prompt("Enter border value","0");
	info[5]=prompt("Enter alignment","left");
	info[6]=prompt("Enter width","");
	info[7]=prompt("Enter Height","");
	oDiv.innerHTML = oDiv.innerHTML + "<img src='images/" + info[0] + "' alt='" + info[1] + "' hspace=" + info[2] + " vspace=" + info[3] + " border=" + info[4] + " align=" + info[5] + " width=" + info[6] + " height=" + info[7] + ">";
}

// spell checker code
var timerID = null;
function getFlag() 
{
	if(document.spellcheck.flag)
	{
	    clearInterval(timerID);
		if (document.spellcheck.fixedtext!="")
		{
		    oDiv.innerHTML=document.spellcheck.fixedtext;
	    }
		else
		{
		    oDiv.innerHTML=document.spellcheck.text;
	    }
	}
}

function SpellCheck() 
{
	document.spellcheck.ptexttocheck=oDiv.innerHTML;
	document.spellcheck.purltouse="http://mirrordns.com/spellchecker/SC_process.asp";
	document.spellcheck.flag = false;
	document.spellcheck.debug=true;
	document.spellcheck.doDlg();
	oDiv.innerHTML="Processing ........";
	getFlag();
	timerID = setInterval("getFlag()", 2000);
}
