var arrtlist=new Array();
var arrtlist_ru=new Array();
var arrtlist_en=new Array();

function sendRequest(url,method,callback,onerror, callbackArgsArray) {
  var request = false;
  if (window.XMLHttpRequest) // Mozilla, Safari, ...
    request = new XMLHttpRequest();
  else if (window.ActiveXObject) // IE
  	request = new ActiveXObject("Microsoft.XMLHTTP");
  	
  if (typeof(callbackArgsArray)=="undefined") callbackArgsArray=new Array();
  
  request.onreadystatechange = function() {
    requesFunction(request,method,callback,onerror, callbackArgsArray);
  };
  request.open(method, url, true);
  request.send(null);
}

function requesFunction(req,method,fcall,ferror, callbackArgsArray) {
	var self;
  if (req) self={req:req,method:method,fcall:fcall,ferror:ferror,callbackArgsArray:callbackArgsArray,request:arguments.callee.request}
  else self=arguments.callee;
  if(self.req.readyState == 4) {
  	callbackArgsArray=self.callbackArgsArray;
    //var callbackArgsArray=new Array(self.req);
    
    	
    callbackArgsArray=self.callbackArgsArray;

    if (callbackArgsArray.length==1){
    	var param=callbackArgsArray[0];
    	callbackArgsArray[0]=self.req;
    	callbackArgsArray[1]=param;
    }
    else callbackArgsArray[0]=self.req;
    
    try{
    	if(self.req.status == 200) {
        self.fcall.apply(null,callbackArgsArray);
      }
      else self.ferror.apply(null,callbackArgsArray);
    }
    catch(err) {
    	self.ferror.apply(null,callbackArgsArray);
    }
		//self.req.ready=0;
  	//self.req.abort();
  }
}

function fError(xrequest)
{
    // if(window.console) window.console.info(xrequest);
  clearсlist('Невозможно загрузить список...');
}

function createclist(xrequest) {

  var countrylist=document.getElementById("clist");
  countrylist.options.length=0;

  var listrecords=xrequest.responseXML.getElementsByTagName('c');
  if (listrecords)
  {
   countrylist.options.length=0;
   for (i=0; i<listrecords.length;i++)
   {
     var newOption = document.createElement("OPTION");
     newOption.value = listrecords[i].getAttribute("id");
     newOption.text = listrecords[i].getAttribute("name");

     if (listrecords[i].getAttribute("sid")) var selectedIndex=i;
     countrylist.options[countrylist.options.length] = newOption;
     newOption = null;
   }
   countrylist.selectedIndex=selectedIndex;

  }
  else countrylist.options[0]="Нет данных";
}

function createtlist(xrequest)
{
	cleartlist("");

  var townlist=document.getElementById("tlist");
  if (townlist) {

   townlist.options.length=0;

   var listrecords=xrequest.responseXML.getElementsByTagName('t');
   if (listrecords)
   {
    townlist.options.length=0;

	  j=0;
    for (i=0; i<listrecords.length;i++)
    {    	if (listrecords[i].getAttribute("name")=="") continue;

	      var newOption = document.createElement("OPTION");
	      newOption.value = listrecords[i].getAttribute("id");
	      //if (lang!=domain.toLowerCase()) newOption.text = listrecords[i].getAttribute("name_rus");
	      //else newOption.text = listrecords[i].getAttribute("name");
	      newOption.text = listrecords[i].getAttribute("name");

	      arrtlist.push(listrecords[i].getAttribute("name"));
	      arrtlist_ru.push(listrecords[i].getAttribute("name_rus"));
	      arrtlist_en.push(listrecords[i].getAttribute("name_eng"));

	      if (listrecords[i].getAttribute("sid")==1) var currentIndex=j;
	      townlist.options[townlist.options.length] = newOption;
	      newOption = null;
	      j++;
    }
    townlist.selectedIndex=currentIndex;

    if (document.getElementById('tl')) {
    	if (document.getElementById('tl').options.length==0) fillinftlist();
    }
    else updateInf();
   }
   else townlist.options[0]="Нет данных";
  }
}

function cleartlist(str)
{
  var townlist=document.getElementById("tlist");
  townlist.options.length=0;
  if (!document.getElementById('tl')) {
    arrtlist.length=0;
    arrtlist_ru.length=0;
    arrtlist_en.length=0;
  }
  var newOption = document.createElement("OPTION");
  newOption.value = "-1";
  newOption.text = str;
  townlist.options[townlist.options.length] = newOption;
  newOption = null;
}

function clearсlist(str)
{
  var countrylist=document.getElementById("сlist");
  if (!countrylist) return;
  countrylist.options.length=0;
  if (!document.getElementById('tl')) {
    arrtlist.length=0;
    arrtlist_ru.length=0;
    arrtlist_en.length=0;
  }
  var newOption = document.createElement("OPTION");
  newOption.text = str;
  countrylist.options[countrylist.options.length] = newOption;
  newOption = null;
}

function GetHTMLInf(xrequest,idname){
	document.getElementById("informer").innerHTML=xrequest.responseText;
}
