/*
**    File: widgEdit.js
**    Created by: Cameron Adams (http://www.themaninblue.com/)
**    Created on: 2005-01-16
**    Last modified: 2008-03-01
**
**
**    License Information:
**    -------------------------------------------------------------------------
**    Copyright (C) 2008 Cameron Adams
**
**    This program is free software; you can redistribute it and/or modify it
**    under the terms of the GNU General Public License as published by the
**    Free Software Foundation; either version 2 of the License, or (at your
**    option) any later version.
**    
**    This program is distributed in the hope that it will be useful, but
**    WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
**    General Public License for more details.
**    
**    You should have received a copy of the GNU General Public License along
**    with this program; if not, write to the Free Software Foundation, Inc.,
**    59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
**    
*/
var widgStylesheet="/styles/widgContent.css";var widgToolbarItems=new Array();widgToolbarItems.push("bold");widgToolbarItems.push("italic");widgToolbarItems.push("strikethrough");widgToolbarItems.push("hyperlink");var widgInsertParagraphs=true;var widgAutoClean=true;run();function run(){var oldOnload=window.onload;if(typeof(window.onload)!="function"){window.onload=widgInit}else{window.onload=function(){oldOnload();widgInit()}}};function widgInit(){if(typeof(document.designMode)=="string"&&(document.all||document.designMode=="off")){var theTextareas=document.getElementsByTagName("textarea");for(var i=0;i<theTextareas.length;i++){var theTextarea=theTextareas[i];if(theTextarea.className.classExists("widgEditor")){if(theTextarea.id==""){theTextarea.id=theTextarea.name}setTimeout("new widgEditor('"+theTextarea.id+"')",500*(i))}}}else{return false}return true};function widgEditor(replacedTextareaID,userStyleSheet){var self=this;this.theTextarea=document.getElementById(replacedTextareaID);this.theContainer=document.createElement("div");this.theIframe=document.createElement("iframe");this.theInput=document.createElement("input");this.theExtraInput=document.createElement("input");this.IE=false;this.locked=true;this.pasteCache="";this.wysiwyg=true;if(userStyleSheet!=""&&typeof(userStyleSheet)!="undefined"){widgStylesheet=userStyleSheet}if(document.all){this.IE=true}if(this.theTextarea.id==null){this.theTextarea.id=this.theTextarea.name}this.theTextarea.style.visibility="hidden";this.theContainer.id=this.theTextarea.id+"WidgContainer";this.theContainer.className="widgContainer";this.theIframe.id=this.theTextarea.id+"WidgIframe";this.theIframe.className="widgIframe";this.theInput.type="hidden";this.theInput.id=this.theTextarea.id;this.theInput.name=this.theTextarea.name;this.theInput.value=this.theTextarea.value;this.theToolbar=new widgToolbar(this);this.theExtraInput.type="hidden";this.theExtraInput.id=this.theTextarea.id+"WidgEditor";this.theExtraInput.name=this.theTextarea.name+"WidgEditor";this.theExtraInput.value="true";this.theTextarea.id+="WidgTextarea";this.theTextarea.name+="WidgTextarea";this.theContainer.appendChild(this.theToolbar.theList);this.theContainer.appendChild(this.theIframe);this.theContainer.appendChild(this.theInput);this.theContainer.appendChild(this.theExtraInput);this.theContainer.style.visibility="hidden";this.theInput.widgEditorObject=this;this.theTextarea.parentNode.replaceChild(this.theContainer,this.theTextarea);this.writeDocument(this.theInput.value);this.initEdit();this.modifyFormSubmit();return true};widgEditor.prototype.cleanPaste=function(){if(widgAutoClean||confirm("Do you wish to clean the HTML source of the content you just pasted?")){var matchedHead="";var matchedTail="";var newContent=this.theIframe.contentWindow.document.getElementsByTagName("body")[0].innerHTML;var newContentStart=0;var newContentFinish=0;var newSnippet="";var tempNode=document.createElement("div");for(newContentStart=0;newContent.charAt(newContentStart)==this.pasteCache.charAt(newContentStart);newContentStart++){matchedHead+=this.pasteCache.charAt(newContentStart)}for(var i=newContentStart;i>=0;i--){if(this.pasteCache.charAt(i)=="<"){newContentStart=i;matchedHead=this.pasteCache.substring(0,newContentStart);break}else if(this.pasteCache.charAt(i)==">"){break}}newContent=newContent.reverse();this.pasteCache=this.pasteCache.reverse();for(newContentFinish=0;newContent.charAt(newContentFinish)==this.pasteCache.charAt(newContentFinish);newContentFinish++){matchedTail+=this.pasteCache.charAt(newContentFinish)}for(var i=newContentFinish;i>=0;i--){if(this.pasteCache.charAt(i)==">"){newContentFinish=i;matchedTail=this.pasteCache.substring(0,newContentFinish);break}else if(this.pasteCache.charAt(i)=="<"){break}}matchedTail=matchedTail.reverse();if(newContentStart==newContent.length-newContentFinish){return false}newContent=newContent.reverse();newSnippet=newContent.substring(newContentStart,newContent.length-newContentFinish);newSnippet=newSnippet.validTags();newSnippet=newSnippet.replace(/<b(\s+|>)/g,"<strong$1");newSnippet=newSnippet.replace(/<\/b(\s+|>)/g,"</strong$1");newSnippet=newSnippet.replace(/<i(\s+|>)/g,"<em$1");newSnippet=newSnippet.replace(/<\/i(\s+|>)/g,"</em$1");newSnippet=newSnippet.replace(/<[^>]*>/g,function(match){match=match.replace(/ ([^=]+)="[^"]*"/g,function(match2,attributeName){if(attributeName=="alt"||attributeName=="href"||attributeName=="src"||attributeName=="title"){return match2}return""});return match});tempNode.innerHTML=newSnippet;acceptableChildren(tempNode);this.theInput.value=matchedHead+tempNode.innerHTML+matchedTail;this.theInput.value=this.theInput.value.replace(/<\?xml[^>]*>/g,"");this.theInput.value=this.theInput.value.replace(/<[^ >]+:[^>]*>/g,"");this.theInput.value=this.theInput.value.replace(/<\/[^ >]+:[^>]*>/g,"");this.refreshDisplay();if(!this.IE){this.convertSPANs()}}return true};widgEditor.prototype.cleanSource=function(){var theHTML="";if(this.wysiwyg){theHTML=this.theIframe.contentWindow.document.getElementsByTagName("body")[0].innerHTML}else{theHTML=this.theTextarea.value}theHTML=theHTML.validTags();theHTML=theHTML.replace(/^\s+/,"");theHTML=theHTML.replace(/\s+$/,"");theHTML=theHTML.replace(/ style="[^"]*"/g,"");theHTML=theHTML.replace(/<br>/g,"<br />");theHTML=theHTML.replace(/<br \/>\s*<\/(h1|h2|h3|h4|h5|h6|li|p)/g,"</$1");theHTML=theHTML.replace(/(<img [^>]+[^\/])>/g,"$1 />");theHTML=theHTML.replace(/(<[^\/]>|<[^\/][^>]*[^\/]>)\s*<\/[^>]*>/g,"");if(this.wysiwyg){this.theIframe.contentWindow.document.getElementsByTagName("body")[0].innerHTML=theHTML}else{this.theTextarea.value=theHTML}this.theInput.value=theHTML;return true};widgEditor.prototype.convertSPANs=function(theSwitch){if(theSwitch){var theSPANs=this.theIframe.contentWindow.document.getElementsByTagName("span");while(theSPANs.length>0){var theChildren=new Array();var theReplacementElement=null;var theParentElement=null;for(var j=0;j<theSPANs[0].childNodes.length;j++){theChildren.push(theSPANs[0].childNodes[j].cloneNode(true))}switch(theSPANs[0].getAttribute("style")){case"font-weight: bold;":theReplacementElement=this.theIframe.contentWindow.document.createElement("strong");theParentElement=theReplacementElement;break;case"font-style: italic;":theReplacementElement=this.theIframe.contentWindow.document.createElement("em");theParentElement=theReplacementElement;break;case"font-weight: bold; font-style: italic;":theParentElement=this.theIframe.contentWindow.document.createElement("em");theReplacementElement=this.theIframe.contentWindow.document.createElement("strong");theReplacementElement.appendChild(theParentElement);break;case"font-style: italic; font-weight: bold;":theParentElement=this.theIframe.contentWindow.document.createElement("strong");theReplacementElement=this.theIframe.contentWindow.document.createElement("em");theReplacementElement.appendChild(theParentElement);break;case"text-decoration: line-through;":theReplacementElement=this.theIframe.contentWindow.document.createElement("strike");theParentElement=theReplacementElement;break;case"text-decoration: strike; font-weight: bold;":theParentElement=this.theIframe.contentWindow.document.createElement("strong");theReplacementElement=this.theIframe.contentWindow.document.createElement("strike");theReplacementElement.appendChild(theParentElement);break;case"font-style: bold; text-decoration: strike;":theParentElement=this.theIframe.contentWindow.document.createElement("strike");theReplacementElement=this.theIframe.contentWindow.document.createElement("strong");theReplacementElement.appendChild(theParentElement);break;case"text-decoration: strike; font-style: italic;":theParentElement=this.theIframe.contentWindow.document.createElement("em");theReplacementElement=this.theIframe.contentWindow.document.createElement("strike");theReplacementElement.appendChild(theParentElement);break;case"font-style: italic; text-decoration: strike;":theParentElement=this.theIframe.contentWindow.document.createElement("strike");theReplacementElement=this.theIframe.contentWindow.document.createElement("em");theReplacementElement.appendChild(theParentElement);break;case"font-style: italic; text-decoration: strike; font-weight:bold;":theParentElement=this.theIframe.contentWindow.document.createElement("em");theReplacementElement=this.theIframe.contentWindow.document.createElement("strike");theThirdReplacementElement=this.theIframe.contentWindow.document.createElement("strong");theReplacementElement.appendChild(theThirdReplacementElement);theReplacementElement.appendChild(theParentElement);break;case"font-style: italic; font-weight:bold; text-decoration: strike;":theParentElement=this.theIframe.contentWindow.document.createElement("em");theReplacementElement=this.theIframe.contentWindow.document.createElement("strong");theThirdReplacementElement=this.theIframe.contentWindow.document.createElement("strike");theReplacementElement.appendChild(theThirdReplacementElement);theReplacementElement.appendChild(theParentElement);break;case"text-decoration: strike; font-style: italic; font-weight:bold;":theParentElement=this.theIframe.contentWindow.document.createElement("strike");theReplacementElement=this.theIframe.contentWindow.document.createElement("em");theThirdReplacementElement=this.theIframe.contentWindow.document.createElement("strong");theReplacementElement.appendChild(theThirdReplacementElement);theReplacementElement.appendChild(theParentElement);break;case"text-decoration: strike; font-weight:bold; font-style: italic;":theParentElement=this.theIframe.contentWindow.document.createElement("strike");theReplacementElement=this.theIframe.contentWindow.document.createElement("strong");theThirdReplacementElement=this.theIframe.contentWindow.document.createElement("em");theReplacementElement.appendChild(theThirdReplacementElement);theReplacementElement.appendChild(theParentElement);break;case"font-weight:bold; font-style: italic; text-decoration: strike;":theParentElement=this.theIframe.contentWindow.document.createElement("strong");theReplacementElement=this.theIframe.contentWindow.document.createElement("em");theThirdReplacementElement=this.theIframe.contentWindow.document.createElement("strike");theReplacementElement.appendChild(theThirdReplacementElement);theReplacementElement.appendChild(theParentElement);break;case"font-weight:bold; text-decoration: strike; font-style: italic;":theParentElement=this.theIframe.contentWindow.document.createElement("strong");theReplacementElement=this.theIframe.contentWindow.document.createElement("strike");theThirdReplacementElement=this.theIframe.contentWindow.document.createElement("em");theReplacementElement.appendChild(theThirdReplacementElement);theReplacementElement.appendChild(theParentElement);break;default:replaceNodeWithChildren(theSPANs[0]);break}if(theReplacementElement!=null){for(var j=0;j<theChildren.length;j++){theParentElement.appendChild(theChildren[j])}theSPANs[0].parentNode.replaceChild(theReplacementElement,theSPANs[0])}theSPANs=this.theIframe.contentWindow.document.getElementsByTagName("span")}}else{var theEMs=this.theIframe.contentWindow.document.getElementsByTagName("em");while(theEMs.length>0){var theChildren=new Array();var theSpan=this.theIframe.contentWindow.document.createElement("span");theSpan.setAttribute("style","font-style: italic;");for(var j=0;j<theEMs[0].childNodes.length;j++){theChildren.push(theEMs[0].childNodes[j].cloneNode(true))}for(var j=0;j<theChildren.length;j++){theSpan.appendChild(theChildren[j])}theEMs[0].parentNode.replaceChild(theSpan,theEMs[0]);theEMs=this.theIframe.contentWindow.document.getElementsByTagName("em")}var theSTRONGs=this.theIframe.contentWindow.document.getElementsByTagName("strong");while(theSTRONGs.length>0){var theChildren=new Array();var theSpan=this.theIframe.contentWindow.document.createElement("span");theSpan.setAttribute("style","font-weight: bold;");for(var j=0;j<theSTRONGs[0].childNodes.length;j++){theChildren.push(theSTRONGs[0].childNodes[j].cloneNode(true))}for(var j=0;j<theChildren.length;j++){theSpan.appendChild(theChildren[j])}theSTRONGs[0].parentNode.replaceChild(theSpan,theSTRONGs[0]);theSTRONGs=this.theIframe.contentWindow.document.getElementsByTagName("strong")}}return true};widgEditor.prototype.detectPaste=function(e){var keyPressed=null;var theEvent=null;if(e){theEvent=e}else{theEvent=event}if(theEvent.ctrlKey&&theEvent.keyCode==86&&this.wysiwyg){var self=this;this.pasteCache=this.theIframe.contentWindow.document.getElementsByTagName("body")[0].innerHTML;setTimeout(function(){self.cleanPaste();return true},100)}return true};widgEditor.prototype.initEdit=function(){var self=this;try{this.theIframe.contentWindow.document.designMode="on"}catch(e){setTimeout(function(){self.initEdit()},250);return false}if(!this.IE){this.convertSPANs(false)}this.theContainer.style.visibility="visible";this.theTextarea.style.visibility="visible";if(typeof document.addEventListener=="function"){this.theIframe.contentWindow.document.addEventListener("mouseup",function(){widgToolbarCheckState(self);return true},false);this.theIframe.contentWindow.document.addEventListener("keyup",function(){widgToolbarCheckState(self);return true},false);this.theIframe.contentWindow.document.addEventListener("keydown",function(e){self.detectPaste(e);return true},false)}else{this.theIframe.contentWindow.document.attachEvent("onmouseup",function(){widgToolbarCheckState(self);return true});this.theIframe.contentWindow.document.attachEvent("onkeyup",function(){widgToolbarCheckState(self);return true});this.theIframe.contentWindow.document.attachEvent("onkeydown",function(e){self.detectPaste(e);return true},false)}this.locked=false;return true};widgEditor.prototype.insertNewParagraph=function(elementArray,succeedingElement){var theBody=this.theIframe.contentWindow.document.getElementsByTagName("body")[0];var theParagraph=this.theIframe.contentWindow.document.createElement("p");for(var i=0;i<elementArray.length;i++){theParagraph.appendChild(elementArray[i])}if(typeof(succeedingElement)!="undefined"){theBody.insertBefore(theParagraph,succeedingElement)}else{theBody.appendChild(theParagraph)}return true};widgEditor.prototype.modifyFormSubmit=function(){var self=this;var theForm=this.theContainer.parentNode;var oldOnsubmit=null;while(theForm.nodeName.toLowerCase()!="form"){theForm=theForm.parentNode}oldOnsubmit=theForm.onsubmit;if(typeof theForm.onsubmit!="function"){theForm.onsubmit=function(){return self.updateWidgInput()}}else{theForm.onsubmit=function(){self.updateWidgInput();return oldOnsubmit()}}return true};widgEditor.prototype.paragraphise=function(){if(widgInsertParagraphs&&this.wysiwyg){var theBody=this.theIframe.contentWindow.document.getElementsByTagName("body")[0];for(var i=0;i<theBody.childNodes.length;i++){if(theBody.childNodes[i].nodeName.toLowerCase()=="#text"&&theBody.childNodes[i].data.search(/^\s*$/)!=-1){theBody.removeChild(theBody.childNodes[i]);i--}}var removedElements=new Array();for(var i=0;i<theBody.childNodes.length;i++){if(theBody.childNodes[i].nodeName.isInlineName()){removedElements.push(theBody.childNodes[i].cloneNode(true));theBody.removeChild(theBody.childNodes[i]);i--}else if(theBody.childNodes[i].nodeName.toLowerCase()=="br"){if(i+1<theBody.childNodes.length){if(theBody.childNodes[i+1].nodeName.toLowerCase()=="br"){while(i<theBody.childNodes.length&&theBody.childNodes[i].nodeName.toLowerCase()=="br"){theBody.removeChild(theBody.childNodes[i])}if(removedElements.length>0){this.insertNewParagraph(removedElements,theBody.childNodes[i]);removedElements=new Array()}}else if(!theBody.childNodes[i+1].nodeName.isInlineName()){theBody.removeChild(theBody.childNodes[i])}else if(removedElements.length>0){removedElements.push(theBody.childNodes[i].cloneNode(true));theBody.removeChild(theBody.childNodes[i])}else{theBody.removeChild(theBody.childNodes[i])}i--}else{theBody.removeChild(theBody.childNodes[i])}}else if(removedElements.length>0){this.insertNewParagraph(removedElements,theBody.childNodes[i]);removedElements=new Array()}}if(removedElements.length>0){this.insertNewParagraph(removedElements)}}return true};widgEditor.prototype.refreshDisplay=function(){if(this.wysiwyg){this.theIframe.contentWindow.document.getElementsByTagName("body")[0].innerHTML=this.theInput.value}else{this.theTextarea.value=this.theInput.value}return true};widgEditor.prototype.switchMode=function(){if(!this.locked){this.locked=true;if(this.wysiwyg){this.updateWidgInput();this.theTextarea.value=this.theInput.value;this.theContainer.replaceChild(this.theTextarea,this.theIframe);this.theToolbar.disable();this.wysiwyg=false;this.locked=false}else{this.updateWidgInput();this.theContainer.replaceChild(this.theIframe,this.theTextarea);this.writeDocument(this.theInput.value);this.theToolbar.enable();this.initEdit();this.wysiwyg=true}}return true};widgEditor.prototype.updateWidgInput=function(){if(this.wysiwyg){if(!this.IE){this.convertSPANs(true)}this.paragraphise();this.cleanSource()}else{this.theInput.value=this.theTextarea.value}return true};widgEditor.prototype.writeDocument=function(documentContent){var documentTemplate='		<html>			<head>				INSERT:STYLESHEET:END			</head>			<body id="iframeBody">				INSERT:CONTENT:END			</body>		</html>	';if(typeof document.all!="undefined"){documentTemplate=documentTemplate.replace(/INSERT:STYLESHEET:END/,'<link rel="stylesheet" type="text/css" href="'+widgStylesheet+'"></link>')}else{documentTemplate=documentTemplate.replace(/INSERT:STYLESHEET:END/,"")}documentTemplate=documentTemplate.replace(/INSERT:CONTENT:END/,documentContent);this.theIframe.contentWindow.document.open();this.theIframe.contentWindow.document.write(documentTemplate);this.theIframe.contentWindow.document.close();if(typeof document.all=="undefined"){var stylesheet=this.theIframe.contentWindow.document.createElement("link");stylesheet.setAttribute("rel","stylesheet");stylesheet.setAttribute("type","text/css");stylesheet.setAttribute("href",widgStylesheet);this.theIframe.contentWindow.document.getElementsByTagName("head")[0].appendChild(stylesheet)}return true};function widgToolbar(theEditor){var self=this;this.widgEditorObject=theEditor;this.theList=document.createElement("ul");this.theList.id=this.widgEditorObject.theInput.id+"WidgToolbar";this.theList.className="widgToolbar";this.theList.widgToolbarObject=this;for(var i=0;i<widgToolbarItems.length;i++){switch(widgToolbarItems[i]){case"bold":this.addButton(this.theList.id+"ButtonBold","widgButtonBold","Bold","bold");break;case"italic":this.addButton(this.theList.id+"ButtonItalic","widgButtonItalic","Italic","italic");break;case"strikethrough":this.addButton(this.theList.id+"ButtonStrike","widgButtonStrike","Strikethrough","strikethrough");break;case"hyperlink":this.addButton(this.theList.id+"ButtonLink","widgButtonLink","Hyperlink","link");break;case"unorderedlist":this.addButton(this.theList.id+"ButtonUnordered","widgButtonUnordered","Unordered List","insertunorderedlist");break;case"orderedlist":this.addButton(this.theList.id+"ButtonOrdered","widgButtonOrdered","Ordered List","insertorderedlist");break;case"image":this.addButton(this.theList.id+"ButtonImage","widgButtonImage","Insert Image","image");break;case"htmlsource":this.addButton(this.theList.id+"ButtonHTML","widgButtonHTML","HTML Source","html");break;case"blockformat":this.addSelect(this.theList.id+"SelectBlock","widgSelectBlock",widgSelectBlockOptions,"formatblock");break}}return true};widgToolbar.prototype.addButton=function(theID,theClass,theLabel,theAction){var menuItem=document.createElement("li");var theLink=document.createElement("a");var theText=document.createTextNode(theLabel);menuItem.id=theID;menuItem.className="widgEditButton";theLink.href="#";theLink.title=theLabel;theLink.className=theClass;theLink.action=theAction;theLink.onclick=widgToolbarAction;theLink.onmouseover=widgToolbarMouseover;theLink.appendChild(theText);menuItem.appendChild(theLink);this.theList.appendChild(menuItem);return true};widgToolbar.prototype.addSelect=function(theID,theClass,theContentArray,theAction){var menuItem=document.createElement("li");var theSelect=document.createElement("select");menuItem.className="widgEditSelect";theSelect.id=theID;theSelect.name=theID;theSelect.className=theClass;theSelect.action=theAction;theSelect.onchange=widgToolbarAction;for(var i=0;i<theContentArray.length;i+=2){var theOption=document.createElement("option");var theText=document.createTextNode(theContentArray[i+1]);theOption.value=theContentArray[i];theOption.appendChild(theText);theSelect.appendChild(theOption)}menuItem.appendChild(theSelect);this.theList.appendChild(menuItem);return true};widgToolbar.prototype.disable=function(){this.theList.className+=" widgSource";for(var i=0;i<this.theList.childNodes.length;i++){var theChild=this.theList.childNodes[i];if(theChild.nodeName.toLowerCase()=="li"&&theChild.className=="widgEditSelect"){for(j=0;j<theChild.childNodes.length;j++){if(theChild.childNodes[j].nodeName.toLowerCase()=="select"){theChild.childNodes[j].disabled="disabled";break}}}}return true};widgToolbar.prototype.enable=function(){this.theList.className=this.theList.className.replace(/ widgSource/,"");for(var i=0;i<this.theList.childNodes.length;i++){var theChild=this.theList.childNodes[i];if(theChild.nodeName.toLowerCase()=="li"&&theChild.className=="widgEditSelect"){for(j=0;j<theChild.childNodes.length;j++){if(theChild.childNodes[j].nodeName.toLowerCase()=="select"){theChild.childNodes[j].disabled="";break}}}}return true};widgToolbar.prototype.setState=function(theState,theStatus){if(theState!="SelectBlock"){var theButton=document.getElementById(this.theList.id+"Button"+theState);if(theButton!=null){if(theStatus=="on"){theButton.className=theButton.className.addClass("on")}else{theButton.className=theButton.className.removeClass("on")}}}else{var theSelect=document.getElementById(this.theList.id+"SelectBlock");if(theSelect!=null){theSelect.value="";theSelect.value=theStatus}}return true};function widgToolbarAction(){var theToolbar=this.parentNode.parentNode.widgToolbarObject;var theWidgEditor=theToolbar.widgEditorObject;var theIframe=theWidgEditor.theIframe;var theSelection="";if(!theWidgEditor.wysiwyg&&this.action!="html"){return false}switch(this.action){case"formatblock":theIframe.contentWindow.document.execCommand(this.action,false,this.value);theWidgEditor.theToolbar.setState("SelectBlock",this.value);break;case"html":theWidgEditor.switchMode();break;case"link":if(this.parentNode.className.classExists("on")){theIframe.contentWindow.document.execCommand("Unlink",false,null);theWidgEditor.theToolbar.setState("Link","off")}else{if(theIframe.contentWindow.document.selection){theSelection=theIframe.contentWindow.document.selection.createRange().text;if(theSelection==""){alert("Please select the text you wish to hyperlink.");break}}else{theSelection=theIframe.contentWindow.getSelection();if(theSelection==""){alert("Please select the text you wish to hyperlink.");break}}var theURL=prompt("Enter the URL for this link:","http://");if(theURL!=null){theIframe.contentWindow.document.execCommand("CreateLink",false,theURL);theWidgEditor.theToolbar.setState("Link","on")}}break;case"image":var theImage=prompt("Enter the location for this image:","");if(theImage!=null&&theImage!=""){var theAlt=prompt("Enter the alternate text for this image:","");var theSelection=null;var theRange=null;if(theIframe.contentWindow.document.selection){theAlt=theAlt.replace(/"/g,"'");theSelection=theIframe.contentWindow.document.selection;theRange=theSelection.createRange();theRange.collapse(false);theRange.pasteHTML("<img alt=\""+theAlt+"\" src=\""+theImage+"\" />");break}else{try{theSelection=theIframe.contentWindow.getSelection()}catch(e){return false}theRange=theSelection.getRangeAt(0);theRange.collapse(false);var theImageNode=theIframe.contentWindow.document.createElement("img");theImageNode.src=theImage;theImageNode.alt=theAlt;theRange.insertNode(theImageNode);break}}else{return false}default:theIframe.contentWindow.document.execCommand(this.action,false,null);var theAction=this.action.replace(/^./,function(match){return match.toUpperCase()});if(this.action=="insertorderedlist"){theAction="Ordered";theWidgEditor.theToolbar.setState("Unordered","off")}if(this.action=="insertunorderedlist"){theAction="Unordered";theWidgEditor.theToolbar.setState("Ordered","off")}if(theIframe.contentWindow.document.queryCommandState(this.action,false,null)){theWidgEditor.theToolbar.setState(theAction,"on")}else{theWidgEditor.theToolbar.setState(theAction,"off")}}if(theWidgEditor.wysiwyg==true){theIframe.contentWindow.focus()}else{theWidgEditor.theTextarea.focus()}return false};function widgToolbarCheckState(theWidgEditor,resubmit){if(!resubmit){setTimeout(function(){widgToolbarCheckState(theWidgEditor,true);return true},500)}var theSelection=null;var theRange=null;var theParentNode=null;var theLevel=0;var menuListItems=theWidgEditor.theToolbar.theList.childNodes;for(var i=0;i<menuListItems.length;i++){menuListItems[i].className=menuListItems[i].className.removeClass("on")}if(theWidgEditor.theIframe.contentWindow.document.selection){theSelection=theWidgEditor.theIframe.contentWindow.document.selection;theRange=theSelection.createRange();try{theParentNode=theRange.parentElement()}catch(e){return false}}else{try{theSelection=theWidgEditor.theIframe.contentWindow.getSelection();theRange=theSelection.getRangeAt(0);theParentNode=theRange.commonAncestorContainer}catch(e){return false}}while(theParentNode.nodeType==3){theParentNode=theParentNode.parentNode}while(theParentNode.nodeName.toLowerCase()!="body"){switch(theParentNode.nodeName.toLowerCase()){case"a":theWidgEditor.theToolbar.setState("Link","on");break;case"em":theWidgEditor.theToolbar.setState("Italic","on");break;case"li":break;case"ol":theWidgEditor.theToolbar.setState("Ordered","on");theWidgEditor.theToolbar.setState("Unordered","off");break;case"span":if(theParentNode.getAttribute("style")=="font-weight: bold;"){theWidgEditor.theToolbar.setState("Bold","on")}else if(theParentNode.getAttribute("style")=="font-style: italic;"){theWidgEditor.theToolbar.setState("Italic","on")}else if(theParentNode.getAttribute("style")=="font-weight: bold; font-style: italic;"){theWidgEditor.theToolbar.setState("Bold","on");theWidgEditor.theToolbar.setState("Italic","on")}else if(theParentNode.getAttribute("style")=="font-style: italic; font-weight: bold;"){theWidgEditor.theToolbar.setState("Bold","on");theWidgEditor.theToolbar.setState("Italic","on")}break;case"strong":theWidgEditor.theToolbar.setState("Bold","on");break;case"strike":theWidgEditor.theToolbar.setState("Strike","on");break;case"ul":theWidgEditor.theToolbar.setState("Unordered","on");theWidgEditor.theToolbar.setState("Ordered","off");break;default:theWidgEditor.theToolbar.setState("SelectBlock","<"+theParentNode.nodeName.toLowerCase()+">");break}theParentNode=theParentNode.parentNode;theLevel++}return true}function widgToolbarMouseover(){window.status="";return true}function acceptableChildren(theNode){var theChildren=theNode.childNodes;for(var i=0;i<theChildren.length;i++){if(!theChildren[i].nodeName.isAcceptedElementName()){if(!theChildren[i].nodeName.isInlineName()){if(theNode.nodeName.toLowerCase()=="p"){acceptableChildren(replaceNodeWithChildren(theNode));return true}changeNodeType(theChildren[i],"p")}else{replaceNodeWithChildren(theChildren[i])}i=-1}}for(var i=0;i<theChildren.length;i++){acceptableChildren(theChildren[i])}return true}function changeNodeType(theNode,nodeType){var theChildren=new Array();var theNewNode=document.createElement(nodeType);var theParent=theNode.parentNode;if(theParent!=null){for(var i=0;i<theNode.childNodes.length;i++){theChildren.push(theNode.childNodes[i].cloneNode(true))}for(var i=0;i<theChildren.length;i++){theNewNode.appendChild(theChildren[i])}theParent.replaceChild(theNewNode,theNode)}return true};function replaceNodeWithChildren(theNode){var theChildren=new Array();var theParent=theNode.parentNode;if(theParent!=null){for(var i=0;i<theNode.childNodes.length;i++){theChildren.push(theNode.childNodes[i].cloneNode(true))}for(var i=0;i<theChildren.length;i++){theParent.insertBefore(theChildren[i],theNode)}theParent.removeChild(theNode);return theParent}return true};String.prototype.addClass=function(theClass){if(this!=""){if(!this.classExists(theClass)){return this+" "+theClass}}else{return theClass}return this};String.prototype.classExists=function(theClass){var regString="(^| )"+theClass+"\W*";var regExpression=new RegExp(regString);if(regExpression.test(this)){return true}return false};String.prototype.isAcceptedElementName=function(){var elementList=new Array("#text","a","em","h1","h2","h3","h4","h5","h6","img","li","ol","p","strong","ul","strike");var theName=this.toLowerCase();for(var i=0;i<elementList.length;i++){if(theName==elementList[i]){return true}}return false};String.prototype.isInlineName=function(){var inlineList=new Array("#text","a","em","font","span","strong","u","strike");var theName=this.toLowerCase();for(var i=0;i<inlineList.length;i++){if(theName==inlineList[i]){return true}}return false};String.prototype.removeClass=function(theClass){var regString="(^| )"+theClass+"\W*";var regExpression=new RegExp(regString);return this.replace(regExpression,"")};String.prototype.reverse=function(){var theString="";for(var i=this.length-1;i>=0;i--){theString+=this.charAt(i)}return theString};String.prototype.validTags=function(){var theString=this;theString=theString.replace(/<[^> ]*/g,function(match){return match.toLowerCase()});theString=theString.replace(/<[^>]*>/g,function(match){match=match.replace(/ [^=]+=/g,function(match2){return match2.toLowerCase()});return match});theString=theString.replace(/<[^>]*>/g,function(match){match=match.replace(/( [^=]+=)([^"][^ >]*)/g,"$1\"$2\"");return match});return theString};