//Field Validation javaScript

function checkNumeric(theField) {
  fldValue = theField.value
  for (var i = 0; i <fldValue.length;  i++) {
    var ch = fldValue.substring(i,i+1)
    if (fldValue != "" && (ch < "0" || "9" < ch)) {
      alert("This field must be numeric.")
      theField.focus()
      theField.select()
      return(false)
      break
      }
    } return(true)
}

function checkReal(theField) {
  fldValue = theField.value
  for (var i = 0; i <fldValue.length;  i++) {
    var ch = fldValue.substring(i,i+1)
    if (fldValue != "" && (ch < "0" || "9" < ch ) && ch != ".") {
      alert("This field must be numeric.")
      theField.focus()
      theField.select()
      return(false)
      break
      }
    } return(true)
}

function checkRealTBD(theField) {
  fldValue = theField.value
  for (var i = 0; i <fldValue.length;  i++) {
    var ch = fldValue.substring(i,i+1)
    if (fldValue != "" && ((ch < "0" || "9" < ch ) && ch != ".") && fldValue != "NA") {
      alert("This field must be numeric or \"NA\".")
      theField.focus()
      theField.select()
      return(false)
      break
      }
    } return(true)
}

function check_fields(CTLFORM){


  status = reqfields(CTLFORM,x);
  if (status == 'false'){
      return (false)
  }
  return (true);
 } 

function reqfields(CTLFORM,x) {
      // Scott Stadnicki of CSC Copyright 2001 --
      // build array of required controls from array
      // loop through form and see if control equals
      // element in array.  If it does and is null put
      // control name in another array for display.
      // example: x = field1^prompt1^field2^prompt2...
      
        var str = "";
        var count = 0;
        var ind;
        reqarray = new Array();
        v_prompt = new Array();
        missing = new Array();

        if (navigator.appVersion.indexOf('MSIE 3') > -1) {
           v_prompt = MIESplit(x, "^");
        }
        else {
           v_prompt = x.split("^");
        }

        for(i = 0; i < CTLFORM.elements.length; i++)
        {
          if (x.indexOf(CTLFORM.elements[i].name)>-1)
          {
             flag = 0;
             if (CTLFORM.elements[i].type == "select-one" || CTLFORM.elements[i].type == "select-multiple")
             {
               ind = CTLFORM.elements[i].selectedIndex;
               if (ind == -1)
               {
                  flag = 1;
               }
               else
               {
                 if (CTLFORM.elements[i].options[ind].value=="" || CTLFORM.elements[i].options[ind].value=="0")
                 {
                    flag = 1;
                 }
               }
             }
             else
             {
	       if (CTLFORM.elements[i].name=="ProgramName")
		{
		   if (CTLFORM.elements[i].value.length > 50)
			{
			  alert("You have entered to many characters for \"Program\". Please enter 50 characters or less.");
			  CTLFORM.elements[i].focus();
			  return (false);
			}
		}
	       if (CTLFORM.elements[i].name=="PointofContact")
		{
		   if (CTLFORM.elements[i].value.length > 50)
			{
			  alert("You have entered to many characters for \"Point of Contact\". Please enter 50 characters or less.");
			  CTLFORM.elements[i].focus();
			  return (false);
			}
		}
	       if (CTLFORM.elements[i].name=="POCPhone")
		{
		   if (CTLFORM.elements[i].value.length > 15)
			{
			  alert("You have entered to many characters for \"Point of Contact Phone\". Please enter 15 characters or less.");
			  CTLFORM.elements[i].focus();
			  return (false);
			}
		}

               if (CTLFORM.elements[i].value=="")
               {
                  flag = 1;
               }
             }
             if (flag == 1)
             {
               reqarray[count]=CTLFORM.elements[i].name;
               // find piece number of v_prompt
               for(j = 0; j < v_prompt.length; j=j+2)
               {
                  if (v_prompt[j]==CTLFORM.elements[i].name)
                  {
                     missing[count]=v_prompt[j+1];
                  } 
               }
               count = count +1;
             }
          }
        }
        for(i = 0; i < missing.length; i++)
        {
         str = str + missing[i] + "\n";
        }
         if (missing.length>0){
            alert("\nThe following field(s) are required that you have not filled in:" + "\n\n" + str);
            CTLFORM.elements[reqarray[0]].focus();
            return (false);
         }
        return (true);
}



/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Cyanide_7 |  */
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();
  }

  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;
}
