// JavaScript Document
<!--
// **BEGIN GENERIC VALIDATION FUNCTIONS**
// general purpose function to see if an input value 
// has been entered at all
function isEmpty(inputStr) {
  if (inputStr == "" || inputStr == null) {
    return true
  }
  return false
}

// function to determine if value is in acceptable range 
//for this application
function inRange(inputStr, lo, hi) {
  var num = parseInt(inputStr, 10)
  if (num < lo || num > hi) {
    return false
  }
  return true
}
// **END GENERIC VALIDATION FUNCTIONS**

// This function validates that the number entered in the month
// box is between 1 and 12. It gives a javascript alert if not,
// and highlights the month field. 

function validateMonth(field) {
  var input = field.value
  if (isEmpty(input)) {
    alert("Be sure to enter a month value.")
    select(field)
    return false
  } else {
    input = parseInt(field.value, 10)
    if (isNaN(input)) {
      alert("Entries must be numbers only.")
      select(field)
      return false
    } else {
      if (!inRange(input,1,12)) {
         alert("Enter a number between 1 (January) and 12 (December).")
         select(field)
         return false
      }
    }
  }
  return true
}

// ** This function validates the date. Because the month is entered first, 
// we can validate the month, then set the number of days in that month as
// the highest value. In February, the valid range is 1-29. 

function validateDate(field) {
  var input = field.value
  if (isEmpty(input)) {
    alert("Be sure to enter a date value.")
    select(field)
    return false
  } else {
    input = parseInt(field.value, 10)
    if (isNaN(input)) {
      alert("Entries must be numbers only.")
      select(field)
      return false
    } else {
      var monthField = document.age.month
      if (!validateMonth(monthField, true)) return false
      var monthVal = parseInt(monthField.value, 10)
      var monthMax = new Array(31,31,29,31,30,31,30,31,
                               31,30,31,30,31)
      var top = monthMax[monthVal]
      if (!inRange(input,1,top)) {
        alert("Enter a number between 1 and " + top + ".")
        field.select()
        return false
      }
    }
  }
  return true
}

// ** This function validates that a year was entered. We are allowing anything
// from 1890 on. Currently, the upper bound is the current year and is hard coded.
// This value could be nabbed by JavaScript, but if the persons system clock is
// set wrong there is a possibility it will not allow them in. For example if they
// system clock is set to 1975 and they were born in 1976, it won't let them enter
// 1976. Leave it hard coded for now.

function validateYear(field) {
  var input = field.value
  if (isEmpty(input)) {
    alert("Be sure to enter a year value.")
    select(field)
    return false
  } else {
    input = parseInt(field.value, 10)
    if (isNaN(input)) {
      alert("Entries must be numbers only.")
      select(field)
      return false
    } else {
      if (!inRange(input,1890,2008)) {
        alert("Enter a valid Year.")
        select(field)
        return false
      }
    }
  }
  return true
}

// This function sets the focus on and selects the field that is in error. 
// If there is any text selected, it deselects it first.

function select(field) {
	if (document.selection)
			{
				document.selection.empty();
				obj.blur();
			}
  field.focus()
  field.select()
}

// This function checks the entire form when the form is submitted. The 'return false'
// statement cancels the submission if one of the fields is in error.

function checkForm(form) {
  if (validateMonth(form.month)) {
    if (validateDate(form.day)) {
      if (validateYear(form.year)) {
        return true
      }
    }
  }
  return false
}

var downStrokeField;
function autojump(fieldName,nextFieldName,fakeMaxLength)
{
var myForm=document.forms[document.forms.length - 1];
var myField=myForm.elements[fieldName];
myField.nextField=myForm.elements[nextFieldName];

if (myField.maxLength == null)
   myField.maxLength=fakeMaxLength;

myField.onkeydown=autojump_keyDown;
myField.onkeyup=autojump_keyUp;
}

function autojump_keyDown()
{
this.beforeLength=this.value.length;
downStrokeField=this;
}

function autojump_keyUp()
{
if (
   (this == downStrokeField) && 
   (this.value.length > this.beforeLength) && 
   (this.value.length >= this.maxLength)
   )
   this.nextField.focus();
   this.nextField.value='';
downStrokeField=null;
}
//-->
