﻿var AjaxCallback = function (successFunction, argumentArray)
{
    this.success = function (response) 
    {
        if (successFunction) 
        {
            successFunction(response); 
        }
    };
    
    this.failure = function (response) 
    {
        alert(response.responseText) 
    };

    this.argument = argumentArray;
}

function checkApplicationError(response)
{
    var isError = false;

    if (response.getResponseHeader["exit-code"] && ( 1 == parseInt(response.getResponseHeader["exit-code"])))
    {
        isError = true;
    }
    
    return isError;
}

function checkConfirm(response)
{
    var isConfirm = false;
    if (response.getResponseHeader["exit-code"] && ( 2 == parseInt(response.getResponseHeader["exit-code"])))
    {
        isConfirm = true;
    }
    
    return isConfirm;
}

var Campaigns = {
    getCampaign: function (sort, pageIndex, itemCount)
    {
        var transaction = YAHOO.util.Connect.asyncRequest(
            'GET', 
            '/AjaxHandlers/CampaignBrowseHandler.aspx?i='+itemCount+'&s='+sort+'&p='+pageIndex+'&r=' + (Math.floor(Math.random()*1000000)), 
            { success: function(response) { Campaigns.getCampaignResult(response) } }
        ); 
    },
    
    getCampaignResult: function (response)
    {
        if (checkApplicationError(response))
        {
            alert(response.responseText);
            return;
        }    
        $("campaignsPanelDiv").innerHTML = response.responseText;  
        setAsStopped();  
    }
}

var Subscription = {
    subscribe: function (guid)
    {
        var transaction = YAHOO.util.Connect.asyncRequest(
            'GET', 
            '/AjaxHandlers/SubscriptionHandler.aspx?command=subscribe&g='+guid+'&r=' + (Math.floor(Math.random()*1000000)), 
            { success: function(response) { Subscription.subscriptionResult(response) } }
        ); 
    },
    
    subscriptionResult: function (response)
    {
        if (checkApplicationError(response))
        {
            alert(response.responseText);
            CurrentCampaign.getCurrent();
            return;
        }
        else if (checkConfirm(response)){
             confirm(response.responseText.split('|')[0], Subscription.rbtSubsriptionDelAndSet, response.responseText.split('|')[1]);
              //Ext.MessageBox.confirm('Confirm', alert('aa'), Subscription.rbtSubsriptionDelAndSet, response.responseText.split('|')[1]);
        }
    },
    
    rbtSubsriptionDelAndSet: function (r)
    {
        var guid = this;
        if ("yes" == r)
        {
            var transaction = YAHOO.util.Connect.asyncRequest(
            'GET', 
            '/AjaxHandlers/SubscriptionHandler.aspx?command=subscribe&dbs=yes&g='+guid+'&r=' + (Math.floor(Math.random()*1000000)), 
            { success: function(response) { Subscription.subscriptionResult(response) } }
        ); 
        }
    },  
    
    del: function (r)
    {
        if('yes'== r)
        {
            var transaction = YAHOO.util.Connect.asyncRequest(
                'GET', 
                '/AjaxHandlers/SubscriptionHandler.aspx?command=del&r=' + (Math.floor(Math.random()*1000000)), 
                { success: function(response) { Subscription.delResult(response) } }
            ); 
        }
    },
    
    delResult: function (response)
    {
        if (checkApplicationError(response))
        {
            alert(response.responseText);
            CurrentCampaign.getCurrent();
            return;
        }
    }    
}


