var _VALIDATE_GROUP_ = true;


function validateCount(){
if(this.numSelected == null || typeof(this.numSelected) != "function") return true;
if(this.regChangeEvent != true && !this.required) {
this.registerEvent("if(!thisObj.required && thisObj.numSelected()==0) this.setChildStatus('none', true, 'selected');", "change");
}
var count = this.numSelected();

if(this.type == "group" && this.minCount != null){
var doValidation = (this.curElement == null) ? true : this.curElement.name;
if(count < this.minCount)
this.setChildStatus("all", doValidation, "none");
else if(count > this.minCount)
this.setChildStatus("none", doValidation, "selected");
else
this.setChildStatus("selected", doValidation, "selected");
}

var items = (this.type == "group" && !this.containsSimpleFields) ? " field" : " item";
var selected = (this.type == "group" && !this.containsSimpleFields) ? "completed" : "selected";
if(this.minCount != null && count < this.minCount) return this.setMessage("At least " + this.minCount + items + (this.minCount>1?"s":"") + " must be " + selected);
if(this.maxCount != null && count > this.maxCount) return this.setMessage("No more than " + this.maxCount + items + (this.maxCount>1?"s":"") + " may be " + selected);
return true;
}

function validateMembersValid(){
var valid = true;
if(this.type == "group")
for(i in this.element)
valid &= this.element[i].isValid;
return valid;
}

function validateAllOrNothing(){
if(this.numSelected == null || typeof(this.numSelected) != "function") return true;
var count = this.numSelected();
this.minCount = (count == 0) ? 0 : this.element.length;

if(this.type == "group"){
this.setChildStatus((count == 0) ? "none" : "all", (this.curElement == null) ? true : this.curElement.name, "none");
}

if(count == 0 || count == this.element.length) return true;
var items = (this.type == "group" && !this.containsSimpleFields) ? " fields" : " items";
var selected = (this.type == "group" && !this.containsSimpleFields) ? "completed" : "selected";
return this.setMessage("Either all" + items + " must be " + selected + " or no" + items + " may be " + selected);
}


function FieldGroup(formObj, elementName){
if(debug) window.status = "Adding " + element.name;

this.formObj = formObj;
this.name = elementName;
this.type = "group";
this.events = new Array();
this.element = new Array();
this.trappedElements = 0;
this.minCount = 1;
this.isValid = true;
this.containsSimpleFields = true;

this.get = getFG;
this.set = setFG;
this.setAll = setAllFG;
this.numSelected = numSelectedFG;
this.addElement = addElementFG;
this.focus = focusFG;
this.trapEvents = trapEventsFG;
this.trapEvent = trapEventFG;
this.handleEvent = handleEventFG;
this.setChildStatus = setChildStatusFG;
this.compare = compareFG;
this.executeEvent = executeEventFG;

this.setValidation = setValidationFEW;
this.setRequired = setRequiredFEW;
this.validate = validateFEW;
this.validateSibling = validateSiblingFEW;
this.showStatus = showStatusFEW;
this.addEvent = addEventFEW;
this.registerEvent = registerEventFEW;
this.forceError = forceErrorFEW;
this.getSibling = getSiblingFEW;
this.setMessage = setMessageFEW;
this.compareNullable = compareNullableFEW;
this.hasChanged = hasChangedFEW;
this.init = initFEW;

this.init();

if(this.image == null){
this.showStatus = showStatusFG;
this.onErrorChildStatus = "none";
}
}

