/**
 * Utils class holds a bunch of functions that are useful for general
 * javascripting.  Stuff that helps with browser compatibility and location
 * of the mouse, objects and other things like that
 * Later I'd expect to integrate this class with prototype.js.  That will
 * probably allow me to elimate some functions and piggy back on their work
 * and not have to worry about staying as up to date with browser versions
 * as much.
 * 
 * Copyright Peter M. Laurina 2008
 * The use or modification of this property
 * is strictly prohibited unless with written
 * permission from myself, the author.  For
 * questions, please contact me via email at
 * pete at zionvier dot com 
 */

var isIE = navigator.userAgent.indexOf("MSIE") >=0;
var isIE7 = navigator.userAgent.indexOf("MSIE 7.") >=0;

function Utils() {}

Utils.iconPath = "/xeno/xeno/images/";
/**
 * Function for getting the object the activated an event from the event object
 * without having to worry about browser compatibility everytime
 * 
 * @param event an event object
 * @returns the object which fired the event
 */
Utils.getObjFromEvent = function( event )
{
    if (isIE)
      var obj = window.event.srcElement;
    else
      var obj = event.target;
    
    return obj;
}

Utils.stopEvent = function ( event )
{
     if (isIE) 
     {    
         window.event.cancelBubble = true;    
         window.event.returnValue = false;  
     }  
     else
     {
         event.preventDefault();
         event.stopPropagation();
     }
}

Utils.hasClassName = function(obj, CLASSNAME)
{
  if (obj.className == null || obj.className.indexOf( CLASSNAME ) < 0)
      return false;
  else 
    return true;
}
Utils.addClassName = function(obj, CLASSNAME)
{
    if (obj == null || Utils.hasClassName(obj,CLASSNAME) )
      return;
    if (obj.className == null)
      obj.className = CLASSNAME;
    else
      obj.className += " " + CLASSNAME;
      //obj.className = CLASSNAME + " " + obj.className;
}
Utils.removeClassName = function(obj, CLASSNAME)
{
    if ( obj == null || obj.className==null )
        return;

    var tokens = obj.className.split(" ");
    var buf = "";
    for (var i=0; i < tokens.length; i++)
       if ( tokens[i] != CLASSNAME)
            buf += " " + tokens[i];
    
    obj.className = buf;

}

Utils.hideDiv = function(divID)
{
    var obj = document.getElementById(divID);
    if (obj != null) 
        obj.style.display = "none";
}

Utils.showDiv = function(divID)
{
    var obj = document.getElementById(divID);
    if (obj != null) 
        obj.style.display = "block";
}

Utils.getAbsoluteLocation = function(obj)
{
    var loc = {x:0 ,y:0};
    var curr = obj
    while (curr != document)
    {
        loc.x += curr.offsetLeft;
        loc.y += curr.offsetTop;
        curr = curr.parentNode;        
    }
    return loc;
}

Utils.getMetrics = function(obj)
{
    return {x:      obj.offsetLeft,
            y:      obj.offsetTop,
            width:  obj.offsetWidth,
            height: obj.offsetHeight           
           }
}

Utils.getWindowMetrics = function()
{
    if (isIE)
      return{ width: document.body.parentElement.offsetWidth,
              height: document.body.parentElement.offsetHeight  }
    else
      return{ width: window.innerWidth,
              height: window.innerHeight }
}



Utils.showBelow = function(ObjectToMove, MoveBelowObject)
{
    var loc = Utils.getMetrics(MoveBelowObject);
 
    ObjectToMove.style.top = loc.y + "px";
    ObjectToMove.style.left = loc.x + "px";
    ObjectToMove.style.display = "block";
    
}

/**
 * Setup and perform an Ajax request
 * 
 * @param responseHandler   a function name that will handle the response upon return
 * @param action            the action to call into the Xeno Framework
 * @param post              Boolean (TRUE = post, FALSE (or null) = get)
 * @param params            a parameter list to be sent along with the request
*/
Utils.doAJAX = function(responseHandler, action, post, params)
{
      var ajaxRequest;
      try
      {// Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
      }
      catch (e)
      {// Internet Explorer Browsers
        try
        {
          ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
          try
          {
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
          }
          catch (e)
          {// Something went wrong
            alert("Your browser does not support Ajax\nUpgrade your browser or email me with your browser information\nif you feel something else is wrong.");
            return false;
          }
        }
      }
      
      ajaxRequest.onreadystatechange = responseHandler;
      
      var requestLine = "bin/" + action;
      if (post == true)
      {
        ajaxRequest.open("POST", requestLine, true);
        ajaxRequest.send(params);   
      }
      else
      {
        requestLine += "?" + params;
        ajaxRequest.open("GET", requestLine, true);
        ajaxRequest.send(null);   
      }
      
      
    
    
    
      return true;
    
}




///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////// String Functions ////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////


function Strings() {}
Strings.REGEX_ISEMPTY =  /^\s*$/;
Strings.makeUrlPath = function( url1,  url2 )
{
    var path = Strings.rtrim(url1, "\\/") + "/" + Strings.ltrim(url2, "\\/");        
    return path.replace( new RegExp("[/\\\\]","g"), "/" );
    
}
Strings.rtrim = function( text, chars )
{
    //a. Return  if {text} is null or empty
    if ( text==null || text=="" )
        return "";

    //c. Ensure 'text' is a string
    if ( typeof(text) != "string" )
        text = text + "";

    //d. If trim chars is null: return text
    if ( chars==null )
        return text;
                
    //e. Decrease 'end' from the end of the string
    //   until a character NOT IN 'chars' is found or reached 
    //   the begining of the string        
    var end = text.length - 1;
    while( end >= 0 )
    {
        if ( chars.indexOf(  text.charAt(end)  ) < 0 )
            break;
            
        end--;
    }/*while*/

    return text.substring( 0, end+1 ); 
        
}/*rtrim*/    
Strings.ltrim = function( text, chars )
{
    //a. Return  if {text} is null or empty
    if ( text==null || text=="" )
        return "";

    //b. Ensure 'text' is a string
    if ( typeof(text) != "string" )
        text = text + "";

    //c. If trim chars is null: return text
    if ( chars==null )
        return text;
                
    //d. Increment 'start' from the begining of the string
    //   until a character NOT IN 'chars' is found or reached 
    //   the end of the string        
    var start;
    for( start=0; start < text.length; start++)
        if ( chars.indexOf(  text.charAt(start)  ) < 0 )
            break;
        
    return text.substring( start );
        
}
Strings.isEmpty = function( text )
{     
    if ( text==null )
        return true;

    if (  typeof text != "string" )
        return true;

    if ( Strings.REGEX_ISEMPTY.test( text ) )
        return true;
        
    return false;  
      
}
