/*
** Validation functions used to validate a  
** form.
*/

//---------------------------------------------------------------
// Constants
//---------------------------------------------------------------

/* Custom Escape  */
var AWPX_NO_ESCAPE = "!";
var AWPX_ALERT     = "?";

/* Data Types */
var AWPX_TYPE_BYTE   = "byte";
var AWPX_TYPE_SHORT  = "short";
var AWPX_TYPE_INT    = "int";
var AWPX_TYPE_LONG   = "long";
var AWPX_TYPE_FLOAT  = "float";
var AWPX_TYPE_DOUBLE = "double";
var AWPX_TYPE_STRING = "string";

/* Error message keys */
var AWPX_VALIDATE_MIN_LENGTH = "minLength";
var AWPX_VALIDATE_MAX_LENGTH = "maxLength";
var AWPX_VALIDATE_REGEX      = "regex";
var AWPX_VALIDATE_EXTRA      = "extra";

//---------------------------------------------------------------
// Globals
//---------------------------------------------------------------

var AWPX_ERROR_MSGS = new Array();
AWPX_ERROR_MSGS["type." + AWPX_TYPE_BYTE]   = "Invalid byte";
AWPX_ERROR_MSGS["type." + AWPX_TYPE_SHORT]  = "Invalid short";
AWPX_ERROR_MSGS["type." + AWPX_TYPE_INT]    = "Invalid int";
AWPX_ERROR_MSGS["type." + AWPX_TYPE_LONG]   = "Invalid long";
AWPX_ERROR_MSGS["type." + AWPX_TYPE_FLOAT]  = "Invalid float";
AWPX_ERROR_MSGS["type." + AWPX_TYPE_DOUBLE] = "Invalid double";
AWPX_ERROR_MSGS[AWPX_VALIDATE_MIN_LENGTH]   = "Minimum number of characters not reached";
AWPX_ERROR_MSGS[AWPX_VALIDATE_MAX_LENGTH]   = "Maximum number of characters exceeded";
AWPX_ERROR_MSGS[AWPX_VALIDATE_REGEX]        = "Invalid format";
AWPX_ERROR_MSGS[AWPX_VALIDATE_EXTRA]        = "Invalid";

//---------------------------------------------------------------
// Validation Functions
//---------------------------------------------------------------

/*
 * Perform validation on a particular form element.  This is used
 * primarily to validate text fields.  Returns null if there
 * were no error messages, else returns the error message.
 *
 * element:   Form element or id to validate
 * type:      The data type, see AWPX_TYPE_ constants
 * minLength: Minimum length in characters
 * maxLength: Maximum length in characters or 0 = unbounded
 * regex:     Regular expression used to validate expression
 * regexDesc: Description shown in error message instead of regex
 * expressions: Extra expressions to evaluate against the value.  These
 *              will be evaluates like, "'value' expr", e.g., for an
 *              expression "> 3", it would be "value > 3".
 */
