//$Id: cg_shared.js,v 1.16 2007-09-25 02:03:29 thuanduc Exp $
//ig_initShared implements browser independent functionality
function cg_initShared()
{
   this.IsIE = jQuery.browser.msie;
   this.IsFireFox = jQuery.browser.mozilla;
}

cg_initShared.prototype =
{
   stopEventFlow: function(evnt)
   {
      this.cancelEventBubble(evnt);
      this.preventEventDefault(evnt);
   },
   cancelEventBubble: function(evnt)
   {
      if (typeof (evnt.jquery) == "undefined")
         jQuery.event.fix(evnt).stopPropagation();
      else
         evnt.stopPropagation();
   },
   preventEventDefault: function(evnt)
   {
      if (typeof (evnt.jquery) == "undefined")
         jQuery.event.fix(evnt).preventDefault();
      else
         evnt.preventDefault();
   },
   isClick: function(evnt)
   {
      // jQuery event which for click: 1 == left; 2 == middle; 3 == right
      if (typeof (evnt.jquery) == "undefined")
         return jQuery.event.fix(evnt).which == 1;
      return evnt.which == 1;
   },
   isRightClick: function(evnt)
   {
      // jQuery event which for click: 1 == left; 2 == middle; 3 == right (in IE, which is undefined)
      if (typeof (evnt.jquery) == "undefined")
         return jQuery.event.fix(evnt).which == 3 || (cg_shared.IsIE && jQuery.event.fix(evnt).button == 0);
      return evnt.which == 3 || (cg_shared.IsIE && evnt.button == 0);
   },
   getWindow: function(doc)
   {
      if (this.IsIE)
         return doc.parentWindow;
      return doc.defaultView;
   },
   verifyWindowNoScroll: function()
   {
      if (document.body.getAttribute("scroll") == "no")
         document.body.style.overflow = "hidden";
   }
}

///////////////////////////////////////////////////////////////////////
if (typeof cg_shared != "object")
   var cg_shared = new cg_initShared();

if (cg_shared.IsIE)
   $(window).bind("unload", null, CollectGarbage);


///////////////////////////////// UTILITIES ///////////////////////////////////

function cgForm_CanFocus(element)
{
   if (!element || !(element.tagName)) return false;
   var tagName = element.tagName.toLowerCase();
   return (!(element.disabled) &&
            (!(element.type) || element.type.toLowerCase() != "hidden") &&
            cgForm_IsFocusableTag(tagName) &&
            cgForm_IsInVisibleContainer(element)
            );
}
function cgForm_IsFocusableTag(tagName)
{
   return (tagName == "input" ||
            tagName == "textarea" ||
            tagName == "select" ||
            tagName == "button" ||
            tagName == "a");
}
function cgForm_IsInVisibleContainer(ctrl)
{
   var current = ctrl;
   while ((typeof (current) != "undefined") && (current != null))
   {
      if (current.disabled ||
            (typeof (current.style) != "undefined" &&
            ((typeof (current.style.display) != "undefined" &&
                current.style.display == "none") ||
                (typeof (current.style.visibility) != "undefined" &&
                current.style.visibility == "hidden"))))
      {
         return false;
      }
      if (typeof (current.parentNode) != "undefined" &&
                current.parentNode != null &&
                current.parentNode != current &&
                current.parentNode.tagName.toLowerCase() != "body")
      {
         current = current.parentNode;
      }
      else
      {
         return true;
      }
   }
   return true;
}

function cgForm_FocusErrorControl(elementid)
{
   var element = document.getElementById(elementid);
   if (cgForm_CanFocus(element))
      element.focus();
}

