function strrpos (haystack, needle, offset) {    
    var i = -1;
    if (offset) {
        i = (haystack+'').slice(offset).lastIndexOf(needle); // strrpos' offset indicates starting point of range till end,
        // while lastIndexOf's optional 2nd argument indicates ending point of range from the beginning
        if (i !== -1) {
            i += offset;
        }
    }
    else {
        i = (haystack+'').lastIndexOf(needle);
    }
    return i >= 0 ? i : false;
}
	
function switch_visible(id) {
	if (document.getElementById(id).style.display=='block')
		document.getElementById(id).style.display='none';
	else
		document.getElementById(id).style.display='block';
}

		function checkDocumentIntegrity()
                {
                        str = "";

                        str += checkElement("reroot","div tag");
                        str += checkElement("addcomment", "div tag");
                        str += checkElement("comment_reply_ID", "hidden form field");
                        str += checkElement("content", "div tag");
                        str += checkElement("comment", "textfield");
                        str += checkElement("addcommentanchor", "anchor tag");

                        if(str != "")
                        {
                                str = "Brian's Threaded Comments are missing some of the elements that are required for it to function correctly.\nThis is probably the because you have changed the original comments.php that was included with the plugin.\n\nThese are the errors:\n" + str;
                                str += "\nYou should compare your comments.php with the original comments.php and make sure the required elements have not been removed.";

                                alert(str);
                        }
                }

		function collapseThread( theId ) {
                        var comment = document.getElementById(theId);                        if(!comment)
                        {
                                alert("ERROR:\nThe document structure is different\nfrom what Threaded Comments expects.\nYou are missing the element '"+theId+"'");
                                return;
                        }
                        var theBody = findBody(comment);

                        if(comment.className.indexOf("collapsed") > -1) {
                                // Ausklappen
                                comment.className = comment.className.replace(" collapsed", "");;
                        } else {
                                // Einklappen
                                comment.className += " collapsed";
                        }
                }

                function expandThread( theId ) {
                        var comment = document.getElementById(theId);
                        if(!comment)
                        {
                                alert("ERROR:\nThe document structure is different\nfrom what Threaded Comments expects.\nYou are missing the element '"+theId+"'");
                                return;
                        }
                        var theBody = findBody(comment);
                        if(comment.className.indexOf("collapsed") > -1) {                                comment.className = comment.className.replace(" collapsed", "");;
                        }
                }

                function findBody(el)
                {
                        var divs = el.getElementsByTagName("div");                        var ret;
                        for(var i = 0; i < divs.length; ++i) {
                                if(divs.item(i).className.indexOf("body") > -1)
                                        return divs.item(i);
                        }
                        return false;
                }


		function moveAddCommentBelow(theId, threadId, collapse)
                {
                        expandThread( theId );
                        var addComment = document.getElementById("addcomment");
                        if(!addComment)
                        {
                                alert("ERROR:\nThreaded Comments can't find the 'addcomment' div.\nThis is probably because you have changed\nthe comments.php file.\nMake sure there is a tag around the form\nthat has the id 'addcomment'");
                                return
                        }
                        var comment = document.getElementById(theId);
                        if(collapse)
                        {
                                for(var i = 0; i < comment.childNodes.length; ++i) {
                                        var c = comment.childNodes.item(i);
                                        if(typeof(c.className) == "string" && c.className.indexOf("collapsed")<0) {
                                                c.className += " collapsed";
                                        }
                                }
                        }
                        addComment.parentNode.removeChild(addComment);

                        comment.appendChild(addComment);
                        if(comment.className.indexOf("alt")>-1) {
                                addComment.className = addComment.className.replace(" alt", "");
                        } else {
                                addComment.className += " alt";
                        }
                        var replyId = document.getElementById("comment_reply_ID");
                        if(replyId == null)
                        {
                                alert("Brians Threaded Comments Error:\nThere is no hidden form field called\n'comment_reply_ID'. This is probably because you\nchanged the comments.php file and forgot\nto include the field. Please take a look\nat the original comments.php and copy the\nform field over.");
                        }
                        replyId.value = threadId;
                        var reRootElement = document.getElementById("reroot");
                        if(reRootElement == null)
                        {
                                alert("Brians Threaded Comments Error:\nThere is no anchor tag called 'reroot' where\nthe comment form starts.\nPlease compare your comments.php to the original\ncomments.php and copy the reroot anchor tag over.");
                        }
                        reRootElement.style.display = "block";
                        var aTags = comment.getElementsByTagName("A");
                        var anc = aTags.item(0).id;
                        //document.location.href = "#"+anc;
                        document.getElementById("comment").focus();
                }


		function onAddComment() {
                        //checkDocumentIntegrity();
                        var el = document.getElementById("commentform");
                        // Future release: Check if form is filled correctly and mark the form fields.
                        el.submit();
                }

		function checkElement(theId, elDesc)
                {
                        var el = document.getElementById(theId);
                        if(!el)
                        {
                                if(elDesc == null)
                                        elDesc = "element";
                                return "- The "+elDesc+" with the ID '" +theId + "' is missing\n";
                        }
                        else
                                return "";
                }

                function reRoot()
                {
                        var addComment = document.getElementById("addcomment");
                        var reRootElement = document.getElementById("reroot");
                        reRootElement.style.display = "none";
                        var content = document.getElementById("content");
                        addComment.parentNode.removeChild(addComment);
                        content.appendChild(addComment);
                        addComment.className = addComment.className.replace(" alt", "");
                        document.location.href = "#addcommentanchor";
                        document.getElementById("comment").focus();
                        document.getElementById("comment_reply_ID").value = "0";
                }

                function changeCommentSize(d)
                {
                        var el = document.getElementById("comment");
                        var height = parseInt(el.style.height);
                        if(!height && el.offsetHeight)
                                height = el.offsetHeight;
                        height += d;
                        if(height < 20)
                                height = 20;
                        el.style.height = height+"px";
                }

