Thanks for your help.
I changed line 106 to this:
/*if ($returnUrl) {*/
if(strpos($returnUrl, 'ajaxlogin') !== false) {
$link = $returnUrl;
} else {
if ($this->params->get('redirect_previous') == 1) {
$session = JFactory::getSession();
$link = $session->get('redirect_previous_url', $link);
}
}
But I still get "Undefined error".