var IB = new Object;
var posX = 0; posY = 0;
var xOffset = 10; yOffset = 10;
var homepageTimeout = null;
var isHelpBoxVisible = false;

function ShowHelpBox(text)
{
    if(isHelpBoxVisible == true)
        return;

/*  content = "<div style='border:" + IB.nbPixel + "px solid " + IB.colBorder + "'><table border='0' cellpadding='5' cellspacing='0' bgcolor='" + IB.colBG + "'><tr><td><font face='arial' color='" + IB.colText + "'>" + text+"</font></td></tr></table></div>"; */
    var dynHTML = "<div style='color: #fff; border:" + IB.nbPixel + "px solid " + IB.colBorder + "'><div style='border: none; background-color: " + IB.colBG + "; padding: 3px 5px;'>" + text + "</div></div>";

    var finalPosX = posX - xOffset;
    if (finalPosX < 0)
        finalPosX = 0;
        
    if (document.layers)
    {
        document.layers["calendarEvDiv"].document.write(dynHTML);
        document.layers["calendarEvDiv"].document.close();
        document.layers["calendarEvDiv"].top = posY + yOffset + "px";
        document.layers["calendarEvDiv"].left = finalPosX + "px";
        document.layers["calendarEvDiv"].visibility = "show";
        isHelpBoxVisible = true;
    }
    
    if (document.all)
    {
        calendarEvDiv.innerHTML = dynHTML;
        document.all["calendarEvDiv"].style.top = posY + yOffset;
        document.all["calendarEvDiv"].style.left = finalPosX;
        document.all["calendarEvDiv"].style.visibility = "visible";
        isHelpBoxVisible = true;
    }
    else if (document.getElementById)
    {
        document.getElementById("calendarEvDiv").innerHTML = dynHTML;
        document.getElementById("calendarEvDiv").style.top = posY + yOffset + "px";
        document.getElementById("calendarEvDiv").style.left = finalPosX + "px";
        document.getElementById("calendarEvDiv").style.visibility = "visible";
        isHelpBoxVisible = true;
    }
    
}


// Main function to retrieve mouse x-y pos.s
function getMousePosXY(e)
{
  if (document.all)
  { // grab the x-y pos.s if browser is IE
    posX = event.clientX + document.body.scrollLeft;
    //posY = event.clientY + document.body.scrollTop;
    posY = event.clientY;
    posY += (window.pageYOffset)? window.pageYOffset : (document.documentElement && document.documentElement.scrollTop)?document.documentElement.scrollTop:document.body.scrollTop;
//    alert(posY);
  }
  else
  {  // grab the x-y pos.s if browser is NS
    posX = e.pageX;
    posY = e.pageY;
  }  
  // catch possible negative values in NS4
  if (posX < 0) { posX = 0; }
  if (posY < 0) { posY = 0; }  
  return true;
}

function HideHelpBox()
{
	if (document.layers)
    {
      document.layers["calendarEvDiv"].visibility = "hide";
    }

    if (document.all)
    {
      document.all["calendarEvDiv"].style.visibility = "hidden";
    }
	else if (document.getElementById)
    {
      document.getElementById("calendarEvDiv").style.visibility = "hidden";
    }

    homepageTimeout = null;
    isHelpBoxVisible = false;
}

function InitHelpBox(colText, colBG, colBorder, nbPixel)
{
    IB.colText = colText;
    IB.colBG = colBG;
    IB.colBorder = colBorder;
    IB.nbPixel = nbPixel;
	
	if (document.layers)
    {
		window.captureEvents(Event.MOUSEMOVE);window.onMouseMove = getMousePosXY;
		document.write("<layer name='calendarEvDiv' top='0' left='0' visibility='hide'></layer>");
	}
	
	if (document.all)
    {
        document.write("<div id='calendarEvDiv' style='position: absolute; top: 0; left: 0; visibility: hidden;'></div>");
		document.onmousemove = getMousePosXY;
	}
	else if (document.getElementById)
    {
        document.onmousemove = getMousePosXY;
        document.write("<div id='calendarEvDiv' style='position: absolute; top: 0; left: 0; visibility: hidden;'></div>");
	}
}


function CreateBookmarkLink()
{
    titleLink = "POP - Portal de Oncologia Português";
    urlLink = "http://www.pop.eu.com/";
    
    if (window.sidebar)
    {
        // Mozilla Firefox Bookmark
        window.sidebar.addPanel(titleLink, urlLink, "");
    }
    else if(window.external)
    {
        // IE Favorite
        window.external.AddFavorite(urlLink, titleLink);
    }
    else if(window.opera && window.print)
    {
        // Opera Hotlist
        //return true;
		alert('Não foi possível adicionar o nosso site aos favoritos automaticamente. \nPor favor faça-o manualmente. \nObrigado.');
    }
	else
	{
		alert('Não foi possível adicionar o nosso site aos favoritos automaticamente. \nPor favor faça-o manualmente. \nObrigado.');	
	}
}

function ShowDefaultHomepageInfo()
{
    if(homepageTimeout != null)
    {
        HideHelpBox();
        clearTimeout(homepageTimeout);
        homepageTimeout = null;
    }
    else if(document.layers || document.getElementById)
    {
        ShowHelpBox("O seu browser não permite mudar a página de abertura automaticamente."+
            " Pode no entanto ir às opções e fazer a operação manualmente ou tentar arrastar"+
            " o icon para cima da imagem da casinha no canto superior esquerdo dentro da janela do firefox.");
        homepageTimeout = setTimeout("HideHelpBox();", 5000);
    }
}

var win = null;
function NewWindow(mypage, myname, w, h, scroll, pos)
{
    if(pos=="random")
    {
        LeftPosition = (screen.width) ? Math.floor(Math.random() * (screen.width - w)) : 100;
        TopPosition = (screen.height) ? Math.floor(Math.random() * ((screen.height - h) - 75)) :
        100;
    }
    if(pos=="center")
    {
        LeftPosition = (screen.width) ? (screen.width - w) / 2 : 100;
        TopPosition = (screen.height) ? (screen.height - h) / 2 : 100;
    }
    else if((pos != "center" && pos != "random") || pos == null)
    {
        LeftPosition = 0;
        TopPosition = 20
    }
    settings = 'width = ' + w + ',height = ' + h + ', top = '+TopPosition+', left = '+LeftPosition+', scrollbars = ' + scroll+', location = no, directories = no, status = no,enubar = no, toolbar = no, resizable = no';

    win = window.open(mypage, myname, settings);
}

function GetQueryVariable(variable)
{
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++)
    {
        var pair = vars[i].split("=");
        if (pair[0] == variable)
        {
            return pair[1];
        }
    } 
    alert('Query Variable ' + variable + ' not found');
}

function SetFormField(field, value)
{
    obj = document.getElementById(field);
    obj.value = value;
}