// Uses the title attribute of elements with the class of 'has_default' to apply a default value
// Currently, the onfocus and onblur are overwritten to provide the functionality.
function setDefaults(){
  $('.has_default').each(function(index, element){
      if(element.value == '') element.value = element.title;
      element.onfocus = function(){if (this.value == this.title) this.value = ''};
      element.onblur = function(){if(this.value == '') this.value = this.title};
    });
}

function removeDefaults(){
  $('.has_default').each(function(index, element){
      if(element.value == element.title) element.value = '' ;
    });
}

// This will convert seconds into a string mmm:ss
function timeString(ms) {
  var sec = Math.floor(ms/1000);
  var min = Math.floor(sec/60);
  sec = sec % 60;
  t = (sec > 9) ? sec : "0" + sec; // Add a preceding 0 if it is less than 10 seconds.
  t = min + ":" + t;

  if(isNaN(min)){
    // min is Not a Number
    return '';
  }
  else {
    return t;
  }
}

function toggleWorking() {
    $('#working').toggle();
}

function togglePassword(){
  var current = $("input[name$='[password]']")
  var id = current.attr('id');
  var name = current.attr('name');
  var value = current.attr('value');
  var style = current.attr('style');
  var type = (current.attr('type')=='password') ? 'text' : 'password'
  var html = '<input type="' + type + '" name="' + name + '" value="' + value + '" style="' + style + '" id="' + id + '"/>';
  current.after(html).remove();
  return $('#' + id) // Returns the new object if needed
}

function toggleCurrent(hide, show){
  $(hide).attr('class', '');
  $(show).attr('class','current');
}