﻿var xmlHttpObj;
// Common variables
var READYSTATE_UNINITIALIZED = 0;
var READYSTATE_LOADING = 1;
var READYSTATE_LOADED = 2;
var READYSTATE_INTERACTIVE = 3;
var READYSTATE_COMPLETE = 4;

// Common values for HTTP status codes
var HTTPSTATUS_OK = 200;
var HTTPSTATUS_NOTFOUND = 404;
var HTTPSTATUS_SERVERERROR = 500;

// create http request object
function CreateXmlHttpRequestObject() {
    
    if (window.XMLHttpRequest) {
        xmlHttpObj = new XMLHttpRequest();
    }
    else
    {
        try
        {
            xmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e)
        {
            xmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
        }
     }
     
     return xmlHttpObj;
}

// create http request object
function GetXmlHttpRequest() {
    
    if (window.XMLHttpRequest) {
        xmlHttpObj = new XMLHttpRequest();
    }
    else
    {
        try
        {
            xmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e)
        {
            xmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
        }
     }
     
     return xmlHttpObj;
}

function AJAXrequest(url, f) {
    var randomnumber=Math.floor(Math.random()*10001)
    url +=  "&rand=" + randomnumber;

    xmlHttpObj = GetXmlHttpRequest();
    if (xmlHttpObj) {
        xmlHttpObj.open("GET", url, true);
        
        xmlHttpObj.onreadystatechange = f;
        xmlHttpObj.send(null);
    }

}


function AJAXrequestSuccess(targetUrl, f) {
    if (targetUrl == '')
        return;
    
    var req = new Object();
    this.addRandom = false;
    req.processUrl = targetUrl;

    //alert (url);
    
    req.xmlHttp = GetXmlHttpRequest();
    req.processFunction = f;
    this.process = function ()
    {
        //alert (this.xmlHttp);
        if (!req.xmlHttp)
        {
            alert ('break');
            return;
        }
        
        var url = req.processUrl;
        
        if (this.addRandom)
        {
            var randomnumber=Math.floor(Math.random()*10001)
            if (url.indexOf("?") == -1)
                url = url + "?rand=" + randomnumber;
            else
                url = url + "&rand=" + randomnumber;
            
        }

        req.xmlHttp.open("GET", url , true);
       
        req.xmlHttp.onreadystatechange = function()
        {
            if (req.xmlHttp.readyState == 4 && req.xmlHttp.status == "200")
            {
                req.processFunction (req.xmlHttp.responseText);
            }
        };

        req.xmlHttp.send(null);
    }
}


function clearCombo(combo)
{
    combo.options.length = 0;
}




function MouseEvent(e)
{
    if (e)
    {
        this.e = e;
    }
    else
    {
        this.e = window.event;
    }

    if (this.e.pageX)
    {
        this.x = this.e.pageX;
    }
    else
    {
        this.x = this.e.clientX;
    }

    if (this.e.pageY)
    {
        this.y = this.e.pageY;
    }
    else
    {
        this.y = this.e.clientY;
    }
    
    if (this.e.target)
    {
        this.target = this.e.target;
    }
    else
    {
        this.target = this.e.srcElement;
    }

    if (self.pageYOffset) // all except Explorer
    {
        //this.scrollX = self.pageXOffset;
        //this.scrollY = self.pageYOffset;
        this.scrollX = 0;
        this.scrollY = 0;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
        // Explorer 6 Strict
    {
        this.scrollX = document.documentElement.scrollLeft;
        this.scrollY = document.documentElement.scrollTop;
    }
    else if (document.body) // all other Explorers
    {
        this.scrollX = document.body.scrollLeft;
        this.scrollY = document.body.scrollTop;
    }

}

function addListener (type, callback)
{
    if (window.addEventListener)
    {
        window.addEventListener (type, callback, false);
    }
    else if (window.attachEvent)
    {
        window.attachEvent ("on" + type, callback, false);
    }
       
}
function removeListener (type, callback)
{
    if (document.removeEventListener)
    {
        document.removeEventListener (type, callback, false);
    }
    else if (document.detachEvent)
    {
        document.detachEvent ("on" + type, callback, false);
    }
       
}

function setTransparency (target, amount)
{
     target.style.opacity = amount/100;
     target.style.filter = "alpha(opacity=" + amount + ")";

}
