var userAgent = navigator.userAgent.toLowerCase();
var is_opera  = (userAgent.indexOf('opera') != -1);
var is_saf    = ((userAgent.indexOf('applewebkit') != -1) || (navigator.vendor == 'Apple Computer, Inc.'));
var is_webtv  = (userAgent.indexOf('webtv') != -1);
var is_ie     = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
var is_ie4    = ((is_ie) && (userAgent.indexOf('msie 4.') != -1));
var is_moz    = ((navigator.product == 'Gecko') && (!is_saf));
var is_kon    = (userAgent.indexOf('konqueror') != -1);
var is_ns     = ((userAgent.indexOf('compatible') == -1) && (userAgent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4    = ((is_ns) && (parseInt(navigator.appVersion) == 4));
var is_mac    = (userAgent.indexOf('mac') != -1);

var pointer_cursor = (is_ie ? 'hand' : 'pointer');

function array_pop(a)
{
	if (typeof a != 'object' || !a.length)
	{
		return null;
	}
	else
	{
		var response = a[a.length - 1];
		a.length--;
		return response;
	}
}

function array_push(a, value)
{
	a[a.length] = value;
	return a.length;
}

function fetch_object(idname)
{
	if (document.getElementById)
	{
		return document.getElementById(idname);
	}
	else if (document.all)
	{
		return document.all[idname];
	}
	else if (document.layers)
	{
		return document.layers[idname];
	}
	else
	{
		return null;
	}
}

function fetch_tags(parentobj, tag)
{
	if (typeof parentobj.getElementsByTagName != 'undefined')
	{
		return parentobj.getElementsByTagName(tag);
	}
	else if (parentobj.all && parentobj.all.tags)
	{
		return parentobj.all.tags(tag);
	}
	else
	{
		return null;
	}
}

function do_an_e(eventobj)
{
	if (!eventobj || is_ie)
	{
		window.event.returnValue = false;
		window.event.cancelBubble = true;
		return window.event;
	}
	else
	{
		eventobj.stopPropagation();
		eventobj.preventDefault();
		return eventobj;
	}
}

function e_by_gum(eventobj)
{
	if (!eventobj || is_ie)
	{
		window.event.cancelBubble = true;
		return window.event;
	}
	else
	{
		if (eventobj.target.type == 'submit')
		{
			// naughty safari
			eventobj.target.form.submit();
		}
		eventobj.stopPropagation();
		return eventobj;
	}
}

function menu_register(controlid, noimage)
{
	if (typeof BBmenu == 'object')
	{
		BBmenu.register(controlid, noimage);
	}
}

function board_init()
{
	// don't bother doing any exciting stuff for WebTV
	if (is_webtv)
	{
		return false;
	}

	// finalize popup menus
	if (typeof BBmenu == 'object')
	{
		// close all menus on document click
		if (window.attachEvent && !is_saf)
		{
			document.attachEvent('onclick', bbmenu_hide);
			window.attachEvent('onresize', bbmenu_hide);
		}
		else if (document.addEventListener && !is_saf)
		{
			document.addEventListener('click', bbmenu_hide, false);
			window.addEventListener('resize', bbmenu_hide, false);
		}
		else
		{
			window.onclick = bbmenu_hide;
			window.onresize = bbmenu_hide;
		}

		// activate the menu system
		BBmenu.activate(true);
	}

	return true;
}

function mpFoto(img)
{
	foto1 = new Image();
	foto1.src = img;
	mpControl(img);
}

function mpControl(img)
{
	if ( (foto1.width != 0) && (foto1.height != 0) )
	{
		viewFoto(img);
	}
	else
	{
		mpFunc = "mpControl('"+img+"')";
		intervallo = setTimeout(mpFunc,20);
	}
}

function viewFoto(img)
{
	largh = foto1.width + 20;
	altez = foto1.height + 20;
	string = "width="+largh+",height="+altez;
	finestra = window.open(img, "", string);
}

function setCheckboxes(theForm, elementName, isChecked)
{
	var chkboxes = document.forms[theForm].elements[elementName];
	var count = chkboxes.length;
	if ( count )
	{
		for (var i = 0; i < count; i++)
		{
			chkboxes[i].checked = isChecked;
		}
	}
	else
	{
		chkboxes.checked = isChecked;
	}
	return true;
}

function Active(what)
{
	what.style.backgroundColor = '#F9F9F0';
}

function NotActive(what)
{
	what.style.backgroundColor = '';
}

var selectedText = quoteAuthor = '';
function quoteSelection()
{
	theSelection = false;
	if (selectedText) theSelection = selectedText;
	else if (document.selection && document.selection.createRange) theSelection = document.selection.createRange().text; // Get text selection

	if (theSelection)
	{
		// Add tags around selection
		emoticon( (document.post.message.value ? '\n' : '') + '[quote' + (quoteAuthor ? '="' + quoteAuthor + '"' : '') + ']' + theSelection + '[/quote]\n');
		document.post.message.focus();
		selectedText = quoteAuthor = theSelection = '';
		return;
	}
	else
	{
		alert('{L_NO_TEXT_SELECTED}');
	}
}

function toggleSpoiler(e)
{
	var node;
	if (!e) e = window.event;
	if (e.target) node = e.target;
	else if (e.srcElement) node = e.srcElement;
	if (node.nodeType == 3) // defeat Safari bug
		node = node.parentNode;
	while (node != document && node.nodeName != 'DL')
		node = node.parentNode;
	if (node == document) return;
	node = node.getElementsByTagName('dd')[0];
	node.className = (node.className == '') ? 'spoiler_visible' : '';
}

function displayWindow(url, width, height)
{
	var Win = window.open(url,"displayWindow",'width=' + width + ',height=' + height + ',resizable=1,scrollbars=no,menubar=no' );
}

function cellOver(what, color)
{
	if (!what.orgClassName) {
		what.orgClassName = what.className;
	}
	what.className = what.className == 'row1' ? 'row2' : 'row1';
//	what.style.backgroundColor = color;
	what.style.cursor = 'default';
}

function cellOut(what)
{
	what.className = what.orgClassName;
//	what.style.backgroundColor = '';
}

