// init begin state 
function bb_init(div_name, debug_val) {
    bb_target_div = div_name;
    bb_debug = debug_val;
    bb_loadframe();
// set the time of checking page/ state 
// bb_check_state() will be called a time of each one second 
    window.setInterval('bb_check_state()', 1000);
// init save state
    bb_save_state();
}
//----------------------------------------------------------------

//Run from the interval timer (once a second),
//this function reads a cache index value
//stored in the DIV element of the child IFRAME.
//
//If this extracted cache index differs from the
//current cache index, then the back button was
//pressed. In this case, we pull the corresponding
//data from the cache and update the page.
function bb_check_state() {
 
    if (bb_iframe_loaded == false) {
        return;
    }

    var doc =  window.frames['bbFrame1'].document;
    var new_idx = doc.getElementById('divFrameCount').value;

    if (new_idx != bb_curr_idx) {

        //Pull a previous state from the cache (if it exists).

        if (bb_cache[new_idx]) {

            var divBody = document.getElementById("divBody");
            divBody.innerHTML = bb_cache[new_idx];           
        }
        bb_curr_idx = new_idx;       
    }
}

//-------------------------------------------------------------
// When requested, save the current state in a cache.
function bb_save_state() {
// get content which was changed by ajax 
    var div_to_cache = document.getElementById(bb_target_div);
// increasing the number of action of ajax 
    bb_count++;   
 
   // Store the new contents in the cache.
    bb_cache[bb_count] = div_to_cache.innerHTML;
    // Load new page into iframe.
    bb_loadframe();
    // set current state 
    bb_curr_idx = bb_count;
}

//-------------------------------------------------------------
// Update the hidden IFRAME.
function bb_loadframe() {
// get iframe 
    var bbFrame1 = document.getElementById("bbFrame1");
    bb_iframe_loaded = false;
// set source file to iframe 
    bbFrame1.src = bb_iframe_script + "?id=" + bb_count;
}
// Called by child IFRAME
function bb_done_loading() {
    bb_iframe_loaded = true;
}