
var bHtmlMode = false;
var str_iFrameDoc = (document.all)? "document.frames(\"Composition\").document\;": "document.getElementById(\"Composition\").contentDocument\;";

// Inizializzazione

function initialize() {
	 
	iFrameDoc = eval(str_iFrameDoc);
	
	/*
	var testo_inFrame ='';
	
	if (document.forms['InputForm'].elements['post_testo']!=null)
	testo_inFrame = document.forms['InputForm'].elements['post_testo'].value.replace(/[\r\n]/g, "");
	else if (document.forms['InputForm'].elements['testo']!=null)
	testo_inFrame = document.forms['InputForm'].elements['testo'].value.replace(/[\r\n]/g, "");
    else if (document.forms['InputForm'].elements['msg_testo']!=null)
	testo_inFrame = document.forms['InputForm'].elements['msg_testo'].value.replace(/[\r\n]/g, "");
	else
	testo_inFrame = document.forms['InputForm'].elements['contenuto_pg'].value.replace(/[\r\n]/g, "");
	*/
	testo_inFrame = document.forms['InputForm'].elements[elemFrame].value.replace(/[\r\n]/g, "");
	
	regeXp = new RegExp('\\[YT[^\\]]*\\](.*?)\\[/YT\\]', 'gmi');
    testo_inFrame = testo_inFrame.replace(regeXp,function(x,y){return "[YT]"+y.replace(/>/gmi,"&gt;").replace(/</gmi, "&lt;")+"[/YT]";});
	
	iFrameDoc.open();
	iFrameDoc.write("<html><head><style> P { margin:0px; padding:0px;} </style></head><body bgcolor=\"#FFFFFF\" style=\"margin:3px;\">" + testo_inFrame + "</body></html>");
	iFrameDoc.close();
	iFrameDoc.designMode = "On";
	
} 


function Cambia() {
	
	document.forms['InputForm'].elements['categoria'].selectedIndex = 0;
}


// Porta il focus al riquadro di testo
function setFocus() {
if (document.all)
	document.frames("Composition").focus();
else
	document.getElementById('Composition').contentWindow.focus()
return;
}




// Controlla se la toolbar è abilitata nella modalità testo
function validateMode() {
	if (! bHtmlMode)
		return true;
	alert("Deselezionare \"Visualizza HTML\" per utilizzare le barre degli strumenti");
	
	setFocus();
	return false;
}


// Formatta il testo
function formatC(what,opt) {
	if (!validateMode())
		return;
		
	iFrameDoc = eval(str_iFrameDoc);

	if(opt=='carica'){

		if(what=='InsertImage'){
		  mesg = "Inserisci l'url dell'immagine:";
		  cox = 'http://';
		}else if(what=='CreateLink'){
		  mesg = "Inserisci l'url:";
		  cox = 'http://';
		}else if(what=='CreateEmail'){
		  mesg = "Inserisci l'indirizzo email:";
		  cox = 'mailto:';
		  what='CreateLink';
		}else{
		  mesg = "Inserisci il codice:";
		  cox = '';
		}
		
        imagePath = prompt(mesg, cox);
        if ((imagePath != null) && (imagePath != "")) {
        iFrameDoc.execCommand(what, false, imagePath);
		}
		
	}else{
        iFrameDoc.execCommand(what,false,opt);
	}

	setFocus();
	
}

function setFocus1() {
if (document.all)
	document.frames("Riporta").focus();
else
	document.getElementById('Riporta').contentWindow.focus()
return;
}


function EditAddObj(classe){
	iFrameDoc = eval(str_iFrameDoc);
	riquadro = iFrameDoc.body;
	
	formatC('fontsize',0)
	var recTxt = riquadro.innerHTML.replace(/[\r\n]/g, "");
	reXp = new RegExp('<font size="0"[^>]*>(.*?)</font>', 'gmi');
	var extraText = reXp.exec(recTxt);
	if(extraText==null){
		formatC('RemoveFormat');
		code = prompt('Copia il codice:', '');
		if (code != null){
		formatC('InsertImage','delete')
		reXp1 = new RegExp('<img src="delete">', 'gmi');
		code=code.replace(/</gmi, "&lt;");
		code=code.replace(/>/gmi, "&gt;");
		riquadro.innerHTML = riquadro.innerHTML.replace(reXp1,'['+classe+']'+code+'[/'+classe+']')
		}
	}else{
		var context = extraText[1];
	   if(classe=='YT' | classe=='FLASH')
	      riquadro.innerHTML = recTxt.replace(reXp,'['+classe+']'+context+'[/'+classe+']')
	   else
	      riquadro.innerHTML = recTxt.replace(reXp,'<span class="'+classe+'">'+context+'</span>')
	}


}



//Scambia tra la modalità testo e la modalità HTML.
function setMode(newMode) {
	var testo;
	
	bHtmlMode = newMode;
	
	iFrameDoc = eval(str_iFrameDoc);
	riquadro = iFrameDoc.body;
	
	if (document.all) {
		if (bHtmlMode) {
			testo = riquadro.innerHTML;
			riquadro.innerText = testo; 
		} else {
			testo = riquadro.innerText;
			riquadro.innerHTML = testo;
		}
		
	} else if(document.getElementById && document.createTextNode) {
		if (bHtmlMode) {
			testo = document.createTextNode(riquadro.innerHTML);
			riquadro.innerHTML = "";
			riquadro.appendChild(testo);
		} else {
			testo = document.createRange();
			testo.selectNodeContents(riquadro);
			riquadro.innerHTML = testo.toString();
		}	
	}

	setFocus();
}

function addEvent(el, evType, fn, useCapture){
   if (el.addEventListener){
     el.addEventListener(evType, fn, useCapture); 
   } else if (el.attachEvent){
     el.attachEvent('on' + evType, fn);
   } else {
     el['on' + evType] = fn;
   }
}

addEvent(window,'load',initialize);
