﻿function DropDownListGetSelectedValue(DropDownListID)
{
    var selector = "#" + DropDownListID;
    return $(selector).val();
}

function PageMethod(MethodName, ParameterList, OnSuccessHandler, OnErrorHandler)
{
    $.ajax({
        type: "POST",
        url: window.location.pathname + "/" + MethodName,
        contentType: "application/json; charset=utf-8",
        data: "{" + ParameterList + "}",
        dataType: "json",
        success: OnSuccessHandler,
        error: OnErrorHandler
    });
}

function PageMethodBuildParameterList(ParameterList, ParameterName, ParameterValue)
{
    if (ParameterList == null)
        ParameterList = "";
    else
        ParameterList += ",";

    ParameterList += '"' + ParameterName + '"';
    ParameterList += ":";
    ParameterList += '"' + ParameterValue + '"';

    return ParameterList;
}

function PopUpOpen(ControlID, Width, Height, DisplayTitleBar)
{
    if (Width == null)
        Width = '50%';

    if (Height == null)
        Height = 150;

    if (DisplayTitleBar == null)
        DisplayTitleBar = true;

    var selector = "#" + ControlID;
    $(selector).dialog(
            {
                autoOpen: false,
                modal: true,
                width: Width,
                minWidth: Width,    // in case we want a width smaller than the default minWidth
                maxWidth: Width,    // in case we want a width larger than the default maxWidth
                height: Height,
                minHeight: Height,  // in case we want a height smaller than the default minHeight
                maxHeight: Height,  // in case we want a height larger than the default maxHeight
                bgiframe: true,     // so we will display on top of frames
                draggable: false,
                resizable: false,
                closeOnEscape: DisplayTitleBar
            });

    if (DisplayTitleBar == false)
        $(".ui-dialog-titlebar").hide();

    // don't use autoOpen so this can be programmatically opened 
    // and closed more than once on a page instance (in case the 
    // caller wants to do this - like PleaseWait).
    $(selector).dialog("open");
}

function PopUpClose(ControlID)
{
    var selector = "#" + ControlID;

    $(selector).dialog("close");
}

function TableBuildColumnControlSelector(TableId, ColumnNumber, ControlType)
{
    if (ColumnNumber == null)
        ColumnNumber = 1;

    var selector =
        $("#" + TableId +
          " tr td:nth-child(" +
          ColumnNumber.toString() + 
          ") " + ControlType);
          
    return selector;
}

function TableCheckBoxCheckAll(TableId, ColumnNumber)
{
    var selector = TableBuildColumnControlSelector(TableId, ColumnNumber, "input:checkbox");

    $(selector).each
    (
        function()
        {
            this.checked = true;
        }
    );
}

function TableCheckBoxClearAll(TableId, ColumnNumber)
{
    var selector = TableBuildColumnControlSelector(TableId, ColumnNumber, "input:checkbox");

    $(selector).each
    (
        function()
        {
            this.checked = false;
        }
    );
}

function TableCheckBoxGetCheckedRowList(TableId, ColumnNumber)
{
    var selector = TableBuildColumnControlSelector(TableId, ColumnNumber, "input:checkbox");

    var rowIndex = 0;
    var rowIndexes = new Array();

    $(selector).each
    (
        function()
        {
            if (this.checked == true)
                rowIndexes[rowIndexes.length] = rowIndex.toString();
            rowIndex += 1;
        }
    );

    var idList = rowIndexes.join(",");
    if (idList == "")
        idList = null;

    return idList;
}

function TableGetControlsCheckBox(TableId, ColumnNumber)
{
    var selector = TableBuildColumnControlSelector(TableId, ColumnNumber, "input:checkbox");

    var controlList = new Array();

    $(selector).each
    (
        function() {
            controlList[controlList.length] = this;
        }
    );

    return controlList;
}

function TableGetControlsTextArea(TableId, ColumnNumber)
{
    var selector = TableBuildColumnControlSelector(TableId, ColumnNumber, "textarea");

    var controlList = new Array();

    $(selector).each
    (
        function()
        {
            controlList[controlList.length] = this;
        }
    );

    return controlList;
}

function TableGetControlsTextBox(TableId, ColumnNumber)
{
    var selector = TableBuildColumnControlSelector(TableId, ColumnNumber, "input:text");

    var controlList = new Array();

    $(selector).each
    (
        function() {
            controlList[controlList.length] = this;
        }
    );

    return controlList;
}


