// Ajax Kreipimosi i Narsykle Dalis
// *************** START HTTP REQUEST *********************
var page_location;
function createRequestObject() {
    
        var req;

        if(window.XMLHttpRequest){
            // Firefox, Safari, Opera...
            req = new XMLHttpRequest();
        } else if(window.ActiveXObject) {
            // Internet Explorer 5+
            req = new ActiveXObject("Microsoft.XMLHTTP");
        } else {
            // Jeigu naršyklė nepalaiko Ajax
    // arba nutiko kokia kritine klaida
            alert('Naršyklė nepalaiko Ajax');
        }

        return req;

    }

    // Sukuriamas XMLHttpRequest objektas
    var http = createRequestObject();
    var element;
    //var location = "";
// ***************** END HTTP REQUEST *********************

// ***************** START GET METODAS ********************
    function please_make_ajax(act, id) {
        // Atidarome PHP scripta GET metodui
        //http.open('get', 'check.php?user='+act);
        //alert(act + ' id=' + id);
        http.open('get', ''+act);
        element = id;
        http.onreadystatechange = handleResponse;
        http.send(null);
    }
    function please_make_ajax_notload(act, id) {
        // Atidarome PHP scripta GET metodui
        //http.open('get', 'check.php?user='+act);
        //alert(act + ' id=' + id);
        http.open('get', ''+act);
        element = id;
        http.onreadystatechange = handleResponse_notload;
        http.send(null);
    }
    function INPUTplease_make_ajax(act, id, inputid) {
        //act : request eilutė
        //id : elemento kuriame reikia gražinti reikšmę ID
        //inputid : elemento kurio reikšmė formuoja request eilutę reikšmė;
        // Atidarome PHP scripta GET metodui
        //http.open('get', 'check.php?user='+act);
        
        value_input = document.getElementById(inputid).value;
        //alert(act + ' id=' + id + ' inputid=' + inputid + ' value_input=' + value_input);
        http.open('get', '' + act + value_input);
        //alert('action open =' + act + value_input);
        element = id;

        //location = loc;
        http.onreadystatechange = handleResponse_notload;
        http.send(null);
    }
    function SEARCHplease_make_ajax(act, id, inputid) {
        //act : request eilutė
        //id : elemento kuriame reikia gražinti reikšmę ID
        //inputid : elemento kurio reikšmė formuoja request eilutę reikšmė;
        // Atidarome PHP scripta GET metodui
        //http.open('get', 'check.php?user='+act);
        value_input = document.getElementById(inputid).value;
        //alert(act + ' id=' + id + ' inputid=' + inputid + ' value_input=' + value_input);
        http.open('get', '' + act + value_input);
        //alert('action open =' + act + value_input);
        element = id;
        //location = loc;
        http.onreadystatechange = handleResponse_search;
        http.send(null);
    }
    function please_make_ajax_location(act, id, loc) {
        // Atidarome PHP scripta GET metodui
        //http.open('get', 'check.php?user='+act);
        //alert(act + ',   id=' + id + ',    loc=' + loc);

        http.open('get', ''+act);
        element = id;
        http.onreadystatechange = handleResponse_notload_location;
        page_location = loc;
        /*    var elm = document.getElementById(loc);
        var y = elm.offsetTop;
        alert('y=' + y);
        var node = elm;
        while (node.offsetParent && node.offsetParent != document.body) {
            node = node.offsetParent;
            y += node.offsetTop;
        }
*/        //document.location = loc;
        http.send(null);

        //Scroll
        //alert('loc=' + loc);
    }
function smoothScroll(eID) {
    var startY = currentYPosition();
    var stopY = elmYPosition(eID);
    var distance = stopY > startY ? stopY - startY : startY - stopY;
    if (distance < 100) {
        scrollTo(0, stopY); return;
    }
    var speed = Math.round(distance / 100);
    var step = Math.round(distance / 25);
    var leapY = stopY > startY ? startY + step : startY - step;
    var timer = 0;
    if (stopY > startY) {
        for ( var i=startY; i<stopY; i+=step ) {
            setTimeout("window.scrollTo(0, "+leapY+")", timer * speed);
            leapY += step; if (leapY > stopY) leapY = stopY; timer++;
        } return;
    }
    for ( var i=startY; i>stopY; i-=step ) {
        setTimeout("window.scrollTo(0, "+leapY+")", timer * speed);
        leapY -= step; if (leapY < stopY) leapY = stopY; timer++;
    }
}
function currentYPosition() {
    // Firefox, Chrome, Opera, Safari
    if (self.pageYOffset) return self.pageYOffset;
    // Internet Explorer 6 - standards mode
    if (document.documentElement && document.documentElement.scrollTop)
        return document.documentElement.scrollTop;
    // Internet Explorer 6, 7 and 8
    if (document.body.scrollTop) return document.body.scrollTop;
    return 0;
}
function elmYPosition(eID) {
    var elm = document.getElementById(eID);
    //alert('eID=' + eID +  ', elm=' + elm);
    var y = elm.offsetTop;
    var node = elm;
    while (node.offsetParent && node.offsetParent != document.body) {
        node = node.offsetParent;
        y += node.offsetTop;
    } return y;
}
function CKplease_make_ajax(act, id, ckid) {
        // Atidarome PHP scripta GET metodui
        //http.open('get', 'check.php?user='+act);
        ckvalue = document.getElementById(ckid).checked;
        //alert(act+ckvalue);
        //alert(ckid+'='+ckvalue);
        http.open('get', ''+act+ckvalue);
        element = id;
        http.onreadystatechange = handleResponse;
        http.send(null);
    }


