function textLimit(field, maxlen) {
  if (field.value.length > maxlen) 
  {
    field.value = field.value.substring(0, maxlen);
    alert('You can only type ' + maxlen + ' characters in this field...  Your input has been trimmed down to fit.');
  } 
}

function validateEmail(email)
{
  var regex = /^[\w\.]+@[\w]+\.[\w\.]+$/;
  
  var match;
  match = regex.exec(email);
  return (match !== null);
}

function parsePath(pathString) // returns "server" "dirname" "basename"
{
  var regex = /^(https?:\/\/[^\\/:*?"<>|]+)?(\/[^\:*?"<>|]+)\/([^\\/:*?"<>|]+)$/;
  
  var matchFilename;
  matchFilename = regex.exec(pathString);
  // Testing "http://brown.fm/content/2007/09/24/7/1.jpg" >>>
  // [0]: http://brown.fm/content/2007/09/24/7/1.jpg
  // [1]: http://brown.fm
  // [2]: /content/2007/09/24/7
  // [3]: 1.jpg
  //
  // Testing "/content/2007/09/24/7/1.jpg" >>>
  // [0]: /content/2007/09/24/7/1.jpg
  // [1]: 
  // [2]: /content/2007/09/24/7
  // [3]: 1.jpg
  
  var explodedPath = {server:matchFilename[1], dirname:matchFilename[2], basename:matchFilename[3]};
  return explodedPath;
}

// From: http://cass-hacks.com/articles/code/js_url_encode_decode/
function URLEncode(clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        while(hexVal.length < 2) hexVal = "0" + hexVal; // added to make "\n" %0A instead of %A.
        output += '%' + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

// From: http://cass-hacks.com/articles/code/js_url_encode_decode/
function URLDecode(encodedString) 
{
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output;
}


// used for getting around the silly 2-click ActiveX / applet thing.
function writeOut(s)
{
  document.writeln(s);
}

function runWhenLoaded(id, fxn)
{
  if(document.getElementById(id))
  {
    fxn();
  }
  else
  {
    setTimeout(function(){runWhenLoaded(id, fxn);}, 10);
  }
}

function getMousePosition(e)
{
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	// posx and posy contain the mouse position relative to the document
	return {"x":posx, "y":posy};
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function isDescendant(parentId, childId)
{
  compareHTML = document.getElementById(parentId).innerHTML.toLowerCase();
  childId = childId.toLowerCase();
  return  compareHTML.match(" id=" + childId + " ") ||    //IE way - removes ""
          compareHTML.match(" id=\"" + childId + "\" ");  //Mozilla way - leaves as-is
}

function getStyle(el, styleProp)
{
	if (el.currentStyle)
		var y = el.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
	return y;
}

function hasClassName(element, className)
{
  var str = element.className;
  var strPadded = " " + str + " ";
  var classNamePadded = " " + className + " ";
  return strPadded.indexOf(classNamePadded) != -1;
}

function removeClassName(element, className)
{
  if(hasClassName(element, className))
  {
    var strPadded = " " + element.className + " ";
    var classNamePadded = " " + className + " ";
    var result = strPadded.replace(classNamePadded, " ");
    result = result.substring(1, result.length - 1); // exclude first and last characters (both spaces)
    element.className = result;
  }
}

function addClassName(element, className)
{
  var str = element.className;
  if (!hasClassName(element, className))
  {
    str += " ";
    str += className;
    element.className = str;
  }
}

function _importNode(importedNode, deep)
{
  var newNode;
  if(importedNode.nodeType == 1) 
  { // Node.ELEMENT_NODE
    newNode = this.createElement(importedNode.nodeName);
    for(var i = 0; i < importedNode.attributes.length; i++)
    {
      var attr = importedNode.attributes[i];
      if (attr.nodeValue != null && attr.nodeValue != '') 
      {
        newNode.setAttribute(attr.name, attr.nodeValue);
      }
    }
    if (typeof importedNode.style != "undefined") newNode.style.cssText = importedNode.style.cssText;
  } 
  else if(importedNode.nodeType == 3) 
  { // Node.TEXT_NODE
    newNode = this.createTextNode(importedNode.nodeValue);
  }
  
  if(deep && importedNode.hasChildNodes())
  {
    for(var i = 0; i < importedNode.childNodes.length; i++) 
    {
      newNode.appendChild( this.importNode(importedNode.childNodes[i], true) );
    }
  }
  
  return newNode;
}

function clearChildren(element)
{
	if ( element.hasChildNodes() )
	{
			while ( element.childNodes.length >= 1 )
			{
					element.removeChild( element.firstChild );       
			} 
	}

}
