/******************************************************************************* 
//Alspa Consulting Corp. Core JavaScript Library
Copyright (c) 2007-2008. ALSPA Consulting Corp. All Rights Reservered.
Author: Michael S. Kolias - mikek@alspaconsulting.com


********************************************************************************/




/*$
================================================================================
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.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.SetOpacity
================================================================================
Sets the opacity of an html element.
================================================================================
*/
AlspaCore.SetOpacity = function(element, value)
{
   if (element)
   {
	   value = value > 100 ? 100 : value;
	   value = value  < 0 ? 0 : value;	
	   var mozOpacity = value / 100;	
	   element.setAttribute("style", "opacity:" + mozOpacity + ";")

	   if (element.style.setAttribute) //For IE
  	    element.style.setAttribute("filter", "alpha(opacity=" + value + ");");
  	}
};
//AlspaCore.SetOpacity==========================================================



/*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=======================================================




//Namespace for the window object related functions
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======================================================


/*AlspaCore.Window.MouseCoords
================================================================================
Finds the X and Y coordinates of an element positioned relatively on screen
================================================================================
*/
AlspaCore.Window.MouseCoords = 
function(e)
{
    var _x = 0;
    var _y = 0;
    
	if(ev.pageX || ev.pageY)
	{
		_x = ev.pageX;
		_y = ev.pageY;
	}
	else
	{
		_x = ev.clientX + document.body.scrollLeft - document.body.clientLeft;
		_y = ev.clientY + document.body.scrollTop  - document.body.clientTop;
	}
	
	return {x:_x, y:_y};
};
//AlspaCore.Window.MouseCoords========================================================


/*AlspaCore.AjaxConnection
================================================================================
XmlHttpRequest wrapper function for asynchronous server requests.
================================================================================
   0 - Uninitialized - open() has not been called yet.
   1 - Loading - send() has not been called yet.
   2 - Loaded - send() has been called, headers and status are available.
   3 - Interactive - Downloading, responseText holds the partial data.
   4 - Completed - Finished with all operations.
   ****************************************************************************/
AlspaCore.AjaxConnection = function()
{
	var _host = "http://localhost"; //The host to submit the request.
	var _method = "POST";		    //Method of the request (POST or GET);
	var _parameters = "";
	
	this.ReadyState = null;
	this.Status = null;

	var xmlhttp;
	var complete = false;
	
	//Try to create the XmlHttp object.

	try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
	
	catch (ex)
	{ 
		try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
		
		catch (ex) 
		{
			try { xmlhttp = new XMLHttpRequest(); }
	
			catch (ex) { xmlhttp = false; }	
		}
		
	}

		if (!xmlhttp) return null; //Failed to create XmlHttp object.
		
		this.Connect = function(fnCallBack)
		{
			if (_method != "POST" && _method != "GET") _method = "POST";

			try
			{
				if (_method == "POST")
				{
					xmlhttp.open(_method, _host, true);
	        		xmlhttp.setRequestHeader("Method", "POST " + _host + " HTTP/1.1");
      			xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				}
				
				else
				{
					var url = _host + "?" + _parameters;				
					xmlhttp.open(_method, url, true);
					_parameters = "";
				}
				
				xmlhttp.onreadystatechange = function()
				{
					switch(xmlhttp.readyState)
					{
						case 1:
							break;
							
						case 2:
							break;
							
						case 3:
							break;
							
						case 4:
							if (!complete) 
							{ 
								complete = true;
								fnCallBack(xmlhttp);
							}
							break;						
					}
				};
				
				 xmlhttp.send(_parameters);
			}
			
			catch(ex) { alert(ex); return false; }
		};
		
		this.SetHost = function(host) { _host = host };
		this.SetParameters = function(params) { _parameters = params };
		this.SetMethod = function(method) { _method = method };
}




















//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//~~~~~~~~~ DEPRECATED Functions ~~~~~~~~~//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//


/*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================================================================
