/**
 * (c) COPYRIGHT AVIATION SOFTWARE INC. 2003-2011
 */



function loadMainPage(url, params, disconnect) {
    if ( disconnect && disconnect != 0 ) {
        if ( top.valid(top.debug) ) { 
            top.debug.log('should disconnect', 'loadMainPage');
            top.debug.log('url: ' + url, 'loadMainPage');
        }
        new Ajax.Request(url, { 
            method: 'post', 
            parameters: params,
            onComplete: function(obj) {
                if ( top.valid(top.debug) ) { 
                    top.debug.log('completed loading main page', 'loadMainPage');
                    top.debug.log('obj: ' + obj, 'loadMainPage');
                }
                top.obj = obj;
            }
        });
    }
    else {
        if ( top.valid(top.debug) ) { 
            top.debug.log('no disconnect', 'loadMainPage');
        }
        try {
            new Ajax.Updater($('main_content'), url, {
                method: 'post',
                parameters: params,
                evalScripts: true,
                onComplete: function(obj) {
                    headeractions(obj);
                    $('load_status').hide();
                    top.checkStatus.stop();
                }
            });
        } catch(error) {
            top.debug.log(error.description);
        }
    }
}


function doStatusUpdates(a, type) {
    /* if ( top.valid(top.debug) ) { top.debug.log('do update using customized updater', doStatusUpdates) }; */
    return new top.Updater(
        'load_status_info',
        1,
        'load_status',
        {
            progress_type: type,
            action: a
        }
    );
}

function populateContentDiv( obj )
{
     if ( top.valid(top.debug) ) { top.debug.log('populating div with object/string now ....', populateContentDiv); }
    top.checkStatus.stop(); if ( top.valid(top.debug) ) {top.debug.log('checkStatus.stop');}
    $('load_status').hide(); if ( top.valid(top.debug) ) {top.debug.log('load_status hidden');}
    var contentDiv = $('main_content'); if ( top.valid(top.debug) ) {top.debug.log('got contentDiv');}
    if (top.valid(top.debug)) { var metric1 = new Date; top.debug.log('metric1 set'); }
    contentDiv = top.replaceHtml(contentDiv, obj); if ( top.valid(top.debug) ) {top.debug.log('replaceHTML complete');}
    if (top.valid(top.debug)) { 
        var metric2 = new Date; top.debug.log('metric2 set');
        var totalMS = metric2 - metric1;
        var totalS = totalMS / 1000;
         if (typeof obj == 'string') {
             var strLen = obj.length;
             var strLenMB = (strLen / 1024 / 1024);
             top.debug.log(strLen + ' chars/bytes (' + strLenMB + 'MB) written in ' +totalMS + 'ms (' + totalS + 's)', populateContentDiv);    
         }
     }
    //$('main_content').update(obj);
//    if ( obj.headerJSON ) {
//        json = obj.headerJSON;
//
//        $('main_content').update(json.html_payload)
//        if ( json.payload ) {
//            headeractions(json);
//        }
//    }
}

top.headeractions = headeractions = function (obj)
{
    var json;
    if ( !obj || ( ! obj.headerJSON && ! obj.payload ) ) {
        return false;
    }
    else {
        if ( obj.headerJSON ) {
            json = obj.headerJSON;
        }
        else if ( obj.payload ) {
            json = payload;
        }
        else {
            return false;
        }
    }

    if (json.action) {
        switch(json.action) {
            case 'location':
                if (json.payload && json.payload.uri && document.location) {
                    document.location=''+json.payload.uri;
                }
                break;
            default:
                break;
        }
    }

}



