var xml_http_cf = 0;
var xml_http_img = 0;
var xml_http_pr_img = 0;
var xml_http_comment = 0;
var xml_http_album_name = 0;

var reqURL = "";
var callbackFunc = null;

/* Object statuses */
var arrLoadStatuses = new Array();
arrLoadStatuses[0] = 'Не определено';
arrLoadStatuses[1] = 'Открытие';
arrLoadStatuses[2] = 'Подождите, пожалуйста...';
arrLoadStatuses[3] = 'Получение данных...';
arrLoadStatuses[4] = 'Загрузка завершена';


/* Object init */

function initObj(obj)
{
    if (window.ActiveXObject) // If IE
    {
        xml_http_cf = new ActiveXObject("Microsoft.XMLHTTP");
        xml_http_img = new ActiveXObject("Microsoft.XMLHTTP");
        xml_http_pr_img = new ActiveXObject("Microsoft.XMLHTTP");
        xml_http_comment = new ActiveXObject("Microsoft.XMLHTTP");
        xml_http_album_name = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) 
    {
        xml_http_cf = new XMLHttpRequest();
        xml_http_img = new XMLHttpRequest();
        xml_http_pr_img = new XMLHttpRequest();
        xml_http_comment = new XMLHttpRequest();
        xml_http_album_name = new XMLHttpRequest();
    }
}

/* Update request state. Using: category filter */
function ursCF()
{
//    alert(xml_http_cf);
    window.status = 'Load status: ' + arrLoadStatuses[xml_http_cf.readyState];
    document.getElementById('request_status').innerHTML = '<b>Статус</b>: ' + arrLoadStatuses[xml_http_cf.readyState];
}

/* Update request state. Using: load image */
function ursLI()
{
//    alert(xml_http_cf);
    window.status = 'Load status: ' + arrLoadStatuses[xml_http_img.readyState];
//    document.getElementById('request_status').innerHTML = '<b>Статус</b>: ' + arrLoadStatuses[xml_http_cf.readyState];
}

function ursPRLI()
{
//    alert(xml_http_cf);
    window.status = 'Load status: ' + arrLoadStatuses[xml_http_pr_img.readyState];
    document.getElementById('main_div').innerHTML = arrLoadStatuses[xml_http_pr_img.readyState];
//    document.getElementById('request_status').innerHTML = '<b>Статус</b>: ' + arrLoadStatuses[xml_http_cf.readyState];
}

function ursSendComment()
{
//    alert(xml_http_cf);
    window.status = 'Load status: ' + arrLoadStatuses[xml_http_comment.readyState];
//    document.getElementById('request_status').innerHTML = '<b>Статус</b>: ' + arrLoadStatuses[xml_http_cf.readyState];
}

/* Callback. Handle state change.  Using: category filter*/
function hscCF()
{
//    alert('asdasdadasd');
    ursCF();
    if(xml_http_cf.readyState == 4) 
    {
        ursCF();
        if(xml_http_cf.status == 200) 
        {
            document.getElementById('programs_list').innerHTML = xml_http_cf.responseText;
//            document.getElementById('div_result').innerHTML = '';
        }
    }
}

function hscLI()
{
    
    ursLI();
//    alert('aa');

    if(xml_http_img.readyState == 4) 
    {
        ursLI();
    
        

        if(xml_http_img.status == 200) 
        {
            document.getElementById('main_div').innerHTML = xml_http_img.responseText;
            document.getElementById('div_result').innerHTML = '';
//            document.getElementById('div_comments').innerHTML = '<center>Пожалуйста, подождите...</center>';
            SendListCommentsRequest();
        }
    }
}

function hscPRLI()
{
    
    ursPRLI();
//    alert('aa');

    if(xml_http_pr_img.readyState == 4) 
    {
        ursPRLI();
        if(xml_http_pr_img.status == 200) 
        {

//            document.getElementById('divIMG').innerHTML = xml_http_pr_img.responseText;
//            document.getElementById('main_div').innerText = xml_http_pr_img.responseText;
//            alert(document.getElementById('mainAnc').innerHTML);
            document.getElementById('main_div').innerHTML = xml_http_pr_img.responseText;
            
            
            
//            document.getElementById('div_result').innerHTML = '';
            document.getElementById('div_comments').innerHTML = '<center>Загрузка коминтариев...</center>';
            SendListCommentsRequest();
        }
    }
}

function hscSendComment()
{
    ursSendComment();
    

    if(xml_http_comment.readyState == 4) 
    {
        ursSendComment();
//        alert(xml_http_comment.status);
        if(xml_http_comment.status == 200) 
        {
//            alert(xml_http_comment.responseText);
            if(xml_http_comment.responseText)
            {
                document.forms['comment'].elements['comment_text'].value = '';
                
                SendListCommentsRequest();
                document.getElementById('div_result').innerHTML = 'Спасибо, ваш комментарий добавлен... Сейчас обновим ;)';
                
            }                

//            document.getElementById('div_comments').innerHTML = xml_http_comment.responseText;
        }
    }
}

