
// --- ROLLOVER SECTION ---------------------------------------

var imgpath=""; // default base path for rollover images

var rollovers=new Object;
var pageloaded=false;

function rollover(name) {
  var oTemp = eval('rollovers.'+name);
  if (oTemp == null) {
    oTemp = eval('rollovers.'+name+'=new Object');
    oTemp.over = new Array;
  }
  oTemp.index=0;
  for (var i=1; i<rollover.arguments.length; ++i)
    oTemp.over[i]=imgpath+rollover.arguments[i];
  if (pageloaded == true) {
    for (var i=1; i<oTemp.over.length; ++i) {
      var oTemp2 = new Image;
      oTemp2.src=oTemp.over[i];
      oTemp.over[i]=oTemp2;
    }
    oTemp.out=document[name].src;
    document[name].onload=null;
  }
}

function over(name,i) {
  if (!i) i=1;
  var oTemp = document[name]; 
  if (oTemp.onload==null) {
	oTemp.index=i;
    oTemp.src=eval('rollovers.'+name).over[i].src;
  }
}

function out(name) {
  var oTemp = document[name];
  if (oTemp.onload==null) {
	oTemp.index=0;
    oTemp.src=eval('rollovers.'+name).out;
  }
}

function prepreload() {
  setTimeout("preload()",500); // delay, to compensate for some NS problems
}

function preload() {
  for (var j in rollovers) {
    obj=eval("rollovers."+j);
    for (var i=1; i<obj.over.length; ++i) {
      var temp = new Image;
      temp.src=obj.over[i];
      obj.over[i]=temp;
    }
    obj.out=document[j].src;
    document[j].onload=null;
  }
  pageloaded=true;
}

window.onload=prepreload;

// --- POPUP SECTION ---------------------------------------

var oPopupImage = null;
var oPopupPage = null;
var iLoadingPopupX = 200;
var iLoadingPopupY = 200;  
var bPopupComplete = false;
var iPopupMarginX = 0;
var iPopupMarginY = 0;
var iPopupMinX = 0;
var iPopupMinY = 0;

function aiScreenPosXY(iPopupX, iPopupY) {
  var iScreenPosX = (screen.availWidth>iPopupX) ? (screen.availWidth-iPopupX)/2 : 0;
  var iScreenPosY = (screen.availHeight>iPopupY) ? (screen.availHeight-iPopupY)/2 : 0;
  return [iScreenPosX, iScreenPosY];
}

function aiPageXY() {
  var iX, iY;
  if (window.innerWidth) {
    iX = window.innerWidth;
    iY = window.innerHeight;
  }
  else if (document.body.clientWidth) {
    iX = document.body.clientWidth;
    iY = document.body.clientHeight;
  }
  return [iX,iY];
}

function aiBrowserChrome() {
  top.resizeTo(200,200);
  var aiPageSize = aiPageXY();
  aiPageSize[0] = 200 - aiPageSize[0];
  if (aiPageSize[0] < 0)
    aiPageSize[0] = 0; 
  aiPageSize[1] = 200 - aiPageSize[1];
  if (aiPageSize[1] < 0)
    aiPageSize[1] = 0; 
  return aiPageSize;  
}

function showError() {
  if (bPopupComplete == true)
    return;
  bPopupComplete = true;
  LayerPosHide("loading", 0, 0);
  LayerPosShow("error", 0, 0);
}

function showImage(bResize) {
  if (bPopupComplete == true)
    return;
  bPopupComplete = true;
  
  var oPic = (document.layers) ? document.layers.image.document.pic : document.pic;
  
  iPosLoadingX = oPic.width + 2*iPopupMarginX;
  if (iPosLoadingX < iPopupMinX)
    iPosLoadingX = iPopupMinX;

  iPosLoadingY = oPic.height + 2*iPopupMarginY + iPopupButtonY + 600;

  if (iPosLoadingY < iPopupMinY)
    iPosLoadingY = iPopupMinY;
	
	
  LayerPosHide("loading",iPosLoadingX-140,iPosLoadingY-100); // position in lower right corner, used for oversize
  
  if (bResize == true) {
    var aiChrome = aiBrowserChrome();
    var iPopupX = oPic.width + aiChrome[0] + 2*iPopupMarginX;
    var iPopupY = oPic.height + aiChrome[1] + 2*iPopupMarginY + iPopupButtonY;

    if (iPopupX < iPopupMinX)
      iPopupX = iPopupMinX;
    if (iPopupY < iPopupMinY)
      iPopupY = iPopupMinY;
	  
    if (self == top && (iPopupX > screen.availWidth-50 || iPopupY > screen.availHeight-50)) { // larger than screen
      location.href += "&type=oversize&xsize="+iPosLoadingX+"&ysize="+iPosLoadingY;
      return;   	
    }	
    if (iPopupX > screen.availWidth - 50)
      iPopupX = screen.availWidth - 50;
    if (iPopupY > screen.availHeight - 50)
      iPopupY = screen.availHeight - 50;

    var aiScreenPos = aiScreenPosXY(iPopupX, iPopupY);	
    top.moveTo(aiScreenPos[0], aiScreenPos[1]);
    //top.resizeTo(iPopupX,(iPopupY));
	//dpk
    top.resizeTo(iPopupX,(iPopupY+110));
  }

  var aiSize = aiPageXY();
  var iImagePosX = (aiSize[0] - oPic.width) / 2;
  var iImagePosY = (aiSize[1] - oPic.height - iPopupButtonY) / 2;

  
  if (iImagePosX < 0) 
    iImagePosX = iPopupMarginX;
  if (iImagePosY < 0) 
    iImagePosY = iPopupMarginY;
	
  if (document.layers) 

    setTimeout('LayerPosShow("image",'+iImagePosX+','+iImagePosY+')',200);

  else
    LayerPosShow("image",iImagePosX,iImagePosY);
}

