var ajax = new sack();
var varObj;
function whenLoading(){
   //document.getElementById('statusinfo').innerHTML = "<p>Отправляем данные...</p>";
}
function whenLoaded(){
   //document.getElementById('statusinfo').innerHTML = "<p>Данные отправлены...</p>";
}
function whenInteractive(){
   //document.getElementById('statusinfo').innerHTML = "<p>Получаем ответ...</p>";
}
function whenError(){
   //document.getElementById('statusinfo').innerHTML = "<p>Ошибка...</p>";
}
function whenCompleted(){
	var sel1 = document.getElementById('gorid');
    var sel2 = document.getElementById('raiid');
	var globarr = ajax.response.split("!");
    switch(varObj.name){    	case "regid": //заполнение второго списка
    	if(varObj.value == 0) {   //очистка обоих списков если выбран элемент -любой-
              while (sel1.selectedIndex != -1){
				sel1.options[0] = null;
			  }
			  while (sel2.selectedIndex != -1){
				sel2.options[0] = null;
			  }
			  if (document.createElement){
            	var newOption = document.createElement("OPTION");
            	var newOption2 = document.createElement("OPTION");
            	newOption.text = "-- Выбрать --";
            	newOption.value = 0;
            	newOption2.text = "-- Выбрать --";
            	newOption2.value = 0;
            	(sel1.options.add) ? sel1.options.add(newOption) : sel1.add(newOption, null);
            	(sel2.options.add) ? sel2.options.add(newOption2) : sel2.add(newOption2, null);
        	  }else{
            	sel1.options[0] = new Option("-- Выбрать --", 0, false, false);
            	sel2.options[0] = new Option("-- Выбрать --", 0, false, false);
        	  }
			  break;
    	}
    //если изменился первый элемент
    //очистка сисков
    while (sel1.selectedIndex != -1){
		sel1.options[0] = null;
	}
	while (sel2.selectedIndex != -1){
		sel2.options[0] = null;
	}
	if (document.createElement){
	var newOption = document.createElement("OPTION");
	var newOption2 = document.createElement("OPTION");
	newOption.text = "-- Выбрать --";
	newOption.value = 0;
	newOption2.text = "-- Выбрать --";
	newOption2.value = 0;
	(sel1.options.add) ? sel1.options.add(newOption) : sel1.add(newOption, null);
	(sel2.options.add) ? sel2.options.add(newOption2) : sel2.add(newOption2, null);
	}else{
	sel1.options[0] = new Option("-- Выбрать --", 0, false, false);
	sel2.options[0] = new Option("-- Выбрать --", 0, false, false);
	}
    //конец очистки списков
	var resarr2 = globarr[0].split("|")
	var numrecords = resarr2.length;
	var oList = document.forms["firma"].elements["gorid"];
    for(var i=0; i<numrecords; i++){
        var stringarr = resarr2[i].split("###");
        if (document.createElement){
            var newOption = document.createElement("OPTION");
            newOption.text = stringarr[1];
            newOption.value = stringarr[0];
            // тут мы используем для добавления элемента либо метод IE, либо DOM, которые, alas, не совпадают по параметрам…
            (oList.options.add) ? oList.options.add(newOption) : oList.add(newOption, null);
        }else{
            // для NN3.x-4.x
            oList.options[i] = new Option(stringarr[1], stringarr[0], false, false);
        }
    }
    break;
    	case "gorid": //если изменился второй элемент
    	if(varObj.value == 0) {   //очистка третьего списка если выбран элемент -любой-
			  while (sel2.selectedIndex != -1){
				sel2.options[0] = null;
			  }
			  if (document.createElement){
            	var newOption2 = document.createElement("OPTION");
            	newOption2.text = "-- Выбрать --";
            	newOption2.value = 0;
            	(sel2.options.add) ? sel2.options.add(newOption2) : sel2.add(newOption2, null);
        	  }else{
            	sel2.options[0] = new Option("-- Выбрать --", 0, false, false);
        	  }
			  break;
    	}
    //очистка третьего списка
	while (sel2.selectedIndex != -1){
		sel2.options[0] = null;
	}
	if (document.createElement){
	var newOption2 = document.createElement("OPTION");
	newOption2.text = "-- Выбрать --";
	newOption2.value = 0;
	(sel2.options.add) ? sel2.options.add(newOption2) : sel2.add(newOption2, null);
	}else{
	sel2.options[0] = new Option("-- Выбрать --", 0, false, false);
	}
	//конец очистки третьего списка
    //заполнение третьего списка
    var resarr3 = new Array();
	resarr3 = globarr[1].split("|")
	var numrecords = resarr3.length;
	var oList = document.forms["firma"].elements["raiid"];
    for(var i=0; i<numrecords; i++){
        var stringarr = new Array();
        stringarr = resarr3[i].split("###");
        if (document.createElement){
            var newOption = document.createElement("OPTION");
            newOption.text = stringarr[1];
            newOption.value = stringarr[0];
            (oList.options.add) ? oList.options.add(newOption) : oList.add(newOption, null);
        }else{
            oList.options[i] = new Option(stringarr[1], stringarr[0], false, false);
        }
    }
    break;
    }
	/*if (ajax.responseStatus){
		var string = "<p>Статус код: " + ajax.responseStatus[0] + "</p><p>";
		      string += "Сообщение" + ajax.responseStatus[1] + "</p><p>";
		      string += "отправлен URLString: " + ajax.URLString + "</p>";
	} else {
		var string = "<p> отправлен URLString: " + ajax.URLString + "</p>";
	}*/
   //document.getElementById('text').value = ajax.response;
}
function doit(varObjGet){
	varObj = varObjGet;
	ajax.AjaxFailedAlert = "Включите JavaScript, или используйте иной броузер";
		ajax.setVar("re1", document.getElementById('regid').value);
		ajax.setVar("re2", document.getElementById('gorid').value);
	ajax.requestFile = "script/ajax-select.php";
	ajax.method = "GET";
	ajax.element = 'replaceme';
	ajax.onLoading = whenLoading;
	ajax.onLoaded = whenLoaded;
	ajax.onInteractive = whenInteractive;
                     ajax.onError = whenError;
	ajax.onCompletion = whenCompleted;
                     ajax.runAJAX();
}


