function cmxUri(uri)
{
	var u = new String(uri);
	var protocolEnd = u.indexOf("://");
	
	//strip trailing "/" if applicable
	if (u.indexOf("/", protocolEnd + 3) == u.length - 1)
		u = u.substr(0, u.length - 1);

	var tldnEnd = (u.indexOf("/", protocolEnd + 3) > -1) ? u.indexOf("/", protocolEnd + 3) : u.length;
	var host = new String(u.substring(protocolEnd + 3, tldnEnd));
	var domainParts = new Array();
	domainParts = host.split(".");
	var qStart = (u.indexOf("?", protocolEnd + 3) > -1) ? u.indexOf("?", protocolEnd + 3) : u.length;
	var query = u.substring(qStart + 1);
	
	this.absolutePath = u.substring(tldnEnd, qStart);
	this.absoluteUri = u.substring(0, qStart);
	this.host = host;
	this.query = query;
	this.pathAndQuery = (this.query != '') ? this.absolutePath + "?" + this.query : this.absolutePath;
	this.protocol = u.substr(0, protocolEnd);
	this.tldn = domainParts[domainParts.length - 1];
	this.isValidUri = (this.protocol != "") && (this.host != "");
	
	this.queryString = new QueryString(query);
				
	function QueryString(qs)
	{
		this.keys = new Array()
		this.values = new Array()

		var query = new String(qs);
		var pairs = query.split("&");

		for (var i = 0; i < pairs.length; i++)
		{
			var pos = pairs[i].indexOf('=');
			if (pos >= 0)
			{
				var argname = pairs[i].substring(0, pos);
				var value = pairs[i].substring(pos + 1);
				this.keys[this.keys.length] = argname;
				this.values[this.values.length] = value;		
			}
		}

		this.item = function(key)
			{
				for (var i = 0; i < this.keys.length; i++)
					if (this.keys[i] == key)
						return this.values[i];

				return null;
			};
		
		this.containsKey = function(key)
			{
				for (var i = 0; i < this.keys.length; i++)
					if (this.keys[i] == key)
						return true;
				
				return false;
			};
	}
}function cmxRedirector()
{
	var cmxuri = new cmxUri(document.location.href);
	
	this.redirectOnHost = function(host, redirectUrl)
		{
			var r = new cmxUri(redirectUrl);
			var h = new String(cmxuri.host);

			if ((h.indexOf(host) > -1) && (host != r.host))
				this.redirect(redirectUrl);
		};

	this.redirectOnQueryKey = function(key, redirectUrl)
		{
			if (cmxuri.queryString.containsKey(key))
				this.redirect(redirectUrl);
		};

	this.redirectOnQueryValue = function(key, value, redirectUrl)
		{
			if (cmxuri.queryString.item(key) == value)
				this.redirect(redirectUrl);
		};

	this.redirectOnAbsoluteUrl = function(absoluteUrl, redirectUrl)
		{
			var u = new cmxUri(absoluteUrl);
								
			if (u.isValidUri)
			{
				if ((cmxuri.absoluteUri == u.absoluteUri) && (absoluteUrl != redirectUrl))
					this.redirect(redirectUrl);
			}
			else
				window.status = absoluteUrl + " is not a valid URL";
		};
	
	this.redirectOnUrl = function(url, redirectUrl)
		{
			var u = new cmxUri(url);
								
			if (u.isValidUri)
			{
				var r = new cmxUri(redirectUrl);
				var rp = new String(r.protocol);
				var rh = new String(r.host);
				var up = new String(u.protocol);
				var uh = new String(u.host);
				var cp = new String(cmxuri.protocol);
				var ch = new String(cmxuri.host);
				
				if (((up.toLowerCase() == cp.toLowerCase()) && 
					(uh.toLowerCase() == ch.toLowerCase())) && 
					((up.toLowerCase() != rp.toLowerCase()) || 
					(uh.toLowerCase() != rh.toLowerCase())))
					
					this.redirect(redirectUrl);
			}
			else
				window.status = url + " is not a valid URL";
		};
	
	this.redirect = function(redirectUrl)
		{
			var r = new cmxUri(redirectUrl);
					
			if (r.isValidUri)
				document.location.href = redirectUrl;
			else
				window.status = redirectUrl + " is not a valid URL";
		};
}