var BlackList = {
    add:function(companyId,companyName)
    {
        var transaction = YAHOO.util.Connect.asyncRequest(
            'GET', 
            '/AjaxHandlers/BlackListHandler.aspx?command=add&c='+companyId+'&r=' + (Math.floor(Math.random()*1000000)), 
            { success: function(response) { BlackList.addResult(response,companyId, companyName) } }
        ); 
    },
    
    remove:function(companyId, companyName)
    {
        var transaction = YAHOO.util.Connect.asyncRequest(
            'GET', 
            '/AjaxHandlers/BlackListHandler.aspx?command=remove&c='+companyId+'&r=' + (Math.floor(Math.random()*1000000)), 
            { success: function(response) { BlackList.removeResult(response,companyId,companyName) } }
        ); 
    
    },
    
    addResult:function(response,companyId, companyName)
    {
        if (checkApplicationError(response))
        {
            alert(response.responseText);
            return;
        }
        else
        {
            $('trc'+companyId).style.display = 'none';
            
            var tbl = document.getElementById('bannedCompaines');
            var row = tbl.insertRow(tbl.rows.length);
            row.id = 'trb'+companyId;
            
            var cellLeft = row.insertCell(0);
            cellLeft.className="listrow1";
            cellLeft.appendChild(document.createTextNode(companyName.replace('%20',' ')));

            var cellRight = row.insertCell(1);
            cellRight.className="listrow1";
            cellRight.innerHTML='<a href=# onclick=BlackList.remove('+companyId+',"'+companyName.replace(' ','%20')+'")>Engeli kaldır</a>';
            
            alert(response.responseText);
            return;
        }
    },
    
    removeResult:function(response,companyId,companyName)
    {
        if (checkApplicationError(response))
        {
            alert(response.responseText);
            return;
        }
        else
        {
            $('trb'+companyId).style.display = 'none';
            
            var tbl = document.getElementById('clearCompaines');
            var row = tbl.insertRow(tbl.rows.length);
            row.id = 'trc'+companyId;
            
            var cellLeft = row.insertCell(0);
            cellLeft.className="listrow2";
            cellLeft.appendChild(document.createTextNode(companyName.replace('%20',' ')));

            var cellRight = row.insertCell(1);
            cellRight.className="listrow2";
            cellRight.innerHTML='<a href=# onclick=BlackList.add('+companyId+',"'+companyName.replace(' ','%20')+'")>Engelle</a>';
            
            alert(response.responseText);
            return;
        }
    }
    
}
var CurrentCampaign = {
    getCurrent:function()
    {
       var transaction = YAHOO.util.Connect.asyncRequest(
            'GET', 
            '/AjaxHandlers/UserActionHandler.aspx?command=currentCampaign&r=' + (Math.floor(Math.random()*1000000)), 
            { success: function(response) { CurrentCampaign.getCurrentResult(response) } }
        ); 
    },
    getCurrentResult:function(response)
    {
        if (!checkApplicationError(response))
        {
            
            var text = 'Dinlettiğin ton : ' + (response.responseText == '' ? "<b style='color:#FFC100;font-size: 13px'>Şu anda hic bir şey dinletmiyorsun.</b>" : "<span style='color:#FFC100;font-weight:bold;font-size: 13px'>" + response.responseText.split('|')[0] + "</span>.<br/> Ton değişimi için kalan süre: <span style=\"font-size: 13px\">" + response.responseText.split('|')[1] + ". <a style='color:#FFC100;font-weight:bold;cursor:pointer;' onclick='confirm(\"Şu anda çalan reklam müziğini iptal etmek istediğinden emin misin?\", Subscription.del)' href='#'>[İptal et]</a></span>");
            var currentToneName = response.responseText;
            
            $('top-menu-login-b1').innerHTML = text;
            return;
        }
    }
}

