//
// 

var originalValues = new objCollection("fieldName", fieldObject);

function PopulateChild(me) {
	try {
	
		var fieldObj;
		
		// Get the room control for this location
		childlist = document.getElementById(me.ChildControl);
		
		// Check if this field's first value has already been stored in the array and add if necessary
		// This allows the original value to show as selected if the owning lookup is changed again back to the original
		
		StoreOriginalValue(me.ChildControl, childlist.options[childlist.selectedIndex].value)
		
		if (me.options[me.selectedIndex].value == '') {
		// The parent lookup has been changed to the ' - Please Choose ' option - don't do anything to the child lookup
			return;
		}
		// Populate the child lookup using Ajax
		HTML = ES.PopulateChild(me.Label, me.options[me.selectedIndex].value).value;
		
		fieldObj = originalValues.get(me.ChildControl);
		
		// Only display options if there are some (including the none option)
		if (HTML.length > 0) {
			// Empty the control
			childlist.options.length=0;
		
			childlist.options.add(new Option('- Please Choose -', ''));
			for (var i = 0; i < HTML.length; ++i) { 
				if ((fieldObj) && (fieldObj.fieldValue == HTML[i].Value)) {
					childlist.options.add(new Option(HTML[i].Text, HTML[i].Value, true, true));
				}
				else {
					childlist.options.add(new Option(HTML[i].Text, HTML[i].Value));
				}
			} 
		}
	}
	catch (e) {
		window.status = "es_OnClick " + e.description;
	}
}
function StoreOriginalValue(fieldName, fieldValue)
{
	try {
		var i;
		var obj;
		var exists = 'no';
		obj = originalValues.get(fieldName);
		if (!obj) {
			obj = new fieldObject(fieldName, fieldValue);
			originalValues.add(obj, false);
		}
	}
	catch (e) {
		window.status = "es_Store " + e.description;
	}
}
function fieldObject(fName, fValue)
{
	this.fieldName = fName;
	this.fieldValue = fValue;
}

	// Constructor objCollection()
	// Usage
	//		Instantiates a new objCollection
	// Parameters
	//		MemberKeyName: The property to be used as the unique identifier for objCollection members.
	//		MemberType: A reference to the constructor of the objects to be stored in the objCollection
	// Return objCollection
	//
function objCollection(MemberKeyName, MemberType) {
try {
	this.Members = new Object;

	this.MemberKeyName = MemberKeyName;
	if ( !MemberType ) {
		this.MemberType = MemberType;
	} else {
		this.MemberType = null;
	};

	this.Count = 0;

	return this;
}
catch(e) {
window.status = "es_ObjColl " + e.description;
}
};


objCollection.prototype.getCount = function getCount() {

	return this.Count;

};


objCollection.prototype.getMemberType = function getMemberType() {

	return this.MemberType;

};

objCollection.prototype.getMemberKeyName = function getMemberKeyName() {

	return this.MemberKeyName;

};


objCollection.prototype.get = function get(MemberKey) {
try {
	var x = this.Members[MemberKey];
	return x;
}
catch(e) {
window.status = "es_get " + e.description;
}
};

objCollection.prototype.add = function add(NewMember, AllowOverwrite) {
try {
		// Is the object of the right type?
	if ( ! this.isValidType(NewMember) ) {
		return false;
	};

		// Get the Member Key
	var NewMemberKey = NewMember[this.MemberKeyName];

	if ( typeof AllowOverwrite != "boolean" ) {
		AllowOverwrite = false;
	};
		// Does the key exist?
	if ( ! AllowOverwrite ) {
		if ( typeof this.Members[NewMemberKey] != "undefined" ) {
			return false;
		};
	};

	this.Members[NewMemberKey] = NewMember;
	this.Count++;
	return true;
}
catch(e) {
window.status = "es_add " + e.description;
}
};

objCollection.prototype.isValidType = function isValidType(ObjectToCheck) {

		// If the collection does not define a type, return true
	if ( this.MemberType == null ) {
		return true;
	};

		// If we're checking a type, do it
	if ( ( typeof ObjectToCheck == "object" ) && ( ObjectToCheck.constructor == this.MemberType ) ) {
		return true;
	} else {
		return false;
	};

};