var isEditable= false;
var isIE;
var isGecko;
var isSafari;
var isKonqueror;

function Init() 
{ 
	 
	document.getElementById("iView").contentDocument.designMode = "on";
}



function initiateEditor() 
{
	 //check what browser is in use
	 var browser = navigator.userAgent.toLowerCase();
	 isIE = ((browser .indexOf( "msie" ) != -1) && (browser .indexOf( "opera" ) == -1) && (browser .indexOf( "webtv" ) == -1)); 
	 isGecko = (browser .indexOf( "gecko" ) != -1);
	 isSafari = (browser .indexOf( "safari" ) != -1);
	 isKonqueror = (browser.indexOf( "konqueror" ) != -1);
	 
	 //enable designMode if the browser is not safari or konqueror.
	 if (document.getElementById && document.designMode && !isSafari && !isKonqueror) 
	 {
	   isEditable= true;
	 }
}

function initiateEditor2() 
{
	 //check what browser is in use
	 var browser = navigator.userAgent.toLowerCase();
	 isIE = ((browser .indexOf( "msie" ) != -1) && (browser .indexOf( "opera" ) == -1) && (browser .indexOf( "webtv" ) == -1)); 
	 isGecko = (browser .indexOf( "gecko" ) != -1);
	 isSafari = (browser .indexOf( "safari" ) != -1);
	 isKonqueror = (browser.indexOf( "konqueror" ) != -1);
	 
	 //enable designMode if the browser is not safari or konqueror.
	 if (document.getElementById && document.designMode && !isSafari && !isKonqueror) 
	 {
	   isEditable= true;
	 }
}

//Javascript function dislpayEditor will create the textarea.

function displayEditor(editor, html, width, height) {
   if(isEditable){
       document.writeln('<iframe id="' + editor + '" name="' + editor + '" width="' + width + 'px" height="' + height + 'px"></iframe>');
//create a hidden field that will hold everything that is typed in the textarea
       document.writeln('<input type="hidden" id="hidden' + editor + '" name="' + editor + '" value="">');
//assign html (textarea value) to hiddeneditor 
      document.getElementById('hidden' + editor).value = html;
//call function designer
      designer(editor, html);
   }else{
     //document.writeln('<textarea name="' + editor + '" id="' + editor + '" cols="39" rows="10">' + html + '</textarea>');
     document.writeln('<textarea name="' + editor + ' id="' + editor + ' cols="39" rows="10">' + html + '</textarea>');
     
   }
}

function displayEditor2(editor2, html, width, height) {
   if(isEditable){
       document.writeln('<iframe id="' + editor2 + '" name="' + editor2 + '" width="' + width + 'px" height="' + height + 'px"></iframe>');
//create a hidden field that will hold everything that is typed in the textarea
       document.writeln('<input type="hidden" id="hidden' + editor2 + '" name="' + editor2 + '" value="">');
//assign html (textarea value) to hiddeneditor 
      document.getElementById('hidden' + editor2).value = html;
//call function designer
      designer(editor2, html);
   }else{
     //document.writeln('<textarea name="' + editor + '" id="' + editor + '" cols="39" rows="10">' + html + '</textarea>');
     document.writeln('<textarea name="' + editor2 + ' id="' + editor2 + ' cols="39" rows="10">' + html + '</textarea>');
     
   }
}


//this is designer function that enables designMode and writes defalut text to the text area
function designer(editor, html) {
   	//var mainContent= '<html id="' + editor + '"><head></head><body>"' + html + '"</body></html>' ;
    var mainContent= '<html id="' + editor + '"><head></head><body>' + html + '</body></html>' ;
    
//assign the frame(textarea) to the edit variable using that frames id
     var edit = document.getElementById(editor).contentWindow.document;
//write the content to the textarea
      edit.write(mainContent);
//enable the designMode
      edit.designMode =  "On" ;
//enable the designMode for Mozilla
     document.getElementById(content).contentDocument.designMode = "on" ;
}

