/*
 
library of utility functions used by the find-it website

*/


/*
 *  Switches the active tab and content when viewing a listing.
 */
function switch_tab(tab_name, tab_list)
{
  // hide the tabs/tab content
  for (var i = 0; i < tab_list.length; i++) {
    if (tab_list[i] != tab_name)
    {
      var tab_content = document.getElementById(tab_list[i]);
      tab_content.style.display = "none";
      
      var tab = document.getElementById(tab_list[i] + 'tab');
      if (tab.className != "tab_disabled")
        tab.className = "";
    }
  }
  
  // show the one we want to see
  var active = document.getElementById(tab_name);
  active.style.display = "block";
  
  // select the one we want
  var activetab = document.getElementById(tab_name + 'tab');
  activetab.className = "selected";
}

/*
 *  Limits the test typed into a textarea or input text box to limitNum.  Updates
 *  an html element with the number of remaining characters accordingly 
 */
function limitText(limitField, limitCount, limitNum) 
{
  var fieldElement = document.getElementById(limitField);
  if (fieldElement.value.length > limitNum) 
  {
  	fieldElement.value = fieldElement.value.substring(0, limitNum);
  } 
  var countElement = document.getElementById(limitCount);
  countElement.innerHTML = limitNum - fieldElement.value.length;
}
 
 
/*
 *  Allows a function to be added to the list of functions to run on page load
 *  without interfering with any functions that might already be in the list. 
 */     
function addLoadEvent(func) 
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function') 
  {
    window.onload = func;
  } 
  else 
  {
    window.onload = function() 
    {
      oldonload();
      func();
    }
  }
}

/*
 *  Allows a function to be added to the list of functions to run on page unload
 *  without interfering with any functions that might already be in the list. 
 */     
function addUnloadEvent(func) 
{
  var oldonunload = window.onunload;
  if (typeof window.onunload != 'function') 
  {
    window.onunload = func;
  } 
  else 
  {
    window.onunload = function() 
    {
      oldonunload();
      func();
    }
  }
}

/*
 *  Uses AJAX to update the province or location dropdown based on a user 
 *  changing a dropdown value.
 *
 *  REQUIRES prototype  (http://prototype.conio.net/)
 *   
 */
function updateDropDown(elementToUpdate, url, params)
{

  //alert('Debugging');

  new Ajax.Updater(
            elementToUpdate, 
            url, 
            {
                method: 'post', 
                parameters: params
            });
}

/*
 *  Uses AJAX to update the category/listing mapping based on a user checking
 *  or unchecking a checkbox.
 *
 *  REQUIRES prototype  (http://prototype.conio.net/)
 *   
 */
function saveCategoryListing(elementToFlash, checkbox, addAction, delAction, url, params, progressIndicator)
{
  $(progressIndicator).style.display = 'inline';
  if ($(checkbox).checked)
  {
    params = params + addAction;
  }
  else
  {
    params = params + delAction;
  }
  var ajax = new Ajax.Request(
           url,
           {
             method:'post',
             parameters: params,
             onSuccess: function(t) {
                if (t.responseText == 1)
                {
                  new Effect.Highlight(elementToFlash, {startcolor: '#A2E0A9', endcolor:'#FFFFFF'});
                  $(progressIndicator).style.display = 'none';
                }
                else
                {
                  new Effect.Highlight(elementToFlash, {startcolor: '#FF0000', endcolor:'#FFFFFF'});
                  $(progressIndicator).style.display = 'none';
                  alert('Error saving the category')
                }
             }, 
             onFailure: function(t) {
                new Effect.Highlight(elementToFlash, {startcolor: '#FF0000', endcolor:'#FFFFFF'});
                $(progressIndicator).style.display = 'none';
             }
           });
  
}

/*
 *  Clears an html node by removing all children.
 */ 
function clearInnerHTML(obj) {
	
  // so long as obj has children, remove them
	while(obj.firstChild) obj.removeChild(obj.firstChild);
}

/* 
 * set of functions to manage the expandable menu
 */

// this just to keep track of what is visable or not for dhtml etc.
var visibleElements = new Array();

function toggleBlind(elementID) {
  if (visibleElements[elementID] == 0 || visibleElements[elementID] == undefined) {
    new Effect.BlindDown(elementID,{duration:.25});
    visibleElements[elementID] = 1;
  } else {
    new Effect.BlindUp(elementID,{duration:.25});
    visibleElements[elementID] = 0;
  }
}

/*
Effect.BlindDown = function(element) {
  element = $(element);
  var elementDimensions = element.getDimensions();
  return new Effect.Scale(element, 100, Object.extend({
    scaleContent: false,
    scaleX: false,
    scaleFrom: 0,
    scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
    restoreAfterFinish: false,
    afterSetup: function(effect) {
      effect.element.makeClipping().setStyle({height: '0px'}).show();
    }, 
    afterFinishInternal: function(effect) {
      effect.element.undoClipping();
    }
  }, arguments[1] || {}));
}
*/