function getFG(which){
if(which != null){
for(i in this.element)
if(this.element[i].name == which)
return this.element[i].get();
return "";
}
var i, out = "", hasValue = false, elementVal;
for(i in this.element){
elementVal = this.element[i].get();
out += VALUE_SPACER + elementVal;
if(elementVal != null && elementVal != "")
hasValue = true;
}
if(!hasValue) return "";
return out.substring(1);
}
function setFG(val, which){
if(which != null){
if(typeof(which) == "number")
return this.element[which].set(val);
for(i in this.element)
if(this.element[i].name == which)
return this.element[i].set(val);
return;
}
var values = getValues(val);
for(i = 0; i < this.element.length; i++)
this.element[i].set(i < values.length ? values[i] : "");
}
function setAllFG(val){
for(i in this.element){
this.element[i].set(val);
}
}
function numSelectedFG(){
var i, numSelected = 0;
for(i in this.element){
if(this.element[i].get() != null && this.element[i].get() != "")
numSelected++;
}
return numSelected;
}
function addElementFG(elementName){
var element = this.getSibling(elementName);
if(element == null) return;
this.element[this.element.length] = element;
this.trapEvents();
this.lastVal=this.get();
this.containsSimpleFields &= element.type == "radio" || element.type == "checkbox";
}
function focusFG(){
if(this.element.length > 0)
this.element[0].focus();
}
function trapEventsFG(){
var element;
while(this.trappedElements < this.element.length){
this.trapEvent("focus", "", this.trappedElements);
this.trapEvent("blur", "", this.trappedElements);
this.trapEvent("change", "", this.trappedElements);
this.trappedElements++;
}
}
function trapEventFG(event, saveAsEvent, which){
if(which == null) return;
this.element[which].registerEvent("thisObj.getSibling('" + this.name + "').handleEvent(thisObj, '" + event + "')", event);
}
function setChildStatusFG(whichRequired, doValidation, onErrorChildStatus){
this.onErrorChildStatus = onErrorChildStatus;
for(var i = 0; i < this.element.length; i++){
if(whichRequired == "selected")
this.element[i].setRequired(this.element[i].get() != "");
else if (whichRequired == "all" || whichRequired == "none")
this.element[i].setRequired(whichRequired == "all");
else if (whichRequired)
this.element[i].setRequired(whichRequired == this.element[i].name);

if(doValidation == true || doValidation == this.element[i].name || !this.element[i].isValid)
this.element[i].validate();
else
this.element[i].showStatus();
}
}
function executeEventFG(type, which){
if(which != null) return this.element[which].executeEvent(type);
for(i in this.element)
this.element[i].executeEvent(type);
}
function handleEventFG(element, type){
this.curElement = element;
var thisObj = this;
var events = this.events;
var fn = eval("generic_" + type);
if(typeof fn == "function") fn(thisObj);
for(i in events)
if(type == events[i].type){
if(debug) window.status = thisObj.name + "; " + type + "; Executing: " + events[i].code;
eval(events[i].code);
}
this.curElement = null;
}
function showStatusFG(){
if(this.onErrorChildStatus != "none"){
var i, doAll = this.onErrorChildStatus == "all", e;
for(i in this.element){
e = this.element[i];
if(e.image && (doAll || (e.get() != null && e.get() != "")) && e.isValid){
e.image.src = (this.isValid && e.isValid) ? (e.required ? (e.get() == "" ? e.formObj.fieldErrorRequiredRed.src : e.formObj.fieldErrorRequiredBlack.src) : e.formObj.fieldErrorOff.src) : e.formObj.fieldErrorOn.src
e.image.alt = this.message;
e.image.title = this.message;
e.message = this.message;
}
}
}
if(this.message > "") window.status = this.message;
}
function compareFG(value){
var values = getValues(value);
if(values.length == 0)
while(values.length < this.element.length)
values[values.length] = "";
else if(values.length != this.element.length)
return null;

var less = false, greater = false, curCmp;
for(i = 0; i < values.length; i++){
curCmp = this.element[i].compare(values[i]);
if(curCmp < 0) less = true;
if(curCmp > 0) greater = true;
}
if(less && greater) return null;
if(less) return -1;
if(greater) return 1;
return 0;
}

function getValues(val) {
var values = new Array(), curVal = -1, i, c;
if(val > ""){
values[++curVal] = "";
for(i = 0; i < val.length; i++){
c = val.charAt(i);
if(c == VALUE_SPACER)
values[++curVal] = "";
else
values[curVal] += c;
}
}
return values;
}



FormValidator.prototype.createGroup = function(name){
if(this.childElements[name] != null) return null;
var newElement = new FieldGroup(this, name);
this.childElements[name] = newElement;
var i;
for(i = 1; i < arguments.length; i++)
newElement.addElement(arguments[i]);

return newElement;
}
var _SR_;
if(_SR_ != null) _SR_.notify("validategroup.js");