var lastMouseX;
var lastMouseY;
var curPopupWindow = null;

function newWindow(page,w,h) {
        theWindow = window.open(page,'newWin','height=' + h + ',width=' + w + ',directories=0,location=0,menubar=0,scrollbars=1,status=1,toolbar=0,resizable=1');
        theWindow.focus();
}

function select_search(needle,obj)
{
	for (var i=0; i<obj.options.length; i++)
	{
		if (''+obj.options[i].value == ''+needle) { return i; }
	}
	return -1;
}

function setBWDates( element1, element2, adj)
{
	var d = new Date();
	var old_date = d.getDate();
	d.setDate(d.getDate() + adj);
	if( old_date > d.getDate() )
	{
		d.setMonth(d.getMonth() + 1);
		$(element2).selectedIndex = select_search(d.getMonth(), $(element2));
	}
	$(element1).selectedIndex = select_search(d.getDate(), $(element1));
}

function openPopup(url, name, pWidth, pHeight, features, snapToLastMousePosition) {
	openPopupFocus(url, name, pWidth, pHeight, features, snapToLastMousePosition, true);
}

function openPopupFocus(url, name, pWidth, pHeight, features, snapToLastMousePosition, closeOnLoseFocus) {
	closePopup();

	if (snapToLastMousePosition) {
		if (lastMouseX - pWidth < 0) {
			lastMouseX = pWidth;
		}
		if (lastMouseY + pHeight > screen.height) {
			lastMouseY -= (lastMouseY + pHeight + 50) - screen.height;
		}
		lastMouseX -= pWidth;
		lastMouseY += 10;
		features += "screenX=" + lastMouseX + ",left=" + lastMouseX + "screenY=" + lastMouseY + ",top=" + lastMouseY;
	}

	if (closeOnLoseFocus) {
		curPopupWindow = window.open(url, name, features, false);
		curPopupWindow.focus();
	} else {
		win = window.open(url, name, features, false);
		win.focus();
	}
}

function closePopup() {
	if (curPopupWindow != null) {
       
		if (!curPopupWindow.closed)
			curPopupWindow.close();
		
		curPopupWindow = null;
	}
}

function openWindow(url, winname) {
	if (winname == 'undefined') winname = 'popup';
	var popupWin = window.open(url, winname, 'toolbar=no,menubar=no,scrollbars=yes,resizable=yes,dependent,width=790,height=500,left=0,top=0');
	popupWin.focus();
}

function openPrint(url) {
	var printWin = window.open(url, 'printwin', 'toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes,dependent,width=600,height=550,left=0,top=0');
	printWin.focus();
}