/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function changeLocale(locale){
    
    var history = location.href.replace(/&/g, "amp;");

    window.location = ".localization?locale=" + locale + "&history=" + history;
}

function buy(text, loginfirst, exist, contentId, login, pack){

    var newlocation = 'cabinet_edit.do?action=editContent&contentId=' + contentId;
    if(login != null){

        Sexy.confirm(text, {
            onComplete:function(returnvalue){
                if(returnvalue) {

                    CabinetAsync.hasContent(contentId, function(has){
                    
                    
                        if(has){
                            Sexy.exist(exist);
                        }else{

                            CabinetAsync.buy(contentId, pack, function(success){

                                if(success != null){
                                    Sexy.exist(success);
                                } else{

                                    if(pack){
                                        newlocation = "cabinet.do?action=mylist";
                                    }

                                    location.href = newlocation;
                                }

                            });
                        }
                    
                    });
                
                }
            }
        });
    }else{
        Sexy.login(loginfirst, {
            onComplete: function(returnvalue){
                if(returnvalue){

                    saveCookie('CART', encodeURIComponent("search.do?action=search&contentId=" + contentId));
                }
            }
        });
    }

    return false;
}

function aRenew(text, contentId, enable){

    Sexy.arenew(text, {
        onComplete:function(returnvalue){
            if(returnvalue) {

                if(enable){
                    location.href='cabinet.do?action=enableAutoRenew&contentId=' + contentId;
                }else{
                    location.href='cabinet.do?action=disableAutoRenew&contentId=' + contentId;
                }
            }
        }
    });

    return false;
}

function manualRenew(text, contentId){

    Sexy.arenew(text, {
        onComplete:function(returnvalue){
            if(returnvalue) {

                CabinetAsync.enableProfile(contentId, function(error){

                    if(error != null){
                        Sexy.addContactError(error);
                    } else{
                        location.href='cabinet.do?action=mylist';
                    }
                });
            }
        }
    });

    return false;
}

function deleteContent(text, contentId){

    Sexy.removeContent(text, {
        onComplete:function(returnvalue){
            if(returnvalue) {

                location.href='cabinet.do?action=removeContent&contentId=' + contentId;
            }
        }
    });

    return false;

}


function deleteProfile(text, contentId){

    Sexy.removeContent(text, {
        onComplete:function(returnvalue){
            if(returnvalue) {

                location.href='cabinet.do?action=remove&contentId=' + contentId;
            }
        }
    });

    return false;
}

function get_cookie(Name) {
    var search = Name + "="
    var returnvalue = "-1";
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search)
        if (offset != -1) {
            offset += search.length
            end = document.cookie.indexOf(";", offset);
            if (end == -1) end = document.cookie.length;
            returnvalue=unescape(document.cookie.substring(offset, end))
        }
    }
    return returnvalue;
}

function saveCookie(cookieName, cookieValue){

    cookieValue = cookieValue;

    document.cookie=cookieName + "=" + cookieValue;
}

function confirm(text, link){

    Sexy.confirm(text, {
        onComplete:function(returnvalue){
            if(returnvalue) {

                location.href=link;
            }
        }
    });

    return false;
}

var STATUS = 0;

function getStatus(){

    CabinetAsync.getServiceStatus(function(response){

        STATUS = response;

        statusButton();

    });
}

function changeStatus(){
    
    var mute = 0;
    
    if(STATUS == 0){
        
        mute = 1;
    }

    CabinetAsync.muteService(mute, function(response){

        STATUS = response;

        statusButton();
    })

}

function statusButton(){

    if(STATUS == 1){
        document.getElementById("svs_status").innerHTML = SERVICE_OFF;
        document.getElementById("svs_status_description").innerHTML = SERIVCE_ON_DESCRIPTION;
        document.getElementById("svs_status_img").src = 'images/mute_off.png';
    }else{
        document.getElementById("svs_status").innerHTML = SERICE_ON;
        document.getElementById("svs_status_description").innerHTML = SERVICE_OFF_DESCRIPTION;
        document.getElementById("svs_status_img").src = 'images/mute.png';
    }
}