/* Based on the liveComment plugin, which has this copyright note: */

/*
// +----------------------------------------------------------------------+
// | Copyright (c) 2004 Bitflux GmbH                                      |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the "License");      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an "AS IS" BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: Bitflux GmbH <devel@bitflux.ch>                              |
// +----------------------------------------------------------------------+

*/
var liveCommentReq = false;
var liveCommentLast = "";
var liveCommentIsIE = false;
var lc_t = false;

function lc_addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function liveAttach(tel, myevent, liveCommentKeyPress) {
    if (navigator.userAgent.indexOf("Safari") > 0) {
        tel.addEventListener(myevent, liveCommentKeyPress,false);
        // document.getElementById('searchform').addEventListener("submit", liveCommentSubmit,false);
    } else if (navigator.product == "Gecko") {
        tel.addEventListener(myevent, liveCommentKeyPress,false);
        // document.getElementById('searchform').addEventListener("submit", liveCommentSubmit,false);
    } else {
        tel.attachEvent('on' + myevent,   liveCommentKeyPress);

        // document.getElementById('searchform').attachEvent("onsubmit", liveCommentSubmit);
        liveCommentIsIE = true;
    }
}

function liveCommentInit() {
    if (!document.getElementById('serendipity_replyTo')) {
        return false;
    }

    // on !IE we only have to initialize it once
    if (window.XMLHttpRequest) {
        liveCommentReq = new XMLHttpRequest();
    }

    myevent = 'keyup';
    tel = document.getElementById('serendipity_replyTo');
    pel = document.getElementById('serendipity_preview');
    liveAttach(tel, myevent, liveCommentKeyPress);
    liveAttach(pel, 'click', liveCommentPreview);

    tel.style.color = 'green';
}

function liveCommentPreview(event) {
    if (!liveCommentIsIE) { event.preventDefault(); }
    liveCommentDoSearch();
    return false;
}

function liveCommentKeyPress(event) {
    rid = document.getElementById('serendipity_replyTo').value;

    if (rid == 0) {
        return true;
    }

    sourceel = document.getElementById('serendipityCommentFormC');
    fields = sourceel.cloneNode(true);
    sourceel.id = 'serendipityCommentFormOld';

    liveAttach(fields, 'click', liveCommentPreview);
    iNode  = document.getElementById('serendipity_replyform_' + rid);
    iNode.parentNode.insertBefore(fields, iNode);

    pNode = sourceel.parentNode;
    pNode.removeChild(sourceel);
    return true;
}

function liveCommentStart() {
    if (lc_t) {
        window.clearTimeout(lc_t);
    }
    lc_t = window.setTimeout("liveCommentDoSearch()",200);
}

function liveCommentDoSearch() {
        if (liveCommentReq && liveCommentReq.readyState < 4) {
            liveCommentReq.abort();
        }

        if (window.XMLHttpRequest) {
        // branch for IE/Windows ActiveX version
        } else if (window.ActiveXObject) {
            liveCommentReq = new ActiveXObject("Microsoft.XMLHTTP");
        }

        liveCommentReq.onreadystatechange= liveCommentProcessReqChange;
        liveCommentReq.open("POST", lcbase);
        liveCommentReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=' + lcchar);
        liveCommentReq.send('data=' + document.getElementById('serendipity_commentform_comment').value);
}

function liveCommentProcessReqChange() {

    if (liveCommentReq.readyState == 4) {
        rid = document.getElementById('serendipity_replyTo').value;
        if (rid < 1) {
            rid = 0;
        }

        var  res = document.getElementById("serendipity_replyform_" + rid);
        res.innerHTML = liveCommentReq.responseText;
    }
}

function liveCommentSubmit() {
    var highlight = document.getElementById("LSHighlight");
    if (highlight && highlight.firstChild) {
        document.getElementById('searchform').action = highlight.firstChild.getAttribute("href");
        return false;
    } else {
        return true;
    }
}

lc_addLoadEvent(liveCommentInit);
