// ************************************************************
// VINBox.js is copyrighted Veterinary Information Network 2000
// ************************************************************
//window.onerror = new Function("return true;");

var agt = navigator.userAgent.toLowerCase();
var IsNS = (navigator.appName == "Netscape");
var IsMac = ((agt.indexOf('mac_') != -1) || (agt.indexOf('macintosh') != -1));
var IsDOM = (parseInt(navigator.appVersion) >= 5);
isDynamic = (((parseInt(navigator.appVersion) >= 4) && (document.layers || document.all)) || IsDOM);

var DIVRef = (IsNS) ? "document" : "document.all";
var VisibilityRef = (IsNS && !IsDOM) ? ".visibility" : ".style.visibility";
var TopRef = (IsNS && !IsDOM) ? ".top" : ".style.pixelTop";
var LeftRef = (IsNS && !IsDOM) ? ".left" : ".style.pixelLeft";
var WidthRef = (IsNS && !IsDOM) ? ".document.width" : ".clientWidth";
var HeightRef = (IsNS && !IsDOM) ? ".document.height" : ".clientHeight";
if (!document.all && IsDOM) {
  TopRef = ".style.top";
  LeftRef = ".style.left";
  WidthRef = ".offsetWidth";
  HeightRef = ".offsetHeight";
}
var ScrollXRef = (IsNS) ? "window.pageXOffset" : "document.body.scrollLeft";
var ScrollYRef = (IsNS) ? "window.pageYOffset" : "document.body.scrollTop";
var MouseXRef = (IsNS && !IsDOM) ? "e.pageX - window.pageXOffset" : "e.clientX";
var MouseYRef = (IsNS && !IsDOM) ? "e.pageY - window.pageYOffset" : "e.clientY";

var MaxBoxRows = 15;
var PopUpWidth = 400;
var ZIndexDefautlt = 2;
var ZIndexOverride = ZIndexDefautlt;

var CurrentMenu = '';
var MouseInMenu = false;
var MenuBorderColor, MenuBGColor, MenuFontClass, MenuBulletType;
var VINPopUpMenuLayer;
var RegisterArray = new Array();

var MenuCloseOnClick =  true;
var MenuCloseOnOut =  true;
var MenuForce2Columns =  false;

function GetElementStr(ElementName) {
  if (IsDOM) return('document.getElementById("' + ElementName + '")');
  else return(DIVRef + '["' + ElementName + '"]');
}

function ChangeContent(ModLayer, NewHTML) {  
  if (IsNS && !IsDOM) {
    ModLayer.document.open();
    ModLayer.document.write(NewHTML);
    ModLayer.document.close();
  }
  else {
    ModLayer.innerHTML = NewHTML;
  }
}

function SetLayerToHide(LayerName) {
  if (IsNS && !IsDOM) LayerToHide = LayerName;
}

function RegisterFormElement(Element) {
  RegisterArray[RegisterArray.length] = Element;
}

function UpdateFormElement(Visibility) {
  if (!IsMac) {
    if (Visibility) Visibility='visible'; else Visibility='hidden';
    if (IsNS && !IsDOM) {
      // Need to hide form?
    } else {
      for (var i=0; i < RegisterArray.length; i++) {
        RegisterArray[i].style.visibility = Visibility;
      }
    }
  }
}

function SetMenuStyle(NewBorderColor, NewBGColor, NewFontClass, NewBulletType) {
  MenuBorderColor = NewBorderColor;
  MenuBGColor = NewBGColor;
  MenuFontClass = NewFontClass
  MenuBulletType = NewBulletType;
}

function SetMenuOptions(CloseOnClick) {
  MenuCloseOnClick = CloseOnClick;
}

function SetStandardMenu() {
  MenuCloseOnClick = true;
  MenuCloseOnOut =  false;
}

function SetDefaultMenuStyle() {
  SetMenuStyle('#0C0C75', '#E6E6F2', 'All', '&nbsp;<IMG SRC="/Images/Icons/BlackDot.gif" WIDTH=8 HEIGHT=8 BORDER=0>&nbsp;');
}

function LayerStart(IDName) {  
  if (!IsDOM && IsNS) {
    return('<LAYER NAME=' + IDName + ' VISIBILITY="hide" Z-INDEX='+ZIndexOverride+' BGCOLOR="' + MenuBGColor + '" onMouseOver="MenuMouseOver()" onMouseOut="MenuMouseOut()"><TABLE BORDER=1 BORDERCOLOR="' + MenuBorderColor + '" CELLSPACING=0 CELLPADDING=0><TR><TD>');
  }
  else {
    return('<DIV ID=' + IDName + ' STYLE="border-style: solid; border-width: 1px; position: absolute; visibility: hidden; z-index: '+ZIndexOverride+'; border-color: ' + MenuBorderColor + ';  background-color: ' + MenuBGColor + ';" onMouseOver="MenuMouseOver()" onMouseOut="MenuMouseOut()">');
  }
}

function LayerEnd() {
  if (!IsDOM && IsNS) return('</TR></TD></TABLE></LAYER>');
  else return('</DIV>');
}

