/*************************************************************

Ezboxx.Anthem Project

Developed By
- Ezboxx Development team
 
Based on Work done by:
- Jason Diamond (Original Anthem Author)
- Mootools (Javascript Libraries)
 
Special Thanks to
- The Anthem.net community from source forge and Anthemdotnet.com
- Samuel Birch (http://www.phatfusion.net)
 
This work is licensed under a Creative Commons Attribution 3.0 License
http://creativecommons.org/licenses/by/3.0/
 
for more information please visit http://ezboxx.com

Version:	1.1
Prereq:		utils.js

*************************************************************/

var EzAnthem = {};

window.addEvent('domready', function() {
    EzAnthem = new EzAnthem_Controller();

    $$('a').each(function(el, i) {
        if (el.get('onclick') != null) {
            el.set('href', 'javascript:;');
        }
    });

});

/*
Syntax:
EzAnthem.request(this [, options]);
	
Arguments:
1. this - a reference to the control
2. options - object (optional)
	
Options:
url: string, the url of the processing page. (default: the form action)
method: string, (default: POST)
encoding: string, (default: utf-8)
headers: object, (default: {'Content-Type':'application/x-www-form-urlencoded', 'Accept-Encoding':'gzip, deflate'})
ids: array, id(s) of the elements or container whose values are to sent to the processing page.

loader: string, id of the element that is to have the loader. (default: false)

eventTarget: string, id of a control. (default: the initiating control)
eventArgument: string, (default: '')
updateControl: object, {
id: string, the id of the control that needs to be updated as the request starts, 
cssClass:  string, the classname that is to be applied to the control,
text: string, to be used on the control while the request is processing,
src: string, the location of an image to be used while the reqeust is processing (only if the control already accepts the src attribute),
disabled: boolean, if the control is to be disabled while the request is processing
},
params: string or object, querystring formated string or object that will be passed with the request.
invokeMethod: string,
partialViewstate: array, ids of the controls that will have their viewstate passed.
	
Events:
OnInit: function, called before data is collected.
OnLoad: function, called after data collected and before request is sent.
OnSendData: function, called when the request is sent.
OnReceiveData: function, called when the data is received.
OnRender: function, called when all dom updates are complete.
OnEnd: function, called when the entire process is complete.
OnError: function, called when an error occurs.
OnCancel: function, called when the request is cancelled. (triggered by the OnInit event returning false)
		
Returns:
the server will return a json object in the following format. (all first level nodes are optional)
	
{
//update: array of objects that reference elements that are to be updated
update: [
{
id: 'myElement', //id of element
type: 'replace', //type of update, replace - replaces the element with this content, top and bottom - are where to inject the content into the element
content: 'my html content', //html content
viewstate: 'viewstate value for this control',
iframe: '' //id of an iframe, the content will be updated in this iframe instead.
}
],
	
//insert: array of objects that are used to create new elements
insert: [
{
id: 'newElementID', //id of the new element
refId: 'myReferenceID', //id of an element used as a reference point for injecting
type: 'after', //type of update, after - inject after refElement, before - inject before refElement
content: 'my html content' //html content
}
],
	
//scripts: array of scripts to be executed
scripts: [
{
updatePosition : 'AfterControls',
script : 'script to be executed'
}
],
	
//scriptBlocks: array of scripts to be injected into the page
scriptBlocks: [
'function myFunc(){alert("something");}'
],
	
//scriptTags: array of urls for script files to be injected into the page
scriptTags: [
'/js/myscript1.js',
'/js/myscript2.js'
],
	
//states: object containing key/value pairs relating to the .net states to be updated
states: {
'__VIEWSTATE': 'value',
'__VIEWSTATEENCRYPTED': 'value'
}
}
	
	
Example:
<input type="submit" name="button1" value="Click Me!" id="button1" onclick="return EzAnthem.request(this);" />

*/
/*************************************************************
Class:		EzAnthem_Controller
Desc:		This is the containing class, only needs to be initialised once.
Version:	1.0
Status:		Dev
*************************************************************/
var EzAnthem_Controller = new Class({

    initialize: function() {
        this.inProgress = 0;
        this.form = $(document.body).getElement('form');
        this.form.addEvent('submit', function(e) {
            //new Event(e).stop();
        } .bind(this));
    },

    getOptions: function() {
        return {
            url: this.form.action || document.URL,
            method: 'POST',
            data: null,
            encoding: 'utf-8',
            headers: { "Content-Type": "application/x-www-form-urlencoded", "Accept-Encoding": "gzip, deflate" },
            update: null,
            ids: null,
            updateControl: { id: null, cssClass: null, text: null, src: null, disable: false },
            params: null,
            invokeMethod: null,
            partialViewstate: [],
            loaderControl: false,
            loaderContent: false,
            OnInit: $lambda(true),
            OnLoad: $empty,
            OnSendData: $empty,
            OnReceiveData: $empty,
            OnRender: $empty,
            OnEnd: $empty,
            OnError: $empty,
            OnCancel: $empty
        };
    },

    //this is the function that should be called
    request: function(el, options) {
        this.setOptions(this.getOptions(), options);

        if (this.options.OnInit() == false) {
            this.options.OnCancel();
            return false;
        }

        // validation
        var validationPassed = true;
        if (this.options.Validate == 'True') {
            if (!this.options.ValidationGroup) {
                this.options.ValidationGroup = '';
            }

            if (window.Page_ClientValidate != undefined) {
                if ($type(Page_ClientValidate) == 'function') {
                    validationPassed = Page_ClientValidate(options.ValidationGroup);
                }
            }
        }
        if (window.WebForm_OnSubmit != undefined) {
            if ($type(WebForm_OnSubmit) == 'function') {
                validationPassed = WebForm_OnSubmit();
            }
        }

        if (!validationPassed) {
            return false;
        }

        this.target = $(el);
        if (!this.target && this.options.eventTarget) {
            this.target = $(this.options.eventTarget);

            if (!this.target) {
                this.target = $(this.options.eventTarget.split(":").join("_"));
            }
        }

        if (this.options.updateControl) {
            if (!this.options.updateControl.id) {
                this.options.updateControl.id = this.target.id;
            }
        }
        //set eventTarget
        if (this.options.eventTarget) {
            this.updateEvent('TARGET', this.options.eventTarget);
        } else {
            this.updateEvent('TARGET', this.target.id);
        }
        //set eventArgument
        if (this.options.eventArgument) {
            this.updateEvent('ARGUMENT', this.options.eventArgument);
        } else {
            this.updateEvent('ARGUMENT', '');
        }

        //clear the event options else they will be carried voer to the next callback
        this.options.eventTarget = '';
        this.options.eventArgument = '';

        if ($type(this.options.params) == 'object') {
            this.options.params = $H(this.options.params).toQueryString();
        }

        //check how many in progress and if the form has any file types
        if (this.inProgress < 2 && !this.checkForFile(this.options.ids)) {
            //use ajax
            this.inProgress++;
            new EzAnthem_ajax(this.options).send(this.getData(this.options, true));
        } else {
            //console.log('using IOFrame')
            //use ioframe
            new EzAnthem_IOFrame(this.checkForFile(this.options.ids), this.options).send(this.getData(this.options));
        }

        return false;
    },

    getData: function(options, encode) {
        var values = new Hash();
        if (options.ids) {
            //send an item only...
            options.ids.each(function(id) {
                //get value if is a form field...
                var item = $(id);
                var tag = item.get('tag');
                if (tag == 'input' || tag == 'textarea' || tag == 'select') {
                    //values.set(item.name, item.value || item.get('text'));
                    this.setValue(values, el);
                } else {
                    //get values from a div...
                    this.getFormElements(item).each(function(el) {
                        //values.set(el.name, el.value || el.get('text'));
                        this.setValue(values, el);
                    });
                }
            } .bind(this));

        } else if (options.url != this.form.action) {
            //send without viewstate...
            values = this.getFormData();
            values.erase('__VIEWSTATE');

        } else {
            //send the form...
            values = this.getFormData();
        }
        if (options.invokeMethod) {
            values.set('invokeMethod', options.invokeMethod);
        }

        //if the target is an input of type submit then add it as a param
        if (this.target) {
            if (typeof this.target.get == 'function') {
                if (this.target.get('tag') == 'input') {
                    if (this.target.get('type') == 'submit' || this.target.get('type') == 'button')
                        values.set(this.target.name, '');
                }
            }
        }

        //get partialViewstate
        if (options.partialViewstate.length > 0) {
            var states = this.getAnthemViewstates();
            var viewValues = [];
            states.each(function(el) {
                if (options.partialViewstate.contains(el.id)) {
                    viewValues.push(el);
                }
            });
            values.set('Anthem_Viewstate', this.parseAnthemViewstates(viewValues));
        }

        values.set('Anthem_CallbackKey', 'true');

        if (encode) {
            return values.toQueryString();
        } else {
            return values;
        }

    },

    getFormData: function() {
        var values = new Hash();
        for (var i = 0; i < EzAnthem.form.length; i++) {
            var el = EzAnthem.form[i];
            this.setValue(values, el);
        }
        return values;
    },

    setValue: function(hash, el) {
        if (el.type != 'submit' && el.type != 'button' && !el.disabled) {
            if (el.type == 'checkbox' && el.checked || el.type == 'radio' && el.checked) {
                hash.set(el.name, el.value);
            } else if (el.type != 'checkbox' && el.type != 'radio') {

                var prefix = '';
                if (Browser.Engine.trident) {
                    prefix = ' ';
                }
                var value = prefix + el.value;
                if (value.substring(0, 1) == ' ') {
                    value = value.substring(1, value.length);
                }

                if (el.type == "select-multiple") {
                    value = '';
                    el.getElements('option').each(function(opt) {
                        if (opt.selected) {
                            value += opt.value + ',';
                        }
                    });
                    value = value.substring(0, value.length - 1);
                }

                if (el.type != "textarea" && el.type != "text") {
                    if (value == '') {
                        value = null;
                    }
                }

                if (value != null) {
                    //console.log(el.name+' : '+value);
                    hash.set(el.name, value);
                }
            }
        }
    },

    checkForFile: function(ids) {
        var isFile = false;
        //var els = $$(EzAnthem.form.elements);

        var els = [];
        for (var i = 0; i < EzAnthem.form.length; i++) {
            els.push(EzAnthem.form[i]);
        }

        if (ids) {
            els = [];
            ids.each(function(id) {
                var item = $(id);
                var tag = item.get('tag');
                if (tag == 'input' || tag == 'textarea' || tag == 'select') {
                    els.push(item);
                } else {
                    els.combine(this.getFormElements(item));
                }
            } .bind(this))
        }
        els.each(function(el) {
            if (el.type == 'file') {
                isFile = true;
            }
        });
        return isFile;
    },

    getFormElements: function(el) {
        var els = [];
        els.extend(el.getElements('input'));
        els.extend(el.getElements('textarea'));
        els.extend(el.getElements('select'));
        return els;
    },

    updateControl: function(control, options) {
        control.type = control.element.get('type') ? 'value' : 'text';
        if (control.type == 'value') {
            if (control.element.get('type') == 'image') {
                control.type = 'src';
            }
        }
        control.defaultValue = control.element.get('value') || control.element.get('text') || control.element.get('src');
        if (options.text) {
            control.element.set(control.type, options.text);
        }
        if (options.src) {
            control.element.set('src', options.src);
        }
        if (options.cssClass) {
            control.element.addClass(options.cssClass);
        }
        if (options.disable) {
            if (control.element.get('type')) {
                //disable input
                control.element.set('disabled', true);
            } else {
                //disable link
                control.event = control.element.onclick;
                control.element.onclick = function() {
                    return false;
                }
            }
        }
    },

    completed: function(obj, data, ajax) {
        obj.options.OnReceiveData();
        if (ajax) {
            this.inProgress--;
        }
        //
        var el, tag
        //convert response string into json...
        var json = JSON.decode(data);

        // check if there are any validtors and/or validator summaries
        // if yes then reset them to emtpy arrays.
        // new updated arrays will be injected in via the scripts about to execute if applicable
        if (window.Page_Validators != undefined) {
            if ($type(Page_Validators)) {
                Page_Validators = new Array();

                // remove any existing validator sets
                $(document.body).getElements('script').each(function(el) {
                    var scriptBody = el.get('html');
                    if (scriptBody.contains('.evaluationfunction = ') ||
					scriptBody.contains('Page_Validators =  new Array') ||
					scriptBody.contains('var Page_ValidationActive = false;') ||
					scriptBody.contains('function WebForm_OnSubmit()'))
                        el.dispose();
                });
            }
        }

        if (window.Page_ValidationSummaries != undefined) {
            if ($type(Page_ValidationSummaries)) {
                Page_ValidationSummaries = new Array();
            }
        }

        //add script tags...
        if (json.scriptTags) {
            var tags = $(document.head).getElements('script').get('src');
            json.scriptTags.each(function(url) {
                if (!tags.contains(url)) {
                    new Asset.javascript(url);
                }
            });
        }

        //add script blocks
        if (json.scriptBlocks) {
            var exScripts = $(document.body).getElements('script').get('html');
            json.scriptBlocks.each(function(el) {
                if (!exScripts.contains(el)) {
                    var s = new Element('script', {
                        'type': 'text/javascript'//,
                        //'text': el
                    }).inject(document.body);
                    //s.set('text', el);
                    s.innerTEXT = el;
                }
            });
        }

        //if a script needs to be run...
        if (json.scripts) {
            json.scripts.each(function(el) {
                if (el.updatePosition == 'BeforeControls') {
                    if ($type(el.script) == 'function') {
                        el.script();
                    } else {
                        $exec(el.script);
                    }
                }
            });
        }

        //if inserting is needed...
        if (json.insert) {
            json.insert.each(function(data) {
                data.content = data.content.replace("\'", "'");
                var cont = new Element('div').set('html', data.content).inject(data.refId, data.type);
                cont.getChildren().each(function(el) {
                    el.inject(cont, 'before');
                });
                cont.dispose();
            });

            if (obj.loader) {
                //obj.loader.update($(obj.options.loader));
            }
        }

        //if updating is needed...
        if (json.update) {
            var tryAgain = this.updateContent(json.update);

            if (tryAgain.length > 0) {
                this.updateContent(tryAgain);
            }

            if (obj.loader) {
                //obj.loader.update($(obj.options.loader));
            }
        }

        //reset the control text
        if (obj.control) {
            if (obj.options.updateControl.text) {
                obj.control.element.set('text', obj.control.defaultValue);
            } else if (obj.options.updateControl.src) {
                obj.control.element.set('src', obj.control.defaultValue);
            }

            if (obj.options.updateControl.cssClass) {
                obj.control.element.removeClass(obj.options.updateControl.cssClass);
            }

            if (obj.options.updateControl.disable) {
                if (obj.control.element.get('type')) {
                    //enable input
                    obj.control.element.set('disabled', false);
                } else {
                    //enable link
                    obj.control.element.onclick = obj.control.event;
                }
            }

            if (obj.loader) {
                //obj.loader.update($(obj.options.loader));
            }
        }

        //update element if passed in as option...
        if (obj.options.update) {
            el = $(obj.options.update);
            tag = el.get('tag');
            if (tag == 'input') {
                el.set('value', json.content);
            } else if (tag == 'textarea') {
                el.set('text', json.content);
            } else {
                el.set('html', json.content);
            }

            if (obj.loader) {
                //obj.loader.update($(obj.options.loader));
            }
        }

        //update hidden fields...
        if (json.states) {
            for (el in json.states) {
                if ($(el)) {
                    $(el).set('value', json.states[el]);
                }
            }
        }

        //if a script needs to be run...
        if (json.scripts) {
            json.scripts.each(function(el) {
                if (el.updatePosition == 'AfterControls') {
                    if ($type(el.script) == 'function') {
                        el.script();
                    } else {
                        $exec(el.script);
                    }
                }
            });
        }

        // clear the form onsubmit event.. if validators then inject the validators submit event - WebForm_OnSubmit 
        EzAnthem.form.removeEvents('submit');
        if (window.Page_Validators != undefined) {
            if ($type(Page_Validators) == 'array') {
                if (Page_Validators.length > 0) {
                    EzAnthem.form.addEvent('submit', function(e) {
                        return WebForm_OnSubmit();
                    });
                }
            }
        }

        //hide the loader if there is one
        if (obj.loaderID) {
            //obj.loader.hide($(obj.options.loader));
            layers.hide(obj.loaderID);
        }

        //call onsuccess function...
        obj.options.OnRender();

        //call end function...
        obj.options.OnEnd();
    },

    updateContent: function(json) {
        var tryAgain = [];
        json.each(function(data) {
            //var el = $(data.id);
            var doc = $(document);
            var access = true;
            if (data.iframe) {
                try {
                    var frame = new IFrame(data.iframe);
                    doc = frame.contentWindow.document || frame.contentDocument;
                } catch (error) {
                    //console.log(error);
                    access = false;
                    alert(error);
                }
            }
            if (access) {
                var el = doc.getElement('#' + data.id);
                data.content = data.content.replace("\'", "'");
                if (el) {
                    if (!data.type) { data.type = 'replace'; }
                    if (data.type == 'replace') {
                        var cont = new Element('div').set('html', data.content).replaces(el);
                        cont.getChildren().each(function(el) {
                            el.inject(cont, 'before');
                        });
                        cont.dispose();
                    } else if (data.type != 'viewstateonly') {
                        tag = el.get('tag');
                        if (tag == 'input') {
                            if (data.type == 'top') {
                                el.set('value', data.content + el.get('value'));
                            } else
                                if (data.type == 'bottom') {
                                el.set('value', el.get('value') + data.content);
                            } else {
                                el.set('value', data.content);
                            }
                        } else
                            if (tag == 'textarea') {
                            if (data.type == 'top') {
                                el.set('text', data.content + el.get('text'));
                            } else
                                if (data.type == 'bottom') {
                                el.set('text', el.get('text') + data.content);
                            } else {
                                el.set('text', data.content);
                            }
                        } else {
                            if (data.type == 'top') {
                                el.set('html', data.content + el.get('html'));
                            } else
                                if (data.type == 'bottom') {
                                el.set('html', el.get('html') + data.content);
                            } else {
                                el.set('html', data.content);
                            }
                        }
                    }
                    if (data.viewstate) {
                        this.updateViewstate(data.id, data.viewstate, doc);
                    }
                } else {
                    tryAgain.push(data);
                }
            }
        } .bind(this));
        return tryAgain;
    },

    updateViewstate: function(id, value, doc) {
        var states = this.getAnthemViewstates(doc);
        var values = [];
        var add = true;

        for (var i = 0; i < states.length; i++) {
            if (states[i].id == id) {
                states[i].value = value;
                add = false;
            }
            values.push('__Start$' + states[i].id + '__' + states[i].value + '__$End__');
        }
        if (add) {
            values.push('__Start$' + id + '__' + value + '__$End__');
        }
        doc.getElement('#Anthem_Viewstate').set('value', values.join(''));
    },

    getAnthemViewstates: function(doc) {
        if (doc == undefined) {
            doc = $(document);
        }
        var states = [];
        var el = doc.getElement('#Anthem_Viewstate');
        var state = el.get('value');
        var values = state.split('__$End__');
        for (var i = 0; i < values.length - 1; i++) {
            values[i] = values[i].replace('__Start$', '');
            var a = values[i].split('__');
            states.push({ id: a[0], value: a[1] });
        }
        return states;
    },

    parseAnthemViewstates: function(values, doc) {
        var state = '';
        for (var i = 0; i < values.length; i++) {
            state += '__Start$' + values[i].id + '__' + values[i].value + '__$End__';
        }
        return state;
    },

    updateEvent: function(type, value) {
        if ($('__EVENT' + type)) {
            $('__EVENT' + type).set('value', value);
        } else {
            new Element('input').set({
                'type': 'hidden',
                'name': '__EVENT' + type,
                'id': '__EVENT' + type,
                'value': value
            }).inject(this.form);
        }
    }
});

