/* These are initalised in initWindow */
var MODULE               = null;
var ACTION               = null;
var PAGE_NAME            = null;
var oldXMLHttpRequest    = null;

/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.

// and security blocked creation of the objects.
try {
    XMLHttpRequest = function() {  return new ActiveXObject("Msxml2.XMLHTTP"); };
} catch (e) 
{
    try {
       XMLHttpRequest = function() { return new ActiveXObject("Microsoft.XMLHTTP"); };
    } catch (E) 
    {
       XMLHttpRequest = false;
    }
}

function correctPNG() 
{
	for(var i=0; i < document.images.length; i++)
		{
	  		var img = document.images[i];
	  		var imgName = img.src.toUpperCase();

	  		if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
	     		{
		 		var imgID = (img.id) ? "id='" + img.id + "' " : ""
		 		var imgClass = (img.className) ? "class='" + img.className + "' " : ""
		 		var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
		 		var imgStyle = "display:inline-block;" + img.style.cssText 

		 		if (img.align == "left")
				{
					imgStyle = "float:left;" + imgStyle;
				}

		 		if (img.align == "right")
				{
					imgStyle = "float:right;" + imgStyle;
				}

		 		if (img.parentElement.href)
				{
					imgStyle = "cursor:hand;" + imgStyle;		
				}

		 		var strNewHTML   = "<span " + imgID + imgClass + imgTitle;
				strNewHTML      += " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";";
	     			strNewHTML      += "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader";
		 		strNewHTML      += "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";

		 		img.outerHTML = strNewHTML;
		 		i = i - 1;
	     }
      }
}

addEvent( window, "load", correctPNG );

@end @*/

__XMLHttpRequest = XMLHttpRequest;

XMLHttpRequest = function ()
{ 
    var request                   = new __XMLHttpRequest();
    
    var isBusy                    = false;
    var responseXML               = null;
    var responseText              = null;
    var status                    = null;
    var statusText                = null;
    var readyState                = null;
    var onreadystatechange        = function() { };

    this.responseXML              = responseXML;
    this.responseText             = responseText;
    this.status                   = status;
    this.statusText               = statusText;
    this.readyState               = readyState;
    this.onreadystatechange       = onreadystatechange;
    this.isBusy                   = isBusy;
    
    if (request.overrideMimeType)
    {
        this.overrideMimeType      = request.overrideMimeType;
    }

    this.getAllResponseHeaders = function getAllResponseHeaders()
    {
        return request.getAllResponseHeaders();
    }

    this.getResponseHeader = function getResponseHeader( header )
    {
        return request.getResponseHeader( header );
    }

    this.open = function open( method, url, async, user, password )
    {
        request.open( method, url, async, user, password );
    }

    this.setRequestHeader = function setRequestHeader( header, value )
    {
        request.setRequestHeader( header, value );
    }

    this.abort = function abort()
    {
        var tmp                        = request.onreadystatechange;
        request.onreadystatechanged    = function () {};
        this.isBusy                    = false;
        request.abort();
        request.onreadystatechanged    = tmp;
    }

    this.send = function send( body )
    {
        if (this.isBusy)
        {
            request.abort();
        }

        this.isBusy    = true;

        request.send( body );
    }

    this.stateChanged = function stateChanged(obj)
    {       
        this.readyState    = request.readyState;
        
        if (this.readyState == 4)
        {
            this.status        = request.status;
            this.statusText    = request.statusText;
            this.responseXML    = request.responseXML;
            this.responseText   = request.responseText;
            this.isBusy         = false;
        }
        if (this.onreadystatechange)
        {
            this.onreadystatechange( this );
        }
    }

    var test                      = this;
    request.onreadystatechange    = function() { test.stateChanged( ); };
}

function addEvent( obj, evType, fn )
{
    var anon  = function(event) { var t = obj.__fn; obj.__fn = fn; obj.__fn(event); obj.__fn = t; }
    var res;
  
    if (obj.addEventListener){
        obj.addEventListener(evType, anon, false);
        res = true;
    } 

    else if (obj.attachEvent){
        res    = obj.attachEvent("on"+evType, anon);
    }

    else 
    {
        return false;
    }

    return res;
}

function removeEvent(obj, evType, fn)
{	
    var anon  = function(event) { var t = obj.__fn; obj.__fn = fn; obj.__fn(event); obj.__fn = t; }
	
    if (obj.removeEventListener)
    {
        obj.removeEventListener( evType, anon, false );
    }

    else if (obj.detachEvent)
    {
        obj.detachEvent( "on"+evType, anon );
    }

    else
    {
        obj["on"+evType]    = function(  ) { };
    }
}


function addEventIE( obj, evType, fn )
{
    var anon  = function(event) { obj.__fn = fn; return obj.__fn( event ); }
    var res;

    res    = obj.attachEvent( "on"+evType, anon );

    return res;
}

function addEventMoz( obj, evType, fn )
{
    var anon  = function(event) { obj.__fn = fn; return obj.__fn( event ); }

    obj.addEventListener( evType, anon, false );

    return true;
}

function addEventMisc( obj, evType, fn )
{
    obj["on"+evType]    = function(event) { obj.__fn = fn; return obj.__fn( event ); }

    return true;
}

function removeEventIE( obj, evType, fn )
{
    var anon  = function(event) { obj.__fn = fn; return obj.__fn( event ); }

    obj.detachEvent( "on"+evType, anon );
}

function removeEventMoz( obj, evType, fn )
{
    var anon  = function(event) { obj.__fn = fn; return obj.__fn( event ); }

    obj.removeEventListener( evType, anon, false );
}

