function URLUtil(sURL)
{
this.hostname = (sURL.indexOf("://")==-1?
(/^([^/:\?#]*)(?::|\/|$|\?|#)/.test(sURL)?RegExp["$1"]:""):
(/:\/\/([^/:\?#]*)(?::|\/|$|\?|#)/.test(sURL)?RegExp["$1"]:"")).decode();
this.hash = (/(#.*)/.test(sURL)?RegExp["$1"]:"").decode();
this.protocol = (/^(.*?:)\/\//.test(sURL)?RegExp["$1"]:"").decode();
this.port = (/[^:]*?:(\d{1,5})(\/|$)/.test(sURL)?RegExp["$1"]:"").decode();
this.pathname = (sURL.indexOf("://")==-1?
(/(\/[^#\?]*)/.test(sURL)?RegExp["$1"]:""):
(/:\/\/.*?(\/[^\?#]*)/.test(sURL)?RegExp["$1"]:"")).decode();
this._paramMap = this._parseParam(/\?(.*?)($|#)/.test(sURL)?RegExp["$1"]:"");
}
URLUtil.prototype._parseParam=function(sParams)
{
var paramMap={};
if(sParams.length>0) {
var paramArray = sParams.split("&");
for(var i=0;i<paramArray.length;++i) {
var paramPair=paramArray[i].split("=");
paramPair[0]=paramPair[0].decode();
paramPair[1]=paramPair[1]?paramPair[1].decode():"";
if(!paramMap[paramPair[0]]) {
paramMap[paramPair[0]] = new Array(paramPair[1]);
}
else {
paramMap[paramPair[0]].push(paramPair[1]);
}
}
}
return paramMap;
}
URLUtil.prototype.toString=function()
{
var url="";
if (this.protocol.length!=0) {
url += this.protocol;
url += "//";
}
url += this.hostname;
if (this.port.length!=0){
url += ":"
url += this.port;
}
url += this.pathname;
var pairArray=[];
for (var i in this._paramMap) {
for (var j=0;j<this._paramMap[i].length;++j){
pairArray.push(i.encode()+"="+this._paramMap[i][j].encode());
}
}
if(pairArray.length>0) {
url += "?";
url += pairArray.join("&");
}
url += this.hash;
return url;
}
URLUtil.prototype.getParam=function(sName,sDefaultValue)
{
return this._paramMap[sName]?this._paramMap[sName][0]:sDefaultValue||null;
}
URLUtil.prototype.getMultiParam=function(sName,sDefaultValue)
{
return this._paramMap[sName]?this._paramMap[sName]:sDefaultValue||null;
}
URLUtil.prototype.setParam=function(sName, sValue)
{
this.removeParam(sName).addParam(sName,sValue);
return this;
}
URLUtil.prototype.addParam=function(sName)
{
if(!this._paramMap[sName]) {
this._paramMap[sName] = [];
}
for(var i=1;i<arguments.length;++i) {
this._paramMap[sName].unshift(arguments[i].toString());
}
return this;
}
URLUtil.prototype.removeParam=function(sName)
{
delete this._paramMap[sName];
return this;
}
URLUtil.prototype.removeAllParams=function()
{
this._paramMap = {};
return this;
}
//TEST
var URL = new URLUtil('
https://tower.im/members/e7778d39468f439ab47efd2b3ce822b2/?me=1');