defaultMetro = 'nyc';

function E(tag) {
    return document.createElement(tag);
}

function T(text) {
    return document.createTextNode(text);
}

/** String Trim function **/
String.prototype.trim=function(){
    return this.replace(/^\s*|\s*$/g,'');
}

String.prototype.ltrim=function(){
    return this.replace(/^\s*/g,'');
}

String.prototype.rtrim=function(){
    return this.replace(/\s*$/g,'');
}
function hide(el) {
  el.style.display = "none";
}

function show(el) {
    el.style.display = "";
}

// Shows the first element and hides the second
// This can be done better!
function show_hide(object1, object2) {
  a = $( object1 );
  b = $( object2 );
  show(a);
  hide(b);
  //a.style.display = "";
  //b.style.display = "none";
}


function add_option(select, option) {
  try {
    select.add(option, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    select.add(option); // IE only
  }
}

function toggle_display(object) {
  if (object.style.display == "none") {
    object.style.display = "";
  } else {
    object.style.display = "none";
  }
}

// Toggles the display of two elements
function toggle_display_pair(a,b) {
  toggle_display($(a));
  toggle_display($(b));
}


function createFormInput(type, name, value ) {
    var input = null;
    if (ie) {
        input = document.createElement("<INPUT name='"+ name + "'>");
    } else {
        input = document.createElement('INPUT');
        input.name = name;
    }
    input.type = type;
    if (value) {
        input.value = value;
    }
    return input;
}

function setAlerts(message, append) {
    if(append){
        document.getElementById('dynamic_alerts').innerHTML += message;
    }else{
        document.getElementById('dynamic_alerts').innerHTML = message;
    }
}

function handleResponse(data) {
    if (data['message']) {
        setAlerts(data['message']);
    }
}


function trimAll(sString) {
    while (sString.substring(0,1) == ' ') {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ') {
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
}

function blockUser(user_id, action) {
    var answer = confirm("Are you sure you want to " + action + " this user?");
    if (answer) {
        var form = document.createElement('FORM');
        form.method = 'POST';

        form.appendChild(createFormInput('hidden', '_submit_check', 'block_user'));
        form.appendChild(createFormInput('hidden', 'user_id', user_id));
        form.appendChild(createFormInput('hidden', 'block_action', action));

        document.body.appendChild(form);

        form.submit();
    }
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

// Useful DOM stuff

function childWithID(obj, id) {
    var nodes = obj.childNodes;
    for (var i = 0; i < nodes.length; i++) {
        var node = nodes[i];
        if (node.id == id) {
            return node;
        } else {
            var subNode = childWithID(node, id);
            if (subNode) return subNode;
        }
    }
    return null;
}

function parentWithID(obj, id) {
    if (obj.parentNode) {
        if (obj.parentNode.id == id) {
            return obj.parentNode;
        } else {
            return parentWithID(obj.parentNode, id);
        }
    } else {
        return null;
    }
}

  function add_class(className, name) {
      if (className && className.indexOf(name) == -1) {
          className += ' ' + name;
      } else {
          className = name;
      }
      return className;
  }

  function remove_class(className, name) {
      if (className) {
          var index = className.indexOf(name);
          if (index != -1) {
              className = className.substr(0, index) + className.substr(index+name.length);
          }
      }
      return className;
  }

  function array_contains(array, value) {
      if (array == null || array == undefined || array.length == 0) return false;

      for (var i=0; i<array.length; i++) {
          if (array[i] == value) return true;
      }
      return false;
  }

  function array_remove(array, value) {
      if (array == null || array == undefined || array.length == 0) return;

      for (var i=0; i<array.length; i++) {
          if (array[i] == value) array.splice(i, 1);
      }
  }

function createFormOption(text, value, selectedValue) {
    var option = new Option();
    option.text = text;
    option.value = value;
    option.selected = (value == selectedValue);
    return option;
}

function truncate(value, limit) {
  if (value.length > limit) {
    return value.substring(0, limit-2) + '...';
  } else {
    return value;
  }
}

// cookie functions
function setCookie(key, value, expires, path, domain, secure) {
    var cookieString = key + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "; domain=" + hlg_cookie_domain) +
        ((secure) ? "; secure" : "");
    
    document.cookie = cookieString;
}

function getCookie(name) {
    var search = name + "="
    var returnvalue = "";
    var cookieString = document.cookie;
    while (true) {
        if (cookieString.length > 0) {

            offset = cookieString.indexOf(search)
            // if cookie exists
            if (offset != -1) {
                offset += search.length
                // set index of beginning of value

                end = cookieString.indexOf(";", offset);
                // set index of end of cookie value
                if (end == -1) end = cookieString.length;
                returnvalue = unescape(cookieString.substring(offset, end));
                cookieString = cookieString.substring(end, cookieString.length)
            } else {
                break;
            }
        } else {
            break;
        }
    }
    return returnvalue;
}

function submitForm(submit_check, values, target, action) {
    var form = document.createElement('FORM');
    form.method = 'POST';
    if (target) form.target = target;
    if (action) form.action = action;
    
    form.appendChild(createFormInput('hidden', '_submit_check', submit_check));
    for (var i=0; i<values.length; i++) {
        form.appendChild(createFormInput('hidden', values[i][0], values[i][1]));
    }
    
    document.body.appendChild(form);
    
    form.submit();
}

/* fix for $A is not defined on Firefox */
Function.prototype.safeBind = function() {
  var __method = this, args = $A(arguments), object = args.shift();
  return function() {
	if (typeof $A == 'function')
    return __method.apply(object, args.concat($A(arguments)));
  }
}

var data_cache = {}
function get_cache_data(key, callback) {
    if (data_cache[key] == undefined) {
        data_cache[key] = callback();
    }
    return data_cache[key];
}
