function parseField(id,field) {
	var arrField = eval('arrField_' + id);
	for (i=0; i <= arrField.length - 1; i++) {
		try {
			if (field == arrField[i])
				return i;
		} catch(e) {}
	}
}

function clearPrevious(el,id,reset) {
	var theForm = document.forms.form;
	var setBreak = false;
	var arrField = eval('arrField_' + id);
	
	for (i=arrField.length - 1; i >= 0; i--) {
		var objDDL = eval('theForm.' + arrField[i]);
		
		if (!objDDL) {
			alert('No control could be found at level ' + (i+1) + '.');
			break;
		}
		
		objDDL.disabled = true;
		objDDL.options.length = 0;
		if (el.name != arrField[i]) {
			var option = new Option('- select parent -', '')
		} else {
			if (reset)
				var option = new Option('- select parent -', '')
			else
				var option = new Option('- loading -', '');
			setBreak = true;
		}

		try {
			objDDL.add(option, null);
		} catch(e) {
			objDDL.add(option, -1);
		}
		
		if (setBreak)
			break;
	}
}

function fetchDropDown(oElem, oTarget, url, id) {
	var theForm = document.forms.form;
	
	if (!oTarget) {
		alert('No target for ' + oElem.name + ' cound be found.');
		return;
	}
	
	if (!oElem || oElem.value != '') {
		clearPrevious(oTarget,id,false);
		http("get", url, fillDropDown, theForm, id, oTarget.name);
	} else
		clearPrevious(oTarget,id,true);
}

function fillDropDown(obj, id, field) {
	var theForm = document.forms.form;
	var wddx = obj;
	var arrIndex = parseField(id,field);
	var arrField = eval('arrField_' + id);
	var arrID = eval('arrID_' + id);
	var arrText = eval('arrText_' + id);
	var arrValue = eval('arrValue_' + id);
	var objDDL = eval('theForm.' + arrField[arrIndex]);
	var objID = eval('wddx.' + arrID[arrIndex]);
	var objText = eval('wddx.' + arrText[arrIndex]);
	var value = arrValue[arrIndex];

	objDDL.options.length = 0;
	
	defaultText = objDDL.getAttribute("defaulttext");
	var option = new Option(defaultText, '');
	try {
		objDDL.add(option, null);
	} catch(e) {
		objDDL.add(option, -1);
	}

	for (i = 0; i < objID.length; i++) {
		var theText = objText[i];;
		var theValue = objID[i];
		var option = new Option(theText, theValue);
		try {
			objDDL.add(option, null);
		} catch(e) {
			objDDL.add(option, -1);
		}
	}

	if (!objDDL.disable)
		objDDL.disabled = false;
	
	if (value != '') {
		arrValue[arrIndex] = '';
		selectDefault(objDDL, value);
	}

}

function selectDefault(el, value) {
	for (i=0; i <= el.length - 1; i++) {
		if (el[i].value == value) {
			el.selectedIndex = i;
			el.onchange();
			break;
		}
	}
}