
function obtainIndex(value) {
    for (var ix=0;ix<states.length;ix++) {
		if (states[ix][0] == value) {
	    	return ix;
		}
    }
}

function initiateSelect(state_index, county_index, index_count) {
	var state_id = 'states_'+index_count;
	var county_id = 'counties_'+index_count;
	for(var index=0;index<states.length;index++) {
			document.getElementById(state_id).options[index] = new Option(states[index][0], states[index][0]);
    }

	document.getElementById(state_id).selectedIndex = state_index;
	stateChange(index_count);
	document.getElementById(county_id).selectedIndex = county_index-1;
	storeIndexes(index_count);
}

function stateChange(index_count) {
	var state_id = 'states_'+index_count;
	var county_id = 'counties_'+index_count;
	var selIndex = document.getElementById(state_id).selectedIndex;
    var selectedState = document.getElementById(state_id).options[selIndex].value;
    var stateIndex = obtainIndex(selectedState);
	document.getElementById(county_id).options.length = 0;
	for(var index=1; index < states[stateIndex].length; index++) {
	    document.getElementById(county_id).options[index-1] = new Option(states[stateIndex][index], states[stateIndex][index]);
	}
//	storeIndexes(index_count);

}

function storeIndexes(index_count) {
    document.getElementById('s_ix'+index_count).value = document.getElementById('states_'+index_count).selectedIndex;
    document.getElementById('c_ix'+index_count).value = document.getElementById('counties_'+index_count).selectedIndex + 1;
}