/*$
================================================================================
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.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 };
}