//Code from: http://www.pat-burt.com/web-development/how-to-do-a-css-popup-without-opening-a-new-window/
//And: http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html

function toggle(div_id) {
	var el = document.getElementById(div_id);
	if ( el.style.display == 'none' ) {	el.style.display = 'block';}
	else {el.style.display = 'none';}
}
function blanket_size(popUpDivVar, popHeight) {
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	var blanket = document.getElementById('blanket');
	blanket.style.height = blanket_height + 'px';
	var popUpDiv = document.getElementById(popUpDivVar);

	//Use code from: http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
	//Change from:
	//popUpDiv_height=blanket_height/2-150;//150 is half popup's height
	//Change to START:
	popUpDiv_height = f_scrollTop() + viewportheight / 2 - (popHeight / 2); 

	//Change END
	
	popUpDiv.style.top = popUpDiv_height + 'px';
}

//Change to START:
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
    var n_result = n_win ? n_win : 0;
    if (n_docel && (!n_result || (n_result > n_docel)))
        n_result = n_docel;
    return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
//Change END

function window_pos(popUpDivVar, popWidth) {
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	var popUpDiv = document.getElementById(popUpDivVar);
	window_width = window_width / 2 - (popWidth / 2); 
	popUpDiv.style.left = window_width + 'px';
}
function popup(windowname, popWidth, popHeight) {
    blanket_size(windowname, popHeight);
    window_pos(windowname, popWidth);
	toggle('blanket');
	toggle(windowname);		
}
