function findField(field) { return document.getElementById('field' + field); } function markError(field) { document.getElementById('status'+field).src = 'img/field_error.png'; } function markClean(field) { document.getElementById('status'+field).src = 'img/field_unfilled.png'; } function markOk(field) { document.getElementById('status'+field).src = 'img/field_ok.png'; } function checkvalue(field, regex, reqFill) { var r = new RegExp(regex); var f = 'field' + field; var s = 'status' + field; if (/^$/.test(document.getElementById(f).value)) { if (reqFill == 1) { return false; } else { return true; } } else { if (r.test(document.getElementById(f).value)) { return true; } else { return false; } } } function checkequal(field, casesensitive) { var this_val = document.getElementById('field' + field).value; var that_val = document.getElementById('verification' + field).value; if (!casesensitive) { this_val = this_val.toLowerCase(); that_val = that_val.toLowerCase(); } return (this_val && that_val && this_val == that_val); } var triggers = new Array() var fields = new Array() var inactivefields = new Array() function isinactive(f) { if (f) { for (j = 0; j < inactivefields.length; j++) { if (inactivefields[j] == f) { return true; } } } return false; } function checkselection(field,reqFill) { var f = 'field' + field; var s = 'status' + field; if (document.getElementById(f).selectedIndex == 0) { if (reqFill == 1) { return false; } else { return true; } } else { return true; } } function markinactive(f) { if (f && !isinactive(f)) { inactivefields.push(f); } } function markactive(f) { for (k = 0; k < inactivefields.length; k++) { if (inactivefields[k] == f) { inactivefields.splice(k, 1); k--; } } } function checktriggers() { for (i = 0; i < triggers.length; i++) { c = document.getElementById(triggers[i].condition) test = false; if (c.type == "checkbox") { test = c.checked; } else { test = triggers[i].match.test(c.value); } if (test) { markactive(triggers[i].field); document.getElementById(triggers[i].target).style.display = 'table-row'; } else { markinactive(triggers[i].field); document.getElementById(triggers[i].target).style.display = 'none'; } } } function addtrigger(target, condition, match, field) { triggers.push({target:target, condition:condition, match:new RegExp(match), field:field}); } function addcheck(field, check, type, required) { fields.push({field:field, type:type, check:check, required:required}); } function checkfield(field) { checktriggers(); if (isinactive(field)) { markOk(field); } else { for (i = 0; i < fields.length; i++) { o = fields[i] if (o.field == field) { if (o.type == 'field') { if (checkvalue(o.field, o.check, o.required)) { markOk(o.field); } else { markError(o.field); } } else if (o.type == 'selection') { if (checkselection(o.field,o.required)) { markOk(o.field); } else { markError(o.field); } } else if (o.type == 'equal') { if (!checkequal(o.field, false)) { markError(o.field); } } else if (o.type == 'custom') { if ((o.check)()) { markOk(o.field); } else { if (o.required) { markError(o.field); } } } } } } } function checkfields(markerrors, caller) { checktriggers(); errors = false; errorfield = ''; for (i = 0; i < fields.length; i++) { o = fields[i]; if (isinactive(o.field)) { markOk(o.field); } else { if (o.type == 'field') { if (checkvalue(o.field, o.check, o.required)) { markOk(o.field); } else { errors = true; if (caller) { markError(o.field); } else { markClean(o.field); } if (errorfield == '') { errorfield = o.field; } } } else if (o.type == 'selection') { if (checkselection(o.field,o.required)) { markOk(o.field); } else { errors = true; if (caller) { markError(o.field); } else { markClean(o.field); } if (errorfield == '') { errorfield = o.field; } } } else if (o.type == 'custom') { if ((o.check)()) { markOk(o.field); } else { if (o.required) { errors = true; if (errorfield == '') { errorfield = o.field; } markError(o.field); } else { markClean(o.field); } } } } } if (errors) { document.getElementById('field'+errorfield).focus(); } if (caller != null && !errors) { document.getElementById(caller).submit(); } }