// CREDITS: widgEdit.js  by: Cameron Adams (http://www.themaninblue.com/)

var widgStylesheet = "../css/wed_iframe.css"
var widgSelectBlockOptions = 
["", "Formato testo:",
"<h1>", "Titolo 1",
"<h2>", "Titolo 2",
"<h3>", "Titolo 3",
"<h4>", "Titolo 4",
"<h5>", "Titolo 5",
"<h6>", "Titolo 6",
"<p>", "Paragrafo"]

var widgInsertParagraphs = false
var widgAutoClean = false

//END CONFIGURATION


function widgInit(id)
{	var t=new widgEditor(id)
	top.FF.setFld("WED_FRM","W",top.gtP("Token"))
	t.switchMode()
	return t
}

function widgEditor(campo)
{	var p1=top.gtPull("DIV_WED_FRM")  // su Pull
	var p2=top.$("DIV_WED_SUBMIT")
	if(p1 && p2) p2.innerHTML=p1.innerHTML
	top.WedUpdateInput= function(){return t.updateWidgInput()}
	top.ifWed=0 
	var t=this
    t.id=campo
	t.locked = false
	t.pasteCache = ""
	t.theTextarea =  $("MEX_CORPO_A")
	t.theContainer = $("MEX_CORPO_C")
	t.theIframe =	 $("MEX_CORPO_I")
	t.divBottoniera= $("MEX_CORPO_T")
	t.divBottoniera.innerHTML=""
	t.theToolbar=new widgToolbar(t)
	t.divBottoniera.appendChild(t.theToolbar.theList)
	t.wysiwyg = false
	var c=top.gtPull(campo)
	t.getIfrBody().innerHTML=""
	t.theTextarea.value=""
	if (c)
	{	t.theTextarea.value=c.value
		t.getIfrBody().innerHTML=c.value
	}

	return 
}

function widgToolbar(theEd)
{	var t=this
	t.widgEditorObject = theEd
	var o=t.theList=document.createElement("ul")
	o.id = "WidgToolbar"
	o.className = "widgToolbar"
	o.widgToolbarObject = t
	t.addButton(o.id + "ButtonBold",		"widgButtonBold",		"Bold", "bold")
	t.addButton(o.id + "ButtonItalic",	"widgButtonItalic",		"Italic", "italic")
	t.addButton(o.id + "ButtonLink",		"widgButtonLink",		"Hyperlink", "link")
	t.addButton(o.id + "ButtonUnordered","widgButtonUnordered",	"Unordered List", "insertunorderedlist")
	t.addButton(o.id + "ButtonOrdered",	"widgButtonOrdered",	"Ordered List", "insertorderedlist")
	t.addButton(o.id + "ButtonImage",	"widgButtonImage",		"Insert Image", "image")
	t.addButton(o.id + "ButtonHTML",		"widgButtonHTML",		"HTML Source", "html")
	return t;
}


widgEditor.prototype.cleanPaste = function()
{	var matchedHead = ""
	var matchedTail = ""
	var newContent = this.getIfrBody().innerHTML
	var newContentStart = 0
	var newContentFinish = 0
	var z = ""
	var tempNode = document.createElement("div")

	/* Find start of both strings that matches */
	for (newContentStart = 0; newContent.charAt(newContentStart) == this.pasteCache.charAt(newContentStart); newContentStart++)
	{	matchedHead += this.pasteCache.charAt(newContentStart)
	}
	
	/* If newContentStart is inside a HTML tag, move to opening brace of tag */
	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()

	/* Find end of both strings that matches */
	for (newContentFinish = 0; newContent.charAt(newContentFinish) == this.pasteCache.charAt(newContentFinish); newContentFinish++)
	{
		matchedTail += this.pasteCache.charAt(newContentFinish)
	}

	/* If newContentFinish is inside a HTML tag, move to closing brace of tag */
	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 there's no difference in pasted content */
	if (newContentStart == newContent.length - newContentFinish)return false;

	newContent = newContent.reverse();
	z = newContent.substring(newContentStart, newContent.length - newContentFinish)
	z = z.validTags()

	/* bold > strong */
	z = z.replace(/<b(\s+|>)/g, "<strong$1")
	z = z.replace(/<\/b(\s+|>)/g, "</strong$1")

	/* Replace italic > em */
	z = z.replace(/<i(\s+|>)/g, "<em$1")
	z = z.replace(/<\/i(\s+|>)/g, "</em$1")

	/* Strip out unaccepted attributes */
	z = z.replace(/<[^>]*>/g, function(match)
	{	match = match.replace(/ ([^=]+)="[^"]*"/g, function(match2,a){return (a=="alt"||a=="href"||a=="src"||a=="title")?match2:"";})
		return match
	});

	tempNode.innerHTML = z
	acceptableChildren(tempNode)
	
	var x=matchedHead + tempNode.innerHTML + matchedTail
	x=x.replace(/<\?xml[^>]*>/g, "")
	x=x.replace(/<[^ >]+:[^>]*>/g, "")
	x=x.replace(/<\/[^ >]+:[^>]*>/g, "")

	this.theTextarea.value=x
	this.refreshDisplay()
	if (!isIE)this.convertSPANs(true)
	return true
}

