var confirmDeleteText = "Are you sure you want to delete this question?";

function editQuestion(element, id)
{
    $(element).parent().parent().children('.count').append('<input type="hidden" name="EditQuestionId" value="' + id + '" />');
	
    var question = $(element).parent().parent().children('.question').children("div").html()
    $(element).parent().parent().children('.question').html('<textarea name="Question" rows="2">' + fixHtmlString(question) + '</textarea>');
    
    var answer = $(element).parent().parent().next().children('.answer').children("div").html()
    $(element).parent().parent().next().children('.answer').html('<textarea name="Answer" rows="6">' + fixHtmlString(answer) + '</textarea>');
    
    $(element).css("visibility", "hidden");
    
    $(element).parent().parent().children('.question').children('textarea').focus();
}

function fixHtmlString(html)
{
    // Prevent control characters returned from .innerHTML() and .html() being sent to output
    if ($.browser.msie) {
        return html
            .replace(/<br>|<br\/>|<br \/>/ig, '&#13;&#10;')
            .replace(/[\n\r\t]/g, '');
    }
    else {
        return html;
    }
 
}

function moveUp(obj)
{
    var currentRow = $(obj).parent().parent();
    currentRow = currentRow.add($(obj).parent().parent().next());
    currentRow = currentRow.add($(obj).parent().parent().next().next());
    
    var previousRow = $(obj).parent().parent().prev().prev().prev();
    
    currentRow.insertBefore(previousRow);
}

function moveDown(obj)
{
    var currentRow = $(obj).parent().parent();
    currentRow = currentRow.add($(obj).parent().parent().next());
    currentRow = currentRow.add($(obj).parent().parent().next().next());
    
    var nextRow = $(obj).parent().parent().next().next().next().next().next();
    
    if(nextRow.prev().prev().hasClass("NewQuestion") == false)
    {
        nextRow.after(currentRow);
    }
}

function deleteQuestion(obj, id)
{
    if(confirm(confirmDeleteText))
    {
        var currentRow = $(obj).parent().parent();
        
        if(id == -1)
        {
            currentRow = currentRow.add($(obj).parent().parent().next());
            currentRow = currentRow.add($(obj).parent().parent().next().next());
            currentRow.remove();
        }
        else
        {
            currentRow.append('<input type="hidden" name="DeleteQuestionId" value="' + id + '" />');
            
            currentRow = currentRow.add($(obj).parent().parent().next());
            currentRow = currentRow.add($(obj).parent().parent().next().next());
            currentRow.fadeOut();
        }
    }
}

function faqReady() {
    $(document).ready(function() {

        $(".showall").show();
        $(".showsingle").show();
        $("a.showsingle").parent().next().hide();
        $("dt.toggle").next().hide();

        $("dt.toggle p").wrapInner("<a href=\"#\" onclick=\"return false;\" class=\"toggle\"></a>");

        $(".hideall").click(function() {
            $(this).parent().children(".showall").show();
            $(this).parent().next().children("dd").hide();
            $(this).parent().next().find(".showsingle").show();
            $(this).parent().next().find(".hidesingle").hide();
            $(this).hide();
            SI_clearFooter();
        });

        $(".showall").click(function() {
            $(this).parent().children(".hideall").show();
            $(this).parent().next().children("dd").show();
            $(this).parent().next().find(".showsingle").hide();
            $(this).parent().next().find(".hidesingle").show();
            $(this).hide();
            SI_clearFooter();
        });

        $(".showsingle").click(function() {
            $(this).hide();
            $(this).parent().children(".hidesingle").show();
            $(this).parent().next().show();
            SI_clearFooter();

        });

        $(".hidesingle").click(function() {
            $(this).hide();
            $(this).parent().children(".showsingle").show();
            $(this).parent().next().hide();
            SI_clearFooter();
        });

        $("a.toggle").click(function() {
            $(this).parent().parent().children(".showsingle").toggle();
            $(this).parent().parent().children(".hidesingle").toggle();
            $(this).parent().parent().next().toggle();
            SI_clearFooter();
        });

        $('table#FAQEdit').parent().children('a.AddQuestion').click(function() {
            var newRow = $('<tr class="NewQuestion"><td class="count" align="right" valign="top">Q:</td>' +
            '<td class="question"><textarea name="NewQuestion" rows="2"></textarea></td>' +
            '<td class="edit" rowSpan="2" ><div class="Delete" onclick="deleteQuestion(this, -1)"></div></td></tr>' +
            '<tr><td class="count" align="right" valign="top">A:</td><td class="answer"><textarea name="NewAnswer" rows="6"></textarea></td></tr>' +
            '<tr><td>&nbsp;</td></tr>');

            var tableFAQEdit = $('table#FAQEdit');
            $("#footer").hide();
            $(newRow).appendTo(tableFAQEdit);
            $("#footer").show(1);
            $.scrollTo(newRow);
        });

    });
}

// Required by the Microsoft Ajax Framework:
if ((typeof Sys !== "undefined") && (typeof Sys.Application !== "undefined")) {
    Sys.Application.notifyScriptLoaded();
}