﻿VKInited=0;
VKREGInited=0;
VKAuthUrl='/vkauth.php';

function OnReady() {
  if (VKInited==0)
  {
    VK.init({apiId: 2250774});
    VKInited=1;
  }
  x=document.getElementById('vk_auth2');
  if (x)
    VK.Widgets.Auth('vk_auth2', {width: '370px', authUrl: '/vkauth.php'});
}

function enterquicksearch(){
  el = $(":focus");
  if (!el) return false;
  s=el.val();
  if ((s=='Невский пр, 40')|| (s=='Гражданский, 43') || (s=='Р-моторс'))
    el.val('');
}

function showmetro() {
  w = $(window).width();
  h = $(document).height();
  hw = $(window).height();
  $("#Hider").width(w);
  $("#Hider").height(h);
  $("#Hider").show();
  
  $("#Metro").css("left", w/2 - $("#Metro").width()/2);
  $("#Metro").css("top", hw/2 - $("#Metro").height()/2);
  $("#Metro").slideDown();
}

function closemetro() {
  $("#Metro").slideUp();
  $("#Hider").hide();
}

function closedialog() {
  $("#Forms").slideUp();
  $("#Hider").hide();
}
function ShowLoginForm() {
  w = $(window).width();
  h = $(document).height();
  hw = $(window).height();
  $("#Hider").width(w);
  $("#Hider").height(h);
                                                                                                                                                           
  loginform='<form action="/login" class="plainform" method="post"><input type="hidden" name="act" value="enter"><label for="LoginID">Логин</label><input id="LoginID" type=text name="login"><div class="cb h10"></div><label for="PasID">Логин</label><input id="PasID" type=password name="pas"><div class="cb h10"></div><a href="/lostpass" class="forgotlink">Забыли пароль?</a><input type="submit" value="Войти" /><div class="cb"></div></form><div class="cb"></div><p>Если у Вас есть аккаунт в Вконтакте - просто нажмите на кнопку:</p><div id="vk_auth" class=vkarea></div>';

  $("#Forms").html('<h1>Вход на сайт</h1><div class="close"><a href="#" onclick="closedialog(); return false;">я передумал</a></div>'+loginform);
  
  $("#Forms").css("left", w/2 - $("#Forms").width()/2);
  $("#Forms").css("top", hw/2 - $("#Forms").height()/2);
  
  if (VKInited==0)
  {
    VK.init({apiId: 2250774});
    VKInited=1;
  }
  x=document.getElementById('vk_auth');
  if (x)
    VK.Widgets.Auth('vk_auth', {width: '370px', authUrl: '/vkauth.php'});
 
  
  $("#Hider").show();
  $("#Forms").slideDown();  
}

function ShowRegisterForm() {
  w = $(window).width();
  h = $(document).height();
  hw = $(window).height();
  $("#Hider").width(w);
  $("#Hider").height(h);
  loginform='<form action="/registration" enctype="multipart/form-data" method="post" class="plainform"><input type="hidden" name="act" value="do"><input type="hidden" name="return" value=""><label for="login">Логин</label><input type="text" name="login" id="login"><div class="cb h10"></div><label for="password1">Пароль</label><input type="password" name="password1" id="password1"><div class="cb h10"></div><label for="password2">Повторите пароль</label><input type="password" name="password2" id="password2"><div class="cb h10"></div><label for="name">Ваше имя</label><input type="text" name="name" id="name"><div class="cb h10"></div><label for="email">eMail</label><input type="text" name="email" id="email"><div class="cb h10"></div><label for=avatar>Загрузим аватарку?</label><input type="file" name="avatar" id=avatar><div class="cb h10"></div><input type=submit value="Регистрация"><div class="cb"></div></form><div class="cb"></div><p>Если у Вас есть аккаунт в Вконтакте - просто нажмите на кнопку:</p><div id="vk_auth" class=vkarea></div>';
  $("#Forms").html('<h1>Регистрация на сайте</h1><div class="close"><a href="#" onclick="closedialog(); return false;">я передумал</a></div>'+loginform);
  
  $("#Forms").css("left", w/2 - $("#Forms").width()/2);
  $("#Forms").css("top", hw/2 - $("#Forms").height()/2);
  
  if (VKInited==0)
  {
    VK.init({apiId: 2250774});
    VKInited=1;
  }
  x=document.getElementById('vk_auth');
  if (x)
    VK.Widgets.Auth('vk_auth', {width: '370px', authUrl: '/vkauth.php'});
 
  
  $("#Hider").show();
  $("#Forms").slideDown();  
}
function ShowAddPriceForm() {
  $("#AddPriceLinkArea").slideUp();
  $("#AddPriceForm").slideDown();  
}

function ShowAddCommentForm() {
  $("#AddCommentsLinkArea").slideUp();
  $("#AddCommentForm").slideDown();
}

function ShowAddPhotosForm() {
  $("#AddPhotosLinkArea").slideUp();
  $("#AddPhotosForm").slideDown();
}

function numbersonly(myfield, e, dec) {
  var key;
  var keychar;

  if (window.event)
    key = window.event.keyCode;
  else if (e)
    key = e.which;
  else
    return true;
  keychar = String.fromCharCode(key);
  if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) || (key==37) || (key==39) || (key==46) )
    return true;
  else if ((("0123456789").indexOf(keychar) > -1))
    return true;
  else if (dec && (keychar == ".")) {
    myfield.form.elements[dec].focus();
    return false;
  } else
    return false;
}

