// CLIENT JS:  cfuncs.js
//
// PURPOSE  :  This file contains client-side JavaScripts that perform
//             field validations used by all pages.
//
///////////////////////////////////////////////////////////////////////////////

//********************************************************************
//  function:    isEmpty
//  description: General purpose function to see if an input value 
//               has been entered at all
//  input:
//               inputStr
//
// return values:
//               true  - no value entered
//               false - value entered
//********************************************************************
function isEmpty( inputStr ) {

  if ( inputStr == null || inputStr == "" ) {
    return true;
  }
  return false;
}

//********************************************************************
//  function:    isBlank
//  description: General purpose function to see if an
//               input is only spaces
//  input:
//               inputVal
//
// return values:
//               true  - space-only value entered
//               false - non space-only value entered
//********************************************************************
function isBlank( nFieldNo, inputVal ) {

  var inputStr  = inputVal.value.toString();
  var foundChar = false;

  for ( var i = 0; i < inputStr.length; i++ ) {
    var oneChar = inputStr.charAt(i);
    if ( oneChar != ' ' ) {
      foundChar = true;
    }
  }

  if ( foundChar ) { return false; }
  else {
    window.alert( strMsgArray[ nFieldNo  ] );
    inputVal.focus();
    inputVal.select();
    return true;
  }

}

//********************************************************************
//  function:    isPosInteger
//  description: General purpose function to see if a suspected numeric 
//               input is a positive integer
//  input:
//               inputVal
//
// return values:
//               true  - positive integer entered
//               false - positive integer not entered
//********************************************************************
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;
}

//********************************************************************
//  function:    isValidNumber
//  description: 
//  input:
//               nFieldNo  index to field being validated 
//               inputVal  form field to validate
//  return:  
//               true  - if value checked is valid 
//               false - if value is not valid
//********************************************************************
function isValidNumber( nFieldNo, inputVal ) {

  if ( !isEmpty( inputVal.value ) ) {
    if ( !isPosInteger( inputVal.value ) ) {
      window.alert( strMsgArray[ nFieldNo ] );
      inputVal.focus();
      inputVal.select();
      return false;
    }
  }
  if ( !isValidLength( nFieldNo, inputVal ) ) { return false; }

  return true;
}

//********************************************************************
//  function:    isValidLength
//  description: 
//  input:
//               nFieldNo  index to field being validated 
//               inputVal  form field to validate
//  return:  
//               true  - if value checked is valid 
//               false - if value is not valid
//********************************************************************
function isValidLength( nFieldNo, inputVal ) {

  if ( isEmpty( inputVal.value ) ) {
    if ( nFieldLengthArray[ nFieldNo ] > 0 ) {
      window.alert( strMsgArray[ nFieldNo ] );
      inputVal.focus();
      inputVal.select();
      return false;
    }
  }
  else {
    var inputStr = inputVal.value.toString();

    if ( inputStr.length < nFieldLengthArray[ nFieldNo  ] ) {
      window.alert( strMsgArray[ nFieldNo  ] );
      inputVal.focus();
      inputVal.select();
      return false;
    }
  }
  return true;
}

//********************************************************************
//  function:    initForm
//  description: Place cursor at the specified entry field of form 
//               on  form load and reset
//  input:       form
//  return:      none
//  usage:       invoke function 'onLoad' of DOCUMENT and 'onReset' of FORM
//********************************************************************
function initForm( form, element ) {

  // Subtract one because element count starts at 0 while 
  // list of fields starts at 1
  form.elements[element-1].focus();

}


