function showHide(eId)
{
	if(typeof(eId) == "object")
	{
		var o = eId;
		if(o){ if(o.className == 'hide' || o.className == '') o.className = 'inline';	else o.className = 'hide';}
	}
	else
	{
		if(document.getElementById)
		{
			var o = document.getElementById(eId)
			if(o){ if(o.className == 'hide' || o.className == '') o.className = 'inline';	else o.className = 'hide';}
		}
	}
}

function showHideStyle(eId)
{
	if(typeof(eId) == "object")
	{
		var o = eId;
		if(o){ if(o.style.display == 'none' || o.style.display == '') o.style.display = 'block';	else o.style.display = 'none';}
	}
	else
	{
		if(document.getElementById)
		{
			var o = document.getElementById(eId)
			if(o) {	if(o.style.display == 'none' || o.style.display == '') o.style.display = 'block'; else o.style.display = 'none'; }
		}
	}
}

function doAjax(url, containerid)
{
	if (!document.getElementById)
		return;
	var _div = document.getElementById(containerid);
	if(!_div)
		return;

	showHide(containerid);
	
	//alert(_div.style.display);
	if(_div.className == 'inline')
	{
		_div.innerHTML = "<div class='attendere'><img src='images/clessidra.gif' align='absmiddle' alt='Attendere prego' border='0'>&nbsp;Caricamento in corso...</div>";
		//alert(url);
		//alert(containerid);
		ajaxpage(url, containerid);
	}
}

//richiede include ajax.js
function Anteprima(link)
{
	//controlliamo se esiste la textarea
	var _textarea = document.forms[0]["txtBody"];
	if(!_textarea)
	{
		alert("Oggetto textarea, testo non trovato!");
		return;
	}
	
	//recuperiamo i dati
	var testo = trim(_textarea.value);
	if(testo.length == 0)
	{
		alert("Scrivi il messaggio!");
		return;
	}
	
	//misteri della fede allora ie ed opera gestiscono l'accapo con \r\n
	//firefox invece passa solo \n
	//prima levo eventuali accapo di IE/Opera e li riporto al \n e poi
	//li trasformo nuovamente come ie vuole perchè comodo poi da gestire con Enviroment.Newline
	testo = testo.replace( /\r\n/g, "\n" );
	testo = testo.replace( /\n/g, "\r\n" );
	testo = encodeURIComponent(testo);
	
	var poststr = "t=" + testo;
	//alert(poststr);
	
	//div contenitore per la funzione callback
	if (!document.getElementById)
		return;
	var _div = document.getElementById("Anteprima");
	if(!_div)
		return;
	
	showHide(_textarea);
	//alert(_textarea.className);
	showHideStyle("Anteprima");
	
	if(_textarea.className == 'hide')
	{
		link.innerHTML  = "Modifica";
		_div.innerHTML = "<div class='attendere'><img src='images/clessidra.gif' align='absmiddle' alt='Attendere prego' border='0'>&nbsp;Caricamento in corso...</div>";
		ajaxpagepost("anteprima.aspx", "Anteprima", poststr);
		
		//ajaxpage("anteprima.aspx?" + poststr, "Anteprima");
	}
	else
		link.innerHTML  = "Anteprima";
}

//richiede include ajax.js
//si occupa di chiamare la pagina per aggiunta/rimozione dalla blacklist
function DoBlackList(m_id, nEncode64)
{
	//div contenitore per la funzione callback
	if (!document.getElementById)
		return;
	var _span = document.getElementById("bl" + m_id);
	if(!_span)
		return;
	
	var _removeFromBlackList = false;
	for(i=0;i<_span.childNodes.length;i++)
	{
		if(_span.childNodes[i].nodeName == "IMG")
		{
			var _imgName = getImgName(_span.childNodes[i].src);
			//alert(_imgName);
			if(_imgName.indexOf("_on") > 0)
				_removeFromBlackList = true;
			break;
		}
	}
	
	var _msg = "Attenzione, stai per ";
	if(_removeFromBlackList)
		_msg += "rimuovere il nickname dalla blacklist ";
	else
		_msg += "aggiungere il nickname alla blacklist ";
	_msg += "\nSei sicuro?";
	
	if(confirm(_msg))
	{
		var _url = "BlackList.aspx?n64=" + nEncode64;
		if(_removeFromBlackList)
			_url += "&remove=1";
		else
			_url += "&remove=0";
		_span.innerHTML = "<div class='attendere'><img src='images/clessidra.gif' align='absmiddle' alt='Attendere prego' border='0'></div>";
		
		ajaxpage(_url, _span.id);
	}
}

