Proplems with "Load More Posts"

Hello everyone.

Im having some trouble with my javascript. My site uses background refresh to pull new posts from the database in real time. Now i have a whole lot of posts and i tried to create a load more posts button like twitter but a problem occurs.

After a few seconds of browsing a number pops up on my feed and shows the number of posts i have chosen to load in text format. Then all posts get reset back to 90 and i get scrolled back up to the top automatically.

Could you please take a look at this php and javascript code and tell me what the problem is?
PHP Code:
$D->num_results = 0;
$D->start_from = 0;
$D->posts_html = ‘’;

if( $q1!='' && $q2!='' ) {
    $D->num_results    = $db2->fetch_field($q1);
    $D->start_from    = $this->param('start_from') ? intval($this->param('start_from')) : 0;
    $D->start_from    = max($D->start_from, 0);
    $D->start_from    = min($D->start_from, $D->num_results);
    $res    = $db2->query($q2.'LIMIT '.$D->start_from.', '.$C->PAGING_NUM_POSTS);
    $D->posts_number    = 0;
    ob_start();
    while($obj = $db2->fetch_object($res)) {
        $D->p    = new post($obj->type, FALSE, $obj);
        if( $D->p->error ) {
            continue;
        }
        $D->posts_number    ++;
        $D->p->list_index    = $D->posts_number;
        $this->load_template('single_post.php');
    }
    unset($D->p);
    $D->posts_html    = ob_get_contents();
    ob_end_clean();
   
}

if( $this->param('from') == 'ajax' ) {
    echo 'OK:'.$D->posts_number.':';
 $display
    echo $D->posts_html;  
    die();

}

if( $show=='all' || $show=='@me' || $show=='private' || $show=='commented' || $show=='feeds' ) {
    $this->network->reset_dashboard_tabstate($this->user->id, $show);
} 

and my javascript
Code:

function my_scroll_page_to(h) { if( document.body.scrollTop === undefined ) { w.scroll(0, h); return; } var start_scroll = parseInt( typeof(w.pageYOffset)==‘number’ ? w.pageYOffset : document.body.scrollTop, 10); if( isNaN( start_scroll ) ) { w.scroll(0, h); return; } if( start_scroll == h ) { return; } var step = start_scroll>h ? -12 : 12; var breakf = false; var func = function() { start_scroll += step; w.scroll(0, start_scroll); if( start_scroll == h ) { return; } if( step > 0 && start_scroll > h ) { return; } if( step < 0 && start_scroll < h ) { return; } if( breakf ) { return; } setTimeout( func, 1 ); } func(); document.body.addEventListener(“orientationchange”, function(){ breakf = true; }, false); } function load_more_results(div_id, current_results, all_results) { current_results = parseInt(current_results, 10); all_results = parseInt(all_results, 10); var url = w.location.href.toString(); if( ! url ) { return; } if( url.substr(0, siteurl.length) == siteurl ) { url = url.substr(siteurl.length); if( url.indexOf(“#”) != -1 ) { url = url.substr(0, url.indexOf(“#”)); } url = siteurl+url+“/from:ajax/start_from:”+(current_results+1)+“/r:”+Math.round(Math.random()*1000); } else { url = url.replace(/^http(s)?\:\/\//, “”); url = url.substr(url.indexOf(“/”)); if( url.indexOf(“#”) != -1 ) { url = url.substr(0, url.indexOf(“#”)); } url = siteurl+url+“/from:ajax/r:”+Math.round(Math.random()*1000); } var req = ajax_init(false); if( ! req ) { return false; } req.onreadystatechange = function() { if( req.readyState != 4 ) { return; } var txt = trim(req.responseText); var num = txt.match(/^OK\:([0-9]+)\:/g); if( ! num ) { return; } num = num.toString().match(/([0-9]+)/); num = parseInt(num, 10); if( ! num ) { return; } txt = txt.replace(/^OK\:([0-9]+)\:/, “”); txt = trim(txt); var dv = document.createElement(“DIV”); dv.innerHTML = txt; document.getElementById(div_id).appendChild(dv); setTimeout( function() { my_scroll_page_to(dv.offsetTop-30); }, 5); if( current_results+num+1 >= all_results ) { document.getElementById(“loadmore”).style.display = “block”; } document.getElementById(“loadmorelink”).onclick = function() { load_more_results(div_id, current_results+num+1, all_results); }; document.getElementById(“loadmoreloader”).style.display = “none”; } req.open(“GET”, url, true); req.send(“”); document.getElementById(“loadmoreloader”).style.display = “block”; document.getElementById(“loadmorelink”).blur(); }

I would really appreciate any help with this issue as it makes my site look really ugly and we have a lot of members.

Kind Regards

Chan