﻿/******************************************************************************* 
ALSPA JavaScript Core Library
Copyright (c) 2006. ALSPA Consulting Corp. All Rights Reservered.
Author: Michael S. Kolias - mikek@alspaconsulting.com

///COMMENTS
AlspaCore.GetX and AlspaCore.GetY are deprecated. Use AlspaCore.GetCoords instead.
********************************************************************************/


/*$
================================================================================
Quick getElement reference
================================================================================
*/
function $() 
{
	var elements = new Array();
	
	for (var i = 0; i < arguments.length; i++)
	{
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
			
		if (arguments.length == 1)
			return element;
			
		elements.push(element);
	}
	
	return elements;
}
//$=============================================================================


//Alspa Consulting Corp. core JavaScript library global namespace
var AlspaCore = function() {};


/*AlspaCore.CancelEvent
================================================================================
Cancels event bubbling
================================================================================
*/
AlspaCore.CancelEvent = 
function(e)
{		
	e = e || window.event; // Reference to the event
	if (!e) return; // If event is undefined return
	
	// Cancel event bubbling
	e.cancelBubble = true;
	e.returnValue = false;
	if (e.preventDefault) e.preventDefault();
}
//AlspaCore.CancelEvent=========================================================



/*AlspaCore.GetX
/*!DEPRECATED! Use AlspaCore.GetCoords instead *//*
================================================================================
Finds the X coordinate of an element positioned relatively on screen
================================================================================
*/
AlspaCore.GetX = 
function(obj)
{
	var x = 0;
	
	if (obj.offsetParent) 
	{
		while (obj.offsetParent) 
		{
			x += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		x += obj.x;
		
	return x;
}
//AlspaCore.GetX================================================================



/*AlspaCore.GetY
/* !DEPRECATED! Use AlspaCore.GetCoords instead *//*
================================================================================
Finds the Y coordinate of an element positioned relatively on screen
================================================================================
*/
AlspaCore.GetY = 
function(obj) 
{
	var y = 0;
	
	if (obj.offsetParent) 
	{
		while (obj.offsetParent) 
		{
			y += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		y += obj.y;
		
	return y;
};
//AlspaCore.GetY================================================================



/*AlspaCore.Bookmark
================================================================================
Prompts the user to bookmark the url passed to the function
================================================================================
*/
AlspaCore.Bookmark = 
function(title, url) 
{
	if(window.sidebar) //Mozilla, Firefox
	{
		window.sidebar.addPanel(title, url,"");		
	}
	else if (window.opera && window.print) //Opera
	{
		var mbm = document.createElement('a');
		mbm.setAttribute('rel','sidebar');
		mbm.setAttribute('href',url);
		mbm.setAttribute('title',title);
		mbm.click();
	}				
	else if (document.all) //IE
	{
		window.external.AddFavorite(url, title);
	}
};
//AlspaCore.Bookmark============================================================



/*AlspaCore.LTrim
================================================================================
Returns a copy of a string without leading spaces.
================================================================================
*/
AlspaCore.LTrim = 
function(str)
{
   var whitespace = new String(" \t\r\n");
   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) 
	 {
      var i=0, j = s.length;

			//Eat whitespace
      while (i < j && whitespace.indexOf(s.charAt(i)) != -1)
         i++;
							 				 
      s = s.substring(i, j);
   }
	 
   return s;
};
//AlspaCore.LTrim===============================================================



/*AlspaCore.RTrim
================================================================================
Returns a copy of a string without trailing spaces.
================================================================================
*/
AlspaCore.RTrim = 
function(str)
{
   var whitespace = new String(" \t\r\n");
   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) 
	 {
      var i = s.length - 1;
			
			//Eat whitespace
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;

      s = s.substring(0, i+1);
   }

   return s;
};
//AlspaCore.RTrim===============================================================



/*AlspaCore.Trim
================================================================================
Returns a copy of a string without leading or trailing spaces
================================================================================
*/
AlspaCore.Trim = 
function(str)
{
   return AlspaCore.RTrim(AlspaCore.LTrim(str));
};
//AlspaCore.Trim================================================================



/*AlspaCore.SetCookie
================================================================================
Adds a cookie to the cookie collection
================================================================================
*/
AlspaCore.SetCookie = 
function(name, value, duration)
{
    var expires = "";
    duration = parseInt(duration);    

    if (isNaN(duration))
        duration = 0;
        
    if (duration !=  0)
    {
        var date = new Date();
        date.setTime(date.getTime() + (duration*24*60*60*1000));
        expires = ("; expires=" + date.toUTCString());
    }
            
    document.cookie = (name + "=" + value + expires + "; path=/");
};
//AlspaCore.SetCookie===========================================================



/*AlspaCore.GetCookie
================================================================================
Retreives a cookie from the cookie collection
================================================================================
*/
AlspaCore.GetCookie = 
function(name)
{
    var cookieNameEx = name + "=";
    var cookieArray = document.cookie.split(";");
   
   for (var i=0; i<cookieArray.length; i++)
   {
        var cookie = cookieArray[i];
        while (cookie.charAt(0) == " ")
            cookie = cookie.substring(1, cookie.length);
            
        if (cookie.indexOf(cookieNameEx) == 0)
            return cookie.substring(cookieNameEx.length, cookie.length);
   }
   
   return null;          
};
//AlspaCore.GetCookie===========================================================



/*AlspaCore.DeleteCookie
================================================================================
Delete a cookie from the cookie collection
================================================================================
*/
AlspaCore.DeleteCookie = 
function(name)
{
   AlspaCore.SetCookie(name, "", -1);   
};
//AlspaCore.DeleteCookie========================================================



/*AlspaCore.GetCoords
================================================================================
Finds the X and Y coordinates of an element positioned relatively on screen
================================================================================
*/
AlspaCore.GetCoords = 
function(e)
{
	var left = 0;
	var top  = 0;

	while (e.offsetParent)
	{
		left += e.offsetLeft;
		top  += e.offsetTop;
		e = e.offsetParent;
	}

	left += e.offsetLeft;
	top  += e.offsetTop;

	return {x:left, y:top};
};
//AlspaCore.GetCoords=======================================================





AlspaCore.Window = {};


/*AlspaCore.Window.InnerDimensions
================================================================================
Returns the inner dimensions of the browser window.
================================================================================
*/
AlspaCore.Window.InnerDimensions = 
function()
{
   var w = 0;
   var h = 0;   

   if (window.innerWidth)
   {
      //Non-IE
      w = window.innerWidth;
      h = window.innerHeight;
   }
   else if(document.documentElement && document.documentElement.clientHeight)
   {
      //IE 6+ in 'standards compliant mode'
      w = document.documentElement.clientWidth;
      h = document.documentElement.clientHeight;
   }
   else if (document.body && document.body.clientHeight)
   {
      //IE 4 compatible
      w = document.body.clientWidth;
      h = document.body.clientHeight;
   }
   
   
   return {width:w, height:h};
};
//AlspaCore.Window.InnerDimensions=============================================



/*AlspaCore.Window.ScrollOffset
================================================================================
Returns the horizontal and vertical scroll position of the browser window
================================================================================
*/
AlspaCore.Window.ScrollOffset = 
function() 
{
  var _x = 0, _y = 0;
  
  if(typeof( window.pageYOffset ) == 'number') 
  {
    //Netscape compliant
    _y = window.pageYOffset;
    _x = window.pageXOffset;
  } 
  else if(document.body && ( document.body.scrollLeft || document.body.scrollTop )) 
  {
    //DOM compliant
    _y = document.body.scrollTop;
    _x = document.body.scrollLeft;
  } 
  else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) 
  {
    //IE6 standards compliant mode
    _y = document.documentElement.scrollTop;
    _x = document.documentElement.scrollLeft;
  }
  
  return {x:_x, y:_y};
};
//AlspaCore.Window.ScrollOffset======================================================