/**
 * Copyright Notice
 * This file contains proprietary information of Jorge Fraser
 * Copying or reproduction without prior written approval is prohibited.
 * Copyright (c) year
 */

/**
 *
 * Program Name     :   pack.name
 * Type             :   Java File
 *
 *----------------------------------------------------------------------------
 *                        Modification Log
 *----------------------------------------------------------------------------
 *	CVS:	Author		Date		Description
 *----------------------------------------------------------------------------
 *	1.1	Jorge Fraser	today	Initial Release
 *----------------------------------------------------------------------------
 */

var xmlHttpRequestHandler = new Object();
xmlHttpRequestHandler.createXmlHttpRequest = function(){

  var XmlHttpRequestObject;
  if (typeof XMLHttpRequest != "undefined"){
   XmlHttpRequestObject = new XMLHttpRequest();
  }
  else if (window.ActiveXObject){
   // look up the highest possible MSXML version
   var tryPossibleVersions=["MSXML2.XMLHttp.5.0",
                            "MSXML2.XMLHttp.4.0",
                            "MSXML2.XMLHttp.3.0",
                            "MSXML2.XMLHttp",
                            "Microsoft.XMLHttp"];

  for (i=0; i< tryPossibleVersions.length; i++){
   try{
      XmlHttpRequestObject = new ActiveXObject(tryPossibleVersions[i]);
      break;
   }
   catch (xmlHttpRequestObjectError){
      //ignore
   }
  }
 }
 return XmlHttpRequestObject;
}

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;
net.loadMessage = "<br/><br/><b>Loading, please wait...</b>";
net.ContentLoader = function(url, onload, onerror, contentType, div, method) {
    this.url = url;
    this.onload = onload;
    this.contentType = contentType;
    this.div = div;
    this.method = method;
    this.onerror = (onerror) ? onerror : this.defaultError;
    this.loadDoc(url, contentType, method);
}

net.ContentLoader.prototype = {
    loadDoc:function(url, contentType, method) {
        this.req = new xmlHttpRequestHandler.createXmlHttpRequest();

        if (this.req) {
            try {
                var loader = this;
                this.div.innerHTML = net.loadMessage;
                this.req.onreadystatechange = function() {
                    loader.onReadyState.call(loader);
                }
                if (url.indexOf("?") > 0) {
                    url += "&ts=" + new Date().getTime()
                }
                else {
                    url += "?ts=" + new Date().getTime()
                }

                if (method.toUpperCase() == "GET") {
                    this.req.open("GET", url, true);
                    this.req.send(null);
                }
                else {
                    var parameters = url.substr(url.indexOf("?") + 1);
                    this.req.open('POST', url, true);
                    this.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    this.req.setRequestHeader("Content-length", parameters.length);
                    this.req.setRequestHeader("Connection", "close");
                    this.req.send(parameters);
                }
            }
            catch (err) {
                this.onerror.call(this);
            }
        }
    },
    onReadyState:function() {
        var req = this.req;
        var ready = req.readyState;
        var theDiv = document.getElementById(this.div);
        
        if (ready == net.READY_STATE_COMPLETE) {
            var httpStatus = req.status;
            if (httpStatus == 200 || httpStatus == 0) {
                this.onload.call(this);
            }
            else {
                this.onerror.call(this);
            }
        }
        else {
            theDiv.innerHTML = net.loadMessage;
        }
    },
    defaultError:function() {
        alert("Error fetching data!"
            + "\n\nreadyState: " + this.req.readyState
            + "\nStatus: " + this.req.status
            + "\nHeaders: " + this.req.getAllResponseHeaders());
    }
}

function errorHandler() {
    var theDiv = document.getElementById(this.div);

    if (theDiv) {
        if (this.req.getResponseHeader("RLC_MESSAGE")) {
            theDiv.innerHTML = this.req.getResponseHeader("RLC_MESSAGE");
        }
        else {
            theDiv.style.color = "red";
            theDiv.innerHTML = "<p><b>Content is currently not available!</b></p>" +
            			"<p>Please try again later...</p>";
                //"<br/><br/><b>Ready State: </b>" + this.req.readyState +
                //"<br/><b>Status: </b>" + this.req.status +
                //"<br/><b>Headers: </b><pre>" + this.req.getAllResponseHeaders() +
                //"<br/>Page: <br/><br/>" + this.req.responseText + "</pre>";
        }
    }
    else {
        alert("Content not available!"
            + "\n\nreadyState: " + this.req.readyState
            + "\nStatus: " + this.req.status
            + "\nHeaders: " + this.req.getAllResponseHeaders());
    }
}

function loadDiv() {
    var theDiv = document.getElementById(this.div);

    if (theDiv) {
        if (this.contentType == "html")
            theDiv.innerHTML = this.req.responseText;
        else if (this.contentType == "xml")
            theDiv.innerHTML = this.responseXML;
        else {
            theDiv.style.color = "red";
            theDiv.innerHTML = "<b>" + this.url + " could not e loaded. Content type " + this.contentType + " not supported!</b>";
        }
    }
}
