//GetElementsByID Shorthand
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;
}
var responseCache;
var step;

// Crossbrowser XMLHttpRequest Object
function createXMLHttpRequest()
{
    try { return new ActiveXObject("Msxml2.XMLHTTP");    } catch(e) {}
    try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
    try { return new XMLHttpRequest();                   } catch(e) {}
    window.status = "XMLHttpRequest not supported";
    return null;
}

// Update the page with the response.
function stateListener()
{
    step = 1;
    if (window.translator.readyState == 4)
    {
        if (window.translator.status == 200)
        {
            xml = translator.responseXML;
            estimate = xml.getElementsByTagName("estimate")[0].firstChild.nodeValue;
            practic = xml.getElementsByTagName("practic")[0].firstChild.nodeValue;
            $("est_pract_" + estimate).innerHTML = practic;
            window.status = "";
        }
        else
        {
            window.status = "Проблемы при отправке данных.";
        }
    }
    else
    {
        window.status = "Подождите, идёт отправка данных.";
    }
}

// Change message "wait" status
function change_wait_status()
{
    if (window.translator.readyState != 4)
    {
        if (step > 3)
        {
            step = 1;
        }

        if (step == 1)
        {
            window.status = "Подождите, идёт отправка данных.";
        }
        window.status += ".";

        step++;

        window.setTimeout("change_wait_status()", 500);
    }
}

//create the XMLHttpRequest() object
window.translator = createXMLHttpRequest();

//Query the server with the data
function send_request(estimateID, practic)
{
    try
    {
        var time = new Date();

        //create the XMLHttpRequest() object
        window.translator = createXMLHttpRequest();

        translator.onreadystatechange = stateListener;
        translator.open("GET", "estimate_practicality.php?e="+estimateID+"&p="+practic+"&rand="+ time.getTime(), true);
        translator.send(null);
    }
    catch (e)
    {
        alert(e.message);
    }
}

function generate(estimateID, practic)
{
    if(window.translator.readyState == 4 || window.translator.readyState == 0)
    {
        send_request(estimateID, practic);
    }
}