widgEditor.prototype.getIfrBody = function()
{  return this.getIfrDoc().getElementsByTagName("body")[0]
}
widgEditor.prototype.getIfrDoc = function()
{  return this.theIframe.contentWindow.document
}

/* Clean the HTML code of the content area */
widgEditor.prototype.cleanSource = function()
{	var x = ""
	if (this.wysiwyg)x = this.getIfrBody().innerHTML
	else			 x = this.theTextarea.value

	x = x.validTags()
	
	/* Remove leading and trailing whitespace */
	x = x.replace(/^\s+/, "")
	x = x.replace(/\s+$/, "")
	
	/* Remove style attribute inside any tag */
	 x = x.replace(/img style/g, "img stxle")
	 x = x.replace(/ style="[^"]*"/g, "")
	 x = x.replace(/img stxle/g, "img style")

	/* Replace improper BRs */
	//x = x.replace(/<br>/g, "<br />")
	
	/* Remove BRs right before the end of blocks */
	//x = x.replace(/<br \/>\s*<\/(h1|h2|h3|h4|h5|h6|li|p)/g, "</$1")
	
	/* Replace improper IMGs */
	//x = x.replace(/(<img [^>]+[^\/])>/g, "$1 />")
	
	/* Remove empty tags */
	//x = x.replace(/(<[^\/]>|<[^\/][^>]*[^\/]>)\s*<\/[^>]*>/g, "")

	
	if (this.wysiwyg)	this.getIfrBody().innerHTML = x
	this.theTextarea.value=x

	return true;
}


widgEditor.prototype.convertSPANs = function(theSwitch)
{	
//----------------
	return true

	var d=this.getIfrDoc()
	if (theSwitch)
	{	/* Replace styled spans with their semantic equivalent */
		var theSPANs = d.getElementsByTagName("span")
		while(theSPANs.length > 0)
		{	var C = [];
			var e = null; //theReplacementElement
			var p = null;//theParentElement
			for (var j=0;j<theSPANs[0].childNodes.length;j++)C.push(theSPANs[0].childNodes[j].cloneNode(true));
			/* Detect type of span style */
			switch (theSPANs[0].getAttribute("style"))
			{		case "font-weight: bold;":
					e = d.createElement("strong")
					p = e
					break
				
				case "font-style: italic;":
					e = d.createElement("em")
					p = e
					break
					
				case "font-weight: bold; font-style: italic;":
					p = d.createElement("em")
					e = d.createElement("strong")
					e.appendChild(p)
					break
					
				case "font-style: italic; font-weight: bold;":
					p = d.createElement("strong")
					e = d.createElement("em")
					e.appendChild(p)
					break;
					
				default:
					replaceNodeWithChildren(theSPANs[0])
					break
			}
			if (e != null)
			{	for (var j=0;j<C.length;j++)p.appendChild(C[j])
				theSPANs[0].parentNode.replaceChild(e, theSPANs[0])
			}
			theSPANs = d.getElementsByTagName("span")
		}
	}
	else
	{	/* Replace em and strong tags with styled spans */
		var theEMs = d.getElementsByTagName("em")
		while(theEMs.length>0)
		{	var C =[]
			var theSpan = d.createElement("span")
			
			theSpan.setAttribute("style", "font-style: italic;")
			for (var j=0;j<theEMs[0].childNodes.length; j++)C.push(theEMs[0].childNodes[j].cloneNode(true))
				for (var j=0;j<C.length; j++)	theSpan.appendChild(C[j])
			theEMs[0].parentNode.replaceChild(theSpan, theEMs[0])
			theEMs = d.getElementsByTagName("em")
		}
		var theSTRONGs = d.getElementsByTagName("strong")
		while(theSTRONGs.length>0)
		{	var C =[];
			var theSpan = d.createElement("span");
			theSpan.setAttribute("style", "font-weight: bold;")
			for (var j=0;j<theSTRONGs[0].childNodes.length; j++)C.push(theSTRONGs[0].childNodes[j].cloneNode(true))
			for (var j=0;j<C.length;j++)	theSpan.appendChild(C[j])
			theSTRONGs[0].parentNode.replaceChild(theSpan, theSTRONGs[0])
			theSTRONGs = d.getElementsByTagName("strong")
		}
	}
	return true;
}


