// ADAP javaScript functions
// to do useful stuff such as getting request parameters, and cookies etc.
function Adap() {
	
	this.param = param;
	this.dp = dp;
	this.cookie = cookie;
	this.toNumber = toNumber;
	
}

function param(name) {
	// get a value from the query string
	var query = location.search;
	query=query.replace(/^\?/,"");
	var pairs = query.split("&");
	for (var i =0; i < pairs.length; i++) {
		var bits = pairs[i].split("=");
		if (bits[0] == name) {
			return unescape(bits[1]);
		}
	}
	return "";
}


function toNumber(str) {
	// convert a string into a number;
	// there is probably a better way to do this.
	// we should really use parseFloat and parseInt
	str-=1;
	str+=1;
	
	return str || 0;
}



function dp(num) {
	num = toNumber(num || 0);
	var decimalPlaces = arguments[1] || 2;
	if (decimalPlaces == 0) {return Math.round(num)}
	
	
	return num.toFixed(decimalPlaces);
	alert(decimalPlaces);
	
	var multiplier = Math.pow(10,decimalPlaces);
	if (multiplier < 0) {multiplier =1};
	
	num = Math.round(num*multiplier)/multiplier;	// make an integer, then divide it again to make it a deciaml
	num = num.toString();
	
	if (num.indexOf('.') == -1) {num = num + "."};	// add a dot if there isn;t one, so we don't get stuck in a loop
	alert("num: " + num);
	while (num.indexOf('.') != (num.length - decimalPlaces + 1)) {
		num = num + "0";
	}
	return num;
}


function cookie(name) {
	

	
	// return the values of cookies that may be set in the current document
	var allCookies = document.cookie || "";
	
	var index = allCookies.indexOf(name + "=");
	var start = index + name.length + 1;
	var end = allCookies.indexOf(";", start);
	if (end == -1) end = allCookies.length;
	var value = allCookies.substring(start,end);
	value = unescape(value);

	
	return value;
}
	











