﻿
PIE.readonly =
{
  enable: function(input)
  {
    switch (input.tagName.toLowerCase())
    {
      case 'select':
        input.onchange = input._onchange;
        input.onclick = input._onclick;
        break;

      case 'textarea':
        input.disabled = false;
        break;

      case 'input':
        switch (input.type.toLowerCase())
        {
          case 'checkbox':
            input.onclick = input._onclick;
            break;

          case 'radio':
            input.onclick = input._onclick;
            break;

          default:
            input.readOnly = false;
            break;
        }
        break;
    }
    input._readonly = false;
    input.className = input.className.replace(/\s*disabled/, '');
  },

  disable: function(input)
  {
    switch (input.tagName.toLowerCase())
    {
      case 'select':
        input._onchange = input.onchange;
        input._onclick = input.onclick;
        input.onchange = input.onclick = function() { input.selectedIndex = PIE.readonly.find_index_with_value('frmMG', input.name, PIE.FormReset[input.name]); };
        break;

      case 'textarea':
        input.disabled = true;
        break;

      case 'input':
        switch (input.type.toLowerCase())
        {
          case 'checkbox':
            input._onclick = input.onclick;
            input.onclick = function() { input.checked = PIE.FormReset[input.name] != null; };
            break;

          case 'radio':
            input._onclick = input.onclick;
            input.onclick = function()
            {
              var radio = PIE.readonly.find_radio_with_value('frmMG', input.name, PIE.FormReset[input.name]);
              if (radio)
                radio.checked = true;
              return false;
            };
            break;

          default:
            input.readOnly = true;
            break;
        }
        break;
    }

    input._readonly = true;
    if ((input.className.indexOf('disabled') < 0) && (input.className.indexOf('label') < 0))
      input.className += ' disabled';

  },

  find_radio_with_value: function(frm, field_name, value)
  {
    var field = null;
    $(frm).getInputs('radio', field_name).each(function(input)
    {
      if (input.value == value)
        field = input;
    });

    return field;
  },

  find_index_with_value: function(frm, field_name, value)
  {
    var select = $(frm)[field_name];

    for (var index = 0; index < select.options.length; index++)
    {
      if (select.options[index].value == value)
        return index;
    }
    return -1;
  }

};


function disableUnsavedInputs()
{
  var f = $('frmMG');
  var share_levels = $(f['sUpdatesLocked']);

  if (share_levels)
  {
    var locked_sections = $w($F(share_levels));

    var locked_in_context = f['sLockedInContext'] ? $w($F(f['sLockedInContext'])) : [];  //f['sLockedInContext'];

    //force the field disable code to run because some fields differ 
    //from the default page section
    var force_field_level_check = f.sUpdateIsComplex ? parseInt(f.sUpdateIsComplex.value) : 0;

    //the sections for the page to update
    var sections_in_page = [];
    var sections_in_page_input = $(f['sUpdateIds']);
    if (sections_in_page_input)
      sections_in_page = $w($F(sections_in_page_input));

    //if none of the valid sections are enabled or if forcing field level verification
    if ((sections_in_page.intersection(locked_sections).length > 0) || force_field_level_check)
    {
      //      var disable_all = sections_in_page.intersection(locked_sections).length > 0; //  valid_sections.size() > 0 ? !enabled_sections.contains(valid_sections[0]) : true;
      var disable_all = sections_in_page.size() > 0 ? locked_sections.contains(sections_in_page[0]) : true;

      //disable all fields
      if (disable_all)
        f.readonly();

      try
      {
        //if the field has a class of always then it will always be enabled
        var selectors = [];

        //if the field has a class of always then it will always be enabled
        if (disable_all)
          selectors.push('.always');
        else
          selectors.push('.never');

        var search = disable_all ? sections_in_page.exclude(locked_sections) : sections_in_page.intersection(locked_sections);

        search.each(function(elem)
        {
          selectors.push('.' + elem);
        });

        //for each field that has an css class matching an page section that is not locked enable the input
        $$(selectors).each(function(elem)
        {
          if (disable_all)
            elem.readwrite();
          else
            elem.readonly();
        });

      }
      catch (e)
      {
        f.readwrite();
      }
    }

    process_lockable_fields(locked_in_context);

  }
}

function process_lockable_fields(items, make_read_write)
{

  var selectors = [];

  items.each(function(elem)
  {
    selectors.push('.' + elem);
  });

  //for each field that has an css class matching an page section that is not locked enable the input       
  $$(selectors).each(function(elem)
  {
    if (make_read_write)
      elem.readwrite();
    else
      elem.readonly();
  });

}