function hscCommentList()
{
    ursSendComment();
    

    if(xml_http_comment.readyState == 4) 
    {
        ursSendComment();
//        alert(xml_http_comment.status);
        if(xml_http_comment.status == 200) 
        {
//            alert(xml_http_comment.responseText);
//            if(xml_http_comment.responseText)
            document.getElementById('div_comments').innerHTML = xml_http_comment.responseText;
            document.getElementById('div_result').innerHTML = '';
                

            document.getElementById('divSend').style.display = 'block';
//            document.getElementById('div_comments').innerHTML = xml_http_comment.responseText;
        }
    }
}


/* Send request. Using: load cetegories*/
function SendCFRequest(url, id)
{
    var Cat = document.getElementById(id);
    
    xml_http_cf = null;
    initObj(xml_http_cf);
    
    

    if(!Cat)
        return;
        
    
    Cat = Cat.value;                    
    
    if(Cat == -1)
        return;
    
    
    ursCF();
    
    xml_http_cf.onreadystatechange = hscCF;
    xml_http_cf.open('GET', url + "/admin/index.php?s=programs&ev=listing_programs&ajax=1&cat_id=" + Cat, true);
    
    xml_http_cf.send(0);   
}

function SendLIRequest(url)
{
//    alert(url);

    xml_http_img = null;
    initObj(xml_http_img);
    
    
//    alert(xml_http_img);
    ursLI();
    
    
    xml_http_img.onreadystatechange = hscLI;
    xml_http_img.open('GET', url, true);
    
//    alert('send');
    xml_http_img.send(0);          
}

function SendPRLIRequest(url)
{
    
    xml_http_pr_img = null;
    initObj(xml_http_pr_img);
    
    
//    alert(xml_http_img);
    ursPRLI();
    
    
    xml_http_pr_img.onreadystatechange = hscPRLI;
    xml_http_pr_img.open('GET', url, true);
    
//    alert(url);
    xml_http_pr_img.send(0);          
}

function SendComment(url, item, img_num)
{
    xml_http_comment = null;
    initObj(xml_http_comment);
    
    var q = 'comment_text=' + document.forms['comment'].elements['comment_text'].value+
            '&item=' + item + 
            '&img_num='+img_num; 
    
    xml_http_comment.onreadystatechange = hscSendComment;
    xml_http_comment.open('POST', url, true);
    xml_http_comment.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    
    xml_http_comment.send(q);          
}

function SendListCommentsRequest(/*url, item, img_num*/)
{
    var url = siteURL + '/reportages/load_comments/' + curItem + '/' + curImgNum + '/';
    
    xml_http_comment = null;
    initObj(xml_http_comment);
//    
    xml_http_comment.onreadystatechange = hscCommentList;
    xml_http_comment.open('GET', url, true);
//    
    xml_http_comment.send(0);          
}


function ursAlbumName()
{
//    alert(xml_http_cf);
//    document.getElementById('div_result').innerHTML = 'Load status: ' + arrLoadStatuses[xml_http_album_name.readyState];
//    document.getElementById('request_status').innerHTML = '<b>Статус</b>: ' + arrLoadStatuses[xml_http_cf.readyState];
}

function hscAlbumName()
{
    ursAlbumName();
    
//    alert(xml_http_album_name.status);
    if(xml_http_album_name.readyState == 4) 
    {
        ursAlbumName();
//        alert(xml_http_comment.status);
//        alert(xml_http_album_name.status);
        if(xml_http_album_name.status == 200) 
        {
            document.getElementById('div_result').innerHTML = xml_http_album_name.responseText;
//            document.getElementById('span_album_title').innerHTML = xml_http_album_name.responseText;

//            document.getElementById('span_album_title').innerHTML = 'asdasdasd';

            //            alert(document.getElementById('album_title').enabled);
            
//            alert(xml_http_album_name.responseText);
//            return;
            
            document.getElementById('album_title').disabled = false;
            document.getElementById('album_description').disabled = false;
//            document.getElementById('album_title').value = xml_http_album_name.responseText;
            document.getElementById('album_title').value = mainTitle;
            document.getElementById('album_description').value = mainDesc;
        }
    }
}

function SendAlbumNameRequest(obj_id, obj_desc, site_url, item_id, user_type)
{
    var obj = document.getElementById(obj_id);
    var objDesc = document.getElementById(obj_desc);
    
//    var url = site_url + '/profile/photos/saveAlbum.html'
    var url = site_url + '/index.php?s=userphotos&ev=ajax_save_album_info'

    xml_http_album_name = null;
    initObj(xml_http_album_name);
    
    mainTitle = obj.value;
    mainDesc = objDesc.value;
    
    
    var q = 'name=' + obj.value + '&item_id=' + item_id + '&description=' + objDesc.value; 
    
    xml_http_album_name.onreadystatechange = hscAlbumName;
    xml_http_album_name.open('POST', url, true);
    xml_http_album_name.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    
    obj.setAttribute('disabled', 'disabled');
    objDesc.setAttribute('disabled', 'disabled');
    obj.value = 'Отправка данных...';
    objDesc.value = 'Отправка данных...';

    
//    alert('asdasdasd');
//    return;
    
    xml_http_album_name.send(q);
}

