/**
 * This javascript library is initially downloaded from
 * http://www.pat-burt.com/web-development/how-to-do-a-css-popup-without-opening-a-new-window/,
 * and ajusted by zzl for banq use.
 */

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)
{
	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);

	//popUpDiv_height = blanket_height/2-150; //150 is half popup's height
	var divHeight = popUpDiv.style.height.substr(0, popUpDiv.style.height.length -2);
	if(divHeight >= (document.body.parentNode.clientHeight - 20))
	{
		divHeight = document.body.parentNode.clientHeight - 20;
		//alert(divHeight);
		//popUpDiv.style.height = divHeight + 'px';
	}
	popUpDiv_height = (document.body.parentNode.clientHeight - divHeight)/2;
	popUpDiv_height = popUpDiv_height < 0 ? 0 : popUpDiv_height;

	popUpDiv.style.top = popUpDiv_height + 'px';
}

function window_pos(popUpDivVar)
{
	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);

	var divWidth = popUpDiv.style.width.substr(0, popUpDiv.style.width.length -2);
	window_width = window_width/2 - divWidth/2; //150 is half popup's width
	popUpDiv.style.left = window_width + 'px';
}

function popup(windowname)
{
	//var divWidth = divObj.style.width.substr(0, divObj.style.width.length -2);
	//var divHeight = divObj.style.height.substr(0, divObj.style.height.length -2);

	blanket_size(windowname);
	window_pos(windowname);
	toggle('blanket');
	toggle(windowname);
}

/* =========================================================================================
   The following functions are added by zzl
 ========================================================================================= */

/**
 * Close div popup.
 */
function closePopup()
{
	var forms = document.forms;
	if(forms)
	{
		for(var i=0; i<forms.length; i++)
		{
			var form = forms[i];
			for(var j=0; j<form.elements.length; j++)
			{
				if(form.elements[j].type.indexOf('select') >= 0)
				{
					form.elements[j].style.visibility = 'visible';
				}
			}
		}
	}

	popup('popupDiv');
	return false;
}

function getPopupDivHeight()
{
	// Get the height of the popup div section
	var popDivHeight = document.getElementById('popupDiv').style.height;
	var regex = /[^0-9]/;
	while(popDivHeight.match(regex))
	{
		popDivHeight = popDivHeight.replace(regex, '');
	}
	var popDivH = parseInt(popDivHeight);
	return popDivH;
}

/**
 * Display a standard (simple) div popup (that disables the background).
 * You need to create a hidden div named <code>popupDiv</code> in your page
 * and defined the width and height of the popup in <code>css style</code>.
 * See an example in dspace-source/ark/element-templage-2-*.jsp.
 *
 * @param title the title of the poupup
 * @param content the html code you want to display in the popup's main area.
 *
 */
function showStandardPopup(title, content)
{
	var popDivH = getPopupDivHeight();
	var titleH = 30;

	var html = '<center>';

	html = html + '<div width="99%" style="height:' + titleH + 'px; overflow: hidden;">';
	html = html + '<table width="100%" border="0" cellpadding="0" cellspacing="0" style="padding: 6px 6px 6px 8px; background-color: #c1cad5">';
	html = html + '<tr>';
	html = html + '<td align="left" valign="center" style="font: bold 12px Verdana;">';
	html = html + title;
	html = html + '</td>';
	html = html + '<td align="right" valign="center">';
	html = html + '<a href="#" onclick="return closePopup()">';
	html = html + '<img src="' + baseUrl + '/banq/image/close.gif" border="0"/>';
	html = html + '</a>';
	html = html + '</td>';
	html = html + '</tr>';
	html = html + '</table>';
	html = html + '</div>';
	html = html + '\n\n';

	html = html + '<div width="99%" style="height:' + (popDivH - titleH) + 'px; overflow-y: scroll; padding: 6px 6px 6px 6px; text-align: left">';
	html = html + content;
	html = html + '</div>';
	html = html + '</center>';

	//document.getElementById('objForm').style.display = 'none';
	var forms = document.forms;
	if(forms)
	{
		for(var i=0; i<forms.length; i++)
		{
			var form = forms[i];
			for(var j=0; j<form.elements.length; j++)
			{
				if(form.elements[j].type.indexOf('select') >= 0)
				{
					form.elements[j].style.visibility = 'hidden';
				}
			}
		}
	}


	//alert("height: " + popDivHeight);
	document.getElementById('popupDiv').innerHTML = html;
	popup('popupDiv');
	return false;
}