$(document).ready(function() {

$('#selectpage').change(function()
{
	$('#urldeeplink').val(urldecode($('#selectpage').val()));
});

    $('#deeplinks-errors').hide();
    $('#deeplinks-success').hide();
   
    $('#deeplinks-errors').click(function()
    {
        $('#deeplinks-errors').hide();
    });

    $('#deeplinks-success').click(function()
    {
        $('#deeplinks-success').hide();
    });

 $("#edit-deeplink").click(function()
                    {
                        hideDeeplinkMessages();
                        var urlwebsite = $('#website_url').val();
                        var name = $('#textanchor').val();
                        var type = $('#typelink').val();
                        var targerwebsite = $('#targerwebsite').val();
                        var selectpage = $('#selectpage').val();
                        var url = $('#urldeeplink').val();
                        var nofollow = "";
                        if($('#nofollow').attr("checked")==true)
                        {
                            nofollow =1;
                        }else
                        {
                            nofollow =0;
                        }
                        var id = $('#id').val();
                        name = trim(name);

                        if(name=="")
                        {
                            $("#deeplinks-errors").show();
                            $("#anchor-empty-error").show();
                            return;
                        }

                        if(type=="int")
                        {
                            selectpage = trim(selectpage);
                            if(selectpage=="")
                            {
                                $("#deeplinks-errors").show();
                                $("#selectpage-empty-error").show();
                                return;
                            }
                        }
                        id = $("#id").val();
                        if(id>0)
                        {
                            $.ajax({
                                type : 'POST',
                                url:''+urlwebsite+'sys/backend_ajax/editdeeplink/',
                                data: ({
                                    name : name,
                                    type : type,
                                    targerwebsite : targerwebsite,
                                    selectpage : selectpage,
                                    url : url,
                                    nofollow : nofollow,
                                    id : id
                                }),
                                    success: function(response){
                                    if(response=="exists")
                                    {
                                        $("#deeplinks-errors").show();
                                        $("#anchor-exists-error").show();
                                    }
                                    else if(response=="page error")
                                    {
                                        $("#deeplinks-errors").show();
                                        $("#page-empty-error").show();
                                    } else if(response=="invalid url")
                                     {
                                     $("#deeplinks-errors").show();
                                     $("#invalid-url-error").show();
                                     }
                                    else if(response=="empty url")
                                    {
                                     $("#deeplinks-errors").show();
                                     $("#empty-url-error").show();
                                    }
                                    else if(response=="anchor only letters")
                                    {
                                     $("#deeplinks-errors").show();
                                     $("#anchor-regex-error").show();
                                    }
                                    else
                                    {
                                        $("#deeplinks-success").show();
                                        $("#deeplink-edited").show();


                                        $("#add-deeplink").css('display','block');
                                        $("#edit-deeplink").css('display','none');

                                        var arr = response.split("~");
                                        $("#mandeepl-"+arr[0]).empty();
                                        $("#mandeepl-"+arr[0]).html(arr[1]);
                                        clearForm();
                                    }
                                }
                            });
                        }
                    });

});


function clearForm()
{
    $('#textanchor').val("");
    $('#typelink').val("int");
    $('#selectpage').val("select page");
    $('#urldeeplink').val("http://");
    $('#nofollow').attr("checked",false);
    $('#for_internal_type').css('display','block');
    $('#urldeeplink').attr('readonly','readonly');
}

function hideDeeplinkMessages()
{
    $("#deeplinks-errors").hide();
    $("#anchor-empty-error").hide();
    $("#anchor-exists-error").hide();
    $("#anchor-regex-error").hide();
    
    $("#selectpage-empty-error").hide();
    $("#page-empty-error").hide();
    $("#empty-url-error").hide();
    $("#invalid-url-error").hide();
    

    $("#deeplinks-success").hide();
    $("#deeplink-added").hide();
    $("#deeplink-edited").hide();
}

function trim(str)
{
    var ltrim = /^\s+/g;
    var rtrim = /\s+$/g;

    str = str.replace(ltrim,'');
    str = str.replace(rtrim,'');
    return str;
}

