var viewMode = 1;

  function Init()
  {
    iView.document.designMode = 'On';
  }
  
  function selOn(ctrl)
  {
	ctrl.style.borderColor = '#000000';
	ctrl.style.backgroundColor = '#B5BED6';
	ctrl.style.cursor = 'hand';	
  }
  
  function selOff(ctrl)
  {
	ctrl.style.borderColor = '#D6D3CE';  
	ctrl.style.backgroundColor = '#D6D3CE';
  }
  
  function selDown(ctrl)
  {
	ctrl.style.backgroundColor = '#8492B5';
  }
  
  function selUp(ctrl)
  {
    ctrl.style.backgroundColor = '#B5BED6';
  }
    
  function doBold()
  {
	iView.document.execCommand('bold', false, null);
  }

  function doItalic()
  {
	iView.document.execCommand('italic', false, null);
  }

  function doUnderline()
  {
	iView.document.execCommand('underline', false, null);
  }
  
  function doLeft()
  {
    iView.document.execCommand('justifyleft', false, null);
  }

  function doCenter()
  {
    iView.document.execCommand('justifycenter', false, null);
  }

  function doRight()
  {
    iView.document.execCommand('justifyright', false, null);
  }

  function doOrdList()
  {
    iView.document.execCommand('insertorderedlist', false, null);
  }

  function doBulList()
  {
    iView.document.execCommand('insertunorderedlist', false, null);
  }
  
  function doForeCol()
  {
    var fCol = prompt('Enter foreground color', '');
    
    if(fCol != null)
      iView.document.execCommand('forecolor', false, fCol);
  }

  function doBackCol()
  {
    var bCol = prompt('Enter background color', '');
    
    if(bCol != null)
      iView.document.execCommand('backcolor', false, bCol);
  }

  function doLink()
  {
    iView.document.execCommand('createlink');
  }
  
  function doImage()
  {
    var imgSrc = prompt('Enter image location', '');
    
    if(imgSrc != null)    
     iView.document.execCommand('insertimage', false, imgSrc);
  }
  
  function doRule()
  {
    iView.document.execCommand('inserthorizontalrule', false, null);
  }
  
  function doFont(fName)
  {
    if(fName != '')
      iView.document.execCommand('fontname', false, fName);
  }
  
  function doSize(fSize)
  {
    if(fSize != '')
      iView.document.execCommand('fontsize', false, fSize);
  }
  
  function doHead(hType)
  {
    if(hType != '')
    {
      iView.document.execCommand('formatblock', false, hType);  
      doFont(selFont.options[selFont.selectedIndex].value);
    }
  }

  function grabHTML()
  {
  var htmlCode = iView.document.body.innerHTML;
  document.frmText.Content.value = htmlCode;
  return true;
  }

 
  
  function doToggleView() 
{ 
if(viewMode == 1) 
{ 
iHTML = iView.document.body.innerHTML; 
iView.document.body.innerText = iHTML; 

tblCtrls.style.display = 'none'; 
selFont.style.display = 'none'; 
selSize.style.display = 'none'; 
selHeading.style.display = 'none'; 
iView.focus(); 

viewMode = 2; 
} 
else 
{ 
iText = iView.document.body.innerText; 
iView.document.body.innerHTML = iText; 

tblCtrls.style.display = 'inline'; 
selFont.style.display = 'inline'; 
selSize.style.display = 'inline'; 
selHeading.style.display = 'inline'; 
iView.focus(); 

viewMode = 1; 
} 
} 

//-----------------------------------------------
// OK button is active only after text has been entered
//-----------------------------------------------

function OKactive(oFieldName,oButtonName) {

	if (document.getElementById(oFieldName).value != "") { document.getElementById(oButtonName).disabled = false; }
	else { document.getElementById(oButtonName).disabled = true; }

}

//-----------------------------------------------
// Open an edit-options window
//-----------------------------------------------

function openModal(URL,oWidth,oHeight) {

  window.showModalDialog(URL,window,"dialogWidth:" + oWidth + "px;dialogHeight:" + oHeight + "px;center:on;resizable:off;edge:raised;help:off;scroll:off;status:off");
  
}

//-----------------------------------------------
// Find & Replace text
//-----------------------------------------------

function findReplace() {

	var callerWindowObj = dialogArguments;
	var rng = callerWindowObj.iView.document.body.createTextRange();
	var find = oForm.oFind.value;
	var replace = oForm.oReplace.value;
	
	for (i=0; rng.findText(find)!=false; i++) {
    	rng.text = replace;
	}
}

//-----------------------------------------------
// Selection value
//-----------------------------------------------

function selectionValue(oFieldName) {

	var callerWindowObj = dialogArguments;
	var selectionWord = callerWindowObj.iView.document.selection.createRange();
	document.getElementById(oFieldName).value = selectionWord.text;

}

//-----------------------------------------------
// Find text
//-----------------------------------------------

var pos = 0;
function findText() {
    
    	var found = false;
		var find = oForm.oFind.value;
		var callerWindowObj = dialogArguments;
        var text = callerWindowObj.iView.document.body.createTextRange();
		
        for (var i=0; i<=pos && (found=text.findText(find)) != false; i++) {
            text.moveStart("character", 1);
            text.moveEnd("textedit");
        }
		
        if (found) {
            text.moveStart("character", -1);
            text.findText(find);
            text.select();
            text.scrollIntoView();
            pos++;
        } 

}



