$(document).ready(function(){
  var the_content;
  var top_content;
  var bottom_content;

  top_content = '<table cellspacing=0 cellpadding=0 border=0 width=100%><tr><td style="padding:16px">';
  bottom_content = '</td></tr></table>';

  $.jtabber({
    mainLinkTag: "#nav a", // much like a css selector, you must have a 'title' attribute that links to the div id name
    activeLinkClass: "selected", // class that is applied to the tab once it's clicked
    hiddenContentClass: "hiddencontent", // the class of the content you are hiding until the tab is clicked
    showDefaultTab: 1, // 1 will open the first tab, 2 will open the second etc.  null will open nothing by default
    showErrors: true, // true/false - if you want errors to be alerted to you
    effect: 'fade', // null, 'slide' or 'fade' - do you want your content to fade in or slide in?
    effectSpeed: 'fast' // 'slow', 'medium' or 'fast' - the speed of the effect
  })

  $('div.hiddencontent').each(function() {
    the_content = top_content + $(this).html() + bottom_content;
    $(this).html(the_content);
    $(this).corner("10px");
  });
  $('#nav a:first-child').css("margin-left", "17px");
  $('#org_load').corner("10px");

  $("#load_user").attr("disabled", "true");

  if ($("#srv_login").attr("value")) {
    $(".for_user").html(" (для пользователя <b>" + $("#srv_login").attr("value") + "</b>)");
    $("#use_profile").css("display", "none");
  } else {
    $("#use_profile").css("display", "block");
  }                                  


  $("#use_profile_link").bind("click", function(e){
    $("#org_load").css("display", "block");
    $("#chooseOrgForm").css("display", "none");
    $("#org_load_lnk").css("display", "none");
    return false;
  });


  $("#load_user_cancel").bind("click", function(e){
    $("#org_load").css("display", "none");
    $("#chooseOrgForm").css("display", "block");
    $("#org_load_lnk").css("display", "block");
    return false;
  });


  $("#user_login, #user_passwd").bind("keyup", function(e){
    if (!$("#user_login").attr("value") || !$("#user_passwd").attr("value"))
      $("#load_user").attr("disabled", "true");
    else
      $("#load_user").removeAttr("disabled");
  });

  /*


  $('#manual').click(function(e) {
    e.preventDefault();
    var content = 'Content wrote in JavaScript<br />';
    jQuery.each(jQuery.browser, function(i, val) {
      content+= i + " : " + val+'<br />';
    });
    $.nyroModalManual({
      bgColor: '#3333cc',
      content: content
    });
    return false;
  });
  $('#manual2').click(function(e) {
    e.preventDefault();
    $.nyroModalManual({
      url: 'demoSent.php'
    });
    return false;
  });
  */
});


function check_org_form (form) {
  if ((!form.yur.checked)&&(!form.pred.checked)&&(!form.chast.checked)) {
    alert ("Выберите нужный вариант");
    return false;
  }
  return true;
}

function check_login (form) {
  var the_login, the_passwd;

  $("#load_user").attr("disabled", "true");

  the_login = $("#user_login").attr("value");
  the_passwd = $("#user_passwd").attr("value");

  $.post("login.php", { action: "login", login: the_login, password: the_passwd}, function(data) {

    var the_login;

    $("#load_user").removeAttr("disabled");

    if (data == "false") {
      alert ("Ошибка входа!");
    } else {
      the_login = $("#user_login").attr("value");
      $("#srv_login").attr("value", the_login);
      $("#use_profile").css("display", "none");
      $(".for_user").html(" (для пользователя <b>" + the_login + "</b>)");
      $("#" + data).attr("checked", "true");
      alert ("Вход осуществлен успешно!");
      $("#send").click();
    }
  
  });

  return false;
}


