/**
 * Request parameter parser.
 * Mimics some of the functionality of the HTTPRequest class in Java.
 * @author Ed Jenkins
 * Usage:
 * var request = new Request();
 * var foo = request.getParameter("foo");
 */
var Request = Class.create();

/**
 * Request prototype.
 */
Request.prototype =
{

    /**
     * An array of parameters (name/value pairs).
     * It's like a List<Map<String,String>>.
     */
    parameters: [],

    /**
     * Constructor.
     * Parses the current URL to get a list of map of request parameters.
     */
    initialize: function()
    {
        // Get current URL.
        var url = window.location.href;
        // Split into an array of 2 strings (before and after the "?").
        var url_args = url.split(/\?/);
        // Stop parsing if there are no request parameters.
        if(url_args.length != 2)
        {
            return;
        }
        var args = url_args[1];
        if(args == "")
        {
            return;
        }
        // Split request parameters part of URL into an array of strings.
        var a = args.split(/&/);
        // Loop through the array of parameters.
        var x = 0;
        var y = a.length;
        var z = 0;
        for(x=0; x<y; x++)
        {
            // Get a parameter in "name=value" format.
            var o = a[x];
            // Translate into an array of name/value pairs.
            var p = o.split(/=/);
            // Skip if there is a name with no value.
            if(p.length != 2)
            {
                continue;
            }
            if(p[1] == "")
            {
                continue;
            }
            // Decode values.
            var name = p[0];
            var value = unescape(p[1]);
            var nvp = [name, value];
            // Add the name/value pair to the list of parameters we found.
            this.parameters[z++] = nvp;
        }
    },

    /**
     * Gets all parameters.
     * @return the parameters.
     */
    getParameters: function()
    {
        // Create return variable.
        var r = [];
        // Loop through the list of parameters we gathered earlier.
        var x = 0;
        var y = this.parameters.length;
        for(x=0; x<y; x++)
        {
            // Get a name/value pair.
            var nv = this.parameters[x];
            // Make a copy.
            var name = new String(nv[0]);
            var value = new String(nv[1]);
            var nvp = [name, value];
            r[x] = nvp;
        }
        // Return result.
        return r;
    },

    /**
     * Gets a parameter value.
     * @param name the name of the parameter to get.
     * @return the value of the named parameter or null if not found.
     */
    getParameter: function(name)
    {
        // Verify parameters.
        if(name == null)
        {
            return null;
        }
        // Loop through the list of parameters we gathered earlier.
        var value = null;
        var x = 0;
        var y = this.parameters.length;
        for(x=0; x<y; x++)
        {
            // Get a name/value pair.
            var nvp = this.parameters[x];
            // See if the name matches the one we're looking for.
            if(nvp[0] == name)
            {
                // If it does, save it and stop searching.
                value = nvp[1];
                break;
            }
        }
        // Return result.
        return value;
    }

}
