
$(document).ready(function() {

    //show watermarks
    $('input[type=text]').simpleWaterMark('watermark');

    //collapse all search panels (based on searched criteria)
    HideSearchPanels();

    //set up expand panels
    $(".expandPanel .label").click(function() {

        var content = $(this).next(".expandPanel .content");

        //add expand/collapse class to label
        if (content.is(":visible")) {
            $(this).removeClass("expanded");
        }
        else {
            $(this).addClass("expanded");
        }

        $(this).next(".expandPanel .content").slideToggle(300);
    });

    //set up course search so 'enter' submits
    $('.courseSearch input, .courseSearch select').live('keypress', function (e) {
        
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            
            $('#btnCourseSearch').click();
            return false;
        }
    });
});


var processCell = function(cellDiv, id) {
    $(cellDiv).addClass("edit");

    $(cellDiv).click(function() {
        document.location = 'editContent.aspx?id=' + id;
    });
}

function EncodeQuery(value) {
    if (value != null) {
        value = value.replace('=', '%equals').replace('&', '%amp');
    }
    return value;
}

function searchOnEnter(e, grid, criteria) {
    if (e.keyCode == 13) {
        performSearch(grid, criteria);
        e.preventDefault();
        return false;
    }
} 

var performSearch = function(grid, GetQueryCriteria) {
    grid.flexOptions({ query: GetQueryCriteria(), newp: 1 });
    grid.flexReload();
}


function moveItemUp(listBox) {
    $(listBox + ' option:selected').each(function() {
        $(this).insertBefore($(this).prev());
    });
}

function moveItemDown(listBox) {
    $(listBox + ' option:selected').each(function() {
        $(this).insertAfter($(this).next());
    });
}

function removeItem(listBox) {
    $(listBox + ' option:selected').each(function() {
        $(this).remove();
    });
}


function GetDivRow(cellDiv, id) {
    return $("#item-list tr[id=row" + id + "]")
}

var GetCheckBoxList = function(name)
{
    var checkValues = [];
    
    $('input[name=' + name + ']:checked').each(function() {
        checkValues.push($(this).val());
    });
    
    return checkValues.join(",");
}


function ClearSearch() {

    $('.courseSearch .checkboxList input:checked').attr('checked', false);
    $('.courseSearch select').val('');

    $('#courseType_0').click();
    $('.courseSearch input[type = "text"]').val('');
        
    //collapse all panel
    HideSearchPanels();
}

function HideSearchPanels() {
    //hide content if nothing checked
    $('.courseSearch .checkboxList').each(function() {

        if ($(this).find(" input:checked").length <= 0) {
            $(this).find(".content").hide();
        }
    });
}

function ShowCheckBoxPanel(id) {
    $('#' + id).show();
}

function HideCheckBoxPanel(id) {
    $('#' + id).hide();
    $('#' + id + ' input:checked').attr('checked', false);
}
