/*
ZebraFeeds - copyright (c) 2006 Laurent Cazalet
http://www.cazalet.org/zebrafeeds
client side functions - ajax stuff, included only if the template
has {dynamiclength} in "template header" area
*/

function getHTTPObject() {
    var xmlhttp;

    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }


    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    return xmlhttp;
}

function requestContent(requestparams) {
    scripturl = ZFURL + "/zebrafeeds.php";

    http.onload = null;
    http.open("GET", scripturl + '?' + requestparams, true);
    //alert(scripturl + '?' + requestparams);
    //xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");

    http.onreadystatechange = handleResponse;
    http.send(null);
}

/* when data returning from server arrives
structured this way
<OBJECT ID>|,|,|<content>
*/

function handleResponse() {
    if (http.readyState == 4) { // Complete

        if (http.status == 200) { // OK response

            /* split according to our separator */
            results = http.responseText.split("|,|,|");

            //alert(results[0]);
            //alert('ZFCONTENT' + results[0]);
            element = document.getElementById(results[0]);
            if (element != null) {
                document.getElementById(results[0]).innerHTML = results[1];
            } else {
                alert('Problem with response: ' + http.responseText);
            }


        }
    }
}


/* all in one
itemid is the zfeeder news item id, not the html element id
*/
function showItem(feedurl, itemid) {

    if(document.getElementById('ZFCONTENT'+ itemid))  {
        var element = document.getElementById('ZFCONTENT'+ itemid);

        // if the element is empty, load from server
        if ( element.innerHTML == "" ) {
            requestparams = "type=item&xmlurl=" + escape(feedurl) + "&itemid=" + itemid;
            requestContent(requestparams);
            element.innerHTML = '...<br/>';
        }

        toggleVisible(element, 'none');
    }
}


function getAllItems(feedurl,refreshtime) {
     requestparams = "type=channelallitems&xmlurl=" + escape(feedurl) + "&refreshtime="+refreshtime;
     requestContent(requestparams);
}

function refreshChannel(feedurl,showeditems,refreshtime) {
     requestparams = "type=channelforcerefresh&xmlurl=" + escape(feedurl) + "&maxitems="+showeditems+ "&refreshtime="+refreshtime;
     requestContent(requestparams);

}


var http = getHTTPObject();

// another global var, ZFURL will be defined on the fly