function popupImage(sFileName, sTitle, sDescriptionOne, sDescriptionTwo, sDescriptionThree, sDescriptionFour) {
  if (sTitle == null || sTitle == "")
    sTitle = "Image Popup";
  if (oPopupImage != null) {
    oPopupImage.close();
	oPopupImage = null;
  }
  aiScreenPos = aiScreenPosXY(iLoadingPopupX,iLoadingPopupY);
  oPopupImage = window.open("popup.asp?fileName="+escape(sFileName)+"&title="+escape(sTitle)+"&descriptionOne="+escape(sDescriptionOne)+"&descriptionTwo="+escape(sDescriptionTwo)+"&descriptionThree="+escape(sDescriptionThree)+"&DescriptionFour="+escape(sDescriptionFour),"","scrollbars=yes,resizable,width="+iLoadingPopupX+",height="+iLoadingPopupY+",screenX="+aiScreenPos[0]+",screenY="+aiScreenPos[1]+",left="+aiScreenPos[0]+",top="+aiScreenPos[1]);
  oPopupImage.focus();
}

function popupPage(sPageURL, iXsize, iYsize, bSeparate, bNoScrollbars) {
  if (iXsize == null)
    iXsize=500;
  if (iYsize == null)
    iYsize=350;
  var sScroll = "scrollbars,";
  if (bNoScrollbars == true)
    sScroll = "";
  var iXpos = (screen.width-26>iXsize) ? (screen.width-iXsize)/2 : 0;
  var iYpos = (screen.height-26>iYsize) ? (screen.height-iYsize)/2 : 0;
  if (bSeparate == true)
    window.open(sPageURL,"",sScroll+"resizable,width="+iXsize+",height="+iYsize+",screenX="+iXpos+",screenY="+iYpos+",left="+iXpos+",top="+iYpos);
  else {
    if (oPopupPage != null && oPopupPage.closed == false)
      oPopupPage.close();
    oPopupPage = window.open(sPageURL,"PopupPage",sScroll+"resizable,width="+iXsize+",height="+iYsize+",screenX="+iXpos+",screenY="+iYpos+",left="+iXpos+",top="+iYpos);
    oPopupPage.focus();
  }
}

function closePopups() {
  if (oPopupImage != null)
    oPopupImage.close();
  if (oPopupPage != null && oPopupPage.closed == false)
    oPopupPage.close();
}

window.onunload=closePopups;

// --- LISTBOX SECTION ---------------------------------------

function decodeURL(oSelect) {
  var sEncodedURL = oSelect.options[oSelect.selectedIndex].value;
  oSelect.selectedIndex = -1;
  if (sEncodedURL == "")
    return;
  var asURL = sEncodedURL.split(":");
  if (asURL[0] == "anchor") {
    if (top.frame0)
      top.frame0.location.hash = asURL[1];
    else
      top.location.hash = asURL[1];
  }	   
  else if (asURL[0] == "mailto")
    self.location.href="mailto:"+asURL[1];
  else if (asURL[0] == "_blank")
    window.open(asURL[1]+"?name="+asURL[3]);
  else if (asURL[0] == "_top" && window.cmsframeset != asURL[1])
    top.location.href = asURL[1]+"?name="+asURL[3];
  else if (asURL[3] != "") {
    if (top.frame0)
      top.frame0.location.href = asURL[2]+"?name="+asURL[3];
    else
      top.location.href = asURL[2]+"?name="+asURL[3];
  }
}

// --- FORM SECTION ---------------------------------------

function showSearchPage(sForm, iPage) {
  var oForm = document.forms[sForm];
  if (oForm != null) {
    oForm.searchpage.value = iPage;
	oForm.submit();
  }
}

