//---------- [one.ngn] Content Managent System --------------
//----------    created by zerophobic.com      --------------
//-----------------------------------------------------------
// MODULE	:	validation javascript
// FUNCTION	:	performs form validation
// AUTHOR	:	jim richards <grumpy@cyber4.org>
// CREATED	:	01/10/2001
// MODIFIED	:	11/01/2002
//-----------------------------------------------------------
//-----------------------------------------------------------


/* start form validation code */
// returns true if all the characters in value are in str
function isOnly(value, str) {

    // check if character 'c' from value is in str
    for (var i = 0 ; i < value.length ; i++) {
        var c = value.charAt(i);
        if (str.indexOf(c) == -1)
            return false;
    }
    return true;
}

// function to test for a valid element
function isValid(value) {
    // for for null and empty
    if ((value == null) || (value == ""))
        return false;

    // check for all blank
    return !isOnly(value, " \t\n");
}

// this checks for a valid number
function isValidNumber(value) {

    if (!isValid(value))
        return false;

    return isOnly(value, "0123456789");
}

function isValidCreditMonth(value) {

    if (!isValid(value))
        return false;

    return isOnly(value, "0123456789") && value.length == 2 && 1 <= value && value <= 12;
}


function isValidCreditYear(value) {

    if (!isValid(value))
        return false;

    // tricky as year is NOT Y2K at all, have a 2 digit value between 00 and 99
    return isOnly(value, "0123456789") && value.length == 2 && 0 <= value && value <= 99;
}

// this checks for a phone valid number
function isValidPhoneNumber(value) {

    if (!isValid(value))
        return false;

    // now check for a number, perhaps with '-', '(', ')' and '+' in it ' '
    return isOnly(value, "-|+0123456789() ");
}

// this checks for a valid username
function isValidUsername(value) {
    
    if (!isValid(value))
        return false;

    // Check that length is 4-12.
    // Check that charset is valid.
    return ((value.length >= 4) && (value.length <= 20)) && (isOnly(value, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_"))
}

function isValidPassword(value) {

    if (!isValid(value))
        return false;

    // Check that length is 4-15.
    return ((value.length >= 4) && (value.length <= 15));
}

function isValidEmailAddress(value) {

    if (!isValid(value))
        return false;

    // check that it has some text, a '@' and then some more text
    var at_sign = value.indexOf('@');
    return at_sign != -1 && at_sign != 0 && at_sign != value.length;
}

function validate(data_form) {

    // assume valid, until shown otherwise
    var valid_form = true;

    // the error message string for display
    var empty_elements = "";

    // the first empty element
    var first_element = null;

    // loop through the form looking for fields that are required
    // as these are required fields and must have input.
    for (var i = 0 ; i < data_form.length ; i++) {
        var element = data_form.elements[i];

        // the data is invalid if either
        //  element is not required, something is supplied and it has a validation routine it fails
        //   or
        //  element is required, it has a validation routine it fails or it fails the default one

        if
        (
            (!element.required && (isValid(element.value) && element.validate != null && !element.validate(element.value)))
            ||
            (element.required && ((element.validate != null && !element.validate(element.value)) || !isValid(element.value)))
        )
        {

            // so the form doesn't submit
            valid_form = false;

            // for the error message (clean it up for display)
            empty_elements += "   - " + element.display_name + "\n";

            // set the first element that is empty for focus (and not hidden)
            if (first_element == null && (element.type != "hidden"))
                first_element = element;
        }
    }

    if (!valid_form) {

        var error_message = "";

        // build the error message
        if (empty_elements != "")
            error_message = "These fields do not have correct values:\n" + empty_elements;

        // and display for all to see
        alert(error_message);

        // and jump to the first element
        if ((first_element != null) && (first_element.style.visibility != 'hidden'))
            first_element.focus();

    }

    // if cookie code is being used, the call to cookie.save() is made here
    // if there are no errors in the validation of the form
    // if (valid_form)
        // save_data();

    return valid_form;
}
/* end form validation code */


