var oChildWindow=null;

function getForm()
{
	return document.getElementById('pageForm__');	
}

xSize = null;
ySize = null;

function setWindowSize (x ,y)
{
    xSize = x;
    ySize = y;
}
function getControl(elem)
{
  var form = getForm();
  return form.elements[elem];
}

function popup_size_xy(mylink, windowname, width, height)
{
	width = popup_calcSize(width, screen.availWidth);
	height = popup_calcSize(height, screen.availHeight);
	return popupsize(mylink, windowname, width, height, 0, 0, "");
}

function popup_calcSize(size, fullsize)
{
	var size;
    if (size.substring(size.length - 1) == "%") 
    {
    	size = size.substring(0, size.length - 1);
    	size = fullsize * (size/100);
    }
    return size;
}

function popupsize(mylink, windowname, width, height, toolbar, menubar, strparam)
{
    var href;
    if (typeof(mylink) == 'string')
        href=mylink;
    else
        href=mylink.href;

    var str = "";

    str += "resizable=1,";
	str += "scrollbars=1,";

	str += "width=" + Math.floor(width) + ",";
	str += "height=" + Math.floor(height) + ",";

    // work out x and y position of centre
	if ( window.screen ) {
		var ah = screen.availHeight - 30;
		var aw = screen.availWidth - 10;

		var xc = Math.floor(( aw - width ) / 2);
		var yc = Math.floor(( ah - height ) / 2);

		str += ",left=" + xc + ",screenX=" + xc;
		str += ",top=" + yc + ",screenY=" + yc;
	}
	
	var params = str;
	if (toolbar)
	{
		params += ",toolbar=" + toolbar;
	}
	if (menubar)
	{
		params += ",menubar=" + menubar;
	}
	if (strparam)
	{
		params += strparam;
	}
    oChildWindow=window.open('', windowname, params);

	var windowOpen = !oChildWindow.closed;
	var urlEmpty = false;
	var urlAbout = false;
	// IE has security about looking at the document of 3rd party links
	try
	{
		urlEmpty = !oChildWindow.document.URL;
		urlAbout = oChildWindow.document.URL.indexOf("about") == 0;
	}
	catch (err)
	{
		windowOpen = false;
	}
	
	var alreadyFocussed = false;
	if( windowOpen && !(urlEmpty || urlAbout) )
	{
		alreadyFocussed = true; 
		oChildWindow.focus();
	}
	
	oChildWindow.location = href;
	
	if (!alreadyFocussed)
	{
		oChildWindow.focus();
	}
	
	return false;
}

function popup(mylink, windowname)
{
    return popupsize(mylink, windowname, 640, 480);
}

function popup_percent(mylink, windowname, percent)
{
    return popup_percent_xy(mylink, windowname, percent, percent);
}

function popup_percent_xy(mylink, windowname, percentx, percenty)
{
    return popup_percent_xy(mylink, windowname, percentx, percenty, 0, 0, "");
}

function popup_percent_xy(mylink, windowname, percentx, percenty, toolbar, menubar, strparam)
{
    var width=800;
    var height=640;
    if (window.screen)
    {
        height = screen.availHeight * (percenty/100);
        width = screen.availWidth * (percentx/100);
    }
    return popupsize(mylink, windowname, width, height, toolbar, menubar, strparam);
}

function buttonUpDown(btnName, btnFile)
{
    document.images[btnName].src = btnFile;
    return true;
}

function changePage(elem, page)
{
	var form = getForm();
	form.elements[elem].value = page;
	form.target = '';
	// form.popup.value = 'false';
	form.submit();
}

function disableEnterKey(evt)
{
	if (evt && evt.keyCode == 13)
	{
		return false;
	}
	return true;
}


function fnInitPage()
{
    var x, y;
    if ((xSize!=null) && (ySize!=null))
    {
        x = xSize;
        y = ySize;
	    window.resizeTo( x , y );
    }
    else
    {
        y = document.body.scrollHeight;
        x = document.body.scrollWidth;
    }
    var obj = window.opener;

    if ((obj != null) && (window.history.length==0))
    {
		var ah = screen.availHeight - 0;
		var aw = screen.availWidth - 0;

		var xc = Math.floor(( aw - x ) / 2);
		var yc = Math.floor(( ah - y ) / 2);

        if ((xc>0) && (yc>0))
            window.moveTo( xc, yc );
    }
}
