var ajax = new Object();

ajax.ContentLoader = function(url, params, validate, error) {
    this.req = null;
    this.url = url;
    this.params = params;
    this.onload = (validate) ? validate : this.genericLoad;
    this.onerror = (error) ? error : this.genericError;
    this.counter = 0;
}

ajax.ContentLoader.prototype = {
    load:function() {
        this.req = (window.ActiveXObject) ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
        if (this.req) {
            try {
                var loader = this;
                this.req.onreadystatechange = function() {
                    loader.onStateChange.call(loader);
                }
       		    var ran_number= getUnique(8);
        	    var firstLetter = (this.url.indexOf('?')<0) ? '?' : '&';
		        this.url = this.url + firstLetter + 'bznetid=' + ran_number;
                if (this.params == null) {
                    this.req.open('GET', this.url, true);
                }
                else {
                    this.req.open('POST', this.url, true);
                    this.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    this.req.setRequestHeader("Content-length", this.params.length);
                    this.req.setRequestHeader("Connection", "close");
                }
                this.req.send(this.params);
            }
            catch (e) {
                this.onerror.call(this);
            }
        }
    },
    onStateChange:function() {
        var state = this.req.readyState;
        if (state == 4) {
            var status = this.req.status;
            if (status == 200) {
                this.counter = 0;
                this.onload.call(this);
            }
            else if (status == 12030) {
                this.counter++;
                if (this.counter <= 5) {
                    this.load();
                }
                else {
                    this.counter = 0;
                    this.onerror.call(this);
                }
            }
            else {
                this.counter = 0;
                alert('Network Error: ' + status);
                this.onerror.call(this);
            }
        }
    },
    genericLoad:function() {
        try {
            ;
        }
        catch (error) {
            alert(error.description);
        }
        finally {
            this.req.abort();
            this.req = null;
        }
    },
    genericError:function() {
        alert('Internal Error');        
    }
}

