﻿var whizzywig_version = 'Whizzywig v55i'; //IE color palette bug; idTa!=TA?x HTML + hidden; row/col bug; link form bugs;Enter = <P>; fix user but bug; 
//Copyright © 2005-2007 John Goodman - john.goodman(at)unverse.net  *date 070324
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 

/*
Lots of changes done to make it work with more than one textarea.
Torben Wilhelmsen, 2008, www.wil.dk
*/
var owButtonPath;  //path to toolbar button images;  unset or "textbuttons" means don't use images
var buttonStrip=[]; //object containing button strip information (optional)
var owCssFile;     //url of CSS stylesheet to attach to edit area
var imageBrowse; //path to page for image browser
var linkBrowse;  //path to page for link browser
var idTa;        //id of the textarea to whizzywig (param to makeWhizzyWig)
var owGentleClean = "true";  //true: owGentleClean preserves spans, inline styles and classes; false: deletes same; ask: asks.

//OTHER GLOBALS
var oW, sel, rng, papa, trail, ppw; //Whizzy contentWindow, current sel, range, parent, DOM path, popwindow;
var sels = '';
var buts = ''; 
var vals = new Array();
var opts = new Array();
var dobut = new Array();
var whizzies = new Array();
var owLangIndex;

function makeWhizzyWig(txtArea, controls){ // make a WhizzyWig from the textarea

	objTa = owFindObj(txtArea);
	idTa = objTa.id;
	if (!idTa) {
		idTa = objTa.name + '_' + parseInt(Math.random()*10000);
		objTa.id = idTa;
		}
	whizText = ''
	whizzies[whizzies.length]=idTa;
	if (!document.designMode) {// (Safari ???)
		if (idTa.nodeName=="TEXTAREA") tagButs();
		alert("Whizzywig "+owLangKey("edit_not_487"));
		return;
	}
	var taContent = objTa.defaultValue ? objTa.defaultValue : objTa.innerHTML ? objTa.innerHTML: ''; //anything in the textarea?
	//if (!objTa.rows < 5) objTa.rows='15';//IE won't use % from style
	taWidth = objTa.style.width ? objTa.style.width : objTa.cols + "ex";  //grab the width and...
	taHeight = objTa.style.height ? objTa.style.height : objTa.rows + "em";  //...height from the textarea
	if (objTa.nodeName=="TEXTAREA"){
		objTa.style.color = '#060';
		objTa.style.zIndex = '2';
	} else whizText += whiztextAdd('<input type="hidden" id="wzhid_'+idTa+'" name="'+idTa+'" />');
	owHideTa(idTa);
	var frm=objTa.parentNode;
	while (frm.nodeName != 'FORM') frm=frm.parentNode;//if not form, keep trying
	objTaParent = objTa.parentNode;
	objTaParent.className = 'openwysiwygparent';
	whizObj = document.createElement('div')
	whizObjText = ''
	objTaParent.insertBefore(whizObj,objTa);
	addEvt(frm,"submit",syncTextarea);
	whizObjText = whiztextAdd2ObjHTML(whizObjText,'<style type="text/css">body, div, textarea, table, td {font-family:\'Lucida Grande\',Verdana;font-size:12px;} button {border:1px solid #ffffff;width:10px;vertical-align:middle;padding:0;margin:1px 0} button img{vertical-align:middle;margin:-3px} select{vertical-align:middle;margin:1px}  .wzCtrl {background:ButtonFace; border:2px outset ButtonShadow; padding:5px;} #sourceTa{color:#060;font-family:mono;} #whizzyWig {border-width:1px}</style>');
	var dsels = 'fontname fontsize formatblock';
	var dbuts = ' bold italic underline | left center right | number bullet indent outdent | undo redo  | color hilite rule | link image table | word clean html spellcheck ';
	var tbuts = ' tstart add_row_above add_row_below delete_row | add_column_before add_column_after delete_column | table_in_cell';
	var t_end = ''; //table controls end, if needed
	controls = controls ? controls.toLowerCase() : "all";
	if (controls=="all") controls = dsels +' newline '+ dbuts + tbuts;
	else controls += tbuts;
	whizObjText = whiztextAdd2ObjHTML(whizObjText,'<div style="width:'+taWidth+'" onmouseover="owSetCurrent(\''+idTa+'\')"><div style="width:'+taWidth+'" id="CONTROLS'+idTa+'" class="wzCtrl" unselectable="on">');
	gizmos = controls.split(' ');
	for (var i = 0; i < gizmos.length; i++) {
		if (gizmos[i]){ //make buttons and selects for toolbar, in order requested
			if (gizmos[i] == 'tstart') {
				whizObjText = whiztextAdd2ObjHTML(whizObjText,'<div id="TABLE_CONTROLS'+idTa+'" style="display:none" unselectable="on">');
				t_end = '</div>';
			}
			else if (gizmos[i] == '|') whizObjText = whiztextAdd2ObjHTML(whizObjText,'&nbsp;<big style="padding-bottom:2em">|</big>&nbsp;');
			else if (gizmos[i] == 'newline') whizObjText = whiztextAdd2ObjHTML(whizObjText,'<br/>');
			else if ((dsels+sels).indexOf(gizmos[i]) != -1) whizObjText = makeSelect(whizObjText,gizmos[i])
			else if ((dbuts+buts+tbuts).indexOf(gizmos[i]) != -1) whizObjText = makeButton(whizObjText,gizmos[i]);
		}
	}
	whizObjText = whiztextAdd2ObjHTML(whizObjText,t_end) //table controls end
	//whizObjText = whiztextAdd2ObjHTML(whizObjText,'<a href="http://www.unverse.net" style="color:buttonface" title="'+whizzywig_version+'">.</a> ');
	whizObjText = whiztextAdd2ObjHTML(whizObjText,fGo('LINK'));
	if (linkBrowse) whizObjText = whiztextAdd2ObjHTML(whizObjText,'<input type="button" onclick=doWin("'+linkBrowse+'"); value="'+owLangKey("browse_4")+'"> ');
	whizObjText = whiztextAdd2ObjHTML(whizObjText,owLangKey('link_addr_58')+': <input type="text" id="lf_url'+idTa+'" size="60"><br/><input type="button" value="http://" onclick="owFindObj(\'lf_url'+idTa+'\').value=\'http://\'+owFindObj(\'lf_url'+idTa+'\').value"> <input type="button" value="mailto:" onclick="owFindObj(\'lf_url'+idTa+'\').value=\'mailto:\'+owFindObj(\'lf_url'+idTa+'\').value"><input type="checkbox" id="lf_new'+idTa+'">'+owLangKey("open_link_71")+owFNo(owLangKey("ok_683"),"insertLink('"+idTa+"')"));//LINK_FORM end
	whizObjText = whiztextAdd2ObjHTML(whizObjText,fGo('IMAGE'));
	if (imageBrowse) whizObjText = whiztextAdd2ObjHTML(whizObjText,'<input type="button" onclick=doWin("'+imageBrowse+'"); value="'+owLangKey("browse_4")+'"> ');
	whizObjText = whiztextAdd2ObjHTML(whizObjText,owLangKey('imag_addr_17')+': <input type="text" id="if_url'+idTa+'" size="30"> <label title='+owLangKey("to_disp_133")+'><br/>'+owLangKey("alte_text_71")+':<input id="if_alt'+idTa+'" type="text" size="30"></label><br/>'+owLangKey("align_56")+':<select id="if_side'+idTa+'"><option value="none">_&hearts;_ '+owLangKey("normal_1")+'</option><option value="left">&hearts;= &nbsp;'+owLangKey("left_608")+'</option><option value="right">=&hearts; &nbsp;'+owLangKey("right_43")+'</option></select> '+owLangKey("border_8")+':<input type="text" id="if_border'+idTa+'" size="2" value="0" title="'+owLangKey("numb_or_514")+'"> '+owLangKey("margin_5")+':<input type="text" id="if_margin'+idTa+'" size="2" value="0" title="'+owLangKey("numb_or_931")+'"><br/>'+owFNo(owLangKey("inse_imag_20"),"insertImage()"));//IMAGE_FORM end
	whizObjText = whiztextAdd2ObjHTML(whizObjText,fGo('TABLE')+owLangKey("rows_139")+':<input type="text" id="tf_rows'+idTa+'" size="2" value="3"> <select id="tf_head'+idTa+'"><option value="0">'+owLangKey("no_head_346")+'</option><option value="1">'+owLangKey("incl_head_45")+'</option></select> '+owLangKey("columns_")+':<input type="text" id="tf_cols'+idTa+'" size="2" value="3"> '+owLangKey("bord_widt_28")+':<input type="text" id="tf_border'+idTa+'" size="2" value="1"> '+owFNo(owLangKey("inse_tabl_39"),"makeTable()"));//TABLE_FORM end
	whizObjText = whiztextAdd2ObjHTML(whizObjText,fGo('COLOR')+'<input type="hidden" id="cf_cmd'+idTa+'"><div style="background:#0f0;padding:1px;height:22px;width:125px;float:left"><div id="cPrvw'+idTa+'" style="background-color:red; height:100%; width:100%"></div></div> <input type=text id="cf_color'+idTa+'" value="red" size=17 onpaste=vC(value) onblur=vC(value)> <input type="button" onmouseover=vC() onclick=sC(false,\''+idTa+'\') value="'+owLangKey("ok_683")+'">  <input type="button" onclick="owHideDialogs();" value="'+owLangKey("cancel_7")+'"><br/> '+owLangKey("clic_belo_10")+' <a href="http://www.unverse.net/colortable.htm" target="_blank">'+owLangKey("colo_name_21")+'</a><br clear=all> <table border=0 cellspacing=1 cellpadding=0 width="'+taWidth+'" bgcolor="#000000">'+"\n");
	colorTdWidth = parseInt(Math.floor(400/36.0));
	var wC = new Array("00","33","66","99","CC","FF")  //color table
	for (i=0; i<wC.length; i++){
		whizObjText = whiztextAdd2ObjHTML(whizObjText,"<tr>");
		for (j=0; j<wC.length; j++){
			for (k=0; k<wC.length; k++){
				var clr = wC[i]+wC[j]+wC[k];
				whizObjText = whiztextAdd2ObjHTML(whizObjText,' <td style="background:#'+clr+';height:'+colorTdWidth+'px;width:'+colorTdWidth+'px" onmouseover=vC("#'+clr+'") onclick=sC(\''+idTa+'\',"#'+clr+'")></td>'+"\n");
			}
		}
		whizObjText = whiztextAdd2ObjHTML(whizObjText,'</tr>');
	}
	whizObjText = whiztextAdd2ObjHTML(whizObjText,"</table></div>\n"); //end color table,COLOR_FORM
	whizObjText = whiztextAdd2ObjHTML(whizObjText,"</div>\n"); //controls end
	whizObjText = whiztextAdd2ObjHTML(whizObjText,'<div class="wzCtrl" id="showWYSIWYG'+idTa+'" style="display:none"><input type="button" onclick="owShowDesign(\''+idTa+'\');" value="'+owLangKey("hide_html_22")+'">');
	whizObjText = tagButs(whizObjText,idTa);
	whizObjText = whiztextAdd2ObjHTML(whizObjText,'</div>'+"\n");
	
	whizObjText = whiztextAdd2ObjHTML(whizObjText,'<iframe class="owFrame" style="width:100%;height:'+taHeight+'" src="javascript:;" id="whizzy'+idTa+'"></iframe></div>'+"\n");
	whizObj.innerHTML = whizObjText;
	//alert(whizObjText);
	var startHTML = "<html>\n<head>\n";
	if (owCssFile) startHTML += '<link media="all" type="text/css" href="'+owCssFile+'" rel="stylesheet">\n';
	startHTML += '</head>\n<body id="'+idTa+'">\n'+owTidyD(taContent)+'</body>\n</html>';
	oW = owFindObj("whizzy"+idTa).contentWindow;
	//alert("whizzy"+idTa);
	var d=oW.document;
	try{d.designMode = "on";} catch(e){ setTimeout('oW.designMode = "on";', 100);}
	d.open();
	d.write(startHTML);
	d.close();
	if (oW.addEventListener) oW.addEventListener("keypress", owKb_handler, true); //keyboard shortcuts for Moz
	else {d.body.attachEvent("onpaste",function(){setTimeout('owCleanUp("'+idTa+','+owGentleClean+')',10);});}
	addEvt(d,"mouseup", owWhereAmI);
	addEvt(d,"keyup", owWhereAmI);
	addEvt(d,"dblclick", doDbl);
	idTa=null;
	objTa=null;
// 	alert(owLangCollect);
	} 
