2007-07-19 12:40:28 +02:00
|
|
|
var _CURRENT_FORM;
|
|
|
|
|
|
|
|
function initialiseForm(form, fromAnOnBlur) {
|
|
|
|
_CURRENT_FORM = form;
|
2007-09-16 18:54:20 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(fromAnOnBlur) {
|
|
|
|
limitValidationErrorsTo(fromAnOnBlur);
|
|
|
|
} else {
|
|
|
|
clearValidationErrorLimit();
|
|
|
|
}
|
2007-09-16 18:54:20 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
_HAS_HAD_FORM_ERROR = false;
|
|
|
|
clearValidationErrorCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
function hasHadFormError() {
|
|
|
|
return _HAS_HAD_FORM_ERROR || !_ERROR_CACHE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns group with the correct classname
|
|
|
|
*/
|
2007-09-16 18:54:20 +02:00
|
|
|
function findIndexOf(group,index) {
|
|
|
|
var i;
|
|
|
|
for(i = 0; i < group.length; i++) {
|
|
|
|
if(group[i].className.indexOf(index) > -1) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return group[i];
|
|
|
|
}
|
|
|
|
}
|
2007-09-16 18:54:20 +02:00
|
|
|
return null;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function clearErrorMessage(holderDiv){
|
2007-08-23 07:47:54 +02:00
|
|
|
//merged by nlou 23/08/2007, r#40674
|
|
|
|
if(holderDiv.tagName == 'TD'){//for tablefield.
|
|
|
|
$$('span.message', holderDiv).each(function(el){
|
|
|
|
Element.hide(el);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}else{
|
|
|
|
$$('span.message', holderDiv.parentNode).each(function(el) {
|
|
|
|
Element.hide(el);
|
|
|
|
});
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-09-16 18:54:20 +02:00
|
|
|
function clearAllErrorMessages() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$$('span.message').each(function(el) {
|
|
|
|
Element.hide(el);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function require(fieldName,cachedError) {
|
|
|
|
el = _CURRENT_FORM.elements[fieldName];
|
2007-09-16 18:54:20 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// see if the field is an optionset
|
2007-09-16 18:54:20 +02:00
|
|
|
if(el == null) {
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
var descendants = _CURRENT_FORM.getElementsByTagName('*');
|
2007-09-16 18:54:20 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
el = $(fieldName);
|
2007-09-16 18:54:20 +02:00
|
|
|
|
|
|
|
if(el == null)
|
2007-07-19 12:40:28 +02:00
|
|
|
return true;
|
2007-09-16 18:54:20 +02:00
|
|
|
|
|
|
|
if(Element.hasClassName(el, 'optionset')) {
|
|
|
|
el.type = 'optionset';
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
var options = el.getElementsByTagName('input');
|
2007-09-16 18:54:20 +02:00
|
|
|
|
|
|
|
for(var i = 0; i < options.length; i++) {
|
|
|
|
if(options[i].checked)
|
|
|
|
if(el.value != null)
|
2007-07-19 12:40:28 +02:00
|
|
|
el.value += ',' + options[i].value;
|
|
|
|
else
|
|
|
|
el.value = options[i].value;
|
|
|
|
}
|
|
|
|
}
|
2007-09-16 18:54:20 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-09-16 18:54:20 +02:00
|
|
|
|
|
|
|
if(el != null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
// Sets up radio and checkbox validation
|
2007-09-16 18:54:20 +02:00
|
|
|
if(el.type == 'checkbox' || el.type == 'radio') {
|
2007-07-19 12:40:28 +02:00
|
|
|
var set = el.checked;
|
2007-08-23 07:47:54 +02:00
|
|
|
}//merged by nlou 23/08/2007, r#40674
|
|
|
|
else if(el.type == 'select-one'){
|
|
|
|
if(el.value == ''||el.value == '0'){
|
|
|
|
var set = '';
|
|
|
|
}else{
|
|
|
|
var set = el.value;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}else{
|
|
|
|
var set = el.value;
|
|
|
|
}
|
2007-09-16 18:54:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
var baseEl;
|
2007-09-16 18:54:20 +02:00
|
|
|
|
|
|
|
// Sometimes require events are triggered of
|
2007-07-19 12:40:28 +02:00
|
|
|
// associative elements like labels ;-p
|
2007-09-16 18:54:20 +02:00
|
|
|
if(el.type) {
|
|
|
|
if(el.parentNode.className.indexOf('form') != -1) set = true;
|
2007-07-19 12:40:28 +02:00
|
|
|
baseEl = el;
|
2007-09-16 18:54:20 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
|
|
|
if(_CURRENT_FORM.elements[fieldName]) {
|
|
|
|
//Some elements are nested and need to be "got"
|
|
|
|
var i, hasValue = false;
|
2007-09-16 18:54:20 +02:00
|
|
|
if(_CURRENT_FORM.elements[fieldName].length > 1) {
|
|
|
|
for(i=0; i < el.length; i++) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(el[i].checked && el[i].value) {
|
|
|
|
hasValue = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(hasValue) set = true;
|
|
|
|
else set = "";
|
|
|
|
baseEl = el[0].parentNode.parentNode;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
set = "";
|
2007-09-16 18:54:20 +02:00
|
|
|
baseEl = el.parentNode;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
set = true;
|
|
|
|
}
|
|
|
|
}
|
2007-09-16 18:54:20 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// This checks to see if the input has a value, and the field is not a readonly.
|
2007-09-16 18:54:20 +02:00
|
|
|
if((typeof set == 'undefined' || set == "")) {
|
2007-07-19 12:40:28 +02:00
|
|
|
//fieldgroup validation
|
|
|
|
var fieldLabel = findParentLabel(baseEl);
|
|
|
|
|
2007-09-16 18:54:20 +02:00
|
|
|
// Some fields do-not have labels, in
|
2007-07-19 12:40:28 +02:00
|
|
|
// which case we need a blank one
|
2007-09-16 18:54:20 +02:00
|
|
|
if(fieldLabel == null || fieldLabel == "") {
|
|
|
|
fieldlabel = "this field";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 18:54:20 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
var errorMessage = "Please fill out \"$FieldLabel\", it is required."
|
|
|
|
if(baseEl.requiredErrorMsg) errorMessage = baseEl.requiredErrorMsg;
|
|
|
|
else if(_CURRENT_FORM.requiredErrorMsg) errorMessage = _CURRENT_FORM.requiredErrorMsg;
|
2007-09-16 18:54:20 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
validationError(baseEl, errorMessage.replace('$FieldLabel', fieldLabel),"required",cachedError);
|
|
|
|
return false;
|
2007-09-16 18:54:20 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
if(!hasHadFormError()) {
|
2007-07-19 12:40:28 +02:00
|
|
|
clearErrorMessage(baseEl.parentNode);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2007-09-16 18:54:20 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the label of the blockset which contains the classname left
|
|
|
|
*/
|
2007-09-16 18:54:20 +02:00
|
|
|
function findParentLabel(el) {
|
|
|
|
// If the el's type is HTML then were at the uppermost parent, so return
|
2007-07-19 12:40:28 +02:00
|
|
|
// null. its handled by the validator function anyway :-)
|
2007-09-16 18:54:20 +02:00
|
|
|
if(el) {
|
|
|
|
if(el.className == "undefined") {
|
2007-07-19 12:40:28 +02:00
|
|
|
return null;
|
2007-09-16 18:54:20 +02:00
|
|
|
} else {
|
|
|
|
if(el.className) {
|
|
|
|
if(el.className.indexOf('field') == 0) {
|
2007-07-19 12:40:28 +02:00
|
|
|
labels = el.getElementsByTagName('label');
|
2007-09-13 05:17:35 +02:00
|
|
|
if(labels){
|
2007-07-19 12:40:28 +02:00
|
|
|
var left = findIndexOf(labels,'left');
|
|
|
|
var right = findIndexOf(labels,'right');
|
2007-09-16 18:54:20 +02:00
|
|
|
if(left) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return strip_tags(left.innerHTML);
|
2007-09-16 18:54:20 +02:00
|
|
|
} else if(right) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return strip_tags(right.innerHTML);
|
2007-09-16 18:54:20 +02:00
|
|
|
} else {
|
2007-07-19 12:40:28 +02:00
|
|
|
return findParentLabel(el.parentNode);
|
2007-09-16 18:54:20 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-08-23 07:47:54 +02:00
|
|
|
}//merged by nlou 23/08/2007, r#40674
|
|
|
|
else if(el.className.indexOf('tablecolumn') != -1){
|
|
|
|
return el.className.substring(0, el.className.indexOf('tablecolumn')-1);
|
2007-07-19 12:40:28 +02:00
|
|
|
}else{
|
|
|
|
return findParentLabel(el.parentNode);
|
|
|
|
}
|
2007-09-16 18:54:20 +02:00
|
|
|
} else {
|
2007-07-19 12:40:28 +02:00
|
|
|
return findParentLabel(el.parentNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// backup
|
|
|
|
return "this";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a validation error to an element
|
|
|
|
*/
|
2007-09-16 18:54:20 +02:00
|
|
|
function validationError(field,message, messageClass, cacheError) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(typeof(field) == 'string') {
|
|
|
|
field = $(field);
|
|
|
|
}
|
2007-09-16 18:54:20 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(cacheError) {
|
|
|
|
_ERROR_CACHE[_ERROR_CACHE.length] = {
|
|
|
|
"field": field,
|
|
|
|
"message": message,
|
|
|
|
"messageClass": messageClass
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2007-09-16 18:54:20 +02:00
|
|
|
|
|
|
|
// The validation function should only be called if you've just left a field,
|
2007-07-19 12:40:28 +02:00
|
|
|
// or the field is being validated on final submission
|
2007-09-16 18:54:20 +02:00
|
|
|
if(_LIMIT_VALIDATION_ERRORS && _LIMIT_VALIDATION_ERRORS != field) {
|
2007-07-19 12:40:28 +02:00
|
|
|
// clearErrorMessage(field.parentNode);
|
|
|
|
return;
|
|
|
|
}
|
2007-09-16 18:54:20 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
_HAS_HAD_FORM_ERROR = true;
|
2007-09-16 18:54:20 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// See if the tag has a reference to the validationMessage (quicker than the one below)
|
|
|
|
var validationMessage = field.validationMessage;
|
2007-09-16 18:54:20 +02:00
|
|
|
|
|
|
|
// Cycle through the elements to see if it has a span
|
|
|
|
// (for a validation or required messages)
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!validationMessage) {
|
2007-09-16 18:54:20 +02:00
|
|
|
|
|
|
|
//Get the parent holder of the element
|
2007-07-19 12:40:28 +02:00
|
|
|
var FieldHolder = field.parentNode;
|
|
|
|
var allSpans = FieldHolder.getElementsByTagName('span');
|
|
|
|
validationMessage = findIndexOf(allSpans,'message');
|
|
|
|
}
|
2007-09-16 18:54:20 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// If we didn't find it, create it
|
|
|
|
if(!validationMessage) {
|
|
|
|
validationMessage = document.createElement('span');
|
|
|
|
FieldHolder.appendChild(validationMessage);
|
|
|
|
}
|
2007-09-16 18:54:20 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Keep a reference to it
|
|
|
|
field.validationMessage = validationMessage;
|
2007-09-16 18:54:20 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Set the attributes
|
|
|
|
validationMessage.className = "message " + messageClass;
|
|
|
|
validationMessage.innerHTML = message;
|
|
|
|
validationMessage.style.display = "block";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a limitation so that only validation errors for the given element will actually be shown
|
|
|
|
*/
|
2007-09-16 18:54:20 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
var _LIMIT_VALIDATION_ERRORS = null;
|
|
|
|
function limitValidationErrorsTo(field) {
|
|
|
|
_LIMIT_VALIDATION_ERRORS = field;
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearValidationErrorLimit() {
|
|
|
|
_LIMIT_VALIDATION_ERRORS = null;
|
|
|
|
}
|
|
|
|
|
2007-09-16 18:54:20 +02:00
|
|
|
function clearValidationErrorCache() {
|
2007-07-19 12:40:28 +02:00
|
|
|
_ERROR_CACHE = new Array();
|
|
|
|
}
|
|
|
|
|
2007-09-16 18:54:20 +02:00
|
|
|
function showCachedValidationErrors() {
|
|
|
|
for(i = 0; i < _ERROR_CACHE.length; i++) {
|
2007-07-19 12:40:28 +02:00
|
|
|
validationError(_ERROR_CACHE[i]["field"],
|
|
|
|
_ERROR_CACHE[i]["message"],
|
|
|
|
_ERROR_CACHE[i]["messageClass"],
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function strip_tags(text) {
|
|
|
|
return text.replace(/<[^>]+>/g,'');
|
|
|
|
}
|