var XAM_Namespace = 'XAM';

window[XAM_Namespace] = new Object();

window[XAM_Namespace].Core =
{

  DefaultNamespace : XAM_Namespace,
  
  AddToNamespace : function(namespace, oname, obj)
  {
    if (typeof(window[namespace]) == 'undefined')
    {
      window[namespace] = new Object();
    }  
    window[namespace][oname] = obj;
  },

  SetOnLoadCallback : function(f)
  {
    var w=window;
    if (w.addEventListener) { w.addEventListener("load",f,false); }
    else if (w.attachEvent) { w.attachEvent("onload",f); }
    else { w["onload"]=f; }
  },

  SetOnUnloadCallback : function(f)
  {
    var w=window;
    if (w.addEventListener) { w.addEventListener("unload",f,false); }
    else if (w.attachEvent) { w.attachEvent("onunload",f); }
    else { w["onunload"]=f; }
  },

  GetObjectRef : function(objName)
  {
    var obj = null ;
    if (document.getElementById)
      obj = document.getElementById(objName)
    else
     if (document.all)
       obj = document.all[objName];
    return obj;
  }

}; // XAM.Core

window[XAM_Namespace].Forms =
{
  // returns true if anything has changed on the form, otherwise false
  FormElementsChanged : function(form)
  {
    if (typeof(form) == 'undefined') { return false; }
    if (typeof(form.elements) == 'undefined') { return false; }

    for (var i=0; i < form.elements.length; i++)
    {
      var e = form.elements[i] ;
      switch (e.type) {
        case 'select-one' :
          for (var j=0; j < e.options.length; j++)
             if (e.options[j].defaultSelected != e.options[j].selected) { return true; }
          break; 
        case 'text' :
          if (e.value != e.defaultValue) { return true; }
          break;
        case 'file' :
          if (e.value != e.defaultValue) { return true; }
          break;  
        case 'textarea' :
          if (e.value != e.defaultValue) { return true; }
          break;
        case 'password' :
          if (e.value != e.defaultValue) { return true; }
          break;
        case 'hidden' :
          if (e.value != e.defaultValue) { return true; }
          break;
        case 'radio' :
          if (e.defaultChecked != e.checked) { return true; }
          break ;
        case 'checkbox' :
          if (e.defaultChecked != e.checked) { return true; }
          break ;
      }
    }
  
    return false;
  }

}; // XAM.Forms

window[XAM_Namespace].Utility =
{
  TrimString : function(sInString)
  {
    sInString = sInString.replace( /^\s+/g, "" ); // strip leading
    return sInString.replace( /\s+$/g, "" );      // strip trailing
  }
}; // XAM.Utility


window[XAM_Namespace].Messages =
{

  // message handler object
  MessageHandler : function(msg_display_obj_name)
  {
    this.obj_name = (typeof(msg_display_obj_name) == 'string') ? msg_display_obj_name : 'no_message_obj_passed';
    this.obj = XAM.Core.GetObjectRef(this.obj_name);
    this.mainTimeoutId = -1;
    this.fadeTimeoutId = -1;
    this.fadeTimeout = 1300;

    this.display = function(str, timeout)
    {
      if (this.obj == null) return false;
      str = (typeof(str) != 'undefined') ? str : '';
      
      timeout = (typeof(timeout)=='number') ? timeout : 5000;
      this.obj.innerHTML = str;
      this.obj.style.display = (str) ? 'block' : 'none';

      this.obj.style.opacity = 1;
      this.obj.style.filter = 'alpha(opacity=100)';

      window.clearTimeout(this.mainTimeoutId);
      window.clearTimeout(this.fadeTimeoutId);

      if (str && (timeout > 0))
      {
        if (timeout >= this.fadeTimeout)
        {
          timeout -= this.fadeTimeout;
        }
        
        this.mainTimeoutId = window.setTimeout(this.clear_display_callback(), timeout);
      }
      return true;
    }

    this.nodisplay = function()
    {
      return this.display();
    }

    this.hide = function()
    {
      return this.display();
    }

    this.fade = function(startTick)
    {
      var curTick = new Date().getTime();
      startTick = (typeof(startTick)=='number') ? startTick : curTick;
      var elapsedTicks = curTick - startTick;
    
      var fadeTicksLeft = this.fadeTimeout - elapsedTicks;
      var opacity = fadeTicksLeft/this.fadeTimeout;

      if (fadeTicksLeft <= 0) { return this.hide(); }

      this.obj.style.opacity = opacity;
      this.obj.style.filter = 'alpha(opacity = ' + (opacity*100) + ')';

      var self = this;
      self.fadeTimeoutId = window.setTimeout(function() { self.fade(startTick); }, 75);
      
    }
 
    this.clear_display_callback = function()
    {
      var self = this;
      if (self.fadeTimeout > 0)
        return function() { self.fade(); };
      else
        return function() { self.hide(); };
    }
  }  

}; // XAM.Messages






