var $popPullDown = null;
var $popPullDownContainer = null;
var $popPullDownWebService = null;

function pullDownWebServiceInit () {
	
	if (($popPullDownWebService == null)) $popPullDownWebService = new WebService ();
}

function populatePullDown ($srcPullDown, $optionsArray) {
	
	var $idx, $optionElement;
	
	$srcPullDown.selectedIndex = 0;
	
	for ($idx = $srcPullDown.options.length - 1; $idx > 0; --$idx) {
		$srcPullDown.options [$idx] = null;
	}
	
	for ($idx = 0; $idx < $optionsArray.length; ++$idx) {
		$optionElement = new Option ($optionsArray [$idx].text, $optionsArray [$idx].value);
		$srcPullDown.options [$srcPullDown.options.length] = $optionElement;
	}
}

function requestPopulatePullDownCallBack ($responseCode) {
	
	populatePullDown ($popPullDown, eval ($responseCode));
	$popPullDown = null;
}

function requestPopulatePullDown ($srcPullDownId, $destPullDownId, $populateURL, $containerElemId, $containerURL) {
	
	// define variables
	var $srcPullDown, $destPullDown, $containerElem;
	
	// init web service
	pullDownWebServiceInit ();
	
	// define source pulldown
	$srcPullDown = document.getElementById ($srcPullDownId);
	
	// check if not first option and pop pulldown not set
	if (($srcPullDown.selectedIndex != 0) && ($popPullDown == null)) {
		
		// define container pulldown
		$containerElem = document.getElementById ($containerElemId);
		
		// check if container not set OR container has been ititialized
		if (($containerElem == null) || (pullDownContainerIsInit ($destPullDownId, $containerElemId, $containerURL))) {
			
			// define destination pulldown
			$destPullDown = document.getElementById ($destPullDownId);
			
			// execute Code
			$popPullDown = $destPullDown;
			
			// create request
			$popPullDownWebService.createRequest ($populateURL, requestPopulatePullDownCallBack, '',
				[[$srcPullDown.id, $srcPullDown.options [$srcPullDown.selectedIndex].value]]);
			
		} else {
			// otherwise
			
			// call function again in 100 ms
			setTimeout ('requestPopulatePullDown ("' + $srcPullDownId + '", "' + $destPullDownId + '", "' +
					$populateURL + '", "' + $containerElemId + '", "' + $containerURL + '");', 100);
		}
	}
	
}

function pullDownContainerIsInitCallBack ($responseCode) {
	
	$popPullDownContainer.innerHTML = $responseCode;
}

function pullDownContainerIsInit ($destPullDownId, $containerElemId, $containerURL) {
	
	var $destPullDown, $containerElem, $pullDownExists;
	
	$destPullDown = document.getElementById ($destPullDownId);
	$containerElem = document.getElementById ($containerElemId);
	
	$pullDownExists = ($destPullDown != null);
	
	if ((!$pullDownExists)) {
		
		$popPullDownContainer = $containerElem;
		
		$popPullDownWebService.createRequest ($containerURL, pullDownContainerIsInitCallBack, '', '');
	}
	
	return $pullDownExists;
}