function DROPDOWNplease_make_ajax(act, id, did) {
        selected_text = document.getElementById(did).options[document.getElementById(did).selectedIndex].value;
        http.open('get', ''+act+selected_text);
        //alert(''+act+selected_text);
        element = id;
        http.onreadystatechange = handleResponse;
        http.send(null);
    }
function import_file(import_file, id, did) {
        //alert(act+id);
        // Atidarome PHP scripta GET metodui
        //http.open('get', 'check.php?user='+act);
        selected_text = document.getElementById(did).options[document.getElementById(did).selectedIndex].value;
       // alert('selected_text='+selected_text);
        //alert(act+selected_text);
        //alert(ckid+'='+ckvalue);
        http.open('get', ''+act+selected_text);
        element = id;
        http.onreadystatechange = handleResponse;
        http.send(null);
    }
function validate_file(valid_file, id, did) {
        //alert(act+id);
        // Atidarome PHP scripta GET metodui
        //http.open('get', 'check.php?user='+act);
        selected_text = document.getElementById(did).options[document.getElementById(did).selectedIndex].value;
       // alert('selected_text='+selected_text);
        //alert(act+selected_text);
        //alert(ckid+'='+ckvalue);
        http.open('get', ''+act+selected_text);
        element = id;
        http.onreadystatechange = handleResponse;
        http.send(null);
    }

    function handleResponse() {
        //alert(http.readyState + ' ir ' + http.status);
        if(http.readyState == 4 ){
            // Tekstas kuris grazinamas is PHP failo
            if (http.status == 200){
            var response = http.responseText;
            //alert('response =' + response);
                if(response) {
                    // Atnaujiname nurodyta ID
                    document.getElementById(element).innerHTML = response;
                    
                    //setTimeout(countPosts,20);
                }else  {
                    alert('Negauta jokiu duomenų iš *.php failo');
                }
            }
        }
      /*  if (http.readyState < 4){
            loading_element_id = element;
            load_image = '<frame><div style="text-align:center;">\n\
            <img src="images/ajax-loader.gif" width="100" height="10" alt="ajax-loader"/></div></frame>';
            
            //document.getElementById(loading_element_id).innerHTML = load_image;
        }*/
    }
    function handleResponse_notload() {
        //alert(http.readyState + ' ir ' + http.status);
        if(http.readyState == 4 ){
            // Tekstas kuris grazinamas is PHP failo
            if (http.status == 200){
            var response = http.responseText;
            //alert('element=' + element + ' response =' + response);
                if(response) {
                    // Atnaujiname nurodyta ID
                    document.getElementById(element).innerHTML = response;
                    //setTimeout(countPosts,20);
                }else  {
                    alert('Negauta jokiu duomenų iš *.php failo');
                }
            }
        }
    }
    function handleResponse_notload_location() {
        //alert(http.readyState + ' ir ' + http.status);
        if(http.readyState == 4 ){
            // Tekstas kuris grazinamas is PHP failo
            if (http.status == 200){
            var response = http.responseText;
            //alert('response =' + response);
                if(response) {
                    // Atnaujiname nurodyta ID
                    document.getElementById(element).innerHTML = response;

    //window.location = location;
                    //setTimeout(countPosts,20);
                }else  {
                    alert('Negauta jokiu duomenų iš *.php failo');
                }
            }
            //alert('page_location='+page_location);
            location = '#'+page_location;
            //alert('location nustatytas');
        }
    }
    function handleResponse_search() {
        //alert('handleResponse_search=' + http.readyState + ' ir ' + http.status);
        if(http.readyState == 4 ){
            // Tekstas kuris grazinamas is PHP failo
            if (http.status == 200){
            var response = http.responseText;
            //alert('response =' + response);
                if(response) {
                    // Atnaujiname nurodyta ID
                    document.getElementById(element).innerHTML = response;

                    //setTimeout(countPosts,20);
                }else  {
                    alert('Negauta jokiu duomenų iš *.php failo');
                }
            }
        }
        if (http.readyState < 4){
            loading_element_id = element;
            load_image = '<frame><div style="text-align:center;">\n\
            <img src="images/ajax-loader.gif" width="100" height="10" alt="ajax-loader"/></div></frame>';

            //document.getElementById(loading_element_id).innerHTML = load_image;
        }
    }
// ***************** END GET METODAS ************************

function clear_field_and_click(id) {
    inputel = document.getElementById(id).value = '';
    document.getElementById(id).click();
}
