//turn form validation off- other pages might turn it on later
var validate=0;

//capture mouse events
if(document.captureEvents) {document.captureEvents(Event.MOUSEMOVE);}


////////////////detect browser/////////////////////////////////////////

var ie, ns, ver, br;

if (navigator.appName == "Netscape") {
   br="ns";ns =1;
} else {
   br="ie";ie=1;
}
ver = navigator.appVersion.substring(0,1);


////////////////Generic image swapping functions///////////////////////

var restoreImg = ""; //source last image switched, for RestoreImage
var lastImg = ""; //name of last image switched, for RestoreImage


function SwapImage (oldimg,newimg) {
  if (document.images) {
   restoreImg = document.images[oldimg].src;
   document.images[oldimg].src=newimg;
   lastImg = oldimg;
  }
}

function RestoreImage () {
  if (document.images) {
   document.images[lastImg].src=restoreImg;
  }
}

function PreloadImages() { 
  images = new Array;
  for (x=0; x<PreloadImages.arguments.length; x++) {
  	images[x]=new Image;
  	images[x].src=PreloadImages.arguments[x];
  	}
}

/////////////////Toolbar menu functions////////////////////////

var formlayers = 0; //number of hideable form layers on the page
var clipLeft = 0;
var clipRight = 0;
var clipBottom = 0;
var clipTop = 0;
var toolbarxy = new Array;
toolbarxy[1] = new point(27,129);
toolbarxy[2] = new point(119,129);
toolbarxy[3] = new point(211,129);
toolbarxy[4] = new point(295,129);
toolbarxy[5] = new point(411,129);
toolbarxy[6] = new point(0,0);
toolbarxy[7] = new point(0,0);
var curmenu = 0;

function point (x, y) {
   this.x = x;
   this.y = y;
}

//////Get the coords of any layer
function GetBox (elem)
	{
	var result = new Object;
	
	var left	= 0;
	var top		= 0;

	for (var item = elem; item; item = item.offsetParent)
		{
		left += item.offsetLeft;
		top  += item.offsetTop;
		
		if (item.style.borderLeftWidth)
			{
			var bwid = parseInt (item.style.borderLeftWidth);
			if (!isNaN (bwid))
				left += bwid;
			}
		}
	
	result.left		= left;
	result.top		= top;
	result.right	= left + elem.offsetWidth;
	result.bottom	= top  + elem.offsetHeight;

	return result;
	}

//////Return appropriate layer object for broswer, or 0 if layer doesn't exist
function GetDiv (lyr, nostyle) {
   if (br == "ns") {
      if (ver <= 4) {
         if (document.layers['lyr']) {
          lyr=eval("document.layers."+lyr);
         }
      } else {
         if (document.getElementById(lyr)) {
          lyr = document.getElementById(lyr);
          if (!nostyle) {
            lyr = lyr.style;
          }
         } else {
          lyr = 0;
         }
      }
   } else {
      if (eval("document.all."+lyr)) {
         if (!nostyle) {
           lyr=eval("document.all."+lyr+".style");
         } else {
           lyr=eval("document.all."+lyr);
         }
      }
   }
   return lyr;
}

//////Show or hide a layer
function ShowDiv(lyr,xCoord,yCoord,show) {
   var vis;
   if (show) {
      vis = "visible";
   } else {
      vis = "hidden";
   }
lyr = GetDiv(lyr);
if (lyr) {
lyr.left = xCoord;
lyr.top = yCoord;
lyr.visibility = vis;   
}
}    

//////Show or hide all layers with hideable form elements (so that menus aren't cut off by forms)
function ShowFormLayers (vis) {
  for (x = 1; x<= formlayers; x++) {
    ShowDiv('formlayer'+x,0,0,vis);
  }
}

/*
Show or hide toolbar button- can't use generic SwapImage, because we needed added functionality
to track which menu is active, hide forms, etc; plus we don't want to set the RestoreImg and
LastImg variables.
*/

function SwapToolbarButton (button,visible) {
   //ignore hide requests on the current menu button
   if (button == curmenu) {
      return;
   }
   //show all form elements
   ShowFormLayers(1);
      
   //first clear all the other toolbar buttons
   for (x=1; x<=7; x++) {
      if (document.images['toolbar_button'+x]) {
         document.images['toolbar_button'+x].src="images/toolbar_buttons/toolbar_"+x+"_off.gif";
      }
   }
   //then turn one on, if requested
   if (visible==1) {
      //turn off the current menu, if any
      ShowToolbarMenu(0);
      curmenu=0;
      document.images['toolbar_button'+button].src="images/toolbar_buttons/toolbar_"+button+"_on.gif";
   }
}   

//////////Get the clipping coords of a layer

function GetClip (lyr) {
var x, y;
   if (br == "ns") {
      if (ver <= 4) {
         x = document.layers[lyr].clip.width;
         y = document.layers[lyr].clip.height;
      } else {
         x = parseInt(document.getElementById(lyr).offsetWidth);
         y = parseInt(document.getElementById(lyr).offsetHeight);
      }  
   } else {
      x = document.all[lyr].offsetWidth;
      y = document.all[lyr].offsetHeight;
   }
   
   p = new point(x,y);
   return p;
}

