// Doesn't totally disable the link if the disableClass == "selected"
function enableLink(obj, enable, enableClass, disableClass){
  if(! obj) return;
  var className = null;
  
  if(enable){
    var href = obj.getAttribute("_href_disabled");
    if(href && href != "" && href != null){
      obj.setAttribute('href', href);
    }

    var onclick = obj.getAttribute('onclick')
    if(onclick && onclick != "" && onclick != null){
      obj.setAttribute('onclick', onclick.replace(/^(return false;)+/,""))
    }

    className = enableClass
  }
  else{
    var href = obj.getAttribute("href");
    if(disableClass != "selected" && href && href != "" && href != null){
       obj.setAttribute('_href_disabled', href);
    }
    obj.removeAttribute('href');

    var onclick = obj.getAttribute('onclick')
    if(disableClass != "selected" && onclick && onclick != "" && onclick != null){
      obj.setAttribute('onclick', onclick.replace(/^(return false;)*/, "return false;"))
    }

    className = disableClass
  }

  // set the class if given, or clear if null
  if (className){
    obj.className = className;
  }
  else{
    obj.removeAttribute('class');
  }
  
}

function enable(obj, enable, enableClass, disableClass){
  var className = null;
  
  if(enable){
    obj.removeAttribute('disabled');
    className = enableClass
  }
  else{
    obj.setAttribute('disabled', "");
    className = disableClass
  }

  // set the class if given, or clear if null
  if (className){
    obj.className = className;
  }
  else{
    obj.removeAttribute('class');
  }
}

// serialize a form and send it to a link
function ajax_serialized_form(link, form_or_id) {
  params = $(form_or_id).serialize();
  new Ajax.Request(link, {asynchronous:true, evalScripts:true, parameters:params});
}


Date.padded2 = function(hour) { padded2 = hour.toString(); if (parseInt(hour) < 10) padded2="0" + padded2; return padded2; }
Date.prototype.getPaddedMinutes = function() { return Date.padded2(this.getMinutes()); }
Date.prototype.getPaddedSeconds = function() { return Date.padded2(this.getSeconds()); }
Date.prototype.getAMPMHour = function() { hour=this.getHours(); return (hour == 0) ? 12 : (hour > 12 ? hour - 12 : hour ) }
Date.prototype.getAMPM = function() { return (this.getHours() < 12) ? "am" : "pm"; }

Date.short_months = $w("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
Date.prototype.toBrief = function (options) {
  options = $H({
    with_respect_to: new Date(),
    all_day_time: null
  }).update(options);
  
  output = Date.short_months[this.getMonth()] + " " + this.getDate();
  
  if (this.getFullYear() != options.get('with_respect_to').getFullYear()) 
    output += ", " + this.getFullYear();
    
  if (! this.toString().include(options.get('all_day_time')) )
  {
    output += ", " + this.getAMPMHour();
    if ( (this.getMinutes()!=0) || (this.getSeconds()!=0)) output += ":" + this.getPaddedMinutes();
    if (this.getSeconds()!=0) output += ":" + this.getPaddedSeconds();
    output += " " + this.getAMPM();
  }
  
  return output;
}

function dateStringToBrief(string, options) { return new Date(string).toBrief(options); }

Number.prototype.asPercentage = function() { 
  return Math.round(this * 100) + '%';    
}

Math.log_with_base = function (value, base) {
  return (Math.log(value))/(Math.log(base));
}

Date.parseDbFormat = function(string) {
  var d = string.match(/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/)
  var date = new Date(d[1], d[2] - 1, d[3], d[4], d[5], d[6], 0);
  return date;
}

function number_to_currency(number, options) {
  try {
    var options   = options || {};
    var precision = options["precision"] || 2;
    var unit      = options["unit"] || "$";
    var separator = precision > 0 ? options["separator"] || "." : "";
    var delimiter = options["delimiter"] || ",";
  
    var parts = parseFloat(number).toFixed(precision).split('.');
    return unit + reports.number_with_delimiter(parts[0], delimiter) + separator + parts[1].toString();
  } catch(e) {
    return number
  }
}

function number_with_delimiter(number, delimiter, separator) {
  try {
    var delimiter = delimiter || ",";
    var separator = separator || ".";
    
    var parts = number.toString().split('.');
    parts[0] = parts[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1" + delimiter);
    return parts.join(separator);
  } catch(e) {
    return number
  }
}