var net = new Object();
net.READY_STATE_UNINITIALIZED = 0;
net.READY_STATE_LOADING = 1;
net.READY_STATE_LOADED = 2;
net.READY_STATE_INTERACTIVE = 3;
net.READY_STATE_COMPLETE = 4;

//Constructor
net.ContentLoader = function (url,onload,onerror,method,params,contentType,extraParams,container,statusContainer,returnType,sync){
    this.onload = onload;
    this.onerror = (onerror)? onerror : this.defaultError;
    this.defaultError;
    this.data;
    this.status;
    this.extraParams = extraParams;
    this.container = container;
    this.statusContainer = statusContainer;
    this.returnType = (returnType=='XML')? 'XML' : 'TEXT';
    this.async = (!sync || sync == null)? true : false;
    this.error = true;
    this.loadXMLDoc(url,method,params,contentType);
}
net.ContentLoader.prototype.loadXMLDoc = function(url,method,params,contentType){
    if (!method){
        method = 'GET';
    }
    if (!contentType && method=='POST'){
        contentType = 'application/x-www-form-urlencoded';
    }
    if(window.XMLHttpRequest){
        this.req = new XMLHttpRequest();
    }else if(window.ActiveXObject){
        this.req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    if (this.req){
        try{
            var loader = this;
            if(this.async){
                this.req.onreadystatechange = function(){
                    loader.onReadyState.call(loader);
                }
            }

            this.req.open(method,url,this.async);
            
            if(contentType){
                this.req.setRequestHeader("Content-Type",contentType);
            }
            if(!document.all){
                this.req.overrideMimeType('text/html; charset=ISO-8859-1');
            }
            this.req.send(params);
            if(!this.async){
                loader.onReadyState.call(loader);
            }
        }catch (err){
            this.onerror.call(this);
        }
    }
}
net.ContentLoader.prototype.onReadyState = function(){
    var ready=this.req.readyState;
    if(ready == net.READY_STATE_COMPLETE){
        var httpStatus = this.req.status;
        if (httpStatus==200 || httpStatus==0){
            if (this.returnType=='TEXT'){
                this.data = this.req.responseText;
            }else{
                this.data = this.req.responseXML;
            }
            this.error = false;
            if(this.async){
                this.onload.call(this);
            }
        }else{
            this.onerror.call(this);
        }
    }else{
        if(!this.statusContainer){
            this.data = 'Loading...';
        }else{
            this.status = 'Loading...';
        }
        this.onload.call(this);
    }
}
net.ContentLoader.prototype.defaultError = function(){
//    this.data = 'Error loading data!';
    alert('Error loading data!');
}
net.ContentLoader.prototype.decode = function(utftext){
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }
        return string;  
}
net.ContentLoader.prototype.encode = function(string){
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }
        return utftext; 
}

/*function getResponse(){
    var oDiv = document.getElementById('alt');
    if (oDiv){
        oDiv.innerHTML = this.data;
    }
}*/
function getResponse(){
    var oDiv = document.getElementById(this.container);
    var oDivS = document.getElementById(this.statusContainer);
    if(oDivS){
        if(!document.all){
            oDivS.style.top = oDiv.offsetTop+285+'px';
        }else{
            oDivS.style.top = oDiv.offsetTop+515+'px';
        }
        oDivS.style.left = oDiv.offsetLeft+400+'px';
        oDivS.innerHTML = this.status;
    }
    if (oDiv && this.data && this.async){
        oDiv.innerHTML = this.data;
        oDivS.innerHTML = '';
    }
}

/*function showAlt(id,e){
    var posx = 0;
    var posy = 0;    
    if (document.all){
        posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
        posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
    }else{
        posx = e.pageX;
        posy = e.pageY;
    }
    var oDiv = document.createElement('DIV');
    oDiv.id='alt';
    oDiv.style.top = posy+20+'px';
    oDiv.style.left = posx+10+'px';
    document.body.appendChild(oDiv);
    var loader = new net.ContentLoader('index.php?action=getAlt&id='+id,getResponse,null,'GET',null);
}*/
function hideAlt(){
    var oDiv = document.getElementById('alt');
    document.body.removeChild(oDiv);
}