function removeEventMisc( obj, evType, fn )
{		
    obj["on"+evType]    = function() { };
}

function px( str )
{
    return str + "px";
}

function em( str )
{
    return str + "em";
}

function ns4_getElementById( el )
{
    return document.layers[el];
}

function ie3_getElementById( el )
{
    return document.all[el];
}

function addInterval( obj, fn, time )
{
    var args    = new Array();
    var len     = addInterval.registered_intervals_array.length;
    var fnArgs  = "";

    addInterval.registered_intervals_array[len]        = { id: "", args: "", arg: new Array(), object: obj, func: fn, interval: time };

    addInterval.registered_intervals_array[len].args    = "";

    for (var i = 3; i < arguments.length; i++)
    {
        addInterval.registered_intervals_array[len].arg[i-3]     = arguments[i];
        
        addInterval.registered_intervals_array[len].args       += "addInterval.registered_intervals_array["+len+"].arg["+(i-3)+"]";
        if (i < arguments.length - 1)
        {
            addInterval.registered_intervals_array[len].args   += ", ";
        }
    }
    
    var anon    = function( ) {obj.__fn = fn; return eval( "obj.__fn( " + addInterval.registered_intervals_array[len].args + "  );")  }

    addInterval.registered_intervals_array[len].id    = setInterval( anon, time );

    return addInterval.registered_intervals_array[len].id;
}

addInterval.registered_intervals_array = new Array(  );

function removeInterval( obj, fn, time )
{
    for (var i = 0; i <  addInterval.registered_intervals_array.length; i++)
    {
        if (addInterval.registered_intervals_array[i].object == obj && addInterval.registered_intervals_array[i].interval == time && addInterval.registered_intervals_array[i].func == fn)
        {
            clearInterval( addInterval.registered_intervals_array[i].id );
            addInterval.registered_intervals_array[i]    = null;
            break;
        }
    }
}

function addTimeout( obj, fn, time )
{
    var args    = new Array();
    var len     = addTimeout.registered_timeouts_array.length;
    var fnArgs  = "";

    addTimeout.registered_timeouts_array[len]        = { id: "", args: "", arg: new Array(), object: obj, func: fn, interval: time };

    addTimeout.registered_timeouts_array[len].args    = "";

    for (var i = 3; i < arguments.length; i++)
    {
        addTimeout.registered_timeouts_array[len].arg[i-3]     = arguments[i];
        
        addTimeout.registered_timeouts_array[len].args       += "addTimeout.registered_timeouts_array["+len+"].arg["+(i-3)+"]";
        if (i < arguments.length - 1)
        {
            addTimeout.registered_timeouts_array[len].args   += ", ";
        }
    }
    
    var anon    = function( ) {obj.__fn = fn; return eval( "obj.__fn( " + addTimeout.registered_timeouts_array[len].args + "  );")  }

    addTimeout.registered_timeouts_array[len].id    = setTimeout( anon, time );

    return addTimeout.registered_timeouts_array[len].id;
}

function removeTimeout( obj, fn, time )
{
    for (var i = 0; i <  addTimeout.registered_timeouts_array.length; i++)
    {
        if (addTimeout.registered_timeouts_array[i].object == obj && addTimeout.registered_timeouts_array[i].interval == time && addTimeout.registered_timeouts_array[i].func == fn)
        {
            clearInterval( addTimeout.registered_timeouts_array[i].id );
            addTimeout.registered_timeouts_array[i]    = null;
            break;
        }
    }
}

function errorHandler( message, url, line )
{
    var debug    = document.createElement( "img" );
    debug.src    = "./jsError.php?message=" + escape( message ) + "&url=" + escape( url ) + "&line=" + escape( line ) + "&module=" + escape( MODULE ) + "&page=" + escape( PAGE_NAME ) + "&action=" + escape( ACTION );
    debug.style.visibility = "hidden";
    debug.style.display    = "none";
    debug.style.width      = "1px";
    debug.style.height     = "1px";
    debug.style.overflow   = "hidden";
    
    addEvent( debug, "load", function( e ) { document.body.removeChild( this ); } );

    document.body.appendChild( debug );

    alert( message + " " + line );

    return false;
}

function initWindow( e )
{
    if (document.body.attachEvent)
    {
        addEvent       = addEventIE;
        removeEvent    = removeEventIE;
    }

    else if (document.body.addEventListener)
    {
        addEvent       = addEventMoz;
        removeEvent    = removeEventMoz;
    }
    else
    {
        addEvent       = addEventMisc;
        removeEvent    = removeEventMisc;
    }

    if (!document.getElementById)
    {
        if (document.all)
        {
            document.getElementById    = ns4_getElementById;
        }
        else if (document.layers)
        {
            document.getElementById    = ie3_getElementById;
        }
    }
    
    var url    = window.location.href;

    if (!url)
    {
        url    = window.location;
    }

    url        = url.split( "/" );
    var page   = url[url.length-1].split( "?" );

    PAGE_NAME  = page[0];

    if (page[1])
    {
        var query  = page[1].split( "&" );
    
        for (var i = 0; i < query.length; i++)
        {
            var pair    = query[i].split( "=" );

            switch (pair[0])
            {
                case "module":
                    MODULE    = pair[1];
                break;
                case "action":
                    ACTION    = pair[1];
                break; 
            }
        }
    }

    window.onerror     = errorHandler;
    addEvent( window, "unload", destroyWindow );
}

function destroyWindow( e )
{
    
}

addEvent( window, "load", initWindow );
