var ajax_loading = false; // pokud se nacita jeden ajax, nemel by se nacitat dalsi
var ajax_body = false;
var show_loading = false;// zda se ma zobrazovat nacitani

function send_xmlhttprequest(fce, id, method, url, data, headers) {   
    var xmlhttp = (XMLHttpRequest ? new XMLHttpRequest : (ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
    if (!xmlhttp) {
        return false;
    }
    xmlhttp.open(method, url);
    xmlhttp.onreadystatechange = function () {
        xml_state_change(xmlhttp, fce, id);
    };
    if (headers) {
        for (var key in headers) {
            xmlhttp.setRequestHeader(key, headers[key]);
        }
    }
    if (method == "POST") {
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-length", data.length);
        xmlhttp.setRequestHeader("Connection", "close");
    }
    xmlhttp.send(data);
    return true;
}


function xml_state_change(xmlhttp, fce, id) {
    if (xmlhttp.readyState == 4) {             
        ajax_loading_stop();
        var odpoved = xmlhttp.responseText;
        
        //alert(fce.name+'\n'+odpoved+'\n'+id);
        //ajax_check_err(odpoved);
        fce(odpoved,id);
    }
}
/**
 * @param form - form element which elements send
 * @param fce  - function called on receive response from server
 * @param id   - ID of element to update (or simly ANY var or const to give as second arg. for fce)
 * @param cond - if cond == false -> return false and do not call Ajax (good for form checking function)
 * return FALSE on SUCCESS (to avoid usual form submit)    
 */     
function ajax_form(form,fce,id,cond) {
    if (!cond) return false;
    var data = "";
    for (i=0; i<form.elements.length; i++) {
        el = form.elements[i];
        if (el.name.length > 0) data = data + "&" + el.name + "=" + encodeURI(el.value);
    }
    data = data + "&ajax=true";
    if (send_xmlhttprequest(fce, id, "POST", form.action, data)) return false; //nechceme aby se odeslal
    else return true; //chceme aby se odeslal formular
}
 
function ajax_request(url,method,fce,id,data) {
    if (ajax_loading){
        setTimeout(function(){ajax_request(url,method,fce,id,data)}, 100);
    }
    else {
        ajax_loading_start();
        url = encodeURI(url);
        if (send_xmlhttprequest(fce, id, method, url, data)) return false; //nechceme aby se preslo na adresu odkazu
        else return true; //chceme aby se odeslal formular
    }     
} 

function update_all(text,id) {
        //alert(text);
        document.getElementById(id).innerHTML = text;
}

function update_before(text,id) {
}

function update_after(text,id) {
   // alert(text);
    div = document.getElementById(id);
    div.innerHTML += text;
    
    //alert(div.scrollTop+" : "+div.scrollHeight);
}

function getArrayFromAnswer(answer){
    var result = new Array();
    var data = new Array();
    var datas = answer.split(/\{array_separator\}/);
    for(var i in datas){
        data = datas[i].split(/\{data_separator\}/);  
        if (data.length > 1){
            result[data[0]] = data[1];
        }
    }
    return result;
}


function ajax_check_err(error){
    var t = new RegExp("framework_error", 'gi');  
    if ((error != '') && (t.test(error))) {
        ajax_alert_err(error);
    }
    t = new RegExp("^error:", 'gi');
    if (t.test(error)){
        alert(error);
    }
    t = new RegExp("Parse error", 'gi');
    if (t.test(error)){
        ajax_alert_err(error);
    }
}
function ajax_alert_err(error){                
    var d = document.getElementById('AjaxError');
    ajax_body = document.getElementsByTagName('body')[0]; 
    if (!d){                        
        var d = document.createElement('div');
        var width = (screen.width);
        var height = (screen.height);
        var top = 0;
        var left = 0; 
        if( typeof( window.innerWidth ) == 'number' ) {
            //Non-IE
            width = window.innerWidth;
            height = window.innerHeight;
        } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
            //IE 6+ in 'standards compliant mode'
            width = document.documentElement.clientWidth;
            height = document.documentElement.clientHeight;
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
            //IE 4 compatible
            width = document.body.clientWidth;
            height = document.body.clientHeight;
        }        
        d.setAttribute('style', 'position:absolute; top:'+top+'px; left:'+left+'px; width:'+width+'px; height:'+height+'px; overflow:auto; color:#F00; background:#000; opacity:0.9; filter: alpha(90);');
        d.setAttribute('id', 'AjaxError');
        d.onclick = function(){ ajax_body.removeChild(this); }
        d.innerHTML = error;
        ajax_body.appendChild(d);                             
        //alert('chyba v ajaxu!');
    }
}


function ajax_loading_start(){
    ajax_loading = true;
    if (show_loading){
        ajax_body = document.getElementsByTagName('body')[0];    
        var d = document.getElementById('procesingDiv');
        if (!d){
        var d = document.createElement('div');
        var width = (screen.width);         
        var height = (screen.height);             
        var top = 0;//(screen.height-height)/2;
        var left = 0;
        if( typeof( window.innerWidth ) == 'number' ) {
            //Non-IE
            width = window.innerWidth;
            height = window.innerHeight;
        } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
            //IE 6+ in 'standards compliant mode'
            width = document.documentElement.clientWidth;
            height = document.documentElement.clientHeight;
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
            //IE 4 compatible
            width = document.body.clientWidth;
            height = document.body.clientHeight;
        }              
    
        d.setAttribute('style', 'position:absolute; top:'+top+'px; left:'+left+'px; width:'+width+'px; height:'+height+'px; overflow:auto; background:black; color:#000; opacity:0.1; filter: alpha(10);');
        d.setAttribute('id', 'procesingDiv');
        d.onclick = function(){ ajax_body.removeChild(this); }
        d.innerHTML = '<center><h1 style="color:#0f0" >Loading...</h1></center>';
        ajax_body.appendChild(d);  
        }
    } 
}
function ajax_loading_stop(){   
    ajax_loading = false; 
    if (show_loading){      
        ajax_body = document.getElementsByTagName('body')[0]; 
        var d = document.getElementById('procesingDiv');
        if (d){
            setTimeout(function(){ajax_body.removeChild(d)}, 1000);
        }
    }
}
    
