function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [ myWidth, myHeight ];
}

function centreForm( popup )
{
      var w = popup.style.width;
      var h = popup.style.height;
       
	  w = w.substring( 0, w.length-2 );
	  h = h.substring( 0, h.length-2 );	  
         
      var size = getWindowSize();
      var scroll = getScrollXY();

      var px = size[0]/2 - (w/2) + scroll[0];
      var py = size[1]/2 - (h/2) + scroll[1];

      
       /*  
      var margin = 50;
         
         // alert( scroll[0] + "," +scroll[1] );
         
         if (px-scroll[0] < margin) px=scroll[0]+margin;
         if ( (px+w-scroll[0]) > (size[0]-margin)) px = size[0]+scroll[0]-w-margin;
         
         if (py-scroll[1] < margin) py=scroll[1]+margin;         
         if ( py+h-scroll[1] > size[1]-margin)
            py = size[1]+scroll[1]-h-margin;
             
         // if (
        if (px<10) px=10;
		
         if ( (px+w) > (document.body.clientWidth-10+ document.body.scrollLeft + document.documentElement.scrollLeft) )
              px = document.body.clientWidth-w-10+ document.body.scrollLeft + document.documentElement.scrollLeft; 
         if (py<10) py=10;
         if ( (py+h) > (document.body.clientHeight-10) )
              py = document.body.clientHeight-h-10;
    
	*/
         
      popup.style.top = py + "px";
      popup.style.left = px + "px";    
}

/*
 * Find the absolute position (relative to document) of the specified element
 */
function findPos(obj) {

	if (!obj)
	  return [0,0];
	  
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function positionForm( popup, posx, posy )
{
      var w = popup.offsetWidth;
      var h = popup.offsetHeight;
//      alert ("size="+ popup.offsetWidth + ", " + popup.offsetHeight );
  //       var h = 80;
           
         var px = posx - (w/2);
         var py = posy - (h/2);
         
         var size = getWindowSize();
         var scroll = getScrollXY();
         
         var margin = 50;
         
         // alert( scroll[0] + "," +scroll[1] );
         
         if (px-scroll[0] < margin) px=scroll[0]+margin;
         if ( (px+w-scroll[0]) > (size[0]-margin)) 
		    px = size[0]+scroll[0]-w-margin;
         
         if (py-scroll[1] < margin) py=scroll[1]+margin;         
         if ( py+h-scroll[1] > size[1]-margin)
            py = size[1]+scroll[1]-h-margin;
                  
         // if (
       /*  if (px<10) px=10;
         if ( (px+w) > (document.body.clientWidth-10+ document.body.scrollLeft + document.documentElement.scrollLeft) )
              px = document.body.clientWidth-w-10+ document.body.scrollLeft + document.documentElement.scrollLeft; 
         if (py<10) py=10;
         if ( (py+h) > (document.body.clientHeight-10) )
              py = document.body.clientHeight-h-10;
         */
         popup.style.top = py + "px";
         popup.style.left = px + "py";    
}