function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

function getSel()
{
	var txt = '';
	var foundIn = '';
	if (self.framechattesto.document.getSelection)
	{
		txt = self.framechattesto.document.getSelection();
		foundIn = 'parent.framechattesto.document.getSelection()';
		return rtrim(txt);
	}
	else if (self.framechattesto.document.selection)
	{
		txt = self.framechattesto.document.selection.createRange().text;
		foundIn = 'self.framechattesto.document.selection.createRange()';
		return rtrim(txt);
	}
	else return "";
}

function ImpostaComando(tag0, tag1)
{
	var content = getSel();
	if (content == null) content = "Destinatario";
	
	content = tag0+content+tag1;
	return content;
}

function sussurro()
{
	var content = getSel();
	if (content == null) content = "Destinatario";
	
	content = "$"+content+"$";
	return content;
}

function toggleVisibility(id, NNtype, IEtype, WC3type, e)
{
	if (document.getElementById)
	{
        eval("document.getElementById(id).style.visibility = \"" + WC3type + "\"");
    }
	else
	{
        if (document.layers)
		{
            document.layers[id].visibility = NNtype;
        }
		else
		{
            if (document.all)
			{
				eval("document.all." + id + ".style.visibility = \"" + IEtype + "\"");
            }
        }
    }
}

function toggleVisibilityAndMove(id, NNtype, IEtype, WC3type, e)
{
	if (document.all) {
		xPos = event.clientX + document.body.scrollLeft;
		yPos = event.clientY + document.body.scrollTop;
	} else {
		xPos = 10;
		yPos = 10;
	}

	if (document.getElementById) {
        eval("document.getElementById(id).style.visibility = \"" + WC3type + "\"");
        eval("document.getElementById(id).style.left = " + xPos);
        eval("document.getElementById(id).style.top = " + yPos);
    } else {
        if (document.layers) {
            document.layers[id].visibility = NNtype;
            document.layers[id].left = xPos;
            document.layers[id].top = yPos;
        } else {
            if (document.all) {
				eval("document.all." + id + ".style.visibility = \"" + IEtype + "\"");
                eval("document.all." + id + ".style.left = " + xPos);
                eval("document.all." + id + ".style.top = " + yPos);
            }
        }
    }
}

function ImpostaSussurro()
{
	parent.iframeinput.document.invio.Messaggio.value=ImpostaComando('@','@');
	parent.iframeinput.document.invio.Messaggio.focus();
	return 1;
}

function ImpostaDenaro()
{
	parent.iframeinput.document.invio.Messaggio.value=ImpostaComando('$','$');
	parent.iframeinput.document.invio.Messaggio.focus();
	return 1;
}

function ImpostaDadi()
{
	parent.iframeinput.document.invio.Messaggio.value="%dadi 10";
	parent.iframeinput.document.invio.Messaggio.focus();
	return 1;
}

function ImpostaAspetto()
{
	toggleVisibility('divaspetto','show','visible','visible');
}

function closeballoon(id)
{
	parent.document.getElementById(id).style.left = -500 + "px";
}

function closedialog(id, detach)
{
	parent.document.getElementById(id).style.left = -1000 + "px";
	parent.document.getElementById("iframedialog").src = "loading.html";
}

function opendialog(id, x, y, link)
{
	var el = parent.document.getElementById("iframedialog"); // da rivedere
	el.src = link;

	parent.document.getElementById('dialoglink');
	parent.document.getElementById('dialoglink').href = link;
	parent.document.getElementById('dialoglink').target="_blank";

	parent.document.getElementById(id).style.left = parseInt(x) + "px";
	parent.document.getElementById(id).style.top = parseInt(y) + "px";
	
	return false;
}