function RecalcNalog() {
  hp = $("#hpsid").val();
  if (document.getElementById("autotype1").checked)
  {
    if (hp<=100)
      n10=hp*24;
    if ((hp>100)&&(hp<=150))
      n10=hp*35;
    if ((hp>150)&&(hp<=200))
      n10=hp*50;
    if ((hp>200)&&(hp<=250))
      n10=hp*75;
    if (hp>250)
      n10=hp*150;
  }
  if (document.getElementById("autotype2").checked)
  {
    if (hp<=20)
      n10=hp*10;
    if ((hp>20)&&(hp<=35))
      n10=hp*20;
    if (hp>35)
      n10=hp*50;
  }
  if (document.getElementById("autotype3").checked)
  {
    if (hp<=200)
      n10=hp*50;
    if (hp>200)
      n10=hp*65;
  }
  if (document.getElementById("autotype4").checked)
  {
    if (hp<=100)
      n10=hp*25;
    if ((hp>100)&&(hp<=150))
      n10=hp*40;
    if ((hp>150)&&(hp<=200))
      n10=hp*50;
    if ((hp>200)&&(hp<=250))
      n10=hp*55;
    if (hp>250)
      n10=(hp*25)+'<i> руб</i> <small>до 3 лет;</small><br> '+(hp*35)+'<i> руб</i> <small>от 3 до 5 лет;</small><br>'+(hp*65)+'<i> руб</i> <small>старше 5 лет</small>';
  }
  $("#NalogValue").html(n10+'<i> руб</i>');
}

function PreCheckTalk() {
  if ($('#TalkNameId').val()=='')
  {
    alert('Не указано название статьи!');
    return false;
  }
  s=$('#redactor').val();
  s=s.replace(/<\/?[^>]+>/gi, '');
  s=s.replace( /^\s+/g, '');;
  if (s=='')
  {
    alert('Не указано содержание статьи!');
    return false;
  }
  s=$('#redactor').val();
  x=s.indexOf('<hr class="redactor_cut"/>') + 1;
  if (x==0)
  {
    alert('Не установлен разрыв страницы!');
    return false;
  }
  return true;
}

function getcaption(x, s1, s2, s3)
{
  x=x%100;
  if ((x>=10)&&(x<=19))
    return s3;
  else
  {
    x=x%10;
    if (x==0)
      return s3;
    if (x==1)
      return s1;
    else
    if ( (x==2)||(x==3)||(x==4))
      return s2;
    else
    if ( (x>=5)||(x<10))
      return s3;
    else
     return s1;
  }
}

function ShowAnalogs(pid) {
  $("#AnalogTable"+pid).toggleClass('hidden');
  if ($("#AnalogTable"+pid).hasClass('hidden'))
    $("#AnalogTable"+pid+"link").html('&darr; показать');
  else
    $("#AnalogTable"+pid+"link").html('&uarr; скрыть');
}

function ShowPartArea(pid) {
  
  $(".partsareas").addClass('hidden');
  $(".partsareas").removeClass('selected');
  
  $("#PartArea"+pid).removeClass('hidden');
  $("#PartArea"+pid).addClass('selected');
  
  
  $(".partselect a").removeClass('selected');
  $("#PartArea"+pid+"link").addClass('selected');
  
  RecalcPrice();
}

function formattime($x) {
  $hours = Math.floor($x/100);
  $mins= $x-($hours*100);
  $mins = (60/100)*$mins;
  $mins=Math.round($mins/10)*10;
  if ($hours>0)
  {
    if ($mins==0)
      return $hours+' '+getcaption($hours, 'час', 'часа', 'часов');
    else
      return $hours+' '+getcaption($hours, 'час', 'часа', 'часов')+' и '+$mins+" "+getcaption($mins, 'минута', 'минуты', 'минут');
  }
  else
    return $mins+" "+getcaption($mins, 'минута', 'минуты', 'минут');
}

function RecalcPrice() {
  $total=0;
  $(".partsareas.selected ul.works input[type=checkbox]").each(
        function() {
          if ($(this).is(":checked")) {
            if ($(this).attr('vl'))
            {
              $(this).parent().removeClass('notchecked');
              $total = $total + parseInt($(this).attr('vl'));
            }
          }
          else
            $(this).parent().addClass('notchecked');
        }
  );
  RefreshASPrices($total);
  $("#totaltime").html(formattime($total));
}

function RefreshASPrices(x) {
  $(".pricetable .pricecol").each(
        function() {
          if ($(this).attr('vl'))
          {
            y=$(this).attr('vl');
            $pr=Math.round((x/100)*y);
            $(this).html($pr+' руб.');
          }
          else
          {
          }
        }
  );
}
function FilterWorkGroups() {
  flt=$("#filterbyname").val();
  if (flt=='')
  {
    $("#filterbyname").show();
  }
  else
  {
    if ((flt.length>=3)||(apl==1))
    {
      apl=1;
      $("#WorkGroupsTable tr").each(
        function() {
          vis=0;
          yf = $(this).attr('ln');
          if (yf) 
          {
            if(yf.indexOf(flt) + 1)      
            {
              vis=1;
            }
          }
          if (vis==0)
          {
            if ($(this).css("display")!="none")
              $(this).hide();
          }
          else
          if ($(this).css("display")=="none")
            $(this).show();
        }        
      );  
    }
  }
}