//////////Show or hide a toolbar menu

function ShowToolbarMenu(menu) {
//first hide all the menus
   for (x = 1; x <= 7; x++) {
      ShowDiv('toolbar'+x,toolbarxy[x].x,toolbarxy[x].y,0);
   } 
//if menu = 0 or the current menu, we just wanted to hide them all
   if (menu == 0 || menu==curmenu) {
   //stop chasing the mouse
      document.onmousemove="";
   //there is no active menu
      curmenu=0;      
   //show all form elements
      ShowFormLayers(1);
   return;   
   }
   
//otherwise show the one we want
   //hide all form elements
   ShowFormLayers(0);
   placer = GetDiv('t'+menu+'_placer',1);
   b = GetBox(placer);
   lyr = GetDiv('toolbar'+menu);
   p = GetClip('toolbar'+menu);
   clipRight = b.left + p.x + 5;
   clipLeft = b.left;
   if (menu == 7) {
      clipLeft -= 67;
      clipRight-= 67;
      b.left -= 67;
   }
   clipBottom = b.bottom + p.y + 5;  
   ShowDiv('toolbar'+menu,b.left,b.bottom,1);
   curmenu = menu;
   //capture mouse events
   document.onmousemove=CheckBounds;
   

}

//////Check whether or not the mouse is in the current clipping range, which is set by
//////the active menu

function CheckBounds(e) {
var x, y;
   if (br == "ns") {
      x = e.pageX;
		y = e.pageY;
   } else {
   	x = window.event.clientX;
		y = window.event.clientY;

   }
   if (x < clipLeft || x > clipRight || y < clipTop || y > clipBottom) {
      ShowToolbarMenu(0);
      menu = curmenu;
      curmenu = 99;
      SwapToolbarButton (menu,0);
   }
}


/////////////////////Page switching functions////////////////////


///////Grab a new page- use this for pretty much all links on the site
function GetPage (page, querystring, validate, wrapper) {
   var url;
   //wrap page in index.php unless otherwise instructed
   if (wrapper) {
      url = wrapper;
   } else {
      url = "index.php";
   }
   //add the page we're requesting to the query string;
   url = url + "?page=" + page;
   //add the rest of the query string;
   if (querystring!="") {
      url = url + "&" + querystring;
   }
   
   //default to validated
   valok = 1;
   
   //if we request validation, do it
   if (validate) {
      valok = valFields();
   }
   
   //if we validated ok, get the page
   if (valok) {
      window.location = url;
   }
}

///////Grab a new page from a popup
function GetPageFromPopup (page, querystring, validate, wrapper) {
   var url;
   //wrap page in index.php unless otherwise instructed
   if (wrapper) {
      url = wrapper;
   } else {
      url = "index.php";
   }
   //add the page we're requesting to the query string;
   url = url + "?page=" + page;
   //add the rest of the query string;
   if (querystring!="") {
      url = url + "&" + querystring;
   }
   
   //default to validated
   valok = 1;
   
   //if we request validation, do it
   if (validate) {
      valok = valFields();
   }
   
   //if we validated ok, get the page into the main site and close the popup
   if (valok) {
      top.opener.location = url;
      top.close();
   }
}


/////////////////Open new window////////////////////////
function GetPopup(url, height, width, nameW) {
 if (nameW==""){nameW='popup';}
 if (width==-1) {width=475;}
 if (height==-1) {height=480;}
 if (navigator.appVersion.indexOf('4') != -1) {
 // Vars for centering the new window on Version 4 Browsers
 xTop = screen.width/2 - (width/2);
 yTop = screen.height/2 - (height/2);
 newWin = window.open(url, nameW, 'height='+height+',width='+width+',scrollbars=1,resizable=1,menubar=0,toolbar=0,status=0,location=0,directories=0,left=' + xTop + ',top=' + yTop + '');
 } else {
 newWin = window.open(url, nameW, 'height='+height+',width='+width+',scrollbars=1,resizable=1,menubar=0,toolbar=0,status=0,location=0,directories=0,left=150,top=200');
 }
 newWin.focus();
}
  

    

function newWindow(url, height, width, nameW) {
    if (nameW==""){nameW='popup';}
 if (navigator.appVersion.indexOf('4') != -1) {
 // Vars for centering the new window on Version 4 Browsers
 xTop = screen.width/2 - (width/2);
 yTop = screen.height/2 - (height/2);
 window.open(url, nameW, 'height='+height+',width='+width+',scrollbars=0,resizable=0,menubar=0,toolbar=0,status=0,location=0,directories=0,left=' + xTop + ',top=' + yTop + '');
 } else {
 window.open(url, nameW, 'height='+height+',width='+width+',scrollbars=0,resizable=0,menubar=0,toolbar=0,status=0,location=0,directories=0,left=150,top=200');
 }
}
