// ADAP javaScript functions
// to do useful stuff such as getting request parameters, and cookies etc.

ADAP = {};
	
//	this.param = param;
//	this.dp = dp;
//	this.cookie = cookie;
//	this.toNumber = toNumber;
	


	ADAP.param = function(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 "";
	};


	ADAP.toNum = function(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;
	};



	ADAP.dp = function(val) {
		var val = ADAP.toNum(val || 0);
		var decimalPlaces = arguments[1];
		var debug = arguments[2] || 0;
		var recursing = arguments[3] || false;
		
		if (decimalPlaces == null) {
			decimalPlaces=2;	
		}
		
		decimalPlaces+=2;
		
		if (debug) {
			alert("dp()ing to " + decimalPlaces + "points");
			alert("dp " + val);
		}
		
		val = ADAP._round(val, decimalPlaces, debug);	// actually get the right value
		
		if (debug) {
			alert("rounded:" + val);	
		}
		val += ''; // make us a string;
	//	alert("val:" + val + "    decimal:"+decimalPlaces);
		
		if (decimalPlaces > 0) {	
			if (val.indexOf('.') == -1) {val = val + "."};	// add a dot if there isn;t one, so we don't get stuck in a loop
			var count=0;
			while ((count < 10) && val.charAt(val.length - decimalPlaces -1) != '.') {
				val = val + "0";
				count++;
			}
		}
		
		if (!recursing) {
			if (debug) {
				alert("going back to do round 2" + decimalPlaces);	
			}
			
			val = ADAP.dp(val, decimalPlaces-4, debug, true);	
		}
		
		return val;	
	};
	
	ADAP._round = function(n, places, debug) {
		var factor = Math.pow(10, (places || 0));
		if (debug) {
			alert("factor:" + factor);
		}
		var huge = (n * factor) + (n < 0 ? -1 : 1) * 0.5;
		if (debug) {
			alert("huge:" + huge);	
		}
		return parseInt(huge) / factor;
	};


	
	ADAP.cookie = function(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;
	};
	
	ADAP.selectSelectOptions = function(selectElementId, valuesToSelect) {
			// valuesToSelect is an array of values
			var selectElement = document.getElementById(selectElementId);
			
			// turn everything off first
			for (loop=0; loop<selectElement.options.length; loop++) {
				selectElement.options[loop].selected=false;
			}
			
			for (var valIndex=0; valIndex<valuesToSelect.length; valIndex++) {
				for (loop=0; loop<selectElement.options.length; loop++) {
					if (selectElement.options[loop].value == valuesToSelect[valIndex]) {
						selectElement.options[loop].selected=true;
					}
				}
			}
		};
	
	ADAP.popUp = function(url, width, height) {
				var ok=true;
				if (!url) {
					alert("Error: No URL supplied")
					ok =false;
				}
				
				if (ok) {
					var windowname = Math.random()
					newWin = window.open(url, windowname, "toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes,width=" + width + ",height=" + height);
					newWin.focus();
				}
		};
		
		

	ADAP.formatDataSize = function(bytes) {

		if (bytes < 1024) {
			return bytes + "b";
		} else if (bytes < 1048576) {
			return Math.floor(bytes / 1024) + "K";
		} else {
			return ADAP.dp(bytes / 1024 / 1024,2) + "MB";
		}
	};







