/**
 * Sets up the editing lock
 *
 * @param object $lock
 * @param string $ouwiki
 */
function ouwiki_print_editlock($lock, $ouwiki)
{
    global $DB, $PAGE;
    // Prepare the warning about lock without JS...
    $a = new StdClass();
    $a->now = userdate(time(), get_string('strftimetime'));
    $a->minutes = (int) (OUWIKI_LOCK_NOJS / 60);
    $a->deadline = userdate(time() + $a->minutes * 60, get_string('strftimetime'));
    $nojswarning = get_string('nojswarning', 'ouwiki', $a);
    $nojsstart = '<p class="ouw_nojswarning">';
    // Put in the AJAX for keeping the lock, if on a supported browser
    $ie = check_browser_version('MSIE', 6.0);
    $ff = check_browser_version('Gecko', 20051106);
    $op = check_browser_version('Opera', 9.0);
    $sa = check_browser_version('Safari', 412);
    $ch = check_browser_version('Chrome', 14);
    $js = $ie || $ff || $op || $sa || $ch;
    if ($js) {
        $nojsdisabled = get_string('nojsdisabled', 'ouwiki');
        $nojs = $nojsstart . $nojsdisabled . ' ' . $nojswarning . '<img src="nojslock.php?lockid=' . $lock->id . '" alt=""/></p>';
        $PAGE->requires->yui2_lib(array('yahoo', 'event', 'connection'));
        $strlockcancelled = ouwiki_javascript_escape(get_string('lockcancelled', 'ouwiki'));
        $intervalms = OUWIKI_LOCK_RECONFIRM * 1000;
        $timeoutscript = '';
        if ($ouwiki->timeout) {
            $countdownurgent = ouwiki_javascript_escape(get_string('countdownurgent', 'ouwiki'));
            $timeoutscript = "var ouw_countdownto = (new Date()).getTime()+1000*{$ouwiki->timeout};\n                    var ouw_countdowninterval=setInterval(function() {\n                    var countdown=document.getElementById('ouw_countdown');\n                    var timeleft=ouw_countdownto-(new Date().getTime());\n                    if (timeleft < 0) {\n                        clearInterval(ouw_countdowninterval);\n                        document.forms['mform1'].elements['save'].click();\n                        return;\n                    }\n                    if(timeleft<2*60*1000) {\n                        var urgent=document.getElementById('ouw_countdownurgent');\n                        if(!urgent.firstChild) {\n                            urgent.appendChild(document.createTextNode(\"" . $countdownurgent . "\"));\n                            countdown.style.fontWeight='bold';\n                            countdown.style.color='red';\n                        }\n                    }\n                    var minutes=Math.floor(timeleft/(60*1000));\n                    var seconds=Math.floor(timeleft/1000) - minutes*60;\n                    var text=minutes+':';\n                    if(seconds<10) text+='0';\n                    text+=seconds;\n                    while(countdown.firstChild) {\n                        countdown.removeChild(countdown.firstChild);\n                    }\n                    countdown.appendChild(document.createTextNode(text));\n                },500);\n            ";
        }
        print "<script type='text/javascript'>\n            var intervalID;\n            function handleResponse(o) {\n                if(o.responseText=='cancel') {\n                    document.forms['mform1'].elements['id_preview'].disabled=true;\n                    document.forms['mform1'].elements['id_save'].disabled=true;\n                    clearInterval(intervalID);\n                    alert(\"{$strlockcancelled}\");\n                }\n            }\n            function handleFailure(o) {\n                // Ignore for now\n            }\n            intervalID=setInterval(function() {\n                YAHOO.util.Connect.asyncRequest('POST','confirmlock.php',\n                    {success:handleResponse,failure:handleFailure},'lockid={$lock->id}');\n                },{$intervalms});\n            {$timeoutscript}\n            </script>\n            <noscript>\n            {$nojs}\n            </noscript>\n        ";
    } else {
        // If they have a non-supported browser, update the lock time right now without
        // going through the dodgy image method, to reserve their 15-minute slot.
        // (This means it will work for Lynx, for instance.)
        print $nojsstart . get_string('nojsbrowser', 'ouwiki') . ' ' . $nojswarning . '.</p>';
        $lock->seenat = time() + OUWIKI_LOCK_NOJS;
        $DB->update_record('ouwiki_locks', $lock);
    }
}
if (class_exists('ouflags') && ou_get_is_mobile()) {
    $usehtmleditor = false;
}
// This is a bit evil. What happens is we print the plaintext version of the format (in case
// they have javascript off) then set the content to the original version if they have JS on
// and are using the editor.
print_textarea($usehtmleditor, 30, 100, 0, 0, 'content', ouwiki_xhtml_to_plain($existing));
print '</td></tr></table>
<input type="hidden" id="ouw_format" name="format" value="1" />
<input type="hidden" name="sesskey" value="' . sesskey() . '" />
<input type="submit" id="ouw_save" name="save" value="' . get_string('savechanges') . '" />
<input type="submit" id="ouw_preview" name="preview" value="' . get_string('preview', 'ouwiki') . '" />
<input type="submit" name="cancel" value="' . get_string('cancel') . '" />';
if ($usehtmleditor) {
    print '
<script type="text/javascript">
document.getElementById("edit-content").value="' . ouwiki_javascript_escape($existing) . '";
document.getElementById("ouw_format").value="0";
var button1 = document.getElementById("ouw_save"), button2 = document.getElementById("ouw_preview");
var fn = function() { setTimeout(function() { button1.disabled = true; button2.disabled = true; }, 0); };
button1.onclick = fn;
button2.onclick = fn;
</script>
';
}
print '</form>';
if ($usehtmleditor) {
    use_html_editor();
}
// Footer
ouwiki_print_footer($course, $cm, $subwiki, $pagename);