function doAjaxTree_old(img, url, containerid)
{
	//alert(getImgName(img.src));
	//alert(mid);
	
	var _imgName = getImgName(img.src);
	var _newImgName = _imgName.substring(4);
	if(_imgName.substring(0,4) == "open")
	{
		//alert("open " + containerid);
		
		if(document.getElementById)
		{
			var o = document.getElementById(containerid)
			if(o) 
			{	
				//mostro il div
				o.style.display = 'block'; 
				//attendere
				o.innerHTML = "<div class='attendere'><img src='images/clessidra.gif' align='absmiddle' alt='Attendere prego' border='0'>&nbsp;Caricamento in corso...</div>";
				
				//cambio img
				img.src = "images/clos" + _newImgName;
				img.alt = "Chiudi";
				
				//chiamata ajax
				//alert("chiamo ajax: " + url);
				ajaxpage(url, containerid);
			}
			
		}
	}
	else
	{
		//alert("chiudo " + containerid);
		
		if(document.getElementById)
		{
			var o = document.getElementById(containerid)
			if(o) 
			{	
				//nascondo il div
				o.style.display = 'none'; 
				
				//cambio img
				img.src = "images/open" + _newImgName;
				img.alt = "Apri";
			}
		}
	}
}

//inizializza l'albero con la url richiesta
function InitAjaxTree(url, containerid)
{
	if(document.getElementById)
	{
		var o = document.getElementById(containerid)
		if(o) 
		{	
			//mostro il div
			o.style.display = 'block'; 
			//attendere
			o.innerHTML = "<div class='attendere'><img src='images/clessidra.gif' align='absmiddle' alt='Attendere prego' border='0'>&nbsp;Caricamento in corso...</div>";
				
			//chiamata ajax
			//alert("chiamo ajax: " + url);
			ajaxpage(url, containerid);
		}
	}
}


//torna il nome del file a fine url
function getImgName(source) 
{
	var _directory = source.substring(0, source.lastIndexOf('/') + 1);
	//alert(_directory);
    return source.substring(_directory.lastIndexOf(_directory) + _directory.length);
}


//decrementa il contatore del form di posting
function textCounter(field, countfield, maxlimit) 
{
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
	// otherwise, update 'characters left' counter
	else 
		countfield.value = maxlimit - field.value.length;
}

//formattazione emoticon
function formatta(startTag,defaultText,endTag,replace)
{
	if(document.getElementsByTagName)
	{
		elements = document.getElementsByTagName("textarea")
			
		if(elements.length == 1)
		{
			//alert(startTag);
			//alert(defaultText);
			//alert(endTag);
	
			t = elements[0];
			//debug(t);
			if(t.createTextRange) 
			{
				t.focus(t.caretPos);
				t.caretPos = document.selection.createRange().duplicate();
				if(t.caretPos.text.length>0) 
				{
					var sel = t.caretPos.text;
					var fin = '';
					while(sel.substring(sel.length-1, sel.length)==' ') 
					{
						sel = sel.substring(0, sel.length-1)
						fin += ' ';
					}
					if(replace) t.caretPos.text = startTag + sel + endTag + fin;
					else t.caretPos.text = sel + fin + startTag + defaultText + endTag;
				} 
				else 
				{
					t.caretPos.text = startTag+defaultText+endTag;
				}
			}
			else 
			{
				//MOZILLA/NETSCAPE support
				if (t.selectionStart || t.selectionStart == "0") 
				{
					var startPos = t.selectionStart;
					var endPos = t.selectionEnd;

					if (startPos != endPos) 
					{
						if(startTag.length > 0)
						{
							t.value = t.value.substring(0, startPos)
			              		+	startTag
					            + t.value.substring(startPos, endPos) 
				              	+ endTag
				              	+ t.value.substring(endPos, t.value.length);
						}
						else
						{
							t.value = t.value.substring(0, endPos)
				              + endTag
				              + t.value.substring(endPos, t.value.length);
						}
					}
					else
					{
						t.value = 	t.value.substring(0, startPos)
									+ endTag
									+ t.value.substring(endPos, t.value.length);
	
						t.selectionStart = (t.value.substring(0, startPos) + endTag).length;
						t.selectionEnd = (t.value.substring(0, startPos) + endTag).length;
					}
				} 
				else 
				{
					t.value += startTag+defaultText+endTag;
				}
			}
		}
	}
}

function RedirectToIframe()
{
	//alert(top.location.search);
	//alert(top.location);
	//alert(top.location.hash);
	//alert(top.location.pathname);
	//var _newUrl = "b.aspx" + top.location.search + "#post";
	var _newUrl = top.location.pathname + top.location.search + "#post";
	
	//alert(_newUrl);
	top.location = _newUrl;
	return false;
}

function RefreshCaptcha()
{
	if(document.getElementById)
	{
		var o = document.getElementById('imgcaptcha');
		if(o)
			o.src = "captcha.aspx?" + new Date().getTime().toString();
	}
}

function ltrim(argvalue) 
{
	while (1) 
	{
		if (argvalue.substring(0, 1) != " ")
			break;
		argvalue = argvalue.substring(1, argvalue.length);
	}
	return argvalue;
}

function rtrim(argvalue) 
{
	while (1) 
	{
		if (argvalue.substring(argvalue.length - 1, argvalue.length) != " ")
			break;
		argvalue = argvalue.substring(0, argvalue.length - 1);
	}
	return argvalue;
}

function trim(argvalue) 
{
	var tmpstr = ltrim(argvalue);
	return rtrim(tmpstr);
}

function debug(obj)
{
	obj = eval(obj);
	var temp = "";
	for (x in obj)
	{
		temp += x + ": " + obj[x] + "\n";
		alert (x + ": " + obj[x] + "\n");
	}
	alert (temp);
}