Event.observe(window, 'load', init, false);
function init(){
  if(typeof(myPage)=='undefined') return;
  myPage.target=$(myPage.targetID);
  if(typeof(myPage.erorMsgID)=='string') myPage.erorMsg=$(myPage.erorMsgID);
  if(typeof(myPage.loginCheckMsgID)=='string') myPage.loginCheckMsg=$(myPage.loginCheckMsgID);
  if(typeof(myPage.NewCountryTD)=='string') myPage.NewCountryTD=$(myPage.NewCountryTD);
  if(typeof(myPage.NewCityTD)=='string') myPage.NewCityTD=$(myPage.NewCityTD);
}

function lookup(params) {
  if(myPage.params) params=myPage.params+params;
  var ajax1   = new Ajax.Request(
               myPage.url,
              {method: 'get',
               parameters: params,
               onFailure: reportError,
               onSuccess: showResponse,
               onLoading: showResponse}
      );
}

function reportError(request) {
  if(myPage.erorMsg) myPage.erorMsg.innerHTML="Ошибка "+request.statusText;

}

function showResponse(request) {
  if (request.readyState == 4) {
     if (request.status == 200) {
         fillSelect(myPage.target, request.responseText+"\n");
         if(myPage.erorMsg) myPage.erorMsg.innerHTML='';
     }
     else if(myPage.erorMsg) myPage.erorMsg.innerHTML="Ошибка "+request.statusText;
  }else{
     // данные в процессе получения, можно повеселить
     // пользователя сообщениями ЖДИТЕ ОТВЕТА
     fillSelect(myPage.target, "0\tПожалуйста подождите ...\n\n");
     myPage.erorMsg.innerHTML="<img src='/images/ajax.gif' border='0' width='16' height='16'>";
  }
}
function addOption (oListbox, text, value, isDefaultSelected, isSelected)
{
  var oOption = document.createElement("option");
  oOption.appendChild(document.createTextNode(text));
  oOption.setAttribute("value", value);

  if (isDefaultSelected) oOption.defaultSelected = true;
  else if (isSelected) oOption.selected = true;

  oListbox.appendChild(oOption);
}

function fillSelect (mySelect, myData){
  // если данных нет – не делаем больше ничего
  if(myData.length == 0) return alert('Пустой результат!');
  mySelect.length = 0;
  mySelect.options.length = 0;
  var myRows = myData.split('\n');
  var mySelected = true;
  // для каждой строки
  for(i=0;i<myRows.length;i++){
      // в массиве val – поля полученной таблицы
      val = myRows[i].split('\t');
      // добавляем новый объект OPTION к нашему SELECT
      if(typeof val != 'undefined' && val[0]!='')
      addOption(mySelect, val[1], val[0], mySelected, mySelected);
      mySelected=false;
  }
  if(mySelect.length==0) return;
  mySelect.SelectedIndex=0;
  if(typeof(myPage.NewCityTD)=='object'){
    if(mySelect.options[0].value==-1) myPage.NewCityTD.style.display='block';
    else myPage.NewCityTD.style.display='none';
  }
}

function checklogin(params) {
  var ajax2   = new Ajax.Request(
               myPage.url,
              {method: 'get',
               parameters: params,
               onFailure: reportError,
               onSuccess: loginCheckResult}
      );
}

function loginCheckResult(request) {
  if (request.readyState == 4) {
     if (request.status == 200) {
         myPage.loginCheckMsg.innerHTML=request.responseText;
     }
     else if(myPage.erorMsg) myPage.erorMsg.innerHTML="Ошибка "+request.statusText;
  }else{
     // данные в процессе получения, можно повеселить
     // пользователя сообщениями ЖДИТЕ ОТВЕТА
     myPage.loginCheckMsg.innerHTML="<img src='/images/ajax.gif' border='0' width='16' height='16'>";
  }
}
