
String.prototype.trim = function(chars) {
	return this.ltrim(chars).rtrim(chars);
}

String.prototype.ltrim = function(chars) {
	chars = chars || "\\s";
	return this.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

String.prototype.rtrim = function(chars) {
	chars = chars || "\\s";
	return this.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function distanceBetweenPoints(p1, p2) {
  if (!p1 || !p2) {
    return 0;
  }

  var R = 6371; // Radius of the Earth in km
  var dLat = (p2.lat() - p1.lat()) * Math.PI / 180;
  var dLon = (p2.lng() - p1.lng()) * Math.PI / 180;
  var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(p1.lat() * Math.PI / 180) * Math.cos(p2.lat() * Math.PI / 180) *
    Math.sin(dLon / 2) * Math.sin(dLon / 2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  var d = R * c;
  return d;
};

if(!Array.indexOf){
  Array.prototype.indexOf = function(obj){
    for(var i=0; i<this.length; i++){
      if(this[i]==obj){
          return i;
      }
    }
    return -1;
  }
}


Array.prototype.randomize = function() {
  return this.sort(function() {return 0.5 - Math.random()})
}

Array.prototype.flatten = function(s1, s2) {
  if (s2 == undefined) {
    return this.join(s1);
  }

  if (this.length <= 2) {
    return this.join(s2);
  }

  var last = this.pop();

  str = this.join(s1) + s2 + last;
  this.push(last);
  return str;
}



