﻿/*
  Appends options to a drop down list
  
  list - A select html element
  items - JSON representing a SWListItem collection  ex. {{'Text':'text', 'Value':'value', 'Selected':true, 'Enabled':true},{...}...}
*/
function appendDropDownList(list, items) {
  var jsonArray = $(JSON.parse(items));
  $(list).append('<option value="-1">- Select -</option>');
  jsonArray.each(function() {
    var selected = '';
    if (this.Selected)
      selected = ' selected = "selected" ';
      
    $(list).append('<option value="' + this.Value + '"' + selected + '>' + this.Text + '</option>');
  });
}
/*
  Creates a self contained radio button group and attaches it to the 'list' container
  
  list - Container for the radio button collection.
  items - JSON representing a SWListItem collection  ex. {{'Text':'text', 'Value':'value', 'Selected':true, 'Enabled':true},{...}...}
  groupName - ID prefix for checkboxes in list. Needed for uniqueness since list is dynamically created.  ie : box1, box2, etc. 
*/
function appendRadioButtonList(list, items, groupName) {
  var jsonArray = $(JSON.parse(items));

  var eachID = 1;
  $(list).append('<div class="rbList">');
  jsonArray.each(function() {

    var checked = '';
    if (this.Selected)
      checked = ' checked="checked" ';

    var enabled = '';
    if (this.Enabled == false) {
      enabled = ' disabled="disabled" ';
    }

    var tags = '<div style="float:left;" ' + enabled + '><input type="radio" value="' + this.Value + '" id="' + groupName + eachID + '" ' + '" name="' + groupName + '" ' + checked + enabled + '/>' +
                   '<label for="' + groupName + eachID + '"' +enabled+' >' + this.Text + '</label></div>';

    $(list).append(tags);
    eachID++;
  });
  $(list).append('</div>');
}
/*
  Creates a self contained checkbox group and attaches it to the 'list' container
  
  list - container for the checkbox collection.
  items - JSON representing a SWListItem collection  ex. {{'Text':'text', 'Value':'value', 'Selected':true, 'Enabled':true},{...}...}
  groupName - ID prefix for checkboxes in list. Needed for uniqueness since list is dynamically created.  ie : box1, box2, etc. 
*/
function appendCheckBoxList(list, items, groupName) {
  var jsonArray = $(JSON.parse(items));

  var eachID = 1;
  $(list).append('<div class="rbList">');
  jsonArray.each(function() {

    var checked = '';
    if (this.Selected)
      checked = ' checked="checked" ';

    var enabled = '';
    if (this.Enabled == false) {
      enabled = ' disabled="disabled" ';
    }

    var tags = '<div style="float:left;" ' + enabled + '><input type="checkbox" value="' + this.Value + '" id="' + groupName + eachID + '" ' + '" name="' + groupName + '" ' + checked + '/>' +
                   '<label for="' + groupName + eachID + '" >' + this.Text + '</label></div>';

    $(list).append(tags);
    eachID++;
  });
  $(list).append('</div>');
}

//Launches a new browser window that can have dynamic dimensions but defaults to 800x810 pixels, used primarily for SSRS reporting.  
function showWindowPopUp(url, height, width) {
  if (height == '')
    height = '800px';
  if (width == '')
    width = '810px';
  window.open(url, '_blank', 'toolbar=no,directories=no,menubar=no,resizable=yes,status=no,height=' + height + ',width=' + width + "'");
}

