function cleanup(elem)
{
  num = elem.childNodes.length;
  
  while(num)
  {
    num--;
    
    if(num > 1)
    {
      elem.removeChild(elem.lastChild);
    }
  }
  
  return true;
}


function setNoticeByElementId(id, s)
{
  if(s)
  {
    $(id).show();
  }else{
    $(id).hide();
  }
  return true;
}


function getAutoByCity(city)
{
  new Ajax.Request("/client/autocredit/get/csv/sample.php?k=autolist", {
   method:'post',
   parameters: "city=" + city,
   asynchronous:false,
   onComplete: function(request){
      var rst = eval('(' + request.responseText + ')');
      
      cleanup($("form2")["auto"]);
      
      for(var i=0; i<rst.list.length; i++)
      {
        new Insertion.Bottom($("form2")["auto"], '<option value="' + rst.list[i] + '">' + rst.list[i] + '</option>');
        ($("form2")["auto"]).selectedIndex = 0;
      }
   }
  });
  $("tfa").innerHTML="";
  return true;
}



function getDealerByAuto(auto)
{
  var city = $("form1")["city"];
  
  new Ajax.Request("/client/autocredit/get/csv/sample.php?k=dealerlist", {
   method:'post',
   parameters: "city=" + city.value + "&auto=" + auto,
   asynchronous:false,
   onComplete: function(request){
      var rst = eval('(' + request.responseText + ')');
      
      var buf  = '';
          buf += '<table border="0" cellspacing="0" cellpadding="0">';
          buf += '<tr>';
      
      for(var i=0; i<rst.length; i++)
      {
        // if(i>0 && i%3==0) buf += '</tr><tr>';
        buf += '<tr>';
        buf += '<td width="270" align="left" valign="top">';
        buf += '<div class="avtosalon">';
        buf += '<div class="color_red"><strong>' + rst[i].name + '</strong></div>';
        buf += '<p>' + rst[i].adr + (rst[i].phone? '<br />' + (rst[i].phone).gsub(', ', '<br />') : '') + '</p>';
        // buf += '<p>' + rst[i].adr + '</p>';
        buf += '</div>';
        buf += '</td>';
        buf += '</tr>';
        
        // токены
        var x = rst[i].x? rst[i].x : 0;
        var y = rst[i].y? rst[i].y : 0;
        
        if(x && y)
        {
          var token = "token_x" + x + "_y" + y;
          
          // var html  = '<div class="ag" onMouseOver=\' setNoticeByElementId("' + token + '_notice", true); \' onMouseOut=\' setNoticeByElementId("' + token + '_notice", false); \' id="' + token + '" style="left:' + x + 'px; top:' + y + 'px;">';
          //     html += '<div id="' + token + '_notice" style="display:none;">' + rst[i].name + '<br />' + rst[i].adr + '</div>';
          //     html += '</div>';
          
          var html  = '<div class="dot" id="' + token + '" style="left: ' + x + 'px; top: ' + y + 'px;" onMouseOver=\' setNoticeByElementId("' + token + '_notice", true); \' onMouseOut=\' setNoticeByElementId("' + token + '_notice", false); \'></div>';
              html += '<div class="ag" id="' + token + '" style="left: ' + x + 'px; top: ' + y + 'px;">';
              html += '<div class="notice" id="' + token + '_notice" style="display: none;"><nobr>' + rst[i].name + '</nobr><br /><nobr>' + rst[i].adr + '</nobr><br /><nobr>' + (rst[i].phone).gsub(', ', '<br />') + '</nobr></div>';
              html += '</div>';
              
          new Insertion.Bottom("token", html);
        }
      }
      
          buf += '</tr>';
          buf += '</table>';
          
      $("tfa").innerHTML = buf;
   }
  });
  return true;
}