// Javascript code for AJAX functions
var postHash = '{null}';
var postString = '';
var debug = 0;
var act = '';

function doLoad(force) {
    // Получаем текст запроса из <input>-поля.
    // Создаем новый объект JSHttpRequest.
    var xmlHttp;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    xmlHttp.onreadystatechange = function() {
        if(xmlHttp.readyState == 4) {
            var scriptResponse = new Array();
            var b = xmlHttp.responseText;
            scriptResponse = b.split('|');
            if(act == 'login') {
                if(scriptResponse[0] == "1") {
                    //succsess
                    window.location = scriptResponse[1];
                } else {
                    //error
                    errorMessage = '<font color="#FF0000" face="Verdana" size="1">' + scriptResponse[1] + '</font>';
                    document.getElementById('result').innerHTML = errorMessage;
                }
            }
            //document.getElementById('result').innerHTML = xmlHttp.responseText;
            hideAjaxDiv();
        }
    }
    getUrl = 'index.php?action=doajaxstuff&' + postString;
    xmlHttp.open("POST", getUrl, true);
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp.send(postHash);
}

var timeout = null;
var fusername = fpassword = '';
function doLoadUp() {
    if (timeout) clearTimeout(timeout);
    timeout = setTimeout(doLoad, 1000);
}

function doAjaxLogin(fmLogin) {
    showAjaxDiv();
    act = 'login';
    //fusername = document.getElementById('ajaxFusername').value;
    //fpassword = document.getElementById('ajaxFpassword').value;
    //sidU = document.getElementById('sidU').value;
    postString = sidU;
    //postHash = '{ fusername:' + fusername + ',fpassword:' + fpassword + ',do:login }';
    postHash = "fusername=" + fusername + "&fpassword=" + fpassword + "&do=login";
    doLoad(true);
}

function doAjaxSelectRotations(fmSelect, sidU) {
    showAjaxDiv();
    act = 'login';
    memID = document.fmAjaxSelectRotations.memID.value;
    postString = sidU;
    postHash = "memID=" + memID + "&do=selectRotations";
    doLoad(true);
}
