/**
*
*  AJAX IFRAME METHOD (AIM)
*  http://www.webtoolkit.info/
*
**/

AIM = {

    frame : function(c) {

        var n = 'f' + Math.floor(Math.random() * 99999);
        var d = document.createElement('div');
        d.innerHTML = '<iframe style="display:none;" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
        //d.innerHTML = '<iframe style="background: #fff; left: 0; top: 0; width: 800px; height: 300px; position: absolute" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
        document.body.appendChild(d);

        var i = document.getElementById(n);
        if (c && typeof(c.onComplete) == 'function') {
            i.onComplete = c.onComplete;
        }

        return n;
    },

    form : function(f, name) {
        f.setAttribute('target', name);
    },

    submit : function(f, c) {
        AIM.form(f, AIM.frame(c));
        if (c && typeof(c.onStart) == 'function') {
            return c.onStart();
        } else {
            return true;
        }
    },

    loaded : function(id) {
        var i = document.getElementById(id);
        if (i.contentDocument) {
            var d = i.contentDocument;
        } else if (i.contentWindow) {
            var d = i.contentWindow.document;
        } else {
            var d = window.frames[id].document;
        }
        if (d.location.href == "about:blank") {
            return;
        }

        if (typeof(i.onComplete) == 'function') {
            i.onComplete(d.body.innerHTML);
        }
    }

}

startCallback = function () {
    $("<div id='loader'><img align='absmiddle' src='template/images/loading.gif'> Report Generating...</div>").appendTo("#loadingbar").hide();
    $("#loader").fadeIn(500);
    return true;
}

completeCallback = function ( response ) {
    $("#loader").fadeOut(500);
    $("#loader").remove();
    if ( response.substring ( 0, 5) == "Error" ) {
        //$("#worktext").slideDown(1000);
        alert ( response.substring ( 6, response.length ) );
        return false;
    }
    $("<div id='resulttext' style='margin: 0 auto; text-align:center;'>Generated Report</div>").insertBefore("#result").hide();
    $("#epnresult").html(response);
    $("#epnresult").fadeIn(500);
    return true;
}