var RbtInbox = {

   check:function()
    {
       var transaction = YAHOO.util.Connect.asyncRequest(
            'GET', 
            '/AjaxHandlers/RBTToneBoxHandler.aspx?command=check&r=' + (Math.floor(Math.random()*1000000)), 
            { success: function(response) { RbtInbox.checkResult(response) } }
        ); 
    },
    checkResult:function(response)
    {
        if (checkApplicationError(response))
        {
           return;
        }
        else 
        {
            var response = response.responseText.split('|');
            if(1 == response[0]){
                var mesgToConfirm = "ÇalarkenDinlet ayarlarında yaptığın değişikler nedeniyle şu an TonlaKazan'dan ödül kazanamıyorsun.<br/><br/>Daha önce dinlettiğin <b>" + response[1] +"</b>  tonunu tekrar atayıp, TonlaKazan'dan kazanmaya devam etmek için aşağıdaki butona tıkla.<br/><br/>";
                Ext.MessageBox.buttonText.yes = response[1] + " reklam müziğini tekrar yükle";
                Ext.MessageBox.buttonText.no = "Böyle kalsın";
                confirm(mesgToConfirm, RbtInbox.setTone); 
                Ext.MessageBox.buttonText.yes ="Tamam";       
                Ext.MessageBox.buttonText.no = "İptal";   
            }
            else if(2 == response[0]){
                var mesgToConfirm = "ÇalarkenDinlet ayarlarında yaptığın değişikler nedeniyle şu an TonlaKazan'dan ödül kazanamıyorsun.<br/><br/>Daha önce dinlettiğin <b>" + response[1] +"</b>  tonunu tekrar atayıp, TonlaKazan'dan kazanmaya devam etmek için aşağıdaki butona tıkla.<br/><br/>";
                Ext.MessageBox.buttonText.yes = response[1] + " reklam müziğini yükle";
                Ext.MessageBox.buttonText.no = "Böyle kalsın";
                confirm(mesgToConfirm, RbtInbox.setTone);           
                Ext.MessageBox.buttonText.yes ="Tamam";       
                Ext.MessageBox.buttonText.no = "İptal";                   
            }
            else if(3 == response[0]){
                var mesgToConfirm = "ÇalarkenDinlet ayarlarında yaptığın değişikler nedeniyle şu an TonlaKazan'dan ödül kazanamıyorsun.<br/><br/>Daha önce dinlettiğin <b>" + response[1] +"</b>  tonunu tekrar atayıp, TonlaKazan'dan kazanmaya devam etmek için aşağıdaki butona tıkla.<br/><br/>";
                Ext.MessageBox.buttonText.yes = response[1] + " reklam müziğini tekrar yükle";
                Ext.MessageBox.buttonText.no = "Böyle kalsın";
                confirm(mesgToConfirm, RbtInbox.setTone); 
                Ext.MessageBox.buttonText.yes ="Tamam";       
                Ext.MessageBox.buttonText.no = "İptal";                             
            }
            
        }
    },
    
    setTone: function (response)
    {
        if('yes'== response)
        {
            var transaction = YAHOO.util.Connect.asyncRequest(
                'GET', 
                '/AjaxHandlers/RBTToneBoxHandler.aspx?command=set&r=' + (Math.floor(Math.random()*1000000)), 
                { success: function(response) { RbtInbox.setToneResult(response) } }
            ); 
        }
    },
    setToneResult: function (response)
    {
        alert(response.responseText);
    }
		
}

var Membership = {
    cancelConfirm : function(){
        Ext.MessageBox.buttonText.yes = "Evet";
        Ext.MessageBox.buttonText.no = "Hayır";    
        confirm("Tonlakazan üyeliğini iptal etmek istiyor musun?", Membership.cancel);
    },
    cancel : function(response){
       if('yes'== response){
            var transaction = YAHOO.util.Connect.asyncRequest(
                'GET', 
                '/AjaxHandlers/UserActionHandler.aspx?command=cancelMembership&r=' + (Math.floor(Math.random()*1000000)), 
                { success: function(response) { Membership.cancelResult(response) } }
            );     
        }
    
    },
    cancelResult : function(response){
        window.setInterval('Membership.redirect()',  5 * 1000);
        alert(response.responseText);
        return;
    },
    redirect : function(){
        location.href= '/';
    },
    logOutAlert:function()    {
        var mesgToConfirm = "SüperŞifre kullanan tüm uygulamalardan çıkıyorsunuz. Şifre gerektiren yeni bir işlem yapmanız gerektiğinde <span style='color:#F2AF51;font-weight:bold'>SüperŞifrenizi tekrar girmeniz gerekecektir.</span><br/><br/> Devam etmek istiyor musunuz?";
        Ext.MessageBox.buttonText.yes = "Evet";
        Ext.MessageBox.buttonText.no = "Hayır";
        confirm(mesgToConfirm, Membership.logOutResponse); 
        Ext.MessageBox.buttonText.yes ="Tamam";       
        Ext.MessageBox.buttonText.no = "İptal";  
    },        
    logOutResponse : function(response)
    {
        if('yes'==response){
            window.location.href= "/Logoff.aspx";
        }
    }
}

var SponsoredSms = {
    forceLoginOrSignup:function()    {
        var mesgToConfirm = 'TonlaKazan üyeleri Ayda 150 SMS\'i bedava gönderebiliyor. <br/><br/>Üye olmak ya da giriş yapmak için "Devam"a tıkla';
        confirm(mesgToConfirm, SponsoredSms.forceLoginOrSignupReponse); 
    },        
    forceLoginOrSignupReponse : function(response)
    {
        if('yes'==response){
            window.location.href= "http://sso.tonlakazan.com/SSOProxy.aspx";
        }
    }
}