function designer2(editor2, html) {
   	//var mainContent= '<html id="' + editor + '"><head></head><body>"' + html + '"</body></html>' ;
    var mainContent= '<html id="' + editor2 + '"><head></head><body>' + html + '</body></html>' ;
    
//assign the frame(textarea) to the edit variable using that frames id
     var edit = document.getElementById(editor2).contentWindow.document;
//write the content to the textarea
      edit.write(mainContent);
//enable the designMode
      edit.designMode =  "On" ;
//enable the designMode for Mozilla
     document.getElementById(content2).contentDocument.designMode = "on" ;
}



function updateEditor(editor) 
{
 if (!isEditable) return;
//assign the value of the textarea to the hidden field. 
 var hiddenField = document.getElementById('hidden' + editor);
 if (hiddenField.value == null) hiddenField.value = "";
  hiddenField.value = document.getElementById(editor).contentWindow.document.body.innerHTML;
}
function updateEditor2(editor2) 
{
 if (!isEditable) return;
//assign the value of the textarea to the hidden field. 
 var hiddenField = document.getElementById('hidden' + editor2);
 if (hiddenField.value == null) hiddenField.value = "";
  hiddenField.value = document.getElementById(editor2).contentWindow.document.body.innerHTML;
}

// execCommand calls
function boldIt() 
{ 
	document.getElementById("content").contentDocument.execCommand('bold', false, null);
	//iView.document.execCommand('bold', false, null); 
}
function boldIt2() 
{ 
	document.getElementById("content2").contentDocument.execCommand('bold', false, null);
	//iView.document.execCommand('bold', false, null); 
}
function italicCommand() 
{ 
	document.getElementById("content").contentDocument.execCommand('italic', false, null);
}
function italicCommand2() 
{ 
	document.getElementById("content2").contentDocument.execCommand('italic', false, null);
}

function underlineCommand() 
{ 
	document.getElementById("content").contentDocument.execCommand('underline', false, null);
}
function underlineCommand2() 
{ 
	document.getElementById("content2").contentDocument.execCommand('underline', false, null);
}

function strikethroughCommand() 
{ 
	document.getElementById("content").contentDocument.execCommand('strikethrough', false, null);
}
function strikethroughCommand2() 
{ 
	document.getElementById("content2").contentDocument.execCommand('strikethrough', false, null);
}

function justifyLeftCommand() 
{ 
	document.getElementById("content").contentDocument.execCommand('justifyleft', false, null);
}
function justifyLeftCommand2() 
{ 
	document.getElementById("content2").contentDocument.execCommand('justifyleft', false, null);
}

function justifyRightCommand() 
{ 
	document.getElementById("content").contentDocument.execCommand('justifyright', false, null);
}
function justifyRightCommand2() 
{ 
	document.getElementById("content2").contentDocument.execCommand('justifyright', false, null);
}

function justifyCenterCommand() 
{ 
	document.getElementById("content").contentDocument.execCommand('justifycenter', false, null);
}
function justifyCenterCommand2() 
{ 
	document.getElementById("content2").contentDocument.execCommand('justifycenter', false, null);
}


function indentCommand() 
{ 
	document.getElementById("content").contentDocument.execCommand('indent', false, null);
}
function indentCommand2() 
{ 
	document.getElementById("content2").contentDocument.execCommand('indent', false, null);
}


function outdentCommand() 
{ 
	document.getElementById("content").contentDocument.execCommand('outdent', false, null);
}
function outdentCommand2() 
{ 
	document.getElementById("content2").contentDocument.execCommand('outdent', false, null);
}

function undoCommand() 
{ 
	document.getElementById("content").contentDocument.execCommand('undo', false, null);
}
function undoCommand2() 
{ 
	document.getElementById("content2").contentDocument.execCommand('undo', false, null);
}

function redoCommand() 
{ 
	document.getElementById("content").contentDocument.execCommand('redo', false, null);
}
function redoCommand2() 
{ 
	document.getElementById("content2").contentDocument.execCommand('redo', false, null);
}

function paragraphCommand() 
{ 
	document.getElementById("content").contentDocument.execCommand('insertparagraph', false, null);
}
function paragraphCommand2() 
{ 
	document.getElementById("content2").contentDocument.execCommand('insertparagraph', false, null);
}

function linkCommand() 
{ 
	document.getElementById("content").contentDocument.execCommand('createlink', false, prompt('Enter a full URL', 'http://www.'));
}
function linkCommand2() 
{ 
	document.getElementById("content2").contentDocument.execCommand('createlink', false, prompt('Enter a full URL', 'http://www.'));
}