function popup(url,param) {
	winContent = window.open(url,'_blank',param)	;
}

function hideReplyForm() {
	document.getElementById('reply_form_bottom').appendChild(document.getElementById('reply_form'));
	document.getElementById('reply_form').style.display='none';
	document.getElementById('reply_name').value = '';
	document.getElementById('reply_email').value = '';
	document.getElementById('reply_comment').value = '';
	document.getElementById('reply_form_error').style.display='none';
}

function moveReplyForm (id) {
	document.getElementById('reply_parent_id').value = id;
	id = "reply_form-"+id;
	var rf = document.getElementById('reply_form');
	document.getElementById(id).appendChild (rf);	
	rf.style.display='block';
}

function switchDisplay(id) {
	if (document.getElementById(id).style.display == 'none') {
		document.getElementById(id).style.display = 'block';
	} else {
		document.getElementById(id).style.display = 'none';
	}
}

function sbOver(text) {
	if (text == '') {
		text = '...';
	} else {
		text=' '+text;
	}
	document.getElementById('sbText').innerHTML=text;
}

function sbDo(was) {
	var sburl=encodeURIComponent(window.location.protocol+'//'+(window.location.hostname+window.location.pathname).replace(/\/:.*?\//g, '/'));
	var sbtitle=encodeURIComponent(document.title);
	switch(was) {		
		case 'tausendreporter': 		
			window.open('http://tausendreporter.stern.de/submit.php?url='+sburl);
			break;
		case 'delicious':
			window.open('http://del.icio.us/post?url='+sburl+'&title='+sbtitle);
			break;
		case 'wong':
			window.open('http://www.mister-wong.de/index.php?action=addurl&bm_url='+sburl+'&bm_description='+sbtitle);
			break;
		case 'blinkList':
			window.open('http://www.blinklist.com/index.php?Action=Blink/addblink.php&Description=&Url='+sburl+'&Title='+sbtitle);
			break;
		case 'yahoo':
			window.open('http://myweb2.search.yahoo.com/myresults/bookmarklet?u='+sburl+'&t='+sbtitle);
			break;
		case 'yigg':
			window.open('http://yigg.de/neu?exturl='+sburl+'&exttitle='+sbtitle);
			break;	
		case 'furl':
			window.open('http://www.furl.net/storeIt.jsp?u='+sburl+'&t='+sbtitle);
			break;
		case 'oneview':
			window.open('http://beta.oneview.de:80/quickadd/neu/addBookmark.jsf?URL='+sburl+'&title='+sbtitle);			
			break;
		case 'folkd':
			window.open('http://www.folkd.com/submit/page/'+sburl);
			break;
		case 'linkarena':	
			window.open('http://linkarena.com/bookmarks/addlink/?url='+sburl+'&title='+sbtitle+'&desc=&tags=');
			break;
		case 'google': 		
			window.open('http://www.google.com/bookmarks/mark?op=add&hl=de&bkmk='+sburl+'&title='+sbtitle);
			break;
		case 'webnews': 		
			window.open('http://www.webnews.de/einstellen?url='+sburl+'&title='+sbtitle);
			break;
		case 'wikio':
			window.open('http://www.wikio.de/vote?url='+sburl);
			break;	
	}
}

// request_get: make a GET-ajax-request

function request_get (url,callback) {
	if (!callInProgress(http)) {
	    http.open('get', url, true);
	    http.onreadystatechange = eval(callback);
	    http.send(null);
	}		
}

// request_get: make a POST-ajax-request

function request_post (url,params,callback) {
    if (!callInProgress(http)) {
    	http.open('POST', url, true);
	    http.onreadystatechange = eval(callback);
	    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	    http.setRequestHeader("Content-length", params.length);
	    http.setRequestHeader("Connection", "close");
	  	http.send(params);
		}
}

// createRequestObject: create xml-HTTP-object

function createRequestObject() {
    var xmlhttp;

    try {

        xmlhttp = new XMLHttpRequest();
    } catch (e) {
        var MSXML_XMLHTTP_PROGIDS = new Array(
          'MSXML2.XMLHTTP.5.0',
          'MSXML2.XMLHTTP.4.0',
          'MSXML2.XMLHTTP.3.0',
          'MSXML2.XMLHTTP',
          'Microsoft.XMLHTTP');
        var success = false;
        for (var i=0;i < MSXML_XMLHTTP_PROGIDS.length && !success; i++) {
            try {
                xmlhttp = new ActiveXObject(MSXML_XMLHTTP_PROGIDS[i]);
                success = true;
            } catch (e) {}
        }
        if (! success) {
            //alert ('No AJAX-Support for this browser :(!');
        }
    }
    return xmlhttp;
}

function callInProgress(xmlhttp) {
    switch ( xmlhttp.readyState ) {
        case 1:
              case 2:
              case 3:
            return true;
        break;
        default:
            return false;
        break;
    }
}

function nil() {
	
}

var http = createRequestObject();
