/*
 * Opens new window named wName containing wUri with scrollbars and
 * resizable by default. To fix it, pass 0 as wScroll, wResizable.
 */
function openWin(wUri, wName, wWidth, wHeight, wScroll, wResizable) {
	var scrollBars = wScroll!=0 ? 1 : 0;
	var resizable = wResizable!=0 ? 1 : 0;
	var positionLeft = (screen.width - wWidth) / 2;
	var positionTop = (screen.height - wHeight) / 2;
	var rest = 'location=no,status=0,menubar=0,titlebar=0,toolbar=0,directories=0,hotkeys=0';
	var myW = window.open(wUri, wName, 'width='+wWidth+',height='+wHeight+',top='+positionTop+',left='+positionLeft+',resizable='+resizable+',scrollbars='+scrollBars+','+rest);
	try { myW.focus(); /* can fail if content loads long */ }
	catch(e) { myW.onload = function(e) { this.focus(); }; }
	return false;
}

function htmlEncode(str) {             
	var v = String(str);
	v = v.replace(/&/g, '&amp;').replace(/</g, '&lt;');
	v = v.replace(/>/g, '&gt;').replace(/"/g, '&quot;');
	return v;
}

function htmlDecode(str) {             
	var v = String(str);
	v = v.replace(/&quot;/g, '"').replace(/&gt;/g, '>');
	v = v.replace(/&lt;/g, '<').replace(/&amp;/g, '&');
	return v;
}

function getFirstElementByClassName(node, classname)  {
	if (!node) return null;
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");
	for (var i=0, j=els.length; i<j; i++)
        	if (re.test(els[i].className))
			return els[i];
	return null;
}