widgEditor.prototype.detectPaste = function(e)
{	e=e||event;
	if (e.ctrlKey && e.keyCode==86 && this.wysiwyg)
	{	var self = this
		this.pasteCache = this.getIfrBody().innerHTML
		setTimeout(function(){self.cleanPaste(); return true;}, 100)
	}
	return true
}

widgEditor.prototype.initEdit = function()
{	var t = this;
	try{t.getIfrDoc().designMode="on"}
	catch (e)
	{   setTimeout(function(){t.initEdit()},250)
		return false
	}
	setTimeout(function(){t.getIfrBody().innerHTML=t.theTextarea.value},100)
	
	if(!isIE)this.convertSPANs(false)
	
	var d=t.getIfrDoc()

	/* Mozilla event capturing */
	if (typeof document.addEventListener == "function")
	{
		d.addEventListener("mouseup", function(){widgToolbarCheckState(t); return true;}, false)
		d.addEventListener("keyup",   function(){widgToolbarCheckState(t); return true;}, false)
		d.addEventListener("keydown", function(e){t.detectPaste(e); return true;}, false)
	}
	/* IE event capturing */
	else
	{	d.attachEvent("onmouseup", function(){widgToolbarCheckState(t); return true;})
		d.attachEvent("onkeyup",   function(){widgToolbarCheckState(t); return true;})
		d.attachEvent("onkeydown", function(e){t.detectPaste(e); return true;}, false)
	}
	t.locked = false
	return true
}

/* Add elements to a paragraph and inserts the paragraph before a given element in the body */
widgEditor.prototype.insertNewParagraph = function(elementArray, succeedingElement)
{	var b=this.getIfrBody()
	var theParagraph=this.getIfrDoc().createElement("p")
	for (var i=0;i<elementArray.length;i++)theParagraph.appendChild(elementArray[i])
	if (typeof(succeedingElement)!="undefined")b.insertBefore(theParagraph, succeedingElement)
	else		b.appendChild(theParagraph)
	return true
}

