urlParams = function(sHRef) {
    this.sHRef = (typeof(sHRef)=='undefined'?new String(window.location):sHRef);
    this.sProtocol = '';
    this.sHost = '';
    this.sHostName = '';
    this.sPort = '80';
    this.sPathName = '';
    this.sHash = '';
    this.sScriptName = '';
    this.sFullPath = '';
    this.aParams = new Array();
    this.readURL();
};

urlParams.prototype = {
    readURL: function() {
        var aTempParms;
        var sPath;
        var nPos;
        var i;

        if ((nPos = this.sHRef.lastIndexOf('#')) != -1) {
            this.sHash = this.sHRef.substr(nPos+1);
            sPath = this.sHRef.substr(0, nPos);
        }
        else {
            this.sHash = '';
            sPath = this.sHRef;
        }

        this.aParams = new Array();
        if ((nPos = sPath.indexOf('?')) != -1) {
            aTempParms = sPath.substr(nPos + 1).split('&');
            sPath = sPath.substr(0, nPos).replace(/\\/g, '/');
            for (i=0; i<aTempParms.length; i++) {
                if (aTempParms[i].substr(0, 4).toLowerCase() == 'amp;') {
                    aTempParms[i] = aTempParms[i].substr(4);
                }
                if ((nPos = aTempParms[i].indexOf('=')) != -1) {
                    this._setParameter(aTempParms[i].substr(0, nPos), aTempParms[i].substr(nPos+1));
                }
            }
        }
        else {
            sPath = sPath.replace(/\\/g, '/');
        }

        if ((nPos = sPath.indexOf('://')) != -1) {
            this.sProtocol = sPath.substr(0, nPos).toLowerCase();
            sPath = sPath.substr(nPos + 3);
            nPos = (this.sProtocol == 'file'?sPath.indexOf('/', 1):sPath.indexOf('/'));
            if (nPos != -1) {
                this.sHost = sPath.substr(0, nPos);
                sPath = sPath.substr(nPos);
                if (this.sProtocol != 'file' && (nPos = this.sHost.indexOf(':')) != -1) {
                    this.sHostName = this.sHost.substr(0, nPos);
                    this.sPort = this.sHost.substr(nPos + 1);
                }
                else {
                    this.sHostName = this.sHost;
                    this.sPort = '';
                }
            }
        }
        if ((nPos = sPath.lastIndexOf('/')) != -1) {
            if (sPath.charAt(0) != '/') {
                this.sPathName = '/' + sPath.substr(0, nPos + 1);
            }
            else
                this.sPathName = sPath.substr(0, nPos + 1);
            this.sScriptName = sPath.substr(nPos + 1);
        }
        else {
            this.sPathName = '/';
            this.sScriptName = sPath;
        }
    },

    setParameter: function(sName, sValue) {
        this._setParameter(sName, sValue);
        this._buildHRef();
    },

    setParameters: function(sParams) {
        var aParams;
        var nPos;
        var i;
        if (typeof(sParams) == 'string') {
            aParams = sParams.split('&');
            for (i=0; i< aParams.length; i++) {
                if ((nPos = aParams[i].indexOf('=')) != -1) {
                    this._setParameter(aParams[i].substr(0, nPos), aParams[i].substr(nPos+1));
                }
            }
            this._buildHRef();
        }
    },

    delParameter: function(sName) {
        var i;
        var sTempName = sName.toLowerCase();

        i = this._findParameter(sTempName);

        if (i !== null) {
            this.aParams.splice(i, 1);
        }
    },

    getParameter: function(sName) {
        var i;
        var aValues = null;
        var sTempName = sName.toLowerCase();

        i = this._findParameter(sTempName);
        if (i !== null) {
            aValues = this.aParams[i].value;
        }
        return aValues;
    },


    _findParameter: function(sName) {
        var i;

        for (i=0; i<this.aParams.length; i++) {
            if (this.aParams[i].name == sName) {
                break;
            }
         }

        if (i == this.aParams.length)
            i = null;

        return(i);
    },


    _setParameter: function(sName, sValue) {
        var sTempName = sName.toLowerCase();
        var aValues;
        var i;
        var j;

        i = this._findParameter(sTempName);
        if (i === null) {
            i = this.aParams.length;
        }
        if (typeof(sValue) == 'undefined') {
            aValues = new Array();
        }
        else if (typeof(sValue) == 'array') {
            aValues = sValue;
        }
        else {
            aValues = new Array();
            aValues.push(sValue);
        }
        this.aParams[i] = {name:sTempName, value:aValues};
    },

    protocol: function(sP) {
        if (typeof(sP) != 'undefined') {
            this.sProtocol = (sP===null?'':sP);

            this._buildHRef();
        }
        return(this.sProtocol);
    },

    hostName: function(sP) {
        if (typeof(sP) != 'undefined') {
            this.sHostName = (sP===null?'':sP);
            if (this.sPort !== '' && this.sHostName !== '') {
                this.sHost = this.sHostName + ':' + this.sPort;
            }
            else {
                this.sHost = this.sHostName;
            }

            this._buildHRef();
        }
        return(this.sHostName);
    },

    host: function(sP) {
        var nPos;

        if (typeof(sP) != 'undefined') {
            this.sHost = (sP===null?'':sP);
            if ((nPos = this.sHost.indexOf(':')) != -1) {
                this.sPort = this.sHost.substr(nPos+1);
                this.sHostName = this.sHost.substr(0, nPos);
            }
            else {
                this.sPort = '';
                this.sHostName = this.sHost;
            }
            this._buildHRef();
        }
        return(this.sHost);
    },

    fullPath: function() {
        this._buildHRef();
        return(this.sFullPath);
    },

    pathName: function(sP) {
        if (typeof(sP) != 'undefined') {
            this.sPathName = (sP===null?'':sP);
            if (this.sPathName.charAt(0) != '/')
                this.sPathName = '/' + this.sPathName;
            if (this.sPathName.charAt(this.sPathName.length - 1) != '/')
                this.sPathName += '/';

            this._buildHRef();
        }
        return(this.sPathName);
    },

    port: function(sP) {
        if (typeof(sP) != 'undefined') {
            this.sPort = (sP===null?'':sP);
            if (this.sHostName !== '' && this.sPort !== '')
                this.sHost = this.sHostName + ':' + this.sPort;

            this._buildHRef();
        }
        return(this.sPort);
    },

    hash: function(sP) {
        if (typeof(sP) != 'undefined') {
            this.sHash = (sP===null?'':sP);

            this._buildHRef();
        }
        return(this.sHash);
    },

    scriptName: function(sP) {
        if (typeof(sP) != 'undefined') {
            this.sScriptName = (sP===null?'':sP);

            this._buildHRef();
        }
        return(this.sScriptName);
    },

    toString: function() {

        return(this.sHRef);
    },

    _buildHRef: function() {

        var sHRef = '';
        var i, j;
        var sExtra = '?';

        if (this.sProtocol === '' && this.sHost !== '') {
            sHRef = 'http://' + this.sHost;
        }
        else if (this.sProtocol !== '' && this.sHost !== '') {
            sHRef = this.sProtocol + '://' + this.sHost;
        }

        sHRef += this.sPathName;
        this.sFullPath = sHRef;

        if (this.sScriptName !== '') {
            sHRef += this.sScriptName;
        }
        if (this.aParams.length !== 0) {
            for (i=0; i<this.aParams.length; i++) {
                for (j=0; j<this.aParams[i].value.length; j++) {
                    sHRef += sExtra + this.aParams[i].name + '=' + this.aParams[i].value[j];
                    sExtra = '&';
                }
            }
        }
        if (this.sHash !== '')
            sHRef += '#' + this.sHash;
        this.sHRef = sHRef;
    }
};

var docParams = new urlParams();

