function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function preparePop() {
  if (!document.getElementsByTagName) return false;
  var lnks = document.getElementsByTagName("a");
  for (var i=0; i<lnks.length; i++) {
    if (lnks[i].className == "popup") {
      lnks[i].onclick = function() {
        popUp(this.getAttribute("href"));
        return false;
      }
    }
  }
}

function popUp(winURL) {
  window.open(winURL,"popup","width=600,height=400");
}

function initpop()
{
	if (!document.getElementById("pop_win")) return false;
	var closeElementId='closewindow';
	var closeLinkText='Close window';
	var windowAttributes='width=600,height=400,left=0,top=0,scrollbars=auto,location=no';

	if(!window.opener)
	{
		var as,i,popfun
		as=document.getElementsByTagName('a');
		for (i=0;i<as.length;i++)
		{
			if(as[i].target)
			{
				popfun=function(){window.open(this.href,'',windowAttributes);return false;};
				as[i].onclick=popfun;
				as[i].onkeypress=popfun;
			}
		}
	} else {
		var closep,closelink,closetext;
		closelink=document.createElement('a');
		closetext=document.createTextNode(closeLinkText);
		closelink.href='#';
		closelink.appendChild(closetext);
		closelink.onclick=function(){self.close();};
		if(document.getElementById(closeElementId))
		{
			document.getElementById(closeElementId).appendChild(closelink);	
		} else {
			closep=document.createElement('p');
			closep.id=closeElementId;
			closep.appendChild(closelink);
			document.body.insertBefore(closep,document.body.firstChild);	
		}
	}
}

addLoadEvent(preparePop);
addLoadEvent(initpop);
