// support for IE's specific click() function in FF
if (!document.all) 
{
  HTMLElement.prototype.click = function() 
  {
    var evt = this.ownerDocument.createEvent('MouseEvents');
    evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
    this.dispatchEvent(evt);
  } 
}

/**
 * AGP library namespace.
 */
var AGPLibrary = {}

/**
 * Returns JSF UINamingContainer associated separator.
 * 
 * TODO associate the correct value from the JSF
 */
AGPLibrary.getNamingSeparator = function()
{
  return ":";
}

/**
 * Returns appropriate clientId for the given parent and partial id.
 * 
 * @param parent parent clientId
 * @param id partial id to complete
 * @return complete clientId
 */
AGPLibrary.getClientId = function(parent, id)
{
  return parent + AGPLibrary.getNamingSeparator() + id;
}

/**
 * Returns the parent component of the specified source.
 * 
 * @param source source component to resolve parent id from
 * @return parent component or null
 */
AGPLibrary.getParent = function(source)
{
  var separator = AGPLibrary.getNamingSeparator();
  var pos = source.id.lastIndexOf(separator);
  var parentId = (pos < 0) ? null : source.id.substring(0, pos);
  var parent = (parentId != null) ? document.getElementById(parentId) : null;
  
  return parent;
}

/**
 * Returns the target component from the specified source with the given id.
 * 
 * @param source parent component (or its id) to resolve target from
 * @param id target component id
 * @return target component
 */
AGPLibrary.getTarget = function(source, id)
{
  var parent = (dojo.isString(source)) ? document.getElementById(source) : source;
  var targetId = AGPLibrary.getClientId(parent.id, id);
  var target = document.getElementById(targetId);

  return target;
}

/**
 * Focuses element with given id.
 *
 * @param elemId element identifier
 */
AGPLibrary.focus = function(elemId)
{
  if (elemId) 
  {
    var el = document.getElementById(elemId);

    if (el && el.focus) 
    {
      el.focus();
    }
  }
}

/**
 * Submites form with given id. Checks whether onsubmit method is defined; if 
 * so checks for its return value to decide if form can be submitted, otherwise 
 * submits form directly.
 *
 * @param formId id of form
 */
AGPLibrary.submitForm = function(formId)
{
  var f = $(formId);

  if (f.onsubmit) 
  {
    if (f.onsubmit()) 
    {
      f.submit();
    }
  }
  else 
  {
    f.submit();
  }
}

/**
 * Opens browser window centered to screen for given url, name, width, height 
 * and window features.
 * 
 * @param url url to open in window
 * @param target window open target (default is _blank)
 * @param width window width (default is 640)
 * @param height window height (default is 480)
 * @param features string representation of window features (visibility flags)
 * @return opened window instance
 */
AGPLibrary.openWindow = function(url, target, width, height, features)
{
  var defFeatures = 'toolbar=0,'
                  + 'location=0,'
                  + 'status=0,'
                  + 'menubar=0,'
                  + 'scrollbars=1,'
                  + 'resizable=1';

  var name = (name) ? name : '_blank';
  var width = (width) ? width : 640;
  var height = (height) ? height : 480;
  var top = (screen.availHeight - height) / 2;
  var left = (screen.availWidth - width) / 2;
  var features = (features) ? features : defFeatures;

  features += ','
           +  'width=' + width + ','
           +  'height=' + height + ','
           +  'top=' + top + ','
           +  'left=' + left + ','
  
  var win = window.open(url, name, features);
  win.focus();
  
  return win;
}

/**
 * Process multiple checkbox checking. Checks only that checkboxes which are not
 * disabled and contains name in their id attribute.
 *
 * @param name group name of checkbox to check
 * @param elemId cheking invocation element id; also holds checking state
 * @return false
 */
AGPLibrary.checkAll = function(name, elemId) 
{
  var el = $(elemId);

  var checked = el.getAttribute('checked');
  var elements = el.form.elements;

  for (var i = 0; i < elements.length; i++) 
  {
    var child = elements[i];

    if (child.disabled || !_isOfType(child, 'checkbox'))
    {
      continue;
    }

    if (child.id && child.id.indexOf(name) != -1) 
    {
      child.checked = !checked;
      el.setAttribute('checked', child.checked);
    }
  }
  
  return false;
}

