/* ------------------------------------------------------------------------
 * Util
 * ------------------------------------------------------------------------
 * Set of JS functions that help in the every day life of this site.
 * ------------------------------------------------------------------------
 * TELUS
 * Copyright 2006
 * Vancouver, BC
 * 604.801.5758
 * esolutions@telus.com
 * ------------------------------------------------------------------------
 * /

/*  ------------------------------------------
	findURI
	------------------------------------------ 
	Is a function that strips out just the URI (strips out the
	querystring) and only prepends "x" amount of the parent path.
	If "x" is -1, then the full path minus the host will be returned.
	Drive letters are not supported.
	------------------------------------------ */
	function findURI(url, parentLength) {
		var result = "";
		var urlList = url.split("?");

		// If nothing is supplied, return nothing.
		if (urlList.length <= 0) return result;
		
		// If asking for full virtual path.
		if (parentLength < 0) {
			var protocolIndex = url.indexOf("://");
			
			if (protocolIndex >= 0) {
				url = url.substr(protocolIndex + 3);

				var rootIndex = url.indexOf("/");
				if (rootIndex >= 0) {
					result = url.substr(rootIndex);
				} // if
			
			} else {
				result = url;
			} // if 

		} else {
			// Otherwise get it as normal.
		
			var url = urlList[0];
			var uriList = url.split("/");
			result = uriList[uriList.length - 1];

			if (parentLength > 0) {
				var uriCount = uriList.length - 2;

				while (parentLength > 0 && uriCount >= 0) {
					result = uriList[uriCount] + "/" + result;
					uriCount--;
					parentLength--;
				}
			} // if
		} // if

		return result;
	} // findURI


/*  ------------------------------------------
	isArray
	------------------------------------------ 
	Returns true if input is array.  "Borrowed" from:
	http://www.planetpdf.com/developer/article.asp?ContentID=6383
	------------------------------------------ */

	function isArray()
	{
	  if (typeof arguments[0] == 'object')
	  {  
		var criterion = arguments[0].constructor.toString().match(/array/i);
	   return (criterion != null);  
	  }
	  return false;
	}


/*  ------------------------------------------
	isURIMe
	------------------------------------------ 
	Finds out if the incoming URI is the same as the testing
	one up to the parent path length.
	------------------------------------------ */

	function isURIMe(incomingURI, parentLength) {
		var bResult = false;

		// find me.
		var meURI = findURI(location.href, parentLength);

		// find other URI.
		var testURI = findURI(incomingURI, parentLength);

		if (meURI.toLowerCase() == testURI.toLowerCase()) {
			bResult = true;
		} // if

		return bResult;

	} // isURIMe

