jQuery().ready(function()
{
$(".refresh_captcha").click(RefreshCaptcha);
});

function RefreshCaptcha(){
    $(".captcha_image").attr("src", "../captcha.php?refresh=" + Math.random()+"&p="+$(".captcha_image").attr("title"));
}

function expDiv( div , span )
{
    if( document.getElementById (div).style.display == "block" )
    {
        document.getElementById (div).style.display = "none";
        document.getElementById (span).innerHTML = "+";
    }
    else
    {
        document.getElementById (div).style.display = "block";
        document.getElementById (span).innerHTML = "-";
    }
}

function trim(sString)
{
    while(sString.substring(0,1) == ' ')
    {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
}



function Delete( deleteItemName )
{
    if( confirm( "Are you sure you want to delete this " + deleteItemName  + "?" ) )
    {
        return true;
    }
    else
    {
        return false;
    }
}

function Clone( cloneName )
{
    if( confirm( "Are you sure you want to clone this " + cloneName + "?" ) )
    {
        return true;
    }
    else
    {
        return false;
    }
}

function GoToFeedbackPage()
{
    document.feedbackForm.submit();
}

jQuery().ready(function()
{
    SetupNavigation( 1, 1, $("#last_index").val( ) );
});
function FaceBox()
{
    $('a[rel*=facebox]').facebox() ;
}


function handleLinks()
{
    $(".dr-dscr-act").each(function(){this.className = "dr-dscr-inact";});
    this.className = "dr-dscr-act";
    getPage(this.innerHTML, $("#sort_column").val(), $("#sort_order").val(), '', 'json', '');
}

function SetupNavigation( startIndex, currentIndex, lastIndex )
{
    $("#nav_first").click(function(){$("#current_page").val(1); getPage(1, $("#sort_column").val(), $("#sort_order").val(), '', 'json', '');});
    $("#nav_previous").click(function(){$("#current_page").val(eval( currentIndex ) - 1); getPage( eval( currentIndex ) - 1, $("#sort_column").val(), $("#sort_order").val(), '', 'json', '');});
    $("#nav_last").click(function(){$("#current_page").val(lastIndex); getPage(lastIndex, $("#sort_column").val(), $("#sort_order").val(), '', 'json', '');});
    $("#nav_next").click(function(){$("#current_page").val(eval( currentIndex ) + 1); getPage( eval( currentIndex ) + 1, $("#sort_column").val(), $("#sort_order").val(), '', 'json', '');});
    $("#next_set").click(function(){var next_set_arr = this.innerHTML.split(" - "); getPage( next_set_arr[0], $("#sort_column").val(), $("#sort_order").val(), '', 'json', '');});
    $("#previous_set").click(function(){var previous_set_arr = this.innerHTML.split(" - "); getPage( previous_set_arr[1], $("#sort_column").val(), $("#sort_order").val(), '', 'json', '');});
}

function ValidateLength( text )
{
    var textLength = text.length;

    if( textLength > 32000 )
    {
        alert( "Description cannot be greater than 32000 characters, Please enter accordingly !!!" );

        return false;
    }

    return true;
}

/* Loads pager according to the url specified*/
function loadPager( type, url, data )
{
    
    $("#working").show();
    
    $.ajax({
        async: true,
        type: type,
        url: url,
        data: data,
        dataType: "json",
        success:function(json)
        {
            if( json.response != null )
            {
                var response = json.response.pager;
                var num_of_rows = response["number_of_rows"];
                var current_page = response["current_page"];
                var sort_order = response["sort_order"];
                var sort_column = response["sort_column"];

                $("#pager").html(response["pager_rows"]);
                if( response["pagination"] != null )
                {
                    $("#pagination").show();
                    $("#pagination").html(response["pagination"]);
                    SetupNavigation( 0, current_page, response["number_of_pages"] );
                }
                else
                {
                    $("#pagination").hide();
                }

                $("#error_message").html('');

                $(".dr-dscr-inact").click(handleLinks);

                $("#working").hide();

                $("#current_page").val(response["current_page"]);
                $("#sort_order").val(response["sort_order"]);
                $("#sort_column").val(response["sort_column"]);
		//$('a[rel*=facebox]').facebox();
            }
            else
            {
                $("#pagination").hide();
                $("#pager").html("<tr><td class=\"dr-subtable-cell rich-subtable-cell \">No Records Found</td></tr>");
                $("#working").hide();
		//$('a[rel*=facebox]').facebox();
                try{$("#moveToProduction").hide();}
                catch(e){}
            }
        }
    });
}
