// WHOA: Writer's Homepage for Obsessing about Amazon
// http://nedbatchelder.com/whoa

function getParams() {
	myurl = location.href;
	var index = myurl.indexOf('?');
	var params = new Object();
	if (index != -1) {
		var nameValuePairs = myurl.substring(index+1, myurl.length).split('&');
		for (var i = 0; i < nameValuePairs.length; i++) {
			nameVal = nameValuePairs[i].split('=');
			params[nameVal[0]] = nameVal[1];
		}
	}
	return params;
}

function makeUrl(params) {
	url = location.href;
	index = url.indexOf('?');
	if (index != -1) {
		url = url.substring(0, index);
	}

	punct = "?";

	for (var i in params) {
		url = url + punct + i + "=" + params[i];
		punct = "&";
	}

	return url;
}

function go() {
	location.href = makeUrl(theParams);
}

function addArgument(arg, value) {
	theParams[arg] = value;
}

function delArgument(arg) {
	delete theParams[arg];
}

function cleanIds() {
	theParams.ids = theParams.ids.replace(/,+/g, ",");
	theParams.ids = theParams.ids.replace(/^,/, "");
	theParams.ids = theParams.ids.replace(/,$/g, "");
}

// Callbacks for buttons

function customize() {
	if (typeof theParams.ids == "undefined") {
		addArgument("ids", theAsins);
	}
	addArgument("customize", "1");
	go();
}

function doneCustomize() {
	delArgument("customize");
	go();
}

function addBooks() {
	asins = document.asinsform.asins.value;
	asins = asins.replace(/[^0-9A-Za-z]+/g, ",");
	theParams.ids = theParams.ids + "," + asins;
	cleanIds();
	go();
}

function removeBook(asin) {
	theParams.ids = theParams.ids.replace(asin, "");
	cleanIds();
	go();
}

theParams = getParams();

