///////////////////////////////////////////////////////////////////////////////////////////////////
// File: config.js
// Creator: Monjay Settro, Devbleue Inc.
// Purpose: library of calendar category functions...
///////////////////////////////////////////////////////////////////////////////////////////////////

function config_data(selected_subcategory)
{
   if (!calconf || !calconf['init'])
   {
      calwait(false);
      return false;
   }
   if (!calconf['config'])
   {
      calconf['config'] = new Array();
      calconf['config']['data'] = new Array();
      calconf['config']['selected'] = new Array();
   }
   calconf['selected_subcategory'] = ''
   if (selected_subcategory)
   {
       if (selected_subcategory.match(/[Ss][Uu][Bb][Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy][0-9]/))
       {
          calconf['selected_subcategory'] = selected_subcategory
       }
   }
   if (!calconf['config']['pipe'])
   {
      var url = ('data/subcategories.html?' + new Date).replace(/ /g, '+');  //avoid browser caches

      calconf['config']['pipe'] = getXMLHttp();
      calconf['config']['pipe'].open('GET', url);
      calconf['config']['pipe'].onreadystatechange = config_data_handler;
      try
      {
         calconf['config']['pipe'].send(null);
      }
      catch (e)
      {
         calwait(false);
         calerror(109, "Looks like there is a problem [underline]getting[/underline] calendar config data.[newline]~~~ ~~~ ~~~ ~~~[newline]" + ((e.description) ? e.description : e))
         return false;
      }
   }
   else
   {
      if (!calconf['config']['xml'])
      {
         calwait(false);
         calerror(108, "Looks like there was a previous problem [underline]getting[/underline] calendar config data.", "Try hitting your browser REFRESH button, then navigating back to this month.")
         return false;
      }
      config_data_handler_core(1, calconf['config']['responseText']);
   }

   return true;
}

function config_data_handler()
{
   if ( calconf['config']['pipe'].readyState == 4 )
   {
      var text = calconf['config']['pipe'].responseText;
      if (!text || !text.length)
      {
         calwait(false);
         calerror(107, "Looks like there is a problem [underline]reading the content[/underline] of calendar config data.")
         return false;
      }
      return config_data_handler_core(2, text);
   }
   return false;
}

function config_data_handler_core(source, responseText)
{
   if (calconf['config']['selected']['category'])
   {
      if (calconf['selected_subcategory'] == calconf['config']['selected']['category'])
      {
         config_next_action(1)
         return true
      }
   }
   var result = get_calconf_xml(responseText)
   switch(result['error_code'])
   {
      case 1: 
               calwait(false)
               calconf['config']['xml'] = false
               return false
               break
      case 2:
               calwait(false)
               calerror(100, "Looks like there is a problem [underline]loading[/underline] calendar config data.")
               calconf['config']['xml'] = false
               return false
               break
      case 3:
               calwait(false);
               calerror(101, "Looks like there is a problem [underline]parsing[/underline] calendar config data.")
               calconf['config']['xml'] = false
               return false;
               break
      default:
               if (!calconf['config']['xml'])
               {
                  calconf['config']['xml'] = result['doc']
                  calconf['config']['responseText'] = responseText
                  calconf['config']['h1_tags'] = calconf['config']['xml'].getElementsByTagName('h1');
               }
               if (calconf['config']['h1_tags'].length)
               {
                  if (render_calendar_config_data())
                  {
                     calconf['config']['selected']['category'] = calconf['selected_subcategory']
                     config_next_action(2)
                     return true;
                  }
               }
               calwait(false);
               calerror(102, "Looks like there is a problem [underline]confirming the content[/underline] of calendar config data.")
               calconf['config']['xml'] = false;
               return false;
   } //switch

   return false;
}

function config_next_action(source)
{
   // 08/10/08, monjay, added this if stmnt so that prev or next month the "every event note" content renders
   if (calconf['subcategory_event_note'])
   {
      setCalVal('eventDetails', 
                '<hr class="eventDetailsSeparator" />' + 
                calconf['subcategory_event_note'] + 
                '<hr class="eventDetailsSeparator" />');
   }
                  
   if (config_test_functions())
   {
caldebug('config next action() - finished - source = ' + source);
      return events_data()
   }
   return false;
}

