// An Object whose properties hold Arrays of OptionType/Value pairs
var MAIN_OPTIONS = new Object();

function update_MAIN_OPTIONS(sTargetFieldID, sTypeName, sOptionTypeValue) {
	// create the MAIN_OPTIONS' object property that'll hold the array of type/value pairs for this set of options
	// as long as the property doesn't already exist.
	if (MAIN_OPTIONS[sTargetFieldID] == undefined) {
		MAIN_OPTIONS[sTargetFieldID] = new Array();
	}

	// add the type/value pair to the array and an id, so we can directly update if needed.
	if (MAIN_OPTIONS[sTargetFieldID][sTypeName] == undefined) {
		MAIN_OPTIONS[sTargetFieldID][MAIN_OPTIONS[sTargetFieldID].length] = sOptionTypeValue;
		MAIN_OPTIONS[sTargetFieldID][sTypeName] = new Array(
				(MAIN_OPTIONS[sTargetFieldID].length - 1),
				MAIN_OPTIONS[sTargetFieldID][MAIN_OPTIONS[sTargetFieldID].length - 1]);
	} else {
		// directly update the type/value pair in the array.
		MAIN_OPTIONS[sTargetFieldID][sTypeName][1] = sOptionTypeValue;
		MAIN_OPTIONS[sTargetFieldID][MAIN_OPTIONS[sTargetFieldID][sTypeName][0]] = sOptionTypeValue;
	}
}

// Function for updating the hidden options field.
function optionTypeValues(oSelectObj) {
	// get the values that will make up MAIN_OPTIONS	
	var aObjValues = oSelectObj.id.split('_');
	/*
		aObjValues[0] = 'options'
		aObjValues[1] = ${product.pk.asString}
		aObjValues[2] = ${option.pk.asString}
		aObjValues[3] = ${option.optionType.pk.asString}
	
	*/
	
	var sTypeName = aObjValues[0] + "_" + aObjValues[1] + "_" + aObjValues[3];
	
	// object ref which points to the related options hidden field.
	var oOptionTypeValues = eval("document.getElementById('mainForm').optionTypeValues_"+aObjValues[1]); 
	
	// string for holding the target field's ID.
	var sTargetFieldID = oOptionTypeValues.id;

	// string for holding the concatenated value of the optionTypePk and the optionPk.
	//var sOptionTypeValue = (sTypeName.split("_"))[2] + "=" + oSelectObj[oSelectObj.selectedIndex].value;
	var sOptionTypeValue =  aObjValues[3] + "=" + aObjValues[2];

	// update the MAIN_OPTIONS object to reflect additions or updates
	update_MAIN_OPTIONS(sTargetFieldID, sTypeName, sOptionTypeValue);

	// update the target field's value, for posting the data.
	oOptionTypeValues.value = MAIN_OPTIONS[sTargetFieldID].join(":");
}