//font colors
function fontColorCommand(color) 
{ 
	document.getElementById("content").contentDocument.execCommand('forecolor', false, color);
}
function fontColorCommand2(color) 
{ 
	document.getElementById("content2").contentDocument.execCommand('forecolor', false, color);
}

//background colors
function backgroundColorCommand(color) 
{ 
	document.getElementById("content").contentDocument.execCommand('backcolor', false, color);
}
function backgroundColorCommand2(color) 
{ 
	document.getElementById("content2").contentDocument.execCommand('backcolor', false, color);
}


function doFontSize(hType) 
{ 
	if(hType != '') 
	{ 
		document.getElementById("content").contentDocument.execCommand('fontsize', false, hType);
		//iView.document.execCommand('formatblock', false, hType); 
		//doFont(selFont.options[selFont.selectedIndex].value); 
	} 
} 
function doFontSize2(hType) 
{ 
	if(hType != '') 
	{ 
		document.getElementById("content2").contentDocument.execCommand('fontsize', false, hType);
		//iView.document.execCommand('formatblock', false, hType); 
		//doFont(selFont.options[selFont.selectedIndex].value); 
	} 
} 

function doFont(fName) 
{ 
	if(fName != '')
	{
		//iView.document.execCommand('fontname', false, fName); 
		document.getElementById("content").contentDocument.execCommand('fontname', false, fName);
	}
} 
function doFont2(fName) 
{ 
	if(fName != '')
	{
		//iView.document.execCommand('fontname', false, fName); 
		document.getElementById("content2").contentDocument.execCommand('fontname', false, fName);
	}
} 

function insertImage (imageLocation)
{
	document.getElementById("content").contentDocument.execCommand('insertimage', false, imageLocation);
	
}
function insertImage2 (imageLocation)
{
	document.getElementById("content2").contentDocument.execCommand('insertimage', false, imageLocation);
	
}


function updateMessage() 
{
  //var x = document.getElementById("iView").value;
  //document.getElementById("iView").childNodes[0].nodeValue = x;
	 
  // - note that for this to work, the div must already have a child node.  
  // - Otherwise, you'll get an error.
  
  
}


function doToggleView(content) 
{ 
	var viewMode = 1; // WYSIWYG 
	
	if (viewMode == 1) 
	{ 
				//iHTML = iView.document.body.innerHTML; 
				//iView.document.body.innerText = iHTML; 

		
		
		
		var iHTML = document.getElementById(content).contentDocument.body.innerHTML;
		document.getElementById(content).contentDocument.body.innerText = iHTML;
		
		
		// Hide all controls 
		tblCtrls.style.display = 'none'; 
		selectFont.style.display = 'none'; 
		seectlSize.style.display = 'none'; 
		selectHeading.style.display = 'none'; 
		iView.focus(); 
		viewMode = 2; // Code 
	} 
	/*else 
	{ 
		iText = document.getElementById("iView").contentDocument.body.innerText;
		document.getElementById("iView").contentDocument.body.innerText = iText;
		
		// Show all controls 
		tblCtrls.style.display = 'inline'; 
		selectFont.style.display = 'inline'; 
		selectSize.style.display = 'inline'; 
		selectHeading.style.display = 'inline'; 
		iView.focus(); 
		viewMode = 1; // WYSIWYG 
	} */
} 



//CSS calls for action over Bold, Italic etc buttons
function selectOn(control) 
{ 
	control.style.borderColor = '#000000'; 
	control.style.backgroundColor = '#B5BED6'; 
	control.style.cursor = 'hand';  //cursor types: crosshair, hand, wait, help, text, n-resize,  
									// s-resize, auto, default
} 

function selectOff(control)
{
	/*control.style.borderColor = ; */
	control.style.backgroundColor = '#FFFFFF'; 
	control.style.cursor = 'default'; 
	
}

function selectDown(control)
{
	control.style.borderColor = '#000000'; 
	control.style.backgroundColor = '#FFFF66'; 
	control.style.cursor = 'hand'; 	
}

function selectUp(control)
{
	control.style.borderColor = '#000000'; 
	control.style.backgroundColor = '#FFFFF3'; 
	control.style.cursor = 'default'; 	
}
