function ajaxEngine(replaceID, type, js) {
    let xmlhttp = false;
    /*if(type != 2)
     {
     gid(replaceID).innerHTML = '<div align="center"><img src="media/loading.gif" /></div>';
     }*/
    try {
        xmlhttp = new XMLHttpRequest();
    }
    catch (e) {
        try {
            xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
        }
        catch (e) {
            xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
        }
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
                if (type == 2) {
                    document.getElementById(replaceID).value = xmlhttp.responseText;
                    eval(js);
                } else {
                    document.getElementById(replaceID).innerHTML = xmlhttp.responseText;
                    eval(js);
                }
            } else {
                //alert('AJAX: ошибка выполнения!');
            }
        }
    }
    return xmlhttp;
}
function fixedEncodeURIComponent(str) {
    const newString = str.replace(/\//g, "|");
    return encodeURIComponent(newString).replace(/[!'()*]/g, escape);
}
function ajaxGet(uri, id) {
    const xmlhttp = ajaxEngine(id);
    const link = uri;
    xmlhttp.open('GET', link, true);
    xmlhttp.setRequestHeader('X_REQUESTED_WITH', 'XMLHttpRequest');
    xmlhttp.send(null);
}
function ajaxSuccess(js) {
    eval(js);
}
function ajaxGetJS(uri, js, id) {
    const xmlhttp = ajaxEngine(id, null, js);
    const link = uri;
    const code = js;
    xmlhttp.open('GET', link, true);
    xmlhttp.setRequestHeader('X_REQUESTED_WITH', 'XMLHttpRequest');
    xmlhttp.send(null);
    // setTimeout(function() {
    //  xmlhttp.onload = demoHighCharts.init();
    //  }, 2000);
}
function ajaxQuery(uri, id, data, type) {
    const xmlhttp = ajaxEngine(id, type);
    const link = uri;
    xmlhttp.open('GET', link, true);
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send(data);
}
function ajaxPost(uri, id, data, type) {
    const xmlhttp = ajaxEngine(id, type);
    const link = uri + '/rand-' + Math.random();
    xmlhttp.open('POST', link, true);
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send(data);
}
function genPreview(id_title, id_date, id_category, id_short, id_full) {
    tinymce.triggerSave();
    const _title = gid(id_title).value;
    const _date = gid(id_date).value;
    const _category = gid(id_category).value;
    const _shortNews = tinyMCE.get(id_short).getContent();
    const _fullNews = tinyMCE.get(id_full).getContent();
    if (_title == '') {
        notif('warning', lang.info, lang.preview_title);
    } else {
        $.fancybox({
            href: '/ajax.php?do=getPreview&title=' + encodeURIComponent(_title) + '&date=' + encodeURIComponent(_date) + '&category=' + encodeURIComponent(_category) + '&shortNews=' + encodeURIComponent(_shortNews) + '&fullNews=' + encodeURIComponent(_fullNews),
            title: lang.preview,
            type: 'ajax',
            maxWidth: 880,
            maxHeight: 600,
            fitToView: false,
            width: '70%',
            height: '70%',
            autoSize: false,
            closeClick: false
        });
    }
}
function genContentPreview(id_title, id_date, id_category, id_short) {
    tinymce.triggerSave();
    const _title = gid(id_title).value;
    const _date = gid(id_date).value;
    const _category = gid(id_category).value;
    const _shortNews = tinyMCE.get(id_short).getContent();
    if (_title == '') {
        notif('warning', lang.info, lang.preview_title);
    } else {
        $.fancybox({
            href: '/ajax.php?do=getContentPreview&title=' + encodeURIComponent(_title) + '&date=' + encodeURIComponent(_date) + '&category=' + encodeURIComponent(_category) + '&&shortNews=' + encodeURIComponent(_shortNews),
            title: lang.preview,
            type: 'ajax',
            maxWidth: 880,
            maxHeight: 600,
            fitToView: false,
            width: '70%',
            height: '70%',
            autoSize: false,
            closeClick: false
        });
    }
}
function inputTags(tags, id, input) {
    arr = tags.split(',');
    if (arr[(arr.length - 1)].length > 1) {
        if (gid(id).style.display == "none") {
            showhide(id);
        }
        ajaxGet('ajax.php?do=inputTags&query=' + tags + '&input=' + input, id);
    }
}
function EditTitle(div, mod, id, subarr) {
    let text;
    const tempText = gid(div).innerHTML;
    if (subarr) {
        text = gid(subarr).innerHTML;
    } else {
        text = gid(div).innerHTML;
    }
    text = text.replace(/"/g, '&quot;');
    gid(div).innerHTML = '<input type="text" id="inputEdit_' + id + '" value="' + text + '" style="width:90%" class="EditTitle" />';
    gid('inputEdit_' + id).focus();
    gid('inputEdit_' + id).onblur = function () {
        gid(div).innerHTML = tempText;
        gid(div).onclick = function () {
            EditTitle(div, mod, id, subarr);
        }
    };
    gid('inputEdit_' + id).onkeypress = function (e) {
        e = e ? e : event;
        if (e.keyCode == 13 || e.keyCode == 10) {
            SaveTitle(mod, id, this.value);
            return false;
        }
    }
    gid(div).onclick = function () {
    }
}
function getTranslit(text, input) {
    if (text.length > 0) {
        const data = "query=" + encodeURIComponent(text);
        ajaxPost('ajax.php?do=getTranslit', input, data, 2);
    }
    else {
        gid(input).value = '';
    }
}
function getRelates(val, id) {
    if (val.length > 3) {
        AJAXEngine.setPostVar('title', encodeURI(val, true));
        AJAXEngine.sendRequest('ajax.php?do=alt_title', id)
    }
}
function getRedir(val, id) {
    if (val.length > 3) {
        AJAXEngine.setPostVar('oldurl', encodeURI(val, true));
        AJAXEngine.sendRequest('ajax.php?do=alt_redir', id)
    }
}
function getUserRelates(val, id) {
    if (val.length > 3) {
        AJAXEngine.setPostVar('nick', encodeURI(val, true));
        AJAXEngine.sendRequest('ajax.php?do=check_login', id)
    }
}
function getEmailRelates(val, id) {
    if (val.length > 3) {
        AJAXEngine.setPostVar('mail', encodeURI(val, true));
        AJAXEngine.sendRequest('ajax.php?do=check_email', id)
    }
}
function getValText(val, id) {
    if (val.length > 3) {
        AJAXEngine.setPostVar('text', encodeURI(val, true));
        AJAXEngine.sendRequest('ajax.php?do=validate_text', id)
    }
}
function getTagcode(val, id) {
    if (val.length > 3) {
        AJAXEngine.setPostVar('title', encodeURI(val, true));
        AJAXEngine.sendRequest('ajax.php?do=alt_tagcode', id)
    }
}
function getRssInform(val, id) {
    if (val.length > 3) {
        AJAXEngine.setPostVar('title', encodeURI(val, true));
        AJAXEngine.sendRequest('ajax.php?do=alt_rssinform', id)
    }
}
function getCats(val, id) {
    if (val.length > 3) {
        AJAXEngine.setPostVar('name', encodeURI(val, true));
        AJAXEngine.sendRequest('ajax.php?do=alt_cats', id)
    }
}
function getSmileIcon(val, id) {
    if (val.length > 3) {
        AJAXEngine.setPostVar('cat', encodeURI(val, true));
        AJAXEngine.sendRequest('ajax.php?do=smile_icon', id)
    }
}
function getSearch(val, id) {
    if (val.length > 2) {
        AJAXEngine.setPostVar('search', encodeURI(val, true));
        AJAXEngine.sendRequest('ajax.php?do=alt_domen', id)
    }
}