
////////////////////////////////////////////////////////////
// 
// Code to create conditional blocks
// 
////////////////////////////////////////////////////////////


// TigerConditional - object
// 
// Fields for definition:
//		oSource - TigerField object - source field for condition (i.e. the one which decides if the other field is available)
//		oTarget - TigerField object - target field (i.e. the one that is made available only on condition met)
//		nConditionType - eConditionType enum - specifies the manner of the condition. Defaults to 'Not Empty' if not specified
//		ValueList - an array containing the pertinent values or range. These are normally defined as a literal list, e.g.
//			["UK", "IE", "FR"]. If there is only one value it should still be passed in as an array, e.g. ["Mr"].
//			Ranges should be passed with one or two items in the array. The first sets the lower limit of the array. The second, 
//			if specified, sets the upper limit. If it isn't specified, there is no upper limit. Note that ranges are INCLUSIVE at
//			both ends.
//			Ranges using NUMBERS have not yet been tested. There is a strong chance that with the current code, the routine will compare
//			even number values like strings. Please test before use.

function TigerConditional(oSource, oTarget, nConditionType, ValueList, nActionIfFalse, nActionIfTrue) {

	this.SourceField = oSource;
	this.TargetField = oTarget;
	
	if (nConditionType == undefined) {
		this.ConditionType = eConditionType.NotEmpty;
	} else {
		this.ConditionType = nConditionType;
		this.ConditionValues = ValueList;
	}
	
	if (nActionIfFalse == undefined) {
		this.ActionIfFalse = eDisplayStatus.Disabled;
	} else {
		this.ActionIfFalse = nActionIfFalse;
	}
	if (nActionIfTrue == undefined) {
		this.ActionIfTrue = eDisplayStatus.Normal;
	} else {
		this.ActionIfTrue = nActionIfTrue;
	}
}

// Called from the relevant TigerField event to 'SetConditionals' - recurses through each condition, and sets this
// Not usually called publically. It checks the details of the condition against the current value(s) of the source
// field, then applies the appropriate Display Status to the target object.

TigerConditional.prototype.Set = function () {
	lConditionMet = false;
	switch(this.ConditionType) {
		case eConditionType.Empty:
			if (EmptyNullUndefined(this.SourceField.GetValue()))
				lConditionMet = true;
			break;	
		case eConditionType.NotEmpty:
			if (!EmptyNullUndefined(this.SourceField.GetValue()))
				lConditionMet = true;
			break;
		case eConditionType.InValueList:
			uVal = this.SourceField.GetValue();
			if (isArray(uVal)) {
				for (Val in this.ConditionValues) {
					if (inArray(uVal, this.ConditionValues[Val])) {
						lConditionMet = true;
						break;
					}
				}
			} else {
				for (Val in this.ConditionValues) {
					if 	(uVal == this.ConditionValues[Val]) {
						lConditionMet = true;
						break;
					}
				}
			}
			break;
		case eConditionType.NotInValueList:
			lConditionMet = true;
			uVal = this.SourceField.GetValue();
			if (isArray(uVal)) {
				for (Val in this.ConditionValues) {
					if (inArray(uVal, this.ConditionValues[Val])) {
						lConditionMet = false;
						break;
					}
				}
			} else {
				for (Val in this.ConditionValues) {
					if 	(uVal == this.ConditionValues[Val]) {
						lConditionMet = false;
						break;
					}
				}
			}
			break;
		case eConditionType.InRange:
			cVal = this.SourceField.GetValue();
			MinVal = this.ConditionValues[0];
			MaxVal = this.ConditionValues[1];
			if 	(cVal >= MinVal) {
				if (MaxVal == undefined || cVal <= MaxVal) {
					lConditionMet = true;
				}
			}
			break;
		case eConditionType.NotInRange:
			lConditionMet = true;
			cVal = this.SourceField.GetValue();
			MinVal = this.ConditionValues[0];
			MaxVal = this.ConditionValues[1];
			if 	(cVal >= MinVal) {
				if (MaxVal == undefined || cVal <= MaxVal) {
					lConditionMet = false;
				}
			}
			break;
	}

	if (lConditionMet) {
		this.TargetField.SetDisplayStatus(this.ActionIfTrue)
	} else {
		this.TargetField.SetDisplayStatus(this.ActionIfFalse)
	}
}
