/*******************************************************************************

  File:    validate.js
  Author:  bwl
  Date:    02/16/99

  This script file contains functions that validate user inputs.

  Each of these functions will return the focus to the object, if the input is
  invalid.  Therefore, it is possible to cause a deadlock as multiple objects
  request for focus.  To avoid the problem, the object that currently has the
  focus will be recorded.

  An object is allowed to keep the focus for a maximum number of times,
  specified by MaxAlerts.  When the maximum is reached, the focus will be
  released and any object can get the focus.

*******************************************************************************/


// Global Constants
// ----------------
MaxAlerts = 3			// Max# of alerts to be sent regarding an error


// Global Variables
// ----------------
var numAlerts = 0		// number of alerts displayed for an object
var objOnFocus = ""		// name of the object that needs the focus


/*------------------------------------------------------------------------------
  Function   :  CheckNum
  Purpose    :  Check the value in a text box against an upper and a lower
                value.  If it is out of range, send an alert.
  Applies To :  Text
  Parameter  :
    high - the upper value; "any" means no upper limit
    low  - the lower value; "any" means no lower limit
    obj  - the text object
  Returns    :
    true  - value is within the range
    false - value is not an integer, or value is out of range
------------------------------------------------------------------------------*/
function CheckNum(obj, low, high) {

    var condition = "";
    var message = "(must be";


    // continue if the input is not blank
    if (NotBlank(obj, "Blank is not allowed") == false) {
        return false;
    }


    // continue if the input is an integer
    if (IsInt(obj) == false) {
        return false;
    }


    // determine the condition(s) to check
    if (low == 0 || low != "any") {
        condition = "obj.value < low";
        message = message + " >= " + low;
    }

    if (high == 0 || high != "any") {
        if (condition != "") {
            condition += " || obj.value > high";
            message = message + " and <= " + high;
        }
        else {
            condition = "obj.value > high";
            message = message + " <= " + high;
        }
    }

    message += ")";


    if (eval(condition)) {

        // To avoid two objects fighting for the focus, ONLY set focus and
        // display alert IF there is no other object needs the focus.
        if (objOnFocus == "" || objOnFocus == obj.name) {

            // If the max# of alerts has been sent, abort the validation.
            if (numAlerts < MaxAlerts) {
                numAlerts = numAlerts + 1;
                objOnFocus = obj.name;
                alert("Input Out of Range " + message);
                obj.select();
                obj.focus();
            }
            else {
                numAlerts = 0;
                objOnFocus = "";
            }
        }

        return false;
    }

    // Reset the global variables as this object no longer needs the focus.
    if (objOnFocus == obj.name) {
        numAlerts = 0;
        objOnFocus = "";
    }

    return true;

}  // CheckNum


/*------------------------------------------------------------------------------
  Function   :  IsInt
  Purpose    :  Determine whether the input is an integer.
  Applies To :  Text
  Parameter  :
    obj  - the text object
  Returns    :
    true  - input is an integer
    false - input is not an integer
------------------------------------------------------------------------------*/
function IsInt(obj) {

    var intval;
    var intfl = true;
    var pos = 0;
    var strval = obj.value;


    // remove leading 0's
    while (strval.charAt(pos) == "0") {
        pos++;
    }

    if (pos == strval.length) {
        strval = 0;
    }
    else {
        strval = strval.substring(pos, strval.length);
    }

    intval = parseInt(strval);

    if (isNaN(intval) || (intval == 0 && strval != 0)) {
        intfl = false;
    }

    intval = intval.toString();
    if (intval != strval) {
        intfl = false;
    }

    if (intfl == false) {

        // To avoid two objects fighting for the focus, ONLY set focus and
        // display alert IF there is no other object needs the focus.
        if (objOnFocus == "" || objOnFocus == obj.name) {

            // If the max# of alerts has been sent, abort the validation.
            if (numAlerts < MaxAlerts) {
                numAlerts = numAlerts + 1;
                objOnFocus = obj.name;
	        alert(obj.value + " is not an integer");
                obj.select();
                obj.focus();
            }
            else {
                numAlerts = 0;
                objOnFocus = "";
            }
        }

        return false;
    }

    // Reset the global variables as this object no longer needs the focus.
    if (objOnFocus == obj.name) {
        numAlerts = 0;
        objOnFocus = "";
    }

    return true;

}  // IsInt


/*------------------------------------------------------------------------------
  Function   :  NotBlank
  Purpose    :  Check whether a text box is blank.  If it is blank, send an
                alert.
  Applies To :  Text
  Parameter  :
    obj - the text object
    msg - the error message
  Returns    :
    true  - text box is not blank
    false - it is blank
------------------------------------------------------------------------------*/
function NotBlank(obj, msg) {

    if (obj.value == "") {

        // To avoid two objects fighting for the focus, ONLY set focus and
        // send alert IF there is no other object needs the focus.
        if (objOnFocus == "" || objOnFocus == obj.name) {

            // If the max# of alerts has been sent, abort the validation.
            if (numAlerts < MaxAlerts) {
                numAlerts = numAlerts + 1;
                objOnFocus = obj.name;
                alert(msg);
                obj.focus();
            }
            else {
                numAlerts = 0;
                objOnFocus = "";
            }
        }

        return false;
    }

    // Reset the global variables as this object no longer needs the focus.
    if (objOnFocus == obj.name) {
        numAlerts = 0;
        objOnFocus = "";
    }

    return true;

}  // NotBlank
