function alertError(e) {
	alert("An error has occured. The error description is:\n" + e.description);
}

function setClass(element, cssClass) {
    try {
        element.className = cssClass;
    } catch(e) {
        alertError(e);
    }
}

function getDocumentElement(elemName) {
	var e;
	if(document.all) {
		e = document.all[elemName];
	} else if(document.getElementById) {
		e = document.getElementById(elemName);
	} else if(document.layers) {
		e = document.layers(elemName);
	}
	return e;
}

// ********** KeyValuePair class **********
function KeyValuePair(key, value) {
	this.Key = key;
	this.Value = value;
}

// Currently only one additional ToString format has been specified ("u") for use in a URL
KeyValuePair.prototype.ToString = function(format) {
	var s = this.Key + "," + this.Value;
	
	switch(format) {
		case "u":
			s = this.Key + "=" + this.Value;
			break;
		default:
			break;
	}
	
	return s;
}

/*
Sample Usage:
		var builder = new UrlBuilder(); // optionally pass a URL to the constructor, else current URL is used
		builder.FindParameter("a").Value = encodeURI("hello");
		alert(builder.ToString());
*/
// ********** UrlBuilder class **********
function UrlBuilder(url) {
	var segments;
	
	if(url == undefined) {
		var href = window.location.href;
		segments = href.split("?");
	} else {
		segments = url.split("?");
	}

	var urlBase = segments[0];
	var queryString = segments[1];
			
	this.UrlBase = urlBase;
	this.Parameters = this.GetQueryStringArray(queryString);
}

// splits the querystring passed into array elements
UrlBuilder.prototype.GetQueryStringArray = function(queryString) {
	if(queryString == undefined) {
		return new Array();
	}

	var pairs = queryString.split("&"); //might use encoded value: %26;
	var keyValuePairs = new Array();
	for(var i = 0; i < pairs.length; i++) {
		var pair = pairs[i].split("=");

		var key = pair[0];
		var value = encodeURI(decodeURI(pair[1])); // decode/encode used to ensure all variables are caught but not double-encoded (eg. %3d doesn't become %253d)
		var kv = new KeyValuePair(key, value);

		keyValuePairs[i] = kv;
	}

	return keyValuePairs;
}

UrlBuilder.prototype.FindParameter = function(key) {
	for(var i = 0; i < this.Parameters.length; i++) {
		if(key == this.Parameters[i].Key) {
			return this.Parameters[i];
		}
	}

	return null;
}

// returns the URL
UrlBuilder.prototype.ToString = function() {
	this.TrimArray();
	
	var queryStringArray = new Array();

	for(var i = 0; i < this.Parameters.length; i++) {
		var pair = this.Parameters[i].ToString("u");
		queryStringArray[i] = pair;
	}

	var queryString = queryStringArray.join("&");

	if(queryString == undefined || queryString == "") {
		return this.UrlBase;
	}

	return this.UrlBase + "?" + queryString;
}

// JavaScript arrays are sparse, this routine removes any gaps
UrlBuilder.prototype.TrimArray = function() {
	for(var i = 0; i < this.Parameters.length; i++) {
		var key = this.Parameters[i].Key;
		if(key == undefined || key == "") {
			this.Parameters.splice(i, 1);
		}
	}
}