$(document).ready(function(){
    razmessage();
});

function razmessage(){
  $("#message-warning").hide();
  $("#message-warning-custom").hide();
  $("#message-error").hide();
  $("#message-success").hide();
}

function close_message_warning(){
    $("#message-warning").slideUp("slow");
}

function close_message_warning_custom(){
    $("#message-warning-custom").slideUp("slow");
}

function close_message_error(){
    $("#message-error").slideUp("slow");
}

function close_message_success(){
    $("#message-success").slideUp("slow");
}

function processing_show(){
    $("#processing").html("<div class='progress progress-info progress-striped active'><div class='bar' style='width: 100%;'>&nbsp;&nbsp;Processing...&nbsp;&nbsp;</div></div>");
    $("#processing").show();
}

function processing_hide(){
    $("#processing").hide();
}

function check_data(){
   if(document.getElementById("sponsor").value==="0")return false;
   if(document.getElementById("vertical").value==="0")return false;
   if(document.getElementById("sid").value==="")return false;
   if(document.getElementById("name").value==="")return false;
   if(document.getElementById("page_link").value==="")return false;
   if(document.getElementById("unsub_link").value==="")return false;  
   return true;
}

function clear_data(){
   document.getElementById("sponsor").value="0";
   document.getElementById("sid").value="";
   document.getElementById("name").value="";
   document.getElementById("page_link").value="";
   document.getElementById("unsub_link").value="";
}

function add()
{
    if(check_data())
    {
        processing_show();
        $.ajax
        ({
            url: 'scripts.php?action=add',
            type: 'post',
            data: 
            {
                id_sponsor  :   document.getElementById("sponsor").value,
                id_vertical :   document.getElementById("vertical").value,
                sid         :   document.getElementById("sid").value,
                country         :   document.getElementById("country").value,
                fname         :   document.getElementById("fname").value,
                subject         :   document.getElementById("subject").value,
                name        :   document.getElementById("name").value,
                page_link   :   document.getElementById("page_link").value,
                unsub_link  :   document.getElementById("unsub_link").value
            },
            success: function(data) 
            {
                var str=data.replace(/(\r\n|\n|\r)/gm,"");
                if(str==="0")
                { 
                    $("#message-success").slideDown("slow");
                    setTimeout(function(){close_message_success();},3000);
                    clear_data();
                }
                if(str==="1")
                { 
                    $("#message-custom").text("Offer SID already in use for this Sponsor");
                    $("#message-warning-custom").slideDown("slow");
                    setTimeout(function(){close_message_warning_custom();},3000);
                }
                if(str==="2")
                { 
                    $("#message-error").slideDown("slow");
                    setTimeout(function(){close_message_error();},3000);
                }
                processing_hide();
            },
            error: function() 
            {
                $("#message-error").slideDown("slow");
                setTimeout(function(){close_message_error();},3000);
                processing_hide();
            }
            
        }); 
    }else{
        $("#message-warning").slideDown("slow");
        setTimeout(function(){close_message_warning();},3000);
    }
}



function refresh()
{
    processing_show();
    $.ajax
    ({
        url     :   'scripts.php?action=show',
        type    :   'get',
        data    : 
        {
            id_sponsor  : document.getElementById("sponsor").value,
            id_vertical : document.getElementById("vertical").value,
            country : document.getElementById("country").value
        },
        success: function(data) 
        {
            var list        =   JSON.parse(data);
            var activeon    =   '';
            var activeoff   =   '';
            $('#show').dataTable().fnClearTable();
            for (var i = 0; i < list.length; i++) 
            {
                if(list[i].active==1)
                {
                    activeon="btn-success disabled";
                    activeoff="btn-default";
                }
                else
                {
                    activeon="btn-default";
                    activeoff="btn-warning disabled";
                }
                $('#show').dataTable().fnAddData
                ([ 
                    list[i].id,
                    list[i].sid,
                    list[i].name,
                    list[i].suppression_status,
                    "<div class='btn-group btn-toggle'><button id='active-on-"+list[i].id+"' class='btn btn-xs "+activeon+"' onclick='active_on("+list[i].id+");'>ON</button><button id='active-off-"+list[i].id+"' class='btn btn-xs "+activeoff+"'' onclick='active_off("+list[i].id+");'>OFF</button></div>",
                    "<i class='icon-edit icon-white' title='Edit' style='cursor: pointer;' onclick="+"location.href='edit.php?id_offer="+list[i].id+"'"+"></i>&nbsp;&nbsp;\n\
                    <i class='icon-download icon-white' title='Upload Suppression List' style='cursor: pointer;' onclick='upload_supression_list("+list[i].id+");'></i>\n\
                    &nbsp;&nbsp;<i class='icon-remove-circle icon-white' title='Delete' style='cursor: pointer;' onclick="+"location.href='delete.php?id_offer="+list[i].id+"'"+"></i>"
                ]);
            }
            processing_hide();
        },
        error: function() 
        {
            $("#message-error").slideDown("slow");
            setTimeout(function(){close_message_error();},3000);
            processing_hide();
        }
    });
}

