//*****************************************************************************
// The folowing fuctions (autoTab, constainsElement, and getIndex) work together
// to provide for auto tabing functionality of phone number fields
//*****************************************************************************
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input, len, e)
{
  var keyCode = (isNN) ? e.which : e.keyCode; 
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  
  if(input.value.length >= len && !containsElement(filter,keyCode))
  {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
    input.form[(getIndex(input)+1) % input.form.length].select();
  }

  function containsElement(arr, ele)
  {
    var found = false, index = 0;
    while(!found && index < arr.length)
    if(arr[index] == ele)
    found = true;
    else
    index++;
    return found;
  }

  function getIndex(input) 
  {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
    if (input.form[i] == input)index = i;
    else i++;
    return index;
  }
  return true;
}


//*****************************************************************************
//function checkDate(loMonth, loDay, loYear, loSubmit, lDateType)
//
//Purpose: This function verifies the input parameters to be a valid date
//         based on a simple check that the month is between 1-12, the day
//         is between 1-28,29,30,31 as appropriate, and the year is 4 digits
//
//Parameters: loMonth - input month to check, should be a 2 digit number
//            loDay   - input day to check, should be a 2 digit number
//            loYear  - input year to check, should be a 4 digit number
//            loSubmit - submit object to modify the submit action
//            lDateType - type of date being worked on: 0 for "Open" 1 for "Close"
//*****************************************************************************
function checkDate(loMonth, loDay, loYear, loSubmit, lDateType)
{
  lbValid = true;
  lsError = "";
  if (0 == lDateType)
  {
    lDateType = "Open";
  }
  if (1 == lDateType)
  {
    lDateType = "Close";
  }
  
  // Case where default date values will be generated; do nothing here
  if (("" == loMonth.value) && ("" == loDay.value) && ("" == loYear.value))
  {
    lbValid = true;
  }
  // Case where 00/00/0000 has been entered to set and indefinitely open job
  else if (("00" == loMonth.value) && ("00" == loDay.value) && ("0000" == loYear.value))
  {
    lbValid = true;
  }
  // Check if each value is a number...
  else if (!isNaN(loMonth.value) && !isNaN(loDay.value) && !isNaN(loYear.value))
  {
    lnMonth = Number(loMonth.value);
    lnDay = Number(loDay.value);
    
    // Check for valid month value...
    if ((lnMonth < 1) || (lnMonth > 12))
    {
      lbValid = false;
      lsError += "The ";
      lsError += lDateType;
      lsError += " 'Month' value must be between 1 and 12.\r\n";
    }
    else
    {
      switch(lnMonth)
      {
        case 2:
                  if ((lnDay < 1) || (lnDay > 29))
                  {
                    lbValid = false;
                    lsError += "The ";
                    lsError += lDateType;
                    lsError += " 'Day' value must be between 1 and 29.\r\n";
                  }
                  break;
        case 4:   
        case 6:   
        case 9:   
        case 11: if ((lnDay < 1) || (lnDay > 30))
                  {
                    lbValid = false;
                    lsError += "The "
                    lsError += lDateType;
                    lsError += " 'Day' value must be between 1 and 30.\r\n";
                  }
                  break;
        case 1:   
        case 3:   
        case 5:   
        case 7:   
        case 8:   
        case 10:  
        case 12: if ((lnDay < 1) || (lnDay > 31))
                  {
                    lbValid = false;
                    lsError += "The ";
                    lsError += lDateType;
                    lsError += " 'Day' value must be between 1 and 31.\r\n";
                  }
                  break;
        default:
      }
    }
  
    if (4 != loYear.value.length)
    {
      lbValid = false;
      lsError += "The ";
      lsError += lDateType;
      lsError += " 'Year' value must be four digits.\r\n";
    }
  }
  else
  {
    lbValid = false;
    lsError += "The ";
    lsError += lDateType;
    lsError += " date appears to be invalid or incomplete.\r\n";
  }
  
  // Display error and clear submit to the page is re-displayed for user to correct.
  if (!lbValid)
  {
    loSubmit.value = "";
    alert(lsError);
  }
  
  return (lbValid);
}