function config_test_functions()
{
   // call functions here to test
   // then return true to continue with calendar processing, false otherwise
   return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////

function render_calendar_config_data(selected_subcategory)
{
   var xml = calconf['config']['xml'];
   if (!calconf['config']['xml'])
   {
      return false;
   }

   //var content = prep_calendar_data_value(calconf['config']['xml'].getElementsByTagName("h1")[0]).split("=")[1];
   var content = prep_calendar_data_value(calconf['config']['h1_tags'][0]).split("=")[1];
   if (!content)
   {
      return false;
   }

   calconf['current_category'] = content;
   setCalVal('dataCalendarTitle', calconf['current_category']);

   var tds = calconf['config']['xml'].getElementsByTagName("table")[0].getElementsByTagName("td")
   var subcat_count = 0;
   var display = new Array();

   for (var i = 0; i < tds.length; i = i + 6)
   {
      content = ''
      if (tds[i].childNodes.length)
      {
         content = prep_calendar_data_value(tds[i+0]);
      }

      if (content)
      {
         if (content.match('[Ss][Uu][Bb][Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]'))
         {
            // Subcategory styles and object in the calendar are specifically named 'SubcategoryN' where N is 1 ..6
            // In the case that 'SubcategoryN' in calendar data is editted to something like 'subCateGoryN' or 'subcategoryN'
            // we correct the case to the expected value to guarantee we operate correctly when identifying objects

            content = content.replace(/[Ss][Uu][Bb][Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]/, 'Subcategory');

            var sKey = content;
            var sName = prep_calendar_data_value(tds[i+1]);

            // only show subcategories with names on the calender
            // default to the first subcategory with a name
            if (sName)
            {
               // keep key/name mapping
               // calconf['data_'+sKey] = sName;
               if (!calconf['selected_subcategory'])
               {
                  calconf['selected_subcategory'] = sKey;
               }
               if (sKey == calconf['selected_subcategory'])
               {
                  setCalVal('data' + sKey, sName);       // place subcategory descriptive name on calendar
                  setCalFormVal('field' + sKey, sName);  // set the form value
                  setCalChecked('field' + sKey, true)    // turn radio button on for the selected category
                  setCalClass('tb' + sKey, 'subcat subcatSelected');

                  setCalVal('dataAction', prep_calendar_data_value(tds[i+2]));

                  calconf['subcategory_event_note'] = prep_calendar_data_value(tds[i+3]);
                  if (calconf['subcategory_event_note'])
                  {
                     setCalVal('eventDetails', 
                               '<hr class="eventDetailsSeparator" />' + 
                               calconf['subcategory_event_note'] + 
                               '<hr class="eventDetailsSeparator" />');
                  }

                  setCalVal('footer',     prep_calendar_data_value(tds[i+4]));
                  setCalVal('bottomText', prep_calendar_data_value(tds[i+5]));
               }
               else
               {
                  setCalVal('data' + sKey, sName);       // place subcategory descriptive name on calendar
                  setCalFormVal('field' + sKey, sName);  // set the form value
                  setCalChecked('field' + sKey, false);  // turn radio button off
                  setCalClass('tb' + sKey, 'subcat');    // make the category visible
               }
               if (sKey.match('1|2|3'))
               {
                  display['row1'] = true
               }
               else if (sKey.match('4|5|6'))
               {
                  display['row2'] = true;
               }
            } // if subcategory has a name
         } // if subcategory has a name
      } // if tag content
   } // for loop

   // make the subcategory rows visible

   if (display['row1'])
   {
      setCalClass('subcatRow1', 'subcatRow');
   }
   if (display['row2'])
   {
      setCalClass('subcatRow2', 'subcatRow');
   }

   return true;

} // render calendar config data

function remove_calendar_config(selected_subcategory)
{
   if ((!calconf['selected_subcategory']) || (calconf['selected_subcategory'] != selected_subcategory))
   {
      setCalVal('footer',     '&nbsp;');
      setCalVal('dataAction', '&nbsp;');
      setCalVal('bottomText', '&nbsp;');
      return true;
   }
   return false;
}

function config_render_reset(whole)
{
   setCalClass('subcatRow1', 'subcatRowOff');  //this adds a herky jerky behavior to the calendar
   setCalClass('subcatRow2', 'subcatRowOff');  //this adds a herky jerky behavior to the calendar

   for(var s = 1; s < 7; s++)
   {
      setCalVal('dataSubcategory' + s, '');
      setCalFormVal('fieldSubcategory' + s, '');
      setCalFormCheck('fieldSubcategory' + s, '');
      setCalChecked('fieldSubcategory' + s, false);
      //setCalClass('tbSubcategory' + s, 'subcatOff');      // make it invisible, but it jumps around alot
      setCalVal('eventDetails', '&nbsp;');
   }

   setCalVal('footer',     '&nbsp;');
   setCalVal('dataAction', '&nbsp;');
   setCalVal('bottomText', '&nbsp;');

   setCalVal('dataCalendarTitle', '&nbsp;');
}