/*
	VERSION 1.11

	Last updated: Friday, September 14, 2001 1:12 PM

	Library to create custom url's and querystrings
*/
function url(address) {
	// PROPERTIES
	this.popup = false;					// use popupwindow instead of link
	this.address = address;				// holds address 
	this.args = new Object();			// holds name value pairs

	// METHODS
	this.setAddress = setAddress;			// set address
	this.addOption = addOption;			// adds option to querystring
	this.setOption = setOption;			// does same
	this.deleteOption = deleteOption;	// delete option from querystring
	this.deleteAllOptions = deleteAllOptions;	// delete all options
	this.getUrl = getUrl;					// construct url will all options
	this.getArgsCount = getArgsCount;	// get number of arguments in query string
	this.scan = scan;

	this.scan(address);
}

// parse address for options already in url
function scan(address) {
	// split address in array delimited by & and ?
	var re = /[&?]/g;
	var ar = address.split(re);
	if (ar.length > 0) {
		this.address = ar[0];
	}
	// now split up in name, values
	re = /=/;
	for (var i=1;i<ar.length;i++) {
		var opt = ar[i].split(re);
		var name = opt[0];
		var value = "";
		if (opt.length > 1) {
			value = opt[1];
		}
		// add name and values
		this.addOption(name, value);
	}
}

function setPopup(name) {
	this.popup = name;
}

function setAddress(address) {
	this.address = address;
}

function addOption(name, value) {
	this.setOption(name, value);
}

function setOption(name, value) {
	if (value+"" != "undefined" && value+"" != "" && value+"" != "null") {
		this.args[name] = value;
	}
}

function deleteOption(name) {
	delete this.args[name];
}

function deleteAllOptions() {
	// deletes all options by creating reference to new object.
	this.args = new Object();
}

/*
	Construct valid url with querystring from args object
*/
function getUrl() {
	var counter = 0;
	var qs = this.address;
	for (key in this.args) {
		// test for first element
		qs += (counter == 0) ? "?" : "&";
		// encode argument
		qs += key+"="+escape(this.args[key]);
		counter++;
	}
	return qs;
}

function getArgsCount() {
	var counter = 0;
	for (key in this.args) {
		counter++;
	}
	return counter;
}
