// Casey Smith
// General Scripts
// Last updated: 05/01/02
// This file contains useful general functions for dealing with forms, 
// dynamic html content, and menuing within html.

function message(text) {
  status=text;
  return true;
}

// functions for dealing with forms
// Some adapted from Danny Goodmans JavaScript Bible

//see if an input value has been entered
function isEmpty(intputStr) {
  if(inputStr == null || inputStr == "") {
    return true;
  }
  return false;
}

//see if in input value is a positive integer
function isPosInteger(inputVal) {
  inputStr = inputVal.toString();
  for(var i=0; i<inputStr.length; i++) {
    var oneChar = inputStr.charAt(i);
    if(oneChar < "0" || oneChar > "9"){
      return false;
    }
  }
  return true;
}

//see if in an input value is a positive or negative integer
function isInteger(inputVal) {
  inputStr = inputVal.toString();
  for(var i=0; i<inputStr.length; i++){
    var oneChar = inputStr.charAt(i);
    if(i==0 && oneChar == "-"){
      continue;
    }
    if(oneChar < "0" || oneChar > "9") {
      return false;
    }
  }
  return true;
}

//see if an input value is a positive or negative number
function isNumber(inputVal) {
  oneDecimal = false;
  inputStr = inputVal.toString();
  for(var i=0; i<inputStr.length; i++){
    var oneChar = inputStr.charAt(i);
    if(i==0 && oneChar =="-") {
      continue;
    }
    if(oneChar == "." && !oneDecimal) {
      oneDecimal = true;
      continue;
    }
    if(oneChar < "0" || oneChar > "9") {
      return false;
    }
  }
  return true;
}

//used for "onChange" directives.  Pass "this" as the first arguement and 
//the range as the next two.  If it is not an integer, the field is set to the
//default value.  If it is below the range, it gets the min val.  If it is
//above the range, it gets the max val.
function requireIntegerRange(inputField, minVal, maxVal) {
  if(!isInteger(inputField.value)) {
    inputField.value = inputField.defaultValue;
    return;
  }
  if(inputField.value < minVal) {
    inputField.value = minVal;
    return;
  }
  if(inputField.value > maxVal) {
    inputField.value = maxVal;
    return;
  }
}
function requireOddIntegerRange(inputField, minVal, maxVal) {
  if(!isInteger(inputField.value)) {
    inputField.value = inputField.defaultValue;
    return;
  }
  if(inputField.value < minVal) {
    inputField.value = minVal;
    return;
  }
  if(inputField.value > maxVal) {
    inputField.value = maxVal;
    return;
  }
  if(!(inputField.value % 2)) {
    inputField.value = parseInt(inputField.value) + 1;
  }
}
function requireFloatRange(inputField, minVal, maxVal) {
  if(!isNumber(inputField.value)) {
    inputField.value = inputField.defaultValue;
    return;
  }
  if(inputField.value < minVal) {
    inputField.value = minVal;
    return;
  }
  if(inputField.value > maxVal) {
    inputField.value = maxVal;
    return;
  }
}


//place two elements at the same location
function alignElements(stillObj, movingObj){
  var args = alignElements.arguments;
  stillObj = getObject(args[0]);
  var x = getGrossOffsetLeft(stillObj);
  var y = getGrossOffsetTop(stillObj);
  for(var i=1; i<args.length; i++){
    movingObj = getObject(args[i]);	
    shiftTo(movingObj, x, y);
  }
}



//Determining content area properties
function getInnerHeight(){
  if(typeof(window.innerHeight) != 'undefined') {
    return innerHeight;
  } else if(typeof(document.documentElement) != 'undefined'){
    return document.documentElement.clientHeight;
  } else if(typeof(document.body) != 'undefined') {
    return document.body.clientHeight;
  }
}

function getInnerWidth(){
  if(typeof(window.innerWidth) != 'undefined') {
    return innerWidth;
  } else if(typeof(document.documentElement) != 'undefined'){
    return document.documentElement.clientWidth;
  } else if(typeof(document.body) != 'undefined') {
    return document.body.clientWidth;
  }
}


//Finding where elements are
function getGrossOffsetLeft(elem) {
  var offset = 0;
  while(elem) {
    offset += parseInt(elem.offsetLeft);
    elem = elem.offsetParent;
  }
  return offset;
}
function getGrossOffsetTop(elem) {
  var offset = 0;
  while(elem) {
    offset += parseInt(elem.offsetTop);
    elem = elem.offsetParent;
  }
  return offset;
}



//API for DHTML, some parts adapted from Danny Goodmans Javascript Bible

if(!document.getElementById){
  alert("Netscape 6, IE 5.5, or better.  Parts of this site may not work.");
}

//converts an id string to the corresponding object, if neccessary
function getObject(obj){
  if (typeof obj == "string"){
    return document.getElementById(obj);
  } else {
    return obj;
  }
}

//places an object at the specified coordinates
function shiftTo(obj, x, y){
  var theObj = getObject(obj);
  theObj.style.left = x + "px";
  theObj.style.top = y + "px";
}

//moves an object by the specified amount
function shiftBy(obj, deltaX, deltaY){
  var theObj = getObject(obj);
  var newX = parseInt(theObj.style.left) + deltaX;
  var newY = parseInt(theObj.style.top) + deltaY;
  theObj.style.left = newX + "px";
  theObj.style.top = newY + "px";
}

//Sets the z index
function setZIndex(obj, zOrder) {
  var theObj = getObject(obj);
  theObj.style.zIndex = zOrder;
}

//Sets the background color of an object
function setBGColor(obj, color) {
  var theObj = getObject(obj);
  theObj.style.backgroundColor = color;
}

//makes an object visible
function show(obj){
  var theObj = getObject(obj);
  theObj.style.visibility = "visible";
}

