﻿/*
==================================================================
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;
}

/*
==================================================================
Cancels event bubbling. Only tested on Firefox 1.5.0.4 and IE6SP1
==================================================================
*/
function CancelEvent(e)
{
    e.cancelBubble = true;
    e.returnValue = false;
    if (e.preventDefault) e.preventDefault();
}

/*
==================================================================
Checks if value is an integer. e.g. (100, -45, +1)
==================================================================
*/
function isInteger(value)
{
     var result = /^[-\+]{0,1}\d+$/.test(value);
     return result;
}

/*
==================================================================
Checks if value is an unsinged integer. e.g. (100, 45, 1)
==================================================================
*/
function isUInteger(value)
{
     var result = /^\d+$/.test(value);
     return result;
}

/*
==================================================================
Sets the status alert message and flashes the status alert container.
==================================================================
*/
function StatusAlert(msg)
{
    document.getElementById('status').innerHTML = msg;
    document.getElementById('status_container').style.backgroundColor = "#ff0";
    setTimeout("document.getElementById('status_container').style.backgroundColor='#fff'", 1000);
}

/*
==================================================================
Fires the Click event of the Buttton 'btn' when the Enter key is
depressed.
==================================================================
*/
function DoButtonClick(btn, e) 
{    
	if (document.all)
	{
		if (e.keyCode == 13)
		{
		    CancelEvent(e);
			btn.click();
		}
	}
	
	else if (document.getElementById)
	{	
		if (e.which == 13)
		{
			CancelEvent(e);
			btn.click();
		}	
	}
	
	else if(document.layers)
	{	
		if(e.which == 13)
		{
			CancelEvent(e);
			btn.click();
		}	
	}
}

/*
==================================================================
Dictionary() : Javascript implementation of a dictionary object
==================================================================
It supports three methods:
    Add(key, value) - Adds a key/value pair to the collection.
    Remove(key) -  Removes an item from the collection.
    Length() - Returns the number of items in the collection.
==================================================================
*/
function Dictionary()
{
	var _count = 0;
	
	this.Add = function(key, value)
	{
		this[key] = value;
		_count++;
	};
			
	this.Remove = function(key)
	{
		if (_count == 0) return;
		
		if (this[key])
		{		
		    this[key] = null;
		    _count--;
		}
	};
	
	this.Length = function()
	{ 
	    return _count;
	};
}


/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}

/*
==================================================================
Finds the X-position of an element positioned relatively on screen
==================================================================
*/
function findPosX(obj)
{
	var curleft = 0;
	
	if (obj.offsetParent) 
	{
		while (obj.offsetParent) 
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
		
	return curleft;
}

/*
==================================================================
Finds the X-position of an element positioned relatively on screen
==================================================================
*/
function findPosY(obj) 
{
	var curtop = 0;
	
	if (obj.offsetParent) 
	{
		while (obj.offsetParent) 
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
		
	return curtop;
}


function Cookies()
{
    this.SetCookie = function(key, value, TTL)
    {
        var expires = "";
                                
        if (isInteger(TTL))
        {
            iTTL = parseInt(TTL);
            if (iTTL != 0)
            {
                var expDate = new Date();
                expDate.setTime(expDate.getTime() + iTTL);
                expires = ";expires=" + expDate.toUTCString();
            }
        }
        
        document.cookie = key + "=" + value + expires + ";path=/";
    };
    
    this.Cookie = function (key)
    {
        var ca = document.cookie.split(';');
        var value = null;
        	    
	    for(i=0;i < ca.length;i++) 
	    {	        
		    var cookie = ca[i].split('=');		    
		    if (Trim(cookie[0]) == key)
		        value = cookie[1];		    
	    }
	    
	    return value;
    };
    
    this.DeleteCookie = function(key)
    {
        this.SetCookie(key,"",-1000);
    };
}

/*ScrollOffset
================================================================================
Returns the horizontal and vertical scroll position of the browser window
================================================================================
*/
function GetScrollOffset()
{
  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};
};
//ScrollOffset======================================================


function RecordScrollOffset()
{
    var scrollOffset = GetScrollOffset();
    $("__scrollOffset").value = (scrollOffset.x + "|" + scrollOffset.y);
}