	var oAutoComp = null;

/**
Refreshes the AutoCOmplete component with the specified array, updates the text box and town DIV.
*/
function refreshAutoComplete(townArray, inputId, containerId) {
	if(oAutoComp == null) {
		oAutoComp = new YAHOO.widget.AutoComplete(inputId, containerId, townArray);
		oAutoComp.queryDelay = .1;
		oAutoComp.prehighlightClassName = "yui-ac-prehighlight";
		oAutoComp.useShadow = false;
		oAutoComp.minQueryLength = 0;
		oAutoComp.animVert = false;
		oAutoComp.useIFrame = true;
		oAutoComp.typeAhead = false;
		oAutoComp.allowBrowserAutocomplete = false;
	} else {
		oAutoComp.dataSource = townArray;
	}
}

/**
 Finds the selected radio button, based on the rbId prefix, and 
 loads the towns associated with the radio buttons value 
 */
function initAutoCompleteFromRB(rbId, inputId, containerId, clearTextBox) {
	var state;
	
	var i = 1;
	var radioBtns = document.getElementsByName(rbId);
	for(var i=0;i<radioBtns.length;++i) {
		var rbInput = radioBtns[i];
		if(rbInput) {
			if(rbInput.checked) {
				state = rbInput.value;
				break;
			} 		
		} else {
			break;
		}
	}
	initAutoComplete(state, inputId, containerId, clearTextBox);
}

function initAutoComplete(stateCode, inputId, containerId, clearTextBox){
	var targetTextBox = document.getElementById(inputId);
	
	if(clearTextBox && clearTextBox == true ) {
		targetTextBox.value = "";
	}
	
	if(stateCode && stateCode != -1){
		var arrayName = stateCode + "Towns";
		var arr = eval(arrayName);
		var divContainer = document.getElementById(containerId);
		if(!targetTextBox){
			return false;
		}else if(!divContainer){
			return false;
		}
		
		if(arr){
			refreshAutoComplete(arr, inputId, containerId);
				
			if(targetTextBox.disabled) {
				targetTextBox.disabled = false;
				targetTextBox.value = "";
			}
		}
	} else {
		var input = document.getElementById(inputId);	
		if(targetTextBox) {
			targetTextBox.disabled = true;
			targetTextBox.value = "Select State";
		}
	}
}