var ui_utils = {
	formatDate: function(obj)
	{
		var str = obj.value;
		String.prototype.count = function(match)
		{
			var res = this.match(new RegExp(match,"g"));
			if (res==null) { return 0; }
			return res.length;
		}

		//If dashes were used as separators, convert to slashes...
		str = str.replace("-", "/");

		//If year was left off, append it...
		var now = new Date();
		if(str.count("/") == 1)
			str += "/" + now.getFullYear();

    //Now check for two digit year - assume 2000...
    ar = str.split("/");
    if(ar.length > 2)
    {
      if(ar[2].length == 2) ar[2] = "20" + ar[2];
      str = ar[0] + "/" + ar[1] + "/" + ar[2];
    }

		obj.value = str;
	},

	formatPhone: function(obj)
	{
		var str = obj.value;
		if(/^(?=\b\w{10}\b)\b\w*[\d]\w*\b/.test(str))
		{
			str = "(" + str.substr(0, 3) + ") " + str.substr(3, 3) + "-" + str.substr(6);
		}
		if(/^\d{3}[-\.]\d{3}[-\.]\d{4}/.test(str))
		{
			str = "(" + str.substr(0, 3) + ") " + str.substr(4, 3) + "-" + str.substr(8);
		}

		obj.value = str;
	},

	//This fuction recieves an HTML textbox as input, and changes the case of
	//the text according to the value of the switch parameter, which can be:
	//  1 - capitalizes the first letter of the first word.
	//  2 - capitalizes the first letter of each word.
  capitalize: function(obj, which)
	{
		txt = obj.value;
		if(txt=="") return;

		if(which == 1)
		{
			rmdr = txt.substr(1);
			first = txt.substr(0, 1);
			obj.value = first.toUpperCase() + rmdr;
		}
	},

	showActivityFeedback: function(formname, feedbackDiv)
	{
		try
		{
      if($(formname))
      {
        if($(formname).elements)
        {
          for(i = 0; i < $(formname).elements.length; i++)
          {
            var obj = $(formname).elements[i].disabled='disabled';
          }
        }

        new Effect.Opacity($(formname), {from: 0.0, to: 0.6, duration: 0.5});
      }
      $(feedbackDiv).style.display = "block";
		}
		catch(e)
		{
			alert(e.message);
		}
	},

	hideActivityFeedback: function(formname, feedbackDiv)
	{
		try
		{
      if($(formname))
      {
        new Effect.Opacity($(formname), {from: 0.0, to: 1.0, duration: 0.5});

        if($(formname).elements)
        {
          for(i = 0; i < $(formname).elements.length; i++)
          {
            $(formname).elements[i].disabled='';
          }
        }
      }

      $(feedbackDiv).style.display = "none";
    }
		catch(e)
		{
			alert(e.message);
		}
	},

  showWorking: function(elem, hPos, vPos, img)
  {
    alert("Don't use this!");
    return;

    var x = util.getXCoord(elem);
    var y = util.getYCoord(elem);

    img.style.position = "absolute";
    img.style.display = "inline";
    //img.style.border = "1px solid";

    if(hPos == 'right')
    {
      x += elem.clientWidth + img.clientWidth / 2 + 5;
    }

    if(vPos == 'middle')
    {
      y += elem.clientHeight / 2 - img.clientHeight / 2;
    }

    img.style.top = y + "px";
    img.style.left = x + "px";
  },

  hideWorking: function(img)
  {
    img.style.display='none';
  },

  showMarker: function(x, y, img)
  {
    img.style.position = "absolute";
    img.style.display = "inline";
    img.style.top = y - 5 + "px";
    img.style.left = x - 5 + "px";
  },

  hideMarker: function(img)
  {
    img.style.display='none';
  },

  setPaneMaxHeight: function(left, right)
  {
    if(!left || !right) return;

    //getMaxCoordY returns the absolute position of the bottom of the tallest element, so it must be adjusted to deduct
    //the top of the pane.
    var maxY = util.getMaxCoordY(left, 0);
    //this.showMarker(420, maxY, $('imgMarker'));
    //maxY -= util.getYCoord(left);
    //this.showMarker(420, util.getYCoord(left), $('imgMarker'));
    //this.showMarker(420, util.getMaxCoordY(left, 0), $('imgMarker'));

    //alert(maxY);

    //I have to explicitly remove the padding from the offsetHeight value.
    var nh = Math.max(left.offsetHeight, right.offsetHeight) - 20;
    //alert(left.offsetHeight + "\n" + right.offsetHeight);

    //Set both columns to equal height...
    left.style.display="block";
    left.style.height=nh+"px";

    right.style.display="block";
    right.style.height=nh+"px";
  },

	reload_js: function(name)
	{
		//How to deal with checking if exists??
    if (self.extraFunction) { // Already exists
      return;
    }

    var head = document.getElementsByTagName("head")[0];
		for(i=0; i < head.childNodes.length; i++)
		{
			node = head.childNodes[i];
			if(node && typeof(node.src) != "undefined" && node.src.match(name))
			{
				head.removeChild(node);
			}
		}

		//alert("Loading script " + name);
    script = document.createElement('script');
    script.id = 'extraScript';
    script.type = 'text/javascript';
    script.src = name;
    head.appendChild(script)
  }

}
