﻿/**
 *   Class XmlHttp
 */
function XmlHttp(img)
{
        if (typeof img != 'undefined') this._loading_img = img
        else this._loading_img = ''
        this._return_function = null
        this._div = null
        this._getXmlHttpObject()
}

/*  Variables/Constants  */
//XmlHttp.prototype._loading_img = ''

/*  Functions  */
XmlHttp.prototype.setReturnFunction = function(func)
{
        this._return_function = func
}
XmlHttp.prototype._getXmlHttpObject = function()
{
        if (window.ActiveXObject)
        {
                try
                {
                        try
                        {
                                this._xmlObject = new ActiveXObject("Microsoft.XMLHTTP")
                        }
                        catch(e)
                        {
                                this._xmlObject = new ActiveXObject("Msxml2.XMLHTTP")
                        }
                }
                catch(e)
                {
                        this._xmlObject = new ActiveXObject("Microsoft.XMLDOM")
                }
        }
        else if (window.XMLHttpRequest)
        {
                this._xmlObject = new XMLHttpRequest()
        }
        else
        {
                alert("Greiciausiai puslapis nefunkcionuos, nes nenaudojate naujausios narsykles versijos")
                this._xmlObject = new XMLHttpRequest()
        }

}

XmlHttp.prototype.getContents = function(path, div, async)
{
        if ((this._loading_img != '')&&(typeof div != 'undefined')) $(div).innerHTML = this._loading_img
        this._div = div
        var xmlHttpRequest = this._xmlObject
        var XmlHttp = this
        xmlHttpRequest.onreadystatechange = function()
        {
                if(xmlHttpRequest.readyState == 4 || xmlHttpRequest.readyState == "complete")
                {
                        XmlHttp._return_function(xmlHttpRequest.responseText, XmlHttp._div)
                }
        }
        if (typeof async == 'undefined')
        {
                xmlHttpRequest.open('GET', path, true)
        }
        else
        {
                xmlHttpRequest.open('GET', path, false)
        }
        xmlHttpRequest.send(null)
}

XmlHttp.prototype.postContents = function(path, div, data, async)
{
        if ((this._loading_img != '')&&(typeof div != 'undefined')) $(div).innerHTML = this._loading_img
        this._div = div
        var xmlHttpRequest = this._xmlObject
        var XmlHttp = this
        xmlHttpRequest.onreadystatechange = function()
        {
                if(xmlHttpRequest.readyState == 4 || xmlHttpRequest.readyState == "complete")
                {
                        XmlHttp._return_function(xmlHttpRequest.responseText, XmlHttp._div)
                }
        }
        if (typeof async == 'undefined')
        {
                xmlHttpRequest.open('POST', path, true)
        }
        else
        {
                xmlHttpRequest.open('POST', path, false)
        }
        xmlHttpRequest.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded")
        xmlHttpRequest.send(data)
}

function addMouseMoveListener(fn)
{
        if (typeof window.addEventListener != 'undefined')
        {
                window.addEventListener('mousemove', fn, false);
        }
        else if (typeof document.addEventListener != 'undefined')
        {
                document.addEventListener('mousemove', fn, false);
        }
        else if (typeof window.attachEvent != 'undefined')
        {
                document.attachEvent('onmousemove', fn);
        }
        else
        {
                var oldfn = window.onmousemove;
                if (typeof window.onmousemove != 'function')
                {
                        window.onmousemove = fn;
                }
                else
                {
                        window.onmousemove = function()
                        {
                                oldfn();
                                fn();
                        };
                }
        }
}
/*  Underscore (_) because tinyMCE has the same function  */
function addLoadListener(fn)
{
        if (typeof window.addEventListener != 'undefined')
        {
                window.addEventListener('load', fn, false);
        }
        else if (typeof document.addEventListener != 'undefined')
        {
                document.addEventListener('load', fn, false);
        }
        else if (typeof window.attachEvent != 'undefined')
        {
                window.attachEvent('onload', fn);
        }
        else
        {
                var oldfn = window.onload;
                if (typeof window.onload != 'function')
                {
                        window.onload = fn;
                }
                else
                {
                        window.onload = function()
                        {
                                oldfn();
                                fn();
                        };
                }
        }
}

function addLoadEvent(func)
{
	var oldonload = window.onload
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else {
		window.onload = function()
		{
			if (oldonload)
			{
			    oldonload()
			}
			func()
		}
	}
}

function $()
{
	var elements = new Array()
	for (var i = 0; i < arguments.length; i++)
	{
		var element = arguments[i]
		if (typeof element == 'string')
			element = document.getElementById(element)
		if (arguments.length == 1)
			return element
		elements.push(element)
	}
	return elements
}