
var xhr = false;
var xhrrsp = false;

function initAjaxObj(){
    try { xhr = new ActiveXObject('Msxml2.XMLHTTP');} catch (e){
        try { xhr = new ActiveXObject('Microsoft.XMLHTTP'); } catch (e2){
          try { xhr = new XMLHttpRequest(); } catch (e3) { xhr = false; }
        }
     }
     return xhr;
}

function _ajaxReq(type, post, data, handlersp){
    if(xhr){
       if(type=='GET'){post = post + '?' + data; data = null;}
       xhr.open(type, post, true);
       if(handlersp)xhr.onreadystatechange = handleResponse;
       if(type=='POST'){xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");}
       xhr.send(data);
    }
}

function ajaxReq(type, post, data, rsp){
     if (initAjaxObj()) {
         _ajaxReq(type,post,data,rsp);
     }
}

function ajaxPost(type,post,data){
    ajaxReq(type,post,data,0);
}

function ajaxGet(type,post,data){
    ajaxReq(type,post,data,1);
}

function postComment(p1,p2,p4,cnt,ln,p6) {
    document.getElementById('i'+p1).style.border="0px";
    document.getElementById('i'+p2).style.display="none";
    document.getElementById('i'+p4).style.border="0px";
    data = 'cnt=' + cnt + '&ln=' + ln + apndData('cm',p1) + apndData('nm',p4) + apndData('ms',p6);
    ajaxPost('POST','/post/comments.php',data);
}

function getData(lbl,id){
    if(!id||isNaN(id)){
        return lbl + '=' + id;
    }
    x=document.getElementById('i'+id).value;
    return lbl + '=' + encodeURIComponent(x);
}

function apndData(lbl,id){
    res = getData(lbl,id);
    if(res!='')return '&'+res;
    return '';
}

function  handleResponse()  {
    if(xhr&&xhrrsp&&xhr.readyState == 4){
        rsp = xhr.responseText;
        document.getElementById(xhrrsp).innerHTML = rsp;
        document.getElementById(xhrrsp).style.visibility = 'visible';
        document.getElementById(xhrrsp).style.display = 'block';
    }
}

function rate(v,l,s) {
    ln = document.getElementById('alikes');
    dln = document.getElementById('adislikes');
    if(l==1)ln.value=parseInt(ln.value)+l;
    if(l==-1)dln.value=parseInt(dln.value)-l;
    document.getElementById('btnlike').disabled=true;
    document.getElementById('btndislike').disabled=true;
    data = 'v=' + encodeURIComponent(v) + '&l=' + encodeURIComponent(l) + '&s=' + encodeURIComponent(s);
    ajaxPost('POST', '/post/rate.php',data);
}

function login(el1, el2, rspt) {
    data = getData('u',el1) + apndData('p',el2);
    xhrrsp = 'i'+rspt;
    ajaxGet('POST','/post/login.php',data);
}

function sendRequest(e,n1,n2,sbj,xinfo,txt,rsp) {
    data = getData('e',e)+apndData('n1',n1)+apndData('n2',n2)+apndData('sbj',sbj)+apndData('xinfo',xinfo)+apndData('txt',txt);
    xhrrsp = 'i'+rsp;
    ajaxGet('POST','/post/sendrequest.php',data);
}

function clearAll(){
    xhrrsp = false;
    xhr = false;
}

function registerContent(v, t, s){
    data = 'type=video&subtype=' + t + '&id=' + v + '&st=' + s;
    ajaxPost('POST', '/post/register.php',data);
}

function fl(u, n) {
    parent[n].location.href = u;
    return 0;
}