EzAnthem_Controller.implement(new Options, new Events);


/*************************************************************
Class:		EzAnthem_ajax
Desc:		creates an ajax call
Version:	1.0
Status:		Dev
*************************************************************/
var EzAnthem_ajax = new Class({

    getOptions: function() {
        return {

    };
},

initialize: function(options) {
    this.setOptions(this.getOptions(), options);
    this.options.OnLoad();
    obj = this;
    if (this.options.updateControl.id) {
        this.control = {};
        this.control.element = $(this.options.updateControl.id);
    }
},

send: function(data) {
    //sets up and sends ajax request

    this.request = new Request({
        url: this.options.url,
        method: this.options.method,
        encoding: this.options.encoding,
        headers: this.options.headers,
        onSuccess: this.success,
        onFailure: this.failure,
        onException: this.exception,
        onCancel: this.options.OnCancel
    });

    if (this.options.OnSendData() != false) {
        if (this.options.loaderControl) {
            if (this.options.loaderContent) {
                this.loaderID = this.options.loaderContent;
            } else {
                this.loaderID = $time();
            }

            layers.show(this.loaderID, { modalTarget: this.options.loaderControl });
        }
        var params = '';
        if (this.options.params) {
            params = '&' + this.options.params;
        }
        this.request.send(data + params);

        //update control
        if (this.control) {
            EzAnthem.updateControl(this.control, this.options.updateControl);
            if (this.loader) {
                //this.loader.update($(this.options.loader));
            }
        }
    } else {
        EzAnthem.inProgress--;
        this.options.OnCancel();
        if (this.loaderID) {
            //this.loader.hide($(this.options.loader));
            layers.hide(this.loaderID);
        }
    }
},

failure: function() {
    EzAnthem.inProgress--;
    if (obj.loaderID) {
        //obj.loader.hide($(obj.options.loader));
        layers.hide(obj.loaderID);
    }
    obj.options.OnError();
},

exception: function() {
    EzAnthem.inProgress--;
    if (obj.loader) {
        //obj.loader.hide($(obj.options.loader));
    }
    obj.options.OnError();
},

success: function(text, xml) {
    EzAnthem.completed(obj, text, true);
}
});
EzAnthem_ajax.implement(new Options, new Events);