function awpxValidate(element, type, minLength, maxLength, regex, regexDesc)
{
    var el = awpxGetElement(element);
    var error = "";
    var value = el.value;
    
    /* If value exists, then validate it by type */
    if(value)
    {
        if(type == AWPX_TYPE_INT    ||
           type == AWPX_TYPE_SHORT  || 
           type == AWPX_TYPE_LONG   ||
           type == AWPX_TYPE_FLOAT  ||
           type == AWPX_TYPE_DOUBLE ||
           type == AWPX_TYPE_BYTE)
        {
            if(isNaN(value))
            {
                error += awpxGetTypeValidationError(type, value);
            }
            else if(type == AWPX_TYPE_BYTE)
            {
                var bValue = parseInt(value);
                if(bValue < -128 || bValue > 127)
                {
                    error += awpxGetTypeValidationError(type, value);
                }
            }
            else if(type == AWPX_TYPE_SHORT)
            {
                var sValue = parseInt(value);
                if(sValue < -32768 || sValue > 32767)
                {
                    error += awpxGetTypeValidationError(type, value);
                }
            }
        }
    }

    /* Perform length checking, independent of type */
    if(minLength && value.length < minLength)
    {
        error += awpxGetValidationError(AWPX_VALIDATE_MIN_LENGTH, minLength);
    }
    
    if(maxLength && maxLength > 0 && value.length > maxLength)
    {
        error += awpxGetValidationError(AWPX_VALIDATE_MAX_LENGTH, maxLength);
    }

    /* Perform regular expression checking */
    if(regex)
    {
        if(!regex.test(value.toString()))
        {
            var msg = (regexDesc ? regexDesc : regex);
            error += awpxGetValidationError(AWPX_VALIDATE_REGEX, msg);
        }
    }
                
    /* Evaluate any extra expressions against the value */
    for(var i=8; i<arguments.length; i++)
    {
        var res = eval("'" + value + "'" + arguments[i]);
        if(!res)
        {
            error += awpxGetValidationError(AWPX_VALIDATE_EXTRA, arguments[i]);
        }
    }

    /* If success, then just return */
    if(error.length <= 1)
    {
        return null;
    }
    else
    {
        return error;
    }
}    

/*
 * Perform validation on a particular form element, then brings up
 * a dialog indicating what the problems were.  This is used
 * primarily to validate text fields.  Returns true if all was validate,
 * else false.
 *
 * element:   Form element or id to validate
 * errorMsg:  Error message to display in the popup if validation fails
 * escapeMsg: Message used if you have an option in the dialog.
 *            Use AWPX_NO_ESCAPE if no option, always fails
 *            Use AWPX_ALERT if no option, always succeeds
 * type:      The data type, see AWPX_TYPE_ constants
 * minLength: Minimum length in characters
 * maxLength: Maximum length in characters or 0 = unbounded
 * regex:     Regular expression used to validate expression
 * regexDesc: Description shown in error message instead of regex
 * expressions: Extra expressions to evaluate against the value.  These
 *              will be evaluates like, "'value' expr", e.g., for an
 *              expression "> 3", it would be "value > 3".
 */
function awpxValidateWithDialog(element, errorMsg, escapeMsg, type, 
                                minLength, maxLength, regex, regexDesc)
{
    var el = awpxGetElement(element);
    var error = awpxValidate(element, type, minLength, maxLength, regex, regexDesc);

    /* If success, then just return */
    if(!error)
    {
        return true;
    }
                
    if(!errorMsg)
    {
        errorMsg = "Invalid entry.  Please check the value:";
    }
    
    /* If user has option, then show confirm dialog */
    if(escapeMsg != AWPX_NO_ESCAPE && escapeMsg != AWPX_ALERT)
    {
        if(!confirm(errorMsg + "\n" + error + "\n\n" + escapeMsg))
        {
            return true;
        }
        el.focus();
        return false;
    }

    /* If user doesn't have option, show alert dialog */
    alert(errorMsg + "\n" + error);
    el.focus();

    return (escapeMsg == AWPX_NO_ESCAPE ? false : true);
}

/*
 * Returns the validation message key for the given type
 *
 * type: The data type
 */
function awpxGetTypeKey(type)
{
    return "type." + type;
}

/*
 * Initializes error messages based on their keys 
 */
function awpxSetValidationMsg(key, msg)
{
    AWPX_ERROR_MSGS[key] = msg;
}

/*
 * Returns the error message based on the key.
 */
function awpxGetValidationMsg(key)
{
    return AWPX_ERROR_MSGS[key];
}

/*
 * Returns the given type validation message
 */
function awpxGetTypeValidationError(type, value)
{
    return awpxGetValidationError(awpxGetTypeKey(type), value);
}

/*
 * Returns the given validation message 
 */
function awpxGetValidationError(key, value)
{
    return "\n" + awpxGetValidationMsg(key) + ": " + value;
}
