function preloadSiteImages() { 
	image1 = new Image();
	image1.src = "http://www.johnwordsworth.com/images/headerbuttonover.png";
}

/*
 * Code for JavaScript Rich Text Editor
 */
 
var isIE = ((navigator.userAgent.toLowerCase().indexOf( "msie" ) != -1) && (navigator.userAgent.toLowerCase().indexOf( "webtv" ) == -1)); 
var isOpera = (navigator.userAgent.toLowerCase().indexOf( "opera" ) != -1);
var isGecko = (navigator.userAgent.toLowerCase().indexOf( "gecko" ) != -1); 
var isSafari = (navigator.userAgent.toLowerCase().indexOf( "safari" ) != -1); 
var isKonqueror = (navigator.userAgent.toLowerCase().indexOf( "konqueror" ) != -1);
var isEditable = (document.getElementById && document.designMode && !isSafari && !isKonqueror) ? true : false;


function displayEditor(editor, html, width, height) { 
	if(isEditable) { 
		document.writeln('<iframe style="border:dotted #dddddd 1px; clear: both; overflow: hidden" frameborder="0" id="' + editor + '" name="' + editor + '" width="' + width + '" height="' + height + 'px"></iframe>'); 
		document.writeln('<input type="hidden" id="ViewSrc' + editor + '"  name="ViewSrc' + editor + '" value="0">');
		document.writeln('<input type="hidden" id="' + editor + 'Hidden" name="' + editor + '" value="">'); 
		document.getElementById(editor + 'Hidden').value = html; 
		designer(editor, html); 
	} else { 
		document.writeln('<textarea style="clear: both; width: ' + width + 'px; height = ' + height + 'px" name="' + editor + '" id="' + editor + '" cols="39" rows="10">' + html + '</textarea>'); 
	} 
} 

function designer(editor, html) { 
	var mainContent= '<html id="' + editor + '"><head></head><body style="text-align: justify">' + html + '</body></html>' ; 
	var edit = document.getElementById(editor).contentWindow.document; 
	edit.open(); 
	edit.write(mainContent); 
	edit.close();
	edit.designMode = "On"; 

	if(document.contentDocument) { 
		document.getElementById(editor).contentDocument.designMode = "on" ; 
	} 
	
	
	setTimeout("frameUpdate('" + editor + "')", 2000);
}

function frameUpdate(editor) {
	if ( document.getElementById(editor).contentWindow.document.height ) {
		if ( document.getElementById(editor).height != document.getElementById(editor).contentWindow.document.height + 60 ) 
			document.getElementById(editor).height = document.getElementById(editor).contentWindow.document.height + 60;
	} else {
		if ( document.getElementById(editor).height != document.getElementById(editor).contentWindow.document.body.scrollHeight + 60 ) 
			document.getElementById(editor).height = document.getElementById(editor).contentWindow.document.body.scrollHeight + 60;
	}

	document.getElementById(editor + "Hidden").value = getEditorSubmitData(editor);
	setTimeout("frameUpdate('" + editor + "')", 1000);
}

function editorCommand(editor, command, option) { 
	var mainField = document.getElementById(editor).contentWindow; 
	try { mainField.focus(); mainField.document.execCommand(command, false, option); mainField.focus(); } catch (e) { } 
}

function displayEditorButtons(editor, showSaveButton) {
	var style="display: block; float: left; width: 32px; text-align: center; background-color: lightblue; border: solid 1px #6699ee";
	var style2="display: block; float: right; width: 60; text-align: center; background-color: lightblue; border: solid 1px #6699ee";
	
	if ( showSaveButton ) 
		document.writeln("<a style='" + style2 + "' accessKey='S' href=\"javascript:alert(getEditorSubmitData('" + editor + "'));\"><u>S</u>ave</a>");
	
	document.writeln("<a style='" + style2 + "' accessKey='P' id='" + editor + "ButtonSource' href=\"javascript:toggleHTMLSource('" + editor + "');\">Source</a>");
	document.writeln("<a style='" + style + "' accessKey='B' href=\"javascript:editorCommand('" + editor + "', 'bold', '');\"><b>B</b></a>");
	document.writeln("<a style='" + style + "' accessKey='I' href=\"javascript:editorCommand('" + editor + "', 'italic', '');\"><i>I</i></a>");
	document.writeln("<a style='" + style + "' accessKey='U' href=\"javascript:editorCommand('" + editor + "', 'underline', '');\"><u>U</u></a>");
	document.writeln("<a style='" + style + "' accessKey='N' href=\"javascript:editorCommand('" + editor + "', 'insertimage', window.prompt('Enter the URL of the image you would like to insert:','http://'));\">Img</a>");
	document.writeln("<a style='" + style + "' accessKey='A' href=\"javascript:editorCommand('" + editor + "', 'createlink', window.prompt('Enter the URL of the link target:','http://'));\">URL</a>");
	document.writeln("<a style='" + style + "' accessKey='Q' href=\"javascript:editorCommand('" + editor + "', 'unlink', '');\"><strike>URL</strike></a>");
	document.writeln("<a style='" + style + "' accessKey='Z' href=\"javascript:editorCommand('" + editor + "', 'undo', '');\">Undo</a>");
}


function htmlentities(s) {
    return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\n/g, "<br>");
}

function unhtmlentities(s) { 
	return s.replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/<br>/ig, "\n");
}

function toggleHTMLSource(editor) {
	if ( document.getElementById("ViewSrc" + editor).value == 0 ) {
		var innerHTML = document.getElementById(editor).contentWindow.document.body.innerHTML;
		document.getElementById(editor).contentWindow.document.body.innerHTML = htmlentities(innerHTML);
		document.getElementById("ViewSrc" + editor).value = 1;
		document.getElementById(editor + "ButtonSource").innerHTML = "Preview";
	} else {
		var innerHTML = document.getElementById(editor).contentWindow.document.body.innerHTML;
		document.getElementById(editor).contentWindow.document.body.innerHTML = unhtmlentities(innerHTML);
		document.getElementById("ViewSrc" + editor).value = 0;	
		document.getElementById(editor + "ButtonSource").innerHTML = "Source";
	}
}

function getEditorSubmitData( editor ) {
	if ( document.getElementById("ViewSrc" + editor).value == 0 ) {
		return document.getElementById(editor).contentWindow.document.body.innerHTML;
	} else {
		return unhtmlentities(document.getElementById(editor).contentWindow.document.body.innerHTML);
	}
}