function addDeeplink(){
    hideDeeplinkMessages();

    var urlwebsite = $('#website_url').val();
    var name = $('#textanchor').val();
    var type = $('#typelink').val();
    var targerwebsite = $('#targerwebsite').val();
    var selectpage = $('#selectpage').val();
    var url = $('#urldeeplink').val();
    var nofollow = "";
    if($('#nofollow').attr("checked")==true)
    {
        nofollow =1;
    }else
    {
        nofollow =0;
    }

    name = trim(name);
    if(name=="")
    {
        $("#deeplinks-errors").show();
        $("#anchor-empty-error").show();
     
        return;
    }
        
    if(type=="int")
    {
        selectpage = trim(selectpage);
        
        if(selectpage=="")
        {
            $("#deeplinks-errors").show();
            $("#selectpage-empty-error").show();
            return;
        }
    }

    $.ajax({
        type : 'POST',
        url:''+urlwebsite+'sys/backend_ajax/adddeeplink/',
        data: ({
            name : name,
            type : type,
            targerwebsite : targerwebsite,
            selectpage : selectpage,
            url : url,
            nofollow : nofollow           
        }),
          //dataType: 'jsonp',
        success: function(response){
           hideDeeplinkMessages();
             
             if(response=="exists")
            {
                $("#deeplinks-errors").show();
                $("#anchor-exists-error").show();
            }else
                if(response=="page error")
            {
                $("#deeplinks-errors").show();
                $("#page-empty-error").show();
            }
            else if(response=="invalid url")
                {
                 $("#deeplinks-errors").show();
                 $("#invalid-url-error").show();
                }
                else if(response=="empty url")
                {
                 $("#deeplinks-errors").show();
                 $("#empty-url-error").show();
                }
                else if(response=="anchor only letters")
                {
                 $("#deeplinks-errors").show();
                 $("#anchor-regex-error").show();
                }
            else
            {
                $("#deeplinks-success").show();
                $("#deeplink-added").show();
                $('#mandeeplcontainer').prepend(response);
                clearForm();
            }
            return;
        }
    });

}


function fiilDeeplinkForm(id)
{
    
    var urlwebsite = $('#website_url').val();
    if(id>0)
    {
        $.ajax({
            type : 'POST',
            url:''+urlwebsite+'sys/backend_ajax/fiildeeplinkform/',
            data: ({
                id : id
            }),
            
            success: function(response){
                if(response)
                {
                    var arr = response.split("~");
                    
                    $("#id").val(arr[0]);
                    $("#textanchor").val(arr[1]);
                    $("#selectpage").val(arr[2]);
                    $("#typelink").val(arr[3]);


                    if($("#typelink").val()=="int")
                    {
                        $('#for_internal_type').css('display','block');
                        $('#urldeeplink').attr('readonly','readonly');
                    }else
                    {
                        $('#for_internal_type').css('display','none');
                        $('#nofollow').attr('checked',true);
                        document.getElementById('urldeeplink').removeAttribute('readonly');
                    }

                    if(arr[4]==1)
                    {
                        $("#nofollow").attr("checked",true);
                        $("#nofollow").val(1);
                    }else
                    {
                        $("#nofollow").attr("checked",false);
                        $("#nofollow").val(0);
                    }


                    $("#urldeeplink").val(arr[5]);
                    $("#action").val("editDeeplink");
                    $("#add-deeplink").css('display','none');
                    $("#edit-deeplink").css('display','block');
                }
        
            }
        });
    }
}


function deleteDeeplink(id)
{
    var urlwebsite = $('#website_url').val();
    if(confirm('Do you really want to delete this deeplink?'))
    {
        $.ajax({
            type : 'POST',
            url:''+urlwebsite+'sys/backend_ajax/deletedeeplink/',
            data: ({
                id : id
            }),
            dataType: 'json',
            success: function(id){
                $('#mandeepl-'+id).remove();
            }
        });
    }
}

function changeTypeDeeplink(type)
{
    
    switch(type)
    {
        case 'int':
            $('#select-page-block').show();
            $('#urldeeplink').attr('readonly','readonly');
            $('#nofollow').attr('checked',false);
            break;
        case 'ext':
            $('#select-page-block').hide();
            $('#nofollow').attr('checked',true);
            document.getElementById('urldeeplink').removeAttribute('readonly');
            break;
    }
}
