// Form validation script

// A utility function that returns true if a string contains only
// whitespace characters.
function isblank(s)
{
        for(var i = 0; i < s.length; i++) {
                var c = s.charAt(i);
                if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
        }
        return true;
}

// Check to see if a string contains any non numeric characters

function nonNum(s) {
        for(var i = 0; i < s.length; i++) {
                var c = s.charAt(i);
                var numeric = false
                for(var k = 0; k < 10; k++)
                        if (k == c) numeric = true;

                if (numeric == false && c != " ") return true;
        }

        return false;
}


// This is the function that performs form verification.  It will be invoked
// from the onSubmit() event handler.  The handler should return whatever
// value this function returns.
// usage: verify(document.nameofform);
// NB. make sure that the names of the elements are correct, otherwise it
// will not perform the validation.
function verify(f)
{
        var msg;
        var empty_fields = "";
        var errors = "";

        // Loop through the elements of the form, looking for all
        // text and textarea elements that don't have an "optional" property
        // defined.  Then, check for fields that are empty and make a list of them.
        // Also, if any of these elements have a "min" or a "max" property defined,
        // then verify that they are numbers and that they are in the right range.
        // Put together error messages for fields that are wrong.
        for(var i = 0; i < f.length; i++) {
                var e = f.elements[i];
                var ename;

                // Get actual name
                if (e.actname == "" || e.actname == null) ename = e.name;
                else ename = e.actname;

                if (((e.type == "text") || (e.type == "textarea") || (e.type == "select-one") || (e.type == "checkbox")) && (!e.optional)) {
                        // first check if the field is empty
                        if (e.type == "select-one" && document.layers) {

                                var opIndex = e.selectedIndex;

                                if(e.options[opIndex].value == null || e.options[opIndex].value == "") {
                                                empty_fields += ", " + ename;
                                                continue;

                                } else continue;

                        } else if ((e.value == null) || (e.value == "") || isblank(e.value)) {
                                empty_fields += ", " + ename;
                                continue;
                        }

                        // check for valid email address
                        if (e.email || (e.value == null) || (e.value == "")) {
                                if (e.value.indexOf('@') < 0)
                                        errors += "The field '" + ename + "' must be a valid email address.\n";
                                continue;
                        }

                        // Now check for fields that are supposed to be numeric.
                        if (e.numeric || (e.min != null) || (e.max != null)) {
                                var v = e.value;
                                if (nonNum(v) ||
                                        ((e.min != null) && (v < e.min)) ||
                                        ((e.max != null) && (v > e.max))) {
                                        errors += "The field '" + ename + "' must be a numerical value";
                                        if (e.min != null)
                                                errors += " that is greater than " + e.min;
                                        if (e.max != null && e.min != null)
                                                errors += " and less than " + e.max;
                                        else if (e.max != null)
                                                errors += " that is less than " + e.max;
                                        errors += ".\n";
                                }
                        }
                }
        }

        // Now, if there were any errors, then display the messages, and
        // return true to prevent the form from being submitted.  Otherwise
        // return false
        if (!empty_fields && !errors) return true;

        msg  = "Sorry, some of the information we need is incorrect. Please go back and try again.\n\n";
        if (empty_fields) {
                empty_fields = empty_fields.substr(1);
                msg += "The following required field(s) are empty;\n\n"
                        + empty_fields + "\n\n";
                if (errors) msg += "";
        }
        msg += errors;
        alert(msg);
        return false;
}

// The following function counts all the check boxes with
// the same name and makes sure there are more checked then
// the num variable is set to.
// usage: countCheckBoxes(thisform,'brochureIDs',3);
function countCheckBoxes(f,tocheck,num)
{
        var msg;
        var empty_fields = '';
        var errors = '';
        var count = 0;
        for(var i = 0; i < f.length; i++) {
                var e = f.elements[i];
                if ((e.type == 'checkbox') && (e.name == tocheck) && (e.checked)) {
                        count++;
                }
        }
        if (count > num) {
                errors += 'You can only select a maximum of three brochures.\n';
        }
        if (count <= 0) {
                errors += 'You have to select at least one brochure.\n';
        }
        // Now, if there were any errors, then display the messages, and
        // return true to prevent the form from being submitted.  Otherwise
        // return false
        if (!empty_fields && !errors) return true;
        msg = 'Please go back and try again.\n';
        msg += errors;
        alert(msg);
}

function performValidate()
{
}