/*
    This small class helps with manipulating tokenized URLs on the client. The 
    URL format is expected to match that created by the LinkFactory.cs code. The
    tokens are expected to be "xSOMETHINGx".
*/    
function LinkFactory()
{
	this.Init = function(id)
	{
	    this._path = "";
	    this._qstring = "";
	}
	
	this.ReplaceToken = function(token, value)
	{
	    // Create a regular expression out of the token to be replaced
        var re = new RegExp(token, "g");
        
        if(isNaN(value))
        {
            // The "cleaning done on the token values needs to be kept in synch with that done by LinkFactory.cs code.
            
            // Clean the value going into the keywords/path portion.
            pathValue = value.replace(/[\.\*\+ /\-]/g, "_");
            pathValue = pathValue.replace(/[""'&]/g, "");
            pathValue = pathValue.replace(/_+_/g, "-");
            this._path = this._path.replace(re, encodeURIComponent(pathValue));
            
            // Clean the value and replace in the query string.
            this._qstring = this._qstring.replace(re, encodeURIComponent(value.replace(/[_]/g, "~")));
        }
        else
        {
            this._path = this._path.replace(re, value);
            this._qstring = this._qstring.replace(re, value);
        }
	
	}
	
	this.FinalizeUrl = function()
    {
            // Cleanup URL

            // Cleanup remaining tokens in the parm list section.
            this._qstring = this._qstring.replace(/[a-zA-Z0-9]*_x\w*x_?/g, "");
            
            // Cleanup remaingin tokens in the keyword section of the path
            this._path = this._path.replace(/-x\w*x/g, "");
            this._path = this._path.replace(/x\w*x-/g, "");
            
            return this._path + "?" + this._qstring;
    }

}