//end makeWhizzyWig

function addEvt(o,e,f){ if(window.addEventListener) o.addEventListener(e, f, false); else o.attachEvent("on"+e,f);}

function doDbl(){if (papa.nodeName == 'IMG') doImage(); else if (papa.nodeName == 'A') doLink();}

function makeButton(whizObjText,button){  // assemble the button requested
	var butHTML, ucBut = button.substring(0,1).toUpperCase();
	ucBut += button.substring(1);
	ucBut = owLangKey(ucBut.replace(/_/g,' '));
	if (!document.frames && (button=="word" ||button=="spellcheck")) return whiztextAdd2ObjHTML(whizObjText,''); //Not allowed from Firefox
	if (objTa.nodeName!="TEXTAREA" && button=="html") return whiztextAdd2ObjHTML(whizObjText,''); 
	if (!owButtonPath || owButtonPath == "textbuttons") butHTML = '<button type="button" onClick="makeSo(\''+button+'\',\''+idTa+'\')">'+ucBut+"</button>\n";
	else butHTML = '<button  title="'+ucBut+'" type=button onClick=makeSo("'+button+'","'+idTa+'")>'+(buttonStrip[button]!=undefined?'<div style="width:'+buttonStrip._w+'px;height:'+buttonStrip._h+'px;background-image:url('+buttonStrip._f+');background-position:'+buttonStrip[button]+'px 0px"></div>':'<img src="'+owButtonPath+button+'.gif" alt="'+ucBut+'" onError="this.parentNode.innerHTML=this.alt">')+'</button>';
	//if space in name include text as well
	return whiztextAdd2ObjHTML(whizObjText,butHTML);
	}