function BuildMenu(MenuID, MenuTitle, MenuStartURL, MenuEndURL, MenuTargetURL, MenuItems, MenuURLs) {
  var MenuStr = '';
  if (MenuTargetURL) MenuTargetURL =  ' TARGET="' + MenuTargetURL + '"';
  MenuStr += LayerStart(MenuID) + '<TABLE BGCOLOR="' + MenuBGColor + '"><TR>';
  if (MenuTitle) MenuStr += '<TD COLSPAN=2><TABLE WIDTH="100%" HEIGHT=18 BORDER=0 CELLSPACING=0 CELLPADDING=0 BGCOLOR=Blue><TR HEIGHT=18><TD VALIGN=MIDDLE NOWRAP><IMG SRC="/Images/Icons/VINBoxIcon.gif" WIDTH=40 HEIGHT=14 BORDER=0><FONT CLASS="'+MenuFontClass+'" COLOR=White>&nbsp;<B>' + MenuTitle + '</B>&nbsp;</FONT></TD><TD VALIGN=MIDDLE ALIGN=right><A HREF="javascript: HideCurrentMenu();"><IMG SRC="/Images/Icons/XBox.gif" WIDTH=16 HEIGHT=14 BORDER=0 ALT="Click to Close"></A></TD></TR></TABLE></TD></TR><TR>';
  var SwitchColumn = -1;
  if ((MenuTitle || MenuForce2Columns) && (MenuItems.length > MaxBoxRows)) {
    MenuStr += '<TD VALIGN=TOP NOWRAP><FONT CLASS="'+MenuFontClass+'">';
    SwitchColumn = MenuItems.length;
    if (MenuItems.length % 2) SwitchColumn++; 
    SwitchColumn = SwitchColumn / 2;
  }
  else MenuStr += '<TD VALIGN=TOP COLSPAN=2 NOWRAP><FONT CLASS="'+MenuFontClass+'">';
  for (var i=0; i < MenuItems.length; i++) {
    if (i == SwitchColumn) MenuStr += '</FONT></TD><TD VALIGN=TOP NOWRAP><FONT CLASS="'+MenuFontClass+'">';
    if (MenuURLs[i]) MenuStr += MenuBulletType+'<A HREF="' + MenuStartURL + MenuURLs[i] + MenuEndURL + '"'+ MenuTargetURL + ' CLASS="'+MenuFontClass+'">';
    MenuStr += MenuItems[i];
    if (MenuURLs[i]) MenuStr += '</A>&nbsp;<BR>';
  }
  MenuStr += '</FONT></TD></TR></TABLE>' + LayerEnd();
  document.write(MenuStr);
}

function ShowMenuOver(MenuID, LeftX, TopY) {
  if (CurrentMenu) ShowMenu(MenuID, LeftX, TopY);
}

function ShowMenu(MenuID, LeftX, TopY) {
  if (isDynamic && CurrentMenu != MenuID) {
	if (CurrentMenu) HideMenu(CurrentMenu);
    UpdateFormElement(false);
    CurrentMenu = MenuID;
    var Element = GetElementStr(MenuID);
    if (TopY) eval(Element + TopRef + ' = ' + TopY);
    if (LeftX) eval(Element + LeftRef + ' = ' + LeftX);
    eval(Element + VisibilityRef + ' = "visible"');
  }
}

function ShowMenuRel(MenuID, e, LeftX, TopY, LockX, LockY) {
  if (isDynamic && CurrentMenu != MenuID) {
    var Element = GetElementStr(MenuID);
    if (LeftX < 0) LeftX -= eval(Element + WidthRef);
    if (TopY < 0) TopY -= eval(Element + HeightRef);
    if (!LockX) LeftX = eval(MouseXRef) + LeftX + eval(ScrollXRef);
    if (!LockY) TopY = eval(MouseYRef) + TopY + eval(ScrollYRef);
    ShowMenu(MenuID, LeftX, TopY);    
  }
}

function HideMenu(MenuID) {
  if (isDynamic) {
    eval(GetElementStr(MenuID) + VisibilityRef + ' = "hidden"');
    CurrentMenu = '';
    UpdateFormElement(true);
  }
}

function HideCurrentMenu() {
  if (CurrentMenu) HideMenu(CurrentMenu);
}

function MenuMouseOver() {
  if (!IsNS) MouseInMenu = true;
}

function MenuMouseOut() {
  if (!IsNS && CurrentMenu && document.all[CurrentMenu].contains(window.event.toElement)) return;
  if (MenuCloseOnOut && MouseInMenu) HideCurrentMenu();
  MouseInMenu = false;
}

function PageMouseUp() {
  if (MenuCloseOnClick && !MouseInMenu) HideCurrentMenu();
}

function NSReload() {	
  if (innerWidth != origWidth || innerHeight != origHeight) location.reload();
}	

if (isDynamic) {
  SetDefaultMenuStyle();
  if (IsNS) document.captureEvents(Event.MOUSEUP);
  document.onmouseup = PageMouseUp;

  // Work-around Netscape resize bug
  if (IsNS) {
    origWidth = innerWidth;
    origHeight = innerHeight;	
    onresize = NSReload;
  }
}
