/*
  Utility stuff
*/

if (!utils) {var utils = {}}

function contains(a, obj) {
  /* Checks whether an array contains an object.
     Everything I read points to this being the quickest way
  */
  var i = a.length;
  while (i--) {
    if (a[i] === obj) {return true;}
  }
  return false;
}

utils.handle_input_focus = function(event) {
  /* Selects an input on click and, more importantly, deselects it on a second
  click. This will be attached to every input field site wide.
  */
  var inputobj = $j(this);
  
  // if (inputobj.hasClass('__selected')) {
  inputobj.removeClass('__selected').removeClass('virgin')
  
  
  if (inputobj.attr('value').strip().slice(0, 4) == 'e.g.') {
    inputobj.attr('value', '')
    return
  }
  
  inputobj.select().addClass('__selected')
  inputobj.bind('blur', function() {inputobj.removeClass('__selected')})
  
}

function observe_form_element(elemId, frequency, callback, lastValue) {
/**
 * Pretty much ripped this from Prototype,
 * checks to see if an element has changed. If it has, runs callback
 * @param {String} elemId - id of the form element
 * @param {number} frequency - number of seconds for each check
 * @param {function} callback - function to call when elem has different value
 * @param {string|null} lastValue (optional) - used recursively to compare values
 */
	if (typeof lastValue == "undefined") { lastValue = null; }
    var current_value = $j("#"+elemId)[0].value;
    
    if (lastValue != null)
    {
	    if (current_value != lastValue)
	    	{ callback(elemId, current_value); }
	}
    	
    var next_func = function() { observe_form_element(elemId, frequency, callback, current_value); };
    setTimeout(next_func, frequency*1000);
}