function login_submit_handler(form)
{
  jQuery(form).ajaxSubmit({
       url: '/ajax/login.php',
       dataType: 'json',
       success: function(Response){
               
            if (Response.need_redirect)
            {
                 location.href = Response.redirect_url;
                 return;
            }

            var html = ['<div id="errorMessageBox" class="messageBox"><ul class="messageList">'];
            var Messages = Response.Messages.login;
            for(field in Messages)
            {
                 html[html.length] = '<li>'+Messages[field]+'</li>';
            }
            html[html.length] = '</ul></div>';

            $('#dlgLoginResponse').html(html.join(''));
            $('#singin input').removeAttr('disabled');
       }
 });
}

function open_login_window()
{
     $('#dlg-login').dialog('open').css({width:'auto', height:'auto'});
}
   
$(function(){
       var $Fields = $('#singin input');
       var $dlgLoginResponse = $('#dlgLoginResponse');

       var $loader = $('#loader');
       jQuery().ajaxStart(function() {
            $dlgLoginResponse.empty();
            $Fields.attr('disabled', 'true');
            $loader.show();
       }).ajaxStop(function() {
            $loader.hide();
       }).ajaxError(function(a, b, e) {
            $Fields.removeAttr('disabled');
            throw e;
       });

     var $dlgLoginValidate = $("#singin").validate({
       submitHandler: login_submit_handler
     });
     $("#dlg-login").dialog({
          autoOpen:false,
          bgiframe:true,
          modal:true,
          resizable:false,
          height: 230
     });

     var $uiDialog = $('.ui-dialog');
     $uiDialog.css({width:'auto', height:'auto'});

     if (jQuery.fn.example)
     {
       $("#login").example('Email address');
       //$("#pass").example('Password');
     }

     $("#login_form").validate();
     $("#forgot").validate();
});
