var 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>';
        document.body.appendChild(d);

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

        return n;
    },

    form : function(f, name) {
        f.setAttribute('target', name);
        var protectJavaScript = document.createElement("INPUT");
        protectJavaScript.type = "hidden";
        protectJavaScript.name = "protectJavaScript";
        protectJavaScript.value = "true";
        f.appendChild(protectJavaScript);
    },

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

    loaded : function(iframeId) {
        var iframe = document.getElementById(iframeId);
        var doc = null;
        if (iframe.contentDocument) {
            doc = iframe.contentDocument;
        } else if (iframe.contentWindow) {
            doc = iframe.contentWindow.document;
        } else {
            doc = window.frames[iframeId].document;
        }

        if (doc != null && doc.location.href == "about:blank") {
            return;
        }

        if (typeof(iframe.onComplete) == 'function') {
            var response = doc.body.innerHTML;
            var re = new RegExp("<!-- PROTECT_JAVASCRIPT ([\\s\\S]*) -->", "gm");
            response = response.replace(re, "$1");
            iframe.onComplete(response.stripScripts());
            response.evalScripts.bind(response).defer();
        }
    }

}

var PostAjaxForm = {
    submit: function(form, ajaxCallbackDivId) {
        return AIM.submit(form, {'onComplete' : function(response) { $(ajaxCallbackDivId).innerHTML = response;}} );
    }
}

        

