// Originally from
// <http://simon.incutio.com/archive/2004/05/26/addLoadEvent>
// @(#) $Id: public.js 8068 2005-10-07 11:18:53Z peter $

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function check_free_access_code (access_code) {
  return true;
}

function check_required_fields () {
    foundempty=0;
    for (i=0; i<arguments.length; i++) {
        eval ('var isundef=0; if (document.forms.main.' + arguments[i] + '== undefined) { isundef++ } ');
        if (isundef) { // if the field doesn't appear on the page, don't validate it
            continue;
        }
        eval ('myval = document.forms.main.' + arguments[i] + '.value');
        if (! myval) {
            alert ("Please fill in the required fields (" + arguments[i] + ")");
            foundempty++;
            break;
        } 
    }
    return foundempty ? false : true;
}

function check_email () {
    if (document.forms.main.email == undefined) { // dont' check if not present on form
        return true;
    }
    var email = document.forms.main.email.value;
    if ((email == "") || (email.indexOf ('@') == -1) || (email.indexOf ('.') == -1)) {
        alert ("Please enter a valid email address");
        return false;
    } else {
       return true;
    }
}

function check_userpass () {
    if (document.forms.main.userpass == undefined) { // dont' check if not present on form
        return true;
    }
    var input = document.forms.main.userpass.value;
    var lines=[];
    lines = input.split(/[\n\r]/);
    for (var i=0; i < lines.length; i++) {
        if (lines[i] != '' && ! isValidUserpass(lines[i])) {
            return false;
        }
    }
    return true;
}

function isValidUserpass (input) {
    var re = /\//;
    var inputparts=[];
    if (re.test(input)) {
        // contains /
        inputparts = input.split(re);
        if (inputparts[0].length < 6
         || inputparts[1].length < 6) {
            alert (input + " : Username and password must be at least 7 characters");
            return false;
        }
    } else {
        alert ("Please check the format of the username field");
        return false;
    }
    return true;
}



// vim: set ai et sw=4 :