function set(id){
    if(check_data() && id!=null){
        processing_show();
        $.ajax({
            url: 'scripts.php?action=set',
            type: 'post',
            data: {
                id: id,
                id_sponsor: document.getElementById("sponsor").value,
                id_vertical :   document.getElementById("vertical").value,
                sid: document.getElementById("sid").value,
                country: document.getElementById("country").value,
                fname: document.getElementById("fname").value,
                subject: document.getElementById("subject").value,
                name: document.getElementById("name").value,
                page_link : document.getElementById("page_link").value,
                unsub_link : document.getElementById("unsub_link").value
            },
            success: function(data) {
                var str=data.replace(/(\r\n|\n|\r)/gm,"");
                if(str==="0"){ 
                    $("#message-success").slideDown("slow");
                    setTimeout(function(){close_message_success()},3000);
                }
                if(str==="1"){ 
                    $("#message-custom").text("Offer SID already in use for this Server");
                    $("#message-warning-custom").slideDown("slow");
                    setTimeout(function(){close_message_warning_custom();},3000);
                }
                if(str==="2"){ 
                    $("#message-error").slideDown("slow");
                    setTimeout(function(){close_message_error();},3000);
                }
                processing_hide();
            },
            error: function() {
                $("#message-error").slideDown("slow");
                setTimeout(function(){close_message_error();},3000);
                processing_show();
            }
            
        }); 
    }else{
        $("#message-warning").slideDown("slow");
        setTimeout(function(){close_message_warning();},3000);
    }
}

function active_on(id){
    processing_show();
    $.ajax({
        url: 'scripts.php?action=set_active_on',
        type: 'get',
        data: {
            id: id
        },
        success: function(data) {
            $("#active-off-"+id).removeClass("btn-warning disabled").addClass("btn-default");
            $("#active-on-"+id).addClass("btn-success disabled");
            $("#message-success").slideDown("slow");
            setTimeout(function(){close_message_success();},3000);
            processing_hide();
        },
        error: function() {
            $("#message-error").slideDown("slow");
            setTimeout(function(){close_message_error();},3000);
            processing_hide();
        }
    });
    
}

function active_off(id){
    processing_show();
    $.ajax({
        url: 'scripts.php?action=set_active_off',
        type: 'get',
        data: {
            id: id
        },
        success: function(data) {
            $("#active-on-"+id).removeClass("btn-success disabled").addClass("btn-default");
            $("#active-off-"+id).addClass("btn-warning disabled");
            $("#message-success").slideDown("slow");
            setTimeout(function(){close_message_success();},3000);
            processing_hide();
        },
        error: function() {
            $("#message-error").slideDown("slow");
            setTimeout(function(){close_message_error();},3000);
            processing_hide();
        }
    });
}

function show_sponsors(){
    html= "<option value='0' disabled=''>Waiting for extracting Sponsors...</option>";
    $("#sponsor").html(html);
    $.ajax({
        url: 'scripts.php?action=get_sponsors',
        type: 'get',
        success: function(data) {
            var list = JSON.parse(data);
            var html = "<option value='0'>Select here...</option>";
            for (var i = 0; i < list.length; i++) {
                html += "<option value='"+list[i].id+"'>"+list[i].name+"</option>";
            }
            $("#sponsor").html(html);
        }
    });
}

function select_sponsor(id){
    html= "<option value='0' disabled=''>Waiting for extracting Sponsors...</option>";
    $("#sponsor").html(html);
    $.ajax({
        url: 'scripts.php?action=get_sponsors',
        type: 'get',
        success: function(data) {
            var list = JSON.parse(data);
            var html = "<option value='0'>Select here..</option>";
            for (var i = 0; i < list.length; i++) {
                html += "<option value='"+list[i].id+"'>"+list[i].name+"</option>";
            }
            $("#sponsor").html(html);
            $('#sponsor option[value=' + id + ']').attr('selected', 'selected');
        }
    });
}

