function account_validate(formData, jqForm, options) {
    var form = jqForm[0];

    var id = (form.id.value != undefined) ? form.id.value : '';
    var email = trim(form.email.value);
    var password = trim(form.password.value);
    var fname = trim(form.fname.value);
    var lname = trim(form.lname.value);

    var error_msg = "Please correct the following errors:\n\n";
    var error_found = false;

    //alert('id: ' + id + ' email: ' + email + ' password: ' + password + ' lname: ' + lname + ' fname: ' + fname);

    $.ajax({
        async: false,
        type: 'POST',
        url: 'account_email_validate.php',
        data: 'email=' + email + '&id=' + id,
        success: function(xml_response) {
            var duplicate_exists = $('duplicate', xml_response).text();

            if (duplicate_exists == 'TRUE') {
                error_msg = error_msg + " * An account has already been created for " + email + "\n";
                error_found = true;
            }
        }
    });

    if (!lname) {
        error_msg = error_msg + " * Please provide a last name\n";
        error_found = true;
    }

    if (!fname) {
        error_msg = error_msg + " * Please provide a first name\n";
        error_found = true;
    }

    if (!email) {
        error_msg = error_msg + " * Please provide an email address\n";
        error_found = true;
    }
    else if (!email.match(/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-\+])+\.)+([a-zA-Z0-9]{2,4})+$/)) {
        error_msg = error_msg + " * Please provide a valid email address\n";
        error_found = true;
    }

    if (password.length < 6) {
        error_msg = error_msg + " * Please provide a password with at least six characters\n";
        error_found = true;
    }

    if (error_found) {
        alert(error_msg);
        return false;
    }

    return true;
}

function trim(str) {
    return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function redirect(location) {
    window.location = location;
}
