/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*$RCSfile: validateUtilities.js,v $ $Rev: 478676 $ $Date: 2006-11-23 21:35:44 +0000 (Thu, 23 Nov 2006) $ */
/**
 * This is a place holder for common utilities used across the javascript validation
 *
 **/

/**
 * Retreive the name of the form
 * @param form The form validation is taking place on.
 */
function jcv_retrieveFormName(form) {

    // Please refer to Bugs 31534, 35127, 35294, 37315 & 38159
    // for the history of the following code

    var formName;

    if (form.getAttributeNode) {
        if (form.getAttributeNode("id") && form.getAttributeNode("id").value) {
            formName = form.getAttributeNode("id").value;
        } else {
            formName = form.getAttributeNode("name").value;
        }
    } else if (form.getAttribute) {
        if (form.getAttribute("id")) {
            formName = form.getAttribute("id");
        } else {
            formName = form.attributes["name"];
        }
    } else {
        if (form.id) {
            formName = form.id;
        } else {
            formName = form.name;
        }
    }

    return formName;

}


function jcv_displayErrorsAsAlert(messages, displayOrder) {
    var list = "";

    if (displayOrder) {
        var len = displayOrder.length;
        if (len > 0) {
            // show in order
            for (i = 0; i < len; i++) {
                var key = displayOrder[i];
                var message = messages[key];
                if (message) {
                    list += message + '\n';
                }
            }
        } else {
            for (key in messages) {
                list += messages[key] + '\n';
            }
        }
    } else {
        for (key in messages) {
            list += messages[key] + '\n';
        }
    }
    alert(list);
}


function jcv_displayErrorsAsList(container, messages, displayOrder) {

    var errorDiv = document.getElementById(container);

    if (!errorDiv) {
        alert('Cannot find container element: ' + container)
        return;
    }

    var listHTML = '<ul>\n';

    if (displayOrder) {
        var len = displayOrder.length;
        if (len > 0) {
            // show in order
            for (i = 0; i < len; i++)
            {
                var key = displayOrder[i];
                var message = messages[key];
                if (message) {
                    listHTML += '<li>' + message + '</li>\n';
                }
            }
        } else {
            for (key in messages) {
                listHTML += '<li>' + messages[key] + '</li>\n';
            }
        }
    } else {
        for (key in messages) {
            listHTML += '<li>' + messages[key] + '</li>\n';
        }
    }


    listHTML += '<ul>\n';
    errorDiv.innerHTML = listHTML;
    errorDiv.style.display = "block";

}

function jcv_focusField(focusField) {
    if (focusField && focusField != null) {
        var doFocus = true;
        if (focusField.disabled || focusField.type == 'hidden') {
            doFocus = false;
        }
        if (doFocus &&
            focusField.style &&
            focusField.style.visibility &&
            focusField.style.visibility == 'hidden') {
            doFocus = false;
        }
        if (doFocus) {
            focusField.focus();
        }
    }
}

function returnObjById(id)
{
    if (document.getElementById)
        var returnVar = document.getElementById(id);
    else if (document.all)
        var returnVar = document.all[id];
    else if (document.layers)
            var returnVar = document.layers[id];
    return returnVar;
}

/**
 * Checks that the array element is a valid
 * Commons Validator element and not one inserted by
 * other JavaScript libraries (for example the
 * prototype library inserts an "extends" into
 * all objects, including Arrays).
 * @param name The element name.
 * @param value The element value.
 */
function jcv_verifyArrayElement(name, element) {
    if (element && element.length && element.length >= 2) {
        return true;
    } else {
        return false;
    }
}

/**
 * Checks whether the field is present on the form.
 * @param field The form field.
 */
function jcv_isFieldPresent(field) {
    var fieldPresent = true;
    if (field == null || (typeof field == 'undefined')) {
        fieldPresent = false;
    } else {
        if (field.disabled) {
            fieldPresent = false;
        }
    }
    return fieldPresent;
}

/**
 * Check a value only contains valid numeric digits
 * @param argvalue The value to check.
 */
function jcv_isAllDigits(argvalue) {
    argvalue = argvalue.toString();
    var validChars = "0123456789";
    var startFrom = 0;
    if (argvalue.substring(0, 2) == "0x") {
        validChars = "0123456789abcdefABCDEF";
        startFrom = 2;
    } else if (argvalue.charAt(0) == "0") {
        validChars = "01234567";
        startFrom = 1;
    } else if (argvalue.charAt(0) == "-") {
        startFrom = 1;
    }

    for (var n = startFrom; n < argvalue.length; n++) {
        if (validChars.indexOf(argvalue.substring(n, n + 1)) == -1) return false;
    }
    return true;
}

/**
 * Check a value only contains valid decimal digits
 * @param argvalue The value to check.
 */
function jcv_isDecimalDigits(argvalue) {
    argvalue = argvalue.toString();
    var validChars = "0123456789";

    var startFrom = 0;
    if (argvalue.charAt(0) == "-") {
        startFrom = 1;
    }

    for (var n = startFrom; n < argvalue.length; n++) {
        if (validChars.indexOf(argvalue.substring(n, n + 1)) == -1) return false;
    }
    return true;
}