function fGo(id){ return '<div id="'+id+'_FORM'+idTa+'" unselectable="on" style="display:none" onkeypress="if(event.keyCode==13) {return false;}"><hr>'+"\n"; }//new form

function owFNo(txt,go){ //form do it/cancel buttons
	return ' <input type="button" onclick="'+go+'" value="'+txt+'"> <input type="button" onclick="owHideDialogs();" value='+owLangKey("cancel_7")+"></div>\n";
	}

function makeSelect(whizObjText,select){ // assemble the <select> requested
	if (select == 'formatblock') {
		var h = owLangKey("heading_");
		var values = ["<p>", "<p>", "<h1>", "<h2>", "<h3>", "<h4>", "<h5>", "<h6>", "<address>",  "<pre>"];
		var options = [owLangKey('choo_styl_76')+":", owLangKey("Paragraph"), h+" 1 ", h+" 2 ", h+" 3 ", h+" 4 ", h+" 5 ", h+" 6", owLangKey("address_"), owLangKey("fixe_widt_43")];
		} 
	else if (select == 'fontname') {
		var values = ["Arial, Helvetica, sans-serif", "Arial, Helvetica, sans-serif","'Arial Black', Helvetica, sans-serif", "'Comic Sans MS' fantasy", "Courier New, Courier, monospace", "Georgia, serif", "Impact,sans-serif","'Times New Roman', Times, serif", "'Trebuchet MS',sans-serif", "Verdana, Arial, Helvetica, sans-serif"];
		var options = [owLangKey("font_477")+":", "Arial","Arial Black", "Comic", "Courier", "Georgia", "Impact","Times New Roman", "Trebuchet","Verdana"];
		} 
	else if (select == 'fontsize') {
		var values = ["3", "1", "2", "3", "4", "5", "6", "7"];
		var options = [owLangKey("font_size_29")+":", "1 "+owLangKey("small_12"), "2", "3", "4", "5", "6", "7 "+owLangKey("big_834")];
		}
	else { 
		var values = vals[select];
		var options = opts[select];
		}
	whizObjText = whiztextAdd2ObjHTML(whizObjText,'<select id="'+select+idTa+'" onchange="doSelect(this.id,\''+idTa+'\');">'+"\n");
	for (var i = 0; i < values.length; i++) {
		whizObjText = whiztextAdd2ObjHTML(whizObjText,' <option value="' + values[i] + '">' + options[i] + "</option>\n");
		}
	whizObjText = whiztextAdd2ObjHTML(whizObjText,"</select>\n");
	return whizObjText;
	}
