

function login_functions() {
    if ($('#login #signOut').length > 0) return;
    var signIn = document.getElementById('signIn');
    var signInNow = document.getElementById('signInNow');

    
    signIn.onclick = function() {
        $('#signInNow').toggleClass('visible');
        $('#signIn').toggleClass('signInOnForm');
        $('#signIn').toggleClass('active');
        return false;
    }
    var uregid = document.getElementById('uregid');
    if (uregid) {
    var uregidLabel = document.getElementById('uregidLabel');
    if(uregid.value != ''){
        uregidLabel.style.display = 'none';
    }
    else{
        uregid.onfocus = function() {
                uregidLabel.style.display = 'none';        
        }        
        uregid.onblur = function() {
            if(uregid.value != ''){
                uregidLabel.style.display = 'none';        
            }
            else{
                uregidLabel.style.display = 'block';
            };
        }        
    };
    uregid.onclick = function() {
        uregidLabel.style.display = 'none';
        uregid.focus();
    }  
    }  
    var regid = document.getElementById('regid');
    var regidLabel = document.getElementById('regidLabel');
    if(regid.value != ''){
        regidLabel.style.display = 'none';
    }
    else{
        regid.onfocus = function() {
                regidLabel.style.display = 'none';        
        }        
        regid.onblur = function() {
            if(regid.value != ''){
                regidLabel.style.display = 'none';        
            }
            else{
                regidLabel.style.display = 'block';
            };
        }        
    };
    regid.onclick = function() {
        regidLabel.style.display = 'none';
        regid.focus();
    }    
    $('#login label').bind('mousemove', function() {
      if ($(this).is('.error')) {
        $(this).hide().prev().each(function() {this.focus(); });
      }
      return false;
    })    
    var cancel = $('div#login a.cancel');
    
    cancel.each(function(){
        $(this).bind('click', function() {
            if ($('#signInNow').is('.visible')){
                $('#signInNow').removeClass('visible');
                $('#signIn').removeClass('active');
                $('#signIn').toggleClass('signInOnForm');
            }  
            return false;
        });
    });
  $('#regid').bind('keyup', function() {
    if ( /^[01-9 \+\-\)\(]+$/.test($('#regid').attr('value'))) {
      $('#signInNow a.primary').removeClass('disabled').find('span').removeClass('disabled');
    } else {
      $('#signInNow a.primary').addClass('disabled').find('span').addClass('disabled');
    }
  }).bind('keydown', function(event) {
    var key = event.charCode ? event.charCode : event.keyCode;
    if (key==13) {
      $('#signInNow a.primary').trigger('click');
      this.blur();
    }
  });

  $('#signInNow a.reglink').bind('click', function() {
    if (!$(this).is('.disabled')) {
      $('#invis').load('/login.php?regid=' + encodeURIComponent($('#regid').attr('value')) + '&save_login=' + ($('#rememberMe').attr('checked') ? 1 : 0), function() {
        var s = $('#invis').html().split('|');
        if (s[0] == 5) {
          window.loginblock = s[1];
          window.logincity = s[2];
          window.logincurr = s[3];
          $('#signInNow a.secondary').trigger('click');
          if ($('div.container[@rel=conflict]').length) {
            while ($('div.container:last').attr('rel') != 'conflict') page_close();
            page_close();
          }
          //conflict_close();
          //$('#login').html(s[1]);
          //logout_functions();
          //reload_cart();
          //reload_containers();
          var last_href = modal_link('/conflict/');
          $('div.selectedItem[@rel=c]').hide();          
          $('div.container:last').hide().each(function() {
            var el = document.createElement('div');
            $(el).attr('id', 'container').attr('class', 'container').attr('rel', 'conflict').insertAfter($(this)).load(last_href, function() {
              set_win_title();
              //page_func();
              conflict_func();      
              Custom.init();
            });
          });    
          set_href('/conflict/');
          return false;
        } else if (s[0] == 4) {
          $('#login').html(s[1]);
          clickcity(s[2]);
          clickcurr(s[3]);
          logout_functions();
          conflict_close();
          reload_cart();
          reload_containers();
        } else if (s[0] == 3) {
          $('#signInNow div').not('.buttons').add('#signInNow div.buttons a.primary').hide();
          $('#signInNow div.buttons a.secondary').html('<span>Закрыть</span>').removeClass('secondary').addClass('primary').parent().before('<div class="logmsg">' +htmlspecialchars(s[1])+ '</div>');
        } else if (s[0] == 1) {
          $('#regidLabel').html(s[1]).show().addClass('error');
        }
        $(this).html('');
      })
    }
    return false;
  });
}
function logout_functions() {
  if ($('#login #signOut').length == 0) return;
  $('#signOut').filter('.modal').bind('click', function() {
    $('#login').load('/logout.php?modal=1', function() {
      clickcity(20);
      clickcurr(1);
      login_functions(); 
      if ($('div.container:last').attr('rel') == 'conflict') {
        if ($('div.container').length == 0) {
          document.location.href = '/';
        } else {
          page_close();
        }
      }
      Custom.init();     
      reload_cart();
      reload_containers(1);
    });
    return false;
  })
  $('#login .address2').bind('click', function() {
    if ($('div.container:last').attr('rel') == 'profile') {
      page_close();
    } else {
      var last_href = modal_link('/profile/');
      $('div.selectedItem[@rel=c]').hide();
      $('div.container:last').hide().each(function() {
        var el = document.createElement('div');
        $(el).attr('id', 'container').attr('class', 'container').attr('rel', 'profile').insertAfter($(this)).load(last_href, function() {
          set_win_title();
          page_func();
          profile_func();        
          Custom.init();
        });
      }); 
      set_href('/profile/');
    }
    return false;
  })
}
$(document).bind('ready', login_functions).bind('ready', logout_functions);