function select_sponsor_2(id){
    html= "<option value='0' disabled=''>Waiting for extracting Sponsors...</option>";
    $("#sponsor").html(html);
    $.ajax({
        url: 'scripts.php?action=get_sponsors',
        type: 'get',
        success: function(data) {
            var list = JSON.parse(data);
            var html = "<option value='0'>Select here..</option>";
            for (var i = 0; i < list.length; i++) {
                html += "<option value='"+list[i].id+"'>"+list[i].name+"</option>";
            }
            $("#sponsor").html(html);
            $('#sponsor option[value=' + id + ']').attr('selected', 'selected');
            refresh();
        }
    });
}

function get_verticals()
{
    html= "<option value='0' disabled=''>Waiting for extracting Verticals...</option>";
    $("#vertical").html(html);
    $.ajax
    ({
        url     :   'scripts.php?action=get_verticals',
        type    :   'get',
        success :   function(data) 
        {
            var list    = JSON.parse(data);
            var html    = "<option value='0'>Select here...</option>";
            for (var i = 0; i < list.length; i++) 
            {
                html += "<option value='"+list[i].id_vertical+"'>"+list[i].name_vertical+"</option>";
            }
            $("#vertical").html(html);
        }
    });
}

function select_vertical(p_id_vertical)
{
    html= "<option value='0' disabled=''>Waiting for extracting Verticals...</option>";
    $("#sponsor").html(html);
    $.ajax
    ({
        url: 'scripts.php?action=get_verticals',
        type: 'get',
        success: function(data) 
        {
            var list = JSON.parse(data);
            var html = "<option value='0'>Select here..</option>";
            for (var i = 0; i < list.length; i++) 
            {
                html += "<option value='"+list[i].id_vertical+"'>"+list[i].name_vertical+"</option>";
            }
            $("#vertical").html(html);
            $('#vertical option[value=' + p_id_vertical + ']').attr('selected', 'selected');
        }
    });
}

function upload_supression_list(p_id_offer)
{
    processing_show();
    
    $.ajax
    ({
        url: './suppression/download_suppression_file.php?id_offer='+p_id_offer,
        type: 'get',
        success: function(data) 
        { 
            if(data)
            {
                alert(data);
                
				var tab = data.split("___");
                switch (tab[0]) 
                {
                    case 'STATIC_LINK':
                        var urlSupressionFile = tab[1];
                        var urlajax =   './suppression/download_suppression_file.php?id_offer='+p_id_offer+'&url='+urlSupressionFile;
                        
                        $.ajax
                        ({
                            url: urlajax,
                            type: 'get',
                            success: function(data) 
                            {
                                if(data)
                                {
                                    processing_hide();
                                    refresh();
                                    alert(data+'\n\n\n'+'Suppression File downloaded successfully !');
                                }
                            }
                        });
                        break;
                    
                    case 'OPTIZMO':
                        
                        alert(tab[0]+'\n\n\n'+'Suppression File downloaded successfully !');
						refresh();
						break;
                        
                    case 'EZEPO':
                        var urlSupressionFile = tab[1];
                        window.open(urlSupressionFile, '_blank');
						alert('This offer use EZEPO Compliance Plateform : \n\n You have to download manually the Suppression File');
                        break;
                    
                    case 'UNSUB_CENTRAL':
                        var urlSupressionFile = tab[1];
                        window.open(urlSupressionFile, '_blank');
                        alert('This offer use UnsubCentral Compliance Plateform : \n\n You have to download manually the Suppression File');
                        break;
						
					case 'BOX_PLATEFORM':
                        var urlSupressionFile = tab[1];
                        window.open(urlSupressionFile, '_blank');
                        alert('This offer use Box Drive Plateform : \n\n You have to download manually the Suppression File');
                        break;
						
						
                        
						//copier - coller here
                    case 'UNKNOWN':
                        alert('This offer use UNKNOWN (not EZEPO / not UnsubCentral / not Optizmo / not Direct Link) Compliance Plateform : \n\n You have to download manually the Suppression File');
                        break;
                }
				processing_hide();
				
            }
        },
        error: function() 
        {
            $("#message-error").slideDown("slow");
            setTimeout(function(){close_message_error();},3000);
            processing_hide();
        }
    });
}