// configure service call 		
var config0 = {
	service:'campuses/BRI0003/programs',
	param:Array('category_id', 'program_name'),
	value:Array('1', 'Arts in Business'), 
	container:'program_select',
	label:'Please select a Degree',
	elt_name:'program', 
	elt_value:Array('ProgramCode'),
	elt_text:Array('DefaultName'),
	delim:':'
}

var config1 = {
	service:'campuses/BRI0003/categories', 
	param:Array('education_level_code'),  
	container:'category_select',
	label:'Please select a Program',
	elt_name:'category',
	elt_value:Array('CategoryID', 'DistinctName'),
	elt_text:Array('DistinctName'), 
	delim:':',
	handler:'0' // config_num to attach
}

var configs = Array(config0, config1);

var IEFIXWIDTH_DIV = '<span class="ie-select-width-fix" style="vertical-align:middle;" >';
var DEFAULT_CAT = IEFIXWIDTH_DIV + '<select name="category" id="category" style="width:161px"><option value="AA/Business">AA/Business</option></select></span>';
var DEFAULT_PROG = IEFIXWIDTH_DIV + '<select name="program" id="program" style="width:161px"><option value="AAB">AA/Business</option></select></span>';

// dunno why 'they' left this out of javascript
Array.prototype.in_array = function(obj){ return new RegExp('(^|\,)'+obj+'(\,|$)','gi').test(this); }

// call cdm-client				
Ext.onReady(function() {
	CdmClient;
	CdmClient.init(configs);

	function eventHandler() {
		var eduLevelValue = Ext.getDom('education_level_code').value;
		var radioValue = Ext.getDom('age').value;
		if (radioValue && eduLevelValue) loadPrograms(radioValue, eduLevelValue);
	}
	
	Ext.get('credits').on('change', eventHandler);
	Ext.get('education_level_code').on('change', eventHandler);
	Ext.get('age').on('change', eventHandler);
});

function loadPrograms(value, value2) {
	var run = true;
	if(value == '17-20') {
		Ext.getDom(config1.container).innerHTML = DEFAULT_CAT;
		Ext.getDom(config0.container).innerHTML = DEFAULT_PROG;
	} else if(value == '20-21') {
		Ext.getDom(config1.container).innerHTML = "<select disabled><option>Loading...</option>";
		Ext.getDom(config0.container).innerHTML = '<select name="program" id="program"><option value="">Select a Program first</option></select>';

		var creditsValue = Ext.getDom('credits').value;
		eduTest = Array("SOME", "HS", "GED");
		creditTest = Array('15-29', '30-44', '45-59', '60-74', '75-89', '90-99', '100+');
		
		if(eduTest.in_array(value2)) {
			Ext.getDom(config1.container).innerHTML = DEFAULT_CAT;
			Ext.getDom(config0.container).innerHTML = DEFAULT_PROG;
			run = false;
		}
		else {
			Ext.getDom(config1.container).innerHTML = DEFAULT_CAT;
			Ext.getDom(config0.container).innerHTML = DEFAULT_PROG;
		}
		
		if (run == true) {
			CdmClient.setConfig(1);
			CdmClient.callService(Array(value2));
		}
	} else if(value) {
		Ext.getDom(config1.container).innerHTML = "<select disabled><option>Loading...</option>";
		Ext.getDom(config0.container).innerHTML = '<select name="program" id="program"><option value="">Select a Program first</option></select>';
		
		CdmClient.setConfig(1);
		CdmClient.callService(Array(value2));
	}
}