/* Format the HTML with paragraphs. Any parentless text is enclosed in a paragraph, double breaks are paragraph markers */
widgEditor.prototype.paragraphise = function()
{	if (widgInsertParagraphs && this.wysiwyg)
	{	var b=this.getIfrBody()
		/* Remove all text nodes containing just whitespace */
		var cN=b.childNodes
		for (var i=0;i<cN.length; i++)
		{
			if (cN[i].nodeName.toLowerCase()=="#text"&&cN[i].data.search(/^\s*$/) != -1)
			{
				b.removeChild(cN[i])
				i--
			}
		}
		var removedElements=[];
		for (var i=0;i<cN.length; i++)
		{	if (cN[i].nodeName.isInlineName())
			{
				removedElements.push(cN[i].cloneNode(true))
				b.removeChild(cN[i])
				i--
			}
			else if (cN[i].nodeName.toLowerCase() == "br")
			{	if (i+1<cN.length)
				{
					/* If the current break tag is followed by another break tag */
					if (cN[i+1].nodeName.toLowerCase() == "br")
					{
						/* Remove consecutive break tags */
						while (i<cN.length && cN[i].nodeName.toLowerCase()=="br")b.removeChild(cN[i])
						if(removedElements.length>0)
						{
							this.insertNewParagraph(removedElements, cN[i])
							removedElements = []
						}
					}
					/* If the break tag appears before a block element */
					else if(!cN[i + 1].nodeName.isInlineName())b.removeChild(cN[i])
					else if (removedElements.length>0)
					{
						removedElements.push(cN[i].cloneNode(true))
						b.removeChild(cN[i])
					}
					else b.removeChild(cN[i])
					i--
				}
				else b.removeChild(cN[i])
			}
			else if(removedElements.length>0)
			{
				this.insertNewParagraph(removedElements, cN[i])
				removedElements = []
			}
		}
		if(removedElements.length>0)this.insertNewParagraph(removedElements)
	}
	return true
}


widgEditor.prototype.refreshDisplay = function()
{   if (this.wysiwyg)this.getIfrBody().innerHTML=this.theTextarea.value
	return true
}

widgEditor.prototype.updateWidgInput = function()
{	if (this.wysiwyg)
	{	if (!isIE)	this.convertSPANs(false)
		this.paragraphise()
		this.cleanSource()
	}
	return true
}

/* Add button to toolbar */
widgToolbar.prototype.addButton = function(theID, theClass, theLabel, theAction)
{
	var m=document.createElement("li")
	var a=document.createElement("a")
	var t=document.createTextNode(theLabel)
	m.id=theID
	m.className = "widgEditButton"
	a.href = "#"
	a.title = theLabel
	a.className = theClass
	a.action = theAction
	a.onclick = widgToolbarAction
	//a.onmouseover = widgToolbarMouseover
	a.appendChild(t)
	m.appendChild(a)
	this.theList.appendChild(m)
	return true
}



widgToolbar.prototype.setDisabled=function(bDisable)
{	var l=this.theList
	if(bDisable) l.className+= " widgSource"
	else l.className = l.className.replace(/ widgSource/, "");
	return true
}

/* Change the status of the selected toolbar item */
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 s = document.getElementById(this.theList.id + "SelectBlock")
		if (s!=null)
		{	s.value = ""
			s.value = theStatus
		}
	}
	return true	
}

