function getHTTPObject()
{
    var httpRequest;

    if (window.XMLHttpRequest)
    {
        httpRequest = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        try
        {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {}
        }
    }

    return httpRequest;
}


function HTTPRequest(func, url, method, args, extra)
{
    var request;

    if (!(request = new getHTTPObject()))
    {
        alert("Couldn't create new HTTP object.");
        return;
    }

    if (!method)
    {
        method = "GET";
    }

    method = method.toUpperCase();

    if ((method == "GET") && args)
    {
        url += '?' + args;
    }

    request.onreadystatechange = function()
    {
        if (request.readyState == 4)
        {
            func(request.responseText, extra);
        }
    }

    try
    {
        request.open(method, url, true);
    }
    catch (e)
    {
        alert("Couldn't open HTTP " + method + " connection.\n" + e);
        return;
    }

    if ((method == "POST") && args)
    {
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
        request.setRequestHeader("Content-length", args.length);
        request.setRequestHeader("Connection", "close");
        request.send(args);
    }
    else
    {
        request.send(null);
    }
}


function parseResponse(response)
{
    var results = new Array();

    if (!response)
    {
        return results;
    }

    var idx = 0;
    var re = /^\d$/;

    while (response.substr(idx, 1).match(re))
    {
        idx++;
    }

    if (!idx)
    {
        results[0] = response;
        return results;
    }

    var len = Number(response.substr(0, idx));

    if (response.substr(idx, 1) != ":")
    {
        results[0] = response;
        return results;
    }

    response = response.substr(idx + 1);
    var data = response.substr(0, len);

    if (!data)
    {
        results[0] = response;
        return results;
    }

    results[0] = data;
    response = response.substr(len + 1);

    if (!response)
    {
        return results;
    }

    var tmp = parseResponse(response);

    if (!tmp || !tmp.length)
    {
        return results;
    }

    for (idx = 0; idx < tmp.length; idx++)
    {
        results[idx + 1] = tmp[idx];
    }

    return results;
}


function encodeUTF8(txt)
{
    if (!txt)
    {
        return '';
    }

    var utftext = '';

    for (var idx = 0; idx < txt.length; idx++)
    {
        var c = txt.charCodeAt(idx);

        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 decodeUTF8(txt)
{
    var str = '';
    var idx = 0;
    var c = c1 = c2 = 0;

    while (idx < txt.length)
    {
        c = txt.charCodeAt(idx);

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

    return str;
}


function fixURIChars(txt)
{
    if (!txt)
    {
        return '';
    }

    txt = txt.replace('$', '%24');
    txt = txt.replace('%', '%25');
    txt = txt.replace('&', '%26');
    txt = txt.replace('+', '%2B');
    txt = txt.replace(',', '%2C');
    txt = txt.replace('/', '%2F');
    txt = txt.replace(':', '%3A');
    txt = txt.replace(';', '%3B');
    txt = txt.replace('<', '%3C');
    txt = txt.replace('=', '%3D');
    txt = txt.replace('>', '%3E');
    txt = txt.replace('?', '%3F');
    txt = txt.replace('@', '%40');
    return txt;
}

