function TryLogin() {

  if (!$('userid').value) {

    $('flashlogin').className = 'flash_alert';
    $('flashlogin').set('html', 'Please enter your username.');

    $('userid').focus();
    $('userid').highlight();

  } else if (!$('passwd').value) {

    $('flashlogin').className = 'flash_alert';
    $('flashlogin').set('html', 'Please enter your password.');

    $('passwd').focus();
    $('passwd').highlight();

  } else {

    $('flashlogin').innerHTML = '';
    $('flashlogin').className = 'flash_hide';

/* implement the Challenge Handshaking Authentication Protocol */

    var chap = hex_md5(hex_md5($('passwd').value) + $('challenge').value);

  	var myXHR = new Request( {url: 'php/controllers/controller_user.php?action=login', onSuccess:function(rsp){

  	  var rc = JSON.decode(rsp);


      if (rc['success'] == true) {

        if($('rememberme').checked) {

// save username and password in a cookie

          var cookie = Cookie.write('hlusername', $('userid').value, {duration:365});
          var cookie = Cookie.write('hlpassword', $('passwd').value, {duration:365});

        } else {

          Cookie.dispose('hlusername');
          Cookie.dispose('hlpassword');
        }

        window.location='/';

      } else {

        $('flashlogin').className = 'flash_alert';
        $('flashlogin').set('html', rc['msg']);
      }

  	}}).send('userid='+encodeURIComponent($('userid').value)+'&chap='+chap+'&crypt='+hex_md5($('passwd').value));

  }

}


function RememberMe()
{
  var username = Cookie.read('hlusername');
  var password = Cookie.read('hlpassword');

  if (username && password) {

    document.getElementById('userid').value = username;
    document.getElementById('passwd').value = password;
    document.getElementById('rememberme').checked = true;
  }
}

function ShowForgotMyPassword()
{
  var collapsible = new Fx.Slide($('forgot_password'), {
          duration: 500,
          transition: Fx.Transitions.linear
  });

  collapsible.toggle();
}


function SendForgotMyPassword()
{

  if ($('email').value) {

    $('sendProgress').style.display='inline';

  	var myXHR = new Request( {url: 'php/controllers/controller_user.php?action=sendforgotpassword', onSuccess:function(rsp){

  	  $('sendProgress').style.display='none';
  	  var rc = JSON.decode(rsp);

      if (rc['success']) {

        var collapsible = new Fx.Slide($('forgot_password'), { duration: 500, transition: Fx.Transitions.linear });

        collapsible.toggle();

        $('flashlogin').className = 'flash_notice';
        $('flashlogin').innerHTML = 'An email has been sent to <b>' + $('email').value + '</b> with your login information.';

      } else {

        $('flashlogin').className = 'flash_alert';
        $('flashlogin').innerHTML = rc['message'];
      }

  	}}).send('email='+$('email').value);

  } else {

    $('flashlogin').className = 'flash_alert';
    $('flashlogin').innerHTML = 'Please enter your email address.';
  }
}

// capture the 'enter' key and submit the form
function CheckForEnter(e)
{
  var key;

  if(window.event)
    key = window.event.keyCode;     //IE
  else
    key = e.which;     //firefox

  if(key == 13) {

    TryLogin();

  }
}


window.addEvent('domready', function() {

  // Set slide and styles effects
  var mySlide = new Fx.Slide('forgot_password').hide();
  $('forgot_password').style.visibility = 'visible';
  RememberMe();
});