var findCSTTries=0;
var CSTHandle = null;

function getCSTHandle()
{
   if (CSTHandle == null){
      CSTHandle = getCST();
   }
   return CSTHandle;
}

function findCST(win){
   while ((win.name != 'CST') && (win.opener != null) && (win.opener != win))  {
      findCSTTries++;      
      if (findCSTTries > 10) {
         return null;
      }
      win = win.opener.top;
   }     
   return win.name;
}

function getCST(){
   var theCST = findCST(window.top);
   return theCST;
}

function initMouseHandler()
{
  if (document.layers) {
    document.captureEvents(Event.MOUSEDOWN);
  } else {
    document.oncontextmenu = contextmenuhandler;
  }
  document.onmousedown = mousedown;
}

function contextmenuhandler()
{
  return false;
}

function mousedown(evt)
{
  var isNS = (document.layers) ? true : false;
  var btncode = (isNS) ? evt.which : null; 
  if (!isNS) {
    btncode = event.button;
  }
  if (isNS) {
    if (btncode == 3) {
      return false;		
    } else {
      routeEvent(evt);	
    }
  } else {
    if (btncode == 2) {
      event.cancelBubble = false;
      event.returnValue = false;
      var isCST = getCSTHandle();
      var x0 = window.screenLeft;
      var y0 = window.screenTop;
      var x = window.event.offsetX + x0;
      var y = window.event.offsetY + y0;
      if (isCST) {
        openRCMenu(x, y);
      }
      return false;
    } else {
      return false;
    }
  }
}

initMouseHandler();

function openRCMenu(x, y)
{
  var strURL = "../../context_menu_prjdev_preview.asp";
  var params = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,left="+x+",top="+y+",width=215,height=240"; 
  cstcontextmenu=window.open(strURL,"CSTContextMenu",params); 
  cstcontextmenu.focus();
}