//makes an object invisible
function hide(obj){
  var theObj = getObject(obj);
  theObj.style.visibility = "hidden";
}

//returns 1 if the object is visible
function getVisibility(obj){
  var theObj = getObject(obj);
  if(theObj.style.visibility == "hidden"){
    return 0;
  }
  return 1;
}

function remove(obj) {
  var theObj = getObject(obj);
  theObj.style.display = "none";
}

function replace(obj) {
  var theObj = getObject(obj);
  theObj.style.display = "block";
}

function setDisplay(obj, mode){
  var theObj = getObject(obj);
  theObj.style.display = mode;
}

function getDisplay(obj){
  var theObj = getObject(obj);
  if(theObj.style.display == "none"){
    return 0;
  } else {
    return 1;
  }
}

function getDisplayWord(obj) {
  var theObj = getObject(obj);
  return theObj.style.display;
}

function toggleHide(obj) {
  if(getVisibility(obj)){
    hide(obj);
  } else {
    show(obj);
  }
}


//retrieve the x coordinate of a positionable object
function getObjectLeft(obj) {
  var theObj = getObject(obj);
  return parseInt(theObj.style.left);
}

//retrieve the y coordinate of a positionable object
function getObjectTop(obj) {
  var theObj = getObject(obj);
  return parseInt(theObj.style.top);
}


//******************************
//Functions for creating menus
//Object oriented style

//Object menuElement is one entry on a menu
//The label is the text that appears in the menu, and the action
//is what happens when the menu is clicked.

function displayMenuElement(){
  document.write("<a class=menuElement href=\""+this.action);
  document.write("\" onClick=\"closeAllMenus()\">"+this.label+"</a>");
}

function menuElement(label, action){
  this.label = label;
  this.action = action;
  this.display = displayMenuElement;
}


//Object menuColumn is a header and a list of menu elements
//The first argument is the label, the second argument is
//the unique identifier (used in the id flag), the third
//argument is an action to perform when the menu is opened,
//and the fourth
//argument is an array of menuElements

function displayMenuColumn(){
  document.write("<div class=menuColumn id=\""+this.identification + "\" ");
  document.write("style=\"position: absolute; visibility:hidden;\">");
  for(var i=0; i<this.contents.length; i++){
    this.contents[i].display();
  }
  document.write("<img border=0 src=\"transparent.gif\" id=");
  document.write(this.identification + "BottomAnchor ");
  document.write("height=0 width=0>");
  document.write("</div>");
}

function menuColumn(label, identification, action, contents){
  this.label = label;
  this.identification = identification;
  this.action = action;
  this.contents = contents;
  this.display = displayMenuColumn;
  this.columnBottom = 100;
}


//Menu object
//Identification is for the id= tag (for positioning using css)
//contents is an array of menuColumns
function displayWholeMenu(){
  document.writeln("");
  document.writeln("<div id=" + this.identification + " class=menuBar>");
  for(var i=0; i<this.contents.length; i++){
    document.write("<a href=\""+this.contents[i].action+"\" ");
    document.write("onClick=\"this.temp=getVisibility(\'");
    document.write(this.contents[i].identification+"\'); closeAllMenus(); ");
    document.write("if(this.temp) {");
    document.write(" hide(\'" + this.contents[i].identification + "\'); ");
    document.write("return false; } else { ");
    document.write("show(\'" + this.contents[i].identification + "\'); ");
    document.write ("(event.stopPropagation)? event.stopPropagation() : event.cancelBubble=1;}\" ");
    document.write("class=\"menuHeader\">");
    document.write("<img border=\"0\" src=\"transparent.gif\" id=\"");
    document.write(this.contents[i].identification + "Anchor\" ");
    document.write("height=\"0\" width=\"0\">");
    document.write(this.contents[i].label + "</a> &nbsp;&nbsp;");
  }
  document.write("<img border=0 src=\"transparent.gif\" id=");
  document.write(this.identification + "RightAnchor height=0 width=0>");
  document.write("</div>\n");
  for(var i=0; i<this.contents.length; i++) {
    this.contents[i].display();
  }
  //add function for clearing menus
  document.write("\n<script language=javascript>\n");
  document.writeln("function closeAllMenus() {")
    for(var i=0; i<this.contents.length; i++){
      document.writeln("  hide('" + this.contents[i].identification +"');");
    }
  document.writeln("  " + this.closeAction);
  document.writeln("}");
  document.writeln("function documentClickForMenu() {");
  document.writeln("    closeAllMenus();");
  document.writeln("}");
  document.writeln("document.onclick = documentClickForMenu");
  document.writeln("var menuTimeout;");
  document.writeln("</script>");
}
function alignMenuElements(){
  for(var i=0; i<this.contents.length; i++){
    alignElements(this.contents[i].identification+"Anchor", this.contents[i].identification);
  }
  this.menuBottom = 0;
  for(var i=0; i<this.contents.length; i++){
    var tempBottom = parseInt(getGrossOffsetTop(getObject(this.contents[i].identification+"BottomAnchor")));
    this.contents[i].columnBottom = tempBottom + 10;
    if(tempBottom + 10 > this.menuBottom) {
      this.menuBottom = tempBottom + 10;
    }
  }
  this.menuTop = getGrossOffsetTop(getObject(this.identification));
  this.menuRight = getGrossOffsetLeft(getObject(this.identification + "RightAnchor"));
}

function menu(identification, contents, closeAction){
  this.identification = identification;
  this.contents = contents;
  this.display = displayWholeMenu;
  this.align = alignMenuElements;
  this.closeAction = closeAction;
  this.menuTop = 0;
  this.menuBottom = 100;
  this.menuRight = 100;
}