/*
  Launches a jquery dialog widget with dynamic height.
  dialogID - an id that must be unique on a page so that it will not interfere w/ other markup
  _width - width of the dialog window.
  _title - text displayed in the dialog header
  _text - the text of the dialog.  (** this can be markup)
*/
function popupDialog(dialogID, _width, _title, _text) {
  $("form:first").append('<div style="display:none;" id="dlg__' + dialogID + '">' + _text + '<div>');
  $("#dlg__" + dialogID).dialog({
    bgiframe: true,
    width: _width,
    height: 'auto',
    modal: false,
    title: _title,
    dialogClass: "popupDialog",
    close: function() {
      $("dlg__" + dialogID).remove();
    }
  });
}
/*
  Launches a jquery dialog widget with dynamic height.
  dialogID - an id that must be unique on a page so that it will not interfere w/ other markup
  _width - width of the dialog window.
  _title - text displayed in the dialog header
  _text - the text of the dialog.  (** this can be markup)
  hoverCtrl - ctrl that launches the dialog when you mouse over it.
*/
function popupDialogOnHover(dialogID, _width, _title, _text, hoverCtrl) {
  $("#" + hoverCtrl).mouseenter(function() {
  $("body").append('<div style="display:none;" id="dlgh__' + dialogID + '">' + _text + '<div>');
    $("#dlgh__" + dialogID).dialog({
      bgiframe: true,
      width: _width,
      height: 'auto',
      modal: false,
      title: _title,
      dialogClass: "popupDialog"
    });
  });

  $(hoverCtrl).mouseleave(function() {
    $("#dlgh__" + dialogID).dialog("close");
    $("#dlgh__" + dialogID).remove();
  });
}
/* 
  clears all Inputs in a container control such as a div.
  parent - client side jquery selector representing the container control.
*/
function clearInputs(parent) {
  $(parent).find("input:text").each(function() { $(this).val(''); });
  $(parent).find("input:checked").each(function() { $(this).attr('checked', false); });
  //unselect all dropdown items.
  $(parent).find("select option").each(function() {
    $(this).removeAttr("selected");
  });
  //selects the 1st option if dropdown item if any.
  $(parent).find("select").each(function() { 
    $(this).find("option:first").attr("selected", "selected"); 
  });
}
//removes container surrounding markup.  
$.fn.zap = function() {
  return this.each(function() { $(this.childNodes).insertBefore(this); }).remove(); //http://benalman.com/projects/jquery-unwrap-plugin/
};
//overlays the entire form with a transparent div which blocks all user ui
function disableForm() {
  $('form:first').append('<div id="formOverlay" class="ui-widget-overlay"></div>');
}
//removes transparent overlay blocking user ui
function enableForm() {
  $('#formOverlay').remove();
}
//Binds a checkbox to a controls active status.  When checked enables the control.  When unchecked disables the control.
function checkboxEnabled(cb, ctrl) {
  if ($(cb).is(":checked") == false) {
    $(ctrl).val('');
    $(ctrl).addClass('disabledTextbox');
    $(ctrl).attr('disabled', 'disabled');
   // $(ctrl).removeAttr('disabled');
  }
  else {
    $(ctrl).removeClass('disabledTextbox');
    $(ctrl).removeAttr('disabled');
  }
  $(cb).click(function() {
  if ($(this).is(":checked") == false) {
      $(ctrl).val('');
      $(ctrl).addClass('disabledTextbox');
      $(ctrl).attr('disabled', 'disabled');
    }
    else {
      $(ctrl).removeClass('disabledTextbox');
      $(ctrl).removeAttr('disabled');
    }
  });
}

function ExtendGridProperty(_grid, _property, _object) {
  var propertyObject = $(_grid).jqGrid('getGridParam', _property);
  var newObject = $.extend(propertyObject, _object);
  $(_grid).jqGrid('setGridParam', JSON.stringify({ _property: newObject })).trigger("reloadGrid");
}

// EX. ChangeGridProperty($('#gridid'), {"sord":"asc"});
function ChangeGridProperty(grid, object) {
  $(grid).jqGrid('setGridParam', object).trigger("reloadGrid");
}

//Wraps grid in a dialog w/ location and a popup trigger
function WrapGridInPopupDialog(object, triggerObj, triggerEvent, coordinates, isEdit) {
  if ($(object).length > 0) {
    var wrapper = $(object)[0].id + '_wrapper';
    // $(object).wrap('<div id="' + wrapper + '" />');

    $('#' + wrapper).dialog({
      closeOnEscape: true,
      bgiframe: true,
      autoOpen: false,
      resizable: false,
      dialogClass: "popupDialog",
      width: $(object).width() + 3,
      height: "auto"
    });

    if ($.isArray(coordinates))
      $('#' + wrapper).dialog('option', 'position', coordinates);

    if ($(triggerObj).length > 0) {
      $(triggerObj).bind(triggerEvent, function () {
        if ($('#' + wrapper).dialog("isOpen")) {
          if (!isEdit)
            $('#' + wrapper).dialog("close");
        }
        else {
          $('#' + wrapper).find('.ui-jqgrid-titlebar:first').hide();
          $('#' + wrapper).css('padding', '0');
          $('.ui-jqgrid-bdiv').css('overflow', 'hidden');
          $('#' + wrapper).parent().find('.ui-dialog-title').html($('#' + wrapper).find('.ui-jqgrid-title:first').html());
          $('#' + wrapper).dialog("open");
        }
      });
    }
  }
}


/* pluggins to enable or disable an element on the client  ie : $(element).disableElement */
$.fn.disableElement = function() {
  return this.each(function() {
    if (typeof this.disabled != "undefined") this.disabled = true;
  });
}

$.fn.enableElement = function() {
return this.each(function() {
    if (typeof this.disabled != "undefined") this.disabled = false;
  });
}
  
