// ф-ция, возвращающая массив по заданной строке
function getArrayFromString(index,aName)
{
	var sValues = eval(aName+'['+index+']');
    return sValues.split(","); // преобразуем строку в массив 
}

// главная ф-ция, выводящая динамически список 

function MkListValues(obj, formName, selectNameTop, selectNameLevel2, selectNameLevel3, selectNameLevel4, curLevel)
{

	var index = obj.selectedIndex;
    
    var indexValue = document.forms[formName].elements[selectNameTop].selectedIndex;
	if(curLevel == 1)
    {
    	var aCurrTexts = getArrayFromString(index,'aRayonTexts');
	    var aCurrValues = getArrayFromString(index,'aRayonValues');
	    selectName = selectNameLevel2;

    }
    else if(curLevel == 2)
    {
    	var aCurrTexts = getArrayFromString(index,'aMicroRayonTexts'+indexValue);
	    var aCurrValues = getArrayFromString(index,'aMicroRayonValues'+indexValue);
	    selectName = selectNameLevel3;
	}
	else if(curLevel == 3)
    {
    	var indexMRValue = document.forms[formName].elements[selectNameLevel2].selectedIndex;
    	//alert('aMicroRayon'+indexValue+'_StreetTexts'+indexMRValue);
    	var aCurrTexts = getArrayFromString(index,'aMicroRayon'+indexValue+'_StreetTexts'+indexMRValue);
	   	var aCurrValues = getArrayFromString(index,'aMicroRayon'+indexValue+'_StreetValues'+indexMRValue);
	   	selectName = selectNameLevel4;
	}
	
   
	if (selectName != '')
	{
	   	var nCurrValuesCnt = aCurrValues.length;
		var oList = document.forms[formName].elements[selectName];
	   	var oListVal = oList.value;
	   //	alert   	(oListVal);
	    var oListOptionsCnt = oList.options.length;
	    oList.length = 0; // удаляем все элементы из списка 
	    for (i = 0; i < nCurrValuesCnt; i++){
	        // далее добавляем необходимые значения в список
	        if (document.createElement){
	            var newListOption = document.createElement("OPTION");
	            newListOption.text = aCurrTexts[i];
	            newListOption.value = aCurrValues[i];
	            //alert(oListVal +' = '+ newListOption.text +' = '+ newListOption.value);
	            if (oListVal == newListOption.value)	 newListOption.selected = true;
	            else  newListOption.selected = false;
	            // тут используем для добавления элемента либо метод IE, либо DOM, которые, alas, не совпадают по параметрам…
	            (oList.options.add) ? oList.options.add(newListOption) : oList.add(newListOption, null);
	        }else{
	            // для NN3.x-4.x
	             if (oListVal == newListOption.value)   oList.options[i] = new Option(aCurrValues[i], aCurrValues[i], false, true);
	             else   oList.options[i] = new Option(aCurrValues[i], aCurrValues[i], false, false);
	        }
	    }
	}   
	
   if(curLevel == 1)
    {
	   // MkListValues(document.forms[formName].elements[selectNameLevel2],formName,selectNameLevel3,selectNameTop,selectNameLevel3,selectNameLevel2,false,true,2,3)
	    MkListValues(
	    document.forms[formName].elements[selectNameLevel2],
	    formName,
	    selectNameTop,
	    selectNameLevel2,
	    selectNameLevel3,
	    selectNameLevel4,
	    2);
    }
    else if(curLevel == 2 && selectName != '')
    {
    	MkListValues(
	    document.forms[formName].elements[selectNameLevel3],
	    formName,
	    selectNameTop,
	    selectNameLevel2,
	    selectNameLevel3,
	    selectNameLevel4,
	    3);
    }
}