function tagButs(whizObjText,idTa) {
	return whiztextAdd2ObjHTML(whizObjText,'<input type="button" onclick="owDoTag(\'<h1>\',\''+idTa+'\')" value="H1" title="<H1>"><input type="button" onclick=\'owDoTag("<h2>")\' value="H2" title="<H2>"><input type="button" onclick=\'owDoTag("<h3>")\' value="H3" title="<H3>"><input type="button" onclick=\'owDoTag("<h4>")\' value="H4" title="<H4>"><input type="button" onclick=\'owDoTag("<p>")\' value="P" title="<P>"><input type="button" onclick=\'owDoTag("<strong>")\' value="S" title="<STRONG>" style="font-weight:bold"><input type="button" onclick=\'owDoTag("<em>")\' value="E" title="<EM>" style="font-style:italic;"><input type="button" onclick=\'owDoTag("<li>")\' value="&bull;&mdash;" title="<LI>"><input type="button" onclick=\'owDoTag("<a>")\' value="@" title="<A HREF= >"><input type="button" onclick=\'owDoTag("<img>")\' value="[&hearts;]" title="<IMG SRC= >"><input type="button" onclick=\'owDoTag("<br />")\' value="&larr;" title="<BR />">');
	}
function makeSo(command, idTa, option) {  //format selected text or line in the whizzy
	oW = owFindObj("whizzy"+idTa).contentWindow;
	owHideDialogs();
	if (buts.indexOf(command)!=-1) {owInsHTML(dobut[command]); return;} //user button
	if (!document.all) oW.document.execCommand('useCSS',false, true); //no spans for bold, italic
	if ("leftrightcenterjustify".indexOf(command) !=-1) command = "justify" + command;
	else if (command == "number") command = "insertorderedlist";
	else if (command == "bullet") command = "insertunorderedlist";
	else if (command == "rule") command = "inserthorizontalrule";
	switch (command) {
		case "color": owFindObj('cf_cmd'+idTa).value="forecolor"; if (owTextSel()) owShowTa('COLOR_FORM'+idTa); break;
		case "hilite" : owFindObj('cf_cmd'+idTa).value="backcolor"; if (owTextSel()) owShowTa('COLOR_FORM'+idTa); break;
		case "image" : doImage(idTa); break;
		case "link" : doLink(idTa); break;
		case "html" : showHTML(idTa); break;
		case "table" : doTable(idTa); break;
		case "delete_row" : doRow('delete','0'); break;
		case "add_row_above" : doRow('add','0'); break;
		case "add_row_below" : doRow('add','1'); break;
		case "delete_column" : doCol('delete','0'); break;
		case "add_column_before" : doCol('add','0'); break;
		case "add_column_after" : doCol('add','1'); break;
		case "table_in_cell" : owHideDialogs(); owShowTa('TABLE_FORM'+idTa); break;
		case "clean" : owCleanUp(idTa,owGentleClean); break;
		case "word" : oW.document.execCommand("paste", false, false); owCleanUp(idTa,false); break;
		case "spellcheck" : spellCheck(); break;
		default: oW.document.execCommand(command, false, option);
	}
}
function doSelect(selectname,idTa) {  //select on toolbar used - do it
	oW = owFindObj("whizzy"+idTa).contentWindow;
	var idx = owFindObj(selectname).selectedIndex;
	var selected = owFindObj(selectname).options[idx].value;
	owFindObj(selectname).selectedIndex = 0;
	selectname=selectname.replace(idTa,"");
	if (" _formatblock_fontname_fontsize".indexOf('_'+selectname) > 0) {
		var cmd = selectname;
		oW.document.execCommand(cmd, false, selected);
	} else {
		owInsHTML(selected);
	}  
	oW.focus();
}
function vC(colour) { // view Color
	if (!colour) colour = owFindObj('cf_color'+idTa).value;
	owFindObj('cPrvw'+idTa).style.backgroundColor = colour;
	owFindObj('cf_color'+idTa).value = colour;
}
function sC(idTa, color) {  //set Color
	oW = owFindObj("whizzy"+idTa).contentWindow;
	owHideDialogs();
	var cmd = owFindObj('cf_cmd'+idTa).value;
	if  (!color) color = owFindObj('cf_color'+idTa).value;
	if (rng) rng.select();
	oW.document.execCommand(cmd, false, color);
	oW.focus();
}
function doLink(){
	if (owTextSel()) {
		if (papa.nodeName == 'A') owFindObj("lf_url"+idTa).value = papa.href;
		owShowTa('LINK_FORM'+idTa);
	}
}
function insertLink(idTa) {
	oW = owFindObj("whizzy"+idTa).contentWindow;
	if (rng) rng.select();
	URL = owFindObj("lf_url"+idTa).value; 
	if (URL.replace(/ /g,"") == "") oW.document.execCommand('Unlink',false,null);
	else if (owFindObj("lf_new"+idTa).checked) owInsHTML('<a href="'+URL+'" target="_blank">');
	else oW.document.execCommand('CreateLink',false,URL);
	owHideDialogs();
}
function doImage(){
	if (papa && papa.nodeName == 'IMG'){
		owFindObj("if_url"+idTa).value = papa.src;
		owFindObj("if_alt"+idTa).value=papa.alt;
		owFindObj("if_side"+idTa).selectedIndex=(papa.align=="left")?1:(papa.align=="right")?2:0; 
		owFindObj("if_border"+idTa).value=papa.style.border?papa.style.border:papa.border>0?papa.border:0;
		owFindObj("if_margin"+idTa).value=papa.style.margin?papa.style.margin:papa.hspace>0?papa.hspace:0;
	}
	owShowTa('IMAGE_FORM'+idTa);
}
function insertImage(URL, side, border, margin, alt) { // insert image as specified
	owHideDialogs();
	if (!URL) URL=owFindObj("if_url"+idTa).value;
	if (URL) {
		if (!alt) alt=owFindObj("if_alt"+idTa).value ? owFindObj("if_alt"+idTa).value: URL.replace(/.*\/(.+)\..*/,"$1");
		img='<img alt="' + alt + '" src="' + URL +'" ';
		if (!side) side=owFindObj("if_side"+idTa).value;
		if ((side == "left") || (side == "right")) img += 'align="' + side + '"';
		if (!border)  border=owFindObj("if_border"+idTa).value;
		if (border.match(/^\d+$/)) border+='px solid';
		if (!margin) margin=owFindObj("if_margin"+idTa).value;
		if (margin.match(/^\d+$/)) margin+='px';
		if (border || margin) img += ' style="border:' + border + ';margin:' + margin + ';"';
		img += '/>';
		owInsHTML(img);
	}
}
function doTable(){ //show table controls if in a table, else make table
	if (trail && trail.indexOf('TABLE') > 0) owShowTa('TABLE_CONTROLS'+idTa);
		else owShowTa('TABLE_FORM'+idTa);
}
function doRow(toDo,below) { //insert or delete a table row
	var paNode=papa;
	while (paNode.tagName != "TR") paNode=paNode.parentNode;
	var tRow=paNode.rowIndex;
	var tCols=paNode.cells.length;
	while (paNode.tagName != "TABLE") paNode=paNode.parentNode;
	if (toDo == "delete") paNode.deleteRow(tRow);
	else {
		var newRow=paNode.insertRow(tRow+parseInt(below)); //1=below  0=above
			for (i=0; i < tCols; i++){
				var newCell=newRow.insertCell(i);
				newCell.innerHTML="#";
			}
	}
}
function doCol(toDo,after) { //insert or delete a column
	var paNode=papa;
	while (paNode.tagName != 'TD') paNode=paNode.parentNode;
	var tCol=paNode.cellIndex;
	while (paNode.tagName != "TABLE") paNode=paNode.parentNode;
	var tRows=paNode.rows.length;
	for (i=0; i < tRows; i++){
		if (toDo == "delete") paNode.rows[i].deleteCell(tCol);
		else {
			var newCell=paNode.rows[i].insertCell(tCol+parseInt(after)); //if after=0 then before
			newCell.innerHTML="#";
		}
	}
}
function makeTable() { //insert a table
	owHideDialogs();
	var rows=owFindObj('tf_rows'+idTa).value;
	var cols=owFindObj('tf_cols'+idTa).value;
	var border=owFindObj('tf_border'+idTa).value;
	var head=owFindObj('tf_head'+idTa).value;
	if ((rows > 0) && (cols > 0)) {
		var table='<table border="' + border + '">';
		for (var i=1; i <= rows; i++) {
			table=table + "<tr>";
			for (var j=1; j <= cols; j++) {
				if (i==1) {
				 if (head=="1") table += "<th>Title"+j+"</th>"; //Title1 Title2 etc.
				 else table += "<td>"+j+"</td>";
				}
				else if (j==1) table += "<td>"+i+"</td>";
			else table += "<td>#</td>";
			}
			table += "</tr>";
		}
		table += " </table>";
		owInsHTML(table);
	}
}
function doWin(URL) {  //popup  for browse function
	ppw=window.open(URL,'popWhizz','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=640,height=480,top=100');
	ppw.focus();
}
function spellCheck() {  //check spelling with ieSpell if available
	try {
		var axo=new ActiveXObject("ieSpell.ieSpellExtension");
		axo.CheckAllLinkedDocuments(document);
	} catch(e) {
		if(e.number==-2146827859) {
		if (confirm(owLangKey("iesp_is_7a")+'\n'+owLangKey("iesp_is_7b")))
				window.open("http://www.iespell.com/download.php","DownLoad");
		} else {
			alert("Error Loading ieSpell: Exception " + e.number);
		}
	}
}
function owCleanUp(idTa,gentleclean){  //clean up crud inserted by Micro$oft Orifice
	oW = owFindObj("whizzy"+idTa).contentWindow;
	oW.document.execCommand("removeformat",false,null);
	var h=oW.document.body.innerHTML;
	var vicious=false;
	if (gentleclean=="ask") vicious=confirm(owLangKey('remo_all_449'));
	if (!gentleclean || vicious){ 
	h=h.replace(/<\/?(SPAN|DEL|INS|U|DIR)[^>]*>/gi, "")
	.replace(/\b(CLASS|STYLE)=\"[^\"]*\"/gi, "")
	.replace(/\b(CLASS|STYLE)=\w+/gi, "");
	}
	if(!owFindObj('fontname'+idTa) || !owFindObj('fontsize'+idTa)) h=h.replace(/<\?FONT[^>]*>/gi, "");
	h=h.replace(/<\/?(FONT|SPAN|COL|XML|ST1|SHAPE|V:|O:|F:|F |PATH|LOCK|IMAGEDATA|STROKE|FORMULAS)[^>]*>/gi, "")
	.replace(/\bCLASS=\"?MSO\w*\"?/gi, "")
	.replace(/[ñ]/g,'-') //long ñ
	.replace(/[ëí]/g, "'") //single smartquotes ëí 
	.replace(/[ìî]/g, '"') //double smartquotes ìî
	.replace(/align="?justify"?/gi, "") //justify sends some browsers mad
	.replace(/<(TABLE|TD|TH|COL)(.*)(WIDTH|HEIGHT)=["'0-9A-Z]*/gi, "<$1$2") //no fixed size tables (%OK) [^A-Za-z>]
	.replace(/<([^>]+)>\s*<\/\1>/gi, ""); //empty tag
	oW.document.body.innerHTML=h;
}
function owHideDialogs() {
	owHideTa('LINK_FORM'+idTa);
	owHideTa('IMAGE_FORM'+idTa);
	owHideTa('COLOR_FORM'+idTa);
	owHideTa('TABLE_FORM'+idTa);
	owHideTa('TABLE_CONTROLS'+idTa);
}
function owShowDesign(idTa) {
	oW = owFindObj("whizzy"+idTa).contentWindow;
	oW.document.body.innerHTML=owTidyD(owFindObj(idTa).value);
	owHideTa(idTa);
	owHideTa('showWYSIWYG'+idTa);
	owShowTa('CONTROLS'+idTa);
	owShowTa('whizzy'+idTa);
	if(owFindObj("whizzy"+idTa).contentDocument) owFindObj("whizzy"+idTa).contentDocument.designMode="on"; //FF loses it on hide
	oW.focus();
}
function showHTML(idTa) { 
	oW = owFindObj("whizzy"+idTa).contentWindow;
	var t=(window.get_xhtml) ? get_xhtml(oW.document.body) : oW.document.body.innerHTML;
 	owFindObj(idTa).value=owTidyH(t);
	owHideTa('CONTROLS'+idTa);
	owHideTa('whizzy'+idTa);
	owShowTa(idTa);
	owShowTa('showWYSIWYG'+idTa);
	owFindObj(idTa).focus();
}

function syncTextarea() { //tidy up before we go-go
	for (var i=0;i<whizzies.length;i++){
		var t=whizzies[i];
		var b=owFindObj("whizzy"+t).contentWindow.document.body;
		if (owFindObj(t).style.display == 'block') b.innerHTML=owFindObj(t).value;
		b.innerHTML=owTidyH(b.innerHTML);
		var r=(owFindObj(t).nodeName!="TEXTAREA") ? owFindObj('wzhid_'+owFindObj(t).id) : owFindObj(t);
		r.value=(window.get_xhtml) ? get_xhtml(b) : b.innerHTML;
		r.value=r.value.replace(location.href+'#','#'); //IE anchor bug
	}
}
function owTidyD(h){ //FF designmode likes <B>,<I>...
	h=h.replace(/<(\/?)strong([^>]*)>/gi, "<$1B$2>"); 
	h=h.replace(/<(\/?)em([^>]*)>/gi, "<$1I$2>");
	return h;
}
function owTidyH(h){ //...but <B>,<I> deprecated
	h=h.replace(/<(\w+)[^>]*>\s*<\/\1>/gi, ""); //empty tag <([^>]+)>\s*<\/\1>
	h=h.replace(/(<\/?)[Bb]>/g, "$1strong>");
	h=h.replace(/(<\/?)[Ii]>/g, "$1em>");
	h=h.replace(/(<\/?)[Bb](\s+[^>]*)>/g, "$1strong$2>");
	h=h.replace(/(<\/?)[Ii](\s+[^>]*)>/g, "$1em$2>");
	h=h.replace(location.href+'#','#'); //IE anchor bug
	return h;
}
function owKb_handler(e) { // keyboard controls for Moz
	if (e && (e.ctrlKey && e.keyCode == e.DOM_VK_V)||(e.shiftKey && e.keyCode == e.DOM_VK_INSERT))
		{setTimeout('owCleanUp()',10);}
	if (e && e.keyCode==13 && papa.nodeName=="BODY") makeSo("formatblock","<p>");
	if (e && e.ctrlKey) {
		var k=String.fromCharCode(e.charCode).toLowerCase();
		var cmd=(k=='b')?'bold':(k=='i')?'italic':(k=='u')?'underline':(k=='l')?'link':(k=='m')?'image':'';
		if (cmd) {
			makeSo(cmd, true);
			e.preventDefault();  // stop the event bubble
			e.stopPropagation();
		}
	}
}
function owDoTag(html, idTa) { // insert HTML into text area
	objTa = owFindObj(idTa);
	var url;
	if (!html) html=prompt(owLangKey('ente_some_95')+":", "");
	objTa.focus();
	if (html == '<a>') {
		url=prompt(owLangKey("link_addr_58")+':',"http://");
		html='<a href="'+url+'">';
	}
	if (html == '<img>') {
		//if (!owUnbasedLinks) {
		url=prompt(owLangKey("imag_addr_17")+":","http://");
		var alt=prompt(owLangKey("alte_text_71"));
		html ='<img src="'+url+'" alt="'+alt+'">';
	}
	var close='';
	if (html.indexOf('<') == 0 && html.indexOf('br') != 1 && html.indexOf('img') != 1)
		close=html.replace(/<([a-z0-6]+).*/,"<\/$1>");
	if (html != '<strong>' && html != '<em>') close += '\n';
	if (document.selection) {
		sel=document.selection.createRange();
		sel.text=html+sel.text+close;
	} else {
			before=objTa.value.slice(0,objTa.selectionStart);
			sel=objTa.value.slice(objTa.selectionStart,objTa.selectionEnd);
			after=objTa.value.slice(objTa.selectionEnd);
			objTa.value =before+html+sel+close+after;
	}
	objTa.focus(); 
}
function owInsHTML(html) { //insert HTML at current selection
	if (!html) html=prompt(owLangKey('ente_some_95')+":", "");
	if (html.indexOf('js:') == 0) {
		try{eval(html.replace(/^js:/,''))} catch(e){};
		return;
		}
	try { 	 
		if(sel.type && sel.type!="Text") sel="";
		oW.document.execCommand("inserthtml", false, html + sel); 
		}
	catch (e) { 
		if (document.selection) { 
			if(papa && papa.nodeName == 'IMG') {papa.outerHTML=html;}
			else if(rng) {rng.select(); rng.pasteHTML(html+rng.htmlText);}
			} 
		}
	owWhereAmI();
	}
function owWhereAmI(e){//070322
	if (!e) var e=window.event;
	if (window.getSelection){
		sel=oW.getSelection();
		papa=(e && e.type=='mouseup') ? e.target : (sel.anchorNode.nodeName == '#text') ? sel.anchorNode.parentNode : sel.anchorNode;
		} 
	else { 
		sel=oW.document.selection;
		rng=sel.createRange();
		papa=(e && e.type=='mouseup')? e.srcElement : (sel.type == "Control") ? rng.item(0) : rng.parentElement();
		}
	var paNode=papa;
	trail=papa.nodeName; 
	while (!paNode.nodeName.match(/^(HTML|BODY)/) && paNode.className!="wzCtrl") {
		paNode=paNode.parentNode;
		trail=paNode.nodeName + '>' + trail;
		}
	if (paNode.className=="wzCtrl") trail=sel=rng=null;
	var id=paNode.nodeName=="HTML" ? paNode.getElementsByTagName("BODY")[0].id : paNode.id.replace("CONTROL","");
	owSetCurrent(id); 
	window.status=id+":"+trail;
	if (trail.indexOf('TABLE') > 0) owShowTa('TABLE_CONTROLS'+idTa); else owHideTa('TABLE_CONTROLS'+idTa);
	}
function owSetCurrent(id) {//set current whizzy
	if (id=="" || whizzies.join().indexOf(id)=='-1') return;
	if (id!=idTa){
		idTa=id;
		try {oW=owFindObj("whizzy"+id).contentWindow;} catch(e){alert('set current: '+id);}
		if (oW) {oW.focus();window.status=oW.document.body.id; }
	}
} 
function owTextSel() { if (sel  && sel.type != "None") return true;  else {alert(owLangKey("sele_some_94")); return false;}}
function owShowTa(id) {owFindObj(id).style.display='block';} //show element
function owHideTa(id) {owFindObj(id).style.display='none';} //hide element
function owFindObj(id) { if (typeof(id)=='object') return id; else return document.getElementById(id); } //get element by ID
function owWriteDoc(str) { return document.write(str); } //document write
function whiztextAdd(str) { return str; } //document write
function whiztextAdd2Obj(obj,str) { obj.innerHTML += str; } //document write
function whiztextAdd2ObjHTML(strobj,str) { return strobj + str; } //document write
function owInnerHtml(obj,str) { obj.innerHTML+=str; } //document write
function owLangKey(key) { 
	return (owLanguageTranslation[key])?owLanguageTranslation[key][owLangIndex]:key;
	}
//function owLangKey(key) { return (window.language && language[key]) ? language[key] :  key;} //translationfunction w(str) { return document.write(str); } //document write

var owLangCollect = new Array();

var re = /\bopenwysiwyg\b/;
var iniOpenWysiwygLoaded = false;
function iniOpenWysiwyg() {
	if (!iniOpenWysiwygLoaded) {
		owLangIndex = 0;
		for (j=0;j<owLanguages.length;j++) if (owLanguages[j]==owLanguage) { owLangIndex=j;break; }
		var textareas = document.getElementsByTagName("textarea");
		for (i=0;i<textareas.length;i++) {
			if (textareas[i].className=='openwysiwyg') makeWhizzyWig(textareas[i], owButtons);
			}
		}
	iniOpenWysiwygLoaded = true;
	if (typeof(owUnbasedLinks)=='undefined') owUnbasedLinks = false;
	}

if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) window.attachEvent("onload", iniOpenWysiwyg);
else window.addEventListener('load',iniOpenWysiwyg,true);

