var xmlHttp = createXmlHttpRequestObject();
//alert (xmlHttp);

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject()
{
    // will store the reference to the XMLHttpRequest object
    var xmlHttp;
    // this should work for all browsers except IE6 and older
    try
    {
        // try to create XMLHttpRequest object
        xmlHttp = new XMLHttpRequest();
    }
    catch(e)
    {
        // assume IE6 or older
        var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
                                        'MSXML2.XMLHTTP.5.0',
                                        'MSXML2.XMLHTTP.4.0',
                                        'MSXML2.XMLHTTP.3.0',
                                        'MSXML2.XMLHTTP',
                                        'Microsoft.XMLHTTP');
        // try every prog id until one works
        for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
        {
            try

            {
                // try to create XMLHttpRequest object
                xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
            }
            catch (e) {}
        }
    }
    // return the created object or display an error message
    if (!xmlHttp)
        alert("Error creating the XMLHttpRequest object.");
    else
        return xmlHttp;
}

function send_(form){

    if (xmlHttp)
    {
    	set_progress_bar('num_res');
        try
        {
            var url = 'calc.php?' +
                    'sex=' + form.sex.value +
                    '&weight=' + form.weight.value +
                    '&drink=' + form.drink.value +
                    '&vol=' + form.vol.value +
                    '&hours=' + form.hours.value +
                    '&mins=' + form.mins.value +
                    '&mode=ajax';
            // initiate reading a file from the server
            xmlHttp.open("GET", url, true);
            xmlHttp.onreadystatechange = handleRequestStateChange;
            xmlHttp.send(null);
        }

        catch (e)
        {
            alert("Ошибка запроса:\n" + e.toString());
        }
    }

  return false;
}

function handleRequestStateChange()
{
    // when readyState is 4, we are ready to read the server response
    if (xmlHttp.readyState == 4)
    {
        // continue only if HTTP status is "OK"
        if (xmlHttp.status == 200)
        {
            try
            {
                calc_output();
            }
            catch(e)
            {
                clear_html('num_res');
                clear_html('txt_res');
                clear_html('time_res');
            	// display error message
                alert("Error reading the response: " + e.toString());
            }
        }
        else
        {
            clear_html('num_res');
            clear_html('txt_res');
            clear_html('time_res');
        	// display status message
            alert("There was a problem retrieving the data:\n" +
                xmlHttp.statusText);
        }
    }
}

function calc_output()
{
    var taken_arr = new Array();
    taken_arr = xmlHttp.responseText.split('@_@_@');
    var status = taken_arr[0];
    var msg = taken_arr[1];
    if (status == 0)
    {
        clear_html('num_res');
        clear_html('txt_res');
        clear_html('time_res');
        document.getElementById('calc_res').style.backgroundColor = '#fff';
    	alert(msg);
    }
    else if (status == 1)
    {
        var time_msg = taken_arr[2];
        var txt_msg = taken_arr[3];
        cr_obj = document.getElementById('num_res');
        cr_obj.innerHTML = msg;
        txt_obj = document.getElementById('txt_res');
        txt_obj.innerHTML = txt_msg;
        time_obj = document.getElementById('time_res');
        time_obj.innerHTML = time_msg;
        var color_ = '#fff';
        t1 = msg.indexOf('>') + 1;
        t2 = msg.lastIndexOf('<');
        var num_ = msg.substr(t1,t2 - t1);
        if (num_ < 0.2)
            color_ = '#9fc';
        else if (num_ < 0.5)
            color_ = '#ff3';
        else
            color_ = '#f99';
        document.getElementById('calc_res').style.backgroundColor = color_;
    }
}

function clear_html(div_id)
{
	document.getElementById(div_id).innerHTML = '';
}

function set_progress_bar(div_id)
{
	document.getElementById(div_id).innerHTML = '<center><img src="progress.gif" /></center>';
}

