function exetelLoginOpen()
{
  $('navigation-login').show();
  $('exetel-login-inp').focus();
  return false;
}

function exetelLoginClose()
{
  $('navigation-login').hide();
  return false;
}

function exetelLoginSubmit()
{
  $('exetel-login-form').request({
    onFailure: function(transport)  { $('exetel-login-errors').update('Неизвестная ошибка'); $('exetel-login-errors').show(); },
    onLoading: function(transport)  { $('exetel-login-submit').disable(); $('exetel-login-busy').show(); $('exetel-login-errors').hide(); },
    onComplete: function(transport) { $('exetel-login-submit').enable();  $('exetel-login-busy').hide(); },
    onSuccess: function(transport) {
      reply = transport.responseJSON;
      
      if(!reply || !reply.result)
      {
        $('navigation-login-errors').update('Неверный логин или пароль');
        $('navigation-login-errors').show();
      }
      else
      {
        location.reload();
      }
    }
  });
  
  return false;
}

function initTextArea(id)
{
  //setup  
  var textarea = new Control.TextArea(id);  
  var toolbar = new Control.TextArea.ToolBar(textarea);  
  toolbar.container.id = 'bbcode_toolbar'; //for css styles
    
  //buttons  
  toolbar.addButton('i', function() { this.wrapSelection('[i]','[/i]'); }, { id: 'bbcode_italics' });
  toolbar.addButton('B', function() { this.wrapSelection('[b]','[/b]'); }, { id: 'bbcode_bold' });
  toolbar.addButton('u', function() { this.wrapSelection('[u]','[/u]'); }, { id: 'bbcode_underline' });
  toolbar.addButton('d', function() { this.wrapSelection('[del]','[/del]'); }, { id: 'bbcode_del' });
    
  toolbar.addButton('Ссылка', function(){  
    var selection = this.getSelection();  
    var response = prompt('Введите адрес ссылки','');  
    if(response == null) return;
    
    if(selection == '') this.replaceSelection('[url]'+response+'[/url]');
    else this.replaceSelection('[url='+response+']' + selection + '[/url]');  
  },{ id: 'bbcode_link' });  
    
  toolbar.addButton('Картинка', function(){  
    var selection = this.getSelection();
    if(selection == '')
    {
      var response = prompt('Введите адрес картинки','');  
      if(response == null) return;  
      this.replaceSelection('[img]'+response+'[/img]');
    }
    else 
      this.replaceSelection('[img]'+selection+'[/img]');
  },{  
      id: 'bbcode_image'  
  });
  
  toolbar.addButton('&bull;', function() { this.insertAfterSelection('\n[*] '); }, { id: 'bbcode_ul_li' });
  toolbar.addButton('1.', function() { this.insertAfterSelection('\n[-] '); }, { id: 'bbcode_ol_li' });
  
  toolbar.addButton('Заголовок', function() { this.insertAfterSelection('\n[h1] '); }, { id: 'bbcode_h1' });
  
  toolbar.container.insert(Builder.node('a', { href: 'http://wiki.vism.ru/r1:bbcodes', style: 'float:right', target: '_blank' }, 'Справка'));
}

function rateProvider(l, isp_id)
{
  var l1;
  
  l.up().insert(
    l1 = Builder.node('div', { className: 'isp-rate', style: 'display:none;width:330px;height:180px;top:'+(l.offsetTop + l.getHeight() / 2 - 90)+'px;'+'left:'+(l.offsetLeft + l.getWidth() / 2 - 165)+'px;' })
  );
  
  l1.innerHTML += '<form action="/provider/'+isp_id+'/vote" method="post" onsubmit="return rateProviderSubmit(this);">\
    <table><thead><tr><th>Ваша оценка</th><th>-2</th><th>-1</th><th>0</th><th>+1</th><th>+2</th></tr></thead><tbody>\
    <tr><td align="right">Надежность</td><td><input type="radio" name="vote_1" value="-2"></td><td><input type="radio" name="vote_1" value="-1"></td><td><input type="radio" name="vote_1" value="0" checked=""></td><td><input type="radio" name="vote_1" value="1"></td><td><input type="radio" name="vote_1" value="2"></td></tr>\
    <tr><td align="right">Обслуживание</td><td><input type="radio" name="vote_2" value="-2"></td><td><input type="radio" name="vote_2" value="-1"></td><td><input type="radio" name="vote_2" value="0" checked=""></td><td><input type="radio" name="vote_2" value="1"></td><td><input type="radio" name="vote_2" value="2"></td></tr>\
    <tr><td align="right">Цены</td><td><input type="radio" name="vote_3" value="-2"></td><td><input type="radio" name="vote_3" value="-1"></td><td><input type="radio" name="vote_3" value="0" checked=""></td><td><input type="radio" name="vote_3" value="1"></td><td><input type="radio" name="vote_3" value="2"></td></tr>\
    <tr><td align="right">Локальная сеть</td><td><input type="radio" name="vote_4" value="-2"></td><td><input type="radio" name="vote_4" value="-1"></td><td><input type="radio" name="vote_4" value="0" checked=""></td><td><input type="radio" name="vote_4" value="1"></td><td><input type="radio" name="vote_4" value="2"></td></tr>\
    </tbody></table>\
    <div style="float:right;margin:4px 3px;"><input type="submit" name="" value="Оценить" style="font-weight:bold"></div><div style="float:left;margin:4px 3px;"><input type="button" value="Закрыть" onclick="rateProviderClose(this)"/></div></form>\
    <div style="clear:both;text-align:right;padding:5px 3px 0;font-size:1.1em;"><a href="/provider/'+isp_id+'/write.html">Написать подробный отзыв &rarr;</a></div>';
  
  Effect.Grow($(l1), { duration: 0.5 });
}

function rateProviderSubmit(l)
{
  $(l).request( {
    onSuccess: function(transport) {
      var reply = transport.responseJSON;
      
      l.up().up().down().update(reply.rating);
      Effect.Shrink($(l.up()), { duration: 0.5 });
    }
  });
  
  return false;
}

function rateProviderClose(l)
{
  Effect.Shrink($(l.up().up().up()), { duration: 0.5 });
}

var ispConnectAC;
document.observe('dom:loaded', function () {
  if($('connect-form'))
  {
    ispConnectAC = new Ajax.Autocompleter('connect-street', 'connect-ac', "/provider/street.html", { paramName: "m", minChars: 1 });
  }
});

function ispConnectCheck()
{
  new Ajax.Request('/provider/check.json', {
    method: 'post',
    postBody: $('connect-form').serialize(),
    
    onLoading: function(transport)  { $('connect-submit').disable(); },
    onComplete: function(transport) { $('connect-submit').enable(); },
    onSuccess: function(transport) {
      reply = transport.responseJSON;
      
      $('connect-house').value = reply.house;
      
      if(!reply.house || reply.house == '')
        $('connect-house').addClassName('invalid');
      else
        $('connect-house').removeClassName('invalid');
        
      if(!reply.location)
        $('connect-street').addClassName('invalid');
      else
        $('connect-street').removeClassName('invalid');
      
      if(reply.location && reply.house && reply.house != '')
        location.href = '/provider/connect.html?l=' + reply.location + '&h=' + reply.house;
    }
  });
  
  return false;
}