function bValidateForm(oForm) {
  var sResult = "";
  var oFirstElement = null;
  for (i=0; i< oForm.elements.length; ++i) {
    var oElement = oForm.elements[i];
	if (oElement.name.charAt(0) == "*") { // leading * in name indicates required field
	  var sElementValue = "";
      if (oElement.type == "text" || oElement.type == "textarea" || oElement.type == "password" || oElement.type == "file") {
	    sElementValue = oElement.value;
      }
	  else if (oElement.type == "checkbox") {
        if (oElement.checked)
   	      sElementValue = oElement.value;
	  }
      else if (oElement.type == "radio") {
        if (oElement != oElement.form[oElement.name][0])
		  continue; // ignore if not first element in radio group
		var oRadioElement = oElement.form[oElement.name];
        for (var j=0; j<oRadioElement.length; ++j)
		  if (oRadioElement[j].checked)
 	        sElementValue = oRadioElement[j].value;
      }
      else if (oElement.type == "select-one" || oElement.type == "select-multiple") {
	    if (oElement.selectedIndex >= 0)
          sElementValue = oElement.options[oElement.selectedIndex].value;
      }
	  else {
	    continue; // if not one of these input types, ignore
	  }
      if (oFirstElement == null)
	    oFirstElement = oElement;
      var sElementName = oElement.name.substr(1); // trim leading * from name
      if (sElementName.toLowerCase().indexOf("email") != -1) { // do extended email checking
        if (sElementValue == "")
   	      sResult += "Please enter an email address for "+sElementName+"\n\n";
        else {	  
          var recheck = /\s/;	    
          if (recheck.test(sElementValue) == true)
  	      sResult += "Email addresses cannot contain spaces\n\n";
          if (sElementValue.indexOf("@") == -1)
            sResult += "Email addresses must contain an @ symbol\n\n";
          recheck = /[\w-]+@([\w-]+\.)+[a-zA-Z]{2,6}$/
          if (recheck.test(sElementValue) == false)
            sResult += "There appears to be a mistake in the\nemail address you have typed.\nPlease check the address and try again.\n\n"; 
        }
      }
	  else if (sElementValue == "") {
        sResult += "Please enter a value for "+sElementName+"\n\n";
      }
    }
  }
  if (sResult != "") {
    alert(sResult);
	if (oFirstElement != null && oFirstElement.focus != null)
	  oFirstElement.focus()
    return false;
  }
  return true;
}

// --- FLYUP SECTION ---------------------------------------

var iFlyupTimeout;
var sFlyupName;

function oFindLayer(sLayer) {
  if (document.getElementById)
    return document.getElementById(sLayer);
  if (document.all)
    return eval("document.all."+sLayer);
  return eval("document."+sLayer);
}

function getXY(sName) {
  var iX = 0;
  var iY = 0;
  var oTemp = document[sName];  
  var iXwidth = oTemp.width;
  var iYheight = oTemp.height;

  if (document.layers) {
    iX = oTemp.x;
    iY = oTemp.y;
  }
  else {
    while (oTemp.offsetParent != null) {
      iX += oTemp.offsetLeft;
      iY += oTemp.offsetTop;
      oTemp = oTemp.offsetParent;
    }
//    iX += parseInt(document.body.leftMargin); // Mac IE5
//    iY += parseInt(document.body.topMargin); // Mac IE5
  }
  return [iX,iY,iXwidth,iYheight];
}

function LayerPosShow(layername, iPosX, iPosY) {
  iPosX = 0;
  iPosY = 0;
  //dpk
  
  oLayer = oFindLayer(layername);
  if (document.layers) {
    oLayer.left = iPosX;
    oLayer.top = iPosY;
    oLayer.visibility = "show";
  }
  else {
    oLayer.style.left = iPosX;
    oLayer.style.top = iPosY;
    oLayer.style.visibility = "visible";
  }
}

function LayerPosHide(layername, iPosX, iPosY) {
  oLayer = oFindLayer(layername);
  if (document.layers) {
    oLayer.left = iPosX;
    oLayer.top = iPosY;
    oLayer.visibility = "hide";
  }
  else {
    oLayer.style.left = iPosX;
    oLayer.style.top = iPosY;
    oLayer.style.visibility = "hidden";
  }
}

function FlyupShow(sLayer) {
  if (sFlyupName != null) {
    if (sLayer == sFlyupName) { // re-showing nav about to be hidden, cancel
      if (iFlyupTimeout != null) {
        clearTimeout(iFlyupTimeout);
        iFlyupTimeout = null;
      }
    }
    else {		     
      LayerPosHide(sFlyupName,-500,-500); // hide early
    }
  }
  var aiCoord = getXY("b"+sLayer.substr(1));
  LayerPosShow(sLayer,aiCoord[0],aiCoord[1]+aiCoord[3]);
  sFlyupName = sLayer;
}

function FlyupHide(sLayer) {
  iFlyupTimeout = setTimeout("FlyupDelayedHide('"+sLayer+"')",200);
}

function FlyupDelayedHide(sLayer) {
  LayerPosHide(sLayer,-500,-500);
  if (sLayer == sFlyupName)
    sFlyupName = null; 
}

