var monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var monthNames = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

function padZero(number)
{
	if (arguments.length == 2)
	{
		var len = arguments[1];
		var str = "" + number;
		var strlen = str.length;
		for (var i = 0; i < len - strlen; i++)
		{
			str = "0" + str;
		}
		return str;
	}
	else
	{
		if (number < 10)
		{
			return "0" + number;
		}
		else
		{
			return "" + number;
		}
	}
}

function swapImage(objId)
{
	var imageFile;
	if (arguments.length == 2)
	{
		imageFile = arguments[1];
	}
	else
	{
		imageFile = arguments[1] + arguments[2];
	}
	
	var obj = document.getElementById(objId);
	if (obj != null)
	{
		obj.src = imageFile;
	}
}

function isEmpty(s)
{
	return ((s == null) || (s.length == 0));
}

function isLetter(c)
{
	return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) );
}


function isDigit(c)
{
	return ((c >= "0") && (c <= "9"));
}


function isLetterOrDigit(c)
{
	return (isLetter(c) || isDigit(c));
}

function isInteger(s)
{
	var i;

	if (isEmpty(s))
	{
		return false;
	}

    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if (!isDigit(c))
		{
			return false;
		}
    }

    return true;
}

function replaceAll(oldStr, findStr, repStr)
{
	if (oldStr == null)
	{
		return null;
	}
	
  var srchNdx = 0;  // srchNdx will keep track of where in the whole line
                    // of oldStr are we searching.
  var newStr = "";  // newStr will hold the altered version of oldStr.
  
  while (oldStr.indexOf(findStr, srchNdx) != -1)  
                    // As long as there are strings to replace, this loop
                    // will run. 
  {
    newStr += oldStr.substring(srchNdx, oldStr.indexOf(findStr,srchNdx));
                    // Put it all the unaltered text from one findStr to
                    // the next findStr into newStr.
    newStr += repStr;
                    // Instead of putting the old string, put in the
                    // new string instead. 
    srchNdx = (oldStr.indexOf(findStr, srchNdx) + findStr.length);
                    // Now jump to the next chunk of text till the next findStr.           
  }
  newStr += oldStr.substring(srchNdx, oldStr.length);
                    // Put whatever's left into newStr.             
  return newStr;
}

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;
}

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 getDaysInMonth(year, month)
{
	if (month != 1)
	{
		return monthDays[month];
	}
	else
	{
		return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ? 29 : 28);
	}
}

function getMonthNames()
{
	return monthNames;
}

function enterPressed()
{
	return (window.event.keyCode == 13);
}

function getIndex(field)
{
	var index = -1, i = 0, found = false;
	while (i < field.form.length && index == -1)
	{
		if (field.form[i] == field)
		{
			index = i;
		}
		else
		{
			i++;
		}
	}
	return index;
}

function capitalize(str)
{
	if (str == null)
	{
		return null;
	}
	else
	{
		var newStr = null;
		if (isLetter(str.charAt(0)))
		{
			newStr = str.substr(0, 1).toUpperCase() + str.substr(1, str.length);
		}
		else
		{
			newStr = str;
		}
		return newStr;
	}
}

function toMonetaryString(floatValue)
{
    stringValue = "" + floatValue;
    idx = stringValue.indexOf('.', 0);
    if (idx >= 0)
	{
        stringValue = stringValue.substring(0, idx + 3);
	}
    else
	{
        stringValue = stringValue + ".00";
	}
	
    return stringValue;
}

function formatCurrency(num)
{
	num = num.toString().replace(/\$|\,/g,'');
	if (isNaN(num))
	{
		num = "0";
	}
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100 + 0.50000000001);
	cents = num % 100;
	num = Math.floor(num / 100).toString();
	if (cents < 10)
	{
		cents = "0" + cents;
	}
	for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
	{
		num = num.substring(0, num.length-(4 * i+ 3)) + ',' + num.substring(num.length - (4 * i + 3));
	}
	
	return ((sign ? '' : '-') + '$' + num + '.' + cents);
}

/**
 * Function preloadImages
 *
 * Preload images into the Web browser's cache.  If these images are referred later, such as in a button rollover, they should
 * be presented almost instantaneously, depending on the bandwidth of the connection to the Web server, the Web server's load,
 * and other factors affecting download performance.
 *
 * @param    urls        One or more URLs (Uniform Resource Locators) of the images to preload.
 *                       Example: "../images/img01.gif", "../images/img02.gif", "../images/img03.gif", "../images/img04.gif", 
 */
function preloadImages()
{
	var doc = document;
	if (doc.images)
	{
		if (!doc.preload_images)
		{
			doc.preload_images = new Array();
		}
		var j = doc.preload_images.length;
		var args = preloadImages.arguments;
		for (var i = 0; i < args.length; i++)
		{
			if (args[i].indexOf("#") != 0)
			{
				doc.preload_images[j] = new Image();
				doc.preload_images[j].src = args[i];
				j++;
			}
		}
	}
}

/**
 * Function getWindowSize
 *
 * Get the width and height of the Web browser window.  This function should work in most brands of Web browser.
 *
 * @return   structure   The size of the Web browser window
 *                          'width' is the width of the window in pixels
 *                          'height' is the height of the window in pixels
 */
function getWindowSize()
{
	var myWidth = 0, myHeight = 0;
	if (typeof(window.innerWidth) == 'number')
	{
    	// Non-IE
    	myWidth = window.innerWidth;
    	myHeight = window.innerHeight;
	}
	else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
	{
    	// IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if (document.body && (document.body.clientWidth || document.body.clientHeight))
	{
		// IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	
	var size = new Object();
	size.width = myWidth;
	size.height = myHeight;
	
	return size;
}

/**
 * Function getScrollbarCoordinates
 *
 * Retrieves the coordinates of the browser's scrollbar.
 *
 * @return   structure   The coordinates of the browser's scrollbar
 *                           'x' is the number of pixels the left side of the visible page is from the left edge of
 *                           the browser window.
 *                           'y' is the number of pixels the top of the visible page is from the top of the browser
 *                           window.
 */
function getScrollbarCoordinates()
{
	var x, y;
	if (is_ie)
	{
		if (document.documentElement && document.documentElement.scrollLeft)
		{
			x = document.documentElement.scrollLeft;
		}
		else
		{
			x = document.body.scrollTop;
		}
		
		if (document.documentElement && document.documentElement.scrollTop)
		{
			y = document.documentElement.scrollTop;
		}
		else
		{
			y = document.body.scrollTop;
		}
	}
	else
	{
		x = window.pageXOffset;
		y = window.pageYOffset;
	}
	
	var coords = new Object();
	coords.x = x;
	coords.y = y;
	
	return coords;
}
