
// Ajax Class (execScript/exec idea from mootools.net)

function $exec(text){
    if (!text) return text;
    if (window.execScript){
        window.execScript(text);
    } else {
        var script = document.createElement('script');
        script.setAttribute('type', 'text/javascript');
        script.text = text;
        document.getElementsByTagName('head')[0].appendChild(script);
        document.getElementsByTagName('head')[0].removeChild(script);
    }
    return text;
};


function Ajax (url)
{
    this.url         = url;
    this.httpRequest = null;
    this.result      = '';
    
    this.request = function(target, execScript)
    {
        if (window.XMLHttpRequest) {
            this.httpRequest = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            this.httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }

        this.httpRequest.open('GET', this.url, false);
        this.httpRequest.send(null);
        
        if (target) {
            html = this.httpRequest.responseText;
            if (html == 'false') {
                alert('Error: no Result (' + objAjax.url + ')');
            } else {
                if (document.getElementById(target)) {
                    document.getElementById(target).innerHTML = html;
                    
                    if (execScript) {
                        scripts = '';
                        html.replace(/<script[^>]*>([\s\S]*?)<\/script>/gi, function() {
                            scripts += arguments[1] + '\n';
                            return '';
                        });
                        $exec(scripts);
                    }
                } else {
                    alert('Error: no Element (' + target + ')');
                }
            }
        } else {
            return this.httpRequest.responseText;
        }
    }
}
