
// Unique count for DIVs
var windowCount = 0;

var dialogTopOffset = 60;
var dialogBottomPad = 40;
var lastScrollPos = -1;


function dialogScrollies() {
  var pos = getScrollTop();
  
  if(pos == lastScrollPos) { // wait for scrolling stop
    // rescroll to new position:
    
    for(var i = 0; i < windowCount; ++i) {
      var winName = "win" + i;
      var dialogDiv = document.getElementById(winName);
      
      var dialogPos = Math.max(dialogTopOffset,
          Math.min(dialogTopOffset+pos, (pos + getClientHeight())
          - (dialogDiv.offsetHeight + dialogBottomPad)));

      dialogDiv.style.top = dialogPos + "px";
    }
  }
  
  lastScrollPos = pos;
  var temp = setTimeout('dialogScrollies()',250);
}


function dialogCreate(content, siz, foc, closetext) {
	return dialogCreateEx(content, siz, foc, closetext, false);
}

function dialogCreateEx(content, siz, foc, closetext, transparent) {
  var layer;
  var transparentStr = "";
  if (transparent) {
  	transparentStr = "filter: alpha (opacity=85);"
  }
  pwidth = document.documentElement.clientWidth;
  ctop = getScrollTop();
  layer = document.getElementById("docBody");

  content += '<p style="float:bottom; text-align: right;"><a style="text-decoration:none" href=javascript:closeDialog('+windowCount+')>'+closetext+'</a></p>';
  content = "<div id=win"+windowCount+" class=popup style='z-index: 3; top:0px;left:0px;width:"+siz+"px;padding:10px;"+transparentStr+"'>"+content+"</div>";

  nd = document.createElement("div");
  nd.innerHTML = content;

  document.body.appendChild(nd);

  var winName = "win" + windowCount;
  insideLayer = document.getElementById(winName);
  insideLayer.style.top = ctop + 60 + "px";
  insideLayer.style.left = (pwidth - siz)/2 + "px";
  insideLayer.style.width = siz + "px";
  insideLayer.style.padding = "20px 20px 20px 20px";
  insideLayer.style.visibility = 'visible';
  
  windowCount = windowCount + 1;
  if(windowCount == 1) { dialogScrollies(); } // begin scrolling code
  
  keypressDisabled = true;

  // focus to the named field (foc = fieldname)
  focfield = null;
  for (i=0; i<document.forms.length; i++) {
    for (j=0; j<document.forms[i].elements.length; j++) {
      if (document.forms[i].elements[j].name == foc)
        focfield = document.forms[i].elements[j];
    }
  }

  //if we found the element to focus on, then focus on it.
  if (focfield != null) {
      focfield.focus();
  }
}


function closeDialog(windowNumber) {
  insideLayer = document.getElementById("win"+windowNumber);
  insideLayer.style.visibility = 'hidden';
  keypressDisabled = false;
}



// Move the focus to the next field on this form
function focusAfter(theForm, fname) {
  foundFlag = 0;
  for (ix = 0; ix < theForm.length; ix++) {
    if (foundFlag == 1) {
      theForm.elements[ix].focus();
      return;
    }
    if (theForm.elements[ix].name == fname)
      foundFlag = 1;
  }
  return;
}


// Is the named field in the form (t/f)
function isField(theForm, fname) {
  for (ix = 0; ix < theForm.length; ix++) {
    if (theForm.elements[ix].name == fname)
      return true;
  }
  return false;
}