function widgToolbarAction()
{	var theToolbar = this.parentNode.parentNode.widgToolbarObject
	var theWidgEditor = theToolbar.widgEditorObject
	var theIframe = theWidgEditor.theIframe
	var theSelection = ""
	/* If somehow a button other than "HTML source" is clicked while viewing HTML source, ignore click */	
	if (!theWidgEditor.wysiwyg && this.action != "html")
	{	return false
	}
	switch (this.action)
	{	case "formatblock":
			getIfrDoc().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"))
			{
				getIfrDoc().execCommand("Unlink", false, null)
				theWidgEditor.theToolbar.setState("Link", "off")
			}
			else
			{	var msg="Seleziona il testo su cui vuoi inserire il link."
				if (getIfrDoc().selection)
				{
					theSelection = getIfrDoc().selection.createRange().text
					if(theSelection==""){alert(msg);break}
				}
				else
				{	theSelection = theIframe.contentWindow.getSelection()
					if (theSelection==""){alert(msg);break}
				}

				var theURL = prompt("Inserisci l'URL per questo link:", "http://");

				if (theURL != null)
				{	getIfrDoc().execCommand("CreateLink", false, theURL)
					theWidgEditor.theToolbar.setState("Link", "on")
				}
			}
			break
			
		case "image":
			var theImage = prompt("Inserisci l'URL dell'immagine:", "")
			
			if (theImage != null && theImage != "")
			{
				var theAlt = prompt("Inserisci il testo alternativo per l'immagine:", "")
				var theSelection = null
				var theRange = null
				
				/* IE selections */
				if (getIfrDoc().selection)
				{
					/* Escape quotes in alt text */
					theAlt = theAlt.replace(/"/g, "'")
					theSelection = getIfrDoc().selection;
					theRange = theSelection.createRange()
					theRange.collapse(false)
					theRange.pasteHTML("<img alt=\"" + theAlt + "\" src=\"" + theImage + "\" />")
					break
				}
				/* Mozilla selections */
				else
				{	try	{theSelection = theIframe.contentWindow.getSelection();}
					catch (e)
					{return false;}

					theRange = theSelection.getRangeAt(0)
					theRange.collapse(false)
					var theImageNode = getIfrDoc().createElement("img")
					theImageNode.src = theImage
					theImageNode.alt = theAlt
					theRange.insertNode(theImageNode)
					break
				}
			}
			else return false
		default:
			try //2008
			{	theIframe.contentWindow.document.execCommand(this.action, false, null)
				var theAction = this.action.replace(/^./, function(match){return match.toUpperCase();})
				/* Turn off unordered toolbar item if ordered toolbar item was activated */	
				if (this.action == "insertorderedlist")
				{
					theAction = "Ordered"
					theWidgEditor.theToolbar.setState("Unordered", "off")
				}
				/* Turn off ordered toolbar item if unordered toolbar item was activated */	
				if (this.action == "insertunorderedlist")
				{
					theAction = "Unordered"
					theWidgEditor.theToolbar.setState("Ordered", "off")
				}
				/* If toolbar item was turned on */
				if (theIframe.contentWindow.document.queryCommandState(this.action, false, null))
				{
					theWidgEditor.theToolbar.setState(theAction, "on")
				}
				else	theWidgEditor.theToolbar.setState(theAction, "off")
				}
			catch(e){}
			
	}
	if (theWidgEditor.wysiwyg == true)theIframe.contentWindow.focus()
	else theWidgEditor.theTextarea.focus()
	return false	
}


/* Check the nesting of the current cursor position/selection */
function widgToolbarCheckState(theWidgEditor, resubmit)
{	if (!resubmit)
	{
		/* Allow browser to update selection before using the selection */
		setTimeout(function(){widgToolbarCheckState(theWidgEditor, true); return true;}, 500)
	}
	var theSelection = null
	var theRange = null
	var theParentNode = null
	var theLevel = 0;
	/* Turn off all the buttons */
	var menuListItems = theWidgEditor.theToolbar.theList.childNodes
	for (var i=0;i<menuListItems.length; i++)
	{	menuListItems[i].className = menuListItems[i].className.removeClass("on")
	}
	/* IE selections */
	if (theWidgEditor.getIfrDoc().selection)
	{	theSelection = theWidgEditor.getIfrDoc().selection
		theRange = theSelection.createRange()
		try
		{	theParentNode = theRange.parentElement()
		}
		catch (e)
		{	return false
		}
	}
	/* Mozilla selections */
	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 "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		
}

/* Turn off browser status display for toolbar items */
function widgToolbarMouseover(e){window.status = "";return false}

function acceptableChildren(theNode)
{	var C = theNode.childNodes;
	for (var i=0;i<C.length;i++)
	{	if (!C[i].nodeName.isAcceptedElementName())
		{	if (!C[i].nodeName.isInlineName())
			{	if (theNode.nodeName.toLowerCase() == "p")
				{
					acceptableChildren(replaceNodeWithChildren(theNode))
					return true
				}
				changeNodeType(C[i], "p")
			}
			else replaceNodeWithChildren(C[i])
			i=-1
		}
	}
	for (var i=0;i<C.length;i++)acceptableChildren(C[i])
	return true
}

/* Change the type of a node, e.g. h3 to p */
function changeNodeType(n,tip)
{
	var C=[]
	var nn=document.createElement(tip)
	var p=n.parentNode
	if (p!=null)for (var i=0;i<n.childNodes.length;i++)C.push(n.childNodes[i].cloneNode(true))
	for(var i=0;i<C.length; i++)nn.appendChild(C[i])
	p.replaceChild(nn,n)
	return true
}

/* Replace a node with its children -- delete the item and move its children up one level in the hierarchy */
function replaceNodeWithChildren(theNode)
{	var C=[];
	var theParent=theNode.parentNode
	if (theParent != null)
	{	for (var i=0;i<theNode.childNodes.length;i++)C.push(theNode.childNodes[i].cloneNode(true))
		for (var i=0;i< C.length; i++)theParent.insertBefore(C[i], theNode)
		theParent.removeChild(theNode)
		return theParent
	}
	return true
}

//string prototypes

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 rS="(^| )"+theClass+"\W*"
	var r=new RegExp(rS)
	if (r.test(this))return true
	return false
}