/* * * * * * * * * * * * * * * * * * */

function hscMoveImg()
{
    if(xml_http_album_name.readyState == 4) 
    {
        if(xml_http_album_name.status == 200) 
        {
            document.getElementById('div_images').innerHTML = xml_http_album_name.responseText;
            document.body.style.cursor = 'default';
            
            window.status = 'Готово';  
        }
    }    
}

function moveImg(url, item_id, img_id, direction)
{
    xml_http_album_name = null;
    initObj(xml_http_album_name);
    
    xml_http_album_name.onreadystatechange = hscMoveImg;
    xml_http_album_name.open('GET', url, true);
//  

    document.body.style.cursor = 'wait';
    window.status = 'Подождите, пожалуйста';  
    
    xml_http_album_name.send(0);  
    
}


function hscNodeTitle()
{
    if(xml_http_album_name.readyState == 4) 
    {
        if(xml_http_album_name.status == 200) 
        {      
            var str = trim(xml_http_album_name.responseText);
            onSaveComplete(str);
        }
    }
}

function saveNodeTitle(url, title, item_id, callback)
{
    xml_http_album_name = null;
    initObj(xml_http_album_name);
    
    var q = 'name=' + title + '&item_id=' + item_id; 
    
    xml_http_album_name.onreadystatechange = hscNodeTitle;
    xml_http_album_name.open('POST', url, true);
    xml_http_album_name.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    
    xml_http_album_name.send(q);    
}

function hscAlbumImg()
{
    if(xml_http_album_name.readyState == 4) 
    {
        if(xml_http_album_name.status == 200) 
        {                    
            callbackFunc(xml_http_album_name.responseText);
            document.body.style.cursor = 'default';
        }
    }
}

function getAlbumImg(url, callback)
{
    callbackFunc = callback;
    
    xml_http_album_name = null;
    initObj(xml_http_album_name);
    
    callbackFunc = callback;
    
    xml_http_album_name.onreadystatechange = hscAlbumImg;
    xml_http_album_name.open('GET', url, true);

    document.body.style.cursor = 'wait';
    
    xml_http_album_name.send(0);    
}


function hscCommentsRequest()
{
    if(xml_http_comment.readyState == 4) 
    {
        if(xml_http_comment.status == 200)                                                                                         
        {                    
            callbackFunc(xml_http_comment.responseText);
            document.body.style.cursor = 'default';
        }
    }
}

function hscDelCommentRequest()
{
    if(xml_http_comment.readyState == 4) 
    {
        if(xml_http_comment.status == 200)                                                                                         
        {                    
            callbackFunc(xml_http_comment.responseText);
            document.body.style.cursor = 'default';
        }
    }
}

function imgDelCommentRequest(id, callback, type)
{   
    var url = siteURL + '/profile/photos/image/comments/del/' + id + '/';
    
    callbackFunc = callback; 
    
    xml_http_comment = null;
    initObj(xml_http_comment);

    xml_http_comment.onreadystatechange = hscDelCommentRequest;
    xml_http_comment.open('GET', url, true);

    document.body.style.cursor = 'wait';
    xml_http_comment.send(0);    
}

function imgCommentsRequest(callback, usertype)
{
    var url = siteURL + '/profile/photos/image/comments/' + curItem + '/' + curImgNum + '/'+adm+'/';
         
    callbackFunc = callback; 
    
    xml_http_comment = null;
    initObj(xml_http_comment);

    xml_http_comment.onreadystatechange = hscCommentsRequest;
    xml_http_comment.open('GET', url, true);

    xml_http_comment.send(0);          
}
     

function hscImgSendComment()
{
    if(xml_http_comment.readyState == 4) 
    {
        if(xml_http_comment.status == 200) 
        {                    
            callbackFunc(xml_http_comment.responseText);
        }
    }
}     

     
function imgSendComment(url, callback)
{
    xml_http_comment = null;
    initObj(xml_http_comment);
    
    var q = 'comment_text=' + document.forms['comment'].elements['comment_text'].value+
            '&item=' + curItem + 
            '&img_num='+curImgNum; 
    
    callbackFunc = callback;
    
    xml_http_comment.onreadystatechange = hscImgSendComment;
    xml_http_comment.open('POST', url, true);
    xml_http_comment.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    
    document.forms['comment'].elements['comment_text'].value = 'Отправка данных...';
    document.forms['comment'].elements['comment_text'].setAttribute('disabled', 'disabled');

    xml_http_comment.send(q);    
}  
  
function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