/*************************************************************
Class:		EzAnthem_IOFrame
Desc:		creates an iframe to fake an ajax call
Version:	1.0
Status:		Dev
*************************************************************/
var EzAnthem_IOFrame = new Class({

    getOptions: function() {
        return {
            frameID: 'ioframe_' + new Date().getTime()
        };
    },

    initialize: function(isFile, options) {
        this.setOptions(this.getOptions(), options);
        obj = this;
        this.defaultURL = EzAnthem.form.action;
        this.isFile = isFile;

        var params = '';
        if (this.defaultURL.indexOf('?') > 0) {
            params = '&Anthem_CallbackKey=true&Anthem_IOFrame=true';
        }
        else {
            params = '?Anthem_CallbackKey=true&Anthem_IOFrame=true';
        }

        if (this.options.params) {
            params += '&' + this.options.params;
        }

        this.frame = new IFrame({
            id: this.options.frameID,
            name: this.options.frameID,
            src: 'javascript:void(0);',
            styles: {
                width: 1,
                height: 1,
                border: '0',
                visibility: 'hidden'
            },
            onload: this.success
        }).inject(document.body);

        this.form = new Element('form', {
            'id': this.options.frameID + '_form',
            'action': this.options.url + params,
            'method': this.options.method,
            'target': this.options.frameID,
            'styles': {
                width: 1,
                height: 1,
                overflow: 'hidden',
                visibility: 'hidden'
            }
        }); //.inject(document.body);
        new Element('input', {
            'type': 'submit'
        }).inject(this.form);

        if (isFile) {
            this.form.set('enctype', 'multipart/form-data')
        }

        if (this.options.updateControl.id) {
            this.control = {};
            this.control.element = $(this.options.updateControl.id);
        }
    },

    send: function(data) {

        if (this.options.OnSendData() != false) {

            if (this.options.loaderControl) {
                if (this.options.loaderContent) {
                    this.loaderID = this.options.loaderContent;
                } else {
                    this.loaderID = $time();
                }

                layers.show(this.loaderID, { modalTarget: this.options.loadControl });
            }
            this.options.OnSendData();

            //populate form and send...
            data.each(function(value, key) {
                if ($(key)) {
                    $(key).clone().set('value', value).inject(this.form);
                }
            }, this);

            if (Browser.Engine.trident4) {
                this.form.removeProperty('target');
                this.frame.contentWindow.document.write("<html><body>" + this.form.outerHTML + "</body></html>");
                this.frame.contentWindow.document.forms[0].submit();
            }
            else {
                this.form.inject(document.body);
                this.form.submit();
            }

            //update control
            if (this.control) {
                EzAnthem.updateControl(this.control, this.options.updateControl);
                if (this.loader) {
                    this.loader.update($(this.options.loader));
                }
            }
        } else {
            this.options.OnCancel();
            if (this.loader) {
                this.loader.hide($(this.options.loader));
            }
        }
    },

    success: function() {
        var result = '';
        result = this.document.body.innerHTML;
        result = result.substring(4); // removes starting <!--
        result = result.substr(0, result.lastIndexOf("-->"));

        obj.form.destroy();
        obj.frame.destroy();

        EzAnthem.form.set('action', obj.defaultURL);
        EzAnthem.form.removeProperty('target');

        if (Browser.Engine.trident4) {
            (function() { EzAnthem.completed(obj, result); }).delay(100, obj);
        }
        else {
            EzAnthem.completed(obj, result);
        }
    }
});
EzAnthem_IOFrame.implement(new Options, new Events);