String.prototype.isAcceptedElementName = function()
{	var eL=["#text", "a", "em", "h1", "h2", "h3", "h4", "h5", "h6", "img", "li", "ol", "p", "strong", "ul"]
	var t=this.toLowerCase()
	for(var i=0;i<eL.length;i++)if(t==eL[i])return true
	return false
}

String.prototype.isInlineName = function()
{	var eL=["#text", "a", "em", "font", "span", "strong", "u"]
	var t=this.toLowerCase()
	for(var i=0;i<eL.length; i++)if(t==eL[i])return true
	return false
}

String.prototype.removeClass = function(cl)
{	var r=new RegExp("(^| )"+cl+"\W*")
	return this.replace(r, "")
}

String.prototype.reverse = function()
{	var t=""
	for (var i=this.length-1;i>=0;i--)t+= this.charAt(i)
	return t
}

String.prototype.validTags = function()
{	var t=this
	t=t.replace(/<[^> ]*/g, function(match){return match.toLowerCase();});
	t=t.replace(/<[^>]*>/g, function(match){
		match = match.replace(/ [^=]+=/g, function(match2){return match2.toLowerCase()})
		return match})		
	t=t.replace(/<[^>]*>/g, function(match){match = match.replace(/( [^=]+=)([^"][^ >]*)/g, "$1\"$2\"")
		return match})
	return t
}

widgEditor.prototype.switchMode = function()
{	var t=this
	try
	{	if (!t.locked)
		{	t.locked = true
			if (t.wysiwyg)
			{	t.updateWidgInput()
				t.theIframe.style.visibility='hidden'
				t.theTextarea.style.visibility='visible'
				t.theIframe.style.display='none'
				t.theTextarea.style.display='block'
			}
			else
			{	t.theTextarea.style.display='none'
				t.theTextarea.style.visibility='hidden'
				t.theIframe.style.display='block'
				t.theIframe.style.visibility='visible'
		 		t.initEdit()
				t.theIframe.contentWindow.focus()
				
			}
			t.wysiwyg=!t.wysiwyg
			setTimeout(function(){t.theToolbar.setDisabled(!t.wysiwyg);},200)
		}
	}
	catch(e){}
	t.locked = false
	return true
}

/*
29 settembre ---- da finire
function SalvaAnnot(tipo,campo)  //P=personale R=richiesta 
{	
	if(!campo) campo="MEX_CORPO"
	var o=top.DX_wed
	var t=o.theTextarea.value
	if (o.wysiwyg)
	{	if(window.WedUpdateInput)WedUpdateInput()
		o.updateWidgInput()
		t=o.getIfrBody().innerHTML
	} 
	else (o.switchMode())
	if (t && Trim(t)) 
	{	var x=Trim(t)
		var wl=window.location
		var fp=wl.protocol+'//'+wl.hostname+wl.pathname
		while (x.indexOf(fp)>0) x=x.replace(fp,'') 
		top.FF.setFld("WED_FRM","TIPO",escape(tipo))
		top.FF.setFld("WED_FRM","campo",x)
		top.FF.setFld("WED_FRM","W",top.gtP("Token"))
	    top.FF.setFld("WED_FRM","ID","RECORDID")
	
	 	top.$("LOADING_WED").style.display='block'
		top.$("LOADING_WED").innerHTML="<span color=red><img src=img/loading.gif border=0> "+n+"</span>"
		nascondiDash()
		top.document.WED_FRM.submit()
	} 
	else popERR((tipo=='T')?"La risposta":"L'annotazione"+"è vuota.")
 
}
*/
