
/*
 *ready to proccess the add new page
 **/
function hideFaMessages()
{
    $('#fa-success').hide();
    $('#fa-errors').hide();
    $('#fa-exists').hide();
    $('#fa-empty').hide();
    $('#fa-lenght').hide();
    $('#add-success').hide();
    $('#delete-success').hide();
    $("#featured_name").val("");
}

function fillFeaturedCheckBoxes()
{
    str = $("#featured_ids").val();
    var splitted=str.split(":");
    for(i=0;i<splitted.length-1;i++)
    {
        if(parseInt(splitted[i])){
            $('#facheck-'+splitted[i]).attr('checked',true);
        }
    }

}
function addf()
{
 
    var websiteUrl = $('#website_url').val();
                   
    var name = $("#featured_name").val();
    hideFaMessages();
    if(name!=""){

        if(name.length<=250){
            var flag;
            $.ajax({
                type : 'POST',
                url:''+websiteUrl+'sys/backend_featuredarea/isfa/',
                data: ({
                    name: name
                } ),
                success: function(result){
                    flag = result;
                    if(flag =="no")
                    {
                        addfeaturedarea(websiteUrl,name);
                        /*$('#fa-success').show();
						$('#add-success').show();*/
                    }else
                    {$('#fa-errors').show();
                        $('#fa-exists').show();
                    }
                }
            });
        }else
        {$('#fa-errors').show();
            $('#fa-lenght').show();
        }
    }
    else
    {$('#fa-errors').show();
        $('#fa-empty').show();
    }
        
}
//this method is proccess url to avoid sppear spaces and put a delimiter '-' between words
function proccessUrl(url)
{
    var ltrim = /^\s+/g;
    var rtrim = /\s+$/g;

    url = url.replace(ltrim,'');
    url = url.replace(rtrim,'');
    if(url != '')
    {
        url = url+'.html';
        return replaceSpace(url,'-');
    }else
    {
        return false;
    }

}
//method puts delimiter '-' istead spaces
function replaceSpace(str,delimiter)
{
    var trim = /\s+/g;
    return str.replace(trim,delimiter);
}

function deletefeaturedarea(urlwebsite,id)
{
    hideFaMessages();
    if(confirm("Do you really want to delete this featured area?"))
    {
        $.ajax({
            type : 'POST',
            url:''+urlwebsite+'sys/backend_featuredarea/deletefa/',
            data: ({
                id : id
            } ),
            success: function(object){
                $('#featuredarea_'+id).remove();
				$('#fa-success').show();
                $('#delete-success').show();
            }
        });
    }else
    {
        return false;
    }
}

function isfaexists(urlwebsite,name)
{
    var flag;
    $.ajax({
        type : 'POST',
        url:''+urlwebsite+'sys/backend_featuredarea/isfa/',
        data: ({
            name: name
        } ),
        success: function(result){
            flag = result;
            document.getElementById('exists_flag').value = result;
        //alert("isfaexists = "+document.getElementById('exists_flag').value);
        }

    });
    return flag;
}

function addfeaturedarea(websiteUrl,name)
{
    $.ajax({
        type : 'POST',
        url:''+websiteUrl+'sys/backend_featuredarea/addfa/',
        data: ({
            featured_name: name,
            exists:$('#exists').val()
        } ),
        success: function(result){
            if(result!="error")
            {
                $('#featured-table-list').prepend(result);
                $("#featured_name").val("");
            }
           
        }
    });
}

function selectFeaturedArea(faId,checked_flag)
{
    if(checked_flag){
        var str = $("#featured_ids").val();
        str = str+faId+":";
        $("#featured_ids").val(str);
        //count how many checkboxes were selected
        var splitted=str.split(":");
        $('#show-fa-list-link').empty();
        $('#show-fa-list-link').append('Yes, '+ (splitted.length-1) +' Times.');
    }else
    {
        var str = $("#featured_ids").val();
        var splitted=str.split(":");

        for(i=0;i<splitted.length;i++)
        {
            if(splitted[i]==faId)
            {
                delete splitted[i];
            }
        }

        for(i=0;i<splitted.length;i++)
        {
            if(splitted[i]){
                splitted[i] = splitted[i]+":";
            }
        }

        var resultedtSring = splitted.join("");
        $("#featured_ids").val(resultedtSring);
        //count how many checkboxes were selected
        var splitted=resultedtSring.split(":");
        $('#show-fa-list-link').empty();
        if((splitted.length-1)>0){
            $('#show-fa-list-link').append('Yes, '+ (splitted.length-1) +' Times.');
        }else
        {
            $('#show-fa-list-link').append('Not Yet.');
        }
    }
}

function backToPage()
{
    $("#createpage").show();
    $("#fa-list-div").hide();
}
